From 4893f100216caa5bb9c355c2eadad6c620c8d58b Mon Sep 17 00:00:00 2001 From: Jan Vcelak <jan.vcelak@nic.cz> Date: Sun, 18 May 2014 19:16:32 +0200 Subject: [PATCH] dnstap: conversion for Dnstap values and the real ones --- Knot.files | 2 + src/dnstap/Makefile.am | 8 ++- src/dnstap/convert.c | 127 +++++++++++++++++++++++++++++++++++++++++ src/dnstap/convert.h | 63 ++++++++++++++++++++ 4 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 src/dnstap/convert.c create mode 100644 src/dnstap/convert.h diff --git a/Knot.files b/Knot.files index bf9e7b8f8..d65a06f7a 100644 --- a/Knot.files +++ b/Knot.files @@ -74,6 +74,8 @@ src/common/sockaddr.c src/common/sockaddr.h src/common/strtonum.h src/dnstap/Makefile.am +src/dnstap/convert.c +src/dnstap/convert.h src/dnstap/dnstap.c src/dnstap/dnstap.h src/dnstap/message.c diff --git a/src/dnstap/Makefile.am b/src/dnstap/Makefile.am index f9062aa4c..6d9364428 100644 --- a/src/dnstap/Makefile.am +++ b/src/dnstap/Makefile.am @@ -27,10 +27,16 @@ libdnstap_la_LDFLAGS = \ $(DNSTAP_LIBS) libdnstap_la_SOURCES = \ + convert.c \ + convert.h \ dnstap.c \ + dnstap.h \ message.c \ + message.h \ reader.c \ - writer.c + reader.h \ + writer.c \ + writer.h nodist_libdnstap_la_SOURCES = \ dnstap.pb-c.c \ diff --git a/src/dnstap/convert.c b/src/dnstap/convert.c new file mode 100644 index 000000000..d842551ae --- /dev/null +++ b/src/dnstap/convert.c @@ -0,0 +1,127 @@ +/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> + + This program 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 3 of the License, or + (at your option) any later version. + + This program 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 this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <sys/socket.h> +#include <netinet/ip.h> + +#include "dnstap/convert.h" +#include "dnstap/dnstap.pb-c.h" + +/*! + * \brief Translation between real and Dnstap value. + */ +typedef struct mapping { + int real; + int dnstap; +} mapping_t; + +/*! + * \brief Mapping for network family. + */ +static const mapping_t SOCKET_FAMILY_MAPPING[] = { + { AF_INET, DNSTAP__SOCKET_FAMILY__INET }, + { AF_INET6, DNSTAP__SOCKET_FAMILY__INET6 }, + { 0 } +}; + +/*! + * \brief Mapping from network protocol. + */ +static const mapping_t SOCKET_PROTOCOL_MAPPING[] = { + { IPPROTO_UDP, DNSTAP__SOCKET_PROTOCOL__UDP }, + { IPPROTO_TCP, DNSTAP__SOCKET_PROTOCOL__TCP }, + { 0 } +}; + +/*! + * \brief Get Dnstap value for a given real value. + */ +static int encode(const mapping_t *mapping, int real) +{ + for (const mapping_t *m = mapping; m->real != 0; m += 1) { + if (m->real == real) { + return m->dnstap; + } + } + + return 0; +} + +/*! + * \brief Get real value for a given Dnstap value. + */ +static int decode(const mapping_t *mapping, int dnstap) +{ + for (const mapping_t *m = mapping; m->real != 0; m += 1) { + if (m->dnstap == dnstap) { + return m->real; + } + } + + return 0; +} + +/* -- public API ----------------------------------------------------------- */ + +Dnstap__SocketFamily dt_family_encode(int family) +{ + return encode(SOCKET_FAMILY_MAPPING, family); +} + +int dt_family_decode(Dnstap__SocketFamily dnstap_family) +{ + return decode(SOCKET_FAMILY_MAPPING, dnstap_family); +} + +Dnstap__SocketProtocol dt_protocol_encode(int protocol) +{ + return encode(SOCKET_PROTOCOL_MAPPING, protocol); +} + +int dt_protocol_decode(Dnstap__SocketProtocol dnstap_protocol) +{ + return decode(SOCKET_PROTOCOL_MAPPING, dnstap_protocol); +} + +bool dt_message_type_is_query(Dnstap__Message__Type type) +{ + switch (type) { + case DNSTAP__MESSAGE__TYPE__AUTH_QUERY: + case DNSTAP__MESSAGE__TYPE__CLIENT_QUERY: + case DNSTAP__MESSAGE__TYPE__FORWARDER_QUERY: + case DNSTAP__MESSAGE__TYPE__RESOLVER_QUERY: + case DNSTAP__MESSAGE__TYPE__STUB_QUERY: + case DNSTAP__MESSAGE__TYPE__TOOL_QUERY: + return true; + default: + return false; + } +} + +bool dt_message_type_is_response(Dnstap__Message__Type type) +{ + switch (type) { + case DNSTAP__MESSAGE__TYPE__AUTH_RESPONSE: + case DNSTAP__MESSAGE__TYPE__CLIENT_RESPONSE: + case DNSTAP__MESSAGE__TYPE__FORWARDER_RESPONSE: + case DNSTAP__MESSAGE__TYPE__RESOLVER_RESPONSE: + case DNSTAP__MESSAGE__TYPE__STUB_RESPONSE: + case DNSTAP__MESSAGE__TYPE__TOOL_RESPONSE: + return true; + default: + return false; + } +} diff --git a/src/dnstap/convert.h b/src/dnstap/convert.h new file mode 100644 index 000000000..43e959c03 --- /dev/null +++ b/src/dnstap/convert.h @@ -0,0 +1,63 @@ +/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> + + This program 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 3 of the License, or + (at your option) any later version. + + This program 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 this program. If not, see <http://www.gnu.org/licenses/>. +*/ +/*! + * \file convert.h + * + * \author Jan Vcelak <jan.vcelak@nic.cz> + * + * \brief Dnstap identifiers conversions. + * + * \addtogroup dnstap + * @{ + */ + +#pragma once + +#include <stdbool.h> + +#include "dnstap/dnstap.pb-c.h" + +/*! + * \brief Get Dnstap socket family from the real one. + */ +Dnstap__SocketFamily dt_family_encode(int family); + +/*! + * \brief Get real socket family from the Dnstap one. + */ +int dt_family_decode(Dnstap__SocketFamily dnstap_family); + +/*! + * \brief Get Dnstap protocol from a real one. + */ +Dnstap__SocketProtocol dt_protocol_encode(int protocol); + +/*! + * \brief Get real protocol from the Dnstap one. + */ +int dt_protocol_decode(Dnstap__SocketProtocol dnstap_protocol); + +/*! + * Check if a message type is any type of a query. + */ +bool dt_message_type_is_query(Dnstap__Message__Type type); + +/*! + * Check if a message type is a ny type of a response. + */ +bool dt_message_type_is_response(Dnstap__Message__Type type); + +/*! @} */ -- GitLab