mercredi 3 mai 2023

Reasonably unique string for a given machine

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?




Aucun commentaire:

Enregistrer un commentaire