Probleme Socket Raw

Probleme Socket Raw - C - Programmation

Marsh Posté le 28-11-2007 à 18:57:15    

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 :
  1. #define DATAGRAM_SIZE 4096
  2. csum (unsigned short *buf, int nwords)
  3. {
  4.   unsigned long sum;
  5.   for (sum = 0; nwords > 0; nwords--)
  6.     sum += *buf++;
  7.   sum = (sum >> 16) + (sum & 0xffff);
  8.   sum += (sum >> 16);
  9.   return ~sum;
  10. }
  11. typedef struct iphdr//20 octects
  12. {
  13. unsigned char verlen;//1 = version + longueur
  14. unsigned char tos;   //1
  15. unsigned short tot_len; //2
  16. unsigned short id;      //2
  17. unsigned short offset;  //2
  18. unsigned char ttl;     //1
  19. unsigned char protocol;//1
  20. unsigned short checksum; //2
  21. unsigned int saddr; //4
  22. unsigned int daddr; //4
  23. } IP_HDR;
  24. typedef struct tcphdr
  25. {
  26. unsigned short sport;
  27. unsigned short dport;
  28. unsigned int seqnum;
  29. unsigned int acknum;
  30. unsigned char unused:4, tcp_hl:4;
  31. unsigned char flags;
  32. unsigned short window;
  33. unsigned short checksum;
  34. unsigned short urgPointer;
  35. } TCP_HDR;
  36. ListeningSocketSDP = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  37. if (setsockopt(ListeningSocketSDP, IPPROTO_IP, IP_HDRINCL, (char *)&optval, sizeof(optval)) == SOCKET_ERROR)
  38. {
  39.      printf("[Error] . Function setsockopt() : %d\n", WSAGetLastError());
  40.  WSACleanup();
  41.      exit(-1);
  42. }
  43. ServerAddrSDP.sin_family    =  AF_INET;
  44. ServerAddrSDP.sin_port        =  htons(5004);
  45. ServerAddrSDP.sin_addr.s_addr   =  inet_addr("192.168.0.115" );
  46. ClientAddrSDP.sin_family    =  AF_INET;
  47. ClientAddrSDP.sin_port        =  htons(5008);
  48. ClientAddrSDP.sin_addr.s_addr   =  inet_addr("192.168.0.104" );
  49. //construction du faux paquet TCP...
  50. memset(datagram,0,DATAGRAM_SIZE);
  51. iph->verlen = 69;//version + longueur sur 1 octect (4 et 20)
  52. iph->tos = 0;//0x20;//calculer par windows
  53. iph->tot_len = sizeof(IP_HDR) + sizeof(TCP_HDR);// longueur du datagrame : calculer par win
  54. iph->id = htons(8306);
  55. //flags
  56. iph->offset = 0;
  57. iph->ttl = 57;
  58. iph->protocol = IPPROTO_TCP;
  59. iph->checksum = 0;
  60. iph->saddr = ServerAddrSDP.sin_addr.s_addr;
  61. iph->daddr = ClientAddrSDP.sin_addr.s_addr;
  62. ////paquetTCP = malloc(5 * sizeof(unsigned int));
  63. tcph->sport = htons(5004);
  64. tcph->dport = htons(5008);
  65. tcph->seqnum = htonl(0);
  66. tcph->acknum = 0;
  67. tcph->tcp_hl = 20;
  68. tcph->flags = 0x18;
  69. tcph->window = htons(6432);
  70. tcph->checksum = 0;
  71. tcph->urgPointer = 0;
  72. iph->checksum = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  73. if((numbytes = sendto(ListeningSocketSDP,(const char *)datagram, iph->tot_len , 0, (struct sockaddr *)&ClientAddrSDP, ClientAddrLen)) == -1)
  74. {
  75.  perror("sendto" );
  76. }


 
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 :)

Reply

Marsh Posté le 28-11-2007 à 18:57:15   

Reply

Marsh Posté le 28-11-2007 à 19:58:46    

http://www.frameip.com/forum/94-ra [...] xp-sp2.htm
 
apparemment les socket raw tcp/ip ne fonctionne plus sous xp sp2 !

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed