sendto Problem mit IP_HDRINCL [RAW Sockets]



  • Hallo,
    ich habe ein kleines Problem beim Senden mit RAW Sockets.

    void __fastcall TConnectionTh::setIPHeader(TIPHdr* ipHeader){
      memset(ipHeader, 0x00, sizeof(TIPHdr));
      ipHeader->ip_hl = 5;
    	ipHeader->ip_v = 4;
    	ipHeader->ip_tos = 16;
    	ipHeader->ip_len = htons(40);
    	ipHeader->ip_id = htons(20372);
    	ipHeader->ip_off = htons(0x4000);
    	ipHeader->ip_ttl = 64;
    	ipHeader->ip_p = IPPROTO_TCP;
    	ipHeader->ip_src.s_addr = inet_addr("192.168.1.11");
    	ipHeader->ip_dst = FHost;
      ipHeader->ip_sum = htons(in_cksum( (char *)ipHeader, sizeof(struct TIPHdr)));
    }
    
    void __fastcall TConnectionTh::setTcpHeader(TTcpHdr* tcpHeader, int iTyp, TTCPPseudoHdr* pseudoHdr){
      tcpHeader->th_sport = htons(SRC_PORT);
      tcpHeader->th_dport = htons(FPort);
      tcpHeader->th_seq = htonl(1000000000);
      tcpHeader->th_ack = htonl(1000000000); ;
      tcpHeader->th_x2 = 0;
      tcpHeader->th_off = 0;            //first and only tcp segment
      tcpHeader->th_flags = iTyp;       //initial connection request
      tcpHeader->th_win = htons(1024);
      tcpHeader->th_urp = 0;
      tcpHeader->th_sum = htons(in_cksum( (char *)pseudoHdr, sizeof(struct TTCPPseudoHdr)
                              + sizeof(struct TTcpHdr) ) + 0); //0 => Keine Daten
    }
    
    void __fastcall TConnectionTh::setTcpPseudoHeader(TTCPPseudoHdr* pseudohdr){
    
      pseudohdr->s_addy.s_addr = inet_addr("192.168.1.11");
      pseudohdr->d_addy = FHost;
      pseudohdr->zero = 0x00;
      pseudohdr->protocol = IPPROTO_TCP;
      pseudohdr->length = htons(sizeof(struct TTcpHdr) + 0);  //0 => Keine Daten
    }
    
    int __fastcall TConnectionTh::in_cksum(char *data, int count){
    		/* Compute Internet Checksum for "count" bytes
    		*         beginning at location "addr".
    		*/
    	register long sum = 0;
    
    	while( count > 1 )  {
    		/*  This is the inner loop */
    		sum += * (unsigned short *) data++ ;
    		count -= 2;
    	}
    
    		/*  Add left-over byte, if any */
    	if( count > 0 )
    		sum += * (unsigned char *) data;
    
    		/*  Fold 32-bit sum to 16 bits */
    	while (sum>>16)
    	sum = (sum & 0xffff) + (sum >> 16);
    
    	return ~sum;
    }
    
    bool __fastcall TConnectionTh::sethdrinclude(int sd) {
      int one = 1;
      return(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, (const char *) &one, sizeof(one)) == 0);
    }
    
    unsigned char ucPayload[PACKET_SIZE];
      memset(ucPayload, 0x00, PACKET_SIZE);
    
      TIPHdr* ipbuffer = (struct TIPHdr*)ucPayload;
    
      //Der Pseudoheader liegt im gleichen Speicher wie der IP-Header, um die
      //Berechnung der TCP-Checksumme zu erleichtern
      TTCPPseudoHdr* pseudoHdr = (struct TTCPPseudoHdr*)(ucPayload + sizeof(struct TIPHdr) -
                                  sizeof(struct TTCPPseudoHdr));
    
      TTcpHdr* tcpbuffer = (struct TTcpHdr*)(ucPayload + sizeof(struct TIPHdr));
    
      if(!sethdrinclude(FSock))return;   //Ich will die IP-Header selbst handeln
    
      while(FState != STATE_FINISH){
        switch(WaitForMultipleObjects(EVENT_COUNT, EventList, false, INFINITE)){
          case(WAIT_TIMEOUT	):    FState=STATE_FINISH;break;
          case(WAIT_OBJECT_0):    ResetEvent(ExitEvent);FState=STATE_FINISH;break;   //Exit
          case(WAIT_OBJECT_0+1):  ResetEvent(SleepEvent);FState=STATE_SLEEPING;break;//Sleep
          case(WAIT_OBJECT_0+2):  {
            ResetEvent(ResumeEvent);
            FState=STATE_RUNNING;
            FOpen = false;
            memset(ucPayload, 0x00, PACKET_SIZE);
    
            //use same buffer for pseudohdr (used for crc)
            setTcpPseudoHeader(pseudoHdr);
            setTcpHeader(tcpbuffer, TH_SYN, pseudoHdr);
            setIPHeader(ipbuffer);
    
            FAddr.sin_family = AF_INET;
            FAddr.sin_port = htons(FPort);
            FAddr.sin_addr = ipbuffer->ip_dst;
    
            if(sendto(
              FSock,
              ucPayload,
              PACKET_SIZE,
              0,
              (struct sockaddr*)&FAddr,
              sizeof(struct sockaddr))!=-1){
    
              FD_ZERO(&fds);
              FD_SET(FSock, &fds);
              result=select(0, &fds, NULL, NULL, &timeout);
              if((result > 0)&&(result <10000)){     //valid socket
                if(FD_ISSET(FSock, &fds)){
                  memset(ucPayload, 0x00, PACKET_SIZE);
                  if(recvfrom(FSock, ucPayload, PACKET_SIZE, 0,
                      (struct sockaddr*)&FAddr, &size) != -1)FOpen=true;
                }
              }else FOpen = false;
            }else FOpen = false;
            int i=WSAGetLastError();
            FState=STATE_SLEEPING;
            break;
          }
        }
      }
    

    Liefert bei mir den Fehler 10049(Die angeforderte Adresse ist in diesem Kontext ungültig.)
    Kann mir jemand sagen woran das liegt? Bin mit meiner Weisheit wirklich am Ende.

    BS: Winxp
    Compiler: BCB6

    Gruss
    Michael



  • Raw Sockets funktionieren glaub ich nicht unter Windows XP.



  • Doch doch tun sie, ab win 2K werden RAW Sockets unterstützt.

    Return(Michael);



  • What new functionality is added to this feature in Windows XP Service Pack 2?
    Restricted traffic over raw sockets

    Detailed description

    A very small number of Windows applications make use of raw IP sockets, which provide an industry-standard way for applications to create TCP/IP packets with fewer integrity and security checks by the TCP/IP stack. The Windows implementation of TCP/IP still supports receiving traffic on raw IP sockets. However, the ability to send traffic over raw sockets has been restricted in two ways:
    •

    TCP data cannot be sent over raw sockets.
    •

    UDP datagrams with invalid source addresses cannot be sent over raw sockets. The IP source address for any outgoing UDP datagram must exist on a network interface or the datagram is dropped.



  • Hmm das wäre allerdings blöd. Darf man nach der Quelle deines Zitats fragen?




Anmelden zum Antworten