简体   繁体   English

在 windows 中发送原始 arp 回复数据包

[英]Send Raw arp reply packet in windows

I am currently learning how to use the windows raw sockets.我目前正在学习如何使用 windows 原始 sockets。

I created a raw arp reply frame(includes all headers (ethernet+arp headers)), and when I send it using sendto function, It fails and return SOCKET_ERROR with error code 10047.我创建了一个原始 arp 回复帧(包括所有标头(以太网+arp 标头)),当我使用 sendto function 发送它时,它失败并返回 SOCKET_ERROR,错误代码为 10047。

The parameters I used to create the socket are as follows:我用来创建socket的参数如下:

socket s = socket(AF_INET,SOCK_RAW,IPPROTO_RAW);

and also I changed the socket options as follows:我还更改了套接字选项,如下所示:

int on=1;
setsockopt(s,IPPROTO_IP, 2,(char*)&on,sizeof(on));

(By the way, '2' is equal to IP_HDRINCL, for some reason, visual studio didn't recognize it..) (顺便说一句,'2' 等于 IP_HDRINCL,由于某种原因,Visual Studio 无法识别它..)

I try to send the packet as follows:我尝试按如下方式发送数据包:

socketaddr sa = { 0 };
int SentBytesCount = sendto(s, (char*)&arp_raw_msg,sizeof(Arp_Frame),0,&sa,sizeof(sa));

Where Arp_Frame is a struct that includes ethernet header+arp header+18 Bytes for padding.其中 Arp_Frame 是一个结构,包括以太网头+arp 头+18 字节用于填充。

After this call I get that SentBytesCount is equal to SOCKET_ERROR(-1), and no packet is sent.在这个调用之后,我得到 SentBytesCount 等于 SOCKET_ERROR(-1),并且没有发送数据包。

Thank you for your help!谢谢您的帮助!

Winsock error 10047 is WSAEAFNOSUPPORT : Winsock 错误 10047 是WSAEAFNOSUPPORT

Address family not supported by protocol family.协议族不支持地址族。

An address incompatible with the requested protocol was used.使用了与请求的协议不兼容的地址。 All sockets are created with an associated address family (that is, AF_INET for Internet Protocols) and a generic protocol type (that is, SOCK_STREAM).所有 sockets 均使用关联的地址族(即 Internet 协议的 AF_INET)和通用协议类型(即 SOCK_STREAM)创建。 This error is returned if an incorrect protocol is explicitly requested in the socket call, or if an address of the wrong family is used for a socket, for example, in sendto .如果在socket调用中显式请求了不正确的协议,或者如果套接字使用了错误系列的地址(例如在sendto中),则会返回此错误。

You created an AF_INET (IPv4) socket, but you are not passing sendto() a valid sockaddr_in containing an IPv4 address and port, hence the error.您创建了一个AF_INET (IPv4) 套接字,但您没有向sendto()传递包含 IPv4 地址和端口的有效sockaddr_in ,因此出现错误。 You are passing it an empty socketaddr (what is that?) instead.您正在向它传递一个空的socketaddr (那是什么?)。

Any sockaddr_... struct you use with a socket must match what the socket's address family expects, as set by the socket() call (in your case, AF_INET , which uses sockaddr_in addresses).与套接字一起使用的任何sockaddr_...结构都必须与套接字的地址族所期望的相匹配,正如socket()调用所设置的那样(在您的情况下, AF_INET ,它使用sockaddr_in地址)。

sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("destination IP address");
sa.sin_port = htons(Destination port number);

int SentBytesCount = sendto(s, (char*)&arp_raw_msg, sizeof(Arp_Frame), 0, (struct sockaddr*)&sa, sizeof(sa));

As for IP_HDRINCL , it is defined in ws2tcpip.h .至于IP_HDRINCL ,它在ws2tcpip.h中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM