<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[sendto Problem mit IP_HDRINCL [RAW Sockets]]]></title><description><![CDATA[<p>Hallo,<br />
ich habe ein kleines Problem beim Senden mit RAW Sockets.</p>
<pre><code>void __fastcall TConnectionTh::setIPHeader(TIPHdr* ipHeader){
  memset(ipHeader, 0x00, sizeof(TIPHdr));
  ipHeader-&gt;ip_hl = 5;
	ipHeader-&gt;ip_v = 4;
	ipHeader-&gt;ip_tos = 16;
	ipHeader-&gt;ip_len = htons(40);
	ipHeader-&gt;ip_id = htons(20372);
	ipHeader-&gt;ip_off = htons(0x4000);
	ipHeader-&gt;ip_ttl = 64;
	ipHeader-&gt;ip_p = IPPROTO_TCP;
	ipHeader-&gt;ip_src.s_addr = inet_addr(&quot;192.168.1.11&quot;);
	ipHeader-&gt;ip_dst = FHost;
  ipHeader-&gt;ip_sum = htons(in_cksum( (char *)ipHeader, sizeof(struct TIPHdr)));
}
</code></pre>
<pre><code>void __fastcall TConnectionTh::setTcpHeader(TTcpHdr* tcpHeader, int iTyp, TTCPPseudoHdr* pseudoHdr){
  tcpHeader-&gt;th_sport = htons(SRC_PORT);
  tcpHeader-&gt;th_dport = htons(FPort);
  tcpHeader-&gt;th_seq = htonl(1000000000);
  tcpHeader-&gt;th_ack = htonl(1000000000); ;
  tcpHeader-&gt;th_x2 = 0;
  tcpHeader-&gt;th_off = 0;            //first and only tcp segment
  tcpHeader-&gt;th_flags = iTyp;       //initial connection request
  tcpHeader-&gt;th_win = htons(1024);
  tcpHeader-&gt;th_urp = 0;
  tcpHeader-&gt;th_sum = htons(in_cksum( (char *)pseudoHdr, sizeof(struct TTCPPseudoHdr)
                          + sizeof(struct TTcpHdr) ) + 0); //0 =&gt; Keine Daten
}
</code></pre>
<pre><code>void __fastcall TConnectionTh::setTcpPseudoHeader(TTCPPseudoHdr* pseudohdr){

  pseudohdr-&gt;s_addy.s_addr = inet_addr(&quot;192.168.1.11&quot;);
  pseudohdr-&gt;d_addy = FHost;
  pseudohdr-&gt;zero = 0x00;
  pseudohdr-&gt;protocol = IPPROTO_TCP;
  pseudohdr-&gt;length = htons(sizeof(struct TTcpHdr) + 0);  //0 =&gt; Keine Daten
}
</code></pre>
<pre><code>int __fastcall TConnectionTh::in_cksum(char *data, int count){
		/* Compute Internet Checksum for &quot;count&quot; bytes
		*         beginning at location &quot;addr&quot;.
		*/
	register long sum = 0;

	while( count &gt; 1 )  {
		/*  This is the inner loop */
		sum += * (unsigned short *) data++ ;
		count -= 2;
	}

		/*  Add left-over byte, if any */
	if( count &gt; 0 )
		sum += * (unsigned char *) data;

		/*  Fold 32-bit sum to 16 bits */
	while (sum&gt;&gt;16)
	sum = (sum &amp; 0xffff) + (sum &gt;&gt; 16);

	return ~sum;
}
</code></pre>
<pre><code>bool __fastcall TConnectionTh::sethdrinclude(int sd) {
  int one = 1;
  return(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, (const char *) &amp;one, sizeof(one)) == 0);
}
</code></pre>
<pre><code>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-&gt;ip_dst;

        if(sendto(
          FSock,
          ucPayload,
          PACKET_SIZE,
          0,
          (struct sockaddr*)&amp;FAddr,
          sizeof(struct sockaddr))!=-1){

          FD_ZERO(&amp;fds);
          FD_SET(FSock, &amp;fds);
          result=select(0, &amp;fds, NULL, NULL, &amp;timeout);
          if((result &gt; 0)&amp;&amp;(result &lt;10000)){     //valid socket
            if(FD_ISSET(FSock, &amp;fds)){
              memset(ucPayload, 0x00, PACKET_SIZE);
              if(recvfrom(FSock, ucPayload, PACKET_SIZE, 0,
                  (struct sockaddr*)&amp;FAddr, &amp;size) != -1)FOpen=true;
            }
          }else FOpen = false;
        }else FOpen = false;
        int i=WSAGetLastError();
        FState=STATE_SLEEPING;
        break;
      }
    }
  }
</code></pre>
<p>Liefert bei mir den Fehler 10049(Die angeforderte Adresse ist in diesem Kontext ungültig.)<br />
Kann mir jemand sagen woran das liegt? Bin mit meiner Weisheit wirklich am Ende.</p>
<p>BS: Winxp<br />
Compiler: BCB6</p>
<p>Gruss<br />
Michael</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/113578/sendto-problem-mit-ip_hdrincl-raw-sockets</link><generator>RSS for Node</generator><lastBuildDate>Wed, 01 Jul 2026 16:13:12 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/113578.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 24 Jun 2005 09:10:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to sendto Problem mit IP_HDRINCL [RAW Sockets] on Fri, 24 Jun 2005 09:10:16 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich habe ein kleines Problem beim Senden mit RAW Sockets.</p>
<pre><code>void __fastcall TConnectionTh::setIPHeader(TIPHdr* ipHeader){
  memset(ipHeader, 0x00, sizeof(TIPHdr));
  ipHeader-&gt;ip_hl = 5;
	ipHeader-&gt;ip_v = 4;
	ipHeader-&gt;ip_tos = 16;
	ipHeader-&gt;ip_len = htons(40);
	ipHeader-&gt;ip_id = htons(20372);
	ipHeader-&gt;ip_off = htons(0x4000);
	ipHeader-&gt;ip_ttl = 64;
	ipHeader-&gt;ip_p = IPPROTO_TCP;
	ipHeader-&gt;ip_src.s_addr = inet_addr(&quot;192.168.1.11&quot;);
	ipHeader-&gt;ip_dst = FHost;
  ipHeader-&gt;ip_sum = htons(in_cksum( (char *)ipHeader, sizeof(struct TIPHdr)));
}
</code></pre>
<pre><code>void __fastcall TConnectionTh::setTcpHeader(TTcpHdr* tcpHeader, int iTyp, TTCPPseudoHdr* pseudoHdr){
  tcpHeader-&gt;th_sport = htons(SRC_PORT);
  tcpHeader-&gt;th_dport = htons(FPort);
  tcpHeader-&gt;th_seq = htonl(1000000000);
  tcpHeader-&gt;th_ack = htonl(1000000000); ;
  tcpHeader-&gt;th_x2 = 0;
  tcpHeader-&gt;th_off = 0;            //first and only tcp segment
  tcpHeader-&gt;th_flags = iTyp;       //initial connection request
  tcpHeader-&gt;th_win = htons(1024);
  tcpHeader-&gt;th_urp = 0;
  tcpHeader-&gt;th_sum = htons(in_cksum( (char *)pseudoHdr, sizeof(struct TTCPPseudoHdr)
                          + sizeof(struct TTcpHdr) ) + 0); //0 =&gt; Keine Daten
}
</code></pre>
<pre><code>void __fastcall TConnectionTh::setTcpPseudoHeader(TTCPPseudoHdr* pseudohdr){

  pseudohdr-&gt;s_addy.s_addr = inet_addr(&quot;192.168.1.11&quot;);
  pseudohdr-&gt;d_addy = FHost;
  pseudohdr-&gt;zero = 0x00;
  pseudohdr-&gt;protocol = IPPROTO_TCP;
  pseudohdr-&gt;length = htons(sizeof(struct TTcpHdr) + 0);  //0 =&gt; Keine Daten
}
</code></pre>
<pre><code>int __fastcall TConnectionTh::in_cksum(char *data, int count){
		/* Compute Internet Checksum for &quot;count&quot; bytes
		*         beginning at location &quot;addr&quot;.
		*/
	register long sum = 0;

	while( count &gt; 1 )  {
		/*  This is the inner loop */
		sum += * (unsigned short *) data++ ;
		count -= 2;
	}

		/*  Add left-over byte, if any */
	if( count &gt; 0 )
		sum += * (unsigned char *) data;

		/*  Fold 32-bit sum to 16 bits */
	while (sum&gt;&gt;16)
	sum = (sum &amp; 0xffff) + (sum &gt;&gt; 16);

	return ~sum;
}
</code></pre>
<pre><code>bool __fastcall TConnectionTh::sethdrinclude(int sd) {
  int one = 1;
  return(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, (const char *) &amp;one, sizeof(one)) == 0);
}
</code></pre>
<pre><code>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-&gt;ip_dst;

        if(sendto(
          FSock,
          ucPayload,
          PACKET_SIZE,
          0,
          (struct sockaddr*)&amp;FAddr,
          sizeof(struct sockaddr))!=-1){

          FD_ZERO(&amp;fds);
          FD_SET(FSock, &amp;fds);
          result=select(0, &amp;fds, NULL, NULL, &amp;timeout);
          if((result &gt; 0)&amp;&amp;(result &lt;10000)){     //valid socket
            if(FD_ISSET(FSock, &amp;fds)){
              memset(ucPayload, 0x00, PACKET_SIZE);
              if(recvfrom(FSock, ucPayload, PACKET_SIZE, 0,
                  (struct sockaddr*)&amp;FAddr, &amp;size) != -1)FOpen=true;
            }
          }else FOpen = false;
        }else FOpen = false;
        int i=WSAGetLastError();
        FState=STATE_SLEEPING;
        break;
      }
    }
  }
</code></pre>
<p>Liefert bei mir den Fehler 10049(Die angeforderte Adresse ist in diesem Kontext ungültig.)<br />
Kann mir jemand sagen woran das liegt? Bin mit meiner Weisheit wirklich am Ende.</p>
<p>BS: Winxp<br />
Compiler: BCB6</p>
<p>Gruss<br />
Michael</p>
]]></description><link>https://www.c-plusplus.net/forum/post/816270</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/816270</guid><dc:creator><![CDATA[focus]]></dc:creator><pubDate>Fri, 24 Jun 2005 09:10:16 GMT</pubDate></item><item><title><![CDATA[Reply to sendto Problem mit IP_HDRINCL [RAW Sockets] on Fri, 24 Jun 2005 09:30:16 GMT]]></title><description><![CDATA[<p>Raw Sockets funktionieren glaub ich nicht unter Windows XP.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/816283</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/816283</guid><dc:creator><![CDATA[nicht mehr]]></dc:creator><pubDate>Fri, 24 Jun 2005 09:30:16 GMT</pubDate></item><item><title><![CDATA[Reply to sendto Problem mit IP_HDRINCL [RAW Sockets] on Fri, 24 Jun 2005 09:42:08 GMT]]></title><description><![CDATA[<p>Doch doch tun sie, ab win 2K werden RAW Sockets unterstützt.</p>
<p>Return(Michael);</p>
]]></description><link>https://www.c-plusplus.net/forum/post/816292</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/816292</guid><dc:creator><![CDATA[focus]]></dc:creator><pubDate>Fri, 24 Jun 2005 09:42:08 GMT</pubDate></item><item><title><![CDATA[Reply to sendto Problem mit IP_HDRINCL [RAW Sockets] on Fri, 24 Jun 2005 09:51:13 GMT]]></title><description><![CDATA[<blockquote>
<p>What new functionality is added to this feature in Windows XP Service Pack 2?<br />
Restricted traffic over raw sockets</p>
<p>Detailed description</p>
<p>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:<br />
•</p>
<p>TCP data cannot be sent over raw sockets.<br />
•</p>
<p>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.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/816296</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/816296</guid><dc:creator><![CDATA[return antwort;]]></dc:creator><pubDate>Fri, 24 Jun 2005 09:51:13 GMT</pubDate></item><item><title><![CDATA[Reply to sendto Problem mit IP_HDRINCL [RAW Sockets] on Fri, 24 Jun 2005 10:02:25 GMT]]></title><description><![CDATA[<p>Hmm das wäre allerdings blöd. Darf man nach der Quelle deines Zitats fragen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/816301</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/816301</guid><dc:creator><![CDATA[focus]]></dc:creator><pubDate>Fri, 24 Jun 2005 10:02:25 GMT</pubDate></item><item><title><![CDATA[Reply to sendto Problem mit IP_HDRINCL [RAW Sockets] on Fri, 24 Jun 2005 10:42:55 GMT]]></title><description><![CDATA[<p>von microsoft.</p>
<p><a href="http://www.microsoft.com/technet/prodtechnol/winxppro/maintain/sp2netwk.mspx" rel="nofollow">http://www.microsoft.com/technet/prodtechnol/winxppro/maintain/sp2netwk.mspx</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/816327</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/816327</guid><dc:creator><![CDATA[return zitat;]]></dc:creator><pubDate>Fri, 24 Jun 2005 10:42:55 GMT</pubDate></item></channel></rss>