su_open_c_localinfo.cpp 4.16 KB
Newer Older
Michael Jerris's avatar
Michael Jerris committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/*
 * 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 on Symbian.
 *
 * @author Martti Mela <Martti.Mela@nokia.com>
 * @date Created: Fri May 18 14:31:41 2007 mela
 *
 */

#include "config.h"

#include <unistd.h>
#include <in_sock.h> 
#include <es_sock.h>
#include <e32base.h>
#include <s32mem.h>
#include <s32strm.h>
#include <commdbconnpref.h>

#include <sofia-sip/su.h>


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;
	su->su_family = sa_global->su_family;
	su->su_len = sa_global->su_len;
	
	return 0;
}


/* 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.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();
}


/* 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;
}