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" |
| 13 | #include "ssh.h" |
| 14 | |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 15 | RCSID("$Id: fake-getaddrinfo.c,v 1.2 2001/02/09 01:55:36 djm Exp $"); |
| 16 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 17 | #ifndef HAVE_GAI_STRERROR |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 18 | char *gai_strerror(int ecode) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 19 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 20 | switch (ecode) { |
| 21 | case EAI_NODATA: |
| 22 | return "no address associated with hostname."; |
| 23 | case EAI_MEMORY: |
| 24 | return "memory allocation failure."; |
| 25 | default: |
| 26 | return "unknown error."; |
| 27 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 28 | } |
| 29 | #endif /* !HAVE_GAI_STRERROR */ |
| 30 | |
| 31 | #ifndef HAVE_FREEADDRINFO |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 32 | void freeaddrinfo(struct addrinfo *ai) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 33 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 34 | struct addrinfo *next; |
| 35 | |
| 36 | do { |
| 37 | next = ai->ai_next; |
| 38 | free(ai); |
| 39 | } while (NULL != (ai = next)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 40 | } |
| 41 | #endif /* !HAVE_FREEADDRINFO */ |
| 42 | |
| 43 | #ifndef HAVE_GETADDRINFO |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 44 | static struct addrinfo *malloc_ai(int port, u_long addr) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 45 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 46 | struct addrinfo *ai; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 47 | |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 48 | ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in)); |
| 49 | if (ai == NULL) |
| 50 | return(NULL); |
| 51 | |
| 52 | memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in)); |
| 53 | |
| 54 | ai->ai_addr = (struct sockaddr *)(ai + 1); |
| 55 | /* XXX -- ssh doesn't use sa_len */ |
| 56 | ai->ai_addrlen = sizeof(struct sockaddr_in); |
| 57 | ai->ai_addr->sa_family = ai->ai_family = AF_INET; |
| 58 | |
| 59 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port; |
| 60 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr; |
| 61 | |
| 62 | return(ai); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 63 | } |
| 64 | |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 65 | int getaddrinfo(const char *hostname, const char *servname, |
| 66 | const struct addrinfo *hints, struct addrinfo **res) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 67 | { |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 68 | struct addrinfo *cur, *prev = NULL; |
| 69 | struct hostent *hp; |
Damien Miller | 8e394e7 | 2000-07-08 11:50:37 +1000 | [diff] [blame] | 70 | struct in_addr in; |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 71 | int i, port; |
| 72 | |
| 73 | if (servname) |
| 74 | port = htons(atoi(servname)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 75 | else |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 76 | port = 0; |
| 77 | |
| 78 | if (hints && hints->ai_flags & AI_PASSIVE) { |
| 79 | if (NULL != (*res = malloc_ai(port, htonl(0x00000000)))) |
| 80 | return 0; |
| 81 | else |
| 82 | return EAI_MEMORY; |
| 83 | } |
| 84 | |
| 85 | if (!hostname) { |
| 86 | if (NULL != (*res = malloc_ai(port, htonl(0x7f000001)))) |
| 87 | return 0; |
| 88 | else |
| 89 | return EAI_MEMORY; |
| 90 | } |
| 91 | |
Damien Miller | 7a0e5dc | 2000-07-11 12:15:54 +1000 | [diff] [blame] | 92 | if (inet_aton(hostname, &in)) { |
Damien Miller | 8e394e7 | 2000-07-08 11:50:37 +1000 | [diff] [blame] | 93 | if (NULL != (*res = malloc_ai(port, in.s_addr))) |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 94 | return 0; |
| 95 | else |
| 96 | return EAI_MEMORY; |
| 97 | } |
| 98 | |
| 99 | hp = gethostbyname(hostname); |
| 100 | if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { |
| 101 | for (i = 0; hp->h_addr_list[i]; i++) { |
| 102 | cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr); |
| 103 | if (cur == NULL) { |
| 104 | if (*res) |
| 105 | freeaddrinfo(*res); |
| 106 | return EAI_MEMORY; |
| 107 | } |
| 108 | |
| 109 | if (prev) |
| 110 | prev->ai_next = cur; |
| 111 | else |
| 112 | *res = cur; |
| 113 | |
| 114 | prev = cur; |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | return EAI_NODATA; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 120 | } |
| 121 | #endif /* !HAVE_GETADDRINFO */ |