Commit 181f2d48 authored by Martti Mela's avatar Martti Mela
Browse files

su_popen_c_localinfo.cpp: added missing copyrights, code cleanup

darcs-hash:20070712144504-1b897-69e55951938f0ab59b0200017ce60c64bb378484.gz
parent 362bf0cb
/*
* This file is part of the Sofia-SIP package
*
* Copyright (C) 2005 Nokia Corporation.
*
* Contact: Pekka Pessi <pekka.pessi@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/**@ingroup su_open_c_localinfo.cpp
*
* @CFILE su_open_c_localinfo.cpp
* Functionality for choosing an access point for sockets.
*
* @author Martti Mela <Martti.Mela@nokia.com>
* @date Created: Fri May 18 14:31:41 2007 mela
*
*/
#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include "in_sock.h"
......@@ -13,6 +48,12 @@
su_sockaddr_t sa_global[1];
/* Copy IP address for the sockaddr structure.
*
* @param su pointer to allocated su_sockaddr_t structure
*
* @return 0 if successful.
*/
extern "C" int su_get_local_ip_addr(su_sockaddr_t *su)
{
su->su_sin.sin_addr.s_addr = sa_global->su_sin.sin_addr.s_addr;
......@@ -22,147 +63,86 @@ extern "C" int su_get_local_ip_addr(su_sockaddr_t *su)
return 0;
}
#if 0
// Finds out local IP address of the device
//
extern "C" int su_get_local_ip_address_l(unsigned *ifindex, su_sockaddr_t *su)
{
TCommDbConnPref iPref;
RSocketServ aSocketServ;
RSocket sock;
// Get the IAP id of the underlying interface of this RConnection
iPref.SetDirection(ECommDbConnectionDirectionOutgoing);
iPref.SetDialogPreference(ECommDbDialogPrefPrompt);
//iPref.SetIapId(iapId);
iPref.SetBearerSet(KCommDbBearerUnknown/*PSD*/);
aSocketServ = RSocketServ();
aSocketServ.Connect();
RConnection aConnection = RConnection();
aConnection.Open(aSocketServ);
aConnection.Start(iPref);
*ifindex = iPref.IapId();
TUint32 iapId = *ifindex;
User::LeaveIfError(sock.Open(aSocketServ, KAfInet, KSockStream, KProtocolInetTcp));
User::LeaveIfError(aConnection.GetIntSetting(_L("IAP\\Id"), iapId));
// Get IP information from the socket
TSoInetInterfaceInfo ifinfo;
TPckg<TSoInetInterfaceInfo> ifinfopkg(ifinfo);
TSoInetIfQuery ifquery;
TPckg<TSoInetIfQuery> ifquerypkg(ifquery);
// To find out which interfaces are using our current IAP, we must
// enumerate and go through all of them and make a query by name
// for each.
User::LeaveIfError(sock.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
while(sock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, ifinfopkg) == KErrNone)
{
ifquery.iName = ifinfo.iName;
TInt err = sock.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifquerypkg);
if(err == KErrNone && ifquery.iZone[1] == iapId) // IAP ID is index 1 of iZone
{
// We have found an interface using the IAP we are interested in.
if(ifinfo.iAddress.Address() > 0)
{
// found a IPv4 address
//*ipv4 = ifinfo.iAddress.Address();
su->su_sin.sin_addr.s_addr = htonl(ifinfo.iAddress.Address());
su->su_family = 2048;
su->su_len = 28;
sock.Close();
return 0; // stop & return KErrNone
}
}
else if(err != KErrNone)
break;
}
sock.Close();
return -1; // return with KErrNotFound
}
#endif
// Set up the access point for the stack
//
/* Set up the access point for the stack. Code adapted from Forum Nokia,
* http://wiki.forum.nokia.com/index.php/LocalDeviceIpAddress.
*
* @param su su_sockaddr_t structure
* @param ifindex pointer to interface index
*
* @return Connection object
*/
extern "C" void *su_localinfo_ap_set(su_sockaddr_t *su, int *ifindex)
{
TCommDbConnPref iPref;
RSocketServ aSocketServ;
RSocket sock;
// Get the IAP id of the underlying interface of this RConnection
TUint32 iapId;
iPref.SetDirection(ECommDbConnectionDirectionOutgoing);
iPref.SetDialogPreference(ECommDbDialogPrefPrompt);
//iPref.SetIapId(iapId);
iPref.SetBearerSet(KCommDbBearerUnknown/*PSD*/);
aSocketServ = RSocketServ();
aSocketServ.Connect();
RConnection *aConnection = new RConnection();
aConnection->Open(aSocketServ);
aConnection->Start(iPref);
User::LeaveIfError(sock.Open(aSocketServ, KAfInet, KSockStream, KProtocolInetTcp));
User::LeaveIfError(aConnection->GetIntSetting(_L("IAP\\Id"), iapId));
// Get IP information from the socket
TSoInetInterfaceInfo ifinfo;
TPckg<TSoInetInterfaceInfo> ifinfopkg(ifinfo);
TSoInetIfQuery ifquery;
TPckg<TSoInetIfQuery> ifquerypkg(ifquery);
// To find out which interfaces are using our current IAP, we must
// enumerate and go through all of them and make a query by name
// for each.
User::LeaveIfError(sock.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
while(sock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, ifinfopkg) == KErrNone)
{
ifquery.iName = ifinfo.iName;
TInt err = sock.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifquerypkg);
if(err == KErrNone && ifquery.iZone[1] == iapId) // IAP ID is index 1 of iZone
{
// We have found an interface using the IAP we are interested in.
if(ifinfo.iAddress.Address() > 0)
{
// found a IPv4 address
//*ipv4 = ifinfo.iAddress.Address();
su->su_sin.sin_addr.s_addr = htonl(ifinfo.iAddress.Address());
sa_global->su_sin.sin_addr.s_addr = su->su_sin.sin_addr.s_addr;
sa_global->su_family = su->su_family = AF_INET;
sa_global->su_len = su->su_len = 28;
*ifindex = iapId;
//sock.Close();
//return 0; // stop & return KErrNone
return (void *) aConnection;
}
}
else if(err != KErrNone)
break;
TCommDbConnPref iPref;
RSocketServ aSocketServ;
RSocket sock;
/* Get the IAP id of the underlying interface of this RConnection */
TUint32 iapId;
iPref.SetDirection(ECommDbConnectionDirectionOutgoing);
iPref.SetDialogPreference(ECommDbDialogPrefPrompt);
iPref.SetBearerSet(KCommDbBearerUnknown /*PSD*/);
aSocketServ = RSocketServ();
aSocketServ.Connect();
RConnection *aConnection = new RConnection();
aConnection->Open(aSocketServ);
aConnection->Start(iPref);
User::LeaveIfError(sock.Open(aSocketServ, KAfInet, KSockStream,
KProtocolInetTcp));
User::LeaveIfError(aConnection->GetIntSetting(_L("IAP\\Id"), iapId));
/* Get IP information from the socket */
TSoInetInterfaceInfo ifinfo;
TPckg<TSoInetInterfaceInfo> ifinfopkg(ifinfo);
TSoInetIfQuery ifquery;
TPckg<TSoInetIfQuery> ifquerypkg(ifquery);
/* To find out which interfaces are using our current IAP, we
* must enumerate and go through all of them and make a query
* by name for each. */
User::LeaveIfError(sock.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
while(sock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, ifinfopkg) == KErrNone) {
ifquery.iName = ifinfo.iName;
TInt err = sock.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifquerypkg);
/* IAP ID is index 1 of iZone */
if(err == KErrNone && ifquery.iZone[1] == iapId) {
/* We have found an interface using the IAP we are interested in. */
if(ifinfo.iAddress.Address() > 0) {
/* found a IPv4 address */
su->su_sin.sin_addr.s_addr = htonl(ifinfo.iAddress.Address());
sa_global->su_sin.sin_addr.s_addr = su->su_sin.sin_addr.s_addr;
sa_global->su_family = su->su_family = AF_INET;
sa_global->su_len = su->su_len = 28;
*ifindex = iapId;
return (void *) aConnection;
}
}
else if(err != KErrNone)
break;
}
sock.Close();
return NULL;//-1; // return with KErrNotFound
sock.Close();
}
/* Deinitialize the access point in use.
*
* @param aconn Pointer to connection object
*
* @return 0 if successful.
*/
extern "C" int su_localinfo_ap_deinit(void *aconn)
{
RConnection *aConnection = (RConnection *) aconn;
aConnection->Stop();
aConnection->Close();
delete aConnection;
return 0;
RConnection *aConnection = (RConnection *) aconn;
aConnection->Stop();
aConnection->Close();
delete aConnection;
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment