Guest User

Untitled

a guest
Jul 16th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <sys/ioctl.h>
  3. #include <linux/if.h>
  4. #include <netdb.h>
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9. struct ifreq s;
  10. int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  11.  
  12. strcpy(s.ifr_name, "eth0");
  13. if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
  14. int i;
  15. for (i = 0; i < 6; ++i)
  16. printf(" %02x", (unsigned char) s.ifr_addr.sa_data[i]);
  17. puts("n");
  18. return 0;
  19. }
  20. return 1;
  21. }
  22.  
  23. const char * gettaStringFromNativeCode(void)
  24. {
  25. return "This is a string";
  26. }
  27.  
  28. // Using "string" here because its pseudo-code and I don't know what i'm doing. :-)
  29. string getMAC()
  30. {
  31. struct ifreq s;
  32. int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  33.  
  34. strcpy(s.ifr_name, "eth0");
  35. var macAddress = string.Empty; // yah, this is actually C#
  36. if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
  37. int i;
  38. for (i = 0; i < 6; ++i)
  39. // yah, this is a bit of C# too.
  40. macAddress += string.Format(" %02x", (unsigned char) s.ifr_addr.sa_data[i]) );
  41. }
  42. return macAddress;
  43. }
  44.  
  45. #include <sys/socket.h>
  46. #include <sys/ioctl.h>
  47. #include <linux/if.h>
  48. #include <netdb.h>
  49. #include <stdio.h>
  50.  
  51. char *getmac(char *iface)
  52. {
  53. #define MAC_STRING_LENGTH 13
  54. char *ret = malloc(MAC_STRING_LENGTH);
  55. struct ifreq s;
  56. int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  57.  
  58. strcpy(s.ifr_name, iface);
  59. if (fd >= 0 && ret && 0 == ioctl(fd, SIOCGIFHWADDR, &s))
  60. {
  61. int i;
  62. for (i = 0; i < 6; ++i)
  63. snprintf(ret+i*2,MAC_STRING_LENGTH-i*2,"%02x",(unsigned char) s.ifr_addr.sa_data[i]);
  64. }
  65. else
  66. {
  67. perror("malloc/socket/ioctl failed");
  68. exit(1);
  69. }
  70. return(ret);
  71. }
  72.  
  73. int main()
  74. {
  75. char *mac = getmac("eth0");
  76. printf("%sn",mac);
  77. free(mac);
  78. }
  79.  
  80. int getMac(char mac[6])
  81. {
  82. struct ifreq s;
  83. int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  84.  
  85. strcpy(s.ifr_name, "eth0");
  86. if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
  87. int i;
  88. for (i = 0; i < 6; ++i)
  89. mac[i] = s.ifr_addr.sa_data[i];
  90. return 0;
  91. }
  92. return 1;
  93. }
Add Comment
Please, Sign In to add comment