TNB Library
TnbIpHelper.h
[詳解]
1#pragma once
15#include "TnbIcmp.h"
16
17
18
19#ifndef _TnbDOXYGEN //Document作成用シンボル
20#include <iprtrmib.h>
21 #ifndef UDP6_STATS
22 //古いSDKだと以下の宣言がないので、暫定宣言
23 typedef struct _MIBICMPSTATS_EX
24 {
25 DWORD dwMsgs;
26 DWORD dwErrors;
27 DWORD rgdwTypeCount[256];
28 } MIBICMPSTATS_EX, *PMIBICMPSTATS_EX;
29 typedef struct _MIB_ICMP_EX
30 {
31 MIBICMPSTATS_EX icmpInStats;
32 MIBICMPSTATS_EX icmpOutStats;
33 } MIB_ICMP_EX,*PMIB_ICMP_EX;
34 #endif
35#endif
36#include <iphlpapi.h>
37#pragma comment(lib, "iphlpapi.lib")
38
39
40
41//TNB Library
42namespace TNB
43{
44
45
46
58{
59public:
60
67 static INDEX GetMibIndex(LPCSTR lpszAddress)
68 {
69 if ( lpszAddress == NULL || lpszAddress[0] == 0 )
70 {
71 return INVALID_INDEX;
72 }
73 DWORD dwIp = ::inet_addr(lpszAddress);
74 if ( dwIp == INADDR_NONE )
75 {
76 // ドット形式でないとき
77 LPHOSTENT ptHost = ::gethostbyname(lpszAddress);
78 if ( ptHost == NULL )
79 {
80 TRACE2A( "::gethostbyname(%s) error = %d\n", lpszAddress, ::WSAGetLastError() );
81 return INVALID_INDEX;
82 }
83 dwIp = *(reinterpret_cast<DWORD*>(ptHost->h_addr));
84 }
85 //
86 DWORD d = 0;
87 ::GetIpAddrTable(NULL, &d, FALSE);
88 CWorkMem mem(d);
89 MIB_IPADDRTABLE* P = reinterpret_cast<MIB_IPADDRTABLE*>(mem.Ref());
90 if ( ::GetIpAddrTable(P, &d, FALSE) == NO_ERROR )
91 {
92 loop ( i, P->dwNumEntries )
93 {
94 if ( dwIp == P->table[i].dwAddr )
95 {
96 return P->table[i].dwIndex;
97 }
98 }
99 }
100 return INVALID_INDEX;
101 }
102
110 static bool GetMibIfRow(INDEX dwMibIndex, MIB_IFROW& _tMibIfRow)
111 {
112 DWORD d = 0;
113 ::GetIpAddrTable(NULL, &d, FALSE);
114 CWorkMem mem(d);
115 MIB_IFTABLE* P = reinterpret_cast<MIB_IFTABLE*>(mem.Ref());
116 if ( ::GetIfTable(P, &d, FALSE) == NO_ERROR )
117 {
118 loop ( i, P->dwNumEntries )
119 {
120 if ( P->table[i].dwIndex == dwMibIndex )
121 {
122 _tMibIfRow = P->table[i];
123 return true;
124 }
125 }
126 }
127 return false;
128 }
129
137 static bool GetMibIfRow(LPCSTR lpszAddress, MIB_IFROW& _tMibIfRow)
138 {
139 INDEX r = GetMibIndex(lpszAddress);
140 if ( r != INVALID_INDEX )
141 {
142 return GetMibIfRow(r, _tMibIfRow);
143 }
144 return false;
145 }
146
154 static bool GetMacAddress(CWorkMem& _mac, LPCSTR lpszAddress)
155 {
156 IPAddr ipAddr = ::inet_addr(lpszAddress);
157 if ( ipAddr == INADDR_NONE )
158 {
159 return false;
160 }
161 ULONG aulMac[2] = { 0xFFFFFFFF, 0xFFFFFFFF };
162 ULONG ulLen = 6;
163 HRESULT hr = ::SendARP(ipAddr, 0, aulMac, &ulLen);
164 if ( FAILED(hr) )
165 {
166 return false;
167 }
168 CStr s;
169 BYTE* pbHexMac = reinterpret_cast<BYTE*>(aulMac);
170 _mac.Resize(ulLen);
171 MemCopy(_mac.Ref(), pbHexMac, ulLen);
172 return true;
173 }
174};
175
176
177
178}; // TNB
179
180
181
182
183
184#if 0
185
186 // 信号強度を取得する
187
188 IP_INTERFACE_INFO* pIfTable;
189 ULONG dwOutBufLen;
190
191 if ( ::GetInterfaceInfo( NULL, &dwOutBufLen ) == ERROR_INSUFFICIENT_BUFFER )
192 {
193 pIfTable = (IP_INTERFACE_INFO*)malloc( dwOutBufLen );
194 }
195
196 DWORD dwRet = ::GetInterfaceInfo( pIfTable, &dwOutBufLen );
197 if ( dwRet == NO_ERROR )
198 {
199 // pIfTable->NumAdapters件のデータが取得できたので、
200 // 該当するアダプタのpIfTable->Adapter[ index ].Nameから
201 // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}の部分を取得
202 }
203
204
205 TCHAR szFileName[ MAX_PATH ];
206 _stprintf( szFileName, _T( "\\\\.\\%s" ), szAdapterName );
207
208 HANDLE hDevice = ::CreateFile( szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
209 NULL, OPEN_EXISTING, 0, NULL);
210
211
212
213
214 #define MAX_BSSID 16
215 #define BUFFER_SIZE ( sizeof(NDIS_WLAN_BSSID) * MAX_BSSID + sizeof(ULONG) )
216
217 NDIS_802_11_BSSID_LIST* pBSSIDList = (NDIS_802_11_BSSID_LIST*)malloc( BUFFER_SIZE );
218 if ( pBSSIDList == NULL )
219 {
220 return NULL;
221 }
222
223 DWORD nBytesReturned;
224 DWORD dwIoControlCode;
225
226 ::ZeroMemory( pBSSIDList, BUFFER_SIZE );
227 dwIoControlCode = OID_802_11_BSSID_LIST_SCAN;
228
229 ::DeviceIoControl( hDevice, IOCTL_NDIS_QUERY_GLOBAL_STATS,
230 &dwIoControlCode, sizeof(dwIoControlCode),
231 (LPVOID)NULL, 0,
232 &nBytesReturned, NULL );
233
234 // wait
235 ::Sleep( 2000 );
236
237 ::ZeroMemory( pBSSIDList, BUFFER_SIZE );
238 dwIoControlCode = OID_802_11_BSSID_LIST;
239
240 if ( ::DeviceIoControl( hDevice, IOCTL_NDIS_QUERY_GLOBAL_STATS,
241 &dwIoControlCode, sizeof(dwIoControlCode),
242 pBSSIDList, sizeof(NDIS_802_11_BSSID_LIST) * MAX_BSSID,
243 &nBytesReturned, NULL ) )
244 {
245 return pBSSIDList;
246 }
247 else
248 {
249 return NULL;
250 }
251
252
253 NDIS_WLAN_BSSID* pBssid= pBSSIDList->Bssid;
254 for( int i = 0; i < pBSSIDList->NumberOfItems; i++ )
255 {
256 // 表示
257 USES_CONVERSION;
258 TRACE( _T("%s: %d\n"), A2T( (LPCSTR)pBssid->Ssid.Ssid ), pBssid->Rssi );
259
260 pBssid = (NDIS_WLAN_BSSID*)( (BYTE*)pBssid + pBssid->Length );
261 }
262#endif
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
ICMP処理関係のヘッダ
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
IP HELPERクラス
Definition: TnbIpHelper.h:58
static INDEX GetMibIndex(LPCSTR lpszAddress)
IPアドレスからMIB-INDEXを取得
Definition: TnbIpHelper.h:67
static bool GetMacAddress(CWorkMem &_mac, LPCSTR lpszAddress)
[取得] MACアドレス取得
Definition: TnbIpHelper.h:154
static bool GetMibIfRow(INDEX dwMibIndex, MIB_IFROW &_tMibIfRow)
[取得] MIB-INDEXの情報取得
Definition: TnbIpHelper.h:110
static bool GetMibIfRow(LPCSTR lpszAddress, MIB_IFROW &_tMibIfRow)
[取得] MIB-INDEXの情報取得
Definition: TnbIpHelper.h:137
void Resize(size_t l)
[設定] サイズ再設定
Definition: TnbDef.h:672
const TYP * Ref(void) const
[取得] ポインタ取得
Definition: TnbDef.h:712
TNB Library
Definition: TnbDoxyTitle.txt:2
void MemCopy(T *_pDst, const void *pSrc, size_t len)
[複製] メモリコピー
Definition: TnbDef.h:376