snowid_util.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. *
  3. * MIT License
  4. *
  5. * Copyright (c) 2022 beyonddream
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include <ifaddrs.h>
  23. #include <ifaddrs.h>
  24. #include <limits.h>
  25. #include <netdb.h>
  26. #include <stddef.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <sys/time.h>
  31. #include <sys/socket.h>
  32. #include <sys/types.h>
  33. #include "snowid_util.h"
  34. #ifdef __linux__
  35. #include <linux/if_link.h>
  36. #include <linux/if_packet.h>
  37. #define IF_HW_FAMILY AF_PACKET
  38. #else
  39. #include <net/if_dl.h>
  40. #define IF_HW_FAMILY AF_LINK /* MacOS/BSD */
  41. #endif
  42. bool get_hw_addr_as_binary(uint64_t *workerid, char *interface)
  43. {
  44. struct ifaddrs *ifaddr;
  45. int family;
  46. bool found = false;
  47. if (getifaddrs(&ifaddr) == -1) {
  48. perror("get_all_hw_ifs():Call to getifaddrs failed");
  49. goto fail;
  50. }
  51. for (struct ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  52. if (ifa->ifa_addr == NULL) {
  53. continue;
  54. }
  55. family = ifa->ifa_addr->sa_family;
  56. /* skip if family is not link layer type */
  57. if (family != IF_HW_FAMILY) {
  58. continue;
  59. }
  60. /* skip loopback - anything starting with lo */
  61. if ((ifa->ifa_name == NULL) || strncmp(ifa->ifa_name, "lo", 2) == 0) {
  62. continue;
  63. }
  64. #ifdef __linux__
  65. struct sockaddr_ll *sock_addr = (struct sockaddr_ll*)ifa->ifa_addr;
  66. for (int8_t i = 5; i >=0; --i) {
  67. *workerid |= (uint64_t)sock_addr->sll_addr[i] << (CHAR_BIT * i);
  68. }
  69. #else
  70. struct sockaddr_dl *sock_addr = (struct sockaddr_dl*)ifa->ifa_addr;
  71. unsigned char *ptr = (unsigned char *)LLADDR(sock_addr);
  72. for (int8_t i = 5; i >= 0; --i) {
  73. *workerid |= (uint64_t)*ptr++ << (CHAR_BIT * i);
  74. }
  75. #endif
  76. found = true;
  77. if (interface != NULL && strncmp(ifa->ifa_name, interface, strlen(interface) + 1) == 0) {
  78. break;
  79. }
  80. }
  81. freeifaddrs(ifaddr);
  82. fail:
  83. return found;
  84. }
  85. bool get_current_ts(uint64_t *result)
  86. {
  87. struct timeval now;
  88. if (result == NULL) {
  89. return false;
  90. }
  91. if (gettimeofday(&now, NULL) == -1) {
  92. return false;
  93. }
  94. *result = (uint64_t)((now.tv_sec * 1000) + (now.tv_usec / 1000));
  95. return true;
  96. }