kus3000 | Bonjour,
Je voudrais envoyer un paquet TCP via une socket raw à un client.
Voici le code (je n'ai pas mi toutes les déclarations pour que ce soit plus lisible)
Code :
- #define DATAGRAM_SIZE 4096
- csum (unsigned short *buf, int nwords)
- {
- unsigned long sum;
- for (sum = 0; nwords > 0; nwords--)
- sum += *buf++;
- sum = (sum >> 16) + (sum & 0xffff);
- sum += (sum >> 16);
- return ~sum;
- }
- typedef struct iphdr//20 octects
- {
- unsigned char verlen;//1 = version + longueur
- unsigned char tos; //1
- unsigned short tot_len; //2
- unsigned short id; //2
- unsigned short offset; //2
- unsigned char ttl; //1
- unsigned char protocol;//1
- unsigned short checksum; //2
- unsigned int saddr; //4
- unsigned int daddr; //4
- } IP_HDR;
- typedef struct tcphdr
- {
- unsigned short sport;
- unsigned short dport;
- unsigned int seqnum;
- unsigned int acknum;
- unsigned char unused:4, tcp_hl:4;
- unsigned char flags;
- unsigned short window;
- unsigned short checksum;
- unsigned short urgPointer;
- } TCP_HDR;
- ListeningSocketSDP = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
- if (setsockopt(ListeningSocketSDP, IPPROTO_IP, IP_HDRINCL, (char *)&optval, sizeof(optval)) == SOCKET_ERROR)
- {
- printf("[Error] . Function setsockopt() : %d\n", WSAGetLastError());
- WSACleanup();
- exit(-1);
- }
- ServerAddrSDP.sin_family = AF_INET;
- ServerAddrSDP.sin_port = htons(5004);
- ServerAddrSDP.sin_addr.s_addr = inet_addr("192.168.0.115" );
- ClientAddrSDP.sin_family = AF_INET;
- ClientAddrSDP.sin_port = htons(5008);
- ClientAddrSDP.sin_addr.s_addr = inet_addr("192.168.0.104" );
- //construction du faux paquet TCP...
- memset(datagram,0,DATAGRAM_SIZE);
- iph->verlen = 69;//version + longueur sur 1 octect (4 et 20)
- iph->tos = 0;//0x20;//calculer par windows
- iph->tot_len = sizeof(IP_HDR) + sizeof(TCP_HDR);// longueur du datagrame : calculer par win
- iph->id = htons(8306);
- //flags
- iph->offset = 0;
- iph->ttl = 57;
- iph->protocol = IPPROTO_TCP;
- iph->checksum = 0;
- iph->saddr = ServerAddrSDP.sin_addr.s_addr;
- iph->daddr = ClientAddrSDP.sin_addr.s_addr;
- ////paquetTCP = malloc(5 * sizeof(unsigned int));
- tcph->sport = htons(5004);
- tcph->dport = htons(5008);
- tcph->seqnum = htonl(0);
- tcph->acknum = 0;
- tcph->tcp_hl = 20;
- tcph->flags = 0x18;
- tcph->window = htons(6432);
- tcph->checksum = 0;
- tcph->urgPointer = 0;
- iph->checksum = csum ((unsigned short *) datagram, iph->tot_len >> 1);
- if((numbytes = sendto(ListeningSocketSDP,(const char *)datagram, iph->tot_len , 0, (struct sockaddr *)&ClientAddrSDP, ClientAddrLen)) == -1)
- {
- perror("sendto" );
- }
|
numbytes = -1 à la sortie
je ne comprend pas d'ou vient l'erreur. le setsockoption est bon ? j'ai bien le droit de dire de cette manière que je veux un paquet TCP/IP ?
merci à tous ceux qui me répondront |