Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dns_debug.c Source File

dns_debug.c

Go to the documentation of this file.
00001 /**
00002  * @file dns_debug.c
00003  * @brief Data logging functions for debugging purpose (DNS)
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 //Switch to the appropriate trace level
00030 #define TRACE_LEVEL DNS_TRACE_LEVEL
00031 
00032 //Dependencies
00033 #include "core/net.h"
00034 #include "dns/dns_debug.h"
00035 #include "netbios/nbns_client.h"
00036 #include "netbios/nbns_responder.h"
00037 #include "netbios/nbns_common.h"
00038 #include "debug.h"
00039 
00040 //Check TCP/IP stack configuration
00041 #if (DNS_TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
00042 
00043 
00044 /**
00045  * @brief Dump DNS message for debugging purpose
00046  * @param[in] message Pointer to the DNS message
00047  * @param[in] length Length of the DNS message
00048  **/
00049 
00050 void dnsDumpMessage(const DnsHeader *message, size_t length)
00051 {
00052    uint_t i;
00053    size_t pos;
00054    char_t *buffer;
00055 
00056    //Make sure the DNS message is valid
00057    if(length >= sizeof(DnsHeader))
00058    {
00059       //Dump DNS message header
00060       TRACE_DEBUG("  Identifier (ID) = %" PRIu16 "\r\n", ntohs(message->id));
00061       TRACE_DEBUG("  Query Response (QR) = %" PRIu8 "\r\n", message->qr);
00062       TRACE_DEBUG("  Opcode (OPCODE) = %" PRIu8 "\r\n", message->opcode);
00063       TRACE_DEBUG("  Authoritative Answer (AA) = %" PRIu8 "\r\n", message->aa);
00064       TRACE_DEBUG("  TrunCation (TC) = %" PRIu8 "\r\n", message->tc);
00065       TRACE_DEBUG("  Recursion Desired (RD) = %" PRIu8 "\r\n", message->rd);
00066       TRACE_DEBUG("  Recursion Available (RA) = %" PRIu8 "\r\n", message->ra);
00067       TRACE_DEBUG("  Reserved (Z) = %" PRIu8 "\r\n", message->z);
00068       TRACE_DEBUG("  Response Code (RCODE) = %" PRIu8 "\r\n", message->rcode);
00069       TRACE_DEBUG("  Question Count (QDCOUNT) = %" PRIu8 "\r\n", ntohs(message->qdcount));
00070       TRACE_DEBUG("  Answer Count (ANCOUNT) = %" PRIu8 "\r\n", ntohs(message->ancount));
00071       TRACE_DEBUG("  Name Server Count (NSCOUNT) = %" PRIu8 "\r\n", ntohs(message->nscount));
00072       TRACE_DEBUG("  Additional Record Count (ARCOUNT) = %" PRIu8 "\r\n", ntohs(message->arcount));
00073 
00074       //Allocate a memory buffer to holds domain names
00075       buffer = memPoolAlloc(DNS_NAME_MAX_SIZE);
00076       //Failed to allocate memory
00077       if(buffer == NULL)
00078          return;
00079 
00080       //Point to the first question
00081       pos = sizeof(DnsHeader);
00082 
00083       //Start of exception handling block
00084       do
00085       {
00086          //Debug message
00087          TRACE_DEBUG("  Questions\r\n");
00088 
00089          //Parse questions
00090          for(i = 0; i < ntohs(message->qdcount); i++)
00091          {
00092             //Dump current question
00093             pos = dnsDumpQuestion(message, length, pos, buffer);
00094             //Any error to report?
00095             if(!pos)
00096                break;
00097          }
00098 
00099          //Parsing error?
00100          if(!pos)
00101             break;
00102 
00103          //Debug message
00104          TRACE_DEBUG("  Answer RRs\r\n");
00105 
00106          //Parse answer resource records
00107          for(i = 0; i < ntohs(message->ancount); i++)
00108          {
00109             //Dump current resource record
00110             pos = dnsDumpResourceRecord(message, length, pos, buffer);
00111             //Any error to report?
00112             if(!pos)
00113                break;
00114          }
00115 
00116          //Parsing error?
00117          if(!pos)
00118             break;
00119 
00120          //Debug message
00121          TRACE_DEBUG("  Authority RRs\r\n");
00122 
00123          //Parse authority resource records
00124          for(i = 0; i < ntohs(message->nscount); i++)
00125          {
00126             //Dump current resource record
00127             pos = dnsDumpResourceRecord(message, length, pos, buffer);
00128             //Any error to report?
00129             if(!pos)
00130                break;
00131          }
00132 
00133          //Parsing error?
00134          if(!pos)
00135             break;
00136 
00137          //Debug message
00138          TRACE_DEBUG("  Additional RRs\r\n");
00139 
00140          //Parse additional resource records
00141          for(i = 0; i < ntohs(message->arcount); i++)
00142          {
00143             //Dump current resource record
00144             pos = dnsDumpResourceRecord(message, length, pos, buffer);
00145             //Any error to report?
00146             if(!pos)
00147                break;
00148          }
00149 
00150          //End of exception handling block
00151       } while(0);
00152 
00153       //Free previously allocated memory
00154       memPoolFree(buffer);
00155    }
00156 }
00157 
00158 
00159 /**
00160  * @brief Dump DNS question for debugging purpose
00161  * @param[in] message Pointer to the DNS message
00162  * @param[in] length Length of the DNS message
00163  * @param[in] pos Offset of the question to decode
00164  * @param[in] buffer Memory buffer to holds domain names
00165  * @return Offset to the next question
00166  **/
00167 
00168 size_t dnsDumpQuestion(const DnsHeader *message, size_t length, size_t pos, char_t *buffer)
00169 {
00170    size_t n;
00171    DnsQuestion *question;
00172 
00173    //Parse domain name
00174    n = dnsParseName(message, length, pos, buffer, 0);
00175    //Invalid name?
00176    if(!n)
00177       return 0;
00178 
00179    //Make sure the DNS question is valid
00180    if((n + sizeof(DnsQuestion)) > length)
00181       return 0;
00182 
00183    //Point to the corresponding entry
00184    question = DNS_GET_QUESTION(message, n);
00185 
00186    //NB question found?
00187    if(ntohs(question->qtype) == DNS_RR_TYPE_NB)
00188    {
00189 #if (NBNS_CLIENT_SUPPORT == ENABLED || NBNS_RESPONDER_SUPPORT == ENABLED)
00190 #if (IPV4_SUPPORT == ENABLED)
00191       //Decode NetBIOS name
00192       pos = nbnsParseName((NbnsHeader *) message, length, pos, buffer);
00193       //Invalid NetBIOS name?
00194       if(!pos)
00195          return 0;
00196 #endif
00197 #endif
00198    }
00199 
00200    //Dump DNS question
00201    TRACE_DEBUG("    Name (QNAME) = %s\r\n", buffer);
00202    TRACE_DEBUG("      Query Type (QTYPE) = %" PRIu16 "\r\n", ntohs(question->qtype));
00203    TRACE_DEBUG("      Query Class (QCLASS) = %" PRIu16 "\r\n", ntohs(question->qclass));
00204 
00205    //Point to the next question
00206    n += sizeof(DnsQuestion);
00207    //Return the current position
00208    return n;
00209 }
00210 
00211 
00212 /**
00213  * @brief Dump DNS resource record for debugging purpose
00214  * @param[in] message Pointer to the DNS message
00215  * @param[in] length Length of the DNS message
00216  * @param[in] pos Offset of the question to decode
00217  * @param[in] buffer Memory buffer to holds domain names
00218  * @return Offset to the next question
00219  **/
00220 
00221 size_t dnsDumpResourceRecord(const DnsHeader *message, size_t length, size_t pos, char_t *buffer)
00222 {
00223    size_t n;
00224    DnsResourceRecord *record;
00225    DnsSrvResourceRecord *srvRecord;
00226 
00227    //Parse domain name
00228    n = dnsParseName(message, length, pos, buffer, 0);
00229    //Invalid name?
00230    if(!n)
00231       return 0;
00232 
00233    //Point to the corresponding entry
00234    record = DNS_GET_RESOURCE_RECORD(message, n);
00235 
00236    //Make sure the resource record is valid
00237    if((n + sizeof(DnsResourceRecord)) > length)
00238       return 0;
00239    if((n + sizeof(DnsResourceRecord) + ntohs(record->rdlength)) > length)
00240       return 0;
00241 
00242    //NB resource record found?
00243    if(ntohs(record->rtype) == DNS_RR_TYPE_NB)
00244    {
00245 #if (NBNS_CLIENT_SUPPORT == ENABLED || NBNS_RESPONDER_SUPPORT == ENABLED)
00246 #if (IPV4_SUPPORT == ENABLED)
00247       //Decode NetBIOS name
00248       pos = nbnsParseName((NbnsHeader *) message, length, pos, buffer);
00249       //Invalid NetBIOS name?
00250       if(!pos)
00251          return 0;
00252 #endif
00253 #endif
00254    }
00255 
00256    //Dump DNS resource record
00257    TRACE_DEBUG("    Name (NAME) = %s\r\n", buffer);
00258    TRACE_DEBUG("      Query Type (TYPE) = %" PRIu16 "\r\n", ntohs(record->rtype));
00259    TRACE_DEBUG("      Query Class (CLASS) = %" PRIu16 "\r\n", ntohs(record->rclass));
00260    TRACE_DEBUG("      Time-To-Live (TTL) = %" PRIu32 "\r\n", ntohl(record->ttl));
00261    TRACE_DEBUG("      Data Length (RDLENGTH) = %" PRIu16 "\r\n", ntohs(record->rdlength));
00262 
00263    //Dump resource data
00264 #if (IPV4_SUPPORT == ENABLED)
00265    if(ntohs(record->rtype) == DNS_RR_TYPE_A &&
00266       ntohs(record->rdlength) == sizeof(Ipv4Addr))
00267    {
00268       Ipv4Addr ipAddr;
00269 
00270       //Copy IPv4 address
00271       ipv4CopyAddr(&ipAddr, record->rdata);
00272       //Dump IPv4 address
00273       TRACE_DEBUG("      Data (RDATA) = %s\r\n", ipv4AddrToString(ipAddr, NULL));
00274    }
00275    else
00276 #endif
00277 #if (IPV6_SUPPORT == ENABLED)
00278    if(ntohs(record->rtype) == DNS_RR_TYPE_AAAA &&
00279       ntohs(record->rdlength) == sizeof(Ipv6Addr))
00280    {
00281       Ipv6Addr ipAddr;
00282 
00283       //Copy IPv6 address
00284       ipv6CopyAddr(&ipAddr, record->rdata);
00285       //Dump IPv6 address
00286       TRACE_DEBUG("      Data (RDATA) = %s\r\n", ipv6AddrToString(&ipAddr, NULL));
00287    }
00288    else
00289 #endif
00290    if(ntohs(record->rtype) == DNS_RR_TYPE_PTR)
00291    {
00292       //Decode domain name
00293       pos = dnsParseName(message, length, n + sizeof(DnsResourceRecord), buffer, 0);
00294       //Invalid domain name?
00295       if(!pos)
00296          return 0;
00297 
00298       //Dump name
00299       TRACE_DEBUG("      Domain Name (PTRDNAME) = %s\r\n", buffer);
00300    }
00301    else if(ntohs(record->rtype) == DNS_RR_TYPE_SRV)
00302    {
00303       //Cast resource record
00304       srvRecord = (DnsSrvResourceRecord *) record;
00305 
00306       //Dump SRV resource record
00307       TRACE_DEBUG("      Priority = %" PRIu16 "\r\n", ntohs(srvRecord->priority));
00308       TRACE_DEBUG("      Weight = %" PRIu16 "\r\n", ntohs(srvRecord->weight));
00309       TRACE_DEBUG("      Port = %" PRIu16 "\r\n", ntohs(srvRecord->port));
00310 
00311       //Decode target name
00312       pos = dnsParseName(message, length, n + sizeof(DnsSrvResourceRecord), buffer, 0);
00313       //Invalid domain name?
00314       if(!pos)
00315          return 0;
00316 
00317       //Dump name
00318       TRACE_DEBUG("      Target = %s\r\n", buffer);
00319    }
00320    else
00321    {
00322       //Dump resource data
00323       TRACE_DEBUG("      Data (RDATA)\r\n");
00324       TRACE_DEBUG_ARRAY("        ", record->rdata, ntohs(record->rdlength));
00325    }
00326 
00327    //Point to the next resource record
00328    n += sizeof(DnsResourceRecord) + ntohs(record->rdlength);
00329    //Return the current position
00330    return n;
00331 }
00332 
00333 #endif
00334