G
Guillaume Kaddouch
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.
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.