]>
Commit | Line | Data |
---|---|---|
48e671d5 | 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. | |
48e671d5 | 10 | */ |
11 | ||
12 | #include "includes.h" | |
13 | #include "ssh.h" | |
14 | ||
0b202697 | 15 | RCSID("$Id$"); |
16 | ||
48e671d5 | 17 | #ifndef HAVE_GAI_STRERROR |
4a35ae8b | 18 | char *gai_strerror(int ecode) |
48e671d5 | 19 | { |
5daf7064 | 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 | } | |
48e671d5 | 28 | } |
29 | #endif /* !HAVE_GAI_STRERROR */ | |
30 | ||
31 | #ifndef HAVE_FREEADDRINFO | |
5daf7064 | 32 | void freeaddrinfo(struct addrinfo *ai) |
48e671d5 | 33 | { |
5daf7064 | 34 | struct addrinfo *next; |
35 | ||
36 | do { | |
37 | next = ai->ai_next; | |
38 | free(ai); | |
39 | } while (NULL != (ai = next)); | |
48e671d5 | 40 | } |
41 | #endif /* !HAVE_FREEADDRINFO */ | |
42 | ||
43 | #ifndef HAVE_GETADDRINFO | |
5daf7064 | 44 | static struct addrinfo *malloc_ai(int port, u_long addr) |
48e671d5 | 45 | { |
5daf7064 | 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; | |
48e671d5 | 58 | |
5daf7064 | 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); | |
48e671d5 | 63 | } |
64 | ||
5daf7064 | 65 | int getaddrinfo(const char *hostname, const char *servname, |
66 | const struct addrinfo *hints, struct addrinfo **res) | |
48e671d5 | 67 | { |
5daf7064 | 68 | struct addrinfo *cur, *prev = NULL; |
69 | struct hostent *hp; | |
ecc03386 | 70 | struct servent *sp; |
9c13d877 | 71 | struct in_addr in; |
ecc03386 | 72 | int i; |
73 | long int port; | |
5daf7064 | 74 | |
ecc03386 | 75 | port = 0; |
76 | if (servname != NULL) { | |
77 | char *cp; | |
78 | ||
79 | port = strtol(servname, &cp, 10); | |
80 | if (port > 0 && port <= 65535 && *cp == '\0') | |
81 | port = htons(port); | |
82 | else if ((sp = getservbyname(servname, NULL)) != NULL) | |
83 | port = sp->s_port; | |
84 | else | |
85 | port = 0; | |
86 | } | |
5daf7064 | 87 | |
88 | if (hints && hints->ai_flags & AI_PASSIVE) { | |
89 | if (NULL != (*res = malloc_ai(port, htonl(0x00000000)))) | |
90 | return 0; | |
91 | else | |
92 | return EAI_MEMORY; | |
93 | } | |
94 | ||
95 | if (!hostname) { | |
96 | if (NULL != (*res = malloc_ai(port, htonl(0x7f000001)))) | |
97 | return 0; | |
98 | else | |
99 | return EAI_MEMORY; | |
100 | } | |
101 | ||
dc2a6d09 | 102 | if (inet_aton(hostname, &in)) { |
9c13d877 | 103 | if (NULL != (*res = malloc_ai(port, in.s_addr))) |
5daf7064 | 104 | return 0; |
105 | else | |
106 | return EAI_MEMORY; | |
107 | } | |
108 | ||
109 | hp = gethostbyname(hostname); | |
110 | if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { | |
111 | for (i = 0; hp->h_addr_list[i]; i++) { | |
112 | cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr); | |
113 | if (cur == NULL) { | |
114 | if (*res) | |
115 | freeaddrinfo(*res); | |
116 | return EAI_MEMORY; | |
117 | } | |
118 | ||
119 | if (prev) | |
120 | prev->ai_next = cur; | |
121 | else | |
122 | *res = cur; | |
123 | ||
124 | prev = cur; | |
125 | } | |
126 | return 0; | |
127 | } | |
128 | ||
129 | return EAI_NODATA; | |
48e671d5 | 130 | } |
131 | #endif /* !HAVE_GETADDRINFO */ |