onps · 2022年11月12日 0

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

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测试会一直占用协议栈资源。

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()等。

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服务器域名进行校时。