2

onps栈使用说明(2)——ping、域名解析等网络工具测试 - Neo-T

 1 year ago
source link: https://www.cnblogs.com/neo-T/p/onps-5.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

1. ping测试

       协议栈提供ping工具,其头文件为“net_tools/ping.h”,将其include进你的目标系统中即可使用这个工具。

……
#include "onps.h"
#include "net_tools/ping.h"

//* 回调函数,收到目标地址的应答报文后ping工具会调用这个函数完成用户的特定处理逻辑
//* 针对这个测试,在这里就是简单地打印出了应答报文的内容以及ping的响应时间
static void  ping_recv_handler(USHORT usIdentifier,   //* ping的标识id,响应报文与探测报文这个id应该一致
                               in_addr_t unFromAddr,  //* 响应报文的源地址
                               USHORT usSeqNum,       //* 响应报文序号,其与探测报文一致
                               UCHAR *pubEchoData,    //* 响应报文携带的响应数据,其与探测报文一致
                               UCHAR ubEchoDataLen,   //* 响应报文携带的数据长度
                               UCHAR ubTTL,           //* ttl值
                               UCHAR ubElapsedMSecs)  //* 响应时长,单位:秒,从发送探测报文开始计时到收到响应报文结束计时
{
    CHAR szSrcAddr[20];
    struct in_addr stInAddr; 
    stInAddr.s_addr = unFromAddr; 
    printf("<Fr>%s, recv %d bytes, ID=%d, Sequence=%d, Data='%s', TTL=%d, time=%dms\r\n", 
           inet_ntoa_safe(stInAddr, szSrcAddr), //* 这是一个线程安全的ip地址转ascii字符串函数
           (UINT)ubEchoDataLen, 
           usIdentifier, 
           usSeqNum, 
           pubEchoData, 
           (UINT)ubTTL, 
           (UINT)ubElapsedMSecs);
}

int main(void)
{
    if(open_npstack_load(&enErr))
    {    
        printf("The open source network protocol stack (ver %s) is loaded successfully. \r\n", ONPS_VER);
        
        //* 协议栈加载成功,在这里初始化ethernet网卡或等待ppp链路就绪
    #if 0
        emac_init(); //* ethernet网卡初始化函数,并注册网卡到协议栈
    #else
        while(!netif_is_ready("ppp0")) //* 等待ppp链路建立成功
            os_sleep_secs(1); 
    #endif
    }
    else
    {
        printf("The open source network protocol stack failed to load, %s\r\n", onps_error(enErr));
        return -1; 
    }
    
    //* 启动ping测试
    USHORT usSeqNum = 0;
    UINT unErrCount = 0; 
    INT nPing = ping_start(&enErr); 
    if(nPing < 0)
    {
        //* 启动失败,输出一条日志信息
        printf("ping_start() failed, %s\r\n", onps_error(enErr));
        return -1; 
    }
    
    while(TRUE && usSeqNum < 100)
    {
        //* ping目标地址
        INT nRtnVal = ping(nPing, inet_addr("192.168.0.2"), usSeqNum++, 64, GetElapsedMSecs, ping_recv_handler, 3, &enErr);
        if(nRtnVal <= 0) //* ping返回一个错误
        {
            //* 累计ping错误数
            unErrCount++; 

            //* 控制台打印当前错误数        
            printf("no reply received, the current number of errors is %d, current error: %s\r\n", unErrCount, nRtnVal ? onps_error(enErr) : "recv timeout"); 
        }
        os_sleep_secs(1); 
    }    
    
    //* 结束ping测试
    ping_end(nPing);
    
    return 0; 
}

上述示例代码调用了ping测试工具提供的几个api函数。ping_start()函数的调用非常简单,其功能就是开启ping测试。结束ping测试需要调用ping_end()函数,否则ping测试会一直占用协议栈资源。这几个函数的说明如下:

函数原型

INT ping_start(EN_ONPSERR *penErr);

功能

启动ping测试。注意,启动后你可以随时更换目标地址,不必拘泥于一个固定的目标地址。

参数

  • penErr:如果启动失败,该参数用于接收具体的错误码

返回值

成功,返回当前启动的ping测试任务的句柄;失败,返回值小于0,具体错误信息参看pennErr保存的错误码。

函数原型

void ping_end(INT nPing);

功能

结束ping测试,释放占用的协议栈资源。

参数

  • nPing:ping_start()函数返回的ping测试句柄

返回值

函数原型

INT ping(INT nPing,

         in_addr_t unDstAddr,

         USHORT usSeqNum,

         UCHAR ubTTL,

         UINT(*pfunGetCurMSecs)(void),

         void(*pfunRcvHandler)(USHORT usIdentifier,

                               in_addr_t unFromAddr,

                               USHORT usSeqNum,

                               UCHAR *pubEchoData,

                               UCHAR ubEchoDataLen,

                               UCHAR ubTTL,

                               UCHAR ubElapsedMSecs),

         UCHAR ubWaitSecs,

         EN_ONPSERR *penErr);

功能

ping目标地址并等待接收对端的响应报文。其功能与通用的ping测试工具完全相同。

参数

  • nPing:ping_start()函数返回的ping测试句柄
  • unDstAddr:目标地址
  • usSeqNum:报文序号,用于唯一的标识发出的探测报文
  • ubTTL:ttl值,探测报文到达目标地址之前的生存时间
  • pfunGetCurMSecs:函数指针,这个函数返回系统定时器自启动以来的工作时长,单位:毫秒,其精度取决于os定时器粒度,其用于统计
  • ping的响应时间,必须提供这个函数,否则ping操作将无法完成
  • pfunRcvHandler:函数指针,收到响应报文后将调用这个函数完成用户指定的操作,ping_recv_handler()就是它的实际实现
  • ubWaitSecs:响应报文的最长等待时间,单位:秒
  • penErr:如果ping失败,该参数保存具体的错误码

返回值

大于0,ping成功;0,等待响应报文超时;小于0,失败,具体的错误信息参看pennErr保存的错误码。

2. dns测试

       这个测试需要为网卡设定好能够访问互联网的网关、DNS服务器地址等配置信息。当然如果采用dhcp动态地址申请的方式能够得到这些信息那就更省事了。dns查询工具的头文件为"net_tools/dns.h"。

……
#include "onps.h"
#include "net_tools/ping.h"
int main(void)
{
    if(open_npstack_load(&enErr))
    {    
        printf("The open source network protocol stack (ver %s) is loaded successfully. \r\n", ONPS_VER);
        
        //* 协议栈加载成功,在这里初始化ethernet网卡或等待ppp链路就绪
    #if 0
        emac_init(); //* ethernet网卡初始化函数,并注册网卡到协议栈
    #else
        while(!netif_is_ready("ppp0")) //* 等待ppp链路建立成功
            os_sleep_secs(1); 
    #endif
    }
    else
    {
        printf("The open source network protocol stack failed to load, %s\r\n", onps_error(enErr));
        return -1; 
    }
    
    //* dns查询测试
    in_addr_t unPrimaryDNS, unSecondaryDNS;
    INT nDnsClient = dns_client_start(&unPrimaryDNS, &unSecondaryDNS, 3, &enErr);
    if(nDnsClient < 0)
    {
        //* dns客户端启动失败,输出一条错误日志
        printf("%s\r\n", onps_error(enErr)); 
    }
    else
    {
        //* 发送查询请求并等待dns服务器的应答
        in_addr_t unIp = dns_client_query(nDnsClient, unPrimaryDNS, unSecondaryDNS, "gitee.com", &enErr); 
        if(unIp) //* 查询成功
        {
            CHAR szAddr[20];
            printf("The ip addr: %s\r\n", inet_ntoa_safe_ext(unIp, szAddr));
        }
        else
            printf("%s\r\n", onps_error(enErr)); //* 查询失败
        
        //* 结束dns查询,释放占用的协议栈资源
        dns_client_end(nDnsClient);
    }
    
    return 0; 
}

与ping测试工具相同,dns查询工具同样提供了一组简单的api函数用于实现域名查询。这一组函数包括dns_client_start()、dns_client_end()以及dns_client_query(),其使用说明如下:

函数原型

INT dns_client_start(in_addr_t *punPrimaryDNS, in_addr_t *punSecondaryDNS, CHAR bRcvTimeout, EN_ONPSERR *penErr);

功能

启动一个域名查询客户端。

参数

  • punPrimaryDNS:指针类型,用于接收主域名服务器地址。协议栈会选择缺省路由绑定的网卡,并得到网卡携带的dns服务器地址用于接下来的域名查询,该参数即用于保存主dns服务器的地址
  • punSecondaryDNS:指针类型,与上同,用于接收次域名服务器地址
  • bRcvTimeout:查询超时时间,单位:秒
  • penErr:如果启动失败,该参数用于接收具体的错误码

返回值

成功,返回当前启动的dns客户端的句柄;失败,返回值小于0,具体错误信息参看pennErr保存的错误码。

函数原型

void dns_client_end(INT nClient);

功能

结束dns客户端,释放占用的协议栈资源。

参数

  • nClient:dns_client_start()函数返回的dns客户端句柄

返回值

函数原型

in_addr_t dns_client_query(INT nClient, in_addr_t unPrimaryDNS, in_addr_t unSecondaryDNS, const CHAR *pszDomainName, EN_ONPSERR *penErr);

功能

发送dns查询请求,并等待服务器的响应报文,功能与通用的dns客户端完全相同。

参数

  • nClient:dns_client_start()函数返回的dns客户端句柄
  • punPrimaryDNS:主域名服务器地址,其值为dns_client_start()函数得到的主域名服务器地址
  • punSecondaryDNS:次域名服务器地址,其值为dns_client_start()函数得到的次域名服务器地址
  • pszDomainName:指针类型,指向要查询的域名
  • penErr:如果查询失败,该参数用于接收具体的错误码

返回值

成功,返回域名对应的ip地址;失败,返回值为0,具体错误信息参看pennErr保存的错误码。

3. sntp网络校时测试

       与dns的测试要求一样,要进行这个测试依然要确保你的开发板在物理层能够访问互联网,同时你的开发板支持rtc,并提供一组rtc操作函数,包括读取、设置系统当前时间等api。这里假设你的测试环境已经具备上述测试条件。sntp网络校时工具的头文件为"net_tools/sntp.h"。

……
#include "onps.h"
#include "net_tools/sntp.h"
int main(void)
{
    if(open_npstack_load(&enErr))
    {    
        printf("The open source network protocol stack (ver %s) is loaded successfully. \r\n", ONPS_VER);
        
        //* 协议栈加载成功,在这里初始化ethernet网卡或等待ppp链路就绪
    #if 0
        emac_init(); //* ethernet网卡初始化函数,并注册网卡到协议栈
    #else
        while(!netif_is_ready("ppp0")) //* 等待ppp链路建立成功
            os_sleep_secs(1); 
    #endif
    }
    else
    {
        printf("The open source network protocol stack failed to load, %s\r\n", onps_error(enErr));
        return -1; 
    }
    
    //* 先设定个不合理的时间,以测试网络校时功能是否正常,由rtc驱动提供,负责修改系统当前时间
    //* RTC前缀的函数为目标系统应提供的rtc时钟操作函数
    RTCSetSysTime(22, 9, 5, 17, 42, 30); 
    
    //* 开启网络校时,sntp_update_by_ip()与sntp_update_by_dns()均可使用
    ST_DATETIME stDateTime;     
#if 1    
    if(sntp_update_by_ip("52.231.114.183", NULL, RTCSetSystemUnixTimestamp, 8, &enErr)) //* ntp服务器地址直接校时
#else
    if(sntp_update_by_dns("time.windows.com", Time, RTCSetSystemUnixTimestamp, 8, &enErr)) //* ntp服务器域名方式校时
#endif      
    {
        //* 获取系统时间,检查校时结果
        RTCGetSysTime(&stDateTime);
        
        //* 控制台输出当前系统时间
        printf("The time is %d-%02d-%02d %02d:%02d:%02d\r\n", stDateTime.usYear, stDateTime.ubMonth, 
               stDateTime.ubDay, stDateTime.ubHour, stDateTime.ubMin, stDateTime.ubSec);
    }
    else
    {        
        printf("%s\r\n", onps_error(enErr)); 
        return -1; 
    }
    
    return 0; 
}

测试代码首先把时间设定在了2022年9月5日17点42分30秒,目的是为了验证目标系统时间是否会被成功校正。测试代码用到了目标系统应提供的一组rtc时钟操作函数。其中RTCSetSysTime()用于设置系统时间。RTCSetSystemUnixTimestamp()函数同样也是设置系统时间,只不过是通过unix时间戳进行设置。RTCGetSysTime()函数用于读取当前系统时间。相较于ping及dns工具,sntp网络校时工具只提供了一个接口函数sntp_update_by_xx()即可完成校时。我们可以通过ntp服务器地址也可以通过ntp服务器域名进行校时。该函数的详细使用说明如下:

函数原型

BOOL sntp_update_by_ip(const CHAR *pszNtpSrvIp, time_t(*pfunTime)(void), void(*pfunSetSysTime)(time_t), CHAR bTimeZone, EN_ONPSERR *penErr);

功能

发送一个校时请求到pszNtpSrvIp参数指定的ntp服务器,并等待服务器的响应报文,完成校时操作。

参数

  • pszNtpSrvIp:ntp服务器ip地址
  • pfunTime:函数指针,与c库函数time()功能及原型相同,返回自1970年1月1日0时0分0秒以来经过的秒数,可以为空
  • pfunSetSysTime:函数指针,通过unix时间戳设置系统当前时间,由sntp_update_by_xx()内部调用,收到正确的响应报文后调用该函数设置系统时间
  • bTimeZone:时区,例如东8区,其值为8;西8区其值为-8
  • penErr:如果校时失败,该参数用于接收具体的错误码

返回值

校时成功,返回TRUE;失败,返回FALSE,具体错误信息参看pennErr保存的错误码。

sntp_update_by_dns()函数与sntp_update_by_ip()函数除了第一个入口参数变成了域名外,其它完全相同,不再赘述。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK