xref: /f-stack/tools/compat/getaddrinfo.c (revision d4a07e70)
1 /*  $KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $    */
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Copyright (C) 2017-2021 THL A29 Limited, a Tencent company.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the project nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <stdlib.h>
36 
37 #include "netinet/in.h"
38 #include "arpa/inet.h"
39 #include "sys/socket.h"
40 #include "netdb.h"
41 
42 /* FIXME: to support IPv6 */
43 /* Just conver numeric hostname to int and not do anything else. */
44 int
getaddrinfo(const char * hostname,const char * servername,const struct addrinfo * hints,struct addrinfo ** res)45 getaddrinfo(const char *hostname, const char *servername,
46     const struct addrinfo *hints, struct addrinfo **res)
47 {
48     if (hostname == NULL)
49         return EAI_NONAME;
50 
51     *res = NULL;
52     struct addrinfo *ai;
53 
54     ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr));
55     if (ai == NULL)
56         return EAI_MEMORY;
57 
58     ai->ai_next = NULL;
59     ai->ai_canonname = NULL;
60     ai->ai_addr = (struct sockaddr *)(ai+1);
61 
62     struct sockaddr_in *si = (struct sockaddr_in *)ai->ai_addr;
63     si->sin_len = ai->ai_addrlen = sizeof(struct sockaddr);
64     si->sin_family = ai->ai_family = AF_INET;
65     /* si->sin_port ? */
66 
67     if (hints != NULL) {
68         si->sin_family = ai->ai_family = hints->ai_family;
69         ai->ai_socktype = hints->ai_socktype;
70     }
71 
72     if (inet_pton(AF_INET, hostname, &si->sin_addr.s_addr) != 1) {
73         freeaddrinfo(ai);
74         return EAI_NONAME;
75     }
76 
77     *res = ai;
78 
79     return 0;
80 }
81 
82 void
freeaddrinfo(struct addrinfo * ai)83 freeaddrinfo(struct addrinfo *ai)
84 {
85     struct addrinfo *next;
86 
87     do {
88         next = ai->ai_next;
89         if (ai->ai_canonname)
90             free(ai->ai_canonname);
91         /* no need to free(ai->ai_addr) */
92         free(ai);
93         ai = next;
94     } while (ai);
95 }
96 
97 /* Entries EAI_ADDRFAMILY (1) and EAI_NODATA (7) are obsoleted, but left */
98 /* for backward compatibility with userland code prior to 2553bis-02 */
99 static const char *ai_errlist[] = {
100     "Success",                  /* 0 */
101     "Address family for hostname not supported",    /* 1 */
102     "Temporary failure in name resolution",     /* EAI_AGAIN */
103     "Invalid value for ai_flags",           /* EAI_BADFLAGS */
104     "Non-recoverable failure in name resolution",   /* EAI_FAIL */
105     "ai_family not supported",          /* EAI_FAMILY */
106     "Memory allocation failure",            /* EAI_MEMORY */
107     "No address associated with hostname",      /* 7 */
108     "hostname nor servname provided, or not known", /* EAI_NONAME */
109     "servname not supported for ai_socktype",   /* EAI_SERVICE */
110     "ai_socktype not supported",            /* EAI_SOCKTYPE */
111     "System error returned in errno",       /* EAI_SYSTEM */
112     "Invalid value for hints",          /* EAI_BADHINTS */
113     "Resolved protocol is unknown",         /* EAI_PROTOCOL */
114     "Argument buffer overflow",          /* EAI_OVERFLOW */
115 };
116 
117 const char *
gai_strerror(int ecode)118 gai_strerror(int ecode)
119 {
120     if (ecode >= 0 && ecode < EAI_MAX)
121         return ai_errlist[ecode];
122     return "Unknown error";
123 }
124