Solved

setsockopt() problem

Posted on 2003-10-28
8
2,369 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio Exe Issue, 12 30
Windows Mobile 6.5 device Apache Flex 3 45
Launching Internet Explorer inPrivate mode using VBA 14 455
How can my static class become undefined?? 8 49
This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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