Changeset 21414


Ignore:
Timestamp:
Aug 13, 2010, 8:14:33 PM (15 years ago)
Author:
dmik
Message:

iphlpapi: Added support for multiple IPs and gateways per interface and cleaned up the code.

File:
1 edited

Legend:

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

    r21412 r21414  
    4747
    4848#pragma pack(1)
     49
    4950typedef struct
    5051{
    5152        unsigned long IPAddress;
    52         unsigned short interfaceNum;
     53        unsigned short interfaceIndex;
    5354        unsigned long netmask;
    5455        unsigned long broadcastAddress;
    55 } interfaceInformation;
    56 
    57 /* Incomplete but will do for us */
    58 
    59 #ifndef TCPV40HDRS
    60 struct ifnet {
    61         char    *if_name;               /* name, e.g. ``en'' or ``lo'' */
    62         short   if_unit;                /* sub-unit for lower level driver */
    63         short   if_mtu;                 /* maximum transmission unit */
    64 };
    65 #endif
    66 
    67 struct ilist {
    68   struct ilist *next;
    69   unsigned long addr;
    70 };
    71 
    72 struct rlist {
    73   struct rlist *next;
    74   unsigned long gate;
    75   unsigned long net;
    76   unsigned long mask;
    77   char done;
    78 };
     56}
     57AddrInfo;
    7958
    8059#pragma pack()
     
    10887static void i_initializeAdapterInformation(void)
    10988{
    110   unsigned char *p;
    111   char iShortName[6];
    112   struct rlist * rnode = NULL, * rdef = NULL;
    113   PIP_ADAPTER_INFO oldAdapter = NULL,topAdapter = NULL;
    114   struct sockaddr_in * sin;
    115   int rc,i;
    116   char *buffer=NULL,*buffer2=NULL;
    117   struct ifnet *pifnet;
    118   struct ifmib  ifmibget;
    119   int cInterfaces;
    120 #ifndef TCPV40HDRS
    121   struct ortentry *r;
    122 #else
    123   struct rtentry *r;
    124 #endif
    125 
    126   // Init Subsystem and open a socket for ioctl() calls
    127   sock_init();
    128 
    129   int clientSocket = socket(PF_INET, SOCK_STREAM, 0);
    130   dprintf(("IPHLPAPI(Init): Opened socket %d\n", clientSocket));
    131 
    132   // Whole buf minimum size is 65536 and memsets are really needed in other case
    133   // we will get garbage in adapter names.
    134 
    135   buffer = (char*)malloc(64 * 1024);
    136   memset(buffer, 0, 65536);
    137   memset(&ifmibget,0, sizeof(struct ifmib));
    138 
    139   rc = ioctl(clientSocket, SIOSTATIF, (char*)&ifmibget, sizeof(struct ifmib));
    140   dprintf(("IPHLPAPI(SIOSTATIF) ioctl returned: %d\n", rc));
    141   if (rc == -1)
    142   {
     89    char iShortName[8];
     90    PIP_ADAPTER_INFO oldAdapter = NULL, topAdapter = NULL;
     91    int rc;
     92    char *buffer = NULL, *buffer2 = NULL;
     93    struct ifmib  ifmibget;
     94    int cAddresses;
     95    AddrInfo *addrInfo;
     96    struct rtentries *rtentries;
     97
     98    // Init Subsystem and open a socket for ioctl() calls
     99    sock_init();
     100
     101    int clientSocket = socket(PF_INET, SOCK_STREAM, 0);
     102    dprintf(("IPHLPAPI: Init: Opened socket %d\n", clientSocket));
     103
     104    // Whole buf minimum size is 65536 and memsets are really needed in other case
     105    // we will get garbage in adapter names.
     106
     107    memset(&ifmibget,0, sizeof(struct ifmib));
     108    buffer = (char*) malloc(65536);
     109    memset(buffer, 0, 65536);
     110    buffer2 = (char*) malloc(65536);
     111    memset(buffer2, 0, 65536);
     112
     113    rc = ioctl(clientSocket, SIOSTATIF, (char*)&ifmibget, sizeof(struct ifmib));
     114    dprintf(("IPHLPAPI: ioctl(SIOSTATIF) returned: %d\n", rc));
     115    if (rc == -1)
     116    {
     117        free(buffer2);
     118        free(buffer);
     119        soclose(clientSocket);
     120        return;
     121    }
     122    dprintf(("IPHLPAPI: ioctl(SIOSTATIF) returned %d interfaces\n", ifmibget.ifNumber));
     123
     124    rc = ioctl(clientSocket, SIOSTATAT, buffer, 65536);
     125    dprintf(("IPHLPAPI: ioctl(SIOSTATAT) returned: %d\n", rc));
     126    if (rc == -1)
     127    {
     128        free(buffer2);
     129        free(buffer);
     130        soclose(clientSocket);
     131        return;
     132    }
     133    cAddresses = *(short int *) buffer;
     134    addrInfo = (AddrInfo *) (buffer + sizeof(short int));
     135    dprintf(("IPHLPAPI: ioctl(SIOSTATAT) returned %d addresses\n", cAddresses));
     136
     137    rc = ioctl(clientSocket, SIOSTATRT, buffer2, 65536);
     138    dprintf(("IPHLPAPI: ioctl(SIOSTATRT) returned: %d\n", rc));
     139    if (rc == -1)
     140    {
     141        free(buffer2);
     142        free(buffer);
     143        soclose(clientSocket);
     144        return;
     145    }
     146    rtentries = (struct rtentries *) buffer2;
     147    dprintf(("IPHLPAPI: ioctl(SIOSTATRT) returned %d host and %d net routes\n",
     148             rtentries->hostcount, rtentries->netcount));
     149
     150    pmibTable = (PMIB_IFTABLE) malloc(ifmibget.ifNumber * sizeof(MIB_IFTABLE));
     151    memset(pmibTable, 0, ifmibget.ifNumber * sizeof(MIB_IFTABLE));
     152    pmibTable->dwNumEntries = ifmibget.ifNumber;
     153
     154    pmipaddrTable = (PMIB_IPADDRTABLE) malloc(cAddresses * sizeof(MIB_IPADDRTABLE));
     155    memset(pmipaddrTable, 0, cAddresses * sizeof(MIB_IPADDRTABLE));
     156    pmipaddrTable->dwNumEntries = cAddresses;
     157
     158    // loop over interfaces
     159    int idx, i;
     160    for (i = idx = 0; i < IFMIB_ENTRIES && idx < ifmibget.ifNumber; ++i)
     161    {
     162        // skip empty interface entries
     163        if (ifmibget.iftable[i].ifType == 0)
     164            continue;
     165
     166        short ifIndex = ifmibget.iftable[i].ifIndex;
     167        dprintf(("IPHLPAPI: interface index: %u\n", ifIndex));
     168
     169        // Guess the symbolic interface name. I do not like this very much, but
     170        // seems there is no other documented way
     171
     172        if (ifIndex >= 0 && ifIndex < 9) // lanX
     173        {
     174            strcpy(iShortName,"lan");
     175            iShortName[3] = ifIndex + 48;
     176            iShortName[4] = 0;
     177        }
     178        else
     179        if (strstr(ifmibget.iftable[i].ifDescr, "back")) // loopback
     180        {
     181            strcpy(iShortName,"lo");
     182        }
     183        else
     184        if (strstr(ifmibget.iftable[i].ifDescr, "ace ppp")) // pppX
     185        {
     186            strcpy(iShortName, strstr(ifmibget.iftable[i].ifDescr, "ppp"));
     187        }
     188        else
     189        if (strstr(ifmibget.iftable[i].ifDescr,"ace sl")) // slX
     190        {
     191            strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr, "sl"));
     192        }
     193        else
     194        if (strstr(ifmibget.iftable[i].ifDescr,"ace dod")) // dodX
     195        {
     196            strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr, "dod"));
     197        }
     198        else // something else...
     199        {
     200            strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr,"unk"));
     201            iShortName[3] = ifIndex + 48;
     202            iShortName[4] = 0;
     203        }
     204
     205        dprintf(("IPHLPAPI: interface name: %s [%s]\n", iShortName,
     206                 ifmibget.iftable[i].ifDescr));
     207
     208        // Allocate the adapter info entry
     209        pipAdapter = (PIP_ADAPTER_INFO)malloc (sizeof (IP_ADAPTER_INFO));
     210        memset(pipAdapter, 0, sizeof(IP_ADAPTER_INFO));
     211        if (oldAdapter)
     212           oldAdapter->Next = pipAdapter;
     213
     214        // Fill the adapter info entry
     215        pipAdapter->Next = NULL;
     216        pipAdapter->ComboIndex = 1; // unused according to MSDN
     217        strcpy(pipAdapter->AdapterName, ifmibget.iftable[i].ifDescr);
     218        strcpy(pipAdapter->Description, ifmibget.iftable[i].ifDescr);
     219
     220        pipAdapter->AddressLength = sizeof(ifmibget.iftable[i].ifPhysAddr);
     221        memcpy(pipAdapter->Address,ifmibget.iftable[i].ifPhysAddr, sizeof(ifmibget.iftable[i].ifPhysAddr));
     222        pipAdapter->Index = ifIndex;
     223        pipAdapter->Type = ifmibget.iftable[i].ifType; // Careful with this (?)
     224
     225        // what about OS/2 DHCP?
     226        pipAdapter->DhcpEnabled = 0; // Also a question
     227        memset((char*)&pipAdapter->DhcpServer, 0, sizeof(IP_ADDR_STRING));
     228
     229        pipAdapter->HaveWins = 0;
     230        memset((char*)&pipAdapter->PrimaryWinsServer, 0, sizeof(IP_ADDR_STRING));
     231        memset((char*)&pipAdapter->SecondaryWinsServer, 0, sizeof(IP_ADDR_STRING));
     232        pipAdapter->LeaseObtained = 0;
     233        pipAdapter->LeaseExpires = 0;
     234
     235        // Fill the interface table entry
     236        MultiByteToWideChar(CP_ACP, 0, iShortName, strlen(iShortName),
     237                            pmibTable->table[idx].wszName,
     238                            MAX_INTERFACE_NAME_LEN);
     239
     240        pmibTable->table[idx].dwIndex = ifIndex;
     241        pmibTable->table[idx].dwType  = ifmibget.iftable[i].ifType;
     242        pmibTable->table[idx].dwMtu   = ifmibget.iftable[i].ifMtu;
     243        pmibTable->table[idx].dwSpeed = ifmibget.iftable[i].ifSpeed;
     244
     245        pmibTable->table[idx].dwPhysAddrLen = sizeof(ifmibget.iftable[i].ifPhysAddr);
     246        memcpy(pmibTable->table[idx].bPhysAddr, ifmibget.iftable[i].ifPhysAddr,
     247               sizeof(ifmibget.iftable[i].ifPhysAddr));
     248
     249        pmibTable->table[idx].dwAdminStatus =
     250            (ifmibget.iftable[i].ifOperStatus == IFF_UP) ?
     251            MIB_IF_ADMIN_STATUS_UP : MIB_IF_ADMIN_STATUS_DOWN;
     252        pmibTable->table[idx].dwOperStatus =
     253            (ifmibget.iftable[i].ifOperStatus == IFF_UP) ?
     254            MIB_IF_OPER_STATUS_OPERATIONAL : MIB_IF_OPER_STATUS_NON_OPERATIONAL;
     255
     256        pmibTable->table[idx].dwLastChange = ifmibget.iftable[i].ifLastChange;
     257        pmibTable->table[idx].dwInOctets   = ifmibget.iftable[i].ifInOctets;
     258        pmibTable->table[idx].dwInUcastPkts = ifmibget.iftable[i].ifInUcastPkts;
     259        pmibTable->table[idx].dwInNUcastPkts = ifmibget.iftable[i].ifInNUcastPkts;
     260        pmibTable->table[idx].dwInDiscards = ifmibget.iftable[i].ifInDiscards;
     261        pmibTable->table[idx].dwInErrors = ifmibget.iftable[i].ifInErrors;
     262        pmibTable->table[idx].dwInUnknownProtos = ifmibget.iftable[i].ifInUnknownProtos;
     263        pmibTable->table[idx].dwOutOctets = ifmibget.iftable[i].ifOutOctets;
     264        pmibTable->table[idx].dwOutUcastPkts = ifmibget.iftable[i].ifOutUcastPkts;
     265        pmibTable->table[idx].dwOutNUcastPkts = ifmibget.iftable[i].ifOutNUcastPkts;
     266        pmibTable->table[idx].dwOutDiscards = ifmibget.iftable[i].ifOutDiscards;
     267        pmibTable->table[idx].dwOutErrors = ifmibget.iftable[i].ifOutErrors;
     268        pmibTable->table[idx].dwOutQLen = 0; // unused according to MSDN
     269
     270        pmibTable->table[idx].dwDescrLen = strlen(ifmibget.iftable[i].ifDescr);
     271        strncpy((char *)pmibTable->table[idx].bDescr, ifmibget.iftable[i].ifDescr,
     272                sizeof(pmibTable->table[idx].bDescr));
     273
     274        // fill pipAdapter->IpAddressList
     275        int cIfAddresses = 0;
     276        for (int j = 0; j < cAddresses; ++j)
     277        {
     278            if (addrInfo[j].interfaceIndex == ifIndex)
     279            {
     280                ++cIfAddresses;
     281
     282                IP_ADDR_STRING *addr;
     283                if (cIfAddresses == 1)
     284                {
     285                    addr = &pipAdapter->IpAddressList;
     286                }
     287                else
     288                {
     289                    addr->Next = (IP_ADDR_STRING*) malloc(sizeof(IP_ADDR_STRING));
     290                    addr = addr->Next;
     291                }
     292
     293                memset((char *) addr, 0, sizeof(IP_ADDR_STRING));
     294                addr->Next = NULL;
     295                stringIPAddress(addr->IpAddress.String, addrInfo[j].IPAddress);
     296                // mask is in network byte order for some reason
     297                stringIPAddress(addr->IpMask.String, ntohl(addrInfo[j].netmask));
     298                addr->Context = 0;
     299            }
     300        }
     301
     302        // fill pipAdapter->GatewayList
     303        int cIfGateways = 0;
     304        struct rtentry *rtentry = rtentries->rttable;
     305        for (j = 0; j < rtentries->hostcount + rtentries->netcount; ++j)
     306        {
     307            // only take default gateways for this interface
     308            if (strcmp(rtentry->rt_ifp->if_name, iShortName) == 0 &&
     309                ((struct sockaddr_in *)(&rtentry->rt_dst))->sin_addr.s_addr == 0)
     310            {
     311                ++cIfGateways;
     312
     313                IP_ADDR_STRING *addr;
     314                if (cIfGateways == 1)
     315                {
     316                    addr = &pipAdapter->GatewayList;
     317                }
     318                else
     319                {
     320                    addr->Next = (IP_ADDR_STRING *) malloc(sizeof(IP_ADDR_STRING));
     321                    addr = addr->Next;
     322                }
     323
     324                memset((char *) addr, 0, sizeof(IP_ADDR_STRING));
     325                addr->Next = NULL;
     326                struct sockaddr_in * sin =
     327                    (struct sockaddr_in *)(&rtentry->rt_gateway);
     328                strcpy((char *) &addr->IpAddress.String, inet_ntoa(sin->sin_addr));
     329                strcpy((char *) &addr->IpMask.String, "255.255.255.255");
     330                addr->Context = 0;
     331            }
     332
     333            // For some strange reason, the definition of struct rtentries
     334            // is wrong. Each entry in the rtentries::rttable array is followed
     335            // by the interface name. Compensate for that.
     336            ++rtentry;
     337            rtentry = (struct rtentry *)
     338                (((char *) rtentry) + strlen(((char *) rtentry)) + 1);
     339        }
     340
     341
     342        // loop over
     343        if (!topAdapter)
     344            topAdapter = pipAdapter;
     345        oldAdapter =
     346            pipAdapter;
     347
     348        ++idx;
     349    }
     350    pipAdapter = topAdapter;
     351
     352    // loop over addressees to fill pmipaddrTable
     353    for (i = 0; i < cAddresses; ++i)
     354    {
     355        // skip addresses referring to empty interface entries
     356        if (addrInfo[i].interfaceIndex >= IFMIB_ENTRIES ||
     357            ifmibget.iftable[addrInfo[i].interfaceIndex].ifType == 0)
     358            continue;
     359
     360        pmipaddrTable->table[i].dwAddr = addrInfo[i].IPAddress;
     361        pmipaddrTable->table[i].dwIndex = addrInfo[i].interfaceIndex;
     362        // mask is in network byte order for some reason
     363        pmipaddrTable->table[i].dwMask = ntohl(addrInfo[i].netmask);
     364        pmipaddrTable->table[i].dwBCastAddr = addrInfo[i].broadcastAddress;
     365        pmipaddrTable->table[i].dwReasmSize = 0; // ?
     366    }
     367
     368    // current address is the first address so far
     369    pipAdapter->CurrentIpAddress = &pipAdapter->IpAddressList;
     370
     371    // Cleanup
     372    soclose(clientSocket);
     373    free(buffer2);
    143374    free(buffer);
    144     soclose(clientSocket);
    145     return;
    146   }
    147 
    148   rc = ioctl(clientSocket, SIOSTATAT, (char*)buffer, 65536);
    149   dprintf(("IPHLPAPI(SIOSTATAT) ioctl returned: %d\n", rc));
    150   if (rc == -1)
    151   {
    152     free(buffer);
    153     soclose(clientSocket);
    154     return;
    155   }
    156   cInterfaces = *(short int*)buffer;
    157   dprintf(("IPHLPAPI Call returned %d interfaces\n", cInterfaces));
    158 
    159   pmibTable = (PMIB_IFTABLE)malloc(cInterfaces*sizeof(MIB_IFTABLE));
    160   memset(pmibTable, 0, cInterfaces*sizeof(MIB_IFTABLE));
    161   pmibTable->dwNumEntries = cInterfaces;
    162 
    163   pmipaddrTable = (PMIB_IPADDRTABLE)malloc(cInterfaces*sizeof(MIB_IPADDRTABLE));
    164   memset(pmipaddrTable, 0, cInterfaces*sizeof(MIB_IPADDRTABLE));
    165   pmipaddrTable->dwNumEntries = cInterfaces;
    166 
    167   for (int currentInterface = 0; currentInterface < cInterfaces; currentInterface++)
    168   {
    169     interfaceInformation *ifInfo = (interfaceInformation*)(buffer + 2 + (currentInterface * sizeof(interfaceInformation)));
    170     // For now let's dump only lanX and pppX adapters, loopback (?)
    171 //    if (ifInfo->interfaceNum ==9) continue;
    172 
    173     // Allocate and clear mem
    174     pipAdapter = (PIP_ADAPTER_INFO)malloc (sizeof (IP_ADAPTER_INFO));
    175     memset(pipAdapter, 0, sizeof(IP_ADAPTER_INFO));
    176     if (oldAdapter)
    177        oldAdapter->Next = pipAdapter;
    178 
    179     pipAdapter->Next = NULL;
    180     pipAdapter->ComboIndex = 1;
    181     i =  ifInfo->interfaceNum;
    182     // Gather some other stats
    183     dprintf(("IPHLPAPI: interface number: %u\n", ifInfo->interfaceNum));
    184 
    185     if (ifInfo->interfaceNum>=0 && ifInfo->interfaceNum<9) // lanX
    186        { strcpy(iShortName,"lan"); iShortName[3] = ifInfo->interfaceNum+48;
    187          iShortName[4] = 0;}
    188 
    189     // I do not like this very much, but seems there is no other documented
    190     // way
    191 
    192     if (strstr(ifmibget.iftable[i].ifDescr,"back")) // lo
    193         strcpy(iShortName,"lo");
    194 
    195     if (strstr(ifmibget.iftable[i].ifDescr,"ace ppp")) // pppX
    196         strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr,"ppp"));
    197 
    198     if (strstr(ifmibget.iftable[i].ifDescr,"ace sl")) // slX
    199         strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr,"sl"));
    200 
    201     if (strstr(ifmibget.iftable[i].ifDescr,"ace dod")) // dodX
    202         strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr,"dod"));
    203 
    204     dprintf(("IPHLPAPI: interface name[%s] : %s\n",iShortName, ifmibget.iftable[i].ifDescr));
    205     strcpy(pipAdapter->AdapterName, ifmibget.iftable[i].ifDescr);
    206     strcpy(pipAdapter->Description, ifmibget.iftable[i].ifDescr);
    207 
    208     pipAdapter->AddressLength = 6; // MAX address
    209     memcpy(pipAdapter->Address,ifmibget.iftable[i].ifPhysAddr,6);
    210     pipAdapter->Index = ifmibget.iftable[i].ifIndex;
    211     pipAdapter->Type = ifmibget.iftable[i].ifType; // Careful with this (?)
    212     pipAdapter->DhcpEnabled = 0; // Also a question
    213 
    214     MultiByteToWideChar(CP_ACP, 0, iShortName, strlen(iShortName),
    215                         pmibTable->table[currentInterface].wszName,
    216                         MAX_INTERFACE_NAME_LEN);
    217 
    218     pmibTable->table[currentInterface].dwIndex = ifmibget.iftable[i].ifIndex;
    219     pmibTable->table[currentInterface].dwType  = ifmibget.iftable[i].ifType;         /* type of the interface   */
    220     pmibTable->table[currentInterface].dwMtu   = ifmibget.iftable[i].ifMtu;          /* MTU of the interface   */
    221     pmibTable->table[currentInterface].dwSpeed = ifmibget.iftable[i].ifSpeed;
    222 
    223     pmibTable->table[currentInterface].dwPhysAddrLen = 0; //??
    224     memcpy(pmibTable->table[currentInterface].bPhysAddr, ifmibget.iftable[i].ifPhysAddr, sizeof(ifmibget.iftable[i].ifPhysAddr));
    225 
    226     pmibTable->table[currentInterface].dwAdminStatus = ifmibget.iftable[i].ifOperStatus;
    227     pmibTable->table[currentInterface].dwOperStatus = (ifmibget.iftable[i].ifOperStatus == IFF_UP) ? MIB_IF_OPER_STATUS_OPERATIONAL : MIB_IF_OPER_STATUS_NON_OPERATIONAL;
    228 
    229     pmibTable->table[currentInterface].dwLastChange = ifmibget.iftable[i].ifLastChange;
    230     pmibTable->table[currentInterface].dwInOctets   = ifmibget.iftable[i].ifInOctets;
    231     pmibTable->table[currentInterface].dwInUcastPkts = ifmibget.iftable[i].ifInUcastPkts;
    232     pmibTable->table[currentInterface].dwInNUcastPkts = ifmibget.iftable[i].ifInNUcastPkts;
    233     pmibTable->table[currentInterface].dwInDiscards = ifmibget.iftable[i].ifInDiscards;
    234     pmibTable->table[currentInterface].dwInErrors = ifmibget.iftable[i].ifInErrors;
    235     pmibTable->table[currentInterface].dwInUnknownProtos = ifmibget.iftable[i].ifInUnknownProtos;
    236     pmibTable->table[currentInterface].dwOutOctets = ifmibget.iftable[i].ifOutOctets;
    237     pmibTable->table[currentInterface].dwOutUcastPkts = ifmibget.iftable[i].ifOutUcastPkts;
    238     pmibTable->table[currentInterface].dwOutNUcastPkts = ifmibget.iftable[i].ifOutNUcastPkts;
    239     pmibTable->table[currentInterface].dwOutDiscards = ifmibget.iftable[i].ifOutDiscards;
    240     pmibTable->table[currentInterface].dwOutErrors = ifmibget.iftable[i].ifOutErrors;
    241 //    pmibTable->table[currentInterface].dwOutQLen
    242 
    243     pmibTable->table[currentInterface].dwDescrLen = strlen(ifmibget.iftable[i].ifDescr);
    244     strncpy((char *)pmibTable->table[currentInterface].bDescr, ifmibget.iftable[i].ifDescr, sizeof(pmibTable->table[currentInterface].bDescr));
    245 
    246 
    247     pmipaddrTable->table[currentInterface].dwAddr = ifInfo->IPAddress;
    248     // mask is in network byte order for some reason
    249     pmipaddrTable->table[currentInterface].dwMask = ntohl(ifInfo->netmask);
    250     pmipaddrTable->table[currentInterface].dwBCastAddr = ifInfo->broadcastAddress;
    251     pmipaddrTable->table[currentInterface].dwIndex = ifmibget.iftable[i].ifIndex;
    252 
    253     /* MTU of the interface   */
    254 
    255 
    256 
    257     // TODO: Adapter may support multiple IP addrs
    258     IP_ADDR_STRING iasAdapterIP;
    259     iasAdapterIP.Next = NULL;
    260     stringIPAddress((char*)&iasAdapterIP.IpAddress,ifInfo->IPAddress);
    261     stringIPAddress((char*)&iasAdapterIP.IpMask,ntohl(ifInfo->netmask));
    262     iasAdapterIP.Context = 0;
    263 
    264     // Now we are going to catch gateways for this interface
    265     buffer2 = (char*) malloc(64*1024);
    266     memset(buffer2, 0, 65536);
    267 
    268     rc = ioctl(clientSocket, SIOSTATRT, (char*)buffer2, 65536);
    269     dprintf(("IPHLPAPI(SIOSTATRT):ioctl returned: %d\n", rc));
    270 
    271     if (rc == -1)
    272     {
    273       free(buffer);
    274       free(buffer2);
    275       soclose(clientSocket);
    276       // better return nothing than some trash, unwinding and freeing
    277       for (topAdapter; pipAdapter; topAdapter = pipAdapter) {
    278           pipAdapter = topAdapter -> Next;
    279           free(topAdapter);
    280       }
    281       return;
    282      }
    283 
    284      rtentries *routeEntries = (rtentries*)buffer2;
    285      p = (unsigned char *)&routeEntries->rttable[0];
    286 
    287      IP_ADDR_STRING iasGateway;
    288      memset(&iasGateway,0,sizeof(iasGateway));
    289 
    290      for (int currentRoute = 0; currentRoute < (routeEntries->hostcount+routeEntries->netcount); currentRoute++)
    291      {
    292      // First check if this route is for our interface
    293 #ifndef TCPV40HDRS
    294          r = (struct ortentry *) (p);
    295 #else
    296          r = (struct rtentry *) (p);
    297 #endif
    298          if (strcmp((const char*)(p + sizeof(struct rtentry)),iShortName)==0)
    299          {
    300             sin = (struct sockaddr_in *)(&r->rt_dst);
    301             if (strcmp(inet_ntoa(sin->sin_addr),"0.0.0.0")==0)
    302             {
    303                iasGateway.Next = NULL;
    304                // TODO : Some systems may have many gateways
    305                sin = (struct sockaddr_in *)(&r->rt_gateway);
    306                strcpy(iasGateway.IpAddress.String,inet_ntoa(sin->sin_addr));
    307                sin = (struct sockaddr_in *)&p[-4];
    308                strcpy(iasGateway.IpMask.String,inet_ntoa(sin->sin_addr));
    309                iasGateway.Context = 0;
    310             }
    311          }
    312 #ifndef TCPV40HDRS
    313        p+=sizeof(struct ortentry);
    314 #else
    315        p+=sizeof(struct rtentry);
    316 #endif
    317        p+=strlen((char *)p)+1;
    318     }
    319 
    320   memcpy((char*)&pipAdapter->IpAddressList, (char*)&iasAdapterIP, sizeof(iasAdapterIP));
    321   pipAdapter->CurrentIpAddress = &pipAdapter->IpAddressList;
    322   memcpy((char*)&pipAdapter->GatewayList,   (char*)&iasGateway, sizeof(iasGateway));
    323   // what about OS/2 DHCP?
    324   memset((char*)&pipAdapter->DhcpServer, 0, sizeof( IP_ADDR_STRING ) );
    325   pipAdapter->HaveWins = 0;
    326   memset((char*)&pipAdapter->PrimaryWinsServer, 0, sizeof( IP_ADDR_STRING ) );
    327   memset((char*)&pipAdapter->SecondaryWinsServer, 0, sizeof( IP_ADDR_STRING ) );
    328   pipAdapter->LeaseObtained = 0;
    329   pipAdapter->LeaseExpires = 0;
    330   if (!topAdapter) topAdapter = pipAdapter;
    331   oldAdapter = pipAdapter;
    332  }
    333  pipAdapter = topAdapter;
    334 
    335  // Cleanup
    336  if (buffer) free(buffer);
    337  if (buffer2) free(buffer2);
    338  soclose(clientSocket);
    339375}
    340376
Note: See TracChangeset for help on using the changeset viewer.