Skip to content

Commit

Permalink
Generate a random UUID on startup, store and read from bootid
Browse files Browse the repository at this point in the history
Fixes #1166
  • Loading branch information
Jalle19 committed Aug 7, 2024
1 parent 85946e8 commit 96c2190
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 93 deletions.
3 changes: 2 additions & 1 deletion src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 14 additions & 6 deletions src/minisatip.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "stream.h"
#include "utils/alloc.h"
#include "utils/ticks.h"
#include "utils/uuid.h"

#include <arpa/inet.h>
#include <errno.h>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2021,23 +2019,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;
}

Expand Down
7 changes: 0 additions & 7 deletions src/opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions src/opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#define _GNU_SOURCE

#include "utils/uuid.h"
#include <stdint.h>
#include <time.h>

Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
76 changes: 0 additions & 76 deletions src/socketworks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ifaddrs.h>
#include <net/if_dl.h>
#include <sys/socket.h>

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;
Expand Down
1 change: 0 additions & 1 deletion src/socketworks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions src/utils/uuid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "utils/uuid.h"
#include <stdlib.h>
#include <time.h>

// 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';
}
9 changes: 9 additions & 0 deletions src/utils/uuid.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 96c2190

Please sign in to comment.