javacommons/gcfprotocols/socket/socket/src.s60/socketlocalhostinfo.cpp
branchRCL_3
changeset 23 e5618cc85d74
parent 18 9ac0a0a7da70
child 24 6c158198356e
equal deleted inserted replaced
21:4376525cdefb 23:e5618cc85d74
    22 #include "logger.h"
    22 #include "logger.h"
    23 #include "connectionmanager.h"
    23 #include "connectionmanager.h"
    24 
    24 
    25 int GetlocalIPAdressL(char *localaddr, int aMidletIapId, int aApType);
    25 int GetlocalIPAdressL(char *localaddr, int aMidletIapId, int aApType);
    26 
    26 
    27 int SocketLocalHostInfo::getLocalAddress(int /* aSd */, char *aLocalAddr,
    27 OS_EXPORT int SocketLocalHostInfo::getLocalAddress(int /* aSd */, char *aLocalAddr,
    28         int aMidletIapId, int aApType)
    28         int aMidletIapId, int aApType)
    29 {
    29 {
    30     TRAPD(err,GetlocalIPAdressL(aLocalAddr,aMidletIapId, aApType););
    30     TRAPD(err,GetlocalIPAdressL(aLocalAddr,aMidletIapId, aApType););
    31     return err;
    31     return err;
       
    32 
       
    33 }
       
    34 
       
    35 int getIPAddressL(const int aIapId, char *ipAddress)
       
    36 {
       
    37     RSocketServ socketServ;
       
    38     RSocket sock;
       
    39     User::LeaveIfError(socketServ.Connect());
       
    40     User::LeaveIfError(sock.Open(socketServ, KAfInet, KSockStream,
       
    41                                  KProtocolInetTcp));
       
    42 
       
    43     //  find the ip address of the active interface
       
    44     TSoInetInterfaceInfo ifInfo;
       
    45     TPckg<TSoInetInterfaceInfo> ifInfoPkg(ifInfo);
       
    46     TSoInetIfQuery ifQuery;
       
    47     TPckg<TSoInetIfQuery> ifQueryPkg(ifQuery);
       
    48 
       
    49     // To find out which interfaces are using our current IAP, we must
       
    50     // enumerate and go through all of them and make a query by name for each.
       
    51     ILOG(ESOCKET, "Looping thru all the destinations and ap \n\n");
       
    52     User::LeaveIfError(sock.SetOpt(KSoInetEnumInterfaces,
       
    53                                    KSolInetIfCtrl));
       
    54 
       
    55     while (sock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, ifInfoPkg)
       
    56             == KErrNone)
       
    57     {
       
    58         ifQuery.iName = ifInfo.iName;
       
    59         TInt err = sock.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery,
       
    60                                ifQueryPkg);
       
    61         if ((err == KErrNone) && (ifQuery.iZone[1] == aIapId))
       
    62         {
       
    63             ILOG2(ESOCKET, "Network id = %d, IAP id = %d ",(TUint32)(ifQuery.iZone[15]),(TUint32)ifQuery.iZone[1]);
       
    64 
       
    65             // IAP ID is index 1 of iZone
       
    66             // We have found an interface using the IAP we are interested in.
       
    67             if (ifInfo.iAddress.Address() > 0)
       
    68             {
       
    69                 if (!ifInfo.iAddress.IsUnspecified()
       
    70                         && !ifInfo.iAddress.IsLoopback()
       
    71                         && !ifInfo.iAddress.IsLinkLocal())
       
    72                 {
       
    73                     // found a IPv4 address
       
    74                     TBuf8<20> aIP8;
       
    75                     TBuf<20> aIP;
       
    76                     TInetAddr aAddr;
       
    77                     aAddr = ifInfo.iAddress;
       
    78                     aAddr.ConvertToV4();
       
    79                     aAddr.Output(aIP);
       
    80                     aIP8.Copy(aIP);
       
    81 
       
    82                     strncpy(ipAddress, (char*) aIP8.Ptr(),
       
    83                             aIP8.Length());
       
    84                     ipAddress[aIP8.Length()] = '\0';
       
    85                     ILOG1(ESOCKET, "GetlocalIPAdressLLL , ip = %s",
       
    86                           ipAddress);
       
    87                     sock.Close();
       
    88                     socketServ.Close();
       
    89                     ILOG(ESOCKET, "returning from getIpAddr");
       
    90                     return KErrNone;
       
    91                 }
       
    92             }  // end if addr > 0
       
    93             else if (err != KErrNone)
       
    94             {
       
    95                 sock.Close();
       
    96                 return err; // return with error
       
    97             }
       
    98         }
       
    99     }   // end while
       
   100     sock.Close();
       
   101     socketServ.Close();
       
   102     return KErrNone;
    32 
   103 
    33 }
   104 }
    34 
   105 
    35 /**
   106 /**
    36  *In Symbian, when a server connection is opened, it is not bound to any interface
   107  *In Symbian, when a server connection is opened, it is not bound to any interface
    41  */
   112  */
    42 
   113 
    43 int GetlocalIPAdressL(char *localaddr, int aMidletIapId, int aType)
   114 int GetlocalIPAdressL(char *localaddr, int aMidletIapId, int aType)
    44 {
   115 {
    45     JELOG2(ESOCKET);
   116     JELOG2(ESOCKET);
    46 
   117     int err = KErrNone;
    47     TUint32 activeIapId = 0;
   118     TUint32 activeIapId = 0;
    48     TConnectionInfoBuf connectionInfo;
   119     TConnectionInfoBuf connectionInfo;
    49     TUint count = 0;
   120     TUint count = 0;
    50     bool flg = false;
   121     bool flg = false;
    51     RSocketServ socketServ;
   122     RSocketServ socketServ;
   101                       flag);
   172                       flag);
   102                 if (!flag)
   173                 if (!flag)
   103                     continue; // go to the next active IAP
   174                     continue; // go to the next active IAP
   104             }
   175             }
   105 
   176 
   106             //  find the ip address of the active interface
   177             // find the ip address of the active IAP
       
   178             err = getIPAddressL(activeIapId,localaddr);
       
   179             break;  // once we find the IP addr, return
   107 
   180 
   108             TSoInetInterfaceInfo ifInfo;
       
   109             TPckg<TSoInetInterfaceInfo> ifInfoPkg(ifInfo);
       
   110             TSoInetIfQuery ifQuery;
       
   111             TPckg<TSoInetIfQuery> ifQueryPkg(ifQuery);
       
   112             // To find out which interfaces are using our current IAP, we must
       
   113             // enumerate and go through all of them and make a query by name for each.
       
   114             ILOG(ESOCKET, "Looping thru all the destinations and ap \n\n");
       
   115             User::LeaveIfError(sock.SetOpt(KSoInetEnumInterfaces,
       
   116                                            KSolInetIfCtrl));
       
   117             while (sock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, ifInfoPkg)
       
   118                     == KErrNone)
       
   119             {
       
   120                 ifQuery.iName = ifInfo.iName;
       
   121                 TInt err = sock.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery,
       
   122                                        ifQueryPkg);
       
   123 
       
   124                 if ((err == KErrNone) && (ifQuery.iZone[1] == activeIapId))
       
   125                 {
       
   126                     //TUint32 t5 = (TUint32)(ifQuery.iZone[15]);                    
       
   127                     ILOG2(ESOCKET, "Network id = %d, IAP id = %d ",(TUint32)(ifQuery.iZone[15]),(TUint32)ifQuery.iZone[1]);
       
   128                     
       
   129                     // IAP ID is index 1 of iZone
       
   130                     // We have found an interface using the IAP we are interested in.
       
   131                     if (ifInfo.iAddress.Address() > 0)
       
   132                         if (!ifInfo.iAddress.IsUnspecified()
       
   133                                 && !ifInfo.iAddress.IsLoopback()
       
   134                                 && !ifInfo.iAddress.IsLinkLocal())
       
   135                         {
       
   136                             // found a IPv4 address
       
   137                             TBuf8<20> aIP8;
       
   138                             TBuf<20> aIP;
       
   139                             TInetAddr aAddr;
       
   140                             aAddr = ifInfo.iAddress;
       
   141                             aAddr.ConvertToV4();
       
   142                             aAddr.Output(aIP);
       
   143                             aIP8.Copy(aIP);
       
   144 
       
   145                             strncpy(localaddr, (char*) aIP8.Ptr(),
       
   146                                     aIP8.Length());
       
   147                             localaddr[aIP8.Length()] = '\0';
       
   148                             ILOG1(ESOCKET, "GetlocalIPAdressL , ip = %s",
       
   149                                   localaddr);
       
   150                             sock.Close();
       
   151                             return KErrNone;
       
   152                         }
       
   153                 }
       
   154                 else if (err != KErrNone)
       
   155                 {
       
   156                     sock.Close();
       
   157                     return err; // return with error
       
   158                 }
       
   159             } // end while
       
   160         } // end for loop
   181         } // end for loop
   161 
   182 
   162     } // end else active connection
   183     } // end else active connection
       
   184     ILOG(ESOCKET, "GetlocalIPAdressL , got ip ");
       
   185     conn.Close();
       
   186     sock.Close();
       
   187     socketServ.Close();
   163 
   188 
   164     sock.Close();
   189     return err; // return with KErrNotFound
   165     return KErrNotFound; // return with KErrNotFound
       
   166 
   190 
   167 
   191 
   168 }
   192 }