档案网站建设对比,263企业邮箱入口 邮箱登录,数据分析网站,郴州新网最新招聘信息前面写了一篇关于网络相关的文章#xff1a;如何获取当前可用网口。
《简简单单教你如何用C语言列举当前所有网口#xff01;》
那么如何使用C语言直接操作网口#xff1f;
比如读写IP地址、读写MAC地址等。
一、原理
主要通过系统用socket()、ioctl()、实现
int sock…前面写了一篇关于网络相关的文章如何获取当前可用网口。
《简简单单教你如何用C语言列举当前所有网口》
那么如何使用C语言直接操作网口
比如读写IP地址、读写MAC地址等。
一、原理
主要通过系统用socket()、ioctl()、实现
int socket(int domain, int type, int protocol);
功能创建套接字
参数:domain Name Purpose Man pageAF_UNIX, AF_LOCAL Local communication unix(7)AF_INET IPv4 Internet protocols ip(7)typeSOCK_STREAM Provides sequenced, reliable, two-way, connection-basedbyte streams. An out-of-band data transmission mecha‐nism may be supported.SOCK_DGRAM Supports datagrams (connectionless, unreliable messagesof a fixed maximum length).protocol通常为0
返回值成功新的套接字的文件描述符失败错误码负值int ioctl(int fd, unsigned long request, ...);
参数fd 文件描述符request命令... 参数其中网络用到的request定义头文件位于
/usr/include/linux/sockios.h/* Linux-specific socket ioctls */
#define SIOCINQ FIONREAD
#define SIOCOUTQ TIOCOUTQ /* output queue size (not sent not acked) *//* Routing table calls. */
#define SIOCADDRT 0x890B /* add routing table entry */
#define SIOCDELRT 0x890C /* delete routing table entry */
#define SIOCRTMSG 0x890D /* call to routing system *//* Socket configuration controls. */
#define SIOCGIFNAME 0x8910 /* get iface name */
#define SIOCSIFLINK 0x8911 /* set iface channel */
#define SIOCGIFCONF 0x8912 /* get iface list */
#define SIOCGIFFLAGS 0x8913 /* get flags */
#define SIOCSIFFLAGS 0x8914 /* set flags */
#define SIOCGIFADDR 0x8915 /* get PA address */
#define SIOCSIFADDR 0x8916 /* set PA address */
#define SIOCGIFDSTADDR 0x8917 /* get remote PA address */
#define SIOCSIFDSTADDR 0x8918 /* set remote PA address */
#define SIOCGIFBRDADDR 0x8919 /* get broadcast PA address */
#define SIOCSIFBRDADDR 0x891a /* set broadcast PA address */
#define SIOCGIFNETMASK 0x891b /* get network PA mask */
#define SIOCSIFNETMASK 0x891c /* set network PA mask */
#define SIOCGIFMETRIC 0x891d /* get metric */
#define SIOCSIFMETRIC 0x891e /* set metric */
#define SIOCGIFMEM 0x891f /* get memory address (BSD) */
#define SIOCSIFMEM 0x8920 /* set memory address (BSD) */
#define SIOCGIFMTU 0x8921 /* get MTU size */
#define SIOCSIFMTU 0x8922 /* set MTU size */
#define SIOCSIFNAME 0x8923 /* set interface name */
#define SIOCSIFHWADDR 0x8924 /* set hardware address */
#define SIOCGIFENCAP 0x8925 /* get/set encapsulations */
#define SIOCSIFENCAP 0x8926
#define SIOCGIFHWADDR 0x8927 /* Get hardware address */
#define SIOCGIFSLAVE 0x8929 /* Driver slaving support */
#define SIOCSIFSLAVE 0x8930
#define SIOCADDMULTI 0x8931 /* Multicast address lists */
#define SIOCDELMULTI 0x8932
#define SIOCGIFINDEX 0x8933 /* name - if_index mapping */
#define SIOGIFINDEX SIOCGIFINDEX /* misprint compatibility :-) */
#define SIOCSIFPFLAGS 0x8934 /* set/get extended flags set */
#define SIOCGIFPFLAGS 0x8935
#define SIOCDIFADDR 0x8936 /* delete PA address */
#define SIOCSIFHWBROADCAST 0x8937 /* set hardware broadcast addr */
#define SIOCGIFCOUNT 0x8938 /* get number of devices */
……其中ioctl的参数需要借助结构体struct ifreq 定义头文件
/usr/include/linux/if.h#if __UAPI_DEF_IF_IFREQ
struct ifreq {
#define IFHWADDRLEN 6union{char ifrn_name[IFNAMSIZ]; /* if name, e.g. en0 */} ifr_ifrn;union {struct sockaddr ifru_addr;struct sockaddr ifru_dstaddr;struct sockaddr ifru_broadaddr;struct sockaddr ifru_netmask;struct sockaddr ifru_hwaddr;short ifru_flags;int ifru_ivalue;int ifru_mtu;struct ifmap ifru_map;char ifru_slave[IFNAMSIZ]; /* Just fits the size */char ifru_newname[IFNAMSIZ];void * ifru_data;struct if_settings ifru_settings;} ifr_ifru;
};
#endif /* __UAPI_DEF_IF_IFREQ */#define ifr_name ifr_ifrn.ifrn_name /* interface name */
#define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */
#define ifr_addr ifr_ifru.ifru_addr /* address */
#define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */
#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
#define ifr_netmask ifr_ifru.ifru_netmask /* interface net mask */
#define ifr_flags ifr_ifru.ifru_flags /* flags */
#define ifr_metric ifr_ifru.ifru_ivalue /* metric */
#define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
#define ifr_map ifr_ifru.ifru_map /* device map */
#define ifr_slave ifr_ifru.ifru_slave /* slave device */
#define ifr_data ifr_ifru.ifru_data /* for use by interface */
#define ifr_ifindex ifr_ifru.ifru_ivalue /* interface index */
#define ifr_bandwidth ifr_ifru.ifru_ivalue /* link bandwidth */
#define ifr_qlen ifr_ifru.ifru_ivalue /* Queue length */
#define ifr_newname ifr_ifru.ifru_newname /* New name */
#define ifr_settings ifr_ifru.ifru_settings /* Device/proto settings*/二、函数实现
下面将实现不同功能的函数一一列举。
0. 列出所有可用网口
int list_all_port()
{struct ifconf ifconf;struct ifreq *ifr;int m, n, s, fd;if ((fd socket(AF_INET, SOCK_DGRAM, 0)) 0) {return -11;}s sizeof(struct ifreq)*5;for (;;) {ifr malloc(s);ifconf.ifc_len s;ifconf.ifc_req ifr;if (ioctl(fd, SIOCGIFCONF, ifconf) 0) {perror(SIOCGIFCONF:);free(ifr);close(fd);return 1;}if (ifconf.ifc_len ! s)break;free(ifr);s * 2;}close(fd);m ifconf.ifc_len/sizeof(struct ifreq);for (n 0; n m; n){printf(port:\t%s\n,ifconf.ifc_req[n].ifr_name);}free(ifr);
}1. 获取指定网卡IP
int getLocalIp(const char *eth, char *ip) {struct ifreq ifr;struct sockaddr_in sin;int fd;bzero(ifr, sizeof(ifr));if ((fd socket(AF_INET, SOCK_STREAM, 0)) 0) {return -1;}strcpy(ifr.ifr_name, eth);if (ioctl(fd, SIOCGIFADDR, ifr) 0) {close(fd);return -1;}memcpy(sin, ifr.ifr_addr, sizeof(sin));snprintf(ip, IP_SIZE, %s, inet_ntoa(sin.sin_addr));close(fd);return 0;
}2. 设置本网卡IP地址
int setIpAddrManual(const char *eth, char *ipstr) {int fd;struct sockaddr_in sin;struct ifreq ifr;bzero(ifr, sizeof(ifr));if ((fd socket(AF_INET, SOCK_STREAM, 0)) 0){return -1;}strcpy(ifr.ifr_name, eth);sin.sin_addr.s_addr inet_addr(ipstr);sin.sin_family AF_INET;memcpy(ifr.ifr_addr, sin, sizeof(sin));if (ioctl(fd, SIOCSIFADDR, ifr) 0){perror();close(fd);return -1;}close(fd);return 0;
} 3. 获取本机网卡Mac地址
int getLocalMac(const char *eth, char *mac) {int fd;struct ifreq ifr;bzero(ifr, sizeof(ifr));if ((fd socket(AF_INET, SOCK_STREAM, 0)) 0){return -1;}strcpy(ifr.ifr_name, eth);if (ioctl(fd, SIOCGIFHWADDR, ifr) 0){close(fd);return -1;}snprintf(mac,18, %02x:%02x:%02x:%02x:%02x:%02x,(unsigned char) ifr.ifr_hwaddr.sa_data[0],(unsigned char) ifr.ifr_hwaddr.sa_data[1],(unsigned char) ifr.ifr_hwaddr.sa_data[2],(unsigned char) ifr.ifr_hwaddr.sa_data[3],(unsigned char) ifr.ifr_hwaddr.sa_data[4],(unsigned char) ifr.ifr_hwaddr.sa_data[5]);close(fd);return 0;} 4. 设置网卡mac地址
/*
support format [00:11:22:33:44:55]
*/
#define MAC_ARRAY(mac_array) (unsigned int *)mac_array[0],(unsigned int *)mac_array[1],(unsigned int *)mac_array[2],(unsigned int *)mac_array[3],(unsigned int *)mac_array[4],(unsigned int *)mac_array[5]
int setLocalMac(const char *eth, char *mac) {int fd;struct ifreq ifr;unsigned char mac_array[6] {0};bzero(ifr, sizeof(ifr));if ((fd socket(AF_INET, SOCK_STREAM, 0)) 0){return -1;}strcpy(ifr.ifr_name, eth);ifr.ifr_hwaddr.sa_family AF_LOCAL;sscanf(mac,%02x:%02x:%02x:%02x:%02x:%02x,MAC_ARRAY(mac_array));memcpy(ifr.ifr_hwaddr.sa_data,mac_array,6);if (ioctl(fd, SIOCSIFHWADDR, ifr) 0){perror(SIOCSIFHWADDR:);close(fd);return -1;}close(fd);return 0;
} 注意
网卡地址的第一字节必须是偶数sa_family 值必须为AF_LOCAL
5. 获取网卡mtu
int getMtu(const char *eth, char *mtu) {int fd;struct ifreq ifr;bzero(ifr, sizeof(ifr));if ((fd socket(AF_INET, SOCK_STREAM, 0)) 0){return -1;}strcpy(ifr.ifr_name, eth);if (ioctl(fd, SIOCGIFMTU, ifr) 0){close(fd);return -1;} snprintf(mtu,64, %d, (unsigned char) ifr.ifr_mtu);close(fd);return 0;
} 6. 获取广播地址
int getBroadAddr(const char *eth, char *ip) {int fd;struct sockaddr_in sin;struct ifreq ifr;bzero(ifr, sizeof(ifr));if ((fd socket(AF_INET, SOCK_STREAM, 0)) 0){return -1;}strcpy(ifr.ifr_name, eth);if (ioctl(fd, SIOCGIFBRDADDR, ifr) 0){perror();close(fd);return -1;}memcpy(sin, ifr.ifr_broadaddr, sizeof(sin));snprintf(ip, IP_SIZE, %s, inet_ntoa(sin.sin_addr));close(fd);return 0;
} 7. 获取掩码
int getNetMask(const char *eth, char *mask) {int fd;struct sockaddr_in sin;struct ifreq ifr;bzero(ifr, sizeof(ifr));if ((fd socket(AF_INET, SOCK_STREAM, 0)) 0){return -1;}strcpy(ifr.ifr_name, eth);if (ioctl(fd, SIOCGIFNETMASK, ifr) 0){perror();close(fd);return -1;}memcpy(sin, ifr.ifr_netmask, sizeof(sin));snprintf(mask, IP_SIZE, %s, inet_ntoa(sin.sin_addr));close(fd);return 0;
}8. 获取网卡flag
int getFlags(const char *eth, char *fg) {int fd;struct sockaddr_in sin;struct ifreq ifr;bzero(ifr, sizeof(ifr));if ((fd socket(AF_INET, SOCK_STREAM, 0)) 0){return -1;}strcpy(ifr.ifr_name, eth);if (ioctl(fd, SIOCGIFFLAGS, ifr) 0){perror();close(fd);return -1;}snprintf(fg, IP_SIZE, %x, ifr.ifr_flags);close(fd);return 0;
} 三、测试
1. 测试程序
int main(int argc, char **argv)
{int fg0;char mac[32]{};char ip[IP_SIZE]{0};char buf[64];getBroadAddr(ethname,ip);printf(broad ip:\t%s\n,ip);getNetMask(ethname,ip);printf(mask:\t%s\n,ip);setIpAddrManual(ethname, 1.1.1.1);getLocalIp(ethname,ip);printf(ip:\t%s\n,ip);setLocalMac(ethname,00:11:22:33:44:55);getLocalMac(ethname,mac);printf(mac:\t%s\n,mac);getMtu(ethname,buf);printf(mtu:\t%s\n,buf); return 1;
}2. 执行结果 执行后结果
pengubuntu:~/work/test/ip$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:11:22:33:44:55 inet addr:1.1.1.1 Bcast:1.255.255.255 Mask:255.0.0.0inet6 addr: fe80::d9d4:d42b:a04a:9d40/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:188577 errors:0 dropped:0 overruns:0 frame:0TX packets:208116 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:53762370 (53.7 MB) TX bytes:172094089 (172.0 MB)完整代码获取请点赞转发并后台留言eth 文章转载自: http://www.morning.bkqw.cn.gov.cn.bkqw.cn http://www.morning.homayy.com.gov.cn.homayy.com http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.jbysr.cn.gov.cn.jbysr.cn http://www.morning.rbbyd.cn.gov.cn.rbbyd.cn http://www.morning.kpgft.cn.gov.cn.kpgft.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.ggxbyhk.cn.gov.cn.ggxbyhk.cn http://www.morning.yrddl.cn.gov.cn.yrddl.cn http://www.morning.dbdmr.cn.gov.cn.dbdmr.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.nqyzg.cn.gov.cn.nqyzg.cn http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn http://www.morning.dyzbt.cn.gov.cn.dyzbt.cn http://www.morning.kpmxn.cn.gov.cn.kpmxn.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.bzcjx.cn.gov.cn.bzcjx.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.qjzgj.cn.gov.cn.qjzgj.cn http://www.morning.xpmhs.cn.gov.cn.xpmhs.cn http://www.morning.dfhkh.cn.gov.cn.dfhkh.cn http://www.morning.kgnnc.cn.gov.cn.kgnnc.cn http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.gxwyr.cn.gov.cn.gxwyr.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.smyxl.cn.gov.cn.smyxl.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.dpfr.cn.gov.cn.dpfr.cn http://www.morning.nkjkh.cn.gov.cn.nkjkh.cn http://www.morning.cpktd.cn.gov.cn.cpktd.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.jrdbq.cn.gov.cn.jrdbq.cn http://www.morning.jiuyungps.com.gov.cn.jiuyungps.com http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn http://www.morning.trplf.cn.gov.cn.trplf.cn http://www.morning.yxshp.cn.gov.cn.yxshp.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.wqkzf.cn.gov.cn.wqkzf.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.plpqf.cn.gov.cn.plpqf.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.mzqhb.cn.gov.cn.mzqhb.cn http://www.morning.gccdr.cn.gov.cn.gccdr.cn http://www.morning.dmtld.cn.gov.cn.dmtld.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.gltmz.cn.gov.cn.gltmz.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.pxlql.cn.gov.cn.pxlql.cn http://www.morning.mjpgl.cn.gov.cn.mjpgl.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.zcsyz.cn.gov.cn.zcsyz.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.sypby.cn.gov.cn.sypby.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.rxnr.cn.gov.cn.rxnr.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.lywpd.cn.gov.cn.lywpd.cn http://www.morning.heleyo.com.gov.cn.heleyo.com http://www.morning.pbknh.cn.gov.cn.pbknh.cn http://www.morning.swdnr.cn.gov.cn.swdnr.cn http://www.morning.jyfrz.cn.gov.cn.jyfrz.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.jgttx.cn.gov.cn.jgttx.cn http://www.morning.wdply.cn.gov.cn.wdply.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.yfcbf.cn.gov.cn.yfcbf.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn