"Fossies" - the Fresh Open Source Software Archive 
Member "scanssh-2.1/getaddrinfo.c" (31 Mar 2004, 2286 Bytes) of package /linux/privat/old/scanssh-2.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
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 <sys/types.h>
13 #include <sys/socket.h>
14 #include <netdb.h>
15
16 #include "config.h"
17
18 void freeaddrinfo(struct addrinfo *ai)
19 {
20 struct addrinfo *next;
21
22 do {
23 next = ai->ai_next;
24 free(ai);
25 } while (NULL != (ai = next));
26 }
27
28 static struct addrinfo *malloc_ai(int port, u_long addr)
29 {
30 struct addrinfo *ai;
31
32 ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
33 if (ai == NULL)
34 return(NULL);
35
36 memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
37
38 ai->ai_addr = (struct sockaddr *)(ai + 1);
39 /* XXX -- ssh doesn't use sa_len */
40 ai->ai_addrlen = sizeof(struct sockaddr_in);
41 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
42
43 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
44 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
45
46 return(ai);
47 }
48
49 int getaddrinfo(const char *hostname, const char *servname,
50 const struct addrinfo *hints, struct addrinfo **res)
51 {
52 struct addrinfo *cur, *prev = NULL;
53 struct hostent *hp;
54 struct in_addr in;
55 int i, port;
56
57 if (servname)
58 port = htons(atoi(servname));
59 else
60 port = 0;
61
62 if (hints && hints->ai_flags & AI_PASSIVE) {
63 if (NULL != (*res = malloc_ai(port, htonl(0x00000000))))
64 return 0;
65 else
66 return EAI_MEMORY;
67 }
68
69 if (!hostname) {
70 if (NULL != (*res = malloc_ai(port, htonl(0x7f000001))))
71 return 0;
72 else
73 return EAI_MEMORY;
74 }
75
76 if (inet_aton(hostname, &in)) {
77 if (NULL != (*res = malloc_ai(port, in.s_addr)))
78 return 0;
79 else
80 return EAI_MEMORY;
81 }
82
83 hp = gethostbyname(hostname);
84 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
85 for (i = 0; hp->h_addr_list[i]; i++) {
86 cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
87 if (cur == NULL) {
88 if (*res)
89 freeaddrinfo(*res);
90 return EAI_MEMORY;
91 }
92
93 if (prev)
94 prev->ai_next = cur;
95 else
96 *res = cur;
97
98 prev = cur;
99 }
100 return 0;
101 }
102
103 return EAI_NODATA;
104 }