sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
netdb.c
Go to the documentation of this file.
1
8
9/*
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
31 *
32 * This file is part of the lwIP TCP/IP stack.
33 *
34 * Author: Simon Goldschmidt
35 *
36 */
37
38#include "lwip/netdb.h"
39
40#if LWIP_DNS && LWIP_SOCKET
41
42#include "lwip/err.h"
43#include "lwip/errno.h"
44#include "lwip/mem.h"
45#include "lwip/memp.h"
46#include "lwip/ip_addr.h"
47#include "lwip/api.h"
48#include "lwip/dns.h"
49
50#include <string.h> /* memset */
51#include <stdlib.h> /* atoi */
52
54struct gethostbyname_r_helper {
55 ip_addr_t *addr_list[2];
56 ip_addr_t addr;
57 char *aliases;
58};
59
61#if LWIP_DNS_API_DECLARE_H_ERRNO
62int h_errno;
63#endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
64
72#ifndef LWIP_DNS_API_HOSTENT_STORAGE
73#define LWIP_DNS_API_HOSTENT_STORAGE 0
74#endif
75
76/* define "hostent" variables storage */
77#if LWIP_DNS_API_HOSTENT_STORAGE
78#define HOSTENT_STORAGE
79#else
80#define HOSTENT_STORAGE static
81#endif /* LWIP_DNS_API_STATIC_HOSTENT */
82
92struct hostent *
93lwip_gethostbyname(const char *name)
94{
95 err_t err;
96 ip_addr_t addr;
97
98 /* buffer variables for lwip_gethostbyname() */
99 HOSTENT_STORAGE struct hostent s_hostent;
100 HOSTENT_STORAGE char *s_aliases;
101 HOSTENT_STORAGE ip_addr_t s_hostent_addr;
102 HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2];
103 HOSTENT_STORAGE char s_hostname[DNS_MAX_NAME_LENGTH + 1];
104
105 /* query host IP address */
106 err = netconn_gethostbyname(name, &addr);
107 if (err != ERR_OK) {
108 LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
109 h_errno = HOST_NOT_FOUND;
110 return NULL;
111 }
112
113 /* fill hostent */
114 s_hostent_addr = addr;
115 s_phostent_addr[0] = &s_hostent_addr;
116 s_phostent_addr[1] = NULL;
117 strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH);
118 s_hostname[DNS_MAX_NAME_LENGTH] = 0;
119 s_hostent.h_name = s_hostname;
120 s_aliases = NULL;
121 s_hostent.h_aliases = &s_aliases;
122 s_hostent.h_addrtype = AF_INET;
123 s_hostent.h_length = sizeof(ip_addr_t);
124 s_hostent.h_addr_list = (char **)&s_phostent_addr;
125
126#if DNS_DEBUG
127 /* dump hostent */
128 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name == %s\n", s_hostent.h_name));
129 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases == %p\n", (void *)s_hostent.h_aliases));
130 /* h_aliases are always empty */
131 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype == %d\n", s_hostent.h_addrtype));
132 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length == %d\n", s_hostent.h_length));
133 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list == %p\n", (void *)s_hostent.h_addr_list));
134 if (s_hostent.h_addr_list != NULL) {
135 u8_t idx;
136 for (idx = 0; s_hostent.h_addr_list[idx]; idx++) {
137 LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa(s_phostent_addr[idx])));
138 }
139 }
140#endif /* DNS_DEBUG */
141
142#if LWIP_DNS_API_HOSTENT_STORAGE
143 /* this function should return the "per-thread" hostent after copy from s_hostent */
144 return sys_thread_hostent(&s_hostent);
145#else
146 return &s_hostent;
147#endif /* LWIP_DNS_API_HOSTENT_STORAGE */
148}
149
166int
167lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
168 size_t buflen, struct hostent **result, int *h_errnop)
169{
170 err_t err;
171 struct gethostbyname_r_helper *h;
172 char *hostname;
173 size_t namelen;
174 int lh_errno;
175
176 if (h_errnop == NULL) {
177 /* ensure h_errnop is never NULL */
178 h_errnop = &lh_errno;
179 }
180
181 if (result == NULL) {
182 /* not all arguments given */
183 *h_errnop = EINVAL;
184 return -1;
185 }
186 /* first thing to do: set *result to nothing */
187 *result = NULL;
188 if ((name == NULL) || (ret == NULL) || (buf == NULL)) {
189 /* not all arguments given */
190 *h_errnop = EINVAL;
191 return -1;
192 }
193
194 namelen = strlen(name);
195 if (buflen < (sizeof(struct gethostbyname_r_helper) + LWIP_MEM_ALIGN_BUFFER(namelen + 1))) {
196 /* buf can't hold the data needed + a copy of name */
197 *h_errnop = ERANGE;
198 return -1;
199 }
200
201 h = (struct gethostbyname_r_helper *)LWIP_MEM_ALIGN(buf);
202 hostname = ((char *)h) + sizeof(struct gethostbyname_r_helper);
203
204 /* query host IP address */
205 err = netconn_gethostbyname(name, &h->addr);
206 if (err != ERR_OK) {
207 LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
208 *h_errnop = HOST_NOT_FOUND;
209 return -1;
210 }
211
212 /* copy the hostname into buf */
213 MEMCPY(hostname, name, namelen);
214 hostname[namelen] = 0;
215
216 /* fill hostent */
217 h->addr_list[0] = &h->addr;
218 h->addr_list[1] = NULL;
219 h->aliases = NULL;
220 ret->h_name = hostname;
221 ret->h_aliases = &h->aliases;
222 ret->h_addrtype = AF_INET;
223 ret->h_length = sizeof(ip_addr_t);
224 ret->h_addr_list = (char **)&h->addr_list;
225
226 /* set result != NULL */
227 *result = ret;
228
229 /* return success */
230 return 0;
231}
232
240void
241lwip_freeaddrinfo(struct addrinfo *ai)
242{
243 struct addrinfo *next;
244
245 while (ai != NULL) {
246 next = ai->ai_next;
247 memp_free(MEMP_NETDB, ai);
248 ai = next;
249 }
250}
251
273int
274lwip_getaddrinfo(const char *nodename, const char *servname,
275 const struct addrinfo *hints, struct addrinfo **res)
276{
277 err_t err;
278 ip_addr_t addr;
279 struct addrinfo *ai;
280 struct sockaddr_storage *sa = NULL;
281 int port_nr = 0;
282 size_t total_size;
283 size_t namelen = 0;
284 int ai_family;
285
286 if (res == NULL) {
287 return EAI_FAIL;
288 }
289 *res = NULL;
290 if ((nodename == NULL) && (servname == NULL)) {
291 return EAI_NONAME;
292 }
293
294 if (hints != NULL) {
295 ai_family = hints->ai_family;
296 if ((ai_family != AF_UNSPEC)
297#if LWIP_IPV4
298 && (ai_family != AF_INET)
299#endif /* LWIP_IPV4 */
300#if LWIP_IPV6
301 && (ai_family != AF_INET6)
302#endif /* LWIP_IPV6 */
303 ) {
304 return EAI_FAMILY;
305 }
306 } else {
307 ai_family = AF_UNSPEC;
308 }
309
310 if (servname != NULL) {
311 /* service name specified: convert to port number
312 * @todo?: currently, only ASCII integers (port numbers) are supported (AI_NUMERICSERV)! */
313 port_nr = atoi(servname);
314 if (port_nr == 0 && (servname[0] != '0')) {
315 /* atoi failed - service was not numeric */
316 return EAI_SERVICE;
317 }
318 if ((port_nr < 0) || (port_nr > 0xffff)) {
319 return EAI_SERVICE;
320 }
321 }
322
323 if (nodename != NULL) {
324 /* service location specified, try to resolve */
325 if ((hints != NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
326 /* no DNS lookup, just parse for an address string */
327 if (!ipaddr_aton(nodename, &addr)) {
328 return EAI_NONAME;
329 }
330#if LWIP_IPV4 && LWIP_IPV6
331 if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) ||
332 (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) {
333 return EAI_NONAME;
334 }
335#endif /* LWIP_IPV4 && LWIP_IPV6 */
336 } else {
337#if LWIP_IPV4 && LWIP_IPV6
338 /* AF_UNSPEC: prefer IPv4 */
339 u8_t type = NETCONN_DNS_IPV4_IPV6;
340 if (ai_family == AF_INET) {
341 type = NETCONN_DNS_IPV4;
342 } else if (ai_family == AF_INET6) {
343 type = NETCONN_DNS_IPV6;
344 }
345#endif /* LWIP_IPV4 && LWIP_IPV6 */
346 err = netconn_gethostbyname_addrtype(nodename, &addr, type);
347 if (err != ERR_OK) {
348 return EAI_FAIL;
349 }
350 }
351 } else {
352 /* service location specified, use loopback address */
353 if ((hints != NULL) && (hints->ai_flags & AI_PASSIVE)) {
354 ip_addr_set_any_val(ai_family == AF_INET6, addr);
355 } else {
356 ip_addr_set_loopback_val(ai_family == AF_INET6, addr);
357 }
358 }
359
360 total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_storage);
361 if (nodename != NULL) {
362 namelen = strlen(nodename);
363 if (namelen > DNS_MAX_NAME_LENGTH) {
364 /* invalid name length */
365 return EAI_FAIL;
366 }
367 LWIP_ASSERT("namelen is too long", total_size + namelen + 1 > total_size);
368 total_size += namelen + 1;
369 }
370 /* If this fails, please report to lwip-devel! :-) */
371 LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
372 total_size <= NETDB_ELEM_SIZE);
373 ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
374 if (ai == NULL) {
375 return EAI_MEMORY;
376 }
377 memset(ai, 0, total_size);
378 /* cast through void* to get rid of alignment warnings */
379 sa = (struct sockaddr_storage *)(void *)((u8_t *)ai + sizeof(struct addrinfo));
380 if (IP_IS_V6_VAL(addr)) {
381#if LWIP_IPV6
382 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
383 /* set up sockaddr */
384 inet6_addr_from_ip6addr(&sa6->sin6_addr, ip_2_ip6(&addr));
385 sa6->sin6_family = AF_INET6;
386#if LWIP_SOCKET_HAVE_SA_LEN
387 sa6->sin6_len = sizeof(struct sockaddr_in6);
388#endif /* LWIP_SOCKET_HAVE_SA_LEN */
389 sa6->sin6_port = lwip_htons((u16_t)port_nr);
390 sa6->sin6_scope_id = ip6_addr_zone(ip_2_ip6(&addr));
391 ai->ai_family = AF_INET6;
392#endif /* LWIP_IPV6 */
393 } else {
394#if LWIP_IPV4
395 struct sockaddr_in *sa4 = (struct sockaddr_in *)sa;
396 /* set up sockaddr */
397 inet_addr_from_ip4addr(&sa4->sin_addr, ip_2_ip4(&addr));
398 sa4->sin_family = AF_INET;
399#if LWIP_SOCKET_HAVE_SA_LEN
400 sa4->sin_len = sizeof(struct sockaddr_in);
401#endif /* LWIP_SOCKET_HAVE_SA_LEN */
402 sa4->sin_port = lwip_htons((u16_t)port_nr);
403 ai->ai_family = AF_INET;
404#endif /* LWIP_IPV4 */
405 }
406
407 /* set up addrinfo */
408 if (hints != NULL) {
409 /* copy socktype & protocol from hints if specified */
410 ai->ai_socktype = hints->ai_socktype;
411 ai->ai_protocol = hints->ai_protocol;
412 }
413 if (nodename != NULL) {
414 /* copy nodename to canonname if specified */
415 ai->ai_canonname = ((char *)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
416 MEMCPY(ai->ai_canonname, nodename, namelen);
417 ai->ai_canonname[namelen] = 0;
418 }
419 ai->ai_addrlen = sizeof(struct sockaddr_storage);
420 ai->ai_addr = (struct sockaddr *)sa;
421
422 *res = ai;
423
424 return 0;
425}
426
427#endif /* LWIP_DNS && LWIP_SOCKET */
#define EINVAL
Definition errno.h:72
#define ERANGE
Definition errno.h:96
#define EAI_NONAME
Definition tcpip.h:1924
#define LWIP_MEM_ALIGN_BUFFER(size)
Definition arch.h:287
#define LWIP_MEM_ALIGN(addr)
Definition arch.h:294
@ ERR_OK
Definition err.h:55
#define DNS_DEBUG
Definition opt.h:3577
#define DNS_MAX_NAME_LENGTH
Definition opt.h:1163
#define MEMCPY(dst, src, len)
Definition opt.h:137
void * memp_malloc(memp_t type)
Definition memp.c:337
void memp_free(memp_t type, void *mem)
Definition memp.c:420