-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathport_win32.cpp
More file actions
131 lines (113 loc) · 3.9 KB
/
port_win32.cpp
File metadata and controls
131 lines (113 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
port_win32.cpp: win32 related implements
Copyright (C) 2014 C.C.<exiledkingcc@gmail.com>
This file is part of ccnt.
ccnt is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ccnt is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ccnt. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WIN32 //WIN32 implementations blow
#error "only for win32 implements"
#endif // WIN32
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#elif !(_WIN32_WINNT >= 0x0600)
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#include <cstring>
using std::memcpy;
#include <winsock2.h>
#include <ws2ipdef.h>
#include <iphlpapi.h>
#include "eapnic.h"
const std::vector<nic> get_nics() throw(eap_runtime_error)
{
/** using windows api GetAdaptersAddresses, see
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365915%28v=vs.85%29.aspx */
std::vector<nic> all_nics;
char *addr_buf=new char[0x10000];
PIP_ADAPTER_ADDRESSES p_adp_addrs=nullptr;
ULONG buflen=0;
int ret=0,iter=0;
do
{
p_adp_addrs=reinterpret_cast<PIP_ADAPTER_ADDRESSES>(addr_buf);
ret=GetAdaptersAddresses(AF_INET,GAA_FLAG_INCLUDE_GATEWAYS,nullptr,p_adp_addrs,&buflen);
}
while((ret!=NO_ERROR) && (iter++<4));
if(ret!=NO_ERROR)
{
delete[] addr_buf;
throw eap_runtime_error("GetAdaptersAddresses failed");
}
try
{
for(auto p=p_adp_addrs; p!=nullptr; p=p->Next)
{
//if(p->IfType!=IF_TYPE_ETHERNET_CSMACD){ continue; }
const wchar_t *src=p->Description;
int len=wcslen(src)*4+1;
char *desc=new char[len];
memset(desc,0,len);
mbstate_t ps;
wcsrtombs(desc,&src,len,&ps);
nic x(p->AdapterName,desc);
std::memcpy(x._mac,p->PhysicalAddress,6);
delete[] desc;
for(auto pp=p->FirstUnicastAddress; pp!=nullptr; pp=pp->Next)
{
if(pp->Address.lpSockaddr->sa_family == AF_INET)
{
x._ip=reinterpret_cast<sockaddr_in*>(pp->Address.lpSockaddr)->sin_addr.s_addr;
union{ uint32_t x_; uint8_t y_[4]; } _u;
_u.x_=(~0)<<(32-pp->OnLinkPrefixLength);
x._mask=(_u.y_[0]<<24)|(_u.y_[1]<<16)|(_u.y_[2]<<8)|(_u.y_[3]);
break;
}
}
for(auto pp=p->FirstGatewayAddress; pp!=nullptr; pp=pp->Next)
{
if(pp->Address.lpSockaddr->sa_family == AF_INET)
{
x._gateway=reinterpret_cast<sockaddr_in*>(pp->Address.lpSockaddr)->sin_addr.s_addr;
break;
}
}
for(auto pp=p->FirstDnsServerAddress; pp!=nullptr; pp=pp->Next)
{
if(pp->Address.lpSockaddr->sa_family == AF_INET)
{
x._dns=reinterpret_cast<sockaddr_in*>(pp->Address.lpSockaddr)->sin_addr.s_addr;
break;
}
}
x._dhcp=p->Flags&IP_ADAPTER_DHCP_ENABLED;
all_nics.push_back(x);
}
}
catch(std::exception &e)
{
delete[] addr_buf;
throw eap_runtime_error(e.what());
}
delete[] addr_buf;
return all_nics;
}
static HANDLE mutex;
bool enter_running()
{
mutex=CreateMutex(NULL,FALSE,"ccnt_already_running");
return GetLastError() == ERROR_ALREADY_EXISTS ? false : true;
}
void leave_running()
{
ReleaseMutex(mutex);
}