diff options
Diffstat (limited to 'fake-getaddrinfo.c')
-rw-r--r-- | fake-getaddrinfo.c | 171 |
1 files changed, 85 insertions, 86 deletions
diff --git a/fake-getaddrinfo.c b/fake-getaddrinfo.c index 456c41e58..b3af4aa2e 100644 --- a/fake-getaddrinfo.c +++ b/fake-getaddrinfo.c | |||
@@ -7,113 +7,112 @@ | |||
7 | * But these functions are not implemented correctly. The minimum subset | 7 | * But these functions are not implemented correctly. The minimum subset |
8 | * is implemented for ssh use only. For exapmle, this routine assumes | 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. | 9 | * that ai_family is AF_INET. Don't use it for another purpose. |
10 | * | ||
11 | * In the case not using 'configure --enable-ipv6', this getaddrinfo.c | ||
12 | * will be used if you have broken getaddrinfo or no getaddrinfo. | ||
13 | */ | 10 | */ |
14 | 11 | ||
15 | #include "includes.h" | 12 | #include "includes.h" |
16 | #include "ssh.h" | 13 | #include "ssh.h" |
17 | 14 | ||
18 | #ifndef HAVE_GAI_STRERROR | 15 | #ifndef HAVE_GAI_STRERROR |
19 | char * | 16 | char *gai_strerror(int ecode) |
20 | gai_strerror(ecode) | ||
21 | int ecode; | ||
22 | { | 17 | { |
23 | switch (ecode) { | 18 | switch (ecode) { |
24 | case EAI_NODATA: | 19 | case EAI_NODATA: |
25 | return "no address associated with hostname."; | 20 | return "no address associated with hostname."; |
26 | case EAI_MEMORY: | 21 | case EAI_MEMORY: |
27 | return "memory allocation failure."; | 22 | return "memory allocation failure."; |
28 | default: | 23 | default: |
29 | return "unknown error."; | 24 | return "unknown error."; |
30 | } | 25 | } |
31 | } | 26 | } |
32 | #endif /* !HAVE_GAI_STRERROR */ | 27 | #endif /* !HAVE_GAI_STRERROR */ |
33 | 28 | ||
34 | #ifndef HAVE_FREEADDRINFO | 29 | #ifndef HAVE_FREEADDRINFO |
35 | void | 30 | void freeaddrinfo(struct addrinfo *ai) |
36 | freeaddrinfo(ai) | ||
37 | struct addrinfo *ai; | ||
38 | { | 31 | { |
39 | struct addrinfo *next; | 32 | struct addrinfo *next; |
40 | 33 | ||
41 | do { | 34 | do { |
42 | next = ai->ai_next; | 35 | next = ai->ai_next; |
43 | free(ai); | 36 | free(ai); |
44 | } while (NULL != (ai = next)); | 37 | } while (NULL != (ai = next)); |
45 | } | 38 | } |
46 | #endif /* !HAVE_FREEADDRINFO */ | 39 | #endif /* !HAVE_FREEADDRINFO */ |
47 | 40 | ||
48 | #ifndef HAVE_GETADDRINFO | 41 | #ifndef HAVE_GETADDRINFO |
49 | static struct addrinfo * | 42 | static struct addrinfo *malloc_ai(int port, u_long addr) |
50 | malloc_ai(port, addr) | ||
51 | int port; | ||
52 | u_long addr; | ||
53 | { | 43 | { |
54 | struct addrinfo *ai; | 44 | struct addrinfo *ai; |
45 | |||
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; | ||
55 | 56 | ||
56 | if (NULL != (ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + | 57 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port; |
57 | sizeof(struct sockaddr_in)))) { | 58 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr; |
58 | memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in)); | 59 | |
59 | ai->ai_addr = (struct sockaddr *)(ai + 1); | 60 | return(ai); |
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; | ||
63 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port; | ||
64 | ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr; | ||
65 | return ai; | ||
66 | } else { | ||
67 | return NULL; | ||
68 | } | ||
69 | } | 61 | } |
70 | 62 | ||
71 | int | 63 | int getaddrinfo(const char *hostname, const char *servname, |
72 | getaddrinfo(hostname, servname, hints, res) | 64 | const struct addrinfo *hints, struct addrinfo **res) |
73 | const char *hostname, *servname; | ||
74 | const struct addrinfo *hints; | ||
75 | struct addrinfo **res; | ||
76 | { | 65 | { |
77 | struct addrinfo *cur, *prev = NULL; | 66 | struct addrinfo *cur, *prev = NULL; |
78 | struct hostent *hp; | 67 | struct hostent *hp; |
79 | int i, port; | 68 | int i, port; |
80 | 69 | ||
81 | if (servname) | 70 | if (servname) |
82 | port = htons(atoi(servname)); | 71 | port = htons(atoi(servname)); |
83 | else | ||
84 | port = 0; | ||
85 | if (hints && hints->ai_flags & AI_PASSIVE) | ||
86 | if (NULL != (*res = malloc_ai(port, htonl(0x00000000)))) | ||
87 | return 0; | ||
88 | else | ||
89 | return EAI_MEMORY; | ||
90 | if (!hostname) | ||
91 | if (NULL != (*res = malloc_ai(port, htonl(0x7f000001)))) | ||
92 | return 0; | ||
93 | else | ||
94 | return EAI_MEMORY; | ||
95 | if (inet_addr(hostname) != -1) | ||
96 | if (NULL != (*res = malloc_ai(port, inet_addr(hostname)))) | ||
97 | return 0; | ||
98 | else | ||
99 | return EAI_MEMORY; | ||
100 | if ((hp = gethostbyname(hostname)) && | ||
101 | hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { | ||
102 | for (i = 0; hp->h_addr_list[i]; i++) | ||
103 | if (NULL != (cur = malloc_ai(port, | ||
104 | ((struct in_addr *)hp->h_addr_list[i])->s_addr))) { | ||
105 | if (prev) | ||
106 | prev->ai_next = cur; | ||
107 | else | 72 | else |
108 | *res = cur; | 73 | port = 0; |
109 | prev = cur; | 74 | |
110 | } else { | 75 | if (hints && hints->ai_flags & AI_PASSIVE) { |
111 | if (*res) | 76 | if (NULL != (*res = malloc_ai(port, htonl(0x00000000)))) |
112 | freeaddrinfo(*res); | 77 | return 0; |
113 | return EAI_MEMORY; | 78 | else |
114 | } | 79 | return EAI_MEMORY; |
115 | return 0; | 80 | } |
116 | } | 81 | |
117 | return EAI_NODATA; | 82 | if (!hostname) { |
83 | if (NULL != (*res = malloc_ai(port, htonl(0x7f000001)))) | ||
84 | return 0; | ||
85 | else | ||
86 | return EAI_MEMORY; | ||
87 | } | ||
88 | |||
89 | if (inet_addr(hostname) != -1) { | ||
90 | if (NULL != (*res = malloc_ai(port, inet_addr(hostname)))) | ||
91 | return 0; | ||
92 | else | ||
93 | return EAI_MEMORY; | ||
94 | } | ||
95 | |||
96 | hp = gethostbyname(hostname); | ||
97 | if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { | ||
98 | for (i = 0; hp->h_addr_list[i]; i++) { | ||
99 | cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr); | ||
100 | if (cur == NULL) { | ||
101 | if (*res) | ||
102 | freeaddrinfo(*res); | ||
103 | return EAI_MEMORY; | ||
104 | } | ||
105 | |||
106 | if (prev) | ||
107 | prev->ai_next = cur; | ||
108 | else | ||
109 | *res = cur; | ||
110 | |||
111 | prev = cur; | ||
112 | } | ||
113 | return 0; | ||
114 | } | ||
115 | |||
116 | return EAI_NODATA; | ||
118 | } | 117 | } |
119 | #endif /* !HAVE_GETADDRINFO */ | 118 | #endif /* !HAVE_GETADDRINFO */ |