|
发表于 2003-7-14 13:42:45
|
显示全部楼层
[code:1]
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
main()
{
int fd_arp;
struct ifreq ifr;
char device[32];
struct sockaddr_in *sin_ptr;
int i,ret,p[4];
strcpy(device, "eth0");
fd_arp = socket(AF_INET, SOCK_PACKET, htons(0x0806));
strcpy(ifr.ifr_name, device);
if(fd_arp>=0)
{
ret=ioctl(fd_arp, SIOCGIFADDR, &ifr);
if(ret>=0)
{
sin_ptr = (struct sockaddr_in *)&ifr.ifr_addr;
for(i=0;i<4;i++)
{
p[i]=((sin_ptr->sin_addr.s_addr)>>(i*8))&0xff;
}
printf("Your IP is: %d.%d.%d.%d\n",p[0],p[1],p[2],p[3]);
}
}
}
[/code:1] |
|