嵌入式 获取网卡设备以及IP和MAC地址示例以及网络编程之ioctl小结

该博客演示了如何使用C语言在Linux下通过ioctl函数获取网络接口的IP、MAC地址及状态,并展示了查看和操作ARP缓存的示例。博主详细解释了相关结构体如ifreq、ifmap和arpreq,以及ioctl的请求码。通过实际代码运行,展示了获取多个接口和ARP缓存信息的过程。
摘要由CSDN通过智能技术生成

#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h>
#ifdef SOLARIS
#include <sys/sockio.h>
#endif
#define MAXINTERFACES   16
main (argc, argv)
register int argc;
register char *argv[];
{
 register int fd, intrface, retn = 0;
 struct ifreq buf[MAXINTERFACES];
 struct arpreq arp;
 struct ifconf ifc; if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
 {
  ifc.ifc_len = sizeof buf;
  ifc.ifc_buf = (caddr_t) buf;
  if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))
  {
   //获取接口信息
   intrface = ifc.ifc_len / sizeof (struct ifreq);
   printf("interface num is intrface=%d\n\n\n",intrface);
   //根据借口信息循环获取设备IP和MAC地址
   while (intrface-- > 0)
   {
    //获取设备名称
    printf ("net device %s\n", buf[intrface].ifr_name);

    //判断网卡类型
    if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface])))
    {
     if (buf[intrface].ifr_flags & IFF_PROMISC)
     {
      puts ("the interface is PROMISC");
      retn++;
     }
    }
    else
    {
     char str[256];    
     sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
     perror (str);
    }   //判断网卡状态
    if (buf[intrface].ifr_flags & IFF_UP)
    {
     puts("the interface status is UP");
    }
    else
    {
     puts("the interface status is DOWN");
    }   //获取当前网卡的IP地址
    if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
    {
     puts ("IP address is:");
     printf("%s",(char *)inet_ntoa((struct in_addr)((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
     puts("");
     //puts (buf[intrface].ifr_addr.sa_data);
    }
    else
    {
     char str[256];              
     sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
     perror (str);
    }

#ifdef SOLARIS
    //获取MAC地址
    arp.arp_pa.sa_family = AF_INET;
    arp.arp_ha.sa_family = AF_INET;
    ((struct sockaddr_in*)&arp.arp_pa)->sin_addr.s_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr.s_addr;
    if (!(ioctl (fd, SIOCGARP, (char *) &arp)))
    {
     puts ("HW address is:");    //以十六进制显示MAC地址
     printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
       (unsigned char)arp.arp_ha.sa_data[0],
       (unsigned char)arp.arp_ha.sa_data[1],
       (unsigned char)arp.arp_ha.sa_data[2],
       (unsigned char)arp.arp_ha.sa_data[3],
       (unsigned char)arp.arp_ha.sa_data[4],
       (unsigned char)arp.arp_ha.sa_data[5]);                
     puts("");
     puts("");
    }
#else
#if 0

    if (!(ioctl (fd,  SIOCGENADDR, (char *) &buf[intrface])))
    {
     puts ("HW address is:");
     printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
       (unsigned char)buf[intrface].ifr_enaddr[0],
       (unsigned char)buf[intrface].ifr_enaddr[1],
       (unsigned char)buf[intrface].ifr_enaddr[2],
       (unsigned char)buf[intrface].ifr_enaddr[3],
       (unsigned char)buf[intrface].ifr_enaddr[4],
       (unsigned char)buf[intrface].ifr_enaddr[5]);                
     puts("");
     puts("");
    }
#endif
    if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))
    {
     puts ("HW address is:");                
     printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
       (unsigned char)buf[intrface].ifr_hwaddr.sa_data[0],
       (unsigned char)buf[intrface].ifr_hwaddr.sa_data[1],
       (unsigned char)buf[intrface].ifr_hwaddr.sa_data[2],
       (unsigned char)buf[intrface].ifr_hwaddr.sa_data[3],
       (unsigned char)buf[intrface].ifr_hwaddr.sa_data[4],
       (unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]);                
     puts("");
     puts("");
    }

#endif           
    else
    {
     char str[256];
     sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name);
     perror (str);
    }
   } //while
  }
  else
   perror ("cpm: ioctl");  
 }
 else
  perror ("cpm: socket");   
 close (fd);
 return retn;
} //虚拟机worklinux下有更为简洁的代码,不过只能实现获取IP功能

 

在我的机器上运行效果如下:

root@u12d32:/home/kongjun/mywork/hi_test/get_mac_linux# ./device_get
interface num is intrface=2


net device eth0
the interface status is UP
IP address is:
10.10.2.59
HW address is:
00:50:56:a9:70:64


net device lo
the interface status is UP
IP address is:
127.0.0.1
HW address is:
00:00:00:00:00:00


root@u12d32:/home/kongjun/mywork/hi_test/get_mac_linux#

 

Linux 网络编程之ioctl函数:

1.介绍

Linux网络程序与内核交互的方法是通过ioctl来实现的,ioctl与网络协议栈进行交互,可得到网络接口的信息,网卡设备的映射属性和配置网络接口.并且还能够查看,修改,删除ARP高速缓存的信息,所以,我们有必要了解一下ioctl函数的具体实现.

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言网络编程中,可以通过发送ARP请求来获取给定IP地址的MAC地址。下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <net/if_arp.h> #include <net/if.h> #include <sys/ioctl.h> int main() { // 目标IP地址 char* targetIP = "192.168.1.1"; // 创建套接字 int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { perror("socket creation failed"); exit(EXIT_FAILURE); } // 获取接口索引 struct ifreq ifr; strncpy(ifr.ifr_name, "eth0", IFNAMSIZ); if (ioctl(sockfd, SIOCGIFINDEX, &ifr) == -1) { perror("ioctl failed"); close(sockfd); exit(EXIT_FAILURE); } int ifindex = ifr.ifr_ifindex; // 构造ARP请求 struct arpreq arpReq; memset(&arpReq, 0, sizeof(arpReq)); struct sockaddr_in* sin = (struct sockaddr_in*)&arpReq.arp_pa; sin->sin_family = AF_INET; sin->sin_addr.s_addr = inet_addr(targetIP); arpReq.arp_ha.sa_family = ARPHRD_ETHER; arpReq.arp_flags = ATF_COM; // 发送ARP请求 if (ioctl(sockfd, SIOCGARP, &arpReq) == -1) { perror("ioctl failed"); close(sockfd); exit(EXIT_FAILURE); } // 获取MAC地址 unsigned char* macAddress = (unsigned char*)arpReq.arp_ha.sa_data; printf("MAC address of %s: %02X:%02X:%02X:%02X:%02X:%02X\n", targetIP, macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]); // 关闭套接字 close(sockfd); return 0; } ``` 注意,上述示例代码中使用了Linux系统特定的网络API和数据结构。在其他操作系统下,可能需要使用不同的接口来实现相同的功能。此外,需要注意以管理员权限运行程序才能成功获取MAC地址
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值