我正在尝试获取当前使用的网络接口的MAC地址。
首先这个基于msdn tcp客户端示例的简单代码。它是我的客户端应用程序部分的简化版本。它打印出用于与服务器建立TCP连接的IP地址(服务器IP和端口作为命令行参数传递)。在我的客户端应用程序中,我从IP地址获取MAC,我通过getsockname fucntion从socket获取。
int __cdecl main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
int iResult;
// Validate the parameters
if (argc != 3) {
printf("usage: %s server-name port\n", argv[0]);
return 1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(argv[1], argv[2], &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
static size_t attempt = 0;
std::cout << "attempt: " << attempt++ << std::endl;
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
{
char ip_address_info [128] = "";
SOCKADDR_IN host = {0};
int host_size = sizeof (SOCKADDR_IN);
int res;
res = getsockname(ConnectSocket, (SOCKADDR*) &host, &host_size);
if (res == 0) {
sprintf (ip_address_info, "%s:%d", inet_ntoa (host.sin_addr), htons (host.sin_port));
std::cout << "ip_address_info: " << ip_address_info << std::endl;
}
else {
std::cout << "res == 0 after getsockname" << std::endl;
}
host_size = sizeof (SOCKADDR_IN);
res = getpeername (ConnectSocket, (SOCKADDR*) &host, &host_size);
if (res == 0) {
sprintf (ip_address_info, "%s:%d", inet_ntoa (host.sin_addr), htons (host.sin_port));
std::cout << "ip_address_info: " << ip_address_info << std::endl;
}
else {
std::cout << "res == 0 after getpeername" << std::endl;
}
}
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
std::cout << "shutdown run" << std::endl;
// cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
现在问题和疑问! 当我尝试通过我的局域网中的服务器端口3128使用此实用程序时,卡巴斯基反病毒(我假设其他防病毒/ farewalls)开始检查它的内容(网络端口或类似的东西)。我得到代理连接:我的应用程序 - &gt;卡巴斯基 - &gt;我的服务器。我从 getsockname 函数获得的IP变为127.0.0.1。 我曾经在我的应用程序中从IP获取当前使用的MAC,但是当IP为127.0.0.1时我无法这样做。当我将服务器端口更改为2370时,一切正常:我获得了良好的IP,获得了MAC。但问题是,安装程序的用户有时使用代理服务器,端口3128对他们来说很常见,防病毒也是如此。
P.S。:我在Windows上测试了这个(目前也感兴趣),但我认为在其他平台上可以做到这样的事情。
答案 0 :(得分:1)
使用目标套接字地址调用GetBestInterfaceEx。
我相信你使用从这个函数返回的结果索引值来匹配从GetIfTable返回的MIB_IFROW数组。 MIB_IFROW结构有一个名为bPhysAddr的成员值,它是MAC地址。