2 * support/export/hostname.c
4 * Functions for hostname.
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
21 #define xmalloc malloc
27 #define ALIGNMENT sizeof (char *)
30 align (int len, int al)
40 get_hostent (const char *addr, int len, int type)
47 int len_aliases = sizeof (char *);
48 int num_addr_list = 1;
49 int len_addr_list = sizeof (char *);
56 ipv4 = (struct in_addr *) addr;
57 name = inet_ntoa (*ipv4);
64 len_ent = align (sizeof (*cp), ALIGNMENT);
65 len_name = align (strlen (name) + 1, ALIGNMENT);
68 len_addr_list += align (len, ALIGNMENT) + sizeof (char *);
70 cp = (struct hostent *) xmalloc (len_ent + len_name + len_aliases
73 cp->h_addrtype = type;
76 cp->h_name = (char *) &(((char *) cp) [pos]);
77 strcpy (cp->h_name, name);
80 cp->h_aliases = (char **) &(((char *) cp) [pos]);
81 pos += num_aliases * sizeof (char *);
82 cp->h_aliases [0] = NULL;
84 pos = len_ent + len_name + len_aliases;
85 cp->h_addr_list = (char **) &(((char *) cp) [pos]);
86 pos += num_addr_list * sizeof (char *);
87 cp->h_addr_list [0] = (char *) &(((char *) cp) [pos]);
88 memcpy (cp->h_addr_list [0], addr, cp->h_length);
89 pos += align (cp->h_length, ALIGNMENT);
90 cp->h_addr_list [1] = NULL;
96 hostent_dup (struct hostent *hp)
98 int len_ent = align (sizeof (*hp), ALIGNMENT);
99 int len_name = align (strlen (hp->h_name) + 1, ALIGNMENT);
101 int len_aliases = sizeof (char *);
102 int num_addr_list = 1;
103 int len_addr_list = sizeof (char *);
108 for (sp = hp->h_aliases; sp && *sp; sp++)
111 len_aliases += align (strlen (*sp) + 1, ALIGNMENT)
115 for (sp = hp->h_addr_list; *sp; sp++)
118 len_addr_list += align (hp->h_length, ALIGNMENT)
122 cp = (struct hostent *) xmalloc (len_ent + len_name + len_aliases
127 cp->h_name = (char *) &(((char *) cp) [pos]);
128 strcpy (cp->h_name, hp->h_name);
131 cp->h_aliases = (char **) &(((char *) cp) [pos]);
132 pos += num_aliases * sizeof (char *);
133 for (sp = hp->h_aliases, i = 0; i < num_aliases; i++, sp++)
136 cp->h_aliases [i] = (char *) &(((char *) cp) [pos]);
137 strcpy (cp->h_aliases [i], *sp);
138 pos += align (strlen (*sp) + 1, ALIGNMENT);
141 cp->h_aliases [i] = NULL;
143 pos = len_ent + len_name + len_aliases;
144 cp->h_addr_list = (char **) &(((char *) cp) [pos]);
145 pos += num_addr_list * sizeof (char *);
146 for (sp = hp->h_addr_list, i = 0; i < num_addr_list; i++, sp++)
149 cp->h_addr_list [i] = (char *) &(((char *) cp) [pos]);
150 memcpy (cp->h_addr_list [i], *sp, hp->h_length);
151 pos += align (hp->h_length, ALIGNMENT);
154 cp->h_addr_list [i] = *sp;
160 is_hostname(const char *sp)
162 if (*sp == '\0' || *sp == '@')
167 if (*sp == '*' || *sp == '?' || *sp == '[' || *sp == '/')
169 if (*sp == '\\' && sp[1])
177 matchhostname (const char *h1, const char *h2)
179 struct hostent *hp1, *hp2;
182 if (strcasecmp (h1, h2) == 0)
185 if (!is_hostname (h1) || !is_hostname (h2))
188 hp1 = gethostbyname (h1);
192 hp1 = hostent_dup (hp1);
194 hp2 = gethostbyname (h2);
197 if (strcasecmp (hp1->h_name, hp2->h_name) == 0)
204 for (ap1 = hp1->h_addr_list; *ap1 && status == 0; ap1++)
205 for (ap2 = hp2->h_addr_list; *ap2; ap2++)
206 if (memcmp (*ap1, *ap2, sizeof (struct in_addr)) == 0)
221 /* Map IP to hostname, and then map back to addr to make sure it is a
225 get_reliable_hostbyaddr(const char *addr, int len, int type)
230 struct hostent *forward = NULL;
233 hp = gethostbyaddr(addr, len , type);
237 /* must make sure the hostent is authorative. */
239 hp = hostent_dup (hp);
240 tmpname = xstrdup((hp)->h_name);
242 forward = gethostbyname(tmpname);
246 /* now make sure the "addr" is in the list */
247 for (sp = forward->h_addr_list ; *sp ; sp++) {
248 if (memcmp(*sp, addr, forward->h_length)==0)
254 xlog(L_WARNING, "Fake hostname %s for %s - forward lookup doesn't match reverse",
255 forward->h_name, inet_ntoa(*(struct in_addr*)addr));
259 hp = hostent_dup (forward);
262 /* never heard of it. misconfigured DNS? */
263 xlog(L_WARNING, "Fake hostname %s for %s - forward lookup doesn't exist",
264 forward->h_name, inet_ntoa(*(struct in_addr*)addr));
273 print_host (struct hostent *hp)
279 printf ("official hostname: %s\n", hp->h_name);
280 printf ("aliases:\n");
281 for (sp = hp->h_aliases; *sp; sp++)
282 printf (" %s\n", *sp);
283 printf ("IP addresses:\n");
284 for (sp = hp->h_addr_list; *sp; sp++)
285 printf (" %s\n", inet_ntoa (*(struct in_addr *) *sp));
288 printf ("Not host information\n");
292 main (int argc, char **argv)
294 struct hostent *hp = gethostbyname (argv [1]);
302 cp = hostent_dup (hp);
306 printf ("127.0.0.1 == %s: %d\n", argv [1],
307 matchhostname ("127.0.0.1", argv [1]));
308 addr.s_addr = inet_addr(argv [2]);
309 printf ("%s\n", inet_ntoa (addr));
310 cp = get_hostent ((const char *)&addr, sizeof(addr), AF_INET);