Solved

setsockopt() problem

Posted on 2003-10-28
8
2,378 Views
Last Modified: 2013-12-03
I'm running WinXP
setsockopt() below generates error 10049 - Can't assign requested address  ... Help needed...


s_RAW=socket(AF_INET,SOCK_RAW,0);
if(s_RAW==INVALID_SOCKET)
   return 0;
memset((char *) &sockaddrRAW, 0, sizeof(sockaddrRAW));//needed ?
sockaddrRAW.sin_family=AF_INET;
sockaddrRAW.sin_port=htons(uPortIn);//5544
sockaddrRAW.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(s_RAW,(LPSOCKADDR)&sockaddrRAW, sizeof(sockaddrRAW))!=0)//not binded
      return 0;//error

unsigned int bOpt=1; //debug RAW
if(setsockopt(s_RAW,IPPROTO_IP,IP_HDRINCL,(char *)&bOpt,sizeof(bOpt))==SOCKET_ERROR)
  {..error 10049 }
0
Comment
Question by:desktop2
  • 4
  • 3
8 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9634859
desktop2:

> setsockopt() below generates error 10049 - Can't assign requested address  ... Help needed...


Try changing this:
> sockaddrRAW.sin_addr.s_addr=htonl(INADDR_ANY);

To This:
     sockaddrRAW.sin_addr.s_addr = INADDR_ANY;


I'm pretty sure you don't need the htonl() on there.

Hope that helps,
Dex*
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9634914
desktop2:

> unsigned int bOpt=1; //debug RAW
> if(setsockopt(s_RAW,IPPROTO_IP,IP_HDRINCL,(char *)&bOpt,sizeof(bOpt))==SOCKET_ERROR)
>   {..error 10049 }

Oops... Forget what I said before.  According to the following link, IP_HDRINCL is not supported on Windows:
     http://www.cygwin.com/ml/cygwin/2000-11/msg01370.html

Dex*
0
 

Author Comment

by:desktop2
ID: 9635501
It states that it is not supported for cygwin
but http://tangentsoft.net/wskfaq/advanced.html states it is supported under W2K+
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 16

Expert Comment

by:_nn_
ID: 9637203
Indeed desktop2, it's supported.

The surprising thing is that I compiled and ran that code succesfully on my W2k box.
- did you specify version 2.2 in WSAStartup() ?
- could it be some other software (a firewall, a packet-filter, some IDS-engine like a virus-scanner) is preventing you the use of that specific port (maybe sysinternals' TCPView could prove helpful here)
- it didn't seem to cause too much trouble with my W2k, though I recall reading that protocol 0 is not valid when using SOCK_RAW in a socket() call. You could maybe try IPPROTO_UDP instead...

0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9637431
Actually, that link just says it isn't documented in the "setsocketopt" function, which it isn't.  I remember that I have actually used that myself, and it worked just fine on W2K, and it should work just fine on XP as well.

desktop2, try the following code and see if it works for you...

      if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
      {
            // An Error Occurred
      }

      m_hSocket = WSASocket(AF_INET, SOCK_RAW, IPPROTO_IP, NULL, 0,0);
      if (m_hSocket == INVALID_SOCKET)
      {
            // An Error Occurred
      }

      BOOL bOpt = TRUE;
      int ret = setsockopt(m_hSocket, IPPROTO_IP, IP_HDRINCL, (char *)&bOpt, sizeof(bOpt));
      if (ret == SOCKET_ERROR)
      {
            // An Error Occurred
      }

Does anyone else have concerns about answering this question?  Setting custom IP headers has few legitimate uses...

Dex*
0
 

Author Comment

by:desktop2
ID: 9637436
Yes, it is
if(WSAStartup(0x0202, &wsaData))
...

I've changed it to IPPROTO_UDP - same error :-(


If I first call bind() - it can bind to port no problem, but then I have an error 10049 when call setsockopt()

May be WinXP differently handles raw sockets than W2K...
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 250 total points
ID: 9637531
desktop2:

> May be WinXP differently handles raw sockets than W2K...

I checked my program that worked under W2K, and it still works exactly the same under XP, so I don't think that's it...

Things to try:
     1)  Using WSASocket() instead of socket().
     2)  Setting the socket options BEFORE calling bind()

Hope that helps,
Dex*
0
 

Author Comment

by:desktop2
ID: 9641410
WSASocket worked fine !
In addition, to use it I had to add #include winsock2.h ...
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL to ElasticSearch Query 1 711
Visual Studio Debugging 3 140
wordpress limitations 4 124
Please explain the parts of these 2 LINQ expressions 3 57
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question