实时系统vxWorks – 增加删除ip

概述

实际应用中,相信各位小伙伴都遇到过以下的情况,只有一个网络端口,但是想要与多台设备通讯,而不同的设备网段又不一样,这个时候需要频繁的修改自己的ip地址,显得很不方便。

windows系统下,操作系统为我们提供了一种可以添加多个ip地址的方法,用户可以很方便的把多个网段ip加入到系统网络中,这样就避免了频繁修改ip的工作,那么在vxWorks下有没有这种机制呢?答案是当然有。

注意

开发环境:vxWorks6.9.4,workbench3.3.5。

另外,小编所有文章均是自己亲手编写验证,由于文件太多,小编就不在公众号后台一一回复列举了,若需要小编的工程代码,请关注公众号,后台回复需要的工程文件。如想要本文中的工程源文件可回复“实时系统vxWorks - 增加删除ip文件”,小编看到后会第一时间回复。

以下为工程目录文件内容。有需要的小伙伴后台发送相关信息给小编获取。

f9b94e6ee2ed544e974efcbe17fcee10.png

文件内容如下:

obj:存放目标文件。

add_ip:vxWorks应用工程。 

接口

库文件

关于网络ip的操作,包含在头文件ifLib.h中,打开帮助文档,可以看到各接口定义。

注意,在使用ifLib.h的时候,需要添加组件INCLUDE_IPWRAP_IFLIB

c32a113f623805bce49be798835b1317.png

/**
 * @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实现

3caa64ea924ddeb1c410a2169f2d8675.png

/**
 * @增加网络地址到指定网络接口上
 * @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实现

670ec46aef8f0f970415fe4b31c637de.png

/**
 * @删除指定网络接口上的指定网络地址
 * @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(已验证通过)。

d4c7e08081ab29ec635a5a40fc850228.png 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。

9953981470c7e07c20c111db405353a9.png

编译镜像并启动目标机。

输入命令ifconfig,查看当前网络状态,可以看到,

目前系统使用一个网口,网口名为”gei2”,ip地址为192.168.10.144。 

0fb1a445e5e16a0cd52016439579e2bd.png

使用命令行测试

添加IP

使用命令ifAddrAdd "gei2", "192.168.100.2", "192.168.100.0", 0xffffff00,添加ip(192.168.100.2)到网络接口gei2上。

添加完成后,使用ifconfig命令查看是否添加成功。

5a971bf0a2541d3762c17af859320185.png

打开windows命令行,使用ping命令,查看网络是否联通。

ecbb449183bffe014aec491b7d6d845e.png

删除IP

继续使用命令ifAddrDelete "gei2", "192.168.100.2",删除gei2网口上ip(192.168.100.2)。

删除完成后,使用ifconfig命令查看是否删除成功。

739828cfe4b4c99f73b13fc94bdc4090.png

同样打开windows命令行验证网络是否联通。

209238a0fd9f20ddade58d738946457b.png

使用代码的方式测试

添加ip

新建应用工程add_ip,编写相应的代码,编译后下载到目标机。

调用函数add_ip,用于添加ip,调用完成,使用ifconfig查看ip是否添加成功。

3e8faae85052e9dbbc20a8f910f537c8.png

打开windows命令行,使用ping命令,查看网络是否联通。

c05324ecdc0b22561bb3aae8060ec83b.png

删除ip

调用函数del_ip,用于删除ip,调用完成,使用ifconfig查看ip是否删除成功。

63abdb3c0650a25ce9b4c2b705e3328f.png

同样打开windows命令行验证网络是否联通。

1c23938b3acfe2dfb1eab12fe77a72a3.png

往期 · 推荐

实时系统vxWorks - 任务(重要)

实时系统vxWorks - 加载应用程序的方法

实时系统vxWorks - 在线调试

实时系统vxWorks - 虚拟机环境搭建

实时系统vxWorks - zynq7020移植vxWorks

关注

更多精彩内容,请关注微信公众号:不只会拍照的程序猿,本人致力分享linux、设计模式、C语言、嵌入式、编程相关知识,也会抽空分享些摄影相关内容,同样也分享大量摄影、编程相关视频和源码,另外你若想要获得更多内容教程请关注公众号:不只会拍照的程序猿

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇

)">
下一篇>>