使用WinPcap获取网卡MAC地址

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 关键字:WinPcap 网卡 MAC地址 作者:txw1958 在WpdPack_4_1_2\WpdPack\Examples-remote\PacketDriver\GetMacAddress\目录下有获取MAC地址源文件 GetMacAddress.

关键字:WinPcap 网卡 MAC地址

作者:txw1958

在WpdPack_4_1_2\WpdPack\Examples-remote\PacketDriver\GetMacAddress\目录下有获取MAC地址源文件

GetMacAddress.c

  1: /* 
  2:  * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy) 
  3:  * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California) 
  4:  * All rights reserved. 
  5:  * 
  6:  * Redistribution and use in source and binary forms, with or without 
  7:  * modification, are permitted provided that the following conditions 
  8:  * are met: 
  9:  * 
 10:  * 1. Redistributions of source code must retain the above copyright 
 11:  * notice, this list of conditions and the following disclaimer. 
 12:  * 2. Redistributions in binary form must reproduce the above copyright 
 13:  * notice, this list of conditions and the following disclaimer in the 
 14:  * documentation and/or other materials provided with the distribution. 
 15:  * 3. Neither the name of the Politecnico di Torino, CACE Technologies  
 16:  * nor the names of its contributors may be used to endorse or promote  
 17:  * products derived from this software without specific prior written  
 18:  * permission. 
 19:  * 
 20:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21:  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22:  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 23:  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 24:  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 25:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 26:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 27:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 28:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 29:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 30:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 31:  * 
 32:  */ 
 33:  
 34: #include <stdio.h> 
 35: #include <conio.h> 
 36: #include "..\..\..\Include\packet32.h" 
 37: #include "..\..\..\Include\ntddndis.h"   
 38: #define Max_Num_Adapter 10 
 39: char        AdapterList[Max_Num_Adapter][1024]; 
 40:  
 41: int main() 
 42: { 
 43:     LPADAPTER    lpAdapter = 0; 
 44:     int            i; 
 45:     DWORD        dwErrorCode; 
 46:     char        AdapterName[8192]; 
 47:     char        *temp,*temp1; 
 48:     int            AdapterNum=0,Open; 
 49:     ULONG        AdapterLength; 
 50:     PPACKET_OID_DATA  OidData; 
 51:     BOOLEAN        Status; 
 52:      
 53:     // 
 54:     // Obtain the name of the adapters installed on this machine 
 55:     // 
 56:  
 57:     printf("Packet.dll test application. Library version:%s\n", PacketGetVersion()); 
 58:      
 59:     printf("Adapters installed:\n"); 
 60:     i=0;     
 61:      
 62:     AdapterLength = sizeof(AdapterName); 
 63:      
 64:     if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){ 
 65:         printf("Unable to retrieve the list of the adapters!\n"); 
 66:         return -1; 
 67:     } 
 68:     temp=AdapterName; 
 69:     temp1=AdapterName; 
 70:  
 71:     while ((*temp!='\0')||(*(temp-1)!='\0')) 
 72:     { 
 73:         if (*temp=='\0')  
 74:         { 
 75:             memcpy(AdapterList[i],temp1,temp-temp1); 
 76:             temp1=temp+1; 
 77:             i++; 
 78:         } 
 79:         temp++; 
 80:     } 
 81:            
 82:     AdapterNum=i; 
 83:     for (i=0;i<AdapterNum;i++) 
 84:         printf("\n%d- %s\n",i+1,AdapterList[i]); 
 85:     printf("\n"); 
 86:  
 87:  
 88:     do  
 89:     { 
 90:         printf("Select the number of the adapter to open : "); 
 91:         scanf("%d",&Open); 
 92:         if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum);  
 93:     } while (Open>AdapterNum); 
 94:      
 95:  
 96:     // 
 97:     // Open the selected adapter 
 98:     // 
 99:  
100:     lpAdapter =   PacketOpenAdapter(AdapterList[Open-1]); 
101:      
102:     if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE)) 
103:     { 
104:         dwErrorCode=GetLastError(); 
105:         printf("Unable to open the adapter, Error Code : %lx\n",dwErrorCode);  
106:  
107:         return -1; 
108:     }     
109:  
110:     //  
111:     // Allocate a buffer to get the MAC adress 
112:     // 
113:  
114:     OidData = malloc(6 + sizeof(PACKET_OID_DATA)); 
115:     if (OidData == NULL)  
116:     { 
117:         printf("error allocating memory!\n"); 
118:         PacketCloseAdapter(lpAdapter); 
119:         return -1; 
120:     } 
121:  
122:     //  
123:     // Retrieve the adapter MAC querying the NIC driver 
124:     // 
125:  
126:     OidData->Oid = OID_802_3_CURRENT_ADDRESS; 
127:  
128:     OidData->Length = 6; 
129:     ZeroMemory(OidData->Data, 6); 
130:      
131:     Status = PacketRequest(lpAdapter, FALSE, OidData); 
132:     if(Status) 
133:     { 
134:         printf("The MAC address of the adapter is %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", 
135:             (PCHAR)(OidData->Data)[0], 
136:             (PCHAR)(OidData->Data)[1], 
137:             (PCHAR)(OidData->Data)[2], 
138:             (PCHAR)(OidData->Data)[3], 
139:             (PCHAR)(OidData->Data)[4], 
140:             (PCHAR)(OidData->Data)[5]); 
141:     } 
142:     else 
143:     { 
144:         printf("error retrieving the MAC address of the adapter!\n"); 
145:     } 
146:  
147:     free(OidData); 
148:     PacketCloseAdapter(lpAdapter); 
149:     return (0); 
150: } 
151: 

 

运行后获得mac地址

Packet.dll test application. Library version:4.0.0.1040
Adapters installed:

1- \Device\NPF_GenericDialupAdapter

2- \Device\NPF_{B797EC79-A74B-40E4-96E7-0786726CC3C2}

3- \Device\NPF_{B513B814-5657-417F-B515-965AEAC13B83}

Select the number of the adapter to open : 2
The MAC address of the adapter is 02:ff:54:55:4e:01

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
5月前
|
存储 API C++
C/C++ 获取主机网卡MAC地址
MAC地址(Media Access Control address),又称为物理地址或硬件地址,是网络适配器(网卡)在制造时被分配的全球唯一的48位地址。这个地址是数据链路层(OSI模型的第二层)的一部分,用于在局域网(LAN)中唯一标识网络设备。获取网卡地址主要用于网络标识和身份验证的目的。MAC地址是一个唯一的硬件地址,通常由网卡的制造商在制造过程中分配。通过获取MAC地址可以判断当前主机的唯一性可以与IP地址绑定并实现网络准入控制。在Windows平台下获取MAC地址的方式有很多,获取MAC地址的常见方式包括使用操作系统提供的网络API(如Windows的GetAdaptersAddr
128 0
C/C++ 获取主机网卡MAC地址
|
缓存 网络协议 网络架构
以太网数据链路层、Ethernet_II帧格式、IEEE802.3帧格式,以太网的MAC地址的组成,ARP地址解析协议的工作原理,单播帧、组播帧、广播帧的区别
数据链路层,Ethernet_II帧格式、IEEE802.3帧格式,帧格式的区分以及链路层每种帧格式有什么作用,怎么区别分辨帧格式,以太网MAC地址的组成,ARP地址解析协议原理、什么是单播帧?什么是组播数据帧?什么是广播帧?...............
以太网数据链路层、Ethernet_II帧格式、IEEE802.3帧格式,以太网的MAC地址的组成,ARP地址解析协议的工作原理,单播帧、组播帧、广播帧的区别
|
Linux
Linux下修改网卡MAC地址
Linux下修改网卡MAC地址
188 0
Office 2021 for Mac 简体中文 官网下载地址
Office 2021 for Mac 它包括 Word、Excel、PowerPoint 和 Outlook。一次性购买该产品可安装在一台 Mac 上,供家用或办公之用。
613 0
计算机网络学习13:MAC地址
如果信道上的主机只有两台(点对点信道)不需要地址
计算机网络学习13:MAC地址
|
存储 安全 iOS开发
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
412 0
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
|
域名解析 网络协议 测试技术
CCNA-思科网络基础(IP地址 MAC地址 DNS DHCP PING )
CCNA-思科网络基础(IP地址 MAC地址 DNS DHCP PING )
246 0
CCNA-思科网络基础(IP地址 MAC地址 DNS DHCP PING )
|
缓存 网络协议