Switch to an internal implementation of uuid generation, so we can use the randomness pool.

This commit is contained in:
Ian Gulliver
2016-02-22 16:27:44 -08:00
parent d03590439f
commit d6a629fa7e
14 changed files with 82 additions and 34 deletions

View File

@@ -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

View File

@@ -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();

View File

@@ -3,6 +3,7 @@
#include <string.h>
#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);

View File

@@ -9,10 +9,10 @@
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <uuid/uuid.h>
#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);
}

View File

@@ -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

View File

@@ -12,6 +12,8 @@
#include "common.h"
#include "wakeup.h"
#include "uuid.h"
#include "incoming.h"
struct incoming {

View File

@@ -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) {

View File

@@ -10,6 +10,8 @@
#include "common.h"
#include "wakeup.h"
#include "uuid.h"
#include "outgoing.h"
struct outgoing {

View File

@@ -1,5 +1,7 @@
#pragma once
#include <stddef.h>
void rand_init();
void rand_cleanup();
void rand_fill(void *, size_t);

View File

@@ -3,6 +3,7 @@
#include <string.h>
#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);
}

View File

@@ -10,6 +10,8 @@
#include "send.h"
#include "uuid.h"
#include "receive.h"
struct receive;

View File

@@ -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];

21
adsbus/uuid.c Normal file
View File

@@ -0,0 +1,21 @@
#include <stdint.h>
#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);
}

4
adsbus/uuid.h Normal file
View File

@@ -0,0 +1,4 @@
#pragma once
#define UUID_LEN 37
void uuid_gen(char *);