#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <stdlib.h>
#include <linux/ethtool.h>
#include <string.h>
#define ETHTOOL_GDRVINFO 0x00000003
#define SIOCETHTOOL 0x8946
int send_ioctl(int fd, struct ifreq *ifr,void *cmd)
{
ifr->ifr_data = cmd;
return ioctl(fd, SIOCETHTOOL, ifr);
}
static int do_gbus_info(int fd, const char *name,void * ctx)
{
struct ifreq ifr;
int err;
struct ethtool_drvinfo drvinfo;
strcpy(ifr.ifr_name, name);
drvinfo.cmd = ETHTOOL_GDRVINFO;
err = send_ioctl(fd, &ifr, &drvinfo);
if (err < 0) {
perror("Cannot get driver information");
return 71;
}
printf("%s\n", drvinfo.bus_info);
return 0;
}
int main()
{
int i = 0;
int fd;
struct ifconf ifconf;
unsigned char buf[512];
struct ifreq *ifreq;
ifconf.ifc_len = 512;
ifconf.ifc_buf = buf;
struct ethtool_drvinfo drvinfo;
if((fd = socket(AF_INET, SOCK_DGRAM,0)) < 0){
exit(1);
}
printf(" bus-info: [%s]\n",do_gbus_info(fd, "ens33"));
ioctl(fd, SIOCGIFCONF, &ifconf);/*获取所有接口信息*/
ifreq = (struct ifreq *) buf;
/*循环输出所有ip*/
for(i = (ifconf.ifc_len/sizeof(struct ifreq)); i>0; i--){
printf("name is [%s]\n", ifreq->ifr_name);
printf("lcoal addr is [%s]\n", inet_ntoa(((struct sockaddr_in *)&(ifreq->ifr_addr))->sin_addr));
char addr6p[8][5];
FILE *f = NULL;
char addr6[40], devname[21];
int plen, scope, dad_status, if_idx;
if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) {
while(fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
addr6p[0], addr6p[1], addr6p[2], addr6p[3],
addr6p[4], addr6p[5], addr6p[6], addr6p[7],
&if_idx, &plen, &scope, &dad_status, devname) != EOF)
{
if (!strcmp(devname, ifreq->ifr_name)) {
sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",addr6p[0], addr6p[1], addr6p[2], addr6p[3],
addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
printf("addr6 is [%s]\n", addr6);
}
}
}
ifreq ++;
}
return 0;
}
#define _GNU_SOURCE # required for NI_NUMERICHOST
#include <arpa/inet.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <netdb.h>
int main ()
{
struct ifaddrs *ifap, *ifa;
struct sockaddr_in6 *sa;
struct sockaddr_in *sa4;
char addr[INET6_ADDRSTRLEN];
char addr4[INET_ADDRSTRLEN];
int i=0;
if( 0 != getifaddrs (&ifap)) return -1;
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
i++;
if (ifa->ifa_addr->sa_family==AF_INET6) {
sa = (struct sockaddr_in6 *) ifa->ifa_addr;
getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), addr,
sizeof(addr), NULL, 0, NI_NUMERICHOST);
printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr);
} else if(ifa->ifa_addr->sa_family == AF_INET){
sa4 = (struct sockaddr_in *)ifa->ifa_addr;
getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), addr4,
sizeof(addr4), NULL, 0, NI_NUMERICHOST);
/*inet_ntop(AF_INET, sa4, addr4, INET_ADDRSTRLEN);*/
printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr4);
}
}
printf("i = %d\n", i);
freeifaddrs(ifap);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
int main(int argc, char* argv[]){
struct ifaddrs *ifc, *ifc1;
char ip[64] = {};
char nm[64] = {};
if(0 != getifaddrs(&ifc)) return -1;
ifc1 = ifc;
printf("iface\tIP address\tNetmask\n");
for(; NULL != ifc; ifc = (*ifc).ifa_next){
printf("%s", (*ifc).ifa_name);
if(NULL != (*ifc).ifa_addr) {
inet_ntop(AF_INET, &(((struct sockaddr_in*)((*ifc).ifa_addr))->sin_addr), ip, 64);
printf("\t%s", ip);
}else{
printf("\t\t");
}
if(NULL != (*ifc).ifa_netmask){
inet_ntop(AF_INET, &(((struct sockaddr_in*)((*ifc).ifa_netmask))->sin_addr), nm, 64);
printf("\t%s", nm);
}else{
printf("\t\t");
}
printf("\n");
}
freeifaddrs(ifc1);
return 0;
}
编译:gcc network_info_example.c -o nci
运行:./nci
root@lijh-virtual-machine:/home/lijh/c# ./nci
Cannot get driver information: Operation not supported
networkName:lo ipv4:127.0.0.1 ipv6:::1 pci:
networkName:ens33 ipv4:192.168.221.133 ipv6:fe80::f973:61f6:da6d:a1b3 pci:0000:02:01.0
networkName:ens37 ipv4:192.168.221.137 ipv6:fe80::30ab:9db1:20cf:3da1 pci:0000:02:05.0
networkName:ens38 ipv4:192.168.221.138 ipv6:fe80::1b2c:afdd:5ffa:6608 pci:0000:02:06.0