Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1 | /* |
Damien Miller | c28e38d | 2003-06-05 18:52:47 +1000 | [diff] [blame^] | 2 | * Pseudo-implementation of RFC2553 name / address resolution functions |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 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. |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 7 | */ |
| 8 | |
Damien Miller | c28e38d | 2003-06-05 18:52:47 +1000 | [diff] [blame^] | 9 | RCSID("$Id: fake-rfc2553.c,v 1.1 2003/06/05 08:52:48 djm Exp $"); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 10 | |
Damien Miller | c28e38d | 2003-06-05 18:52:47 +1000 | [diff] [blame^] | 11 | #ifndef HAVE_GETNAMEINFO |
| 12 | int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, |
| 13 | size_t hostlen, char *serv, size_t servlen, int flags) |
| 14 | { |
| 15 | struct sockaddr_in *sin = (struct sockaddr_in *)sa; |
| 16 | struct hostent *hp; |
| 17 | char tmpserv[16]; |
| 18 | |
| 19 | if (serv != NULL) { |
| 20 | snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port)); |
| 21 | if (strlcpy(serv, tmpserv, servlen) >= servlen) |
| 22 | return (EAI_MEMORY); |
| 23 | } |
| 24 | |
| 25 | if (host != NULL) { |
| 26 | if (flags & NI_NUMERICHOST) { |
| 27 | if (strlcpy(host, inet_ntoa(sin->sin_addr), |
| 28 | hostlen) >= hostlen) |
| 29 | return (EAI_MEMORY); |
| 30 | else |
| 31 | return (0); |
| 32 | } else { |
| 33 | hp = gethostbyaddr((char *)&sin->sin_addr, |
| 34 | sizeof(struct in_addr), AF_INET); |
| 35 | if (hp == NULL) |
| 36 | return (EAI_NODATA); |
| 37 | |
| 38 | if (strlcpy(host, hp->h_name, hostlen) >= hostlen) |
| 39 | return (EAI_MEMORY); |
| 40 | else |
| 41 | return (0); |
| 42 | } |
| 43 | } |
| 44 | return (0); |
| 45 | } |
| 46 | #endif /* !HAVE_GETNAMEINFO */ |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 47 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 48 | #ifndef HAVE_GAI_STRERROR |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 49 | char * |
| 50 | gai_strerror(int err) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 51 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 52 | switch (err) { |
| 53 | case EAI_NODATA: |
| 54 | return ("no address associated with name"); |
| 55 | case EAI_MEMORY: |
| 56 | return ("memory allocation failure."); |
Damien Miller | 10eac0c | 2003-06-05 09:48:32 +1000 | [diff] [blame] | 57 | case EAI_NONAME: |
| 58 | return ("nodename nor servname provided, or not known"); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 59 | default: |
| 60 | return ("unknown/invalid error."); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 61 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 62 | } |
| 63 | #endif /* !HAVE_GAI_STRERROR */ |
| 64 | |
| 65 | #ifndef HAVE_FREEADDRINFO |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 66 | void |
| 67 | freeaddrinfo(struct addrinfo *ai) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 68 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 69 | struct addrinfo *next; |
| 70 | |
Damien Miller | c322bba | 2003-05-19 10:39:37 +1000 | [diff] [blame] | 71 | for(; ai != NULL;) { |
| 72 | next = ai->ai_next; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 73 | free(ai); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 74 | ai = next; |
| 75 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 76 | } |
| 77 | #endif /* !HAVE_FREEADDRINFO */ |
| 78 | |
| 79 | #ifndef HAVE_GETADDRINFO |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 80 | static struct |
| 81 | addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 82 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 83 | struct addrinfo *ai; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 84 | |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 85 | ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in)); |
| 86 | if (ai == NULL) |
| 87 | return (NULL); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 88 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 89 | memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in)); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 90 | |
| 91 | ai->ai_addr = (struct sockaddr *)(ai + 1); |
| 92 | /* XXX -- ssh doesn't use sa_len */ |
| 93 | ai->ai_addrlen = sizeof(struct sockaddr_in); |
| 94 | ai->ai_addr->sa_family = ai->ai_family = AF_INET; |
| 95 | |
| 96 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port; |
| 97 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr; |
| 98 | |
Darren Tucker | abef562 | 2003-05-14 21:48:51 +1000 | [diff] [blame] | 99 | /* XXX: the following is not generally correct, but does what we want */ |
| 100 | if (hints->ai_socktype) |
| 101 | ai->ai_socktype = hints->ai_socktype; |
| 102 | else |
| 103 | ai->ai_socktype = SOCK_STREAM; |
| 104 | |
| 105 | if (hints->ai_protocol) |
| 106 | ai->ai_protocol = hints->ai_protocol; |
| 107 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 108 | return (ai); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 109 | } |
| 110 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 111 | int |
| 112 | getaddrinfo(const char *hostname, const char *servname, |
| 113 | const struct addrinfo *hints, struct addrinfo **res) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 114 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 115 | struct hostent *hp; |
Damien Miller | 850b942 | 2003-02-06 10:50:42 +1100 | [diff] [blame] | 116 | struct servent *sp; |
Damien Miller | 8e394e7 | 2000-07-08 11:50:37 +1000 | [diff] [blame] | 117 | struct in_addr in; |
Damien Miller | 850b942 | 2003-02-06 10:50:42 +1100 | [diff] [blame] | 118 | int i; |
| 119 | long int port; |
Damien Miller | 62b6b17 | 2003-03-24 13:35:58 +1100 | [diff] [blame] | 120 | u_long addr; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 121 | |
Damien Miller | 850b942 | 2003-02-06 10:50:42 +1100 | [diff] [blame] | 122 | port = 0; |
| 123 | if (servname != NULL) { |
| 124 | char *cp; |
| 125 | |
| 126 | port = strtol(servname, &cp, 10); |
| 127 | if (port > 0 && port <= 65535 && *cp == '\0') |
| 128 | port = htons(port); |
| 129 | else if ((sp = getservbyname(servname, NULL)) != NULL) |
| 130 | port = sp->s_port; |
| 131 | else |
| 132 | port = 0; |
| 133 | } |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 134 | |
| 135 | if (hints && hints->ai_flags & AI_PASSIVE) { |
Damien Miller | 62b6b17 | 2003-03-24 13:35:58 +1100 | [diff] [blame] | 136 | addr = htonl(0x00000000); |
| 137 | if (hostname && inet_aton(hostname, &in) != 0) |
| 138 | addr = in.s_addr; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 139 | *res = malloc_ai(port, addr, hints); |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 140 | if (*res == NULL) |
| 141 | return (EAI_MEMORY); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 142 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if (!hostname) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 146 | *res = malloc_ai(port, htonl(0x7f000001), hints); |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 147 | if (*res == NULL) |
| 148 | return (EAI_MEMORY); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 149 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 150 | } |
| 151 | |
Damien Miller | 7a0e5dc | 2000-07-11 12:15:54 +1000 | [diff] [blame] | 152 | if (inet_aton(hostname, &in)) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 153 | *res = malloc_ai(port, in.s_addr, hints); |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 154 | if (*res == NULL) |
| 155 | return (EAI_MEMORY); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 156 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 157 | } |
| 158 | |
Damien Miller | 10eac0c | 2003-06-05 09:48:32 +1000 | [diff] [blame] | 159 | /* Don't try DNS if AI_NUMERICHOST is set */ |
| 160 | if (hints && hints->ai_flags & AI_NUMERICHOST) |
| 161 | return (EAI_NONAME); |
| 162 | |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 163 | hp = gethostbyname(hostname); |
| 164 | if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 165 | struct addrinfo *cur, *prev; |
| 166 | |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 167 | cur = prev = *res = NULL; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 168 | for (i = 0; hp->h_addr_list[i]; i++) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 169 | struct in_addr *in = (struct in_addr *)hp->h_addr_list[i]; |
| 170 | |
| 171 | cur = malloc_ai(port, in->s_addr, hints); |
Damien Miller | b95bb7f | 2003-06-05 10:04:12 +1000 | [diff] [blame] | 172 | if (cur == NULL) { |
| 173 | if (*res != NULL) |
| 174 | freeaddrinfo(*res); |
| 175 | return (EAI_MEMORY); |
| 176 | } |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 177 | if (prev) |
| 178 | prev->ai_next = cur; |
| 179 | else |
| 180 | *res = cur; |
| 181 | |
| 182 | prev = cur; |
| 183 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 184 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 185 | } |
| 186 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 187 | return (EAI_NODATA); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 188 | } |
| 189 | #endif /* !HAVE_GETADDRINFO */ |