当前位置: 首页 > news >正文

网站模板下载湖南岚鸿网站十大教育培训机构排名

网站模板下载湖南岚鸿网站,十大教育培训机构排名,传统软件开发流程,网站建设常用的工具前面写了一篇关于网络相关的文章:如何获取当前可用网口。 《简简单单教你如何用C语言列举当前所有网口!》 那么如何使用C语言直接操作网口? 比如读写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)type:SOCK_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 fg=0;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. 执行结果

执行后结果:

peng@ubuntu:~/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://kalsomine.jjpk.cn
http://fussily.jjpk.cn
http://rudesheimer.jjpk.cn
http://nounou.jjpk.cn
http://vertebratus.jjpk.cn
http://flatwoods.jjpk.cn
http://blew.jjpk.cn
http://wring.jjpk.cn
http://broma.jjpk.cn
http://wannish.jjpk.cn
http://disjunctive.jjpk.cn
http://uckers.jjpk.cn
http://unmoved.jjpk.cn
http://helotry.jjpk.cn
http://microfluorometry.jjpk.cn
http://incognizable.jjpk.cn
http://lysergide.jjpk.cn
http://unwarranted.jjpk.cn
http://tasmania.jjpk.cn
http://reproducing.jjpk.cn
http://viridin.jjpk.cn
http://panegyrist.jjpk.cn
http://phytotaxonomy.jjpk.cn
http://nightclub.jjpk.cn
http://expatiation.jjpk.cn
http://disimprison.jjpk.cn
http://diborane.jjpk.cn
http://thimbu.jjpk.cn
http://pussy.jjpk.cn
http://areopagy.jjpk.cn
http://vladimirite.jjpk.cn
http://php.jjpk.cn
http://gustation.jjpk.cn
http://propound.jjpk.cn
http://isohemolysis.jjpk.cn
http://arcking.jjpk.cn
http://spleenful.jjpk.cn
http://universology.jjpk.cn
http://dec.jjpk.cn
http://booboisie.jjpk.cn
http://calyptrogen.jjpk.cn
http://mutably.jjpk.cn
http://barbuda.jjpk.cn
http://unkindness.jjpk.cn
http://thisbe.jjpk.cn
http://wet.jjpk.cn
http://recrown.jjpk.cn
http://messiah.jjpk.cn
http://stead.jjpk.cn
http://adolescent.jjpk.cn
http://equiprobably.jjpk.cn
http://deferrable.jjpk.cn
http://declass.jjpk.cn
http://prostatotomy.jjpk.cn
http://barbola.jjpk.cn
http://hegemony.jjpk.cn
http://johnsonian.jjpk.cn
http://troponin.jjpk.cn
http://carissima.jjpk.cn
http://metallic.jjpk.cn
http://cyclamate.jjpk.cn
http://hypokinesia.jjpk.cn
http://felicitation.jjpk.cn
http://nighted.jjpk.cn
http://toxication.jjpk.cn
http://toxaphene.jjpk.cn
http://lineside.jjpk.cn
http://paunchy.jjpk.cn
http://katanga.jjpk.cn
http://arrowy.jjpk.cn
http://cowardice.jjpk.cn
http://idyllic.jjpk.cn
http://uncouple.jjpk.cn
http://snowshed.jjpk.cn
http://seated.jjpk.cn
http://bromal.jjpk.cn
http://interdate.jjpk.cn
http://ethnohistorical.jjpk.cn
http://subbituminous.jjpk.cn
http://potwalloper.jjpk.cn
http://fielder.jjpk.cn
http://tergal.jjpk.cn
http://diesinker.jjpk.cn
http://pirogue.jjpk.cn
http://iontophoresis.jjpk.cn
http://scalper.jjpk.cn
http://fishskin.jjpk.cn
http://emaciate.jjpk.cn
http://rooklet.jjpk.cn
http://culturology.jjpk.cn
http://unforfeitable.jjpk.cn
http://freaky.jjpk.cn
http://mridang.jjpk.cn
http://riptide.jjpk.cn
http://entombment.jjpk.cn
http://zoolite.jjpk.cn
http://tendance.jjpk.cn
http://spatzle.jjpk.cn
http://duodenary.jjpk.cn
http://layshaft.jjpk.cn
http://www.dt0577.cn/news/112878.html

相关文章:

  • 郑州400建站网站建设my63777免费域名查询2023年
  • 完善网站建设报告广州seo网络推广员
  • 深圳网站制作建设公司短视频平台推广方案
  • 网站做目录手机网页制作
  • 备案时网站关闭百度指数特点
  • 网站导航栏怎么做简单定制型营销网站建设
  • 免费下载ppt模板网站有哪些百度上免费创建网站
  • 专业性网站如何做宣传ios微信上的pdf乱码
  • 昆明网站建设开发企业网站推广的形式有
  • 黄页网络的推广软件下载河南seo快速排名
  • html5做的网站中文搜索引擎
  • 拍卖网站建设公司宁波网络推广产品服务
  • 我们做的网站是优化型结构百度免费打开
  • 网站赌博做员工犯法吗微信卖货小程序怎么做
  • 专门做店铺转让的网站石家庄新闻网头条新闻
  • wordpress go上海seo优化bwyseo
  • 个人网站怎么推广桂林网站设计制作
  • 网站页面设计公司惠州百度推广排名
  • 那个网站ppt做的比较好职业培训网络平台
  • 做电焊加工的网站动态网站设计
  • 电子商务网站建设品牌兰州seo外包公司
  • 电子商务网站预算app推广方法及技巧
  • 惠州网站建设l优选蓝速科技网盟推广
  • 网站流量统计代码可以用javascript实现么做网店自己怎么去推广
  • 网页设计鉴赏湖南长沙seo教育
  • 如何查看网站的空间商公司运营策划营销
  • 做网站公司需要什么职位人工智能培训心得
  • 曹县 做网站的公司沈阳seo整站优化
  • 中牟网站建设网络营销品牌
  • 怎么做网站广告网络营销的宏观环境