Raw sockets и IP
От: fsssl_dm  
Дата: 10.04.06 15:07
Оценка:
Здравствуйте!

В "Network Programming for Microsoft Windows" Anthony Jones, Jim
Ohlund сказано:

The one limitation of raw sockets is that you can work only with
certain protocols that are already defined, such as ICMP and IGMP. You
cannot create a raw socket with IPPROTO_UDP and manipulate the UDP
header; likewise with TCP. To manipulate the IP header as well as
either the TCP or UDP header (or any other protocol encapsulated in
IP), you must use the IP_HDRINCL socket option with a raw socket. This
option allows you to build your own IP header as well as the headers
of other protocols.

Но на практике можно обойтись и без IP_HDRINCL, т.е. следующая
программа нормально работает на локальной машине:

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

#define D_PORT 7666
#define D_BUFSIZE 1024

struct UdpHeader
{
unsigned short src_port;
unsigned short targ_port;
unsigned short length;
unsigned short checksum;
};

char message[] = "Hello World!\n";
char msgbuf[D_BUFSIZE];

int main()
{
SOCKET sock;
struct sockaddr_in addr;
struct UdpHeader hdr;
struct WSAData wsd;

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

sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

hdr.targ_port = htons(D_PORT);
hdr.length = htons(sizeof(hdr)+sizeof(message));
hdr.checksum = 0; // не существенно...

memcpy((void *)msgbuf, (void *)&hdr, sizeof(hdr));
memcpy((void *)(msgbuf+sizeof(hdr)), (void *)message, sizeof(message));

sendto(sock, msgbuf, sizeof(hdr)+sizeof(message), 0, (struct sockaddr *)&addr, sizeof(addr));

closesocket(sock);
WSACleanup();
return EXIT_SUCCESS;
}

Где правда?

P.S. WinXP SP2
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.