Solved

setsockopt() problem

Posted on 2003-10-28
8
2,344 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now