Solved

setsockopt() problem

Posted on 2003-10-28
8
2,417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

627 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