📄 getnetparam.c
字号:
/* * getnetparam.c - An IP multicast server */
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.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>
#include "msgstruct.h"
nettype *getnetparam() {
struct in_addr myip, mymask;
int fd_arp; /* socket fd for receive packets */
struct ifreq ifr; /* ifr structure in if.h header */
char device[32]; /* ethernet device name */
struct sockaddr_in *sin_ptr;
u_char *ptr;
int n, i=0;
nettype *getparam;
static nettype init ;
getparam = (nettype *)&init;
bzero(getparam, sizeof(nettype));
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;
myip = 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\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)); */
for(i=0;i<6;i++) {
getparam->mac_value[i] = *(ptr+i);
}
getparam->mac_value[i] = 0;
sprintf( getparam->ip_value, "%s", inet_ntoa(myip));
sprintf( getparam->netmask_value, "%s", inet_ntoa(mymask));
strcpy(getparam->message, CONST_MESSAGE);
strcpy(getparam->dev_type,DEVICE_TYPE);
getparam->msg_type = NETPARAM;
getparam->reserve[0] = DEVICE_CHANNEL;
close(fd_arp);
return getparam;
} /* end of main */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -