"Fossies" - the Fresh Open Source Software Archive

Member "knot-2.8.3/src/contrib/dnstap/convert.c" (16 Jul 2019, 3535 Bytes) of package /linux/misc/dns/knot-2.8.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "convert.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.7.6_vs_2.8.0.

    1 /*  Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
    2 
    3     This program is free software: you can redistribute it and/or modify
    4     it under the terms of the GNU General Public License as published by
    5     the Free Software Foundation, either version 3 of the License, or
    6     (at your option) any later version.
    7 
    8     This program is distributed in the hope that it will be useful,
    9     but WITHOUT ANY WARRANTY; without even the implied warranty of
   10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11     GNU General Public License for more details.
   12 
   13     You should have received a copy of the GNU General Public License
   14     along with this program.  If not, see <https://www.gnu.org/licenses/>.
   15 */
   16 
   17 #include <sys/socket.h>
   18 #include <sys/types.h>
   19 #include <netinet/in.h>
   20 #include <netinet/ip.h>
   21 
   22 #include "contrib/dnstap/convert.h"
   23 #include "contrib/dnstap/dnstap.pb-c.h"
   24 
   25 /*!
   26  * \brief Translation between real and Dnstap value.
   27  */
   28 typedef struct mapping {
   29     int real;
   30     int dnstap;
   31 } mapping_t;
   32 
   33 /*!
   34  * \brief Mapping for network family.
   35  */
   36 static const mapping_t SOCKET_FAMILY_MAPPING[] = {
   37     { AF_INET,  DNSTAP__SOCKET_FAMILY__INET },
   38     { AF_INET6, DNSTAP__SOCKET_FAMILY__INET6 },
   39     { 0 }
   40 };
   41 
   42 /*!
   43  * \brief Mapping from network protocol.
   44  */
   45 static const mapping_t SOCKET_PROTOCOL_MAPPING[] = {
   46     { IPPROTO_UDP, DNSTAP__SOCKET_PROTOCOL__UDP },
   47     { IPPROTO_TCP, DNSTAP__SOCKET_PROTOCOL__TCP },
   48     { 0 }
   49 };
   50 
   51 /*!
   52  * \brief Get Dnstap value for a given real value.
   53  */
   54 static int encode(const mapping_t *mapping, int real)
   55 {
   56     for (const mapping_t *m = mapping; m->real != 0; m += 1) {
   57         if (m->real == real) {
   58             return m->dnstap;
   59         }
   60     }
   61 
   62     return 0;
   63 }
   64 
   65 /*!
   66  * \brief Get real value for a given Dnstap value.
   67  */
   68 static int decode(const mapping_t *mapping, int dnstap)
   69 {
   70     for (const mapping_t *m = mapping; m->real != 0; m += 1) {
   71         if (m->dnstap == dnstap) {
   72             return m->real;
   73         }
   74     }
   75 
   76     return 0;
   77 }
   78 
   79 /* -- public API ----------------------------------------------------------- */
   80 
   81 Dnstap__SocketFamily dt_family_encode(int family)
   82 {
   83     return encode(SOCKET_FAMILY_MAPPING, family);
   84 }
   85 
   86 int dt_family_decode(Dnstap__SocketFamily dnstap_family)
   87 {
   88     return decode(SOCKET_FAMILY_MAPPING, dnstap_family);
   89 }
   90 
   91 Dnstap__SocketProtocol dt_protocol_encode(int protocol)
   92 {
   93     return encode(SOCKET_PROTOCOL_MAPPING, protocol);
   94 }
   95 
   96 int dt_protocol_decode(Dnstap__SocketProtocol dnstap_protocol)
   97 {
   98     return decode(SOCKET_PROTOCOL_MAPPING, dnstap_protocol);
   99 }
  100 
  101 bool dt_message_type_is_query(Dnstap__Message__Type type)
  102 {
  103     switch (type) {
  104     case DNSTAP__MESSAGE__TYPE__AUTH_QUERY:
  105     case DNSTAP__MESSAGE__TYPE__CLIENT_QUERY:
  106     case DNSTAP__MESSAGE__TYPE__FORWARDER_QUERY:
  107     case DNSTAP__MESSAGE__TYPE__RESOLVER_QUERY:
  108     case DNSTAP__MESSAGE__TYPE__STUB_QUERY:
  109     case DNSTAP__MESSAGE__TYPE__TOOL_QUERY:
  110         return true;
  111     default:
  112         return false;
  113     }
  114 }
  115 
  116 bool dt_message_type_is_response(Dnstap__Message__Type type)
  117 {
  118     switch (type) {
  119     case DNSTAP__MESSAGE__TYPE__AUTH_RESPONSE:
  120     case DNSTAP__MESSAGE__TYPE__CLIENT_RESPONSE:
  121     case DNSTAP__MESSAGE__TYPE__FORWARDER_RESPONSE:
  122     case DNSTAP__MESSAGE__TYPE__RESOLVER_RESPONSE:
  123     case DNSTAP__MESSAGE__TYPE__STUB_RESPONSE:
  124     case DNSTAP__MESSAGE__TYPE__TOOL_RESPONSE:
  125         return true;
  126     default:
  127         return false;
  128     }
  129 }
  130 
  131 bool dt_message_role_is_initiator(Dnstap__Message__Type type)
  132 {
  133     switch (type) {
  134     case DNSTAP__MESSAGE__TYPE__AUTH_QUERY:
  135     case DNSTAP__MESSAGE__TYPE__AUTH_RESPONSE:
  136     case DNSTAP__MESSAGE__TYPE__CLIENT_QUERY:
  137     case DNSTAP__MESSAGE__TYPE__CLIENT_RESPONSE:
  138         return false;
  139     default:
  140         return true;
  141     }
  142 }