Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Code formatting and moved external code to own directory
  • Loading branch information
jelu committed Aug 17, 2017
1 parent 4484f39 commit c528ccb
Show file tree
Hide file tree
Showing 89 changed files with 1,459 additions and 1,703 deletions.
6 changes: 6 additions & 0 deletions .clang-format
@@ -0,0 +1,6 @@
BasedOnStyle: webkit
IndentWidth: 4
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignOperands: true
SortIncludes: false
4 changes: 2 additions & 2 deletions debian/copyright
Expand Up @@ -17,11 +17,11 @@ Copyright: 2016 Duane Wessels
2016 The Measurement Factory, Inc.
License: MIT

Files: src/base64.c
Files: src/ext/base64.c
Copyright: 1995-2001 Kungliga Tekniska Högskolan (Royal Institute of Technology, Stockholm, Sweden)
License: BSD-3-Clause

Files: src/lookup3.c
Files: src/ext/lookup3.c
Copyright: 2006 Bob Jenkins
License: Public Domain

Expand Down
7 changes: 7 additions & 0 deletions fmt.sh
@@ -0,0 +1,7 @@
#!/bin/sh

clang-format-4.0 \
-style=file \
-i \
src/*.c \
src/*.h
4 changes: 2 additions & 2 deletions src/Makefile.am
Expand Up @@ -17,7 +17,7 @@ etcdir = $(sysconfdir)/dsc
etc_DATA = dsc.conf.sample

bin_PROGRAMS = dsc
dsc_SOURCES = base64.c \
dsc_SOURCES = ext/base64.c \
generic_counter.c \
pcap_layers/pcap_layers.c \
pcap.c \
Expand Down Expand Up @@ -58,7 +58,7 @@ dsc_SOURCES = base64.c \
dns_source_port_index.c \
config_hooks.c \
hashtbl.c \
lookup3.c \
ext/lookup3.c \
xmalloc.c \
inX_addr.c \
pcap-thread/pcap_thread.c \
Expand Down
91 changes: 42 additions & 49 deletions src/asn_index.c
Expand Up @@ -51,41 +51,41 @@
#include "hashtbl.h"
#include "syslog_debug.h"

extern int debug_flag;
extern char * geoip_asn_v4_dat;
extern int geoip_asn_v4_options;
extern char * geoip_asn_v6_dat;
extern int geoip_asn_v6_options;
static hashfunc asn_hashfunc;
extern int debug_flag;
extern char* geoip_asn_v4_dat;
extern int geoip_asn_v4_options;
extern char* geoip_asn_v6_dat;
extern int geoip_asn_v6_options;
static hashfunc asn_hashfunc;
static hashkeycmp asn_cmpfunc;

#define MAX_ARRAY_SZ 65536
static hashtbl *theHash = NULL;
static int next_idx = 0;
static GeoIP *geoip = NULL;
static GeoIP *geoip6 = NULL;
static char ipstr[81];
static char *nodb = "NODB";
static char *unknown = "??";
static char *unknown_v4 = "?4";
static char *unknown_v6 = "?6";
static char *_asn = NULL;
static hashtbl* theHash = NULL;
static int next_idx = 0;
static GeoIP* geoip = NULL;
static GeoIP* geoip6 = NULL;
static char ipstr[81];
static char* nodb = "NODB";
static char* unknown = "??";
static char* unknown_v4 = "?4";
static char* unknown_v6 = "?6";
static char* _asn = NULL;

typedef struct {
char *asn;
int index;
char* asn;
int index;
} asnobj;

const char *
asn_get_from_message(dns_message * m)
const char*
asn_get_from_message(dns_message* m)
{
transport_message *tm;
char *asn;
char *truncate;
transport_message* tm;
char* asn;
char* truncate;

tm = m->tm;

if (!inXaddr_ntop(&tm->src_ip_addr, ipstr, sizeof(ipstr)-1)) {
if (!inXaddr_ntop(&tm->src_ip_addr, ipstr, sizeof(ipstr) - 1)) {
dfprint(0, "asn_index: Error converting IP address");
return unknown;
}
Expand All @@ -96,31 +96,29 @@ asn_get_from_message(dns_message * m)
_asn = NULL;
}

switch(tm->ip_version) {
switch (tm->ip_version) {
case 4:
if (geoip) {
if ((_asn = GeoIP_name_by_addr(geoip, ipstr))) {
asn = _asn;
}
else {
} else {
asn = unknown_v4;
}
} else {
asn = nodb;
asn = nodb;
}
break;

case 6:
if (geoip6) {
if ((_asn = GeoIP_name_by_addr_v6(geoip6, ipstr))) {
asn = _asn;
}
else {
} else {
asn = unknown_v6;
}
break;
} else {
asn = nodb;
asn = nodb;
}
break;

Expand All @@ -144,17 +142,16 @@ asn_get_from_message(dns_message * m)
return asn;
}

int
asn_indexer(const void *vp)
int asn_indexer(const void* vp)
{
const dns_message *m = vp;
const char *asn;
asnobj *obj;
const dns_message* m = vp;
const char* asn;
asnobj* obj;

if (m->malformed)
return -1;

asn = asn_get_from_message((dns_message *) m);
asn = asn_get_from_message((dns_message*)m);
if (asn == NULL)
return -1;

Expand Down Expand Up @@ -190,10 +187,9 @@ asn_indexer(const void *vp)
return obj->index;
}

int
asn_iterator(char **label)
int asn_iterator(char** label)
{
asnobj *obj;
asnobj* obj;
static char label_buf[128];
if (0 == next_idx)
return -1;
Expand All @@ -209,27 +205,25 @@ asn_iterator(char **label)
return obj->index;
}

void
asn_reset()
void asn_reset()
{
theHash = NULL;
theHash = NULL;
next_idx = 0;
}

static unsigned int
asn_hashfunc(const void *key)
asn_hashfunc(const void* key)
{
return hashendian(key, strlen(key), 0);
}

static int
asn_cmpfunc(const void *a, const void *b)
asn_cmpfunc(const void* a, const void* b)
{
return strcasecmp(a, b);
}

void
asn_indexer_init()
void asn_indexer_init()
{
if (geoip_asn_v4_dat) {
geoip = GeoIP_open(geoip_asn_v4_dat, geoip_asn_v4_options);
Expand All @@ -248,8 +242,7 @@ asn_indexer_init()
memset(ipstr, 0, sizeof(ipstr));
if (geoip || geoip6) {
dsyslog(LOG_INFO, "asn_index: Sucessfully initialized GeoIP ASN");
}
else {
} else {
dsyslog(LOG_INFO, "asn_index: No database loaded for GeoIP ASN");
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/asn_index.h
Expand Up @@ -37,11 +37,10 @@
#ifndef __dsc_asn_index_h
#define __dsc_asn_index_h


/* check HAVE_LIBGEOIP before #including this file */

int asn_indexer(const void *);
int asn_iterator(char **label);
int asn_indexer(const void*);
int asn_iterator(char** label);
void asn_reset(void);

#endif /* __dsc_asn_index_h */
4 changes: 2 additions & 2 deletions src/base64.h
Expand Up @@ -37,7 +37,7 @@
#ifndef __dsc_base64_h
#define __dsc_base64_h

int base64_encode(const void *data, int size, char **str);
int base64_decode(const char *str, void *data);
int base64_encode(const void* data, int size, char** str);
int base64_decode(const char* str, void* data);

#endif /* __dsc_base64_h */
8 changes: 3 additions & 5 deletions src/certain_qnames_index.c
Expand Up @@ -46,10 +46,9 @@
#define QNAME_RSN 1
#define QNAME_OTHER 2

int
certain_qnames_indexer(const void *vp)
int certain_qnames_indexer(const void* vp)
{
const dns_message *m = vp;
const dns_message* m = vp;
if (m->malformed)
return -1;
if (0 == strcmp(m->qname, "localhost"))
Expand All @@ -59,8 +58,7 @@ certain_qnames_indexer(const void *vp)
return QNAME_OTHER;
}

int
certain_qnames_iterator(char **label)
int certain_qnames_iterator(char** label)
{
static int next_iter = 0;
if (NULL == label) {
Expand Down
4 changes: 2 additions & 2 deletions src/certain_qnames_index.h
Expand Up @@ -37,7 +37,7 @@
#ifndef __dsc_certain_qnames_index_h
#define __dsc_certain_qnames_index_h

int certain_qnames_indexer(const void *);
int certain_qnames_iterator(char **label);
int certain_qnames_indexer(const void*);
int certain_qnames_iterator(char** label);

#endif /* __dsc_certain_qnames_index_h */
36 changes: 16 additions & 20 deletions src/client_ip_addr_index.c
Expand Up @@ -45,20 +45,19 @@
#include "hashtbl.h"

#define MAX_ARRAY_SZ 65536
static hashtbl *theHash = NULL;
static int next_idx = 0;
static hashtbl* theHash = NULL;
static int next_idx = 0;

typedef struct
{
inX_addr addr;
int index;
int index;
} ipaddrobj;

int
cip_indexer(const void *vp)
int cip_indexer(const void* vp)
{
const dns_message *m = vp;
ipaddrobj *obj;
const dns_message* m = vp;
ipaddrobj* obj;
if (m->malformed)
return -1;
if (NULL == theHash) {
Expand All @@ -71,7 +70,7 @@ cip_indexer(const void *vp)
obj = acalloc(1, sizeof(*obj));
if (NULL == obj)
return -1;
obj->addr = m->client_ip_addr;
obj->addr = m->client_ip_addr;
obj->index = next_idx;
if (0 != hash_add(&obj->addr, obj, theHash)) {
afree(obj);
Expand All @@ -81,10 +80,9 @@ cip_indexer(const void *vp)
return obj->index;
}

int
cip_iterator(char **label)
int cip_iterator(char** label)
{
ipaddrobj *obj;
ipaddrobj* obj;
static char label_buf[128];
if (0 == next_idx)
return -1;
Expand All @@ -99,24 +97,22 @@ cip_iterator(char **label)
return obj->index;
}

void
cip_reset()
void cip_reset()
{
theHash = NULL;
theHash = NULL;
next_idx = 0;
}

unsigned int
ipaddr_hashfunc(const void *key)
ipaddr_hashfunc(const void* key)
{
const inX_addr *a = key;
const inX_addr* a = key;
return inXaddr_hash(a);
}

int
ipaddr_cmpfunc(const void *a, const void *b)
int ipaddr_cmpfunc(const void* a, const void* b)
{
const inX_addr *a1 = a;
const inX_addr *a2 = b;
const inX_addr* a1 = a;
const inX_addr* a2 = b;
return inXaddr_cmp(a1, a2);
}
8 changes: 4 additions & 4 deletions src/client_ip_addr_index.h
Expand Up @@ -37,12 +37,12 @@
#ifndef __dsc_client_ip_addr_index_h
#define __dsc_client_ip_addr_index_h

int cip_indexer(const void *);
int cip_iterator(char **label);
int cip_indexer(const void*);
int cip_iterator(char** label);
void cip_reset(void);

/* shared between client_ip_addr_index and server_ip_addr_index */
unsigned int ipaddr_hashfunc(const void *key);
int ipaddr_cmpfunc(const void *a, const void *b);
unsigned int ipaddr_hashfunc(const void* key);
int ipaddr_cmpfunc(const void* a, const void* b);

#endif /* __dsc_client_ip_addr_index_h */

0 comments on commit c528ccb

Please sign in to comment.