From 81068a627272b9486bbfb01485fa840aa3f798a0 Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Tue, 10 Apr 2012 19:19:53 +0200 Subject: [PATCH] many: moved asert to bug_on Signed-off-by: Daniel Borkmann --- src/aslookup.c | 26 ++++++++++++++--- src/ct_client.c | 3 +- src/ifpps.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/proto_ipv4.h | 6 ++-- src/ring_rx.c | 7 ++--- src/ring_tx.c | 7 ++--- 6 files changed, 118 insertions(+), 19 deletions(-) diff --git a/src/aslookup.c b/src/aslookup.c index 74534c84..81305ec4 100644 --- a/src/aslookup.c +++ b/src/aslookup.c @@ -10,13 +10,13 @@ #include #include #include -#include #include #include "die.h" #include "xstring.h" #include "aslookup.h" #include "xmalloc.h" +#include "built_in.h" static int ai_family = 0; static int ai_socktype = 0; @@ -29,10 +29,11 @@ int aslookup_prepare(const char *server, const char *port) struct addrinfo hints, *ahead, *ai; struct sockaddr_in6 *saddr6; - assert(server && port); - memset(&ai_ss, 0, sizeof(ai_ss)); + bug_on(!server || !port); + memset(&ai_ss, 0, sizeof(ai_ss)); memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; @@ -78,11 +79,12 @@ int aslookup(const char *lhost, struct asrecord *rec) char *buff; size_t len = 1024; - assert(strlen(lhost) + 8 < len); + bug_on(strlen(lhost) + 8 >= len); fd = socket(ai_family, ai_socktype, ai_protocol); if (fd < 0) return -EIO; + ret = connect(fd, (struct sockaddr *) &ai_ss, sizeof(ai_ss)); if (ret < 0) return -EIO; @@ -102,78 +104,94 @@ int aslookup(const char *lhost, struct asrecord *rec) while ((err = read(fd, buff, len)) > 0) { int state = 0, i; char *ptr = skips(buff), *ptr2; + for (i = 0; i < len; ++i) { if (buff[i] == '|' && state == 0) { buff[i] = 0; ptr2 = &buff[i] - 1; + while (*ptr2 == ' ' && ptr2 > ptr) { *ptr2 = 0; ptr2--; } + strlcpy(rec->number, ptr, strlen(ptr) + 1); ptr = skips(&buff[i] + 1); state = 1; } else if (buff[i] == '|' && state == 1) { buff[i] = 0; ptr2 = &buff[i] - 1; + while (*ptr2 == ' ' && ptr2 > ptr) { *ptr2 = 0; ptr2--; } + strlcpy(rec->ip, ptr, strlen(ptr) + 1); ptr = skips(&buff[i] + 1); state = 2; } else if (buff[i] == '|' && state == 2) { buff[i] = 0; ptr2 = &buff[i] - 1; + while (*ptr2 == ' ' && ptr2 > ptr) { *ptr2 = 0; ptr2--; } + strlcpy(rec->prefix, ptr, strlen(ptr) + 1); ptr = skips(&buff[i] + 1); state = 3; } else if (buff[i] == '|' && state == 3) { buff[i] = 0; ptr2 = &buff[i] - 1; + while (*ptr2 == ' ' && ptr2 > ptr) { *ptr2 = 0; ptr2--; } + strlcpy(rec->country, ptr, strlen(ptr) + 1); ptr = skips(&buff[i] + 1); state = 4; } else if (buff[i] == '|' && state == 4) { buff[i] = 0; ptr2 = &buff[i] - 1; + while (*ptr2 == ' ' && ptr2 > ptr) { *ptr2 = 0; ptr2--; } + strlcpy(rec->registry, ptr, strlen(ptr) + 1); ptr = skips(&buff[i] + 1); state = 5; } else if (buff[i] == '|' && state == 5) { buff[i] = 0; ptr2 = &buff[i] - 1; + while (*ptr2 == ' ' && ptr2 > ptr) { *ptr2 = 0; ptr2--; } + strlcpy(rec->since, ptr, strlen(ptr) + 1); ptr = skips(&buff[i] + 1); state = 6; } else if (buff[i] == '\n' && state == 6) { buff[i] = 0; ptr2 = &buff[i] - 1; + while (*ptr2 == ' ' && ptr2 > ptr) { *ptr2 = 0; ptr2--; } + strlcpy(rec->name, ptr, strlen(ptr) + 1); goto out; } } + memset(buff, 0, len); } out: diff --git a/src/ct_client.c b/src/ct_client.c index e2f9babf..9c60a845 100644 --- a/src/ct_client.c +++ b/src/ct_client.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -267,7 +266,7 @@ static void notify_init(int fd, int udp, struct curve25519_proto *p, err = crypto_auth_hmacsha512256(auth, (unsigned char *) cbuff, clen, token); if (unlikely(err)) syslog_panic("Cannot create init hmac message!\n"); - assert(132 == clen + sizeof(auth)); + bug_on(132 != clen + sizeof(auth)); pad = mt_rand_int32() % 200; msg_len = clen + sizeof(auth) + pad; msg = xzmalloc(msg_len); diff --git a/src/ifpps.c b/src/ifpps.c index 98afcb79..fd24c124 100644 --- a/src/ifpps.c +++ b/src/ifpps.c @@ -118,12 +118,12 @@ Please report bugs to #include #include #include -#include #include "die.h" #include "xmalloc.h" #include "xsys.h" #include "xio.h" +#include "built_in.h" /* * TODO: Cleanups, this got quite a hack over time. @@ -216,17 +216,23 @@ static int rxtx_stats(const char *ifname, struct ifstat *s) whine("Cannot open /proc/net/dev!\n"); return -ENOENT; } + + /* Omit header */ ptr = fgets(buf, sizeof(buf), fp); ptr = fgets(buf, sizeof(buf), fp); + memset(buf, 0, sizeof(buf)); while (fgets(buf, sizeof(buf), fp) != NULL) { buf[sizeof(buf) -1] = 0; + if (strstr(buf, ifname) == NULL) continue; + ptr = buf; while (*ptr != ':') ptr++; ptr++; + ret = sscanf(ptr, "%lu%lu%lu%lu%lu%lu%lu%*u%lu%lu%lu%lu%lu%lu%lu", &s->rx_bytes, &s->rx_packets, &s->rx_errors, &s->rx_drops, &s->rx_fifo, &s->rx_frame, @@ -238,9 +244,12 @@ static int rxtx_stats(const char *ifname, struct ifstat *s) found = 0; break; } + memset(buf, 0, sizeof(buf)); } + fclose(fp); + return found; } @@ -255,11 +264,13 @@ static int wifi_stats(const char *ifname, struct ifstat *s) s->wifi_bitrate = 0; return 0; } + s->wifi_bitrate = wireless_bitrate(ifname); s->wifi_signal_level = adjust_dbm_level(ws.qual.level); s->wifi_noise_level = adjust_dbm_level(ws.qual.noise); s->wifi_link_qual = ws.qual.qual; s->wifi_link_qual_max = wireless_rangemax_sigqual(ifname); + return ret; } @@ -276,6 +287,7 @@ static void stats_check_alloc(struct ifstat *s) if (s->cpu_sys) xfree(s->cpu_sys); if (s->cpu_idle) xfree(s->cpu_idle); if (s->cpu_iow) xfree(s->cpu_iow); + s->irqs_srx = xzmalloc(sizeof(*(s->irqs_srx)) * cpus); s->irqs_stx = xzmalloc(sizeof(*(s->irqs_stx)) * cpus); s->irqs = xzmalloc(sizeof(*(s->irqs)) * cpus); @@ -299,18 +311,25 @@ static int irq_sstats(struct ifstat *s) whine("Cannot open /proc/softirqs!\n"); return -ENOENT; } + stats_check_alloc(s); + memset(buff, 0, sizeof(buff)); while (fgets(buff, sizeof(buff), fp) != NULL) { buff[sizeof(buff) - 1] = 0; + if ((ptr = strstr(buff, "NET_TX:")) == NULL) { ptr = strstr(buff, "NET_RX:"); + if (ptr == NULL) continue; rx = 1; - } else + } else { rx = 0; + } + ptr += strlen("NET_TX:"); + for (i = 0; i < s->irqs_len; ++i) { ptr++; while (*ptr == ' ') @@ -324,9 +343,12 @@ static int irq_sstats(struct ifstat *s) else s->irqs_stx[i] = atoi(ptr2); } + memset(buff, 0, sizeof(buff)); } + fclose(fp); + return 0; } @@ -342,33 +364,43 @@ static int mem_stats(struct ifstat *s) whine("Cannot open /proc/meminfo!\n"); return -ENOENT; } + memset(buff, 0, sizeof(buff)); while (fgets(buff, sizeof(buff), fp) != NULL) { buff[sizeof(buff) - 1] = 0; + if ((ptr = strstr(buff, "MemTotal:")) != NULL) { ptr += strlen("MemTotal:"); ptr++; + while (*ptr == ' ') ptr++; + ret = sscanf(ptr, "%lu", &total); if (ret != 1) total = 0; } else if ((ptr = strstr(buff, "MemFree:")) != NULL) { ptr += strlen("MemFree:"); ptr++; + while (*ptr == ' ') ptr++; + ret = sscanf(ptr, "%lu", &free); if (ret != 1) free = 0; } + memset(buff, 0, sizeof(buff)); } + if (total > 0) s->mem_used = 100.f * (total - free) / total; else s->mem_used = 0.f; + fclose(fp); + return 0; } @@ -383,22 +415,28 @@ static int sys_stats(struct ifstat *s) whine("Cannot open /proc/stat!\n"); return -ENOENT; } + stats_check_alloc(s); + memset(buff, 0, sizeof(buff)); while (fgets(buff, sizeof(buff), fp) != NULL) { buff[sizeof(buff) - 1] = 0; + if ((ptr = strstr(buff, "cpu")) != NULL) { ptr += strlen("cpu"); if (*ptr == ' ') goto next; ptr2 = ptr; + while (*ptr != ' ' && *ptr != 0) ptr++; *ptr = 0; + cpu = atoi(ptr2); if (cpu < 0 || cpu >= s->irqs_len) goto next; ptr++; + ret = sscanf(ptr, "%lu%lu%lu%lu%lu", &s->cpu_user[cpu], &s->cpu_nice[cpu], &s->cpu_sys[cpu], &s->cpu_idle[cpu], &s->cpu_iow[cpu]); @@ -407,32 +445,40 @@ static int sys_stats(struct ifstat *s) } else if ((ptr = strstr(buff, "ctxt")) != NULL) { ptr += strlen("ctxt"); ptr++; + while (*ptr == ' ') ptr++; + ret = sscanf(ptr, "%lu", &s->ctxt); if (ret != 1) s->ctxt = 0; } else if ((ptr = strstr(buff, "processes")) != NULL) { ptr += strlen("processes"); ptr++; + while (*ptr == ' ') ptr++; + ret = sscanf(ptr, "%lu", &s->forks); if (ret != 1) s->forks = 0; } else if ((ptr = strstr(buff, "procs_running")) != NULL) { ptr += strlen("procs_running"); ptr++; + while (*ptr == ' ') ptr++; + ret = sscanf(ptr, "%lu", &s->procs_run); if (ret != 1) s->procs_run = 0; } else if ((ptr = strstr(buff, "procs_blocked")) != NULL) { ptr += strlen("procs_blocked"); ptr++; + while (*ptr == ' ') ptr++; + ret = sscanf(ptr, "%lu", &s->procs_iow); if (ret != 1) s->procs_iow = 0; @@ -440,7 +486,9 @@ static int sys_stats(struct ifstat *s) next: memset(buff, 0, sizeof(buff)); } + fclose(fp); + return 0; } @@ -453,23 +501,30 @@ static int irq_stats(const char *ifname, struct ifstat *s) /* We exclude lo! */ if (!strncmp("lo", ifname, strlen("lo"))) return 0; + FILE *fp = fopen("/proc/interrupts", "r"); if (!fp) { whine("Cannot open /proc/interrupts!\n"); return -ENOENT; } + stats_check_alloc(s); + memset(buff, 0, sizeof(buff)); while (fgets(buff, sizeof(buff), fp) != NULL) { buff[sizeof(buff) - 1] = 0; + if (strstr(buff, ifname) == NULL) continue; + ptr = buff; while (*ptr != ':') ptr++; *ptr = 0; s->irq_nr = atoi(buff); - assert(s->irq_nr != 0); + + bug_on(s->irq_nr == 0); + for (i = 0; i < s->irqs_len; ++i) { ptr++; ptr2 = ptr; @@ -480,9 +535,12 @@ static int irq_stats(const char *ifname, struct ifstat *s) *ptr = 0; s->irqs[i] = atoi(ptr2); } + memset(buff, 0, sizeof(buff)); } + fclose(fp); + return 0; } @@ -490,8 +548,10 @@ static void diff_stats(struct ifstat *old, struct ifstat *new, struct ifstat *diff) { int i; + if(old->irqs_len != new->irqs_len) return; /* Refetch stats and take old diff! */ + diff->rx_bytes = new->rx_bytes - old->rx_bytes; diff->rx_packets = new->rx_packets - old->rx_packets; diff->rx_drops = new->rx_drops - old->rx_drops; @@ -513,8 +573,11 @@ static void diff_stats(struct ifstat *old, struct ifstat *new, diff->forks = new->forks - old->forks; diff->procs_run = new->procs_run - old->procs_run; diff->procs_iow = new->procs_iow - old->procs_iow; + stats_check_alloc(diff); + diff->irq_nr = new->irq_nr; + for (i = 0; i < diff->irqs_len; ++i) { diff->irqs[i] = new->irqs[i] - old->irqs[i]; diff->irqs_srx[i] = new->irqs_srx[i] - old->irqs_srx[i]; @@ -622,6 +685,7 @@ static void print_update(const char *ifname, struct ifstat *s, struct ifstat *t, double interval) { int i; + printf("RX: %16.3f MiB/t %10lu Pkts/t %10lu Drops/t %10lu Errors/t\n", 1.f * s->rx_bytes / (1 << 20), s->rx_packets, s->rx_drops, s->rx_errors); @@ -684,12 +748,14 @@ static void print_update_csv_hdr(const char *ifname, struct ifstat *s, static inline int do_stats(const char *ifname, struct ifstat *s) { int ret = 0; + ret += rxtx_stats(ifname, s); ret += irq_stats(ifname, s); ret += irq_sstats(s); ret += sys_stats(s); ret += mem_stats(s); ret += wifi_stats(ifname, s); + return ret; } @@ -702,22 +768,30 @@ static int screen_loop(const char *ifname, uint32_t interval) memset(&old, 0, sizeof(old)); memset(&new, 0, sizeof(new)); memset(&curr, 0, sizeof(curr)); + screen_init(&screen); + while (!sigint) { if (getch() == 'q') goto out; + screen_update(screen, ifname, &curr, &new, &first, interval); + ret = do_stats(ifname, &old); if (ret != 0) goto out; + sleep(interval); + ret = do_stats(ifname, &new); if (ret != 0) goto out; + diff_stats(&old, &new, &curr); } out: screen_end(); + if (ret != 0) whine("Error fetching stats!\n"); if (old.irqs) @@ -726,6 +800,7 @@ out: xfree(new.irqs); if (curr.irqs) xfree(curr.irqs); + return 0; } @@ -741,16 +816,21 @@ static int print_loop(const char *ifname, uint32_t interval) ret = do_stats(ifname, &old); if (ret != 0) goto out; + sleep(interval); + ret = do_stats(ifname, &new); if (ret != 0) goto out; + diff_stats(&old, &new, &curr); + if (first && (mode & TERM_MODE_CSV_HDR) == TERM_MODE_CSV_HDR) { print_update_csv_hdr(ifname, &curr, &new, interval); first = 0; } + if ((mode & TERM_MODE_CSV) == TERM_MODE_CSV) print_update_csv(ifname, &curr, &new, interval); else if ((mode & TERM_MODE_NORMAL) == TERM_MODE_NORMAL) @@ -765,6 +845,7 @@ out: xfree(new.irqs); if (curr.irqs) xfree(curr.irqs); + return 0; } @@ -889,6 +970,7 @@ int main(int argc, char **argv) if (promisc) leave_promiscuous_mode(ifname, ifflags); xfree(ifname); + return ret; } diff --git a/src/proto_ipv4.h b/src/proto_ipv4.h index 4ac49f8e..e4c21535 100644 --- a/src/proto_ipv4.h +++ b/src/proto_ipv4.h @@ -14,12 +14,12 @@ #include /* for ntohs() */ #include /* for inet_ntop() */ #include -#include #include "csum.h" #include "proto_struct.h" #include "dissector_eth.h" #include "pkt_buff.h" +#include "built_in.h" struct ipv4hdr { #if defined(__LITTLE_ENDIAN_BITFIELD) @@ -91,7 +91,9 @@ static inline void ipv4(struct pkt_buff *pkt) /* TODO: do/print something more usefull (dissect options, ...) */ opts_len = ip->h_ihl * 4 - 20; - assert(opts_len <= 40); + /* XXX: better return and print the rest in hex than panic */ + bug_on(opts_len > 40); + opts = (uint8_t *) pkt_pull_head(pkt, opts_len); if (opts_len && opts) { diff --git a/src/ring_rx.c b/src/ring_rx.c index 12632cd3..9a8e6fa8 100644 --- a/src/ring_rx.c +++ b/src/ring_rx.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -49,9 +48,9 @@ void setup_rx_ring_layout(int sock, struct ring *ring, unsigned int size, ring->layout.tp_frame_size * ring->layout.tp_block_nr; - assert(ring->layout.tp_block_size >= ring->layout.tp_frame_size); - assert((ring->layout.tp_block_size % ring->layout.tp_frame_size) == 0); - assert((ring->layout.tp_block_size % getpagesize()) == 0); + bug_on(ring->layout.tp_block_size < ring->layout.tp_frame_size); + bug_on((ring->layout.tp_block_size % ring->layout.tp_frame_size) != 0); + bug_on((ring->layout.tp_block_size % getpagesize()) != 0); } void create_rx_ring(int sock, struct ring *ring) diff --git a/src/ring_tx.c b/src/ring_tx.c index 9830845d..dbd8c439 100644 --- a/src/ring_tx.c +++ b/src/ring_tx.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -59,9 +58,9 @@ void setup_tx_ring_layout(int sock, struct ring *ring, unsigned int size, ring->layout.tp_frame_size * ring->layout.tp_block_nr; - assert(ring->layout.tp_block_size >= ring->layout.tp_frame_size); - assert((ring->layout.tp_block_size % ring->layout.tp_frame_size) == 0); - assert((ring->layout.tp_block_size % getpagesize()) == 0); + bug_on(ring->layout.tp_block_size < ring->layout.tp_frame_size); + bug_on((ring->layout.tp_block_size % ring->layout.tp_frame_size) != 0); + bug_on((ring->layout.tp_block_size % getpagesize()) != 0); } void create_tx_ring(int sock, struct ring *ring) -- 2.11.4.GIT