Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 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. |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include "includes.h" |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 13 | #include "xmalloc.h" |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 14 | #include "ssh.h" |
| 15 | |
Damien Miller | c322bba | 2003-05-19 10:39:37 +1000 | [diff] [blame^] | 16 | RCSID("$Id: fake-getaddrinfo.c,v 1.8 2003/05/19 00:39:37 djm Exp $"); |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 17 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 18 | #ifndef HAVE_GAI_STRERROR |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 19 | char * |
| 20 | gai_strerror(int err) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 21 | { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 22 | switch (err) { |
| 23 | case EAI_NODATA: |
| 24 | return ("no address associated with name"); |
| 25 | case EAI_MEMORY: |
| 26 | return ("memory allocation failure."); |
| 27 | default: |
| 28 | return ("unknown/invalid error."); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 29 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 30 | } |
| 31 | #endif /* !HAVE_GAI_STRERROR */ |
| 32 | |
| 33 | #ifndef HAVE_FREEADDRINFO |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 34 | void |
| 35 | freeaddrinfo(struct addrinfo *ai) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 36 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 37 | struct addrinfo *next; |
| 38 | |
Damien Miller | c322bba | 2003-05-19 10:39:37 +1000 | [diff] [blame^] | 39 | for(; ai != NULL;) { |
| 40 | next = ai->ai_next; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 41 | free(ai); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 42 | ai = next; |
| 43 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 44 | } |
| 45 | #endif /* !HAVE_FREEADDRINFO */ |
| 46 | |
| 47 | #ifndef HAVE_GETADDRINFO |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 48 | static struct |
| 49 | addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 50 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 51 | struct addrinfo *ai; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 52 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 53 | ai = xmalloc(sizeof(*ai) + sizeof(struct sockaddr_in)); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 54 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 55 | memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in)); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 56 | |
| 57 | ai->ai_addr = (struct sockaddr *)(ai + 1); |
| 58 | /* XXX -- ssh doesn't use sa_len */ |
| 59 | ai->ai_addrlen = sizeof(struct sockaddr_in); |
| 60 | ai->ai_addr->sa_family = ai->ai_family = AF_INET; |
| 61 | |
| 62 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port; |
| 63 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr; |
| 64 | |
Darren Tucker | abef562 | 2003-05-14 21:48:51 +1000 | [diff] [blame] | 65 | /* XXX: the following is not generally correct, but does what we want */ |
| 66 | if (hints->ai_socktype) |
| 67 | ai->ai_socktype = hints->ai_socktype; |
| 68 | else |
| 69 | ai->ai_socktype = SOCK_STREAM; |
| 70 | |
| 71 | if (hints->ai_protocol) |
| 72 | ai->ai_protocol = hints->ai_protocol; |
| 73 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 74 | return (ai); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 75 | } |
| 76 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 77 | int |
| 78 | getaddrinfo(const char *hostname, const char *servname, |
| 79 | const struct addrinfo *hints, struct addrinfo **res) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 80 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 81 | struct hostent *hp; |
Damien Miller | 850b942 | 2003-02-06 10:50:42 +1100 | [diff] [blame] | 82 | struct servent *sp; |
Damien Miller | 8e394e7 | 2000-07-08 11:50:37 +1000 | [diff] [blame] | 83 | struct in_addr in; |
Damien Miller | 850b942 | 2003-02-06 10:50:42 +1100 | [diff] [blame] | 84 | int i; |
| 85 | long int port; |
Damien Miller | 62b6b17 | 2003-03-24 13:35:58 +1100 | [diff] [blame] | 86 | u_long addr; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 87 | |
Damien Miller | 850b942 | 2003-02-06 10:50:42 +1100 | [diff] [blame] | 88 | port = 0; |
| 89 | if (servname != NULL) { |
| 90 | char *cp; |
| 91 | |
| 92 | port = strtol(servname, &cp, 10); |
| 93 | if (port > 0 && port <= 65535 && *cp == '\0') |
| 94 | port = htons(port); |
| 95 | else if ((sp = getservbyname(servname, NULL)) != NULL) |
| 96 | port = sp->s_port; |
| 97 | else |
| 98 | port = 0; |
| 99 | } |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 100 | |
| 101 | if (hints && hints->ai_flags & AI_PASSIVE) { |
Damien Miller | 62b6b17 | 2003-03-24 13:35:58 +1100 | [diff] [blame] | 102 | addr = htonl(0x00000000); |
| 103 | if (hostname && inet_aton(hostname, &in) != 0) |
| 104 | addr = in.s_addr; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 105 | *res = malloc_ai(port, addr, hints); |
| 106 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (!hostname) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 110 | *res = malloc_ai(port, htonl(0x7f000001), hints); |
| 111 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 112 | } |
| 113 | |
Damien Miller | 7a0e5dc | 2000-07-11 12:15:54 +1000 | [diff] [blame] | 114 | if (inet_aton(hostname, &in)) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 115 | *res = malloc_ai(port, in.s_addr, hints); |
| 116 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | hp = gethostbyname(hostname); |
| 120 | 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] | 121 | struct addrinfo *cur, *prev; |
| 122 | |
| 123 | cur = prev = NULL; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 124 | for (i = 0; hp->h_addr_list[i]; i++) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 125 | struct in_addr *in = (struct in_addr *)hp->h_addr_list[i]; |
| 126 | |
| 127 | cur = malloc_ai(port, in->s_addr, hints); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 128 | if (prev) |
| 129 | prev->ai_next = cur; |
| 130 | else |
| 131 | *res = cur; |
| 132 | |
| 133 | prev = cur; |
| 134 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 135 | return (0); |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 136 | } |
| 137 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 138 | return (EAI_NODATA); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 139 | } |
| 140 | #endif /* !HAVE_GETADDRINFO */ |