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