upnp/upnpstack/upnputils/src/upnpipaddressutils.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2005-2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies  this distribution, and is available 
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  IP Address utils
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include <e32base.h>
       
    21 #include <commdb.h>
       
    22 #include <in_sock.h>
       
    23 #include "upnpipaddressutils.h"
       
    24 #define KLogFile _L("DLNAWebServer.txt")
       
    25 #include "upnpcustomlog.h"
       
    26 
       
    27 // ================= MEMBER FUNCTIONS =======================
       
    28 namespace UpnpIpAddressUtil
       
    29     {
       
    30 // -----------------------------------------------------------------------------
       
    31 // UpnpIpAddressUtil::GetIfNameForIap
       
    32 // Reads the IF name for the specified IAP ID.
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35     EXPORT_C void GetIfNameForIap( TUint32 aIapId, TDes& aIfName )
       
    36         {
       
    37         CCommsDatabase* theDb;
       
    38         TUint32 iapId;
       
    39         TBool nameNotFound = ETrue;
       
    40 
       
    41         // First zero the name. If IF Name is not found, it will be zero lenght.
       
    42         aIfName.Zero();
       
    43 
       
    44         // Open CommDB
       
    45         theDb = CCommsDatabase::NewL( EDatabaseTypeIAP );
       
    46         CleanupStack::PushL( theDb );
       
    47 
       
    48         theDb->ShowHiddenRecords();
       
    49 
       
    50         // Open IAP table and find the IAP ID given as method parameter.
       
    51         CCommsDbTableView* view = theDb->OpenTableLC( TPtrC( IAP ) );
       
    52         TInt res = view->GotoFirstRecord();
       
    53         while ( res == KErrNone && nameNotFound )
       
    54             {
       
    55             // Read IAP ID and match it to the one given as parameter
       
    56             view->ReadUintL( TPtrC( COMMDB_ID ), iapId );
       
    57             if ( iapId == aIapId )
       
    58                 {
       
    59                 // Right IAP found, read bearer ID and type
       
    60                 TUint32 iapBearerId;
       
    61                 view->ReadUintL( TPtrC( IAP_BEARER ),iapBearerId );
       
    62                 view->ReadTextL( TPtrC( IAP_BEARER_TYPE ),aIfName );
       
    63 
       
    64                 // Find the correct bearer table specified by bearer type
       
    65                 CCommsDbTableView* ifView = theDb->OpenTableLC( 
       
    66                     TPtrC( aIfName ) );
       
    67                 TInt bearerRes = ifView->GotoFirstRecord();
       
    68                 TUint32 bearerId;
       
    69                 while ( bearerRes == KErrNone && nameNotFound )
       
    70                     {
       
    71                     ifView->ReadUintL( TPtrC( COMMDB_ID ), bearerId );
       
    72                     // Find out right bearer record and read IF name
       
    73                     if ( bearerId == iapBearerId )
       
    74                         {
       
    75                         // Read the IF name, it is returned in the method
       
    76                         // parameter
       
    77                         ifView->ReadTextL( TPtrC( IF_NAME ),aIfName );
       
    78                         nameNotFound = EFalse;
       
    79                         }
       
    80                     // Bearer id does not match, check next record
       
    81                     else
       
    82                         {
       
    83                         bearerRes = ifView->GotoNextRecord();
       
    84                         }
       
    85                     }
       
    86                 CleanupStack::PopAndDestroy( ifView );
       
    87                 }
       
    88             // IAP id does not match, check next record
       
    89             else
       
    90                 {
       
    91                 res = view->GotoNextRecord();
       
    92                 }
       
    93             }
       
    94         CleanupStack::PopAndDestroy( view );
       
    95         CleanupStack::PopAndDestroy( theDb );
       
    96         }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // UpnpFileUtil::MatchIfNameWithIpInterface
       
   100 // Matches IF name with IP address of the TCP/IP stack interfaces.
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103     EXPORT_C TInetAddr MatchIfNameWithIpInterface( RSocketServ& aSocketServer,
       
   104                                                    TDesC& aIfName,
       
   105                                                    TInt& aInterfaceIndex )
       
   106         {
       
   107         // Buffer for IP address
       
   108         const TUint KAddrOutputLength = 50;
       
   109         TBuf<KAddrOutputLength> address;
       
   110         TPckgBuf<TSoInetInterfaceInfo> info;
       
   111         TInetAddr localIpAddress;        
       
   112         TInetAddr autoIpAddress;
       
   113         TInt autoIfIndex = 0;
       
   114         RSocket sock;
       
   115         TBool autoIpFound = EFalse;
       
   116         TBool ipFound = EFalse;
       
   117 
       
   118         _LIT( KUdpProtocol, "udp" );
       
   119         _LIT( KLoop4Interface, "loop4" );
       
   120         _LIT( KLoop6Interface, "loop6" );
       
   121         _LIT( KNoAddress, "0.0.0.0");
       
   122         _LIT( KLocalHost, "127.0.0.1");
       
   123         _LIT( KWlanIPv6IfSearchString, "wlan6[*");
       
   124         _LIT( KWlanIPv4IfSearchString, "wlan[*");
       
   125         _LIT( KWlanIfMatchString, "wlan*");
       
   126         _LIT( KEtherIfSearchString, "eth[*");
       
   127         _LIT( KEtherIfMatchString, "eth*");
       
   128         // BT PAN is the same for both info and NIF name
       
   129         _LIT( KBtPanIfSearchString, "btpannif*");
       
   130         _LIT( KAutoIpMatch, "169.254.*");
       
   131         
       
   132         //the default interface index
       
   133         aInterfaceIndex = 0;
       
   134         
       
   135         TInt ret= sock.Open( aSocketServer, KUdpProtocol );
       
   136         if ( KErrNone == ret )
       
   137             {
       
   138             ret = sock.SetOpt( KSoInetEnumInterfaces, KSolInetIfCtrl );
       
   139             }
       
   140 
       
   141         if ( KErrNone == ret )
       
   142             {
       
   143             while( KErrNone == sock.GetOpt( 
       
   144                        KSoInetNextInterface, KSolInetIfCtrl, info ) )
       
   145                 {
       
   146 
       
   147                 // If state is up and name is not empty, loop4 or loop6, 
       
   148                 // then get the address
       
   149                 if ( ( EIfUp == info().iState ) &&
       
   150                      ( 0 != info().iName.Length() ) &&
       
   151                      ( 0 != info().iName.Match( KLoop4Interface ) ) && 
       
   152                      ( 0 != info().iName.Match( KLoop6Interface ) ) )
       
   153                     {
       
   154                     // Check if we have valid IP
       
   155                     address.Zero();
       
   156                     info().iAddress.Output( address );
       
   157 
       
   158                     if ( ( 0 < address.Length() ) && 
       
   159                          ( 0 != address.Match( KNoAddress ) ) &&
       
   160                          ( 0 != address.Match( KLocalHost ) ) )
       
   161                         {
       
   162                         
       
   163                         // read index of interface 
       
   164                                         
       
   165                         TPckgBuf<TSoInetIfQuery> query;
       
   166     					query().iName = info().iName;
       
   167     					
       
   168     					TInt index;
       
   169     					ret=sock.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, query);
       
   170     					if(KErrNone==ret)
       
   171     					    {
       
   172     					    index = query().iIndex;
       
   173                    
       
   174                             // For ethernet, WLAN and BT PAN interfaces, 
       
   175                             // change the match string to suit right interfaces
       
   176                             if ( 0 <= info().iName.MatchC( KEtherIfSearchString ) )
       
   177                                 {
       
   178                                 // Set the IF name for ETH Match operation below.
       
   179                                 info().iName.Copy( KEtherIfMatchString );
       
   180                                 }
       
   181                             else if ( 0 <= info().iName.MatchC( 
       
   182                                 KWlanIPv4IfSearchString ) )
       
   183                                 {
       
   184                                 // Set the IF name for WLAN Match operation below.
       
   185                                 info().iName.Copy( KWlanIfMatchString );
       
   186                                 }
       
   187                             else if ( 0 <= info().iName.MatchC( 
       
   188                                     KBtPanIfSearchString ) )
       
   189                                 {
       
   190                                 // Set the IF name for BT PAN Match operation below.
       
   191                                 info().iName.Copy( KBtPanIfSearchString );
       
   192                                 }
       
   193 
       
   194                             // Exlude IPv6 addresses, they are not supported.
       
   195                             // If IF name matches with the one given as parameter,
       
   196                             // then we have the same interface with IP.
       
   197                             if ( ( 0 > info().iName.MatchC( KWlanIPv6IfSearchString ) ) &&
       
   198                                  ( 0 <= aIfName.MatchC( info().iName ) ) )
       
   199                                 {
       
   200                                 // If the address is auto-ip, check whether there 
       
   201                                 // is also DHCP IP in the same interface.
       
   202                                 if ( 0 <= address.Match( KAutoIpMatch ) )
       
   203                                     {
       
   204                                     autoIpFound = ETrue;
       
   205                                     autoIpAddress = info().iAddress;
       
   206                                     autoIpAddress.ConvertToV4();
       
   207                                     autoIfIndex = index;
       
   208                                     }
       
   209                                 // Other than auto IP
       
   210                                 else
       
   211                                     {
       
   212                                     ipFound = ETrue;
       
   213                                     localIpAddress = info().iAddress;
       
   214                                     // we're using ipv4 addresses
       
   215                                     localIpAddress.ConvertToV4();
       
   216                                     aInterfaceIndex = index;
       
   217                                     }
       
   218                                 }
       
   219     					    }
       
   220                         }
       
   221                     }
       
   222                 }
       
   223             }
       
   224         sock.Close();
       
   225 
       
   226         // If only auto-IP was found, then return it.
       
   227         // If only DHCP IP was found or also auto-IP, then return the
       
   228         // DHCP IP which is already placed in localIpAddress variable.
       
   229         // It is not possible that there is no IP as the connection has
       
   230         // been started already by the UPnP application and this method can't
       
   231         // be called if there is no connection.
       
   232         if ( !ipFound && autoIpFound )
       
   233             {
       
   234             localIpAddress = autoIpAddress;
       
   235             aInterfaceIndex = autoIfIndex;
       
   236             }
       
   237 
       
   238         address.Zero();
       
   239         localIpAddress.Output( address );
       
   240 
       
   241         // return the address
       
   242         return localIpAddress;
       
   243         }
       
   244     }
       
   245 
       
   246 //  End of File