Changeset 21411 for trunk/src/iphlpapi/iphlpapi.cpp
- Timestamp:
- Aug 12, 2010, 2:04:53 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/iphlpapi/iphlpapi.cpp
r10069 r21411 20 20 21 21 #include <string.h> 22 #include "iphlpapi.h"22 #include <iprtrmib.h> 23 23 #include <winnls.h> 24 24 … … 41 41 #include "iphlwrap.h" 42 42 43 /* from ipexport.h */ 44 typedef ULONG IPAddr; 45 typedef ULONG IPMask; 46 typedef ULONG IP_STATUS; 47 43 48 #pragma pack(1) 44 49 typedef struct … … 94 99 void stringIPAddress(char* dst,u_long data) 95 100 { 96 sprintf(dst, "%u.%u.%u.%u", 101 sprintf(dst, "%u.%u.%u.%u", 97 102 (char)data, 98 103 (char)(*(((char*)&data) + 1)), … … 128 133 #endif 129 134 130 // Init Subsystem and open a socket for ioctl() calls 135 // Init Subsystem and open a socket for ioctl() calls 131 136 sock_init(); 132 137 … … 134 139 dprintf(("IPHLPAPI(Init): Opened socket %d\n", clientSocket)); 135 140 136 // Whole buf minimum size is 65536 and memsets are really needed in other case 141 // Whole buf minimum size is 65536 and memsets are really needed in other case 137 142 // we will get garbage in adapter names. 138 143 … … 146 151 { 147 152 free(buffer); 148 soclose(clientSocket); 149 return; 150 } 153 soclose(clientSocket); 154 return; 155 } 151 156 152 157 rc = ioctl(clientSocket, SIOSTATAT, (char*)buffer, 65536); … … 155 160 { 156 161 free(buffer); 157 soclose(clientSocket); 158 return; 159 } 162 soclose(clientSocket); 163 return; 164 } 160 165 cInterfaces = *(short int*)buffer; 161 166 dprintf(("IPHLPAPI Call returned %d interfaces\n", cInterfaces)); … … 175 180 // if (ifInfo->interfaceNum ==9) continue; 176 181 177 // Allocate and clear mem 182 // Allocate and clear mem 178 183 pipAdapter = (PIP_ADAPTER_INFO)malloc (sizeof (IP_ADAPTER_INFO)); 179 184 memset(pipAdapter, 0, sizeof(IP_ADAPTER_INFO)); 180 if (oldAdapter) 185 if (oldAdapter) 181 186 oldAdapter->Next = pipAdapter; 182 187 … … 184 189 pipAdapter->ComboIndex = 1; 185 190 i = ifInfo->interfaceNum; 186 // Gather some other stats 191 // Gather some other stats 187 192 dprintf(("IPHLPAPI: interface number: %u\n", ifInfo->interfaceNum)); 188 193 … … 195 200 196 201 if (strstr(ifmibget.iftable[i].ifDescr,"back")) // lo 197 strcpy(iShortName,"lo"); 202 strcpy(iShortName,"lo"); 198 203 199 204 if (strstr(ifmibget.iftable[i].ifDescr,"ace ppp")) // pppX 200 strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr,"ppp")); 205 strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr,"ppp")); 201 206 202 207 if (strstr(ifmibget.iftable[i].ifDescr,"ace sl")) // slX 203 strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr,"sl")); 208 strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr,"sl")); 204 209 205 210 if (strstr(ifmibget.iftable[i].ifDescr,"ace dod")) // dodX 206 strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr,"dod")); 211 strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr,"dod")); 207 212 208 213 dprintf(("IPHLPAPI: interface name[%s] : %s\n",iShortName, ifmibget.iftable[i].ifDescr)); 209 214 strcpy(pipAdapter->AdapterName, ifmibget.iftable[i].ifDescr); 210 215 strcpy(pipAdapter->Description, ifmibget.iftable[i].ifDescr); 211 212 pipAdapter->AddressLength = 6; // MAX address 216 217 pipAdapter->AddressLength = 6; // MAX address 213 218 memcpy(pipAdapter->Address,ifmibget.iftable[i].ifPhysAddr,6); 214 219 pipAdapter->Index = ifmibget.iftable[i].ifIndex; … … 217 222 218 223 MultiByteToWideChar(CP_ACP, 0, iShortName, strlen(iShortName), 219 pmibTable->table[currentInterface].wszName, 224 pmibTable->table[currentInterface].wszName, 220 225 MAX_INTERFACE_NAME_LEN); 221 226 222 227 pmibTable->table[currentInterface].dwIndex = ifmibget.iftable[i].ifIndex; 223 228 pmibTable->table[currentInterface].dwType = ifmibget.iftable[i].ifType; /* type of the interface */ … … 265 270 stringNetmask((char*)&iasAdapterIP.IpMask,ifInfo->netmask); 266 271 iasAdapterIP.Context = 0; 267 268 // Now we are going to catch gateways for this interface 269 buffer2 = (char*) malloc(64*1024); 272 273 // Now we are going to catch gateways for this interface 274 buffer2 = (char*) malloc(64*1024); 270 275 memset(buffer2, 0, 65536); 271 276 272 277 rc = ioctl(clientSocket, SIOSTATRT, (char*)buffer2, 65536); 273 278 dprintf(("IPHLPAPI(SIOSTATRT):ioctl returned: %d\n", rc)); 274 279 275 if (rc == -1) 280 if (rc == -1) 276 281 { 277 282 free(buffer); 278 283 free(buffer2); 279 soclose(clientSocket); 284 soclose(clientSocket); 280 285 // better return nothing than some trash, unwinding and freeing 281 286 for (topAdapter; pipAdapter; topAdapter = pipAdapter) { 282 287 pipAdapter = topAdapter -> Next; 283 free(topAdapter); 288 free(topAdapter); 284 289 } 285 return; 286 } 287 290 return; 291 } 292 288 293 rtentries *routeEntries = (rtentries*)buffer2; 289 294 p = (unsigned char *)&routeEntries->rttable[0]; 290 295 291 IP_ADDR_STRING iasGateway; 296 IP_ADDR_STRING iasGateway; 292 297 memset(&iasGateway,0,sizeof(iasGateway)); 293 298 294 299 for (int currentRoute = 0; currentRoute < (routeEntries->hostcount+routeEntries->netcount); currentRoute++) 295 300 { 296 // First check if this route is for our interface 301 // First check if this route is for our interface 297 302 #ifndef TCPV40HDRS 298 303 r = (struct ortentry *) (p); … … 304 309 sin = (struct sockaddr_in *)(&r->rt_dst); 305 310 if (strcmp(inet_ntoa(sin->sin_addr),"0.0.0.0")==0) 306 { 311 { 307 312 iasGateway.Next = NULL; 308 // TODO : Some systems may have many gateways 313 // TODO : Some systems may have many gateways 309 314 sin = (struct sockaddr_in *)(&r->rt_gateway); 310 315 strcpy(iasGateway.IpAddress.String,inet_ntoa(sin->sin_addr)); … … 321 326 p+=strlen((char *)p)+1; 322 327 } 323 328 324 329 memcpy((char*)&pipAdapter->IpAddressList, (char*)&iasAdapterIP, sizeof(iasAdapterIP)); 325 330 pipAdapter->CurrentIpAddress = &pipAdapter->IpAddressList; … … 340 345 if (buffer) free(buffer); 341 346 if (buffer2) free(buffer2); 342 soclose(clientSocket); 347 soclose(clientSocket); 343 348 } 344 349 … … 362 367 { 363 368 PIP_ADDR_STRING pias; 364 369 365 370 // check for sufficient space 366 371 DWORD dwRequired = sizeof( IP_ADAPTER_INFO ); 367 372 368 373 // follow the IP_ADDR_STRING lists 369 374 pias = &piai->IpAddressList; … … 387 392 pias = pias->Next; 388 393 } 389 394 390 395 pias = &piai->PrimaryWinsServer; 391 396 while( pias ) … … 401 406 pias = pias->Next; 402 407 } 403 408 404 409 return dwRequired; 405 410 } … … 417 422 { 418 423 dprintf(("GetAdaptersInfo API called")); 419 dprintf(("Address passed is %p",pAdapterInfo)); 424 dprintf(("Address passed is %p",pAdapterInfo)); 420 425 if (NULL == pOutBufLen) 421 426 return ERROR_INVALID_PARAMETER; 422 427 423 428 // verify first block of memory to write to 424 429 if (IsBadWritePtr(pAdapterInfo, 4)) 425 430 return ERROR_INVALID_PARAMETER; 426 431 427 432 if (NULL == pipAdapter) 428 433 { … … 430 435 i_initializeAdapterInformation(); 431 436 } 432 437 433 438 if (NULL == pipAdapter) 434 439 return ERROR_NO_DATA; 435 440 436 441 // OK, just copy over the information as far as possible 437 442 LONG lSpaceLeft = *pOutBufLen; 438 443 PBYTE pTarget = (PBYTE)pAdapterInfo; 439 444 PIP_ADAPTER_INFO pip; 440 445 441 446 // calculate overall required buffer size 442 447 DWORD dwRequiredBuffer = 0; 443 448 444 449 for( pip = pipAdapter ; pip ; pip = pip->Next ) 445 450 { … … 447 452 dwRequiredBuffer += i_sizeOfIP_ADAPTER_INFO(pip); 448 453 } 449 454 450 455 for( pip = pipAdapter ; pip ; pip = pip->Next ) 451 456 { … … 455 460 lSpaceLeft -= dwRequired; 456 461 if (lSpaceLeft >= 0) 457 { 462 { 458 463 // @PF revised - this thing works because we currently do not support 459 464 // multi-ip, multi-gateway or multi-DHCP servers lists 460 465 // TODO - add lists support 461 466 memcpy(pTarget, pip, sizeof( IP_ADAPTER_INFO )); 462 // point to currentIPAddress 467 // point to currentIPAddress 463 468 ((PIP_ADAPTER_INFO)(pTarget))->CurrentIpAddress = &((PIP_ADAPTER_INFO)(pTarget))->IpAddressList; 464 469 pTarget += sizeof( IP_ADAPTER_INFO ); 465 470 466 471 // i_copyIP_ADDRESS_STRING(&pTarget, &pip->IpAddressList); 467 472 // i_copyIP_ADDRESS_STRING(&pTarget, &pip->GatewayList); … … 488 493 { 489 494 struct sockaddr_in * sin; 490 PFIXED_INFO fi = pFixedInfo; 495 PFIXED_INFO fi = pFixedInfo; 491 496 DWORD memNeeded; 492 497 PIP_ADDR_STRING dnslist = NULL, pdnslist = NULL; … … 495 500 res_init(); 496 501 497 // Check how much mem we will need 502 // Check how much mem we will need 498 503 memNeeded = sizeof(FIXED_INFO)+_res.nscount*sizeof(IP_ADDR_STRING); 499 504 … … 504 509 return ERROR_BUFFER_OVERFLOW; 505 510 } 506 511 507 512 // This is dynamically updated info 508 513 memset(pFixedInfo,0,memNeeded); 509 514 510 515 ODIN_gethostname(fi->HostName,128); 511 516 strcpy(fi->DomainName,_res.defdname); 512 517 513 // Fill in DNS Servers 514 fi->CurrentDnsServer = &fi->DnsServerList; 518 // Fill in DNS Servers 519 fi->CurrentDnsServer = &fi->DnsServerList; 515 520 dnslist = &fi->DnsServerList; 516 521 517 522 for (int i = 0; i<_res.nscount; i++) 518 523 { 519 524 if (pdnslist) pdnslist->Next = dnslist; 520 sin = (struct sockaddr_in *)&_res.nsaddr_list[i]; 525 sin = (struct sockaddr_in *)&_res.nsaddr_list[i]; 521 526 strcpy(dnslist->IpAddress.String,inet_ntoa(sin->sin_addr)); 522 527 dprintf(("IPHLPAPI: GetNetworkParams Adding DNS Server %s",inet_ntoa(sin->sin_addr))); … … 524 529 if ( pdnslist == &fi->DnsServerList) dnslist = (PIP_ADDR_STRING)(fi + 1); 525 530 else dnslist += 1; 526 } 531 } 527 532 fi->EnableDns = 1; 528 533 return ERROR_SUCCESS;
Note:
See TracChangeset
for help on using the changeset viewer.