Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <linux/if.h>
- #include <netdb.h>
- #include <stdio.h>
- int main()
- {
- struct ifreq s;
- int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
- strcpy(s.ifr_name, "eth0");
- if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
- int i;
- for (i = 0; i < 6; ++i)
- printf(" %02x", (unsigned char) s.ifr_addr.sa_data[i]);
- puts("n");
- return 0;
- }
- return 1;
- }
- const char * gettaStringFromNativeCode(void)
- {
- return "This is a string";
- }
- // Using "string" here because its pseudo-code and I don't know what i'm doing. :-)
- string getMAC()
- {
- struct ifreq s;
- int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
- strcpy(s.ifr_name, "eth0");
- var macAddress = string.Empty; // yah, this is actually C#
- if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
- int i;
- for (i = 0; i < 6; ++i)
- // yah, this is a bit of C# too.
- macAddress += string.Format(" %02x", (unsigned char) s.ifr_addr.sa_data[i]) );
- }
- return macAddress;
- }
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <linux/if.h>
- #include <netdb.h>
- #include <stdio.h>
- char *getmac(char *iface)
- {
- #define MAC_STRING_LENGTH 13
- char *ret = malloc(MAC_STRING_LENGTH);
- struct ifreq s;
- int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
- strcpy(s.ifr_name, iface);
- if (fd >= 0 && ret && 0 == ioctl(fd, SIOCGIFHWADDR, &s))
- {
- int i;
- for (i = 0; i < 6; ++i)
- snprintf(ret+i*2,MAC_STRING_LENGTH-i*2,"%02x",(unsigned char) s.ifr_addr.sa_data[i]);
- }
- else
- {
- perror("malloc/socket/ioctl failed");
- exit(1);
- }
- return(ret);
- }
- int main()
- {
- char *mac = getmac("eth0");
- printf("%sn",mac);
- free(mac);
- }
- int getMac(char mac[6])
- {
- struct ifreq s;
- int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
- strcpy(s.ifr_name, "eth0");
- if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
- int i;
- for (i = 0; i < 6; ++i)
- mac[i] = s.ifr_addr.sa_data[i];
- return 0;
- }
- return 1;
- }
Add Comment
Please, Sign In to add comment