sendARP

SendARP

The SendARP function sends an ARP request to obtain the physical address that corresponds to the specified destination IP address.

DWORD SendARP(
  IPAddr DestIP,     // destination IP address
  IPAddr SrcIP,      // IP address of sender
  PULONG pMacAddr,   // returned physical address
  PULONG PhyAddrLen  // length of returned physical addr.
);
Parameters
DestIP
[in] Specifies the destination IP address. The ARP request attempts to obtain the physical address that corresponds to this IP address.
SrcIP
[in] Specifies the IP address of the sender. This parameter is optional. The caller may specify zero for the parameter.
pMacAddr
[out] Pointer to an array of ULONG variables. The first six bytes of this array receive the physical address that corresponds to the IP address specified by the DestIP parameter.
PhyAddrLen
[out] Pointer to a ULONG variable. This variable contains the length of the physical address pointed to by the pMacAddr parameter.
Return Values

If the function succeeds, the return value is NO_ERROR.

If the function fails, use FormatMessage to obtain the message string for the returned error.

Remarks

For information about the IPAddr data type, see Win32 Simple Data Types. To convert an IP address between dotted decimal notation and IPAddr format, use the inet_addr and inet_ntoa functions.

Example Code

The following code demonstrates how to obtain the media access control (MAC) address associated with a specified IP address.

//
// Link with ws2_32.lib and iphlpapi.lib
//

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iphlpapi.h>


int __cdecl main()
{
    HRESULT hr;
    IPAddr  ipAddr;
    ULONG   pulMac[2];
    ULONG   ulLen;

    ipAddr = inet_addr ("216.145.25.31");
    memset (pulMac, 0xff, sizeof (pulMac));
    ulLen = 6;
    
    hr = SendARP (ipAddr, 0, pulMac, &ulLen);
    printf ("Return %08x, length %8d/n", hr, ulLen);
    
    size_t i, j;
    char * szMac = new char[ulLen*3];
    PBYTE pbHexMac = (PBYTE) pulMac;

    //
    // Convert the binary MAC address into human-readable
    //
    for (i = 0, j = 0; i < ulLen - 1; ++i) {
        j += sprintf (szMac + j, "%02X:", pbHexMac[i]);
    }
    
    sprintf (szMac + j, "%02X", pbHexMac[i]);
    printf ("MAC address %s/n", szMac);
    
    delete [] szMac;

    return 0;
}
Requirements

  Windows NT/2000/XP: Included in Windows 2000; Windows XP Pro; and Windows .NET Server.
  Windows 95/98/Me: Unsupported.
  Header: Declared in Iphlpapi.h.
  Library: Use Iphlpapi.lib.

See Also

CreateIpNetEntry, DeleteIpNetEntry, FlushIpNetTable, SetIpNetEntry

Platform SDK Release: August 2001
<think>好的,用户的问题是关于在Linux中使用sendarp相关的头文件。首先,我需要确认是否存在名为sendarp的函数或命令。根据我的知识库,Linux中并没有直接叫做sendarp的标准库函数或系统调用。用户可能混淆了send和ARP相关的操作。 接下来,我应该考虑用户的实际需求。ARP(地址解析协议)用于将IP地址转换为MAC地址,通常在网络编程中使用。在Linux中,处理ARP请求通常涉及套接字编程或使用特定的库函数。 常见的头文件可能包括<netinet/if_ether.h>,其中定义了以太网和ARP数据包的结构,比如struct ether_header和struct ether_arp。此外,<netpacket/packet.h>用于原始套接字,允许用户构造自定义的数据包,包括ARP报文。sys/socket.h是网络编程的基础头文件,提供套接字函数。 用户可能需要构造和发送ARP数据包,这可以通过创建原始套接字并填充适当的ARP结构来实现。例如,使用socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))来创建套接字,然后填充以太网和ARP头部结构,最后通过sendto发送。 此外,用户可能参考现有的工具或库,如libpcap,它提供了更高级的接口来发送和捕获网络数据包。相关的头文件是pcap.h,但需要安装libpcap-dev包。 需要提醒用户注意权限问题,发送原始数据包通常需要root权限。同时,确保引用的正确性,例如struct ether_arp的定义在netinet/if_ether.h中,而原始套接字相关的结构在netpacket/packet.h中。 最后,生成相关问题帮助用户扩展知识,比如如何构造ARP报文、使用libpcap发送ARP请求等。</think>在Linux系统中,没有直接名为`sendarp`的标准库函数。若需要实现ARP数据包的发送,通常需要以下关键头文件和网络编程技术: ### 核心头文件 1. **ARP报文结构定义** ```c #include <netinet/if_ether.h> // 包含struct ether_arp定义 ``` 该头文件定义了ARP报文的结构体`struct ether_arp`,包含发送端/目标协议地址、硬件地址等字段[^3]。 2. **原始套接字支持** ```c #include <netpacket/packet.h> // 原始套接字支持 #include <sys/socket.h> // 基础套接字函数 ``` 创建原始套接字时需要指定协议类型为`ETH_P_ARP`,例如: ```c int sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); ``` ### 代码实现框架 ```c #include <netinet/if_ether.h> #include <netpacket/packet.h> #include <sys/socket.h> void send_arp_request() { // 创建原始套接字 int sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); // 构造以太网帧头 struct ether_header eth_header; eth_header.ether_type = htons(ETH_P_ARP); // 构造ARP报文 struct ether_arp arp_packet; arp_packet.arp_op = htons(ARPOP_REQUEST); // ARP请求类型 // 发送数据包 sendto(sock, &packet, sizeof(packet), 0, (struct sockaddr*)&sockaddr, sizeof(sockaddr)); } ``` ### 注意事项 1. **权限要求**:需要以`root`权限运行程序 2. **报文构造**:需要正确填充源/目标MAC地址、IP地址等字段 3. **协议选择**:`ETH_P_ARP`表示处理ARP协议类型的数据包[^4]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值