编程以获取/设置Linux系统的网络信息
程序员文章站
2022-06-03 08:17:52
...
说明:文章代码整理自网上其它资料。
1. 获取IP地址、子网掩码地址、Mac地址、广播地址
#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <errno.h>
int GetNetworkInfo(void)
{
int fd;
int interfaceNum = 0;
struct ifreq buf[16];
struct ifconf ifc;
struct ifreq ifrcopy;
char mac[32] = {0};
char ip[32] = {0};
char broadAddr[32] = {0};
char subnetMask[32] = {0};
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
close(fd);
return -1;
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
/* 执行完这句后ifc.ifc_len已经不一定就等于sizeof(buf),而是小于或等于sizeof(buf),取决于网卡的个数 */
if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc))
{
interfaceNum = ifc.ifc_len / sizeof(struct ifreq);
while (interfaceNum-- > 0)
{
ifrcopy = buf[interfaceNum];
if (ioctl(fd, SIOCGIFFLAGS, &ifrcopy))
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
//获取IP地址
if (!ioctl(fd, SIOCGIFADDR, (char *)&buf[interfaceNum]))
{
snprintf(ip, sizeof(ip), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_addr))->sin_addr));
printf("device ip: %s\n", ip);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
//获取子网掩码地址
if (!ioctl(fd, SIOCGIFNETMASK, &buf[interfaceNum]))
{
snprintf(subnetMask, sizeof(subnetMask), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_netmask))->sin_addr));
printf("device subnetMask: %s\n", subnetMask);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
//获取mac地址
if (!ioctl(fd, SIOCGIFHWADDR, (char *)(&buf[interfaceNum])))
{
memset(mac, 0, sizeof(mac));
snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[0],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[1],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[2],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[3],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[4],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[5]);
printf("device mac: %s\n", mac);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
//获取广播地址
if (!ioctl(fd, SIOCGIFBRDADDR, &buf[interfaceNum]))
{
snprintf(broadAddr, sizeof(broadAddr), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_broadaddr))->sin_addr));
printf("device broadAddr: %s\n", broadAddr);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
}
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
close(fd);
return 0;
}
2. 设置IP地址、子网掩码、网关地址
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <net/route.h>
#include <unistd.h>
#include <stdio.h>
int SetNetworkInfo(const char *ifname, const char *ipaddr, const char *netmask, const char *gwip)
{
struct sockaddr_in sin;
struct ifreq ifr;
int fd;
bzero(&ifr, sizeof(struct ifreq));
if (!ifname == NULL || !ipaddr || !gwip) return -1;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
{
perror("Not create network socket connection\n");
return -1;
}
//设置IP地址
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(ipaddr);
memcpy(&ifr.ifr_addr, &sin, sizeof(sin));
if (ioctl(fd, SIOCSIFADDR, &ifr) < 0)
{
perror( "Not setup interface\n ");
return -1;
}
//设置子网掩码
bzero(&ifr, sizeof(struct ifreq));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(netmask);
memcpy(&ifr.ifr_addr, &sin, sizeof(sin));
if(ioctl(fd, SIOCSIFNETMASK, &ifr ) < 0)
{
perror("net mask ioctl error\n");
return -1;
}
//设置网关地址
struct rtentry rm;
bzero(&rm, sizeof(struct rtentry));
rm.rt_dst.sa_family = AF_INET;
rm.rt_gateway.sa_family = AF_INET;
rm.rt_genmask.sa_family = AF_INET;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(gwip);
memcpy(&rm.rt_gateway, &sin, sizeof(sin));
rm.rt_dev = (char *)ifname;
rm.rt_flags = RTF_UP | RTF_GATEWAY ;
if(ioctl(fd, SIOCADDRT, &rm ) < 0)
{
perror("gateway ioctl error\n");
return -1;
}
//**接口
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
{
perror("SIOCSIFFLAGS");
return -1;
}
return 0;
}
上一篇: linux通过c语言接口获取网卡信息
下一篇: Linux时间同步服务