C语言 MAC地址打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6063039/
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
MAC Address printing
提问by Saad Talaat
This is a code that gets some info about network the problem is when it prints the MAC address it prints it sometime normally and sometime with fff's like 00:21:84:a2:12:88 and 00:ffffff21:84:a2:12:ffffff88 varies from machine to another
这是一个获取有关网络信息的代码,问题是当它打印 MAC 地址时,它有时会正常打印,有时会像 00:21:84:a2:12:88 和 00:ffffff21:84:a2:12 这样的 fff :ffffff88 因机器而异
here is the code
这是代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/types.h>
# include <netinet/if_ether.h>
#define IP_QUAD(ip) (ip)>>24,((ip)&0x00ff0000)>>16,((ip)&0x0000ff00)>>8,((ip)&0x000000ff)
#define IP_ADDR_LEN 4
struct {
char *dev;
int sd;
struct ether_addr eth;
struct in_addr ip;
struct in_addr bcast;
unsigned int mtu;
} local_info ;
struct ifreq eth_init(char*,struct ifreq);
struct ifreq eth_get_info(struct ifreq);
struct ifreq eth_get_bcast(struct ifreq);
int
main(int argc,char **argv){
int sd;
struct ifreq ifr;
if(argc != 2){
fprintf(stderr,"usage: <command> <devicename>\n");
exit(1);
}
ifr = eth_init(argv[1],ifr);
ifr = eth_get_info(ifr);
printf("> Exiting...\n");
return(0);
}
struct ifreq
eth_init(char *dev,struct ifreq ifr){
//Intitating Socket
if((local_info.sd = socket(PF_INET,SOCK_PACKET,(ETH_P_ALL))) < 0){
printf("> Error initating the ethernet socket..\n");
exit(-1);
}
//Yupeeeeeeee Descriptor open
printf("> Initated Ethernet socket on Descriptor (%x)\n",local_info.sd);
//Set global variables
local_info.dev = dev;
return ifr;
}
struct ifreq
eth_get_info(struct ifreq ifr){
int i = ETHER_ADDR_LEN;
char* ptr;
memset(&ifr,0,sizeof(ifr));
strncpy(ifr.ifr_name,local_info.dev,sizeof(ifr.ifr_name));
//Getting MAC
if(ioctl(local_info.sd,SIOCGIFHWADDR,&ifr) < 0){
printf("> Error Getting the Local Mac address\n");
exit(-1);
}
printf("> Successfully received Local MAC Address : %02x:%02x:%02x:%02x:%02x:%02x\n",
ifr.ifr_hwaddr.sa_data[0],ifr.ifr_hwaddr.sa_data[1],ifr.ifr_hwaddr.sa_data[2]
,ifr.ifr_hwaddr.sa_data[3],ifr.ifr_hwaddr.sa_data[4],ifr.ifr_hwaddr.sa_data[5]);
memcpy(&(local_info.eth),&ifr.ifr_hwaddr.sa_data,ETH_ALEN);
// Getting IP Address
memset(&ifr,0,sizeof(ifr));
strncpy(ifr.ifr_name,local_info.dev,sizeof(ifr.ifr_name));
if( ioctl(local_info.sd,SIOCGIFADDR,&ifr) < 0){
printf("> Error gettint the local IP address\n");
exit(-1);
}
printf("> Successfully received the IP Address %s\n",inet_ntoa((*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr));
memcpy(&(local_info.ip.s_addr),&(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr,IP_ADDR_LEN);
// Get MTU
memset(&ifr,0,sizeof(ifr));
strncpy(ifr.ifr_name,local_info.dev,sizeof(ifr.ifr_name));
if ( ioctl(local_info.sd,SIOCGIFMTU,&ifr) < 0){
printf("> Error Getting the MTU Value\n");
exit(-1);
}
printf("> Recevied Successfully the MTU Value \n");
local_info.mtu = ifr.ifr_mtu;
return ifr;
}
struct ifreq
eth_get_bcast(struct ifreq ifr){
/* get broadcast addr for size */
memset(&ifr,0,sizeof(ifr));
strncpy(ifr.ifr_name, local_info.dev, sizeof (ifr.ifr_name));
if (ioctl(local_info.sd, SIOCGIFBRDADDR, &ifr) < 0 ) {
printf("> Error getting the Broadcast address\n");
exit(-1);
}
printf("> Received the BroadCast address: %s\n",inet_ntoa((*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr));
memcpy(&(local_info.bcast.s_addr),
&(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr,
IP_ADDR_LEN);
return ifr;
}
the problem is the the eth_get_info function the MAC sector , the printing statement
问题是 eth_get_info 函数 MAC 扇区,打印语句
any solutions how to fix that?
任何解决方案如何解决这个问题?
回答by Cédric Julien
It looks like a signed/unsigned problem.
它看起来像一个签名/未签名的问题。
Try to cast into unsigned char :
尝试转换为 unsigned char :
printf("> Successfully received Local MAC Address : %02x:%02x:%02x:%02x:%02x:%02x\n",
(unsigned char) ifr.ifr_hwaddr.sa_data[0],
(unsigned char) ifr.ifr_hwaddr.sa_data[1],
(unsigned char) ifr.ifr_hwaddr.sa_data[2],
(unsigned char) ifr.ifr_hwaddr.sa_data[3],
(unsigned char) ifr.ifr_hwaddr.sa_data[4],
(unsigned char) ifr.ifr_hwaddr.sa_data[5]);
回答by Lee Hambley
Although there is already an accepted answer here, there is a better solution in netinet/ether.h.
虽然这里已经有一个可以接受的答案,但netinet/ether.h.
Given that Mac addresses are typically stored in u_int8_ttypes, as in the ether_addr struct:
鉴于 Mac 地址通常以u_int8_t类型存储,如 ether_addr 结构:
You can simply do this:
你可以简单地这样做:
printf("Mac Address: %s", ether_ntoa((struct ether_addr*)ar->sa));
in my Case aris something that looks like this:
在我的情况下ar是这样的:
struct {
u_int8_t sa[6];
}
You can easily copy this into another buffer with something like asprintf:
您可以轻松地将其复制到另一个缓冲区中,例如asprintf:
char *formatted_mac_address;
asprintf(formatted_mac_address, "Mac Address: %s", ether_ntoa((struct ether_addr*)ar->sa));
If you don't have a struct as I do, you can also just use the address of any u_int8_tin place of ar->sa.
如果你没有像我一样的结构体,你也可以使用 any 的地址u_int8_t代替ar->sa.
Appropriate headers/etc should be pulled in, but that's going to look a lotneater than the accepted solution here.
应该引入适当的标题/等,但这看起来比这里接受的解决方案要整洁得多。
回答by Ves
I prefer use an explicit length modifier in format string for values shorter than int. For example %02hhxinstead of %02xfor one-byte values. It allow me to not worry about those subtle conversion and promotion issues:
对于短于 int 的值,我更喜欢在格式字符串中使用显式长度修饰符。例如,%02hhx而不是%02x一字节值。它让我不用担心那些微妙的转换和推广问题:
#include <stdio.h>
int main(void)
{
signed char sff = '\xff';
unsigned char uff = '\xff';
printf("signed: %02x %02hhx\n", sff, sff);
printf("unsigned: %02x %02hhx\n", uff, uff);
return 0;
}
prints
印刷
signed: ffffffff ff
unsigned: ff ff
回答by user7135185
Here is a simple C program to find the MAC address of system:
这是一个简单的 C 程序来查找系统的 MAC 地址:
#include<stdio.h>
#include<stdlib.h>
int main() {
system("getmac");
return 0;
}

