1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
int TCPRequest()
{
int optval = 1;
struct tcpHeader *tcp;
struct ipHeader *ip;
struct pseudoHeader *psd;
unsigned short packet_size, ip_len;
//Création du socket
SOCKET socketTCP = WSASocket(AF_INET, SOCK_RAW, IPPROTO_RAW, NULL, 0, 0);
if (socketTCP == INVALID_SOCKET)
{
Erreur("Creation Socket TCP");
return -1;
}
if (setsockopt(socketTCP, IPPROTO_IP, 2, (char*)&optval, sizeof(optval)) == SOCKET_ERROR)
{
Erreur("Socket Options : TCP");
return -1;
}
printf("Creation du socket termine ! \n");
//Créations des entêtes
//IP
ip = (struct ipHeader*)malloc(sizeof(struct ipHeader));
memset(ip, 0, sizeof(struct ipHeader));
//Longueur d'entête
ip_len = sizeof(struct ipHeader) / sizeof(unsigned long);
//Remplissage
ip->ip_v = 4;
ip->hl = 5;
ip->tos = 0;
ip->tot_len = htons(sizeof(struct ipHeader) + sizeof(struct tcpHeader));
ip->id = 1;
ip->offset = 0;
ip->ttl = 255;
ip->protocol = IPPROTO_TCP;
ip->saddr = inet_addr("192.168.0.15");
ip->daddr = inet_addr("192.168.0.10");
ip->checksum = 0;
ip->checksum = in_cksum((unsigned short*)ip, sizeof(struct ipHeader));
//TCP
tcp = (struct tcpHeader*)malloc(sizeof(struct tcpHeader));
memset(tcp, 0, sizeof(struct tcpHeader));
//Remplissage
tcp->sport = htons(5000);
tcp->dport = htons(5500);
tcp->seqnum = htonl(1337);
tcp->acknum = htonl(1337);
tcp->dataoffset = (5) << 4;
tcp->flags = 0x02; //Flag SYN
tcp->windows = htons(1337);
tcp->checksum = 0;
tcp->urgpointer = 0;
//Pseudo Header (??)
psd = (struct pseudoHeader*)malloc(sizeof(struct pseudoHeader));
memset(psd, 0, sizeof(struct pseudoHeader));
psd->saddr = inet_addr("192.168.0.15");
psd->daddr = inet_addr("192.168.0.10");
psd->useless = 0;
psd->protocol = IPPROTO_TCP;
psd->length = htons(sizeof(struct tcpHeader));
psd->TCP = *tcp;
tcp->checksum = in_cksum((unsigned short*)psd, sizeof(struct pseudoHeader));
printf("Structures remplies...\n");
//Création du packet
packet_size = sizeof(struct ipHeader) + sizeof(struct tcpHeader);
printf("packet_size: %d\n", packet_size);
char *ptr = NULL, *paquet = NULL;
paquet = (char*)malloc(sizeof(packet_size));
memset(paquet, 0, packet_size);
ptr = paquet;
memcpy(ptr, ip, sizeof(struct ipHeader));
ptr += sizeof(struct ipHeader);
memcpy(ptr, tcp, sizeof(struct tcpHeader));
ptr += sizeof(struct tcpHeader);
//Destinataire
struct sockaddr_in dest;
dest.sin_family = AF_INET;
dest.sin_addr.S_un.S_addr = inet_addr("192.168.0.10");
//Envoie
int resultat = sendto(socketTCP, paquet, packet_size, 0, (struct sockaddr*) &dest, sizeof(struct sockaddr_in));
if (resultat <= 0)
{
Erreur("Erreur envoie TCP");
return -1;
}
printf("%d bytes envoye !\n", resultat);
//Liberez
free(ip);
free(tcp);
free(psd);
//free(paquet);
closesocket(socketTCP);
} |