通过Socket configuration controls 获取网络设备地址

常用的头文件, 基本通用了.

#ifndef _INC_H #define _INC_H #include <stdio.h> #include <errno.h> #include <time.h> #include <signal.h> #include <stdint.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <getopt.h> #include <inttypes.h> #include <sys/types.h> #include <sys/time.h> #include <sys/file.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/in_systm.h> #include <netinet/in.h> #include <netinet/ip.h> #include <netinet/ip_icmp.h> #include <arpa/inet.h> #include <netdb.h> #include <sys/select.h> #include <net/if.h> #include <sys/ioctl.h> #endif

代码实现:

其他Socket configuration controls可以在/usr/include/bits/ioctls.h中查看到.

static int iface_sock = -1; int iface_init(void) { int sock = -1; sock = socket( AF_INET, SOCK_DGRAM, 0 ); if ( sock < 0 ) { fprintf( stderr, "create socket for iface fail.\n" ); return -1; } iface_sock = sock; return 0; } void iface_destory(void) { if ( iface_sock < 0 ) return ; close( iface_sock ); } int iface_address_by_name(char *devname, uint32_t *addr) { int err = -1; struct ifreq ifr; if ( iface_sock < 0 ) { goto _out; } strcpy( ifr.ifr_name, devname ); err = ioctl( iface_sock, SIOCGIFADDR, &ifr, sizeof( ifr ) ); if ( err < 0 ) { goto _out; } printf( "DEBUG: NIC-%s: %s\n", devname, inet_ntoa( ((struct sockaddr_in *)( &ifr.ifr_addr ))->sin_addr ) ); if ( addr ) { *addr = ((struct sockaddr_in *)( &ifr.ifr_addr ))->sin_addr.s_addr; } err = 0; _out: return err; } int iface_address_by_index(uint32_t index, uint32_t *addr) { int err = -1; struct ifreq ifr; if ( iface_sock < 0 ) { goto _out; } ifr.ifr_ifindex = index; err = ioctl( iface_sock, SIOCGIFADDR, &ifr, sizeof( ifr ) ); if ( err < 0 ) { goto _out; } printf( "NIC-%u-: ip=%s\n", index, inet_ntoa( ((struct sockaddr_in *)( &ifr.ifr_addr ))->sin_addr ) ); if ( addr ) { *addr = ((struct sockaddr_in *)( &ifr.ifr_addr ))->sin_addr.s_addr; } err = 0; _out: return err; } int lookup_nic_address(char *devname, uint32_t *addr) { int err = -1; int sock = 0; struct ifconf ifcs; struct ifreq *ifr; sock = socket( AF_INET, SOCK_DGRAM, 0 ); if ( sock < 0 ) { goto _out; } ifcs.ifc_len = 0; ifcs.ifc_req = NULL; err = ioctl( sock, SIOCGIFCONF, &ifcs, sizeof( ifcs ) ); if ( err < 0 ) { goto _out; } if ( ifcs.ifc_len > 0 ) { ifcs.ifc_req = malloc(ifcs.ifc_len); if (ifcs.ifc_req) { if (ioctl(sock, SIOCGIFCONF, &ifcs, sizeof( ifcs ) ) < 0) { free(ifcs.ifc_req); goto _out; } else { for (ifr = ifcs.ifc_req; (char *)ifr < (char *)ifcs.ifc_req + ifcs.ifc_len; ++ifr) { /*printf("%s\n", ifr->ifr_name);*/ if (!strcmp(ifr->ifr_name, devname )) { printf( "INIT: NIC-%s index=%u ip=%s\n", devname, ifr->ifr_ifindex, inet_ntoa( ((struct sockaddr_in *)( &ifr->ifr_addr ))->sin_addr ) ); if ( addr ) { *addr = ((struct sockaddr_in *)( &ifr->ifr_addr ))->sin_addr.s_addr; } err = 0; break; } } free(ifcs.ifc_req); } } } _out: if ( sock > 0 ) { close( sock ); } return err; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值