]> andersk Git - openssh.git/blob - fake-getaddrinfo.c
- Cleanup of auth.c, login.c and fake-*
[openssh.git] / fake-getaddrinfo.c
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 #ifndef HAVE_GAI_STRERROR
16 char *gai_strerror(int ecode)
17 {
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         }
26 }    
27 #endif /* !HAVE_GAI_STRERROR */
28
29 #ifndef HAVE_FREEADDRINFO
30 void freeaddrinfo(struct addrinfo *ai)
31 {
32         struct addrinfo *next;
33
34         do {
35                 next = ai->ai_next;
36                 free(ai);
37         } while (NULL != (ai = next));
38 }
39 #endif /* !HAVE_FREEADDRINFO */
40
41 #ifndef HAVE_GETADDRINFO
42 static struct addrinfo *malloc_ai(int port, u_long addr)
43 {
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;
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);
61 }
62
63 int getaddrinfo(const char *hostname, const char *servname, 
64                 const struct addrinfo *hints, struct addrinfo **res)
65 {
66         struct addrinfo *cur, *prev = NULL;
67         struct hostent *hp;
68         int i, port;
69
70         if (servname)
71                 port = htons(atoi(servname));
72         else
73                 port = 0;
74
75         if (hints && hints->ai_flags & AI_PASSIVE) {
76                 if (NULL != (*res = malloc_ai(port, htonl(0x00000000))))
77                         return 0;
78                 else
79                         return EAI_MEMORY;
80         }
81                 
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;
117 }
118 #endif /* !HAVE_GETADDRINFO */
This page took 0.0797 seconds and 5 git commands to generate.