Solved

setsockopt() problem

Posted on 2003-10-28
8
2,397 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…

738 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