实时系统vxWorks – 增加删除ip
概述
实际应用中,相信各位小伙伴都遇到过以下的情况,只有一个网络端口,但是想要与多台设备通讯,而不同的设备网段又不一样,这个时候需要频繁的修改自己的ip地址,显得很不方便。
windows系统下,操作系统为我们提供了一种可以添加多个ip地址的方法,用户可以很方便的把多个网段ip加入到系统网络中,这样就避免了频繁修改ip的工作,那么在vxWorks下有没有这种机制呢?答案是当然有。
注意
开发环境:vxWorks6.9.4,workbench3.3.5。
另外,小编所有文章均是自己亲手编写验证,由于文件太多,小编就不在公众号后台一一回复列举了,若需要小编的工程代码,请关注公众号,后台回复需要的工程文件。如想要本文中的工程源文件可回复“实时系统vxWorks - 增加删除ip文件”,小编看到后会第一时间回复。
以下为工程目录文件内容。有需要的小伙伴后台发送相关信息给小编获取。
文件内容如下:
obj:存放目标文件。
add_ip:vxWorks应用工程。
接口
库文件
关于网络ip的操作,包含在头文件ifLib.h中,打开帮助文档,可以看到各接口定义。
注意,在使用ifLib.h的时候,需要添加组件INCLUDE_IPWRAP_IFLIB。
/** * @ifLib.h 网络接口库,使用时需添加组件INCLUDE_IPWRAP_IFLIB **/ ifAddrGet( ); /* get the Internet address of a network interface */ ifAddrAdd( ); /* Add an interface address for a network interface */ ifAddrDelete( );/* delete an interface address for a network interface */ ifAddrSet( ); /* set an interface address for a network interface */ ifBroadcastSet( ); /* set the broadcast address for a network interface */ ifBroadcastGet( ); /* get the broadcast address for a network interface */ ifDstAddrGet( );/* get the Internet address of a point-to-point peer */ ifDstAddrSet( ); /* define an address for the other end of a point-to-point link */ ifMaskSet( );/* define a subnet for a network interface */ ifMaskGet( );/* get the subnet mask for a network interface */ ifFlagSet( );/* specify the flags for a network interface */ ifFlagGet( );/* get the network interface flags */ ifFlagChange( );/* change the network interface flags */ ifMetricSet( );/* specify a network interface hop count */ ifMetricGet( );/* get the metric for a network interface */ ifIndexToIfName( );/* returns the interface name given the interface index */ ifNameToIfIndex( );/* returns the interface index given the interface name */ ifProxyArpDisable( );/* disables proxy Arp service on an interface */ ifProxyArpEnable( );/* enables proxy Arp service on an interface */ ifRouteDelete( );/* delete routes associated with a network interface */ ifAllRoutesDelete( );/* delete all routes associated with a network interface */
添加ip
使用接口ifAddrAdd实现
/** * @增加网络地址到指定网络接口上 * @interfaceName:网络接口名,如gei2,ei0等。 * @interfaceAddress:需要添加的IPv4地址 * @broadcastAddress:广播地址,假的? * @subnetMask:子网掩码 * @成功返回OK,失败返回ERROR。 **/ STATUS ifAddrAdd( char *interfaceName, /* name of interface to configure, i.e. ei0 */ char *interfaceAddress, /* IPv4 Address to be added to interface */ char *broadcastAddress, /* Dummy */ int subnetMask /* Optional subnet mask */ )
删除ip
使用接口ifAddrDelete实现
/** * @删除指定网络接口上的指定网络地址 * @interfaceName:网络接口名,如gei2,ei0等。 * @interfaceAddress:需要删除的IPv4地址 * @成功返回OK,失败返回ERROR。 **/ STATUS ifAddrDelete( char *interfaceName, /* name of interface to configure, i.e. ei0 */ char *interfaceAddress /* IPv4 Address to be deleted from the interface */ )
示例
★示例通过编写代码向用户展示如何添加ip和删除ip。
★包含演示程序main.c(已验证通过)。
main.c
/** * @Filename : main.c * @Revision : $Revision: 1.00 $ * @Author : Feng(更多编程相关的知识和源码见微信公众号:不只会拍照的程序猿,欢迎订阅) * @Description : 添加ip和删除ip测试 **/ #include <vxWorks.h> #include <sockLib.h> #include <inetLib.h> #include <hostlib.h> #include "ifLib.h" #include <stdio.h> /** * 添加ip */ void add_ip(void) { if (ifAddrAdd( "gei2", "192.168.100.2", NULL, 0xffffff00) == ERROR) printf("gei2 ip[192.168.100.2] add error...n"); if (ifAddrAdd( "gei2", "192.168.100.3", NULL, 0xffffff00) == ERROR) printf("gei2 ip[192.168.100.3] add error...n"); } /** * 删除ip */ void del_ip(void) { if (ifAddrDelete( "gei2", "192.168.100.2") == ERROR) printf("gei2 ip[192.168.100.2] delete error...n"); if (ifAddrDelete( "gei2", "192.168.100.3") == ERROR) printf("gei2 ip[192.168.100.3] delete error...n"); }
验证
打开镜像工程,添加组件INCLUDE_IPWRAP_IFLIB。
编译镜像并启动目标机。
输入命令ifconfig,查看当前网络状态,可以看到,
目前系统使用一个网口,网口名为”gei2”,ip地址为192.168.10.144。
使用命令行测试
添加IP
使用命令ifAddrAdd "gei2", "192.168.100.2", "192.168.100.0", 0xffffff00,添加ip(192.168.100.2)到网络接口gei2上。
添加完成后,使用ifconfig命令查看是否添加成功。
打开windows命令行,使用ping命令,查看网络是否联通。
删除IP
继续使用命令ifAddrDelete "gei2", "192.168.100.2",删除gei2网口上ip(192.168.100.2)。
删除完成后,使用ifconfig命令查看是否删除成功。
同样打开windows命令行验证网络是否联通。
使用代码的方式测试
添加ip
新建应用工程add_ip,编写相应的代码,编译后下载到目标机。
调用函数add_ip,用于添加ip,调用完成,使用ifconfig查看ip是否添加成功。
打开windows命令行,使用ping命令,查看网络是否联通。
删除ip
调用函数del_ip,用于删除ip,调用完成,使用ifconfig查看ip是否删除成功。
同样打开windows命令行验证网络是否联通。
往期 · 推荐
关注
更多精彩内容,请关注微信公众号:不只会拍照的程序猿,本人致力分享linux、设计模式、C语言、嵌入式、编程相关知识,也会抽空分享些摄影相关内容,同样也分享大量摄影、编程相关视频和源码,另外你若想要获得更多内容教程请关注公众号:不只会拍照的程序猿。