当前位置: 技术问答>linux和unix
求LAN中IP冲突的检测方法
来源: 互联网 发布时间:2015-08-08
本文导语: 在LAN中怎样才能知道本机与其它机器的IP地址有冲突了呢? 我想了一个办法是:本机定时发送一个ARP请求包, ... uint32_t localip; u_char zero [] = {0, 0, 0, 0, 0, 0, 0}; u_char localmac[7]; struct ether_arp *arp; ... arp->arp_hdr = ...
在LAN中怎样才能知道本机与其它机器的IP地址有冲突了呢?
我想了一个办法是:本机定时发送一个ARP请求包,
...
uint32_t localip;
u_char zero [] = {0, 0, 0, 0, 0, 0, 0};
u_char localmac[7];
struct ether_arp *arp;
...
arp->arp_hdr = htons (ETHERTYPE_ARP);
arp->arp_pro = htons (ETHERTYPE_IP);
arp->arp_hln = 6;
arp->arp_pln = 4;
arp->arp_op = htons (ARPOP_REQUEST);
memcpy (arp->arp_sha, localmac, 6);
memcpy (arp->arp_spa, (void *)&localip, 4);
memcpy (arp->arp_tha, zero, 6);
memcpy (arp->arp_tpa, (void *)&localip, 4);
...
我把两个机器的IP设为相同,在此机上运行此程序怎么没有ARP REPLY包呢?
还附一个问题就是:输出MAC地址时,用指针不行但用数组行,如*p++,*p++,*p++,*p++,*p++,*p++,*p++不行,但用p[0],p[1],p[2],p[3],p[4],p[5]行啊,WHY?
我想了一个办法是:本机定时发送一个ARP请求包,
...
uint32_t localip;
u_char zero [] = {0, 0, 0, 0, 0, 0, 0};
u_char localmac[7];
struct ether_arp *arp;
...
arp->arp_hdr = htons (ETHERTYPE_ARP);
arp->arp_pro = htons (ETHERTYPE_IP);
arp->arp_hln = 6;
arp->arp_pln = 4;
arp->arp_op = htons (ARPOP_REQUEST);
memcpy (arp->arp_sha, localmac, 6);
memcpy (arp->arp_spa, (void *)&localip, 4);
memcpy (arp->arp_tha, zero, 6);
memcpy (arp->arp_tpa, (void *)&localip, 4);
...
我把两个机器的IP设为相同,在此机上运行此程序怎么没有ARP REPLY包呢?
还附一个问题就是:输出MAC地址时,用指针不行但用数组行,如*p++,*p++,*p++,*p++,*p++,*p++,*p++不行,但用p[0],p[1],p[2],p[3],p[4],p[5]行啊,WHY?
|
发ARP包的代码。你改写成unix下用的吧。
#include
#include
#include
#include
typedef struct tagIPINFO
{
u_char Ttl; // Time To Live
u_char Tos; // Type Of Service
u_char IPFlags; // IP flags
u_char OptSize; // Size of options data
u_char FAR *Options; // Options data buffer
}IPINFO, *PIPINFO;
typedef struct tagICMPECHO
{
u_long Source;
u_long Status;
u_long RTTime;
u_short DataSize;
u_short Reserved;
void FAR *pData;
IPINFO ipInfo;
}ICMPECHO, *PICMPECHO;
//ICMP API
//HANDLE WINAPI IcmpCreateFile (VOID);
//BOOL WINAPI IcmpCloseHandle (HANDLE IcmpHandle);
//DWORD WINAPI IcmpSendEcho (HANDLE IcmpHandle,IPAddr DestinationAddress,
// LPVOID RequestData,WORD RequestSize,
// PIP_OPTION_INFORMATION RequestOptions,
// LPVOID ReplyBuffer,DWORD ReplySize, DWORD Timeout);
// ICMP.DLL Export Function Pointers
HANDLE (WINAPI *pIcmpCreateFile)(VOID);
BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);
DWORD (WINAPI *pIcmpSendEcho)
(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD);
//
//
void main(int argc, char **argv)
{
WSADATA wsaData; // WSADATA
ICMPECHO icmpEcho; // ICMP Echo reply buffer
HANDLE hndlIcmp; // LoadLibrary() handle to ICMP.DLL
HANDLE hndlFile; // Handle for IcmpCreateFile()
LPHOSTENT pHost; // Pointer to host entry structure
struct in_addr iaDest; // Internet address structure
DWORD *dwAddress; // IP Address
IPINFO ipInfo; // IP Options structure
int nRet; // General use return code
DWORD dwRet; // DWORD return code
int x;
// Check arguments
if (argc != 2)
{
fprintf(stderr,"nSyntax: pingi HostNameOrIPAddressn");
return;
}
// Dynamically load the ICMP.DLL
hndlIcmp = LoadLibrary("ICMP.DLL");
if (hndlIcmp == NULL)
{
fprintf(stderr,"nCould not load ICMP.DLLn");
return;
}
// Retrieve ICMP function pointers
pIcmpCreateFile = (HANDLE (WINAPI *)(void))
GetProcAddress(hndlIcmp,"IcmpCreateFile");
pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
GetProcAddress(hndlIcmp,"IcmpCloseHandle");
pIcmpSendEcho = (DWORD (WINAPI *)
(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))
GetProcAddress(hndlIcmp,"IcmpSendEcho");
// Check all the function pointers
if (pIcmpCreateFile == NULL ||
pIcmpCloseHandle == NULL ||
pIcmpSendEcho == NULL)
{
fprintf(stderr,"nError getting ICMP proc addressn");
FreeLibrary(hndlIcmp);
return;
}
// Init WinSock
nRet = WSAStartup(0x0101, &wsaData );
if (nRet)
{
fprintf(stderr,"nWSAStartup() error: %dn", nRet);
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}
// Check WinSock version
if (0x0101 != wsaData.wVersion)
{
fprintf(stderr,"nWinSock version 1.1 not supportedn");
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}
// Lookup destination
// Use inet_addr() to determine if we're dealing with a name
// or an address
iaDest.s_addr = inet_addr(argv[1]);
if (iaDest.s_addr == INADDR_NONE)
pHost = gethostbyname(argv[1]);
else
pHost = gethostbyaddr((const char *)&iaDest,
sizeof(struct in_addr), AF_INET);
if (pHost == NULL)
{
fprintf(stderr, "n%s not foundn", argv[1]);
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}
// Tell the user what we're doing
printf("nPinging %s [%s]", pHost->h_name,
inet_ntoa((*(LPIN_ADDR)pHost->h_addr_list[0])));
// Copy the IP address
dwAddress = (DWORD *)(*pHost->h_addr_list);
// Get an ICMP echo request handle
hndlFile = pIcmpCreateFile();
for (x = 0; x = 0 )
&& ( dip - ntohl(sip)
#include
#include
#include
#include
typedef struct tagIPINFO
{
u_char Ttl; // Time To Live
u_char Tos; // Type Of Service
u_char IPFlags; // IP flags
u_char OptSize; // Size of options data
u_char FAR *Options; // Options data buffer
}IPINFO, *PIPINFO;
typedef struct tagICMPECHO
{
u_long Source;
u_long Status;
u_long RTTime;
u_short DataSize;
u_short Reserved;
void FAR *pData;
IPINFO ipInfo;
}ICMPECHO, *PICMPECHO;
//ICMP API
//HANDLE WINAPI IcmpCreateFile (VOID);
//BOOL WINAPI IcmpCloseHandle (HANDLE IcmpHandle);
//DWORD WINAPI IcmpSendEcho (HANDLE IcmpHandle,IPAddr DestinationAddress,
// LPVOID RequestData,WORD RequestSize,
// PIP_OPTION_INFORMATION RequestOptions,
// LPVOID ReplyBuffer,DWORD ReplySize, DWORD Timeout);
// ICMP.DLL Export Function Pointers
HANDLE (WINAPI *pIcmpCreateFile)(VOID);
BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);
DWORD (WINAPI *pIcmpSendEcho)
(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD);
//
//
void main(int argc, char **argv)
{
WSADATA wsaData; // WSADATA
ICMPECHO icmpEcho; // ICMP Echo reply buffer
HANDLE hndlIcmp; // LoadLibrary() handle to ICMP.DLL
HANDLE hndlFile; // Handle for IcmpCreateFile()
LPHOSTENT pHost; // Pointer to host entry structure
struct in_addr iaDest; // Internet address structure
DWORD *dwAddress; // IP Address
IPINFO ipInfo; // IP Options structure
int nRet; // General use return code
DWORD dwRet; // DWORD return code
int x;
// Check arguments
if (argc != 2)
{
fprintf(stderr,"nSyntax: pingi HostNameOrIPAddressn");
return;
}
// Dynamically load the ICMP.DLL
hndlIcmp = LoadLibrary("ICMP.DLL");
if (hndlIcmp == NULL)
{
fprintf(stderr,"nCould not load ICMP.DLLn");
return;
}
// Retrieve ICMP function pointers
pIcmpCreateFile = (HANDLE (WINAPI *)(void))
GetProcAddress(hndlIcmp,"IcmpCreateFile");
pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
GetProcAddress(hndlIcmp,"IcmpCloseHandle");
pIcmpSendEcho = (DWORD (WINAPI *)
(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))
GetProcAddress(hndlIcmp,"IcmpSendEcho");
// Check all the function pointers
if (pIcmpCreateFile == NULL ||
pIcmpCloseHandle == NULL ||
pIcmpSendEcho == NULL)
{
fprintf(stderr,"nError getting ICMP proc addressn");
FreeLibrary(hndlIcmp);
return;
}
// Init WinSock
nRet = WSAStartup(0x0101, &wsaData );
if (nRet)
{
fprintf(stderr,"nWSAStartup() error: %dn", nRet);
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}
// Check WinSock version
if (0x0101 != wsaData.wVersion)
{
fprintf(stderr,"nWinSock version 1.1 not supportedn");
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}
// Lookup destination
// Use inet_addr() to determine if we're dealing with a name
// or an address
iaDest.s_addr = inet_addr(argv[1]);
if (iaDest.s_addr == INADDR_NONE)
pHost = gethostbyname(argv[1]);
else
pHost = gethostbyaddr((const char *)&iaDest,
sizeof(struct in_addr), AF_INET);
if (pHost == NULL)
{
fprintf(stderr, "n%s not foundn", argv[1]);
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}
// Tell the user what we're doing
printf("nPinging %s [%s]", pHost->h_name,
inet_ntoa((*(LPIN_ADDR)pHost->h_addr_list[0])));
// Copy the IP address
dwAddress = (DWORD *)(*pHost->h_addr_list);
// Get an ICMP echo request handle
hndlFile = pIcmpCreateFile();
for (x = 0; x = 0 )
&& ( dip - ntohl(sip)