Advertisement
teknoraver

str

Feb 9th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <net/if.h>
  4. #include <sys/ioctl.h>
  5.  
  6. #define PUB_TOPIC   "adbiot/changestatus/deviceupdate/matteo/notebook/12345678901234567890123456789012"
  7.  
  8. char *deviceupdate = PUB_TOPIC;
  9.  
  10. int main(int argc, char** argv)
  11. {
  12.  
  13.     int s = socket(PF_INET, SOCK_DGRAM, 0);
  14.     struct ifreq ifr = { .ifr_name = "eth0" };
  15.     ioctl(s, SIOCGIFHWADDR, &ifr);
  16.  
  17.     sprintf(strrchr(deviceupdate, '/') + 1, "%02x%02x%02x%02x%02x%02x",
  18.         (unsigned char)ifr.ifr_hwaddr.sa_data[0] & 0xff,
  19.         (unsigned char)ifr.ifr_hwaddr.sa_data[1] & 0xff,
  20.         (unsigned char)ifr.ifr_hwaddr.sa_data[2] & 0xff,
  21.         (unsigned char)ifr.ifr_hwaddr.sa_data[3] & 0xff,
  22.         (unsigned char)ifr.ifr_hwaddr.sa_data[4] & 0xff,
  23.         (unsigned char)ifr.ifr_hwaddr.sa_data[5] & 0xff
  24.     );
  25.  
  26.     printf("topic: %s\n", deviceupdate);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement