xref: /f-stack/tools/compat/getaddrinfo.c (revision 2bfe3f2e)
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 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 /* Just conver numeric hostname to int and not do anything else. */
43 int
44 getaddrinfo(const char *hostname, const char *servername,
45     const struct addrinfo *hints, struct addrinfo **res)
46 {
47     if (hostname == NULL)
48         return EAI_NONAME;
49 
50     *res = NULL;
51     struct addrinfo *ai;
52 
53     ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr));
54     if (ai == NULL)
55         return EAI_MEMORY;
56 
57     ai->ai_next = NULL;
58     ai->ai_canonname = NULL;
59     ai->ai_addr = (struct sockaddr *)(ai+1);
60 
61     struct sockaddr_in *si = (struct sockaddr_in *)ai->ai_addr;
62     si->sin_len = ai->ai_addrlen = sizeof(struct sockaddr);
63     si->sin_family = ai->ai_family = AF_INET;
64     /* si->sin_port ? */
65 
66     if (hints != NULL) {
67         si->sin_family = ai->ai_family = hints->ai_family;
68         ai->ai_socktype = hints->ai_socktype;
69     }
70 
71     if (inet_pton(AF_INET, hostname, &si->sin_addr.s_addr) != 1) {
72         freeaddrinfo(ai);
73         return EAI_NONAME;
74     }
75 
76     *res = ai;
77 
78     return 0;
79 }
80 
81 void
82 freeaddrinfo(struct addrinfo *ai)
83 {
84     struct addrinfo *next;
85 
86     do {
87         next = ai->ai_next;
88         if (ai->ai_canonname)
89             free(ai->ai_canonname);
90         /* no need to free(ai->ai_addr) */
91         free(ai);
92         ai = next;
93     } while (ai);
94 }
95 
96 /* Entries EAI_ADDRFAMILY (1) and EAI_NODATA (7) are obsoleted, but left */
97 /* for backward compatibility with userland code prior to 2553bis-02 */
98 static const char *ai_errlist[] = {
99     "Success",                  /* 0 */
100     "Address family for hostname not supported",    /* 1 */
101     "Temporary failure in name resolution",     /* EAI_AGAIN */
102     "Invalid value for ai_flags",           /* EAI_BADFLAGS */
103     "Non-recoverable failure in name resolution",   /* EAI_FAIL */
104     "ai_family not supported",          /* EAI_FAMILY */
105     "Memory allocation failure",            /* EAI_MEMORY */
106     "No address associated with hostname",      /* 7 */
107     "hostname nor servname provided, or not known", /* EAI_NONAME */
108     "servname not supported for ai_socktype",   /* EAI_SERVICE */
109     "ai_socktype not supported",            /* EAI_SOCKTYPE */
110     "System error returned in errno",       /* EAI_SYSTEM */
111     "Invalid value for hints",          /* EAI_BADHINTS */
112     "Resolved protocol is unknown",         /* EAI_PROTOCOL */
113     "Argument buffer overflow",          /* EAI_OVERFLOW */
114 };
115 
116 const char *
117 gai_strerror(int ecode)
118 {
119     if (ecode >= 0 && ecode < EAI_MAX)
120         return ai_errlist[ecode];
121     return "Unknown error";
122 }
123