dns1.c

来自「eCos操作系统源码」· C语言 代码 · 共 934 行 · 第 1/3 页

C
934
字号
//==========================================================================////      tests/dns1.c////      Simple test of DNS client support////==========================================================================//####ECOSGPLCOPYRIGHTBEGIN####// -------------------------------------------// This file is part of eCos, the Embedded Configurable Operating System.// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.// Copyright (C) 2003 Andrew Lunn//// eCos is free software; you can redistribute it and/or modify it under// the terms of the GNU General Public License as published by the Free// Software Foundation; either version 2 or (at your option) any later version.//// eCos is distributed in the hope that it will be useful, but WITHOUT ANY// WARRANTY; without even the implied warranty of MERCHANTABILITY or// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License// for more details.//// You should have received a copy of the GNU General Public License along// with eCos; if not, write to the Free Software Foundation, Inc.,// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.//// As a special exception, if other files instantiate templates or use macros// or inline functions from this file, or you compile this file and link it// with other works to produce a work based on this file, this file does not// by itself cause the resulting work to be covered by the GNU General Public// License. However the source code for this file must still be made available// in accordance with section (3) of the GNU General Public License.//// This exception does not invalidate any other reasons why a work based on// this file might be covered by the GNU General Public License.//// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.// at http://sources.redhat.com/ecos/ecos-license/// -------------------------------------------//####ECOSGPLCOPYRIGHTEND####//==========================================================================//#####DESCRIPTIONBEGIN####//// Author(s):    andrew.lunn// Contributors: andrew.lunn, jskov// Date:         2001-09-18// Purpose:      // Description:  Test DNS functions. Note that the structure below that//               control what addresses the test uses. These must be//               changed to match the particular testing network in which//               the test is to be run.//              //####DESCRIPTIONEND####////==========================================================================#include <pkgconf/ns_dns.h>#include <network.h>#include <netdb.h>#include <arpa/inet.h>#include <cyg/infra/testcase.h>#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)static char stack[STACK_SIZE];static cyg_thread thread_data;static cyg_handle_t thread_handle;#define NELEM(x) (sizeof(x) / sizeof(x[0]))struct test_info_s {    char * dns_server_v4;    char * dns_server_v6;    char * domain_name;    char * hostname_v4;    char * cname_v4;    char * ip_addr_v4;    char * hostname_v6;    char * cname_v6;    char * ip_addr_v6;    char * hostname_v46;    char * cname_v46;    char * ip_addr_v46_v4;    char * ip_addr_v46_v6;};struct test_info_s test_info[] = {#if CYGPKG_NS_DNS_TESTS_ELSIS    {        "194.249.198.85",        NULL,        "test.ecos.",        "hostnamev4",        "cnamev4",        "192.168.88.1",        "hostnamev6",        "cnamev6",        "fec0::88:4:3:2:1",        "hostnamev46",        "cnamev46",        "192.168.88.2",        "fec0::88:4:3:2:2"    },#endif#ifdef CYGPKG_NS_DNS_TESTS_LUNN    {        "80.238.139.98",        "3ffe:bc0:8000::839",        "test.ecos.",        "hostnamev4",        "cnamev4",        "192.168.88.1",        "hostnamev6",        "cnamev6",        "fec0::88:4:3:2:1",        "hostnamev46",        "cnamev46",        "192.168.88.2",        "fec0::88:4:3:2:2"    }#endif};char * familytoa(int family) {        switch (family) {    case AF_INET:        return "AF_INET";#ifdef CYGPKG_NET_INET6    case AF_INET6:        return "AF_INET6";#endif    default:        return "Unknown";    }}void dns_test(struct test_info_s *info) {    struct in_addr addr;    struct hostent *hent;    char dn[256];    char name[256];    char cname[256];    char buffer[256];    char buff[64];    size_t hostlen = 128;    char host[hostlen];    struct addrinfo * res;    struct addrinfo hints;    struct sockaddr_in sa4;#ifdef CYGPKG_NET_INET6    struct sockaddr_in6 sa6;    struct addrinfo *ai4, *ai6;#endif    int error;        if (inet_pton(AF_INET, info->dns_server_v4, (void *)&addr) < 0) {      CYG_TEST_FAIL_FINISH("Error with DNS server address");    }    cyg_dns_res_init(&addr);    setdomainname(NULL,0);        inet_aton(info->ip_addr_v4, &addr);    strcpy(name,info->hostname_v4);    strcat(name,".");    strcat(name,info->domain_name);    strcpy(cname,info->cname_v4);    strcat(cname,".");    strcat(cname,info->domain_name);        // Lookup the IPv4 FQDN hostname     hent = gethostbyname(name);    if (hent != NULL) {        diag_sprintf(buffer,"Lookup %s: Result <%s is %s>",                      name,                     hent->h_name,                      inet_ntoa(*(struct in_addr *)hent->h_addr));        CYG_TEST_INFO(buffer);        CYG_TEST_PASS_FAIL((0 == memcmp((void*)&addr,                                         (void*)(hent->h_addr),                                         sizeof(struct in_addr))),                           "IPv4 FQDN hostname address");        CYG_TEST_PASS_FAIL((0 == strcmp(name, hent->h_name)),                            "IPv4 FQDN hostname name");    } else {        diag_sprintf(buffer,"IPv4 FQDN hostname: error %s",                      hstrerror(h_errno));        CYG_TEST_FAIL(buffer);    }        // Lookup the IPv4 FQDN cname    hent = gethostbyname(cname);    if (hent != NULL) {        diag_sprintf(buffer,"Lookup %s: Result <%s is %s>",                      cname,                     hent->h_name,                      inet_ntoa(*(struct in_addr *)hent->h_addr));        CYG_TEST_INFO(buffer);        CYG_TEST_PASS_FAIL((0 == memcmp((void*)&addr,                                         (void*)(hent->h_addr),                                         sizeof(struct in_addr))),                           "IPv4 FQDN cname address");        CYG_TEST_PASS_FAIL((0 == strcmp(name, hent->h_name)),                            "IPv4 FQDN hostname name");    } else {        diag_sprintf(buffer,"IPv4 FQDN cname: error %s", hstrerror(h_errno));        CYG_TEST_FAIL(buffer);    }        // Lookup the IP address as a string.  This does not require a DNS    // lookup. Just turn the value into binary    hent = gethostbyname(info->ip_addr_v4);    if (hent != NULL) {        diag_sprintf(buffer,"Lookup %s: Result <%s is %s>",                      info->ip_addr_v4,                     hent->h_name,                      inet_ntoa(*(struct in_addr *)hent->h_addr));        CYG_TEST_INFO(buffer);        CYG_TEST_PASS_FAIL((0 == memcmp((void*)&addr,                                         (void*)(hent->h_addr),                                         sizeof(struct in_addr))),                           "IPv4 IP address");        CYG_TEST_PASS_FAIL((0 == strcmp(info->ip_addr_v4, hent->h_name)),                            "IPv4 IP address name");            } else {        diag_sprintf(buffer,"IPv4 IP address: error %s", hstrerror(h_errno));        CYG_TEST_FAIL(buffer);    }        // Reverse lookup the IPv4 address, expect the FQDN hostname    hent = gethostbyaddr((char *)&addr, sizeof(struct in_addr), AF_INET);    if (hent != NULL) {        inet_ntop(AF_INET,(void *)&addr, buff, sizeof(buff));        diag_sprintf(buffer,"Reverse lookup %s: Result <%s is %s>",                      buff,                     hent->h_name,                      inet_ntoa(*(struct in_addr *)hent->h_addr));        CYG_TEST_INFO(buffer);        CYG_TEST_PASS_FAIL((0 == memcmp((void*)&addr,                                         (void*)(hent->h_addr),                                         sizeof(struct in_addr))),                           "Reverse lookup IPv4 IP address");                CYG_TEST_PASS_FAIL((0 == strcmp(name, hent->h_name)),                            "Reverse lookup IPv4 IP address name");    } else {        diag_sprintf(buffer,"Reverse lookup IPv4 IP address: error %s",                      hstrerror(h_errno));        CYG_TEST_FAIL(buffer);    }        // Setup a domainname. We now don't have to use fully qualified    // domain names    setdomainname(info->domain_name, strlen(info->domain_name));    getdomainname(dn, sizeof(dn));    CYG_TEST_PASS_FAIL(0 == strcmp(info->domain_name, dn),                        "{get|set}domainname");        // Make sure FQDN still work    strcpy(name,info->hostname_v4);    strcat(name,".");    strcat(name,info->domain_name);        hent = gethostbyname(name);    if (hent != NULL) {        diag_sprintf(buffer,"Lookup %s: Result <%s is %s>",                      name,                     hent->h_name,                      inet_ntoa(*(struct in_addr *)hent->h_addr));        CYG_TEST_INFO(buffer);        CYG_TEST_PASS_FAIL((0 == memcmp((void*)&addr,                                         (void*)(hent->h_addr),                                         sizeof(struct in_addr))),                           "IPv4 FQDN hostname address");        CYG_TEST_PASS_FAIL((0 == strcmp(name, hent->h_name)),                            "IPv4 FQDN hostname name");    } else {        diag_sprintf(buffer,"IPv4 FQDN hostname: error %s",                      hstrerror(h_errno));        CYG_TEST_FAIL(buffer);    }        // Now just the hostname    hent = gethostbyname(info->hostname_v4);    if (hent != NULL) {        diag_sprintf(buffer,"Lookup %s: Result <%s is %s>",                      info->hostname_v4,                     hent->h_name,                      inet_ntoa(*(struct in_addr *)hent->h_addr));        CYG_TEST_INFO(buffer);        CYG_TEST_PASS_FAIL((0 == memcmp((void*)&addr,                                         (void*)(hent->h_addr),                                         sizeof(struct in_addr))),                           "IPv4 hostname address");        CYG_TEST_PASS_FAIL((0 == strcmp(name, hent->h_name)),                            "IPv4 hostname name");    } else {        diag_sprintf(buffer,"IPv4 hostname: error %s", hstrerror(h_errno));        CYG_TEST_FAIL(buffer);    }        // Run the same tests as above, but this time use getaddrinfo and    // getnameinfo.    setdomainname(NULL,0);        memset(&hints,0,sizeof(hints));    hints.ai_flags = AI_CANONNAME;    memset(&sa4,0,sizeof(sa4));    memcpy(&sa4.sin_addr, &addr, sizeof(addr));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?