/* tcp4_10localip.c */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include // added by liyuan
#include // added by liyuan
struct in_addr myself, 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) {
perror( "arp socket error");
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("ioctl SIOCGIFADDR error");
exit(-1);
}
sin_ptr = (struct sockaddr_in *)&ifr.ifr_addr;
myself = sin_ptr->sin_addr;
/* get network mask of my interface */
if (ioctl(fd_arp, SIOCGIFNETMASK, &ifr) < 0) {
perror("ioctl 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("ioctl SIOCGIFHWADDR error");
exit(-1);
}
ptr = (u_char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
printf( "request mac %02x:%02x:%02x:%02x:%02x:%02x, ",
*ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
*(ptr + 4), *(ptr + 5) );
printf( "request netmask %s ", inet_ntoa(mymask));
printf( "request IP %s\n", inet_ntoa(myself));
} /* end of main */
阅读(286) | 评论(0) | 转发(0) |