1 /*
2 * This file is part of the Sofia-SIP package
3 *
4 * Copyright (C) 2005 Nokia Corporation.
5 *
6 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25 /**@ingroup su_open_c_localinfo.cpp
26 *
27 * @CFILE su_open_c_localinfo.cpp
28 * Functionality for choosing an access point for sockets on Symbian.
29 *
30 * @author Martti Mela <Martti.Mela@nokia.com>
31 * @date Created: Fri May 18 14:31:41 2007 mela
32 *
33 */
34
35 #include "config.h"
36
37 #include <unistd.h>
38 #include <in_sock.h>
39 #include <es_sock.h>
40 #include <e32base.h>
41 #include <s32mem.h>
42 #include <s32strm.h>
43 #include <commdbconnpref.h>
44
45 #include <sofia-sip/su.h>
46
47
48 su_sockaddr_t sa_global[1];
49
50 /* Copy IP address for the sockaddr structure.
51 *
52 * @param su pointer to allocated su_sockaddr_t structure
53 *
54 * @return 0 if successful.
55 */
su_get_local_ip_addr(su_sockaddr_t * su)56 extern "C" int su_get_local_ip_addr(su_sockaddr_t *su)
57 {
58 su->su_sin.sin_addr.s_addr = sa_global->su_sin.sin_addr.s_addr;
59 su->su_family = sa_global->su_family;
60 su->su_len = sa_global->su_len;
61
62 return 0;
63 }
64
65
66 /* Set up the access point for the stack. Code adapted from Forum Nokia,
67 * http://wiki.forum.nokia.com/index.php/LocalDeviceIpAddress.
68 *
69 * @param su su_sockaddr_t structure
70 * @param ifindex pointer to interface index
71 *
72 * @return Connection object
73 */
su_localinfo_ap_set(su_sockaddr_t * su,int * ifindex)74 extern "C" void *su_localinfo_ap_set(su_sockaddr_t *su, int *ifindex)
75 {
76 TCommDbConnPref iPref;
77 RSocketServ aSocketServ;
78 RSocket sock;
79
80 /* Get the IAP id of the underlying interface of this RConnection */
81 TUint32 iapId;
82
83 iPref.SetDirection(ECommDbConnectionDirectionOutgoing);
84 iPref.SetDialogPreference(ECommDbDialogPrefPrompt);
85 iPref.SetBearerSet(KCommDbBearerUnknown /*PSD*/);
86
87 aSocketServ = RSocketServ();
88 aSocketServ.Connect();
89 RConnection *aConnection = new RConnection();
90 aConnection->Open(aSocketServ);
91 aConnection->Start(iPref);
92
93 User::LeaveIfError(sock.Open(aSocketServ, KAfInet, KSockStream,
94 KProtocolInetTcp));
95
96 User::LeaveIfError(aConnection->GetIntSetting(_L("IAP\\Id"), iapId));
97
98 /* Get IP information from the socket */
99 TSoInetInterfaceInfo ifinfo;
100 TPckg<TSoInetInterfaceInfo> ifinfopkg(ifinfo);
101
102 TSoInetIfQuery ifquery;
103 TPckg<TSoInetIfQuery> ifquerypkg(ifquery);
104
105 /* To find out which interfaces are using our current IAP, we
106 * must enumerate and go through all of them and make a query
107 * by name for each. */
108 User::LeaveIfError(sock.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
109 while(sock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, ifinfopkg) == KErrNone) {
110 ifquery.iName = ifinfo.iName;
111 TInt err = sock.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifquerypkg);
112
113 /* IAP ID is index 1 of iZone */
114 if(err == KErrNone && ifquery.iZone[1] == iapId) {
115 /* We have found an interface using the IAP we are interested in. */
116 if(ifinfo.iAddress.Address() > 0) {
117 /* found a IPv4 address */
118 su->su_sin.sin_addr.s_addr = htonl(ifinfo.iAddress.Address());
119 sa_global->su_sin.sin_addr.s_addr = su->su_sin.sin_addr.s_addr;
120 sa_global->su_family = su->su_family = AF_INET;
121 sa_global->su_len = su->su_len = 28;
122 *ifindex = iapId;
123 return (void *) aConnection;
124 }
125 }
126 else if(err != KErrNone)
127 break;
128 }
129
130 sock.Close();
131 return (void *) aConnection;
132 }
133
134
135 /* Deinitialize the access point in use.
136 *
137 * @param aconn Pointer to connection object
138 *
139 * @return 0 if successful.
140 */
su_localinfo_ap_deinit(void * aconn)141 extern "C" int su_localinfo_ap_deinit(void *aconn)
142 {
143 RConnection *aConnection = (RConnection *) aconn;
144 aConnection->Stop();
145 aConnection->Close();
146 delete aConnection;
147 return 0;
148 }
149