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