473,503 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RAW Sockets on Windows XP

Hi,

I am in trouble since many days because i'm facing a problem that i
can't solve.
I used to play with RAW socket under Linux, but the near same code
ported on Windows doesn't works, and i have no idea why.
The purpose is to send an IP packet (RAW, build by ourself) and o send
it to another comp on the same LAN.

The Win32 application console code is :

##############################################
#include <stdio.h>
#include <winsock2.h>

/* definition of IP header version 4 as per RFC 791 */
#define IPVERSION 4

typedef struct IP_HDR
{
unsigned char VerIHL; //Version and IP Header Length
unsigned char Tos;
unsigned short Total_len;
unsigned short ID;
unsigned short off; //Flags 3 bits and Fragment offset 13 bits
unsigned char TTL;
unsigned char Protocol;
unsigned short Checksum;
unsigned long SrcIP;
unsigned long DstIP;
//unsigned long Options_and_Padding;
} IP_HDR;

#define IP_HDR_LEN sizeof(IP_HDR)

void InitWinsock()
{
// variables
WSADATA WSAptr;

// Initialisation
if (WSAStartup(MAKEWORD(2,2), &WSAptr) != 0)
{
MessageBox(0, "winsock error", "erreur", MB_ICONERROR);

}

}
void TerminateWinsock()
{
int res;
res = WSACleanup();
if (res != 0)
{
// ERROR
}
}

int main()
{
// Variables
SOCKET fd;
SOCKADDR_IN dest;
IP_HDR *pckt;
int res;
char * Buffer;

InitWinsock();
//IP header
pckt = (struct IP_HDR *)malloc(sizeof(struct IP_HDR));

pckt->VerIHL = 0x45;
pckt->Tos = 0x00;
pckt->Total_len = 0x05DC;
pckt->ID = 0x78BB;
pckt->off = 0x4000;
pckt->TTL = 0x6C;
pckt->Protocol = 0x00;
pckt->Checksum = 0x45BA;
pckt->SrcIP = inet_addr("192.168.0.1");
pckt->DstIP = inet_addr("192.168.0.2");
printf("packet ok\n\n");

// # socket #
fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (fd == INVALID_SOCKET)
{
MessageBox(0, "socket failed", "erreur", MB_ICONERROR);
}
printf("socket ok\n\n");
// # socket options #
unsigned int bOpt=1; //debug RAW
if(setsockopt(fd, IPPROTO_IP, 2,(char *)&bOpt,
sizeof(bOpt))==SOCKET_ERROR)
{
MessageBox(0, "options failed", "erreur", MB_ICONERROR);
}
printf("options ok\n\n");
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = INADDR_ANY; //inet_addr("192.168.0.2");
dest.sin_port = 0;

res = bind(fd, (sockaddr *)&dest, sizeof(dest));
if (res == SOCKET_ERROR)
{
res = WSAGetLastError();
printf("bind -> %i\n\n", res);

MessageBox(0, "bind failed", "erreur", MB_ICONERROR);
}
printf("bind OK\n\n");


//
//** THE ERROR OCCURS THERE **//
//
// # sending packet #
res = sendto(fd, (const char *)pckt, sizeof(pckt), 0, (sockaddr
*)&dest, sizeof(dest));
if (res == SOCKET_ERROR)
{
res = WSAGetLastError();
printf("sendto -> %i\n\n", res);

MessageBox(0, "send failed", "erreur", MB_ICONERROR);
}
printf("send ok\n\n");
// # fermeture du socket #
closesocket(fd);
return 0;
}

###################################

The error occurs to the "sendto" API call, and the error number returned
is a MSDN documented error :

WSAEADDRNOTAVAIL
10049

Cannot assign requested address.
The requested address is not valid in its context. This normally
results from an attempt to bind to an address that is not valid for the
local computer. This can also result from connect, sendto, WSAConnect,
WSAJoinLeaf, or WSASendTo when the remote address or port is not valid
for a remote computer (for example, address or port 0).

Is anyone has an idea to solve my problem ?

Thanks you.

Guillaume.
Jul 22 '05 #1
1 3847
On Fri, 26 Dec 2003 22:14:58 +0100, Guillaume Kaddouch
<gk***@wanadoo.fr> wrote in comp.lang.c++:
Hi,

I am in trouble since many days because i'm facing a problem that i
can't solve.
I used to play with RAW socket under Linux, but the near same code
ported on Windows doesn't works, and i have no idea why.


[snip]

Neither do we, since sockets are not part of the C++ language, which
is the topic here. Ask in a Windows programming group, or one of
Microsoft's support groups in the news:microsoft.public.* family.
This is not a language issue.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3880
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
4
2138
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
3
2464
by: John P | last post by:
Hi, I know that some older Unix programs that compile with GCC use socket.h to connect to and execute other programs. However, I am using Visual C++ 6.0 and it doesn't seem to have socket.h...
6
6952
by: Laxmikant Rashinkar | last post by:
Is there any way to use a C# socket in promiscuous mode? Any sample code that shows how this is done? any assistance is much appreciated! thanks LK
0
2341
by: Stuart Norris | last post by:
Dear Group, I am having a problem setting SocketOptionName.SendTimeout on a client TCPIP application using the sockets in .NET. From the on-line help it is possible to set a...
4
7082
by: Ron | last post by:
Greetings, below is a sample app for connecting to a mainframe server using Sockets for the purpose of using FTP service to interact with it from a PC. I got as far as creating the connection....
0
1142
by: richard.charts | last post by:
Well, unfortunately, I couldn't get any answer on the msdn forums, so I'll give it a shot here. Real question at the bottom....
2
3574
by: =?Utf-8?B?U2Vhbk1hYw==?= | last post by:
I am familiar with how to use winsock in vb6 to create a network app. I'm trying to find a way to take the current vb6 app and create a windows service using system.net.sockets. How do you create...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.