summaryrefslogtreecommitdiffstats
path: root/src/contrib/dnstap/message.c
blob: a5f798e2b7df56cc28bc758d745469b839b5bb7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*  Copyright (C) 2017 Farsight Security, Inc. <software@farsightsecurity.com>

    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 <https://www.gnu.org/licenses/>.
 */

#include <netinet/in.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "libknot/errcode.h"

#include "contrib/dnstap/convert.h"
#include "contrib/dnstap/message.h"

static void set_address(const struct sockaddr *sockaddr,
                        ProtobufCBinaryData   *addr,
                        protobuf_c_boolean    *has_addr,
                        uint32_t              *port,
                        protobuf_c_boolean    *has_port)
{
	if (sockaddr == NULL) {
		*has_addr = 0;
		*has_port = 0;
		return;
	}

	*has_addr = 1;
	*has_port = 1;

	if (sockaddr->sa_family == AF_INET) {
		const struct sockaddr_in *sai;
		sai = (const struct sockaddr_in *)sockaddr;
		addr->len = sizeof(sai->sin_addr);
		addr->data = (uint8_t *)&sai->sin_addr.s_addr;
		*port = ntohs(sai->sin_port);
	} else if (sockaddr->sa_family == AF_INET6) {
		const struct sockaddr_in6 *sai6;
		sai6 = (const struct sockaddr_in6 *)sockaddr;
		addr->len = sizeof(sai6->sin6_addr);
		addr->data = (uint8_t *)&sai6->sin6_addr.s6_addr;
		*port = ntohs(sai6->sin6_port);
	}
}

static int get_family(const struct sockaddr *query_sa,
	              const struct sockaddr *response_sa)
{
	const struct sockaddr *source = query_sa ? query_sa : response_sa;
	if (source == NULL) {
		return 0;
	}

	return dt_family_encode(source->sa_family);
}

int dt_message_fill(Dnstap__Message             *m,
                    const Dnstap__Message__Type type,
                    const struct sockaddr       *query_sa,
                    const struct sockaddr       *response_sa,
                    const int                   protocol,
                    const void                  *wire,
                    const size_t                len_wire,
                    const struct timespec       *mtime)
{
	if (m == NULL) {
		return KNOT_EINVAL;
	}

	memset(m, 0, sizeof(*m));

	m->base.descriptor = &dnstap__message__descriptor;

	// Message.type
	m->type = type;

	// Message.socket_family
	m->socket_family = get_family(query_sa, response_sa);
	m->has_socket_family = m->socket_family != 0;

	// Message.socket_protocol
	m->socket_protocol = dt_protocol_encode(protocol);
	m->has_socket_protocol = m->socket_protocol != 0;

	// Message addresses
	set_address(query_sa, &m->query_address, &m->has_query_address,
	            &m->query_port, &m->has_query_port);
	set_address(response_sa, &m->response_address, &m->has_response_address,
	            &m->response_port, &m->has_response_port);

	if (dt_message_type_is_query(type)) {
		// Message.query_message
		m->query_message.len = len_wire;
		m->query_message.data = (uint8_t *)wire;
		m->has_query_message = 1;
		// Message.query_time_sec, Message.query_time_nsec
		if (mtime != NULL) {
			m->query_time_sec = mtime->tv_sec;
			m->query_time_nsec = mtime->tv_nsec;
			m->has_query_time_sec = 1;
			m->has_query_time_nsec = 1;
		}
	} else if (dt_message_type_is_response(type)) {
		// Message.response_message
		m->response_message.len = len_wire;
		m->response_message.data = (uint8_t *)wire;
		m->has_response_message = 1;
		// Message.response_time_sec, Message.response_time_nsec
		if (mtime != NULL) {
			m->response_time_sec = mtime->tv_sec;
			m->response_time_nsec = mtime->tv_nsec;
			m->has_response_time_sec = 1;
			m->has_response_time_nsec = 1;
		}
	}

	return KNOT_EOK;
}