Sign in to follow this  
minipie8

SOCK_RAW - sending just one packet

Recommended Posts

minipie8    122
Does anyone know how to send a packet through SOCK_RAW ? Currently, here is the implementation: SOCKET rawsocket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); char * packetdata = "ip and tcp headers and data"; send(rawsocket, packetdata, LengthNumberDefinedEarlier, 0); that send returns SOCKET_ERROR, and WSAGetLastError returns 10057, "socket is not connected". However, using SOCK_RAW, the system is not supposed to care that it isn't connected. I also tried using DWORD theNumberOne = 1; setsockopt(rawsocket, IPPROTO_IP, IP_HDRINCL, (char*)&theNumberOne, sizeof(DWORD)); but that results in SOCKET_ERROR, and WSAGetLastError says it is a parameter error. Do i need to upgrade to a version of winsock other than 2.2 to use IP_HDRINCL ? And what exactly do I do to send a packet using SOCK_RAW ?

Share this post


Link to post
Share on other sites
Thorin_nz    122
Dont quote me on this, but im pretty sure than an error of 10057 means that a previous packet sent couldn't make it to the destination returned.

try sending packets to 127.0.0.1 or something alike...

- Steve

Share this post


Link to post
Share on other sites
minipie8    122
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp

WSAENOTCONN
10057

Socket is not connected.
A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using sendto) no address was supplied. Any other type of operation might also return this error—for example, setsockopt setting SO_KEEPALIVE if the connection has been reset.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Are you using Windows XP? If so, did you install Service Pack 2? If you did, then it will be impossible to send raw IP packets. In SP2, Microsoft removed functionality for sending raw IP packets in an attempt to beef up security.


Here is some code that I used in my programs prior to SP2:


WSAStartup(MAKEWORD(2,2),&wsa);

if((sock = socket( AF_INET,
SOCK_RAW,
IPPROTO_RAW)) == -1)
{
printf("socket(:()\n");
exit(0);
}

...
DWORD bOpt = true;
if (setsockopt( sock,
IPPROTO_IP,
IP_HDRINCL,
(char *)&bOpt,
sizeof(bOpt)) == SOCKET_ERROR)
{
printf("setsockopt(IP_HDRINCL) failed: %d\n", WSAGetLastError());
return -1;
}

...

if( sendto( sock,
sendbuf,
40,
0,
(sockaddr *)&their_addr,
sizeof(their_addr)) == -1)
{
printf("sendto failed: %d\n", WSAGetLastError());
exit(0);
}



This seems very similiar to your code, except for the SendTo rather than the send. For this function, you must set up the sockaddr struct for their address, though. I think the error is being caused because the send function needs a connected socket to work.

Otherwise, your code looks very similiar to mine, so I believe that the problem rests in your use of the send function rather than the sendto function. I reccomend you look up the usage for the sendto function on MSDN.

I hope that helps you get your code working!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this