]>
Commit | Line | Data |
---|---|---|
48e671d5 | 1 | /* |
52d58495 | 2 | * Pseudo-implementation of RFC2553 name / address resolution functions |
48e671d5 | 3 | * |
4 | * But these functions are not implemented correctly. The minimum subset | |
5 | * is implemented for ssh use only. For exapmle, this routine assumes | |
6 | * that ai_family is AF_INET. Don't use it for another purpose. | |
48e671d5 | 7 | */ |
8 | ||
4ed465ec | 9 | #include "includes.h" |
10 | ||
0b202697 | 11 | RCSID("$Id$"); |
12 | ||
52d58495 | 13 | #ifndef HAVE_GETNAMEINFO |
14 | int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, | |
15 | size_t hostlen, char *serv, size_t servlen, int flags) | |
16 | { | |
17 | struct sockaddr_in *sin = (struct sockaddr_in *)sa; | |
18 | struct hostent *hp; | |
19 | char tmpserv[16]; | |
20 | ||
21 | if (serv != NULL) { | |
22 | snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port)); | |
23 | if (strlcpy(serv, tmpserv, servlen) >= servlen) | |
24 | return (EAI_MEMORY); | |
25 | } | |
26 | ||
27 | if (host != NULL) { | |
28 | if (flags & NI_NUMERICHOST) { | |
29 | if (strlcpy(host, inet_ntoa(sin->sin_addr), | |
30 | hostlen) >= hostlen) | |
31 | return (EAI_MEMORY); | |
32 | else | |
33 | return (0); | |
34 | } else { | |
35 | hp = gethostbyaddr((char *)&sin->sin_addr, | |
36 | sizeof(struct in_addr), AF_INET); | |
37 | if (hp == NULL) | |
38 | return (EAI_NODATA); | |
39 | ||
40 | if (strlcpy(host, hp->h_name, hostlen) >= hostlen) | |
41 | return (EAI_MEMORY); | |
42 | else | |
43 | return (0); | |
44 | } | |
45 | } | |
46 | return (0); | |
47 | } | |
48 | #endif /* !HAVE_GETNAMEINFO */ | |
49 | ||
48e671d5 | 50 | #ifndef HAVE_GAI_STRERROR |
9901cb37 | 51 | char * |
52 | gai_strerror(int err) | |
48e671d5 | 53 | { |
9901cb37 | 54 | switch (err) { |
55 | case EAI_NODATA: | |
56 | return ("no address associated with name"); | |
57 | case EAI_MEMORY: | |
58 | return ("memory allocation failure."); | |
02e2a074 | 59 | case EAI_NONAME: |
60 | return ("nodename nor servname provided, or not known"); | |
9901cb37 | 61 | default: |
62 | return ("unknown/invalid error."); | |
5daf7064 | 63 | } |
48e671d5 | 64 | } |
65 | #endif /* !HAVE_GAI_STRERROR */ | |
66 | ||
67 | #ifndef HAVE_FREEADDRINFO | |
9901cb37 | 68 | void |
69 | freeaddrinfo(struct addrinfo *ai) | |
48e671d5 | 70 | { |
5daf7064 | 71 | struct addrinfo *next; |
72 | ||
0509a052 | 73 | for(; ai != NULL;) { |
74 | next = ai->ai_next; | |
5daf7064 | 75 | free(ai); |
9901cb37 | 76 | ai = next; |
77 | } | |
48e671d5 | 78 | } |
79 | #endif /* !HAVE_FREEADDRINFO */ | |
80 | ||
81 | #ifndef HAVE_GETADDRINFO | |
9901cb37 | 82 | static struct |
83 | addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints) | |
48e671d5 | 84 | { |
5daf7064 | 85 | struct addrinfo *ai; |
86 | ||
d6bd2b5a | 87 | ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in)); |
88 | if (ai == NULL) | |
89 | return (NULL); | |
5daf7064 | 90 | |
9901cb37 | 91 | memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in)); |
5daf7064 | 92 | |
93 | ai->ai_addr = (struct sockaddr *)(ai + 1); | |
94 | /* XXX -- ssh doesn't use sa_len */ | |
95 | ai->ai_addrlen = sizeof(struct sockaddr_in); | |
96 | ai->ai_addr->sa_family = ai->ai_family = AF_INET; | |
48e671d5 | 97 | |
5daf7064 | 98 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port; |
99 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr; | |
100 | ||
b1848832 | 101 | /* XXX: the following is not generally correct, but does what we want */ |
102 | if (hints->ai_socktype) | |
103 | ai->ai_socktype = hints->ai_socktype; | |
104 | else | |
105 | ai->ai_socktype = SOCK_STREAM; | |
106 | ||
107 | if (hints->ai_protocol) | |
108 | ai->ai_protocol = hints->ai_protocol; | |
109 | ||
9901cb37 | 110 | return (ai); |
48e671d5 | 111 | } |
112 | ||
9901cb37 | 113 | int |
114 | getaddrinfo(const char *hostname, const char *servname, | |
115 | const struct addrinfo *hints, struct addrinfo **res) | |
48e671d5 | 116 | { |
5daf7064 | 117 | struct hostent *hp; |
ecc03386 | 118 | struct servent *sp; |
9c13d877 | 119 | struct in_addr in; |
ecc03386 | 120 | int i; |
121 | long int port; | |
62086365 | 122 | u_long addr; |
5daf7064 | 123 | |
ecc03386 | 124 | port = 0; |
125 | if (servname != NULL) { | |
126 | char *cp; | |
127 | ||
128 | port = strtol(servname, &cp, 10); | |
129 | if (port > 0 && port <= 65535 && *cp == '\0') | |
130 | port = htons(port); | |
131 | else if ((sp = getservbyname(servname, NULL)) != NULL) | |
132 | port = sp->s_port; | |
133 | else | |
134 | port = 0; | |
135 | } | |
5daf7064 | 136 | |
137 | if (hints && hints->ai_flags & AI_PASSIVE) { | |
62086365 | 138 | addr = htonl(0x00000000); |
139 | if (hostname && inet_aton(hostname, &in) != 0) | |
140 | addr = in.s_addr; | |
9901cb37 | 141 | *res = malloc_ai(port, addr, hints); |
d6bd2b5a | 142 | if (*res == NULL) |
143 | return (EAI_MEMORY); | |
9901cb37 | 144 | return (0); |
5daf7064 | 145 | } |
146 | ||
147 | if (!hostname) { | |
9901cb37 | 148 | *res = malloc_ai(port, htonl(0x7f000001), hints); |
d6bd2b5a | 149 | if (*res == NULL) |
150 | return (EAI_MEMORY); | |
9901cb37 | 151 | return (0); |
5daf7064 | 152 | } |
153 | ||
dc2a6d09 | 154 | if (inet_aton(hostname, &in)) { |
9901cb37 | 155 | *res = malloc_ai(port, in.s_addr, hints); |
d6bd2b5a | 156 | if (*res == NULL) |
157 | return (EAI_MEMORY); | |
9901cb37 | 158 | return (0); |
5daf7064 | 159 | } |
160 | ||
02e2a074 | 161 | /* Don't try DNS if AI_NUMERICHOST is set */ |
162 | if (hints && hints->ai_flags & AI_NUMERICHOST) | |
163 | return (EAI_NONAME); | |
164 | ||
5daf7064 | 165 | hp = gethostbyname(hostname); |
166 | if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { | |
9901cb37 | 167 | struct addrinfo *cur, *prev; |
168 | ||
d6bd2b5a | 169 | cur = prev = *res = NULL; |
5daf7064 | 170 | for (i = 0; hp->h_addr_list[i]; i++) { |
9901cb37 | 171 | struct in_addr *in = (struct in_addr *)hp->h_addr_list[i]; |
172 | ||
173 | cur = malloc_ai(port, in->s_addr, hints); | |
d6bd2b5a | 174 | if (cur == NULL) { |
175 | if (*res != NULL) | |
176 | freeaddrinfo(*res); | |
177 | return (EAI_MEMORY); | |
178 | } | |
5daf7064 | 179 | if (prev) |
180 | prev->ai_next = cur; | |
181 | else | |
182 | *res = cur; | |
183 | ||
184 | prev = cur; | |
185 | } | |
9901cb37 | 186 | return (0); |
5daf7064 | 187 | } |
188 | ||
9901cb37 | 189 | return (EAI_NODATA); |
48e671d5 | 190 | } |
191 | #endif /* !HAVE_GETADDRINFO */ |