Fix style and compiler warnings

This commit is contained in:
Job Snijders
2021-08-17 19:34:30 +00:00
parent 791f3d7790
commit 85d833ca48
5 changed files with 204 additions and 198 deletions

View File

@@ -57,15 +57,15 @@ int pipelining = 1;
int expand_special_asn = 0;
static inline int
tentry_cmp(struct sx_tentry* a, struct sx_tentry* b)
tentry_cmp(struct sx_tentry *a, struct sx_tentry *b)
{
return strcasecmp(a->text, b->text);
}
RB_GENERATE(tentree, sx_tentry, entry, tentry_cmp);
RB_GENERATE_STATIC(tentree, sx_tentry, entry, tentry_cmp);
int
bgpq_expander_init(struct bgpq_expander* b, int af)
bgpq_expander_init(struct bgpq_expander *b, int af)
{
if (!af)
af = AF_INET;
@@ -118,9 +118,9 @@ fixups:
}
int
bgpq_expander_add_asset(struct bgpq_expander* b, char* as)
bgpq_expander_add_asset(struct bgpq_expander *b, char *as)
{
struct sx_slentry* le;
struct sx_slentry *le;
if (!b || !as)
return 0;
@@ -133,9 +133,9 @@ bgpq_expander_add_asset(struct bgpq_expander* b, char* as)
}
int
bgpq_expander_add_rset(struct bgpq_expander* b, char* rs)
bgpq_expander_add_rset(struct bgpq_expander *b, char *rs)
{
struct sx_slentry* le;
struct sx_slentry *le;
if (!b || !rs)
return 0;
@@ -150,10 +150,11 @@ bgpq_expander_add_rset(struct bgpq_expander* b, char* rs)
return 1;
}
int
bgpq_expander_add_already(struct bgpq_expander* b, char* rs)
static int
bgpq_expander_add_already(struct bgpq_expander *b, char *rs)
{
struct sx_tentry* le, lkey;
struct sx_tentry *le, lkey;
lkey.text = rs;
if (RB_FIND(tentree, &b->already, &lkey))
@@ -167,9 +168,10 @@ bgpq_expander_add_already(struct bgpq_expander* b, char* rs)
}
int
bgpq_expander_add_stop(struct bgpq_expander* b, char* rs)
bgpq_expander_add_stop(struct bgpq_expander *b, char *rs)
{
struct sx_tentry* le, lkey;
struct sx_tentry *le, lkey;
lkey.text = rs;
if (RB_FIND(tentree, &b->stoplist, &lkey))
@@ -183,11 +185,11 @@ bgpq_expander_add_stop(struct bgpq_expander* b, char* rs)
}
int
bgpq_expander_add_as(struct bgpq_expander* b, char* as)
bgpq_expander_add_as(struct bgpq_expander *b, char *as)
{
char* eoa;
uint32_t asn1 = 0, asn2 = 0;
uint32_t asno = 0;
char *eoa;
uint32_t asn1 = 0, asn2 = 0;
uint32_t asno = 0;
if (!b || !as)
return 0;
@@ -230,9 +232,10 @@ bgpq_expander_add_as(struct bgpq_expander* b, char* as)
}
int
bgpq_expander_add_prefix(struct bgpq_expander* b, char* prefix)
bgpq_expander_add_prefix(struct bgpq_expander *b, char *prefix)
{
struct sx_prefix *p = sx_prefix_alloc(NULL);
if (!sx_prefix_parse(p, 0, prefix)) {
sx_report(SX_ERROR, "Unable to parse prefix %s\n", prefix);
return 0;
@@ -255,30 +258,30 @@ bgpq_expander_add_prefix(struct bgpq_expander* b, char* prefix)
}
int
bgpq_expander_add_prefix_range(struct bgpq_expander* b, char* prefix)
bgpq_expander_add_prefix_range(struct bgpq_expander *b, char *prefix)
{
return sx_prefix_range_parse(b->tree, b->family, b->maxlen, prefix);
}
int
bgpq_expanded_macro(char* as, struct bgpq_expander* ex,
struct bgpq_request* req)
static int
bgpq_expanded_macro(char *as, struct bgpq_expander *ex,
struct bgpq_request *req)
{
bgpq_expander_add_as(ex, as);
return 1;
}
struct bgpq_request* bgpq_pipeline(struct bgpq_expander* b,
int (*callback)(char*, struct bgpq_expander* b, struct bgpq_request* req),
void* udata, char* fmt, ...);
int bgpq_expand_irrd(struct bgpq_expander* b,
int (*callback)(char*, struct bgpq_expander* b, struct bgpq_request* req),
void* udata, char* fmt, ...);
struct bgpq_request *bgpq_pipeline(struct bgpq_expander *b,
int (*callback)(char*, struct bgpq_expander *b, struct bgpq_request *req),
void *udata, char *fmt, ...);
int bgpq_expand_irrd(struct bgpq_expander *b,
int (*callback)(char*, struct bgpq_expander *b, struct bgpq_request *req),
void *udata, char *fmt, ...);
int
bgpq_expanded_macro_limit(char* as, struct bgpq_expander* b,
struct bgpq_request* req)
static int
bgpq_expanded_macro_limit(char *as, struct bgpq_expander *b,
struct bgpq_request *req)
{
if (!strncasecmp(as, "AS-", 3) || strchr(as, '-') || strchr(as, ':')) {
struct sx_tentry tkey = { .text = as };
@@ -300,7 +303,7 @@ bgpq_expanded_macro_limit(char* as, struct bgpq_expander* b,
req->depth + 1 < b->maxdepth)) {
bgpq_expander_add_already(b, as);
if (pipelining) {
struct bgpq_request* req1 = bgpq_pipeline(b,
struct bgpq_request *req1 = bgpq_pipeline(b,
bgpq_expanded_macro_limit, NULL, "!i%s\n",
as);
req1->depth = req->depth+1;
@@ -341,11 +344,11 @@ bgpq_expanded_macro_limit(char* as, struct bgpq_expander* b,
return 1;
}
int
bgpq_expanded_prefix(char* as, struct bgpq_expander* ex,
struct bgpq_request* req __attribute__((unused)))
static int
bgpq_expanded_prefix(char *as, struct bgpq_expander *ex,
struct bgpq_request *req __attribute__((unused)))
{
char* d = strchr(as, '^');
char *d = strchr(as, '^');
if (!d)
bgpq_expander_add_prefix(ex, as);
@@ -355,11 +358,11 @@ bgpq_expanded_prefix(char* as, struct bgpq_expander* ex,
return 1;
}
int
bgpq_expanded_v6prefix(char* prefix, struct bgpq_expander* ex,
static int
bgpq_expanded_v6prefix(char *prefix, struct bgpq_expander * ex,
struct bgpq_request* req)
{
char* d = strchr(prefix, '^');
char *d = strchr(prefix, '^');
if (!d)
bgpq_expander_add_prefix(ex, prefix);
@@ -369,10 +372,10 @@ bgpq_expanded_v6prefix(char* prefix, struct bgpq_expander* ex,
return 1;
}
int bgpq_pipeline_dequeue(int fd, struct bgpq_expander* b);
int bgpq_pipeline_dequeue(int fd, struct bgpq_expander *b);
static struct bgpq_request*
bgpq_request_alloc(char* request, int (*callback)(char*, struct bgpq_expander*,
bgpq_request_alloc(char *request, int (*callback)(char*, struct bgpq_expander*,
struct bgpq_request*), void* udata)
{
struct bgpq_request* bp = malloc(sizeof(struct bgpq_request));
@@ -400,9 +403,9 @@ bgpq_request_free(struct bgpq_request* req)
}
struct bgpq_request*
bgpq_pipeline(struct bgpq_expander* b,
bgpq_pipeline(struct bgpq_expander *b,
int (*callback)(char*, struct bgpq_expander*, struct bgpq_request*),
void* udata, char* fmt, ...)
void* udata, char *fmt, ...)
{
char request[128];
int ret;
@@ -447,10 +450,10 @@ bgpq_pipeline(struct bgpq_expander* b,
}
static void
bgpq_expander_invalidate_asn(struct bgpq_expander* b, const char* q)
bgpq_expander_invalidate_asn(struct bgpq_expander *b, const char *q)
{
if (!strncmp(q, "!gas", 4) || !strncmp(q, "!6as", 4)) {
char* eptr;
char *eptr;
unsigned long asn = strtoul(q+4, &eptr, 10), asn0, asn1 = 0;
if (!asn || asn == ULONG_MAX || asn >= 4294967295
|| (eptr && *eptr != '\n')) {
@@ -471,7 +474,7 @@ bgpq_expander_invalidate_asn(struct bgpq_expander* b, const char* q)
}
static void
bgpq_write(struct bgpq_expander* b)
bgpq_write(struct bgpq_expander *b)
{
while(!STAILQ_EMPTY(&b->wq)) {
struct bgpq_request* req = STAILQ_FIRST(&b->wq);
@@ -498,7 +501,7 @@ bgpq_write(struct bgpq_expander* b)
}
static int
bgpq_selread(struct bgpq_expander* b, char* buffer, int size)
bgpq_selread(struct bgpq_expander *b, char *buffer, int size)
{
fd_set rfd, wfd;
int ret;
@@ -530,8 +533,8 @@ repeat:
goto repeat;
}
int
bgpq_read(struct bgpq_expander* b)
static int
bgpq_read(struct bgpq_expander *b)
{
static char response[256];
static int off = 0;
@@ -544,7 +547,7 @@ bgpq_read(struct bgpq_expander* b)
SX_DEBUG(debug_expander > 2, "waiting for answer to %s,"
"init %i '%.*s'\n", req->request, off, off, response);
int ret = 0;
char* cres;
char *cres;
if ((cres=strchr(response, '\n')) != NULL)
goto have;
@@ -569,10 +572,10 @@ have:
response);
if (response[0] == 'A') {
char* eon, *c;
unsigned long togot = strtoul(response+1,&eon,10);
char* recvbuffer = malloc(togot+2);
int offset = 0;
char *eon, *c;
unsigned long offset = 0;
unsigned long togot = strtoul(response + 1, &eon, 10);
char *recvbuffer = malloc(togot + 2);
if (!recvbuffer) {
sx_report(SX_FATAL, "error allocating %lu "
@@ -588,7 +591,7 @@ have:
exit(1);
}
if (off - ((eon + 1) - response) > togot) {
if ((unsigned)(off - ((eon + 1) - response)) > togot) {
// full response and more data is already in buffer
memcpy(recvbuffer, eon + 1, togot);
offset = togot;
@@ -606,9 +609,9 @@ have:
off = 0;
}
SX_DEBUG(debug_expander>5,
SX_DEBUG(debug_expander > 5,
"starting read with ready '%.*s', waiting for "
"%lu\n", offset, recvbuffer, togot-offset);
"%lu\n", (int)offset, recvbuffer, togot - offset);
if (off > 0)
goto have3;
@@ -617,7 +620,7 @@ have:
reread:
ret = bgpq_selread(b, recvbuffer + offset, togot-offset);
ret = bgpq_selread(b, recvbuffer + offset, togot - offset);
if (ret < 0) {
if (errno == EAGAIN)
goto reread;
@@ -718,14 +721,15 @@ have3:
}
int
bgpq_expand_irrd(struct bgpq_expander* b,
bgpq_expand_irrd(struct bgpq_expander *b,
int (*callback)(char*, struct bgpq_expander*, struct bgpq_request* ),
void* udata, char* fmt, ...)
void* udata, char *fmt, ...)
{
char request[128], response[128];
va_list ap;
int ret, off = 0;
struct bgpq_request *req;
char request[128], response[128];
va_list ap;
size_t ret;
int off = 0;
struct bgpq_request *req;
va_start(ap, fmt);
vsnprintf(request, sizeof(request), fmt, ap);
@@ -737,7 +741,7 @@ bgpq_expand_irrd(struct bgpq_expander* b,
ret=write(b->fd, request, strlen(request));
if (ret != strlen(request)) {
sx_report(SX_FATAL,"Partial write to IRRd, only %i bytes "
sx_report(SX_FATAL,"Partial write to IRRd, only %lu bytes "
"written: %s\n", ret, strerror(errno));
exit(1);
}
@@ -763,10 +767,10 @@ repeat:
"'%s'\n", (unsigned long)strlen(response), response);
if (response[0] == 'A') {
char* eon, *c;
long togot = strtoul(response+1, &eon, 10);
char *recvbuffer = malloc(togot + 2);
int offset = 0;
char *eon, *c;
long togot = strtoul(response+1, &eon, 10);
char *recvbuffer = malloc(togot + 2);
int offset = 0;
if (!recvbuffer) {
sx_report(SX_FATAL, "Error allocating %lu bytes: %s\n",
@@ -866,12 +870,13 @@ have3:
}
int
bgpq_expand(struct bgpq_expander* b)
bgpq_expand(struct bgpq_expander *b)
{
int fd = -1, err, ret, aquery = 0;
struct sx_slentry* mc;
struct addrinfo hints, *res=NULL, *rp;
struct linger sl;
int fd = -1, err, ret, aquery = 0;
struct sx_slentry *mc;
struct addrinfo hints, *res = NULL, *rp;
struct linger sl;
sl.l_onoff = 1;
sl.l_linger = 5;

View File

@@ -58,41 +58,41 @@ struct bgpq_expander;
struct bgpq_request {
STAILQ_ENTRY(bgpq_request) next;
char* request;
int size, offset;
int (*callback)(char*, struct bgpq_expander*, struct bgpq_request*);
void *udata;
unsigned depth;
char *request;
int size, offset;
int (*callback)(char*, struct bgpq_expander*, struct bgpq_request*);
void *udata;
unsigned int depth;
};
struct bgpq_expander {
struct sx_radix_tree* tree;
struct sx_radix_tree *tree;
STAILQ_HEAD(sx_slentries, sx_slentry) macroses, rsets;
RB_HEAD(tentree, sx_tentry) already, stoplist;
int family;
char* sources;
uint32_t asnumber;
int aswidth;
char* name;
bgpq_vendor_t vendor;
bgpq_gen_t generation;
int identify;
int sequence;
int maxdepth;
int validate_asns;
unsigned char* asn32s[65536];
struct bgpq_prequest* firstpipe, *lastpipe;
int piped;
char* match;
char* server;
char* port;
char* format;
unsigned maxlen;
int family;
char *sources;
uint32_t asnumber;
int aswidth;
char *name;
bgpq_vendor_t vendor;
bgpq_gen_t generation;
int identify;
int sequence;
unsigned int maxdepth;
unsigned int cdepth;
int validate_asns;
unsigned char *asn32s[65536];
struct bgpq_prequest *firstpipe, *lastpipe;
int piped;
char *match;
char *server;
char *port;
char *format;
unsigned int maxlen;
STAILQ_HEAD(bgpq_requests, bgpq_request) wq, rq;
int fd, cdepth;
int fd;
};
int bgpq_expander_init(struct bgpq_expander* b, int af);
int bgpq_expander_add_asset(struct bgpq_expander* b, char* set);
int bgpq_expander_add_rset(struct bgpq_expander* b, char* set);

View File

@@ -36,6 +36,7 @@
#include <string.h>
#include <unistd.h>
#include "extern.h"
#include "sx_report.h"
#ifndef SX_MAXSOCKBUF_MAX

View File

@@ -43,9 +43,9 @@ int debug_aggregation = 0;
extern int debug_expander;
struct sx_prefix*
sx_prefix_alloc(struct sx_prefix* p)
sx_prefix_alloc(struct sx_prefix *p)
{
struct sx_prefix* sp = malloc(sizeof(struct sx_prefix));
struct sx_prefix *sp = malloc(sizeof(struct sx_prefix));
if (!sp)
return NULL;
@@ -59,7 +59,7 @@ sx_prefix_alloc(struct sx_prefix* p)
}
void
sx_prefix_destroy(struct sx_prefix* p)
sx_prefix_destroy(struct sx_prefix *p)
{
if (p)
free(p);
@@ -80,10 +80,10 @@ sx_radix_node_destroy(struct sx_radix_node *n)
}
void
sx_prefix_adjust_masklen(struct sx_prefix* p)
sx_prefix_adjust_masklen(struct sx_prefix *p)
{
int nbytes = (p->family == AF_INET ? 4 : 16);
int i;
unsigned int nbytes = (p->family == AF_INET ? 4 : 16);
unsigned int i;
if (p->masklen == nbytes * 8)
return; /* mask is all ones */
@@ -97,10 +97,10 @@ sx_prefix_adjust_masklen(struct sx_prefix* p)
}
}
void
sx_prefix_mask(struct sx_prefix* p, struct sx_prefix* q)
static void
sx_prefix_mask(struct sx_prefix *p, struct sx_prefix *q)
{
int i;
unsigned int i;
memset(q->addr.addrs, 0, sizeof(q->addr.addrs));
@@ -114,10 +114,10 @@ sx_prefix_mask(struct sx_prefix* p, struct sx_prefix* q)
q->addr.addrs[p->masklen / 8] |= (1 << (8 - i));
}
void
sx_prefix_imask(struct sx_prefix* p, struct sx_prefix* q)
static void
sx_prefix_imask(struct sx_prefix *p, struct sx_prefix *q)
{
int i;
unsigned int i;
memset(q->addr.addrs, 0xff, sizeof(q->addr.addrs));
@@ -133,11 +133,11 @@ sx_prefix_imask(struct sx_prefix* p, struct sx_prefix* q)
int
sx_prefix_parse(struct sx_prefix* p, int af, char* text)
sx_prefix_parse(struct sx_prefix *p, int af, char *text)
{
char* c = NULL;
int masklen, ret;
char mtext[INET6_ADDRSTRLEN+5];
char *c = NULL;
int masklen, ret;
char mtext[INET6_ADDRSTRLEN+5];
strlcpy(mtext, text, sizeof(mtext));
@@ -229,10 +229,10 @@ fixups:
return 0;
}
int
sx_prefix_isbitset(struct sx_prefix* p, int n)
static int
sx_prefix_isbitset(struct sx_prefix *p, int n)
{
unsigned char s;
unsigned char s;
/* bits outside the prefix considered unset */
if (p->family == AF_INET && (n < 0 || n > 32))
@@ -244,10 +244,10 @@ sx_prefix_isbitset(struct sx_prefix* p, int n)
return (s & (0x80 >> ((n - 1) % 8))) ? 1 : 0;
}
void
sx_prefix_setbit(struct sx_prefix* p, int n)
static void
sx_prefix_setbit(struct sx_prefix *p, int n)
{
unsigned char* s;
unsigned char *s;
if (p->family == AF_INET && (n < 0 || n > 32))
return;
@@ -260,8 +260,8 @@ sx_prefix_setbit(struct sx_prefix* p, int n)
}
int
sx_radix_tree_insert_specifics(struct sx_radix_tree* t, struct sx_prefix *p,
static int
sx_radix_tree_insert_specifics(struct sx_radix_tree *t, struct sx_prefix *p,
unsigned min, unsigned max)
{
struct sx_prefix *np;
@@ -285,12 +285,12 @@ sx_radix_tree_insert_specifics(struct sx_radix_tree* t, struct sx_prefix *p,
}
int
sx_prefix_range_parse(struct sx_radix_tree* tree, int af, int maxlen,
char* text)
sx_prefix_range_parse(struct sx_radix_tree *tree, int af, unsigned int maxlen,
char *text)
{
char* d = strchr(text, '^');
struct sx_prefix *p;
unsigned long min, max;
char *d = strchr(text, '^');
struct sx_prefix *p;
unsigned long min, max;
p = sx_prefix_alloc(NULL);
@@ -326,7 +326,7 @@ sx_prefix_range_parse(struct sx_radix_tree* tree, int af, int maxlen,
min = p->masklen;
max = maxlen;
} else if (isdigit(d[1])) {
char* dm = NULL;
char *dm = NULL;
min = strtoul(d+1, &dm, 10);
if (dm && *dm == '-' && isdigit(dm[1])) {
max = strtoul(dm + 1, NULL, 10);
@@ -368,9 +368,9 @@ sx_prefix_range_parse(struct sx_radix_tree* tree, int af, int maxlen,
}
struct sx_prefix*
sx_prefix_new(int af, char* text)
sx_prefix_new(int af, char *text)
{
struct sx_prefix* p = NULL;
struct sx_prefix *p = NULL;
if (!text)
return NULL;
@@ -389,7 +389,7 @@ sx_prefix_new(int af, char* text)
}
int
sx_prefix_fprint(FILE* f, struct sx_prefix* p)
sx_prefix_fprint(FILE *f, struct sx_prefix *p)
{
char buffer[128];
@@ -403,7 +403,7 @@ sx_prefix_fprint(FILE* f, struct sx_prefix* p)
}
int
sx_prefix_snprintf_sep(struct sx_prefix* p, char* rbuffer, int srb, char* sep)
sx_prefix_snprintf_sep(struct sx_prefix *p, char *rbuffer, int srb, char *sep)
{
char buffer[128];
@@ -421,20 +421,19 @@ sx_prefix_snprintf_sep(struct sx_prefix* p, char* rbuffer, int srb, char* sep)
}
int
sx_prefix_snprintf(struct sx_prefix* p, char* rbuffer, int srb)
sx_prefix_snprintf(struct sx_prefix *p, char *rbuffer, int srb)
{
return sx_prefix_snprintf_sep(p, rbuffer, srb, "/");
}
void
sx_prefix_snprintf_fmt(struct sx_prefix* p, FILE* f,
const char* name, const char* format,
sx_prefix_snprintf_fmt(struct sx_prefix *p, FILE *f,
const char *name, const char *format,
unsigned int aggregateLow, unsigned int aggregateHi)
{
unsigned off = 0;
const char* c = format;
struct sx_prefix *q = sx_prefix_alloc(NULL);
char prefix[128];
const char *c = format;
struct sx_prefix *q = sx_prefix_alloc(NULL);
char prefix[128];
while (*c) {
if (*c == '%') {
@@ -511,7 +510,7 @@ sx_prefix_snprintf_fmt(struct sx_prefix* p, FILE* f,
}
int
sx_prefix_jsnprintf(struct sx_prefix* p, char* rbuffer, int srb)
sx_prefix_jsnprintf(struct sx_prefix *p, char *rbuffer, int srb)
{
char buffer[128];
@@ -527,7 +526,7 @@ sx_prefix_jsnprintf(struct sx_prefix* p, char* rbuffer, int srb)
struct sx_radix_tree*
sx_radix_tree_new(int af)
{
struct sx_radix_tree* rt = malloc(sizeof(struct sx_radix_tree));
struct sx_radix_tree *rt = malloc(sizeof(struct sx_radix_tree));
if (!rt)
return NULL;
@@ -539,15 +538,15 @@ sx_radix_tree_new(int af)
}
int
sx_radix_tree_empty(struct sx_radix_tree* t)
sx_radix_tree_empty(struct sx_radix_tree *t)
{
return t->head == NULL;
}
struct sx_radix_node*
sx_radix_node_new(struct sx_prefix* prefix)
sx_radix_node_new(struct sx_prefix *prefix)
{
struct sx_radix_node* rn = malloc(sizeof(struct sx_radix_node));
struct sx_radix_node *rn = malloc(sizeof(struct sx_radix_node));
if (!rn)
return NULL;
@@ -560,20 +559,20 @@ sx_radix_node_new(struct sx_prefix* prefix)
return rn;
}
int
sx_prefix_eqbits(struct sx_prefix* a, struct sx_prefix* b)
static int
sx_prefix_eqbits(struct sx_prefix *a, struct sx_prefix *b)
{
int i;
int nbytes = (a->family == AF_INET ? 4 : 16);
unsigned int i;
unsigned int nbytes = (a->family == AF_INET ? 4 : 16);
for (i = 0; i < nbytes; i++) {
if (a->addr.addrs[i] == b->addr.addrs[i])
continue;
else {
int j;
unsigned int j;
for (j = 0; j < 8 && i * 8 + j <= a->masklen
&& i * 8 + j<= b->masklen; j++) {
&& i * 8 + j <= b->masklen; j++) {
if ((a->addr.addrs[i] & (0x80 >> j))
!= (b->addr.addrs[i] & (0x80 >> j)))
return i * 8 + j;
@@ -588,9 +587,9 @@ sx_prefix_eqbits(struct sx_prefix* a, struct sx_prefix* b)
}
struct sx_prefix*
sx_prefix_overlay(struct sx_prefix* p, int n)
sx_prefix_overlay(struct sx_prefix *p, int n)
{
struct sx_prefix* sp = sx_prefix_alloc(p);
struct sx_prefix *sp = sx_prefix_alloc(p);
sp->masklen = n;
sx_prefix_adjust_masklen(sp);
@@ -598,7 +597,7 @@ sx_prefix_overlay(struct sx_prefix* p, int n)
}
void
sx_radix_tree_unlink(struct sx_radix_tree* tree, struct sx_radix_node* node)
sx_radix_tree_unlink(struct sx_radix_tree *tree, struct sx_radix_node *node)
{
next:
if (node->r && node->l)
@@ -674,10 +673,10 @@ next:
struct sx_radix_node*
sx_radix_tree_lookup(struct sx_radix_tree* tree, struct sx_prefix* prefix)
sx_radix_tree_lookup(struct sx_radix_tree *tree, struct sx_prefix *prefix)
{
int eb;
struct sx_radix_node* candidate = NULL, *chead;
unsigned int eb;
struct sx_radix_node *candidate = NULL, *chead;
if (!tree || !prefix)
return NULL;
@@ -738,19 +737,19 @@ next:
struct sx_radix_node*
sx_radix_tree_insert(struct sx_radix_tree* tree, struct sx_prefix* prefix)
sx_radix_tree_insert(struct sx_radix_tree *tree, struct sx_prefix *prefix)
{
int eb;
struct sx_radix_node** candidate=NULL, *chead;
unsigned int eb;
struct sx_radix_node **candidate=NULL, *chead;
if (!tree || !prefix)
return NULL;
if (tree->family!=prefix->family)
if (tree->family != prefix->family)
return NULL;
if (!tree->head) {
tree->head=sx_radix_node_new(prefix);
tree->head = sx_radix_node_new(prefix);
return tree->head;
}
@@ -762,7 +761,7 @@ next:
if (eb < prefix->masklen && eb < chead->prefix->masklen) {
struct sx_prefix *neoRoot = sx_prefix_alloc(prefix);
struct sx_radix_node* rn, *ret=sx_radix_node_new(prefix);
struct sx_radix_node *rn, *ret=sx_radix_node_new(prefix);
neoRoot->masklen = eb;
sx_prefix_adjust_masklen(neoRoot);
rn=sx_radix_node_new(neoRoot);
@@ -789,7 +788,7 @@ next:
*candidate = rn;
return ret;
} else if (eb == prefix->masklen && eb < chead->prefix->masklen) {
struct sx_radix_node* ret = sx_radix_node_new(prefix);
struct sx_radix_node *ret = sx_radix_node_new(prefix);
if (sx_prefix_isbitset(chead->prefix, eb + 1)) {
ret->r = chead;
} else {
@@ -838,10 +837,11 @@ next:
}
void
sx_radix_node_fprintf(struct sx_radix_node* node, void* udata)
sx_radix_node_fprintf(struct sx_radix_node *node, void *udata)
{
FILE* out = (udata?udata:stdout);
char buffer[128];
FILE *out = (udata?udata:stdout);
char buffer[128];
if (!node) {
fprintf(out, "(null)\n");
} else {
@@ -851,8 +851,8 @@ sx_radix_node_fprintf(struct sx_radix_node* node, void* udata)
}
int
sx_radix_node_foreach(struct sx_radix_node* node,
void (*func)(struct sx_radix_node*, void*), void* udata)
sx_radix_node_foreach(struct sx_radix_node *node,
void (*func)(struct sx_radix_node*, void*), void *udata)
{
func(node, udata);
@@ -866,8 +866,8 @@ sx_radix_node_foreach(struct sx_radix_node* node,
}
int
sx_radix_tree_foreach(struct sx_radix_tree* tree,
void (*func)(struct sx_radix_node*, void*), void* udata)
sx_radix_tree_foreach(struct sx_radix_tree *tree,
void (*func)(struct sx_radix_node *, void *), void *udata)
{
if (!func || !tree || !tree->head)
return 0;
@@ -876,8 +876,8 @@ sx_radix_tree_foreach(struct sx_radix_tree* tree,
return 0;
}
int
sx_radix_node_aggregate(struct sx_radix_node* node)
static int
sx_radix_node_aggregate(struct sx_radix_node *node)
{
if (node->l)
sx_radix_node_aggregate(node->l);
@@ -1048,7 +1048,7 @@ sx_radix_node_aggregate(struct sx_radix_node* node)
}
int
sx_radix_tree_aggregate(struct sx_radix_tree* tree)
sx_radix_tree_aggregate(struct sx_radix_tree *tree)
{
if (tree && tree->head)
return sx_radix_node_aggregate(tree->head);
@@ -1057,7 +1057,7 @@ sx_radix_tree_aggregate(struct sx_radix_tree* tree)
}
static void
setGlueUpTo(struct sx_radix_node* node, void* udata)
setGlueUpTo(struct sx_radix_node *node, void *udata)
{
unsigned refine = *(unsigned*)udata;
@@ -1065,8 +1065,8 @@ setGlueUpTo(struct sx_radix_node* node, void* udata)
node->isGlue = 1;
}
int
sx_radix_node_refine(struct sx_radix_node* node, unsigned refine)
static int
sx_radix_node_refine(struct sx_radix_node *node, unsigned refine)
{
if (!node->isGlue && node->prefix->masklen<refine) {
node->isAggregate = 1;
@@ -1108,7 +1108,7 @@ sx_radix_node_refine(struct sx_radix_node* node, unsigned refine)
}
int
sx_radix_tree_refine(struct sx_radix_tree* tree, unsigned refine)
sx_radix_tree_refine(struct sx_radix_tree *tree, unsigned refine)
{
if (tree && tree->head)
return sx_radix_node_refine(tree->head, refine);
@@ -1117,7 +1117,7 @@ sx_radix_tree_refine(struct sx_radix_tree* tree, unsigned refine)
}
static void
setGlueFrom(struct sx_radix_node* node, void* udata)
setGlueFrom(struct sx_radix_node *node, void *udata)
{
unsigned refine = *(unsigned*)udata;
@@ -1126,7 +1126,7 @@ setGlueFrom(struct sx_radix_node* node, void* udata)
}
static int
sx_radix_node_refineLow(struct sx_radix_node* node, unsigned refineLow)
sx_radix_node_refineLow(struct sx_radix_node *node, unsigned refineLow)
{
if (!node->isGlue && node->prefix->masklen<=refineLow) {
@@ -1177,7 +1177,7 @@ sx_radix_node_refineLow(struct sx_radix_node* node, unsigned refineLow)
}
int
sx_radix_tree_refineLow(struct sx_radix_tree* tree, unsigned refineLow)
sx_radix_tree_refineLow(struct sx_radix_tree *tree, unsigned refineLow)
{
if (tree && tree->head)
return sx_radix_node_refineLow(tree->head, refineLow);
@@ -1188,10 +1188,10 @@ sx_radix_tree_refineLow(struct sx_radix_tree* tree, unsigned refineLow)
#if SX_PTREE_TEST
int
main() {
struct sx_prefix* p;
int n;
struct sx_radix_tree* tree;
struct sx_radix_node* node;
struct sx_prefix *p;
struct sx_radix_tree *tree;
struct sx_radix_node *node;
int n;
p = sx_prefix_new(0, "10.11.12.13/24");
sx_prefix_fprint(stdout, p);

View File

@@ -34,7 +34,7 @@
typedef struct sx_prefix {
int family;
int masklen;
unsigned int masklen;
union {
struct in_addr addr;
struct in6_addr addr6;
@@ -43,19 +43,19 @@ typedef struct sx_prefix {
} sx_prefix_t;
typedef struct sx_radix_node {
struct sx_radix_node* parent, *l, *r, *son;
void* payload;
unsigned int isGlue:1;
unsigned int isAggregated:1;
unsigned int isAggregate:1;
unsigned int aggregateLow;
unsigned int aggregateHi;
struct sx_prefix *prefix;
struct sx_radix_node *parent, *l, *r, *son;
void *payload;
unsigned int isGlue:1;
unsigned int isAggregated:1;
unsigned int isAggregate:1;
unsigned int aggregateLow;
unsigned int aggregateHi;
struct sx_prefix *prefix;
} sx_radix_node_t;
typedef struct sx_radix_tree {
int family;
struct sx_radix_node* head;
int family;
struct sx_radix_node *head;
} sx_radix_tree_t;
/* most common operations with the tree is to: lookup/insert/unlink */
@@ -73,7 +73,7 @@ void sx_radix_node_destroy(struct sx_radix_node* p);
void sx_prefix_adjust_masklen(struct sx_prefix* p);
struct sx_prefix* sx_prefix_new(int af, char* text);
int sx_prefix_parse(struct sx_prefix* p, int af, char* text);
int sx_prefix_range_parse(struct sx_radix_tree* t, int af, int ml, char* text);
int sx_prefix_range_parse(struct sx_radix_tree* t, int af, unsigned int ml, char* text);
int sx_prefix_fprint(FILE* f, struct sx_prefix* p);
int sx_prefix_snprintf(struct sx_prefix* p, char* rbuffer, int srb);
int sx_prefix_snprintf_sep(struct sx_prefix* p, char* rbuffer, int srb, char*);