Discussion:
Newbie: SOCK_RAW problem (CODE INCLUDED)
(too old to reply)
entropy
2005-10-06 19:07:23 UTC
Permalink
I'm trying to write a console app that creates a raw socket with a
non-standard IP PROTO value. The app will run on a machine that is
the destination address for packets sent from a non-Windows box. The
packets sent go out over a raw socket with a matching IP PROTO value.

According to the Winsock doc I have (MSDN), this code should work.
But instead WSAGetLastError() returns 10022 from the recv() call.

<quote>
WSAEINVAL
10022

Invalid argument.
Some invalid argument was supplied (for example, specifying an
invalid level to the setsockopt function). In some instances, it also
refers to the current state of the socket—for instance, calling
accept on a socket that is not listening.
</quote>

A self-contained code example that demonstrates the problem follows.
Link with ws2_32.lib, of course.

---- begin cut
#include <stdio.h>
#include "winsock2.h"

WSADATA wsadata ;

#define MTU 1500

int
main( int ac, char * av[] )
{
unsigned char proto = 55 ;
int num_r, tot_r = 0 ;
SOCKET sock ;
char buf[MTU] ;

if ( WSAStartup( MAKEWORD(2,2), &wsadata ) != NO_ERROR )
{
fprintf(stderr, "WSAStartup(): failed: %ld\n", WSAGetLastError());
WSACleanup();
exit( -1 );
}

if ((sock = socket( AF_INET, SOCK_RAW, (int)proto ))==INVALID_SOCKET)
{
fprintf(stderr, "socket(): failed: %ld\n", WSAGetLastError());
WSACleanup() ;
exit( -1 ) ;
}

while ( (num_r = recv( sock, buf, MTU, 0 )) >= 0 )
{
fprintf( stderr, "Got one: %d\n", num_r ) ;
tot_r += num_r ;
}

switch( num_r )
{
case 0:
fprintf( stderr, "read() EOT\n" ) ;
break ;
case SOCKET_ERROR:
fprintf( stderr, "read() ABEND: %ld\n", WSAGetLastError() ) ;
break ;
}

fprintf( stderr, "Total read: %d\n", tot_r ) ;

return 0 ;
}
---- end cut
Lee W.
2005-10-07 13:24:09 UTC
Permalink
you need to call bind() after you create the socket to attach it to a local
interface and before you call recv(), else it will not know which interface
to listen on.
Post by entropy
I'm trying to write a console app that creates a raw socket with a
non-standard IP PROTO value. The app will run on a machine that is
the destination address for packets sent from a non-Windows box. The
packets sent go out over a raw socket with a matching IP PROTO value.
According to the Winsock doc I have (MSDN), this code should work.
But instead WSAGetLastError() returns 10022 from the recv() call.
--
reply to: codetrifle{at}users.sourceforge.net
visit: http://tracetcp.sourceforge.net
Arkady Frenkel
2005-10-09 14:38:05 UTC
Permalink
Additionally :
what is protocol - 55 , maybe you mean 255 (
IPPROTO_RAW) ?

Arkady
Post by Lee W.
you need to call bind() after you create the socket to attach it to a local
interface and before you call recv(), else it will not know which interface
to listen on.
Post by entropy
I'm trying to write a console app that creates a raw socket with a
non-standard IP PROTO value. The app will run on a machine that is
the destination address for packets sent from a non-Windows box. The
packets sent go out over a raw socket with a matching IP PROTO value.
According to the Winsock doc I have (MSDN), this code should work.
But instead WSAGetLastError() returns 10022 from the recv() call.
--
reply to: codetrifle{at}users.sourceforge.net
visit: http://tracetcp.sourceforge.net
entropy
2005-10-09 15:29:56 UTC
Permalink
what is protocol - 55 , maybe you mean 255 (IPPROTO_RAW) ?
No, I mean 55. http://www.iana.org/assignments/protocol-numbers
--
BUSH: BIG HAT, NO CATTLE.
Arkady Frenkel
2005-10-09 17:11:49 UTC
Permalink
IMHO windows have no support of such , so the error , check winsock2.h for
suppored values
Arkady
Post by entropy
what is protocol - 55 , maybe you mean 255 (IPPROTO_RAW) ?
No, I mean 55. http://www.iana.org/assignments/protocol-numbers
--
BUSH: BIG HAT, NO CATTLE.
entropy
2005-10-09 22:14:23 UTC
Permalink
Post by Arkady Frenkel
IMHO windows have no support of such , so the error , check winsock2.h for
suppored values
No, it's straightforward if a PITA. The problem with my code was, as
another poster pointed out, a missing call to bind().

For raw sockets, the socket() call has to reference the protocol
number (rather than the usual value of 0). To receive (or send) non-
standard protocol numbers, you're doing raw sockets. No way around
it.
Post by Arkady Frenkel
Arkady
Post by entropy
what is protocol - 55 , maybe you mean 255 (IPPROTO_RAW) ?
No, I mean 55. http://www.iana.org/assignments/protocol-numbers
--
BUSH: BIG HAT, NO CATTLE.
Arkady Frenkel
2005-10-10 10:15:20 UTC
Permalink
That's what I wrote at start ( "additionally" ) - bind or connect are must,
nice to hear that you are OK now :)
Arkady
Post by entropy
Post by Arkady Frenkel
IMHO windows have no support of such , so the error , check winsock2.h for
suppored values
No, it's straightforward if a PITA. The problem with my code was, as
another poster pointed out, a missing call to bind().
For raw sockets, the socket() call has to reference the protocol
number (rather than the usual value of 0). To receive (or send) non-
standard protocol numbers, you're doing raw sockets. No way around
it.
Post by Arkady Frenkel
Arkady
Post by entropy
what is protocol - 55 , maybe you mean 255 (IPPROTO_RAW) ?
No, I mean 55. http://www.iana.org/assignments/protocol-numbers
--
BUSH: BIG HAT, NO CATTLE.
Loading...