网络接口,获取local ip broadcast ip
最近在ESP8266上面需要移植一些库,中间遇到需要获取 broadcast ip,原代码如下:
int fd = -1; struct ifreq ifr; char ip_buf[IP_LEN]; (void)ip_len; /**create fd */ fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { printf("get broadcast ip error\n\r"); return -1; } /**get broadcast addr */ bzero(&ifr, sizeof(ifr)); strcpy(ifr.ifr_name, "eth0"); /**notice: change to the right one*/ if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0) { printf("ioctl SIOCGIFBRDADDR error\n\r"); close(fd); return -1; } memset(ip_buf, 0, IP_LEN); sprintf(ip_buf, "%s", inet_ntoa(((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr)); strncpy(ip, ip_buf, strlen(ip_buf)); close(fd); printf("local broadcast:%s\n\r", ip_buf);
参考如下:
https://blog.csdn.net/q_l_s/article/details/52716730
#include<stdlib.h> #include<stdio.h> #include<unistd.h> #include<net/if.h> #include<net/if_arp.h> #include<arpa/inet.h> #include<sys/ioctl.h> #include<errno.h> #define ETH_NAME "eth0" static void get_local_ip(int sock_fd); static void get_netmask_addr(int sock_fd); static void get_mac_addr(int sock_fd); static void get_broad_addr(int sock_fd); struct sockaddr_in sin; struct sockaddr_in netmask; struct sockaddr_in broad; struct ifreq ifr; unsigned char arp[6]; int main() { int sockfd; //create socket sockfd = socket(AF_INET,SOCK_DGRAM,0); if(sockfd == -1) { perror("socket"); return -1; } get_local_ip(sockfd); get_mac_addr(sockfd); get_netmask_addr(sockfd); get_broad_addr(sockfd);
close(fd);//我加的
return 0; }
static void get_local_ip(int sock_fd) { strncpy(ifr.ifr_name,ETH_NAME,IFNAMSIZ); ifr.ifr_name[IFNAMSIZ - 1] = 0; if(ioctl(sock_fd,SIOCGIFADDR,&ifr) == 0) { memcpy(&sin,&ifr.ifr_addr,sizeof(sin)); fprintf(stdout,"eth0: %s\n",inet_ntoa(sin.sin_addr)); } } static void get_mac_addr(int sock_fd) { if(ioctl(sock_fd,SIOCGIFHWADDR,&ifr)==0) { memcpy(arp,ifr.ifr_hwaddr.sa_data,6); printf("adapter hardware address %x:%x:%x:%x:%x:%x\n",arp[0],arp[1],arp[2],arp[3],arp[4],arp[5]); } } static void get_netmask_addr(int sock_fd) { if(ioctl(sock_fd,SIOCGIFNETMASK,&ifr)==0) { memcpy(&netmask,&ifr.ifr_netmask,sizeof(netmask)); printf("netmask: %s\n",inet_ntoa(netmask.sin_addr)); } } static void get_broad_addr(int sock_fd) { if(ioctl(sock_fd,SIOCGIFBRDADDR,&ifr)==0) { memcpy(&broad,&ifr.ifr_broadaddr,sizeof(broad)); printf("broad: %s\n",inet_ntoa(broad.sin_addr)); } }
- #include<stdlib.h>
- #include<stdio.h>
- #include<unistd.h>
- #include<net/if.h>
- #include<net/if_arp.h>
- #include<arpa/inet.h>
- #include<sys/ioctl.h>
- #include<errno.h>
- #define ETH_NAME “eth0”
- static void get_local_ip(int sock_fd);
- static void get_netmask_addr(int sock_fd);
- static void get_mac_addr(int sock_fd);
- static void get_broad_addr(int sock_fd);
- struct sockaddr_in sin;
- struct sockaddr_in netmask;
- struct sockaddr_in broad;
- struct ifreq ifr;
- unsigned char arp[6];
- int main()
- {
- int sockfd;
- //create socket
- sockfd = socket(AF_INET,SOCK_DGRAM,0);
- if(sockfd == -1)
- {
- perror(“socket”);
- return -1;
- }
- get_local_ip(sockfd);
- get_mac_addr(sockfd);
- get_netmask_addr(sockfd);
- get_broad_addr(sockfd);
- return 0;
- }
- static void get_local_ip(int sock_fd)
- {
- strncpy(ifr.ifr_name,ETH_NAME,IFNAMSIZ);
- ifr.ifr_name[IFNAMSIZ – 1] = 0;
- if(ioctl(sock_fd,SIOCGIFADDR,&ifr) == 0)
- {
- memcpy(&sin,&ifr.ifr_addr,sizeof(sin));
- fprintf(stdout,“eth0: %s\n”,inet_ntoa(sin.sin_addr));
- }
- }
- static void get_mac_addr(int sock_fd)
- {
- if(ioctl(sock_fd,SIOCGIFHWADDR,&ifr)==0)
- {
- memcpy(arp,ifr.ifr_hwaddr.sa_data,6);
- printf(“adapter hardware address %x:%x:%x:%x:%x:%x\n”,arp[0],arp[1],arp[2],arp[3],arp[4],arp[5]);
- }
- }
- static void get_netmask_addr(int sock_fd)
- {
- if(ioctl(sock_fd,SIOCGIFNETMASK,&ifr)==0)
- {
- memcpy(&netmask,&ifr.ifr_netmask,sizeof(netmask));
- printf(“netmask: %s\n”,inet_ntoa(netmask.sin_addr));
- }
- }
- static void get_broad_addr(int sock_fd)
- {
- if(ioctl(sock_fd,SIOCGIFBRDADDR,&ifr)==0)
- {
- memcpy(&broad,&ifr.ifr_broadaddr,sizeof(broad));
- printf(“broad: %s\n”,inet_ntoa(broad.sin_addr));
- }
- }