Linux 如何使用 C 程序获取机器的 MAC 地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6052377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:08:55  来源:igfitidea点击:

How to get MAC address of your machine using a C program?

clinuxchromelessjsctypes

提问by David Murdoch

This question is exactly like this question: How to get MAC address of your machine using a C program?

这个问题和这个问题完全一样:How to get the MAC address of your machine using a C program?

"I am working on Ubuntu. How can I get MAC address of my machine or an interface say eth0 using C program."

“我在 Ubuntu 上工作。如何使用 C 程序获取我的机器的 MAC 地址或接口说 eth0。”



Now, I don't usually touch C...but in this case I have to. Since I don't really know what is going on in the following code, which was taken from the answer linked to above, I need some help.

现在,我通常不碰 C ......但在这种情况下我必须这样做。由于我真的不知道下面的代码发生了什么,这是从上面链接的答案中获取的,我需要一些帮助。

#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;
}

Instead of a function that prints the MAC Address I need a function that returns it as a string. You know, like this:

我需要一个将其作为字符串返回的函数,而不是打印 MAC 地址的函数。你知道,像这样:

const char * gettaStringFromNativeCode(void) 
{
    return "This is a string";
}

This is to be used with Mozilla Chromeless, which uses Firefox's new JCTYPES like this.

这将与Mozilla Chromeless一起使用,它像这样使用 Firefox 的新 JCTYPES 。

Basically, I'm looking to do something like this (borrowing from C#):

基本上,我希望做这样的事情(从 C# 借来):

// 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;
}

采纳答案by Seth Robertson

#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("%s\n",mac);
  free(mac);
}

回答by Andrey Sidorov

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];
    close(fd);
    return 0;
  }
  close(fd);
  return 1;
}