📄 tcp4_10localip.c
字号:
/* tcp4_10localip.c */
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.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 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -