Skip to content

Commit

Permalink
Adding labellen indexer which counts the number of labels in a DNS me…
Browse files Browse the repository at this point in the history
…ssage
  • Loading branch information
kdrenard authored and jelu committed Jan 21, 2022
1 parent 1acdcbc commit 6932247
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dsc_SOURCES = asn_index.c certain_qnames_index.c client_index.c \
idn_qname_index.c inX_addr.c ip_direction_index.c ip_proto_index.c \
ip_version_index.c md_array.c md_array_json_printer.c \
md_array_xml_printer.c msglen_index.c null_index.c opcode_index.c \
parse_conf.c pcap.c qclass_index.c qname_index.c qnamelen_index.c \
parse_conf.c pcap.c qclass_index.c qname_index.c qnamelen_index.c labellen_index.c \
qr_aa_bits_index.c qtype_index.c query_classification_index.c rcode_index.c \
rd_bit_index.c server_ip_addr_index.c tc_bit_index.c tld_index.c \
transport_index.c xmalloc.c response_time_index.c tld_list.c \
Expand All @@ -37,7 +37,7 @@ dist_dsc_SOURCES = asn_index.h base64.h certain_qnames_index.h client_index.h \
do_bit_index.h edns_bufsiz_index.h edns_version_index.h geoip.h hashtbl.h \
idn_qname_index.h inX_addr.h ip_direction_index.h ip_proto_index.h \
ip_version_index.h md_array.h msglen_index.h null_index.h opcode_index.h \
parse_conf.h pcap.h qclass_index.h qname_index.h qnamelen_index.h \
parse_conf.h pcap.h qclass_index.h qname_index.h qnamelen_index.h labellen_index.h \
qr_aa_bits_index.h qtype_index.h query_classification_index.h rcode_index.h \
rd_bit_index.h server_ip_addr_index.h syslog_debug.h tc_bit_index.h \
tld_index.h transport_index.h xmalloc.h response_time_index.h tld_list.h \
Expand Down
2 changes: 2 additions & 0 deletions src/dns_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "client_subnet_index.h"
#include "server_ip_addr_index.h"
#include "qnamelen_index.h"
#include "labellen_index.h"
#include "qname_index.h"
#include "msglen_index.h"
#include "certain_qnames_index.h"
Expand Down Expand Up @@ -91,6 +92,7 @@ static indexer indexers[] = {
{ "null", 0, null_indexer, null_iterator },
{ "qclass", 0, qclass_indexer, qclass_iterator, qclass_reset },
{ "qnamelen", 0, qnamelen_indexer, qnamelen_iterator, qnamelen_reset },
{ "labellen", 0, labellen_indexer, labellen_iterator, labellen_reset },
{ "qname", 0, qname_indexer, qname_iterator, qname_reset },
{ "second_ld", 0, second_ld_indexer, second_ld_iterator, second_ld_reset },
{ "third_ld", 0, third_ld_indexer, third_ld_iterator, third_ld_reset },
Expand Down
6 changes: 6 additions & 0 deletions src/dsc.conf.5.in
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,12 @@ section.
Note this is the \*(lqexpanded\*(rq length if the message happens to take
advantage of DNS message \*(lqcompression\*(rq.
.TP
\fBlabellen\fR
The count of labels the first (and usually only) QNAME in a DNS message question
section.
Note this count is based on the full QNAME if the message happens to take
advantage of DNS message \*(lqcompression\*(rq.
.TP
\fBqtype\fR
The query type (QTYPE) for the first QNAME in the DNS message question
section.
Expand Down
87 changes: 87 additions & 0 deletions src/labellen_index.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2008-2022, OARC, Inc.
* Copyright (c) 2007-2008, Internet Systems Consortium, Inc.
* Copyright (c) 2003-2007, The Measurement Factory, 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 "config.h"

#include "labellen_index.h"

#include <string.h>

static int largest = 0;

#define MAX_LABELS 64

int labellen_indexer(const dns_message* m)
{
if (m->malformed)
return -1;

int i, count = 1;
int len = strlen(m->qname);
if (len == 0 || (len == 1 && m->qname[0] == '.')) {
count = 0;
} else {
for (i = 0; i < len; i++)
if (m->qname[i] == '.')
count++;
}
if (count >= MAX_LABELS)
count = MAX_LABELS - 1;
if (count > largest)
largest = count;
return count;
}

static int next_iter;

int labellen_iterator(const char** label)
{
static char label_buf[10];
if (NULL == label) {
next_iter = 0;
return largest + 1;
}
if (next_iter > largest)
return -1;
snprintf(label_buf, sizeof(label_buf), "%d", next_iter);
*label = label_buf;
return next_iter++;
}

void labellen_reset()
{
largest = 0;
}
46 changes: 46 additions & 0 deletions src/labellen_index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2008-2022, OARC, Inc.
* Copyright (c) 2007-2008, Internet Systems Consortium, Inc.
* Copyright (c) 2003-2007, The Measurement Factory, 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.
*/

#ifndef __dsc_labellen_index_h
#define __dsc_labellen_index_h

#include "dns_message.h"

int labellen_indexer(const dns_message*);
int labellen_iterator(const char** label);
void labellen_reset(void);

#endif /* __dsc_labellen_index_h */

0 comments on commit 6932247

Please sign in to comment.