これはばかげた質問かもしれません。すでにここで対処されている場合は申し訳ありませんが、かなり検索しましたが、あまり運がありませんでした。インターフェイスのハードウェア アドレスを C で取得しようとしていますが、OS X (x86-64) を使用しています。でそれを取得する方法は知ってifconfig
いますが、少なくとも OS X コンピューターでは、私のプログラムで自動的にそれを取得したいと考えています。私はこのリンクを投稿した別のスレッドを見つけましたが、これは私が望むことをほとんど行います (いくつかの変更を加えて) が、iokit
関数をリンクすることはできませんld
(私のコンパイラは ですgcc
)。-lIOKit
フラグをコマンド ラインに追加しようと-framework IOKit
しましたgcc
が、それでも同じリンク エラーが発生します。ここに私のコードへのリンクがあります: headerとsource。
4594 次
1 に答える
7
この小さなプログラムは、OSX 上で変更を加えることなく動作します。
コード : (freebsd リストからの Alecs King のクレジット)
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int mib[6], len;
char *buf;
unsigned char *ptr;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
if (argc != 2) {
fprintf(stderr, "Usage: getmac <interface>\n");
return 1;
}
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST;
if ((mib[5] = if_nametoindex(argv[1])) == 0) {
perror("if_nametoindex error");
exit(2);
}
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
perror("sysctl 1 error");
exit(3);
}
if ((buf = malloc(len)) == NULL) {
perror("malloc error");
exit(4);
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
perror("sysctl 2 error");
exit(5);
}
ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + 1);
ptr = (unsigned char *)LLADDR(sdl);
printf("%02x:%02x:%02x:%02x:%02x:%02x\n", *ptr, *(ptr+1), *(ptr+2),
*(ptr+3), *(ptr+4), *(ptr+5));
return 0;
}
ただし、次のように変更する必要がありint len;
ますsize_t len;
于 2012-05-15T03:27:43.517 に答える