From 65b104a06031e3202a39a1e3a11476cfa9ea7f69 Mon Sep 17 00:00:00 2001 From: Sam Stenvall Date: Mon, 5 Aug 2024 14:25:55 +0300 Subject: [PATCH] Generate a random UUID on startup, store and read from bootid Fixes #1166 --- src/Makefile.in | 3 +- src/minisatip.c | 20 +++++++++---- src/opts.c | 7 ----- src/opts.h | 4 +-- src/socketworks.c | 76 ----------------------------------------------- src/socketworks.h | 1 - src/utils/uuid.c | 20 +++++++++++++ src/utils/uuid.h | 9 ++++++ 8 files changed, 47 insertions(+), 93 deletions(-) create mode 100644 src/utils/uuid.c create mode 100644 src/utils/uuid.h diff --git a/src/Makefile.in b/src/Makefile.in index d0ec12253d..6e30936961 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -49,10 +49,11 @@ SOURCES=\ api/variables.c \ utils/logging/logging.c \ utils/alloc.c \ + utils/fifo.c \ utils/hash_table.c \ utils/mutex.c \ utils/ticks.c \ - utils/fifo.c + utils/uuid.c TABLES=0 PMT=0 diff --git a/src/minisatip.c b/src/minisatip.c index c68b6bf016..f0c26e6694 100644 --- a/src/minisatip.c +++ b/src/minisatip.c @@ -28,6 +28,7 @@ #include "stream.h" #include "utils/alloc.h" #include "utils/ticks.h" +#include "utils/uuid.h" #include #include @@ -1186,9 +1187,6 @@ void set_options(int argc, char *argv[]) { // FBC setup if (!access("/proc/stb/frontend/0/fbc_connect", W_OK)) set_slave_adapters("2-7:0"); - - // Generate the static SAT>IP UUID - generate_uuid(&opts); } #define RBUF 32000 @@ -2018,23 +2016,33 @@ int readBootID() { opts.bootid = 0; opts.device_id = 0; char bootid_path[256]; + + // Read existing values snprintf(bootid_path, sizeof(bootid_path) - 1, "%s/bootid", opts.cache_dir); FILE *f = fopen(bootid_path, "rt"); __attribute__((unused)) int rv; if (f) { - rv = fscanf(f, "%d %d", &opts.bootid, &opts.device_id); + rv = fscanf(f, "%d %d %s", &opts.bootid, &opts.device_id, opts.uuid); fclose(f); } + + // Increment bootid and set defaults if values are missing opts.bootid++; if (opts.device_id < 1) { opts.device_id = 1; } + if (!strcmp(opts.uuid, "")) { + uuid4_generate(opts.uuid); + } + + // Store new values f = fopen(bootid_path, "wt"); if (f) { - fprintf(f, "%d %d", opts.bootid, opts.device_id); + fprintf(f, "%d %d %s", opts.bootid, opts.device_id, opts.uuid); fclose(f); } - LOG("Running with bootid %d, device_id %d", opts.bootid, opts.device_id); + LOG("Running with bootid %d, device_id %d, UUID %s", opts.bootid, + opts.device_id, opts.uuid); return opts.bootid; } diff --git a/src/opts.c b/src/opts.c index 86d8587238..96919162ba 100644 --- a/src/opts.c +++ b/src/opts.c @@ -62,10 +62,3 @@ void parse_dvbapi_opt(char *optarg, struct_opts_t *optz) { LOG("Not filtering out encrypted packets from pids=all streams"); } } - -void generate_uuid(struct_opts_t *optz) { - char uuid1[] = "11223344-9999-0000-b7ae"; - char mac[15] = "00000000000000"; - get_mac_address(mac); - sprintf(optz->uuid, "%s-%s", uuid1, mac); -} diff --git a/src/opts.h b/src/opts.h index cf67d2a8d5..ac1d239581 100644 --- a/src/opts.h +++ b/src/opts.h @@ -3,6 +3,7 @@ #define _GNU_SOURCE +#include "utils/uuid.h" #include #include @@ -24,7 +25,7 @@ typedef struct struct_opts { int run_pid; char *disc_host; // discover host char mac[13]; - char uuid[50]; + char uuid[UUID_STR_LEN]; unsigned int log, debug, slog, start_rtp, http_port; int timeout_sec; int force_sadapter, force_tadapter, force_cadapter; @@ -92,7 +93,6 @@ typedef struct struct_opts { } struct_opts_t; void parse_dvbapi_opt(char *optarg, struct_opts_t *optz); -void generate_uuid(struct_opts_t *optz); extern struct_opts_t opts; diff --git a/src/socketworks.c b/src/socketworks.c index 6ffb0a5e54..bf17fa6569 100644 --- a/src/socketworks.c +++ b/src/socketworks.c @@ -985,82 +985,6 @@ void set_sockets_rtime(int i, int r) { if (ss) ss->rtime = r; } -#ifdef __linux__ - -int get_mac_address(char *mac) { - struct ifreq ifr; - struct ifconf ifc; - char buf[1024]; - - if (opts.mac[0]) { - // simulate mac address - _strncpy(mac, opts.mac, 13); - return 0; - } - int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); - - if (sock == -1) - return 0; - - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = buf; - if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) { - }; - - struct ifreq *it = ifc.ifc_req; - const struct ifreq *const end = it + (ifc.ifc_len / sizeof(struct ifreq)); - - for (; it != end; ++it) { - strcpy(ifr.ifr_name, it->ifr_name); - if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) { - if (!(ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback - if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) - break; - } - } - } - unsigned char m[6]; - - memcpy(m, ifr.ifr_hwaddr.sa_data, 6); - sprintf(mac, "%02x%02x%02x%02x%02x%02x", m[0], m[1], m[2], m[3], m[4], - m[5]); - close(sock); - return 1; -} - -#else -#include -#include -#include - -int get_mac_address(char *mac) { - - if (opts.mac[0]) { - // simulate mac address - _strncpy(mac, opts.mac, 13); - return 0; - } - memset(mac, 0, 3 * 6); - const char *if_name = "en0"; - struct ifaddrs *iflist, *cur; - if (getifaddrs(&iflist) == 0) { - for (cur = iflist; cur; cur = cur->ifa_next) { - if ((cur->ifa_addr->sa_family == AF_LINK) && cur->ifa_addr) { - struct sockaddr_dl *sdl = (struct sockaddr_dl *)cur->ifa_addr; - unsigned char *m = (unsigned char *)LLADDR(sdl); - sprintf(mac, "%02x%02x%02x%02x%02x%02x", m[0], m[1], m[2], m[3], - m[4], m[5]); - LOGM("mac -> %s, interface %s", mac, cur->ifa_name); - if ((strcmp(cur->ifa_name, if_name) == 0)) - break; - } - } - - freeifaddrs(iflist); - } - return 0; -} -#endif int sockets_del_for_sid(int sid) { int i; diff --git a/src/socketworks.h b/src/socketworks.h index 8db6c318f7..6bdac5fea9 100644 --- a/src/socketworks.h +++ b/src/socketworks.h @@ -94,7 +94,6 @@ int sockets_add(int sock, USockAddr *sa, int sid, int type, socket_action a, int sockets_del(int sock); int no_action(int s); void *select_and_execute(void *arg); -int get_mac_address(char *mac); int fill_sockaddr(USockAddr *serv, char *host, int port, int ipv4_only); int sockets_del_for_sid(int sid); void set_socket_buffer(int sid, unsigned char *buf, int len); diff --git a/src/utils/uuid.c b/src/utils/uuid.c new file mode 100644 index 0000000000..b1a5e5d9a0 --- /dev/null +++ b/src/utils/uuid.c @@ -0,0 +1,20 @@ +#include "utils/uuid.h" +#include +#include + +// https://stackoverflow.com/a/71826534 +void uuid4_generate(char *uuid) { + srand(time(NULL)); + + char v[] = {'0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + for (int i = 0; i < 36; ++i) { + uuid[i] = v[rand() % 16]; + } + + uuid[8] = '-'; + uuid[13] = '-'; + uuid[18] = '-'; + uuid[23] = '-'; + uuid[36] = '\0'; +} diff --git a/src/utils/uuid.h b/src/utils/uuid.h new file mode 100644 index 0000000000..027990539f --- /dev/null +++ b/src/utils/uuid.h @@ -0,0 +1,9 @@ +#ifndef UUID_H +#define UUID_H +#define _GNU_SOURCE + +#define UUID_STR_LEN 37 + +void uuid4_generate(char *uuid); + +#endif // UUID_H