💡 需要该C++实用库源码的大佬们,可搜索微信公众号“希望睿智”。添加关注后,输入消息“超级好用的C++实用库”,即可获得源码的下载链接。

概述

        在网络相关的项目中,我们经常需要去获取和设置设备的IP地址、子网掩码、网关地址、MAC地址等信息。这些信息一般与操作系统相关,在Windows系统和Linux系统上调用的接口是不一样的。在Linux系统和基于Linux的一些嵌入式系统上,还涉及一些其他操作,比如:添加指定网卡的路由,使能指定网卡的广播路由,或获取指定网卡的丢包率等。简化所有这些信息的获取与设置,并提供跨平台的统一接口,是非常有必要的。

CHP_Network

        为了方便网络相关信息的获取与设置,我们封装了CHP_Network类。这个类是一个接口类,不需要实例化。因此,我们将构造函数和析构函数声明成了私有的。CHP_Network类的头文件,可参考下面的示例代码。

#pragma once

#include <string>
#include <vector>

#ifndef _WIN32
        #include "HP_Mutex.h"
#endif

typedef struct _THPNetAdaptInfo
{
        std::string strIP;
        std::string strMask;
        std::string strGateway;
        char pMac[6];
}THPNetAdaptInfo;

class CHP_Network
{
public:
        static bool IsValidIP(const char *pszIP);

        static bool IsValidIP(unsigned int uiIP);

        static bool IsLanIP(const char *pszIP);

        static bool IsLanIP(unsigned int uiIP);

        static bool IsValidSubnetMask(const char *pszSubnetMask);

        static bool IsValidSubnetMask(unsigned int uiSubnetMask);
        
        static unsigned int GetHostCountBySubnetMask(const char *pszSubnetMask);

        static unsigned int GetHostCountBySubnetMask(unsigned int uiSubnetMask);

        static int SetDNSServerIP(const std::vector<std::string> &vctIP);

        static int GetDNSServerIP(std::vector<std::string> &vctIP);
      
        static int GetNetworkAdaptInfo(std::vector<THPNetAdaptInfo> &vctInfo);

        static int GetLocalIP(unsigned int &uiIP);

        static int GetLocalIP(std::string &strIP);

        static int GetAllLocalIPs(std::vector<std::string> &vctIP);

        static int GetAllLocalIPs(unsigned int *puiIP, int &nCount);

        static int GetLocalNetworkInfo(unsigned int &uiIP, unsigned int &uiMask, unsigned int &uiGateway, unsigned char pMac[6]);

        static int GetLocalNetworkInfo(std::string &strIP, std::string &strMask, std::string &strGateway, unsigned char pMac[6]);

        static int GetLocalMac(unsigned char pMac[6]);

        static int GetAllLocalMacs(unsigned char ppMac[][6], int &nCount);

        static int GetAllOrderedLocalMacs(unsigned char ppMac[][6], int &nCount);

        static bool IsNetworkAlive(const char *pszNetName = "eth0");

        static int SetIP(unsigned int uiIP, unsigned int uiSubnetMask, const char *pszNetName = "eth0");

        static unsigned int GetIP(const char *pszNetName = "eth0");

        static unsigned int GetSubnetMask(const char *pszNetName = "eth0");

        static unsigned int GetGateway(const char *pszNetName = "eth0");

        static int SetGateway(unsigned int uiGateway, const char *pszNetName = "eth0");

        static bool IsNetworkInterfaceExist(const char *pszNetName = "eth0");

        static int AddRoute(unsigned int uiDestIP, unsigned int uiNetMask, const char *pszNetName = "eth0");

        static int DeleteRoute(unsigned int uiDestIP, unsigned int uiNetMask, unsigned int uiGateway, const char *pszNetName = "eth0");

        static int SetDefaultRoute(unsigned int uiGateway, const char *pszNetName = "eth0");

        static int EnableBroadcastRoute(bool bEnable = true, const char *pszNetName = "eth0");

        static int SetRouteMetric(const char *pszNetName, unsigned int uiMetric);

        static bool IsCablePluggedIn(const char *pszNetName = "eth0");

        static int EnableNetwork(bool bEnable, const char *pszNetName = "eth0");

        static int GetMacAddr(unsigned char pucMac[6], bool bGetFromCmdlineIfFailed = true, 
                const char *pszNetName = "eth0");

        static int SetMacAddr(unsigned char pucMac[6], const char *pszNetName = "eth0");

        static int GetPacketLoss(unsigned int &uiLossRate, const char *pszNetName = "eth0");

private:
        CHP_Network();
        ~CHP_Network();

        static int GetLocalIPList(std::vector<std::string> &vctIP);

#ifndef _WIN32
        typedef struct _TMiiData
        {
                unsigned short phy_id;
                unsigned short reg_num;
                unsigned short val_in;
                unsigned short val_out;
        }TMiiData;

        static int GetMac(unsigned char pMac[6]);
        static int GetCommandToken(const char *pCmd, const char *pToken, const char *pKey, std::string &strResult);
#endif

private:
#ifndef _WIN32
        static CHP_Mutex m_mutexResolvConf;
#endif
};

        下面,我们将分别介绍这些静态接口。

        IsValidIP:是否为有效的IP。参数可以为IP地址字符串,也可以为整型IP地址(网络字节序)。返回值为true表示有效的IP,否则为无效的IP。

        IsLanIP:是否为局域网IP。参数可以为IP地址字符串,也可以为整型IP地址(网络字节序)。返回值为true表示局域网IP,否则为非局域网IP。

        IsValidSubnetMask:是否为有效的子网掩码。参数可以为子网掩码字符串,也可以为整型子网掩码(网络字节序)。返回值为true表示有效的子网掩码,否则为无效的子网掩码。

        GetHostCountBySubnetMask:根据子网掩码获取主机数量。参数可以为子网掩码字符串,也可以为整型子网掩码(网络字节序)。返回值为主机数量。

        SetDNSServerIP:设置DNS服务器的IP地址。参数vctIP为DNS服务器的IP地址的数组。返回值为0表示成功,其他为错误码。

        GetDNSServerIP:获取DNS服务器的IP地址。参数vctIP为DNS服务器的IP地址的数组,用于传出。返回值为0表示成功,其他为错误码。

        GetNetworkAdaptInfo:获取本机的网卡信息。参数vctInfo为本机网卡信息的数组,用于传出。返回值为0表示成功,其他为错误码。

        GetLocalIP:获取本地IP地址。参数可以为整型IP地址(网络字节序),也可以为IP地址字符串。返回值为0表示成功,其他为错误码。

        GetAllLocalIPs:获取本地所有网卡的IP地址。参数可以为STL的向量形式,也可以是数组形式,均用于传出。返回值为0表示成功,其他为错误码。

        GetLocalNetworkInfo:获取本地网络信息,包括:IP、子网掩码、网关、MAC地址。传出的参数可以为整型,也可以为字符串。返回值为0表示成功,其他为错误码。

        GetLocalMac:获取本地MAC地址,一般为第一个有线网卡的地址。参数pMac为MAC地址,用于传出。返回值为0表示成功,其他为错误码。

        GetAllLocalMacs:获取本地所有网卡的MAC地址。参数ppMac为MAC地址数组,参数nCount为传入本地MAC地址数组大小,返回本地MAC地址个数。返回值为0表示成功,其他为错误码。

        GetAllOrderedLocalMacs:获取排序(有线网络>无线网络>其他)后的本地所有网卡的MAC地址。

        IsNetworkAlive:判断指定网卡的网络是否正常。参数pszNetName为网卡接口名称,默认为"eth0",仅对Linux有效。返回值为true表示正常,否则异常。

        SetIP:设置指定网卡的IP等信息(该接口仅对Linux有效)。参数uiIP为整型IP地址(网络字节序),参数uiSubnetMask为整型子网掩码(网络字节序),参数pszNetName为网卡接口名称,默认为"eth0"。返回值为0表示成功,其他为错误码。

        GetIP:获取指定网卡的IP地址(该接口仅对Linux有效)。参数pszNetName为网卡接口名称,默认为"eth0"。返回值为非0表示有效的IP地址,0表示无效的IP地址。

        GetSubnetMask:获取指定网卡的子网掩码(该接口仅对Linux有效)。参数pszNetName为网卡接口名称,默认为"eth0"。返回值为非0表示有效的子网掩码,0表示无效的子网掩码。

        GetGateway:获取指定网卡的网关(该接口仅对Linux有效)。参数pszNetName为网卡接口名称,默认为"eth0"。返回值为整型网关(网络字节序),返回0时,表示没有网关。

        SetGateway:设置指定网卡的网关(该接口仅对Linux有效)。参数uiGateway为整型网关(网络字节序),为-1时,表示删除网关。参数pszNetName为网卡接口名称,默认为"eth0"。返回值为0表示成功,其他为错误码。

        IsNetworkInterfaceExist:判断指定网卡接口是否存在(该接口仅对Linux有效)。参数pszNetName为网卡接口名称,默认为"eth0"。返回值为true表示存在,否则不存在

        AddRoute:添加指定网卡的路由(该接口仅对Linux有效)。参数uiDestIP为整型目标IP(网络字节序),参数uiNetMask为整型子网掩码(网络字节序),参数pszNetName为网卡接口名称,默认为"eth0"。返回值为0表示成功,其他为错误码。

        DeleteRoute:删除指定网卡的路由(该接口仅对Linux有效)。参数uiDestIP为整型目标IP(网络字节序),参数uiNetMask为整型子网掩码(网络字节序),参数uiGateway为整型网关(网络字节序),参数pszNetName为网卡接口名称,默认为"eth0"。返回值为0表示成功,其他为错误码。

        SetDefaultRoute:设置指定网卡的默认路由(该接口仅对Linux有效)。参数uiGateway为整型网关(网络字节序),为-1时, 表示删除指定网卡接口的默认路由;若pszNetName也为NULL,则表示删除所有默认路由。参数pszNetName为网卡接口名称,默认为"eth0"。返回值为0表示成功,其他为错误码。

        EnableBroadcastRoute:使能指定网卡的广播路由(该接口仅对Linux有效)。参数bEnable为是否使能,参数pszNetName为网卡接口名称,默认为"eth0"。返回值为0表示成功,其他为错误码。

        SetRouteMetric:设置指定网卡的路由的metric值(该接口仅对Linux有效),一般用于多网卡时选择和切换活动网络。参数pszNetName为网卡接口名称,参数uiMetric为路由的metric值。返回值为0表示成功,其他为错误码。

        IsCablePluggedIn:判断指定网卡的网线是否插入,一般用于有线网卡(该接口仅对Linux有效)。参数pszNetName为网卡接口名称,默认为"eth0"。返回值为true表示存在,否则不存在。

        EnableNetwork:使能指定网卡的网络(该接口仅对Linux有效)。参数bEnable表示是否使能网络,参数pszNetName为网卡接口名称,默认为"eth0"。返回值为0表示成功,其他为错误码。

        GetMacAddr:获取指定网卡的MAC地址(该接口仅对Linux有效)。参数pucMac为MAC地址,参数bGetFromCmdlineIfFailed表示获取失败时,是否从命令行继续获取(网口损坏时,普通方法获取MAC地址会失败),参数pszNetName为网卡接口名称,默认为"eth0"。返回值为0表示成功,其他为错误码。

        SetMacAddr:设置指定网卡的MAC地址(该接口仅对Linux有效)。参数pucMac为MAC地址,参数pszNetName为网卡接口名称,默认为"eth0"。返回值为0表示成功,其他为错误码。

        GetPacketLoss:获取指定网卡的丢包率(该接口仅对Linux有效)。参数uiLossRate为丢包率,单位为百分比,取值为0到100。参数pszNetName为网卡接口名称,默认为"eth0"。返回值为0表示成功,其他为错误码。

总结

        可以看到,我们提供了非常多实用的网络接口。通过封装网络接口,不仅能够简化开发流程,提升开发效率,还能确保代码质量和系统的长期可维护性。

 💡 需要该源码的大佬们,可以移步至这里下载:【免费】超级好用的C++实用库+跨平台资源-CSDN文库

Logo

欢迎加入西安开发者社区!我们致力于为西安地区的开发者提供学习、合作和成长的机会。参与我们的活动,与专家分享最新技术趋势,解决挑战,探索创新。加入我们,共同打造技术社区!

更多推荐