diff options
Diffstat (limited to 'openbsd-compat/fake-getaddrinfo.c')
-rw-r--r-- | openbsd-compat/fake-getaddrinfo.c | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/openbsd-compat/fake-getaddrinfo.c b/openbsd-compat/fake-getaddrinfo.c new file mode 100644 index 000000000..e63bda970 --- /dev/null +++ b/openbsd-compat/fake-getaddrinfo.c | |||
@@ -0,0 +1,135 @@ | |||
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. | ||
10 | */ | ||
11 | |||
12 | #include "includes.h" | ||
13 | #include "ssh.h" | ||
14 | |||
15 | RCSID("$Id: fake-getaddrinfo.c,v 1.5 2003/03/24 02:35:59 djm Exp $"); | ||
16 | |||
17 | #ifndef HAVE_GAI_STRERROR | ||
18 | char *gai_strerror(int ecode) | ||
19 | { | ||
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 | } | ||
28 | } | ||
29 | #endif /* !HAVE_GAI_STRERROR */ | ||
30 | |||
31 | #ifndef HAVE_FREEADDRINFO | ||
32 | void freeaddrinfo(struct addrinfo *ai) | ||
33 | { | ||
34 | struct addrinfo *next; | ||
35 | |||
36 | do { | ||
37 | next = ai->ai_next; | ||
38 | free(ai); | ||
39 | } while (NULL != (ai = next)); | ||
40 | } | ||
41 | #endif /* !HAVE_FREEADDRINFO */ | ||
42 | |||
43 | #ifndef HAVE_GETADDRINFO | ||
44 | static struct addrinfo *malloc_ai(int port, u_long addr) | ||
45 | { | ||
46 | struct addrinfo *ai; | ||
47 | |||
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); | ||
63 | } | ||
64 | |||
65 | int getaddrinfo(const char *hostname, const char *servname, | ||
66 | const struct addrinfo *hints, struct addrinfo **res) | ||
67 | { | ||
68 | struct addrinfo *cur, *prev = NULL; | ||
69 | struct hostent *hp; | ||
70 | struct servent *sp; | ||
71 | struct in_addr in; | ||
72 | int i; | ||
73 | long int port; | ||
74 | u_long addr; | ||
75 | |||
76 | port = 0; | ||
77 | if (servname != NULL) { | ||
78 | char *cp; | ||
79 | |||
80 | port = strtol(servname, &cp, 10); | ||
81 | if (port > 0 && port <= 65535 && *cp == '\0') | ||
82 | port = htons(port); | ||
83 | else if ((sp = getservbyname(servname, NULL)) != NULL) | ||
84 | port = sp->s_port; | ||
85 | else | ||
86 | port = 0; | ||
87 | } | ||
88 | |||
89 | if (hints && hints->ai_flags & AI_PASSIVE) { | ||
90 | addr = htonl(0x00000000); | ||
91 | if (hostname && inet_aton(hostname, &in) != 0) | ||
92 | addr = in.s_addr; | ||
93 | if (NULL != (*res = malloc_ai(port, addr))) | ||
94 | return 0; | ||
95 | else | ||
96 | return EAI_MEMORY; | ||
97 | } | ||
98 | |||
99 | if (!hostname) { | ||
100 | if (NULL != (*res = malloc_ai(port, htonl(0x7f000001)))) | ||
101 | return 0; | ||
102 | else | ||
103 | return EAI_MEMORY; | ||
104 | } | ||
105 | |||
106 | if (inet_aton(hostname, &in)) { | ||
107 | if (NULL != (*res = malloc_ai(port, in.s_addr))) | ||
108 | return 0; | ||
109 | else | ||
110 | return EAI_MEMORY; | ||
111 | } | ||
112 | |||
113 | hp = gethostbyname(hostname); | ||
114 | if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { | ||
115 | for (i = 0; hp->h_addr_list[i]; i++) { | ||
116 | cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr); | ||
117 | if (cur == NULL) { | ||
118 | if (*res) | ||
119 | freeaddrinfo(*res); | ||
120 | return EAI_MEMORY; | ||
121 | } | ||
122 | |||
123 | if (prev) | ||
124 | prev->ai_next = cur; | ||
125 | else | ||
126 | *res = cur; | ||
127 | |||
128 | prev = cur; | ||
129 | } | ||
130 | return 0; | ||
131 | } | ||
132 | |||
133 | return EAI_NODATA; | ||
134 | } | ||
135 | #endif /* !HAVE_GETADDRINFO */ | ||