Debian Codegraph @

Xpjproject/pjlib/src/pj/addr_resolv_symbian.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
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/addr_resolv.h>
20 #include <pj/assert.h>
21 #include <pj/errno.h>
22 #include <pj/ip_helper.h>
23 #include <pj/log.h>
24 #include <pj/sock.h>
25 #include <pj/string.h>
26 #include <pj/unicode.h>
27
28 #include "os_symbian.h"
29
30 #define THIS_FILE "addr_resolv_symbian.cpp"
31 #define TRACE_ME 0
32
33
34 // PJLIB API: resolve hostname
35 PJ_DEF(pj_status_t) pj_gethostbyname(const pj_str_t *name, pj_hostent *he)
36 {
37 static pj_addrinfo ai;
38 static char *aliases[2];
39 static char *addrlist[2];
40 unsigned count = 1;
41 pj_status_t status;
42
43 status = pj_getaddrinfo(PJ_AF_INET, name, &count, &ai);
44 if (status != PJ_SUCCESS)
45 return status;
46
47 aliases[0] = ai.ai_canonname;
48 aliases[1] = NULL;
49
50 addrlist[0] = (char*) &ai.ai_addr.ipv4.sin_addr;
51 addrlist[1] = NULL;
52
53 pj_bzero(he, sizeof(*he));
54 he->h_name = aliases[0];
55 he->h_aliases = aliases;
56 he->h_addrtype = PJ_AF_INET;
57 he->h_length = 4;
58 he->h_addr_list = addrlist;
59
60 return PJ_SUCCESS;
61 }
62
63
64 // Resolve for specific address family
65 static pj_status_t getaddrinfo_by_af(int af, const pj_str_t *name,
66 unsigned *count, pj_addrinfo ai[])
67 {
68 unsigned i;
69 pj_status_t status;
70
71 PJ_ASSERT_RETURN(name && count && ai, PJ_EINVAL);
72
73 #if !defined(PJ_HAS_IPV6) || !PJ_HAS_IPV6
74 if (af == PJ_AF_INET6)
75 return PJ_EIPV6NOTSUP;
76 #endif
77
78 // Return failure if access point is marked as down by app.
79 PJ_SYMBIAN_CHECK_CONNECTION();
80
81 // Get resolver for the specified address family
82 RHostResolver &resv = PjSymbianOS::Instance()->GetResolver(af);
83
84 // Convert name to Unicode
85 wchar_t name16[PJ_MAX_HOSTNAME];
86 pj_ansi_to_unicode(name->ptr, name->slen, name16, PJ_ARRAY_SIZE(name16));
87 TPtrC16 data((const TUint16*)name16);
88
89 // Resolve!
90 TNameEntry nameEntry;
91 TRequestStatus reqStatus;
92
93 resv.GetByName(data, nameEntry, reqStatus);
94 User::WaitForRequest(reqStatus);
95
96 // Iterate each result
97 i = 0;
98 while (reqStatus == KErrNone && i < *count) {
99
100 // Get the resolved TInetAddr
101 TInetAddr inetAddr(nameEntry().iAddr);
102 int addrlen;
103
104 #if TRACE_ME
105 if (1) {
106 pj_sockaddr a;
107 char ipaddr[PJ_INET6_ADDRSTRLEN+2];
108 int namelen;
109
110 namelen = sizeof(pj_sockaddr);
111 if (PjSymbianOS::Addr2pj(inetAddr, a, &namelen,
112 PJ_FALSE) == PJ_SUCCESS)
113 {
114 PJ_LOG(5,(THIS_FILE, "resolve %.*s: %s",
115 (int)name->slen, name->ptr,
116 pj_sockaddr_print(&a, ipaddr, sizeof(ipaddr), 2)));
117 }
118 }
119 #endif
120
121 // Ignore if this is not the same address family
122 // Not a good idea, as Symbian mapps IPv4 to IPv6.
123 //fam = inetAddr.Family();
124 //if (fam != af) {
125 // resv.Next(nameEntry, reqStatus);
126 // User::WaitForRequest(reqStatus);
127 // continue;
128 //}
129
130 // Convert IP address first to get IPv4 mapped address
131 addrlen = sizeof(ai[i].ai_addr);
132 status = PjSymbianOS::Addr2pj(inetAddr, ai[i].ai_addr,
133 &addrlen, PJ_TRUE);
134 if (status != PJ_SUCCESS)
135 return status;
136
137 // Ignore if address family doesn't match
138 if (ai[i].ai_addr.addr.sa_family != af) {
139 resv.Next(nameEntry, reqStatus);
140 User::WaitForRequest(reqStatus);
141 continue;
142 }
143
144 // Convert the official address to ANSI.
145 pj_unicode_to_ansi((const wchar_t*)nameEntry().iName.Ptr(),
146 nameEntry().iName.Length(),
147 ai[i].ai_canonname, sizeof(ai[i].ai_canonname));
148
149 // Next
150 ++i;
151 resv.Next(nameEntry, reqStatus);
152 User::WaitForRequest(reqStatus);
153 }
154
155 *count = i;
156 return PJ_SUCCESS;
157 }
158
159 /* Resolve IPv4/IPv6 address */
160 PJ_DEF(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *nodename,
161 unsigned *count, pj_addrinfo ai[])
162 {
163 unsigned start;
164 pj_status_t status = PJ_EAFNOTSUP;
165
166 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6 || af==PJ_AF_UNSPEC,
167 PJ_EAFNOTSUP);
168 PJ_ASSERT_RETURN(nodename && count && *count && ai, PJ_EINVAL);
169
170 start = 0;
171
172 if (af==PJ_AF_INET6 || af==PJ_AF_UNSPEC) {
173 unsigned max = *count;
174 status = getaddrinfo_by_af(PJ_AF_INET6, nodename,
175 &max, &ai[start]);
176 if (status == PJ_SUCCESS) {
177 (*count) -= max;
178 start += max;
179 }
180 }
181
182 if (af==PJ_AF_INET || af==PJ_AF_UNSPEC) {
183 unsigned max = *count;
184 status = getaddrinfo_by_af(PJ_AF_INET, nodename,
185 &max, &ai[start]);
186 if (status == PJ_SUCCESS) {
187 (*count) -= max;
188 start += max;
189 }
190 }
191
192 *count = start;
193
194 if (*count) {
195 return PJ_SUCCESS;
196 } else {
197 return status!=PJ_SUCCESS ? status : PJ_ENOTFOUND;
198 }
199 }
200