0

I am creating a C program. I mainly care about unix/linux systems but including windows here would be ideal. I want to find an approximately unique identifier for a networked machine. I am wondering if there is a better solution than finding a MAC address. The fact that MAC addresses can be duplicated is not necessarily a deal breaker. One might do something like this (yes, I know this is not portable; yes I know rand isn't really random, this just for demonstration):

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <time.h>   
#include <stdlib.h> 

int main()
{
  struct ifreq s;
  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  
  srand(time(NULL)); 
  strcpy(s.ifr_name, "wlp0s20f3");
  if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
    int i;
    for (i = 0; i < 6; ++i)
      printf("%x", (unsigned char) s.ifr_addr.sa_data[i]);
      printf("%d\n",rand() % 200);
      return 0;
  }
  return 1;
}

But is there some other mechanism that might be considered?

Are there any "gotchas" to dynamically creating header variables during make that are then compiled into the binary later in the make process?

Lucky
  • 627
  • 5
  • 15
  • 4
    Why not just a UUID that you save somewhere? – tadman May 04 '23 at 01:56
  • 4
    Is your goal to come up with a unique value that is the same each time the program is run? Or to make any unique value? If you don't need it to be the same, why not just make a totally random value? – Nick ODell May 04 '23 at 01:57
  • @NickODell I want it to be the same value forever. I suppose if I ran `rand` without `srand` that would do the trick. But that feels wrong to me. – Lucky May 04 '23 at 05:14
  • @tadman I suppose I could run something that generates a UUID on compile time during the make process. That is to say, I don't want it to be in a config file separate from the binary or whatnot. – Lucky May 04 '23 at 05:15
  • Baking it in to the executable seems unnecessary and liable to backfire, especially if it's dumped in a VM and copied, etc. Remember that on POSIX type systems (e.g. Linux) it's extremely normal to have some kind of "dotfile" in the user's root to preserve config like this. – tadman May 04 '23 at 05:18
  • 1
    IP should be unique but not necessarily persistent. MAC addresses are modifiable. If you want guaranteed unique then it's probably electing a leader and have it dole out ids (they usually rely on min or max of some info like mac or IP to break symmetry). It's tricky to implement but there are canned solutions. – Allan Wind May 04 '23 at 06:44
  • 1
    Does https://stackoverflow.com/questions/10152762/best-way-to-get-machine-id-on-linux answer your question? – KamilCuk May 04 '23 at 07:38
  • @KamilCuk that is certainly interesting but I can perceive a requirement for FreeBSD support and such. – Lucky May 04 '23 at 16:33
  • @AllanWind Very true. That may be overkill for my application. – Lucky May 04 '23 at 16:34
  • Consder asking a question `Best way to get machine id on FreeBSD?` and have a `#if FREEBSD use this #else use that #endif`. I doubt there will be a good POSIX solution. – KamilCuk May 04 '23 at 16:47
  • What you're talking about is called a hardware id. Google will describe different ways of getting hardware ids on different operating systems. On linux, for example, there is the special file `/etc/machine-id` This will be globally unique, but are considered secret, so you should hash hardware ids before sending them to other hosts – Carson May 04 '23 at 18:10
  • @Carson I'd prefer to stay away from `systemd`'s effluence if at all possible. Stockholm syndrome and all that. – Lucky May 05 '23 at 00:03
  • @Carson this is interesting: https://unix.stackexchange.com/a/403054/492628 -- A quick check on NetBSD and Ubuntu says that dbus-uuidgen exists on both of them -- What I wonder is, will "desktop bus" tend to exist on purely server installs as a rule? – Lucky May 05 '23 at 00:18

1 Answers1

0

well, you have uuid_* functions in the standard library that allow you to get unique identifiers. Don't reinvent the wheel.

Just man 3 uuid :)

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31