summaryrefslogtreecommitdiffstats
path: root/src/dump_cds.h
blob: 95b987ecdb2364c4868f88ad8235c6c4d2199535 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (c) 2016-2022, OARC, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "dnscap_common.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>

#ifndef __dnscap_dump_cds_h
#define __dnscap_dump_cds_h

#define DUMP_CDS_OK 0
#define DUMP_CDS_EINVAL 1
#define DUMP_CDS_ENOMEM 2
#define DUMP_CDS_ECBOR 3
#define DUMP_CDS_ELDNS 4
#define DUMP_CDS_EWRITE 5
#define DUMP_CDS_FLUSH 6
#define DUMP_CDS_ENOSUP 7
#define DUMP_CDS_EBUF 8

#define CDS_OPTION_RLABELS 0
#define CDS_OPTION_RLABEL_MIN_SIZE 1
#define CDS_OPTION_RDATA_RINDEX_SIZE 2
#define CDS_OPTION_RDATA_RINDEX_MIN_SIZE 3
#define CDS_OPTION_USE_RDATA_INDEX 4
#define CDS_OPTION_RDATA_INDEX_MIN_SIZE 5

#define CDS_DEFAULT_MAX_RLABELS 255
#define CDS_DEFAULT_MIN_RLABEL_SIZE 3
#define CDS_DEFAULT_RDATA_INDEX_MIN_SIZE 5
#define CDS_DEFAULT_RDATA_RINDEX_SIZE 255
#define CDS_DEFAULT_RDATA_RINDEX_MIN_SIZE 5

typedef struct ip_header ip_header_t;
struct ip_header {
    unsigned short is_v6 : 1;
    unsigned short is_reverse : 1;
    unsigned short have_src_addr : 1;
    unsigned short have_src_port : 1;
    unsigned short have_dest_addr : 1;
    unsigned short have_dest_port : 1;

    uint8_t         bits;
    struct in_addr  src_addr4;
    uint16_t        src_port4;
    struct in6_addr src_addr6;
    uint16_t        src_port6;
    struct in_addr  dest_addr4;
    uint16_t        dest_port4;
    struct in6_addr dest_addr6;
    uint16_t        dest_port6;
};

typedef struct dns_label dns_label_t;
struct dns_label {
    unsigned short is_complete : 1;
    unsigned short have_size : 1;
    unsigned short have_extension_bits : 1;
    unsigned short have_offset : 1;
    unsigned short have_label : 1;
    unsigned short have_n_offset : 1;

    uint8_t  size;
    uint8_t  extension_bits;
    uint16_t offset;
    uint8_t* offset_p;
    uint8_t* label;
    size_t   n_offset;
};

#define CDS_RLABEL_LABEL_T_LABEL 64

typedef struct dns_rlabel_label dns_rlabel_label_t;
struct dns_rlabel_label {
    unsigned short have_n_offset : 1;

    uint8_t size;
    uint8_t label[CDS_RLABEL_LABEL_T_LABEL];
    size_t  n_offset;
};

#define CDS_RLABEL_T_LABELS 256

typedef struct dns_rlabel dns_rlabel_t;
struct dns_rlabel {
    dns_rlabel_t* next;
    dns_rlabel_t* prev;

    uint8_t            labels;
    dns_rlabel_label_t label[CDS_RLABEL_T_LABELS];
};

typedef struct dns_rdata dns_rdata_t;
struct dns_rdata {
    unsigned short is_complete : 1;
    unsigned short have_labels : 1;
    unsigned short have_rlabel_idx : 1;
    unsigned short have_rdata : 1;

    size_t       rdata_len;
    uint8_t*     rdata;
    size_t       labels;
    dns_label_t* label;
    size_t       rlabel_idx;
};

typedef struct dns_rr dns_rr_t;
struct dns_rr {
    unsigned short is_complete : 1;
    unsigned short have_labels : 1;
    unsigned short have_rlabel_idx : 1;
    unsigned short have_bits : 1;
    unsigned short have_type : 1;
    unsigned short have_class : 1;
    unsigned short have_ttl : 1;
    unsigned short have_rdlength : 1;
    unsigned short have_rdata : 1;
    unsigned short have_mixed_rdata : 1;
    unsigned short have_rdata_index : 1;
    unsigned short have_rdata_rindex : 1;

    size_t       labels;
    dns_label_t* label;
    size_t       rlabel_idx;
    uint8_t      bits;
    uint16_t     type;
    uint16_t class;
    uint32_t     ttl;
    uint16_t     rdlength;
    uint8_t*     rdata;
    size_t       mixed_rdatas;
    dns_rdata_t* mixed_rdata;
    size_t       rdata_index;
    size_t       rdata_rindex;
};

typedef struct dns dns_t;
struct dns {
    unsigned short header_is_complete : 1;
    unsigned short have_id : 1;
    unsigned short have_raw : 1;
    unsigned short have_cnt_bits : 1;
    unsigned short have_qdcount : 1;
    unsigned short have_ancount : 1;
    unsigned short have_nscount : 1;
    unsigned short have_arcount : 1;
    unsigned short have_rr_bits : 1;
    unsigned short have_questions : 1;
    unsigned short have_answers : 1;
    unsigned short have_authorities : 1;
    unsigned short have_additionals : 1;

    int       id;
    uint16_t  raw;
    uint8_t   cnt_bits;
    uint16_t  qdcount;
    uint16_t  ancount;
    uint16_t  nscount;
    uint16_t  arcount;
    uint8_t   rr_bits;
    size_t    questions;
    dns_rr_t* question;
    size_t    answers;
    dns_rr_t* answer;
    size_t    authorities;
    dns_rr_t* authority;
    size_t    additionals;
    dns_rr_t* additional;
};

int cds_set_cbor_size(size_t size);
int cds_set_message_size(size_t size);
int cds_set_max_rlabels(size_t size);
int cds_set_min_rlabel_size(size_t size);
int cds_set_use_rdata_index(int use);
int cds_set_use_rdata_rindex(int use);
int cds_set_rdata_index_min_size(size_t size);
int cds_set_rdata_rindex_min_size(size_t size);
int cds_set_rdata_rindex_size(size_t size);
int output_cds(iaddr from, iaddr to, uint8_t proto, unsigned flags, unsigned sport, unsigned dport, my_bpftimeval ts, const u_char* pkt_copy, size_t olen, const u_char* payload, size_t payloadlen);
int dump_cds(FILE* fp);
int have_cds_support();

#endif /* __dnscap_dump_cds_h */