Skip to content

Commit

Permalink
Merge commit '6ad717c1b6732c62fde01911ec06f1a98295c6f4' into update-t…
Browse files Browse the repository at this point in the history
…optalk
  • Loading branch information
acooks committed May 16, 2019
2 parents 5abeb09 + 6ad717c commit 073222d
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 28 deletions.
9 changes: 3 additions & 6 deletions deps/toptalk/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ DEFINES += -DMAX_FLOW_COUNT=$(MAX_FLOW_COUNT)
DEFINES += -DINTERVAL_COUNT=$(INTERVAL_COUNT)
endif

LDLIBS := -lpcap -lcurses -lrt -lpthread
LFLAGS = -lpcap -lcurses -lrt -lpthread

CFLAGS_HARDENED = \
-fPIC \
Expand All @@ -41,7 +41,7 @@ all: $(LIB) $(TEST) $(PROG)

$(PROG): $(LIB) $(SRC) $(HEADERS) Makefile main.c
@echo Building $(PROG)
$(CC) -o $(PROG) main.c timeywimey.c $(LIB) $(LDLIBS) $(LDFLAGS) $(CFLAGS)
$(CC) -o $(PROG) main.c timeywimey.c $(LIB) $(LFLAGS) $(CFLAGS)
@echo -e "$(PROG) OK\n"

$(LIB): $(SRC) $(HEADERS) Makefile
Expand All @@ -52,21 +52,18 @@ $(LIB): $(SRC) $(HEADERS) Makefile

$(TEST): $(LIB) test.c
@echo Building $(TEST)
$(CC) -o $(TEST) test.c timeywimey.c $(LIB) $(LDLIBS) $(LDFLAGS) $(CFLAGS)
$(CC) -o $(TEST) test.c timeywimey.c $(LIB) $(LFLAGS) $(CFLAGS)

.PHONY: test
test: $(TEST)
@echo "Test needs sudo for promiscuous network access..."
@sudo ./$(TEST) && echo -e "Test OK\n"

.PHONY: cppcheck
cppcheck:
cppcheck --enable=warning,performance,portability --force .

.PHONY: clang-analyze
clang-analyze: clean
scan-build -v make

.PHONY: clean
clean:
rm $(LIB) $(PROG) $(TEST) *.o *.a || true
33 changes: 32 additions & 1 deletion deps/toptalk/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <time.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <pcap/sll.h>

Expand Down Expand Up @@ -84,13 +85,26 @@ int decode_ip6(const uint8_t *packet, struct flow_pkt *pkt, char *errstr)
int ret;
const void *next = (uint8_t *)packet + sizeof(struct hdr_ipv6);
const struct hdr_ipv6 *ip6_packet = (const struct hdr_ipv6 *)packet;
uint8_t next_hdr, hdr_len;

pkt->flow_rec.flow.ethertype = ETHERTYPE_IPV6;
pkt->flow_rec.flow.src_ip6 = (ip6_packet->ip6_src);
pkt->flow_rec.flow.dst_ip6 = (ip6_packet->ip6_dst);
pkt->flow_rec.flow.tclass = (htonl(ip6_packet->vcf) & 0x0fc00000) >> 20;

next_hdr = ip6_packet->next_hdr;

/* Optional headers */
switch (next_hdr) {
case IPPROTO_DSTOPTS: /* IPv6 Destination Options */
hdr_len = *((uint8_t*)next + 1);
next = (uint8_t*)next + hdr_len;
next_hdr = *((uint8_t*)next);
break;
}

/* Transport proto TCP/UDP/ICMP */
switch (ip6_packet->next_hdr) {
switch (next_hdr) {
case IPPROTO_TCP:
ret = decode_tcp(next, pkt, errstr);
break;
Expand All @@ -106,6 +120,9 @@ int decode_ip6(const uint8_t *packet, struct flow_pkt *pkt, char *errstr)
case IPPROTO_ICMPV6:
ret = decode_icmp6(next, pkt, errstr);
break;
case IPPROTO_ESP:
ret = decode_esp(next, pkt, errstr);
break;
default:
snprintf(errstr, DECODE_ERRBUF_SIZE,
"*** Protocol [0x%02x] unknown", ip6_packet->next_hdr);
Expand All @@ -131,6 +148,7 @@ int decode_ip4(const uint8_t *packet, struct flow_pkt *pkt, char *errstr)
pkt->flow_rec.flow.ethertype = ETHERTYPE_IP;
pkt->flow_rec.flow.src_ip = (ip4_packet->ip_src);
pkt->flow_rec.flow.dst_ip = (ip4_packet->ip_dst);
pkt->flow_rec.flow.tclass = IPTOS_DSCP(ip4_packet->ip_tos);

/* IP proto TCP/UDP/ICMP */
switch (ip4_packet->ip_p) {
Expand All @@ -146,6 +164,9 @@ int decode_ip4(const uint8_t *packet, struct flow_pkt *pkt, char *errstr)
case IPPROTO_IGMP:
ret = decode_igmp(next, pkt, errstr);
break;
case IPPROTO_ESP:
ret = decode_esp(next, pkt, errstr);
break;
default:
snprintf(errstr, DECODE_ERRBUF_SIZE,
"*** Protocol [0x%02x] unknown", ip4_packet->ip_p);
Expand Down Expand Up @@ -223,3 +244,13 @@ int decode_icmp6(const struct hdr_icmp *packet, struct flow_pkt *pkt,
pkt->flow_rec.flow.dport = 0;
return 0;
}

int decode_esp(const struct hdr_esp *packet, struct flow_pkt *pkt, char *errstr)
{
(void)errstr;
(void)packet;
pkt->flow_rec.flow.proto = IPPROTO_ESP;
pkt->flow_rec.flow.sport = 0;
pkt->flow_rec.flow.dport = 0;
return 0;
}
10 changes: 9 additions & 1 deletion deps/toptalk/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct hdr_ipv4 {
int decode_ip4(const uint8_t *packet, struct flow_pkt *pkt, char *errstr);

struct hdr_ipv6 {
uint32_t version : 4, class : 8, flowlabel : 20;
uint32_t vcf;
uint16_t payload_len;
uint8_t next_hdr; /* like protocol in ipv4 */
uint8_t hop_limit;
Expand Down Expand Up @@ -103,6 +103,12 @@ struct hdr_icmp {
uint32_t hdr_data; /* purpose depends on type field */
} __attribute__((__packed__));


struct hdr_esp {
uint32_t spi;
uint32_t seq;
} __attribute__((__packed__));

int decode_icmp(const struct hdr_icmp *packet, struct flow_pkt *pkt,
char *errstr);

Expand All @@ -112,4 +118,6 @@ int decode_igmp(const struct hdr_icmp *packet, struct flow_pkt *pkt,
int decode_icmp6(const struct hdr_icmp *packet, struct flow_pkt *pkt,
char *errstr);

int decode_esp(const struct hdr_esp *packet, struct flow_pkt *pkt,
char *errstr);
#endif
1 change: 1 addition & 0 deletions deps/toptalk/flow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct flow {
uint16_t sport;
uint16_t dport;
uint16_t proto;
uint8_t tclass;
};

struct flow_record {
Expand Down
35 changes: 23 additions & 12 deletions deps/toptalk/intervals.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,20 +446,34 @@ static int init_pcap(char **dev, struct pcap_info *pi)
{
char errbuf[PCAP_ERRBUF_SIZE];
int dlt; /* pcap data link type */
pcap_if_t *alldevs;

if (*dev == NULL) {
*dev = pcap_lookupdev(errbuf);
if (!*dev) {
int err = pcap_findalldevs(&alldevs, errbuf);
if (err) {
fprintf(stderr, "Couldn't list devices: %s\n", errbuf);
return 1;
}

if (!alldevs) {
fprintf(stderr,
"No devices available. Check permissions.\n");
return 1;
}

*dev = strdup(alldevs->name);
pcap_freealldevs(alldevs);
}

if (*dev == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return (2);
return 1;
}

pi->handle = pcap_open_live(*dev, BUFSIZ, 1, 0, errbuf);
if (pi->handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", *dev, errbuf);
return (2);
fprintf(stderr, "Couldn't open device %s\n", errbuf);
return 1;
}

dlt = pcap_datalink(pi->handle);
Expand All @@ -474,18 +488,18 @@ static int init_pcap(char **dev, struct pcap_info *pi)
fprintf(stderr, "Device %s doesn't provide Ethernet headers - "
"not supported\n",
*dev);
return (2);
return 1;
}

if (pcap_setnonblock(pi->handle, 1, errbuf) != 0) {
fprintf(stderr, "Non-blocking mode failed: %s\n", errbuf);
return (2);
return 1;
}

pi->selectable_fd = pcap_get_selectable_fd(pi->handle);
if (-1 == pi->selectable_fd) {
fprintf(stderr, "pcap handle not selectable.\n");
return (2);
return 1;
}
return 0;
}
Expand Down Expand Up @@ -629,11 +643,8 @@ int tt_intervals_init(struct tt_thread_info *ti)
if (!ti->priv) { goto cleanup1; }

err = init_pcap(&(ti->dev), &(ti->priv->pi));
if (err) {
errno = err;
perror("init_pcap failed");
if (err)
goto cleanup;
}

return 0;

Expand Down
48 changes: 40 additions & 8 deletions deps/toptalk/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <arpa/inet.h>
#include <ncurses.h>
#include <pthread.h>
#include <netinet/ip.h>
#include <errno.h>

#include "flow.h"
Expand All @@ -14,7 +15,8 @@ static char const *const protos[IPPROTO_MAX] = {[IPPROTO_TCP] = "TCP",
[IPPROTO_ICMP] = "ICMP",
[IPPROTO_ICMPV6] = "ICMP6",
[IPPROTO_IP] = "IP",
[IPPROTO_IGMP] = "IGMP" };
[IPPROTO_IGMP] = "IGMP",
[IPPROTO_ESP] = "ESP" };

enum speeds { BPS, KBPS, MBPS, GBPS };
enum intervals { MILLISECONDS, SECONDS };
Expand All @@ -31,12 +33,36 @@ static char * const intervalunits[] = {
[SECONDS] = "s "
};

static char const * const dscpvalues[] = {
[IPTOS_DSCP_AF11] = "AF11",
[IPTOS_DSCP_AF12] = "AF12",
[IPTOS_DSCP_AF13] = "AF13",
[IPTOS_DSCP_AF21] = "AF21",
[IPTOS_DSCP_AF22] = "AF22",
[IPTOS_DSCP_AF23] = "AF23",
[IPTOS_DSCP_AF31] = "AF31",
[IPTOS_DSCP_AF32] = "AF32",
[IPTOS_DSCP_AF33] = "AF33",
[IPTOS_DSCP_AF41] = "AF41",
[IPTOS_DSCP_AF42] = "AF42",
[IPTOS_DSCP_AF43] = "AF43",
[IPTOS_DSCP_EF] = "EF",
[IPTOS_CLASS_CS0] = "CS0",
[IPTOS_CLASS_CS1] = "CS1",
[IPTOS_CLASS_CS2] = "CS2",
[IPTOS_CLASS_CS3] = "CS3",
[IPTOS_CLASS_CS4] = "CS4",
[IPTOS_CLASS_CS5] = "CS5",
[IPTOS_CLASS_CS6] = "CS6",
[IPTOS_CLASS_CS7] = "CS7"
};


#define ERR_LINE_OFFSET 2
#define DEBUG_LINE_OFFSET 3
#define TOP_N_LINE_OFFSET 5
#define TP1_COL 47
#define TP2_COL 59
#define TP1_COL 53
#define TP2_COL 64

/* two displayed intervals */
int interval1 = 4, interval2 = 3;
Expand Down Expand Up @@ -86,13 +112,16 @@ int print_hdrs(int tp1, struct timeval interval1, int tp2,
byteunit = byteunits[unit];

attron(A_BOLD);
mvprintw(TOP_N_LINE_OFFSET, 1, "%51s", "Source|SPort|Proto");
mvprintw(TOP_N_LINE_OFFSET + 1, 1, "%46s", "Destination|DPort|");
mvprintw(TOP_N_LINE_OFFSET, 1, "%52s", "Source|SPort|Proto|");
mvprintw(TOP_N_LINE_OFFSET + 1, 1, "%52s", "Destination|DPort|DSCP |");
mvaddch(TOP_N_LINE_OFFSET + 0, 40, ACS_VLINE);
mvaddch(TOP_N_LINE_OFFSET + 1, 40, ACS_VLINE);

mvaddch(TOP_N_LINE_OFFSET + 0, 46, ACS_VLINE);
mvaddch(TOP_N_LINE_OFFSET + 1, 46, ACS_VLINE);
mvaddch(TOP_N_LINE_OFFSET + 0, 52, ACS_VLINE);
mvaddch(TOP_N_LINE_OFFSET + 1, 52, ACS_VLINE);



if (dt1 > 1) {
Expand All @@ -106,7 +135,7 @@ int print_hdrs(int tp1, struct timeval interval1, int tp2,
byteunit, dt1 * 1E3, intervalunits[MILLISECONDS],
byteunit, dt2 * 1E3, intervalunits[MILLISECONDS]);
}
mvaddch(TOP_N_LINE_OFFSET + 1, 59, ACS_VLINE);
mvaddch(TOP_N_LINE_OFFSET + 1, 65, ACS_VLINE);

attroff(A_BOLD);
return div;
Expand All @@ -127,7 +156,9 @@ void print_flow(int row, char *src, char *dst, struct flow_record *fte1,
fte1->flow.dport);
mvprintw(TOP_N_LINE_OFFSET + row + 0, 47, "%s",
protos[fte1->flow.proto]);
mvprintw(TOP_N_LINE_OFFSET + row + 1, 47, "%10d %10d",
mvprintw(TOP_N_LINE_OFFSET + row + 1, 47, "%03s",
dscpvalues[fte1->flow.tclass]);
mvprintw(TOP_N_LINE_OFFSET + row + 1, 55, "%10d %10d",
fte1->bytes / div, fte2->bytes / div);
mvprintw(TOP_N_LINE_OFFSET + row + 2, 0, "%80s", " ");
}
Expand Down Expand Up @@ -292,7 +323,8 @@ void init_thread(struct tt_thread_info *ti)

err = tt_intervals_init(ti);
if (err) {
handle_error_en(err, "tt intervals init");
/* pcap doesn't return a proper errno, but prints its own msg */
exit(EXIT_FAILURE);
}

err = pthread_attr_init(&ti->attr);
Expand Down

0 comments on commit 073222d

Please sign in to comment.