From d6a629fa7eaad5c4e626d635c02ededa2c28e155 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 22 Feb 2016 16:27:44 -0800 Subject: [PATCH] Switch to an internal implementation of uuid generation, so we can use the randomness pool. --- adsbus/Makefile | 4 ++-- adsbus/adsbus.c | 3 +-- adsbus/airspy_adsb.c | 11 ++++++----- adsbus/common.c | 32 +++++++++++++++++++++----------- adsbus/common.h | 12 ++++-------- adsbus/incoming.c | 2 ++ adsbus/json.c | 10 ++++++++-- adsbus/outgoing.c | 2 ++ adsbus/rand.h | 2 ++ adsbus/raw.c | 5 +++-- adsbus/receive.c | 2 ++ adsbus/send.c | 6 ++++-- adsbus/uuid.c | 21 +++++++++++++++++++++ adsbus/uuid.h | 4 ++++ 14 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 adsbus/uuid.c create mode 100644 adsbus/uuid.h diff --git a/adsbus/Makefile b/adsbus/Makefile index 75cdaaa..c4f0968 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -1,11 +1,11 @@ CC ?= clang CFLAGS ?= -Wall -Werror -O4 -g --std=gnu11 --pedantic-errors -fPIE -pie -fstack-protector-strong LDFLAGS ?= $(CFLAGS) -Wl,-z,relro -Wl,-z,now -LIBS ?= -luuid -ljansson +LIBS ?= -ljansson OBJ_NETWORK = receive.o send.o incoming.o outgoing.o OBJ_PROTOCOL = airspy_adsb.o beast.o json.o raw.o stats.o -OBJ_UTIL = rand.o wakeup.o opts.o common.o +OBJ_UTIL = rand.o uuid.o wakeup.o opts.o common.o all: adsbus diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index 838a869..de03452 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -100,10 +100,9 @@ int main(int argc, char *argv[]) { assert(!close(0)); hex_init(); - - peer_init(); rand_init(); wakeup_init(); + peer_init(); send_init(); diff --git a/adsbus/airspy_adsb.c b/adsbus/airspy_adsb.c index 53efdeb..a82ae18 100644 --- a/adsbus/airspy_adsb.c +++ b/adsbus/airspy_adsb.c @@ -3,6 +3,7 @@ #include #include "common.h" +#include "uuid.h" #include "receive.h" #include "airspy_adsb.h" @@ -89,12 +90,12 @@ static bool airspy_adsb_parse_mode_s_long(struct buf *buf, struct packet *packet static void airspy_adsb_fill_common(struct packet *packet, struct airspy_adsb_common_overlay *overlay) { overlay->semicolon1 = overlay->semicolon2 = overlay->semicolon3 = ';'; - hex_from_int( + hex_from_int_upper( overlay->mlat_timestamp, mlat_timestamp_scale_out(packet->mlat_timestamp, UINT32_MAX, SEND_MHZ), sizeof(overlay->mlat_timestamp) / 2); - hex_from_int(overlay->mlat_precision, SEND_MHZ / 2, sizeof(overlay->mlat_precision) / 2); - hex_from_int(overlay->rssi, rssi_scale_out(packet->rssi, UINT16_MAX), sizeof(overlay->rssi) / 2); + hex_from_int_upper(overlay->mlat_precision, SEND_MHZ / 2, sizeof(overlay->mlat_precision) / 2); + hex_from_int_upper(overlay->rssi, rssi_scale_out(packet->rssi, UINT16_MAX), sizeof(overlay->rssi) / 2); } static void airspy_adsb_serialize_mode_s_short(struct packet *packet, struct buf *buf) { @@ -103,7 +104,7 @@ static void airspy_adsb_serialize_mode_s_short(struct packet *packet, struct buf overlay->semicolon = ';'; overlay->cr = '\r'; overlay->lf = '\n'; - hex_from_bin(overlay->payload, packet->payload, sizeof(overlay->payload) / 2); + hex_from_bin_upper(overlay->payload, packet->payload, sizeof(overlay->payload) / 2); airspy_adsb_fill_common(packet, &overlay->common); @@ -116,7 +117,7 @@ static void airspy_adsb_serialize_mode_s_long(struct packet *packet, struct buf overlay->semicolon = ';'; overlay->cr = '\r'; overlay->lf = '\n'; - hex_from_bin(overlay->payload, packet->payload, sizeof(overlay->payload) / 2); + hex_from_bin_upper(overlay->payload, packet->payload, sizeof(overlay->payload) / 2); airspy_adsb_fill_common(packet, &overlay->common); diff --git a/adsbus/common.c b/adsbus/common.c index 71b6846..5449374 100644 --- a/adsbus/common.c +++ b/adsbus/common.c @@ -9,10 +9,10 @@ #include #include #include -#include #include "common.h" #include "rand.h" +#include "uuid.h" #include "wakeup.h" static char server_id[UUID_LEN]; @@ -166,7 +166,8 @@ uint32_t rssi_scale_out(uint32_t value, uint32_t max) { static uint8_t hex_table[256] = {0}; -static char hex_char_table[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', }; +static char hex_upper_table[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', }; +static char hex_lower_table[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; void hex_init() { for (int i = '0'; i <= '9'; i++) { @@ -198,26 +199,35 @@ uint64_t hex_to_int(const char *in, size_t bytes) { return ret; } -void hex_from_bin(char *out, const uint8_t *in, size_t bytes) { +static void hex_from_bin(char *out, const uint8_t *in, size_t bytes, char table[]) { for (size_t i = 0, j = 0; i < bytes; i++, j += 2) { - out[j] = hex_char_table[in[i] >> 4]; - out[j + 1] = hex_char_table[in[i] & 0xf]; + out[j] = table[in[i] >> 4]; + out[j + 1] = table[in[i] & 0xf]; } } -void hex_from_int(char *out, uint64_t in, size_t bytes) { +static void hex_from_int(char *out, uint64_t in, size_t bytes, char table[]) { bytes *= 2; for (int o = bytes - 1; o >= 0; o--) { - out[o] = hex_char_table[in & 0xf]; + out[o] = table[in & 0xf]; in >>= 4; } } +void hex_from_bin_upper(char *out, const uint8_t *in, size_t bytes) { + hex_from_bin(out, in, bytes, hex_upper_table); +} -void uuid_gen(char *out) { - uuid_t uuid; - uuid_generate(uuid); - uuid_unparse(uuid, out); +void hex_from_bin_lower(char *out, const uint8_t *in, size_t bytes) { + hex_from_bin(out, in, bytes, hex_lower_table); +} + +void hex_from_int_upper(char *out, uint64_t in, size_t bytes) { + hex_from_int(out, in, bytes, hex_upper_table); +} + +void hex_from_int_lower(char *out, uint64_t in, size_t bytes) { + hex_from_int(out, in, bytes, hex_lower_table); } diff --git a/adsbus/common.h b/adsbus/common.h index 7427d82..a0d0ea9 100644 --- a/adsbus/common.h +++ b/adsbus/common.h @@ -83,14 +83,10 @@ uint32_t rssi_scale_out(uint32_t, uint32_t); void hex_init(); void hex_to_bin(uint8_t *, const char *, size_t); uint64_t hex_to_int(const char *, size_t); -void hex_from_bin(char *, const uint8_t *, size_t); -void hex_from_int(char *, uint64_t, size_t); - - -///////// uuid - -#define UUID_LEN 37 -void uuid_gen(char *); +void hex_from_bin_upper(char *, const uint8_t *, size_t); +void hex_from_bin_lower(char *, const uint8_t *, size_t); +void hex_from_int_upper(char *, uint64_t, size_t); +void hex_from_int_lower(char *, uint64_t, size_t); ///////// retry timing diff --git a/adsbus/incoming.c b/adsbus/incoming.c index 70e08fa..1d17681 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -12,6 +12,8 @@ #include "common.h" #include "wakeup.h" +#include "uuid.h" + #include "incoming.h" struct incoming { diff --git a/adsbus/json.c b/adsbus/json.c index 9758bd5..7595f30 100644 --- a/adsbus/json.c +++ b/adsbus/json.c @@ -5,6 +5,8 @@ #include "receive.h" #include "send.h" +#include "rand.h" +#include "uuid.h" #include "json.h" static void json_serialize_to_buf(json_t *obj, struct buf *buf) { @@ -34,7 +36,7 @@ static void json_add_common(struct packet *packet, json_t *obj) { static void json_serialize_mode_s_short(struct packet *packet, struct buf *buf) { assert(packet->mlat_timestamp < MLAT_MAX); char hexbuf[14]; - hex_from_bin(hexbuf, packet->payload, 7); + hex_from_bin_upper(hexbuf, packet->payload, 7); json_t *out = json_pack("{ss#}", "payload", hexbuf, 14); json_add_common(packet, out); json_serialize_to_buf(out, buf); @@ -43,7 +45,7 @@ static void json_serialize_mode_s_short(struct packet *packet, struct buf *buf) static void json_serialize_mode_s_long(struct packet *packet, struct buf *buf) { assert(packet->mlat_timestamp < MLAT_MAX); char hexbuf[28]; - hex_from_bin(hexbuf, packet->payload, 14); + hex_from_bin_upper(hexbuf, packet->payload, 14); json_t *out = json_pack("{ss#}", "payload", hexbuf, 28); json_add_common(packet, out); json_serialize_to_buf(out, buf); @@ -51,6 +53,10 @@ static void json_serialize_mode_s_long(struct packet *packet, struct buf *buf) { void json_init() { assert(JSON_INTEGER_IS_LONG_LONG); + + size_t seed; + rand_fill(&seed, sizeof(seed)); + json_object_seed(seed); } void json_serialize(struct packet *packet, struct buf *buf) { diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index 2a87665..7b5aa82 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -10,6 +10,8 @@ #include "common.h" #include "wakeup.h" +#include "uuid.h" + #include "outgoing.h" struct outgoing { diff --git a/adsbus/rand.h b/adsbus/rand.h index 88d92e0..dbdc349 100644 --- a/adsbus/rand.h +++ b/adsbus/rand.h @@ -1,5 +1,7 @@ #pragma once +#include + void rand_init(); void rand_cleanup(); void rand_fill(void *, size_t); diff --git a/adsbus/raw.c b/adsbus/raw.c index 7c894e7..a0ed0d3 100644 --- a/adsbus/raw.c +++ b/adsbus/raw.c @@ -3,6 +3,7 @@ #include #include "common.h" +#include "uuid.h" #include "raw.h" struct __attribute__((packed)) raw_mode_s_short_overlay { @@ -56,7 +57,7 @@ static void raw_serialize_mode_s_short(struct packet *packet, struct buf *buf) { overlay->asterisk = '*'; overlay->semicolon = ';'; overlay->lf = '\n'; - hex_from_bin(overlay->payload, packet->payload, sizeof(overlay->payload) / 2); + hex_from_bin_upper(overlay->payload, packet->payload, sizeof(overlay->payload) / 2); buf->length = sizeof(*overlay); } @@ -65,7 +66,7 @@ static void raw_serialize_mode_s_long(struct packet *packet, struct buf *buf) { overlay->asterisk = '*'; overlay->semicolon = ';'; overlay->lf = '\n'; - hex_from_bin(overlay->payload, packet->payload, sizeof(overlay->payload) / 2); + hex_from_bin_upper(overlay->payload, packet->payload, sizeof(overlay->payload) / 2); buf->length = sizeof(*overlay); } diff --git a/adsbus/receive.c b/adsbus/receive.c index 4414042..3ccbb0c 100644 --- a/adsbus/receive.c +++ b/adsbus/receive.c @@ -10,6 +10,8 @@ #include "send.h" +#include "uuid.h" + #include "receive.h" struct receive; diff --git a/adsbus/send.c b/adsbus/send.c index 424dce8..08163be 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -8,14 +8,16 @@ #include "common.h" -#include "send.h" - #include "airspy_adsb.h" #include "beast.h" #include "json.h" #include "raw.h" #include "stats.h" +#include "uuid.h" + +#include "send.h" + struct send { struct peer peer; char id[UUID_LEN]; diff --git a/adsbus/uuid.c b/adsbus/uuid.c new file mode 100644 index 0000000..505c292 --- /dev/null +++ b/adsbus/uuid.c @@ -0,0 +1,21 @@ +#include + +#include "rand.h" +#include "common.h" + +#include "uuid.h" + +void uuid_gen(char *out) { + uint8_t uuid[16]; + rand_fill(uuid, 16); + uuid[6] = (uuid[6] & 0x0F) | 0x40; + uuid[8] = (uuid[8] & 0x3F) | 0x80; + + out[8] = out[13] = out[18] = out[23] = '-'; + out[36] = '\0'; + hex_from_bin_lower(&out[0], &uuid[0], 4); + hex_from_bin_lower(&out[9], &uuid[4], 2); + hex_from_bin_lower(&out[14], &uuid[6], 2); + hex_from_bin_lower(&out[19], &uuid[8], 2); + hex_from_bin_lower(&out[24], &uuid[10], 6); +} diff --git a/adsbus/uuid.h b/adsbus/uuid.h new file mode 100644 index 0000000..d59032c --- /dev/null +++ b/adsbus/uuid.h @@ -0,0 +1,4 @@ +#pragma once + +#define UUID_LEN 37 +void uuid_gen(char *);