linux网络编程——获取网卡的ip或mac地址

#include<net/if.h> 
/* Interface request structure used for socket ioctl's.  All interface
   ioctl's must have parameter definitions which begin with ifr_name.
   The remainder may be interface specific.  */

struct ifreq
  {
# define IFHWADDRLEN	6
# define IFNAMSIZ	IF_NAMESIZE
    union
      {
	char ifrn_name[IFNAMSIZ];	/* Interface name, e.g. "en0".  */
      } ifr_ifrn;

    union
      {
	struct sockaddr ifru_addr;
	struct sockaddr ifru_dstaddr;
	struct sockaddr ifru_broadaddr;
	struct sockaddr ifru_netmask;
	struct sockaddr ifru_hwaddr;
	short int ifru_flags;
	int ifru_ivalue;
	int ifru_mtu;
	struct ifmap ifru_map;
	char ifru_slave[IFNAMSIZ];	/* Just fits the size */
	char ifru_newname[IFNAMSIZ];
	__caddr_t ifru_data;
      } ifr_ifru;
  };
# define ifr_name	ifr_ifrn.ifrn_name	/* interface name 	*/
# define ifr_hwaddr	ifr_ifru.ifru_hwaddr	/* MAC address 		*/
# define ifr_addr	ifr_ifru.ifru_addr	/* address		*/
# define ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-p lnk	*/
# define ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address	*/
# define ifr_netmask	ifr_ifru.ifru_netmask	/* interface net mask	*/
# define ifr_flags	ifr_ifru.ifru_flags	/* flags		*/
# define ifr_metric	ifr_ifru.ifru_ivalue	/* metric		*/
# define ifr_mtu	ifr_ifru.ifru_mtu	/* mtu			*/
# define ifr_map	ifr_ifru.ifru_map	/* device map		*/
# define ifr_slave	ifr_ifru.ifru_slave	/* slave device		*/
# define ifr_data	ifr_ifru.ifru_data	/* for use by interface	*/
# define ifr_ifindex	ifr_ifru.ifru_ivalue    /* interface index      */
# define ifr_bandwidth	ifr_ifru.ifru_ivalue	/* link bandwidth	*/
# define ifr_qlen	ifr_ifru.ifru_ivalue	/* queue length		*/
# define ifr_newname	ifr_ifru.ifru_newname	/* New name		*/
# define _IOT_ifreq	_IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
# define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
# define _IOT_ifreq_int	_IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)

//SIOCGIFADDR 获取网络接口的ip地址
//SIOCGIFHWADDR 获取网卡的mac地址
#include<net/if.h> 
//socket
#include<sys/socket.h>
#include<sys/types.h>
//标准输入输出
#include<cstdio>
#include<cstdlib>
#include<sys/ioctl.h>
//协议函数类型定义AF—inet
#include<netinet/in.h>
//地址转换函数 inet_addr()等
#include<arpa/inet.h>


void MainWindow::get_mac(char* mac,const char* eth_name){
    struct ifreq ifr;
    int socketfd=socket(AF_INET,SOCK_DGRAM,0);
    if(socketfd==-1){  // <0也可
        qDebug()<<"get mac error"<<endl;
        return;
    }
    //填入ifr_name字段
    strcpy(ifr.ifr_name,eth_name);
    if(ioctl(socketfd,SIOCGIFHWADDR,&ifr)<0) //获取mac地址,更改请求可获得其他的信息
    {
       close();
       return;
    }
    sprintf(mac,"%02x:%02x:%02x:%02x:%02x:%02x",
            (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]);
    return;
}

常见的mac和ip地址转换方式

整形转换为点分十进制的字符串uint32_t -->1.1.1.1

char ip[16];
struct sockaddr_in *sin=(struct sockaddr_in*)&ifr.ifr_addr;
inet_ntop(AF_INET,&sin->sin_addr.s_addr,ip,16);

点分十进制的字符串转换为网络字节序的整形

inet_pton(AF_INET,"1.1.1.1",&sin->sin_addr.s_addr)

MAC地址的字符串作为输入转换为一个六字节的十六进制字节数组

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void mac_to_hex(char *mac_addr, unsigned char *hex_bytes) {
    char *octet;
    int i = 0;
    for (octet = strtok(mac_addr, ":"); octet != NULL; octet = strtok(NULL, ":")) {
        hex_bytes[i++] = (unsigned char)strtoul(octet, NULL, 16);
    }
}
int main() {
    char mac_addr[] = "00:11:22:33:44:55";
    unsigned char hex_bytes[6];
    mac_to_hex(mac_addr, hex_bytes);
    for (int i = 0; i < 6; i++) {
        printf("%02x", hex_bytes[i]);
    }
    return 0;
}

六字节的十六进制字节数组作为输入转换为MAC地址字符串

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void hex_to_mac(unsigned char *hex_bytes, char *mac_addr) {
    sprintf(mac_addr, "%02x:%02x:%02x:%02x:%02x:%02x", hex_bytes[0], hex_bytes[1], hex_bytes[2], hex_bytes[3], hex_bytes[4], hex_bytes[5]);
}

int main() {
    unsigned char hex_bytes[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
    char mac_addr[18];
    hex_to_mac(hex_bytes, mac_addr);
    printf("%s\n", mac_addr);
    return 0;
}

该函数接受一个没有冒号的MAC地址字符串作为输入,并将其转换为带有冒号的MAC地址字符串。函数首先使用strncpy函数将每个八位数复制到一个临时缓冲区中,然后在每个八位数之间插入一个冒号。最后,函数使用strcpy函数将带有冒号的MAC地址字符串复制回原始输入字符串。

在主函数中,我们使用一个char数组存储没有冒号的MAC地址字符串,然后将其传递给函数来添加冒号。最后,我们打印添加冒号后的MAC地址字符串。

#include <stdio.h>
#include <string.h>

void add_colon_to_mac(char *mac_addr) {
    char tmp[18];
    int i;
    for (i = 0; i < 12; i += 2) {
        strncpy(&tmp[i + i/2], &mac_addr[i], 2);
        tmp[i + i/2 + 2] = ':';
    }
    tmp[17] = '\0';
    strcpy(mac_addr, tmp);
}

int main() {
    char mac_addr[] = "001122334455";
    printf("MAC address before: %s\n", mac_addr);
    add_colon_to_mac(mac_addr);
    printf("MAC address after: %s\n", mac_addr);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_53633989/article/details/129349792