|
发表于 2006-2-3 17:58:58
|
显示全部楼层
[code:1]/* 分别读出MAC、NETMASK、IP ADDRESS:*/
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/ioctl.h>
#include<net/if.h>
#include<net/if_arp.h>
#include<net/ethernet.h>
#include<signal.h>
#include<netinet/ip.h>
struct in_addr myip,mymask;
int fd_arp; /* socket fd for receive packets */
struct ifreq ifr; /* ifr structure */
main(int argc,char *argv[])
{
char device[32]; /* ethernet device name */
struct sockaddr from,to;
int fromlen;
struct sockaddr_in *sin_ptr;
u_char *ptr;
int n;
strcpy(device,"eth0");
if((fd_arp = socket(AF_INET,SOCK_PACKET,htons(0x0806)))<0)
exit(-1);
}
strcpy(ifr.ifr_name,device);
/* ifr.ifr_addr.sa_family = AF_INET; */
/* get ip address of my interface */
if(ioctl(fd_arp,SIOCGIFADDR,&ifr)<0)
{
perror("ioctr SIOCGIFADDR error");
exit(-1);
}
sin_ptr = (struct sockaddr_in *)&ifr.ifr_addr;
myip = sin_ptr->sin_addr;
/* get network mask of my interface */
if(ioctl(fd_arp,SIOCGIFNETMASK,&ifr)<0)
{
perror("ioctr SIOCGIFNETMASK error");
exit(-1);
}
sin_ptr = (struct sockaddr_in *)&ifr.ifr_addr;
mymask = sin_ptr->sin_addr;
/* get mac address of the interface */
if(ioctl(fd_arp,SIOCGIFHWADDR,&ifr)<0)
{
perror("ioctr SIOCGIFHWADDR error");
exit(-1);
}
ptr = (u_char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
printf("request mac: %02x:%02x:%02x:%02x:%02x:%02x\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),
*(ptr+5));
printf("request netmask: %s\n",inet_ntoa(mymask));
printf("request IP: %s\n",inet_ntoa(myip));
}
/* end of main */[/code:1]
外网IP 不知道么意思? 网关? |
|