blob: 43df5e160c38d844ec431145e96d7369e87bbf73 [file] [log] [blame]
Benny Prijono62b86eb2007-12-01 08:52:57 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono62b86eb2007-12-01 08:52:57 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pj/ip_helper.h>
20#include <pj/addr_resolv.h>
21#include <pj/assert.h>
22#include <pj/errno.h>
Benny Prijono9db4bd62007-12-31 11:26:21 +000023#include <pj/log.h>
Benny Prijono62b86eb2007-12-01 08:52:57 +000024#include <pj/string.h>
25#include <pj/compat/socket.h>
26
27
28#include "os_symbian.h"
29
Benny Prijono9db4bd62007-12-31 11:26:21 +000030#define THIS_FILE "ip_helper_symbian.cpp"
31#define TRACE_ME 0
32
Benny Prijono62b86eb2007-12-01 08:52:57 +000033static pj_status_t rsock_enum_interface(int af,
34 unsigned *p_cnt,
35 pj_sockaddr ifs[])
36{
37 TInt rc;
38 RSocket rSock;
39 TPckgBuf<TSoInetInterfaceInfo> info;
40 unsigned i;
41
42 if (PjSymbianOS::Instance()->Connection()) {
43
44 rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
45 af, PJ_SOCK_DGRAM, KProtocolInetUdp,
46 *PjSymbianOS::Instance()->Connection());
47 } else {
48
49 rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
50 af, PJ_SOCK_DGRAM, KProtocolInetUdp);
51
52 }
53
54 if (rc != KErrNone)
55 return PJ_RETURN_OS_ERROR(rc);
56
57 rSock.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl);
58
59 for (i=0; i<*p_cnt &&
60 rSock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl,
61 info) == KErrNone; )
62 {
63 TInetAddr &iAddress = info().iAddress;
64 int namelen;
Benny Prijono9db4bd62007-12-31 11:26:21 +000065
66#if TRACE_ME
67 if (1) {
68 pj_sockaddr a;
69 char ipaddr[PJ_INET6_ADDRSTRLEN+2];
70
71 namelen = sizeof(pj_sockaddr);
72 if (PjSymbianOS::Addr2pj(iAddress, a, &namelen,
73 PJ_FALSE) == PJ_SUCCESS)
74 {
75 PJ_LOG(5,(THIS_FILE, "Enum: found address %s",
76 pj_sockaddr_print(&a, ipaddr, sizeof(ipaddr), 2)));
77 }
78 }
79#endif
Benny Prijono62b86eb2007-12-01 08:52:57 +000080
81 namelen = sizeof(ifs[i]);
Benny Prijono9db4bd62007-12-31 11:26:21 +000082 if (PjSymbianOS::Addr2pj(iAddress, ifs[i], &namelen,
83 PJ_TRUE) != PJ_SUCCESS)
84 {
Benny Prijono62b86eb2007-12-01 08:52:57 +000085 continue;
Benny Prijono9db4bd62007-12-31 11:26:21 +000086 }
87
88 if (ifs[i].addr.sa_family != af)
89 continue;
Benny Prijono62b86eb2007-12-01 08:52:57 +000090
91 ++i;
92 }
93
94 rSock.Close();
95
96 // Done
97 *p_cnt = i;
98
99 return PJ_SUCCESS;
Benny Prijono62b86eb2007-12-01 08:52:57 +0000100}
101
102/*
103 * Enumerate the local IP interface currently active in the host.
104 */
105PJ_DEF(pj_status_t) pj_enum_ip_interface(int af,
106 unsigned *p_cnt,
107 pj_sockaddr ifs[])
108{
109 unsigned start;
110 pj_status_t status = PJ_SUCCESS;
111
112 start = 0;
113
114 /* Get IPv6 interface first. */
115 if (af==PJ_AF_INET6 || af==PJ_AF_UNSPEC) {
116 unsigned max = *p_cnt;
117 status = rsock_enum_interface(PJ_AF_INET6, &max, &ifs[start]);
118 if (status == PJ_SUCCESS) {
119 (*p_cnt) -= max;
120 start += max;
121 }
122 }
123
124 /* Get IPv4 interface. */
125 if (af==PJ_AF_INET || af==PJ_AF_UNSPEC) {
126 unsigned max = *p_cnt;
127 status = rsock_enum_interface(PJ_AF_INET, &max, &ifs[start]);
128 if (status == PJ_SUCCESS) {
129 (*p_cnt) -= max;
130 start += max;
131 }
132 }
133
134 *p_cnt = start;
135
136 return start ? PJ_SUCCESS : PJ_ENOTFOUND;
137}
138
139/*
140 * Enumerate the IP routing table for this host.
141 */
142PJ_DEF(pj_status_t) pj_enum_ip_route(unsigned *p_cnt,
143 pj_ip_route_entry routes[])
144{
145 PJ_ASSERT_RETURN(p_cnt && *p_cnt > 0 && routes, PJ_EINVAL);
146 *p_cnt = 0;
147 return PJ_ENOTSUP;
148}
149