我怎样才能找到编程的IP地址/掩码/配置在Linux中特定的网络设备网关? [英] How can I programmatically find the IP address/netmask/gateway configured for a specific network device in Linux?
问题描述
嗨结果
我想编写一段code的它检查,每个网络设备(例如eth0的,不料,主设备)的一些统计数据和配置数据有关的设备。结果
我能找到的统计数据(以及大多数配置数据)的/ SYS /班/ NET / ...,但是,我无法找到的procfs / sysfs中列出INET地址的C / C ++ API或任何条目,子网掩码和网关。
Hi
I would like to write a piece of code which checks, for each network device (e.g. eth0, lo, master devices) some statistics and configuration data about that device.
I could find the statistics data (and most of the configuration data) in /sys/class/net/..., however, I couldn't find any C/C++ API or any entry in procfs/sysfs listing the inet addr, netmask and gateway.
一些替代我查了一下:
- 分析从使用ifconfig /路由/其他实用程序输出:我不想每次我需要做检查的时间启动子 。
- 解析在/ etc / sysconfig / network中的脚本/:会给我只启动配置,而不是目前的状态
此外,由于此code是用于在我的工作场所,其中的每个外部库彻底检查产品(这意味着它会带我永远添加任何外部库)I preFER解决方案,这依赖于Linux原生API,而不是外部库。
Also, since this code is intended for a product in my workplace, where every external library is inspected thoroughly (meaning it will take me forever to add any external library) I prefer solutions which rely on Linux native API and not external libraries.
谢谢!
推荐答案
有肯定是使用的ifreq和ioctl(的结构)调用,你可以抓住所有接口的信息:
There sure is using a struct of ifreq and ioctl() calls you can grab all interface information:
男人页面在这里<一个href=\"http://h30097.www3.hp.com/docs/dev_doc/DOCUMENTATION/HTML/DDK_R2/DOCS/HTML/MAN/MAN9/0143___S.HTM\">Ifreq手册页
/* local interface info */
typedef struct{
char *iface;
struct ether_addr hwa;
struct in_addr ipa;
struct in_addr bcast;
struct in_addr nmask;
u_short mtu;
} ifcfg_t;
/*
* Grabs local network interface information and stores in a ifcfg_t
* defined in network.h, returns 0 on success -1 on failure
*/
int get_local_info(int rsock, ifcfg_t *ifcfg)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifcfg->iface, IF_NAMESIZE);
if((ioctl(rsock, SIOCGIFHWADDR, &ifr)) == -1){
perror("ioctl():");
return -1;
}
memcpy(&(ifcfg->hwa), &ifr.ifr_hwaddr.sa_data, 6);
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifcfg->iface, IF_NAMESIZE);
if((ioctl(rsock, SIOCGIFADDR, &ifr)) == -1){
perror("ioctl():");
return -1;
}
memcpy(&ifcfg->ipa, &(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr, 4);
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifcfg->iface, IF_NAMESIZE);
if((ioctl(rsock, SIOCGIFBRDADDR, &ifr)) == -1){
perror("ioctl():");
return -1;
}
memcpy(&ifcfg->bcast, &(*(struct sockaddr_in *)&ifr.ifr_broadaddr).sin_addr, 4);
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifcfg->iface, IF_NAMESIZE);
if((ioctl(rsock, SIOCGIFNETMASK, &ifr)) == -1){
perror("ioctl():");
return -1;
}
memcpy(&ifcfg->nmask.s_addr, &(*(struct sockaddr_in *)&ifr.ifr_netmask).sin_addr, 4);
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifcfg->iface, IF_NAMESIZE);
if((ioctl(rsock, SIOCGIFMTU, &ifr)) == -1){
perror("ioctl():");
return -1;
}
ifcfg->mtu = ifr.ifr_mtu;
return 0;
}
快速编辑,此功能要求的接口已被分配它被称为前,像这样:
Quick edit, this function requires that the interface has been assigned before it is called, like so:
strcpy(if_cfg->iface, iface)
确保你第一次分配的内存,然后调用像这样
Ensuring you have allocated the memory first, then call like so
if((get_local_info(sock, if_cfg)) != 0){
printf("Unable to get network device info\n");
return -1;
}
这篇关于我怎样才能找到编程的IP地址/掩码/配置在Linux中特定的网络设备网关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!