如何编程实现ARP?(100分)

  • 主题发起人 主题发起人 zw84611
  • 开始时间 开始时间
Z

zw84611

Unregistered / Unconfirmed
GUEST, unregistred user!
ARP协议,已知对方IP,要得到对方的物理地址。如何编程实现呢?
 
笨办法
先ping这个ip,然后arp -a 重定向到一个临时文件里,分析这个文件
 
发ARP包,对方就返回ARP_ANS
 
如何才能发ARP包?
 
用Platform SDK的iphelp库函数:SendARP.
可以直接取得对方的物理地址。
我这是MFC的原吗,库文件是 IPHELPAPI.LIB.
BOOL GetMacAdd(LPCTSTR strIP, CString& strMAcAddr)
{
BOOL bRet = FALSE;
unsigned char ulMacAddr[6];
unsigned long ulMacAddrLen = 6;

CString destIP = CString(strIP);
DWORD dwIPAddr = inet_addr(destIP);

DWORD dwret = SendARP(dwIPAddr, 0,(PULONG)ulMacAddr, &ulMacAddrLen);
if(dwret == 0)
{
strMAcAddr.Format("%02X-%02X-%02X-%02X-%02X-%02X", ulMacAddr[0],ulMacAddr[1],ulMacAddr[2],ulMacAddr[3],ulMacAddr[4],ulMacAddr[5]);
return TRUE;
}
return FALSE;
}
 
to takou:VC中似乎没有IPHELPAPI.LIB,从哪里可以得到?
 
you can get it from Platform SDK.
 
to takou:
How can I get Platform SDK ? Is it in the VisualStudio?
 
no, if you have the entire set of MSDN's CDROM, you can find it.
I 've sent the necessary file to you.
 
to takou: Thanks a lot!
 
to takou:
不了解C的结构,请问SendARP各参数的类型,谢谢。hzcbb@21cn.com
SendARP(dwIPAddr, 0,(PULONG)ulMacAddr, &ulMacAddrLen);
 
please read the sample code.
 
我试过如下写法,报告errorcode=50,不能发送arp包。
也试过参数4用dword,errorcode=87。

所以想请教一下写法,谢谢

Function sendarp(ipaddr:ulong;
temp:dword;
ulmacaddr:pointer;
ulmacaddrleng:pointer) : DWord; StdCall;
implementation

{$R *.DFM}
Function sendarp; External 'Iphlpapi.dll' Name 'SendARP';

procedure TForm1.Button1Click(Sender: TObject);
var
myip:ulong;
mymac:array[0..5] of byte;
mymaclength:ulong;
r:integer;
begin
myip:=inet_addr(PChar('10.0.0.1'));
mymaclength:=length(mymac);
r:=sendarp(myip,0,@mymac,@mymaclength);
label1.caption:='errorcode:'+inttostr(r);
label2.caption:=format('%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x',[mymac[0],mymac[1],mymac[2],mymac[3],mymac[4],mymac[5]]);
end;
 
to cbb:
对于delphi我不熟悉。
有一点要注意:
mymaclength的值如果不是6的话不行。

以下是MSDN的说明。
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 a ULONG variable. This variable receives 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.

祝好运。
如果还不对,请zw84611回答你的问题吧。他也许用delphi成功过。
 
是win98不支持吧?thanks
Requirements
Windows NT/2000 or later: Requires Windows 2000 or later.
Windows 95/98/Me: Unsupported.
Header: Declared in Iphlpapi.h.
Library: Use Iphlpapi.lib.

 
我的系统是NT Server,为什么也出现errorcode=50?
望各位多多指教!!!!
 
后退
顶部