summaryrefslogtreecommitdiff
path: root/openbsd-compat/fake-getaddrinfo.c
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/fake-getaddrinfo.c')
-rw-r--r--openbsd-compat/fake-getaddrinfo.c83
1 files changed, 38 insertions, 45 deletions
diff --git a/openbsd-compat/fake-getaddrinfo.c b/openbsd-compat/fake-getaddrinfo.c
index 34e1fce78..76b8a6274 100644
--- a/openbsd-compat/fake-getaddrinfo.c
+++ b/openbsd-compat/fake-getaddrinfo.c
@@ -10,47 +10,48 @@
10 */ 10 */
11 11
12#include "includes.h" 12#include "includes.h"
13#include "xmalloc.h"
13#include "ssh.h" 14#include "ssh.h"
14 15
15RCSID("$Id: fake-getaddrinfo.c,v 1.6 2003/05/14 11:48:51 dtucker Exp $"); 16RCSID("$Id: fake-getaddrinfo.c,v 1.7 2003/05/18 14:13:39 djm Exp $");
16 17
17#ifndef HAVE_GAI_STRERROR 18#ifndef HAVE_GAI_STRERROR
18char *gai_strerror(int ecode) 19char *
20gai_strerror(int err)
19{ 21{
20 switch (ecode) { 22 switch (err) {
21 case EAI_NODATA: 23 case EAI_NODATA:
22 return "no address associated with hostname."; 24 return ("no address associated with name");
23 case EAI_MEMORY: 25 case EAI_MEMORY:
24 return "memory allocation failure."; 26 return ("memory allocation failure.");
25 default: 27 default:
26 return "unknown error."; 28 return ("unknown/invalid error.");
27 } 29 }
28} 30}
29#endif /* !HAVE_GAI_STRERROR */ 31#endif /* !HAVE_GAI_STRERROR */
30 32
31#ifndef HAVE_FREEADDRINFO 33#ifndef HAVE_FREEADDRINFO
32void freeaddrinfo(struct addrinfo *ai) 34void
35freeaddrinfo(struct addrinfo *ai)
33{ 36{
34 struct addrinfo *next; 37 struct addrinfo *next;
35 38
36 do { 39 for(;ai != NULL; next = ai->ai_next) {
37 next = ai->ai_next;
38 free(ai); 40 free(ai);
39 } while (NULL != (ai = next)); 41 ai = next;
42 }
40} 43}
41#endif /* !HAVE_FREEADDRINFO */ 44#endif /* !HAVE_FREEADDRINFO */
42 45
43#ifndef HAVE_GETADDRINFO 46#ifndef HAVE_GETADDRINFO
44static struct addrinfo *malloc_ai(int port, u_long addr, 47static struct
45 const struct addrinfo *hints) 48addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints)
46{ 49{
47 struct addrinfo *ai; 50 struct addrinfo *ai;
48 51
49 ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in)); 52 ai = xmalloc(sizeof(*ai) + sizeof(struct sockaddr_in));
50 if (ai == NULL)
51 return(NULL);
52 53
53 memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in)); 54 memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in));
54 55
55 ai->ai_addr = (struct sockaddr *)(ai + 1); 56 ai->ai_addr = (struct sockaddr *)(ai + 1);
56 /* XXX -- ssh doesn't use sa_len */ 57 /* XXX -- ssh doesn't use sa_len */
@@ -69,13 +70,13 @@ static struct addrinfo *malloc_ai(int port, u_long addr,
69 if (hints->ai_protocol) 70 if (hints->ai_protocol)
70 ai->ai_protocol = hints->ai_protocol; 71 ai->ai_protocol = hints->ai_protocol;
71 72
72 return(ai); 73 return (ai);
73} 74}
74 75
75int getaddrinfo(const char *hostname, const char *servname, 76int
76 const struct addrinfo *hints, struct addrinfo **res) 77getaddrinfo(const char *hostname, const char *servname,
78 const struct addrinfo *hints, struct addrinfo **res)
77{ 79{
78 struct addrinfo *cur, *prev = NULL;
79 struct hostent *hp; 80 struct hostent *hp;
80 struct servent *sp; 81 struct servent *sp;
81 struct in_addr in; 82 struct in_addr in;
@@ -100,37 +101,29 @@ int getaddrinfo(const char *hostname, const char *servname,
100 addr = htonl(0x00000000); 101 addr = htonl(0x00000000);
101 if (hostname && inet_aton(hostname, &in) != 0) 102 if (hostname && inet_aton(hostname, &in) != 0)
102 addr = in.s_addr; 103 addr = in.s_addr;
103 if (NULL != (*res = malloc_ai(port, addr, hints))) 104 *res = malloc_ai(port, addr, hints);
104 return 0; 105 return (0);
105 else
106 return EAI_MEMORY;
107 } 106 }
108 107
109 if (!hostname) { 108 if (!hostname) {
110 if (NULL != (*res = malloc_ai(port, htonl(0x7f000001), hints))) 109 *res = malloc_ai(port, htonl(0x7f000001), hints);
111 return 0; 110 return (0);
112 else
113 return EAI_MEMORY;
114 } 111 }
115 112
116 if (inet_aton(hostname, &in)) { 113 if (inet_aton(hostname, &in)) {
117 if (NULL != (*res = malloc_ai(port, in.s_addr, hints))) 114 *res = malloc_ai(port, in.s_addr, hints);
118 return 0; 115 return (0);
119 else
120 return EAI_MEMORY;
121 } 116 }
122 117
123 hp = gethostbyname(hostname); 118 hp = gethostbyname(hostname);
124 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { 119 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
120 struct addrinfo *cur, *prev;
121
122 cur = prev = NULL;
125 for (i = 0; hp->h_addr_list[i]; i++) { 123 for (i = 0; hp->h_addr_list[i]; i++) {
126 cur = malloc_ai(port, 124 struct in_addr *in = (struct in_addr *)hp->h_addr_list[i];
127 ((struct in_addr *)hp->h_addr_list[i])->s_addr, hints); 125
128 if (cur == NULL) { 126 cur = malloc_ai(port, in->s_addr, hints);
129 if (*res)
130 freeaddrinfo(*res);
131 return EAI_MEMORY;
132 }
133
134 if (prev) 127 if (prev)
135 prev->ai_next = cur; 128 prev->ai_next = cur;
136 else 129 else
@@ -138,9 +131,9 @@ int getaddrinfo(const char *hostname, const char *servname,
138 131
139 prev = cur; 132 prev = cur;
140 } 133 }
141 return 0; 134 return (0);
142 } 135 }
143 136
144 return EAI_NODATA; 137 return (EAI_NODATA);
145} 138}
146#endif /* !HAVE_GETADDRINFO */ 139#endif /* !HAVE_GETADDRINFO */