Ignore:
Timestamp:
Aug 12, 2010, 2:04:53 PM (15 years ago)
Author:
dmik
Message:

Expose the iphlpapi.dll interface at source level through iprtrmib.h/iphlpapi.h.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/iphlpapi/iphlpapi.cpp

    r10069 r21411  
    2020
    2121#include <string.h>
    22 #include "iphlpapi.h"
     22#include <iprtrmib.h>
    2323#include <winnls.h>
    2424
     
    4141#include "iphlwrap.h"
    4242
     43/* from ipexport.h */
     44typedef ULONG  IPAddr;
     45typedef ULONG  IPMask;
     46typedef ULONG  IP_STATUS;
     47
    4348#pragma pack(1)
    4449typedef struct
     
    9499void stringIPAddress(char* dst,u_long data)
    95100{
    96     sprintf(dst, "%u.%u.%u.%u", 
     101    sprintf(dst, "%u.%u.%u.%u",
    97102                        (char)data,
    98103                        (char)(*(((char*)&data) + 1)),
     
    128133#endif
    129134
    130   // Init Subsystem and open a socket for ioctl() calls 
     135  // Init Subsystem and open a socket for ioctl() calls
    131136  sock_init();
    132137
     
    134139  dprintf(("IPHLPAPI(Init): Opened socket %d\n", clientSocket));
    135140
    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
    137142  // we will get garbage in adapter names.
    138143
     
    146151  {
    147152    free(buffer);
    148     soclose(clientSocket);         
    149     return; 
    150   }       
     153    soclose(clientSocket);
     154    return;
     155  }
    151156
    152157  rc = ioctl(clientSocket, SIOSTATAT, (char*)buffer, 65536);
     
    155160  {
    156161    free(buffer);
    157     soclose(clientSocket);         
    158     return; 
    159   }       
     162    soclose(clientSocket);
     163    return;
     164  }
    160165  cInterfaces = *(short int*)buffer;
    161166  dprintf(("IPHLPAPI Call returned %d interfaces\n", cInterfaces));
     
    175180//    if (ifInfo->interfaceNum ==9) continue;
    176181
    177     // Allocate and clear mem 
     182    // Allocate and clear mem
    178183    pipAdapter = (PIP_ADAPTER_INFO)malloc (sizeof (IP_ADAPTER_INFO));
    179184    memset(pipAdapter, 0, sizeof(IP_ADAPTER_INFO));
    180     if (oldAdapter) 
     185    if (oldAdapter)
    181186       oldAdapter->Next = pipAdapter;
    182187
     
    184189    pipAdapter->ComboIndex = 1;
    185190    i =  ifInfo->interfaceNum;
    186     // Gather some other stats 
     191    // Gather some other stats
    187192    dprintf(("IPHLPAPI: interface number: %u\n", ifInfo->interfaceNum));
    188193
     
    195200
    196201    if (strstr(ifmibget.iftable[i].ifDescr,"back")) // lo
    197         strcpy(iShortName,"lo");       
     202        strcpy(iShortName,"lo");
    198203
    199204    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"));
    201206
    202207    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"));
    204209
    205210    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"));
    207212
    208213    dprintf(("IPHLPAPI: interface name[%s] : %s\n",iShortName, ifmibget.iftable[i].ifDescr));
    209214    strcpy(pipAdapter->AdapterName, ifmibget.iftable[i].ifDescr);
    210215    strcpy(pipAdapter->Description, ifmibget.iftable[i].ifDescr);
    211          
    212     pipAdapter->AddressLength = 6; // MAX address 
     216
     217    pipAdapter->AddressLength = 6; // MAX address
    213218    memcpy(pipAdapter->Address,ifmibget.iftable[i].ifPhysAddr,6);
    214219    pipAdapter->Index = ifmibget.iftable[i].ifIndex;
     
    217222
    218223    MultiByteToWideChar(CP_ACP, 0, iShortName, strlen(iShortName),
    219                         pmibTable->table[currentInterface].wszName, 
     224                        pmibTable->table[currentInterface].wszName,
    220225                        MAX_INTERFACE_NAME_LEN);
    221  
     226
    222227    pmibTable->table[currentInterface].dwIndex = ifmibget.iftable[i].ifIndex;
    223228    pmibTable->table[currentInterface].dwType  = ifmibget.iftable[i].ifType;         /* type of the interface   */
     
    265270    stringNetmask((char*)&iasAdapterIP.IpMask,ifInfo->netmask);
    266271    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);
    270275    memset(buffer2, 0, 65536);
    271    
     276
    272277    rc = ioctl(clientSocket, SIOSTATRT, (char*)buffer2, 65536);
    273278    dprintf(("IPHLPAPI(SIOSTATRT):ioctl returned: %d\n", rc));
    274279
    275     if (rc == -1) 
     280    if (rc == -1)
    276281    {
    277282      free(buffer);
    278283      free(buffer2);
    279       soclose(clientSocket);       
     284      soclose(clientSocket);
    280285      // better return nothing than some trash, unwinding and freeing
    281286      for (topAdapter; pipAdapter; topAdapter = pipAdapter) {
    282287          pipAdapter = topAdapter -> Next;
    283           free(topAdapter); 
     288          free(topAdapter);
    284289      }
    285       return; 
    286      }       
    287          
     290      return;
     291     }
     292
    288293     rtentries *routeEntries = (rtentries*)buffer2;
    289294     p = (unsigned char *)&routeEntries->rttable[0];
    290295
    291      IP_ADDR_STRING iasGateway;   
     296     IP_ADDR_STRING iasGateway;
    292297     memset(&iasGateway,0,sizeof(iasGateway));
    293298
    294299     for (int currentRoute = 0; currentRoute < (routeEntries->hostcount+routeEntries->netcount); currentRoute++)
    295300     {
    296      // First check if this route is for our interface   
     301     // First check if this route is for our interface
    297302#ifndef TCPV40HDRS
    298303         r = (struct ortentry *) (p);
     
    304309            sin = (struct sockaddr_in *)(&r->rt_dst);
    305310            if (strcmp(inet_ntoa(sin->sin_addr),"0.0.0.0")==0)
    306             { 
     311            {
    307312               iasGateway.Next = NULL;
    308                // TODO : Some systems may have many gateways   
     313               // TODO : Some systems may have many gateways
    309314               sin = (struct sockaddr_in *)(&r->rt_gateway);
    310315               strcpy(iasGateway.IpAddress.String,inet_ntoa(sin->sin_addr));
     
    321326       p+=strlen((char *)p)+1;
    322327    }
    323  
     328
    324329  memcpy((char*)&pipAdapter->IpAddressList, (char*)&iasAdapterIP, sizeof(iasAdapterIP));
    325330  pipAdapter->CurrentIpAddress = &pipAdapter->IpAddressList;
     
    340345 if (buffer) free(buffer);
    341346 if (buffer2) free(buffer2);
    342  soclose(clientSocket); 
     347 soclose(clientSocket);
    343348}
    344349
     
    362367{
    363368  PIP_ADDR_STRING pias;
    364  
     369
    365370  // check for sufficient space
    366371  DWORD dwRequired = sizeof( IP_ADAPTER_INFO );
    367    
     372
    368373  // follow the IP_ADDR_STRING lists
    369374  pias = &piai->IpAddressList;
     
    387392    pias = pias->Next;
    388393  }
    389    
     394
    390395  pias = &piai->PrimaryWinsServer;
    391396  while( pias )
     
    401406    pias = pias->Next;
    402407  }
    403  
     408
    404409  return dwRequired;
    405410}
     
    417422{
    418423  dprintf(("GetAdaptersInfo API called"));
    419   dprintf(("Address passed is %p",pAdapterInfo)); 
     424  dprintf(("Address passed is %p",pAdapterInfo));
    420425  if (NULL == pOutBufLen)
    421426    return ERROR_INVALID_PARAMETER;
    422  
     427
    423428  // verify first block of memory to write to
    424429  if (IsBadWritePtr(pAdapterInfo, 4))
    425430    return ERROR_INVALID_PARAMETER;
    426  
     431
    427432  if (NULL == pipAdapter)
    428433  {
     
    430435    i_initializeAdapterInformation();
    431436  }
    432  
     437
    433438  if (NULL == pipAdapter)
    434439    return ERROR_NO_DATA;
    435  
     440
    436441  // OK, just copy over the information as far as possible
    437442  LONG  lSpaceLeft     = *pOutBufLen;
    438443  PBYTE pTarget        = (PBYTE)pAdapterInfo;
    439444  PIP_ADAPTER_INFO pip;
    440  
     445
    441446  // calculate overall required buffer size
    442447  DWORD dwRequiredBuffer = 0;
    443  
     448
    444449  for( pip = pipAdapter ; pip ; pip = pip->Next )
    445450  {
     
    447452    dwRequiredBuffer += i_sizeOfIP_ADAPTER_INFO(pip);
    448453  }
    449  
     454
    450455  for( pip = pipAdapter ; pip ; pip = pip->Next )
    451456  {
     
    455460    lSpaceLeft -= dwRequired;
    456461    if (lSpaceLeft >= 0)
    457     {     
     462    {
    458463      // @PF revised - this thing works because we currently do not support
    459464      // multi-ip, multi-gateway or multi-DHCP servers lists
    460465      // TODO - add lists support
    461466      memcpy(pTarget, pip, sizeof( IP_ADAPTER_INFO ));
    462       // point to currentIPAddress 
     467      // point to currentIPAddress
    463468      ((PIP_ADAPTER_INFO)(pTarget))->CurrentIpAddress = &((PIP_ADAPTER_INFO)(pTarget))->IpAddressList;
    464469      pTarget += sizeof( IP_ADAPTER_INFO );
    465        
     470
    466471//      i_copyIP_ADDRESS_STRING(&pTarget, &pip->IpAddressList);
    467472//      i_copyIP_ADDRESS_STRING(&pTarget, &pip->GatewayList);
     
    488493{
    489494  struct sockaddr_in * sin;
    490   PFIXED_INFO fi = pFixedInfo; 
     495  PFIXED_INFO fi = pFixedInfo;
    491496  DWORD memNeeded;
    492497  PIP_ADDR_STRING dnslist = NULL, pdnslist = NULL;
     
    495500  res_init();
    496501
    497   // Check how much mem we will need 
     502  // Check how much mem we will need
    498503  memNeeded = sizeof(FIXED_INFO)+_res.nscount*sizeof(IP_ADDR_STRING);
    499504
     
    504509   return ERROR_BUFFER_OVERFLOW;
    505510  }
    506  
     511
    507512  // This is dynamically updated info
    508513  memset(pFixedInfo,0,memNeeded);
    509  
     514
    510515  ODIN_gethostname(fi->HostName,128);
    511516  strcpy(fi->DomainName,_res.defdname);
    512517
    513   // Fill in DNS Servers 
    514   fi->CurrentDnsServer = &fi->DnsServerList;   
     518  // Fill in DNS Servers
     519  fi->CurrentDnsServer = &fi->DnsServerList;
    515520  dnslist = &fi->DnsServerList;
    516    
     521
    517522  for (int i = 0; i<_res.nscount; i++)
    518523  {
    519524      if (pdnslist) pdnslist->Next = dnslist;
    520       sin = (struct sockaddr_in *)&_res.nsaddr_list[i];               
     525      sin = (struct sockaddr_in *)&_res.nsaddr_list[i];
    521526      strcpy(dnslist->IpAddress.String,inet_ntoa(sin->sin_addr));
    522527      dprintf(("IPHLPAPI: GetNetworkParams Adding DNS Server %s",inet_ntoa(sin->sin_addr)));
     
    524529      if ( pdnslist == &fi->DnsServerList) dnslist = (PIP_ADDR_STRING)(fi + 1);
    525530      else dnslist += 1;
    526   }     
     531  }
    527532  fi->EnableDns = 1;
    528533  return ERROR_SUCCESS;
Note: See TracChangeset for help on using the changeset viewer.