entropy
2005-10-06 19:07:23 UTC
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
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