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