2 * support/export/hostname.c
4 * Functions for hostname.
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
20 #define xmalloc malloc
26 #define ALIGNMENT sizeof (char *)
29 align (int len, int al)
39 get_hostent (const char *addr, int len, int type)
46 int len_aliases = sizeof (char *);
47 int num_addr_list = 1;
48 int len_addr_list = sizeof (char *);
55 ipv4 = (struct in_addr *) addr;
56 name = inet_ntoa (*ipv4);
63 len_ent = align (sizeof (*cp), ALIGNMENT);
64 len_name = align (strlen (name) + 1, ALIGNMENT);
67 len_addr_list += align (len, ALIGNMENT) + sizeof (char *);
69 cp = (struct hostent *) xmalloc (len_ent + len_name + len_aliases
72 cp->h_addrtype = type;
75 cp->h_name = (char *) &(((char *) cp) [pos]);
76 strcpy (cp->h_name, name);
79 cp->h_aliases = (char **) &(((char *) cp) [pos]);
80 pos += num_aliases * sizeof (char *);
81 cp->h_aliases [0] = NULL;
83 pos = len_ent + len_name + len_aliases;
84 cp->h_addr_list = (char **) &(((char *) cp) [pos]);
85 pos += num_addr_list * sizeof (char *);
86 cp->h_addr_list [0] = (char *) &(((char *) cp) [pos]);
87 memcpy (cp->h_addr_list [0], addr, cp->h_length);
88 pos += align (cp->h_length, ALIGNMENT);
89 cp->h_addr_list [1] = NULL;
95 hostent_dup (struct hostent *hp)
97 int len_ent = align (sizeof (*hp), ALIGNMENT);
98 int len_name = align (strlen (hp->h_name) + 1, ALIGNMENT);
100 int len_aliases = sizeof (char *);
101 int num_addr_list = 1;
102 int len_addr_list = sizeof (char *);
107 for (sp = hp->h_aliases; sp && *sp; sp++)
110 len_aliases += align (strlen (*sp) + 1, ALIGNMENT)
114 for (sp = hp->h_addr_list; *sp; sp++)
117 len_addr_list += align (hp->h_length, ALIGNMENT)
121 cp = (struct hostent *) xmalloc (len_ent + len_name + len_aliases
126 cp->h_name = (char *) &(((char *) cp) [pos]);
127 strcpy (cp->h_name, hp->h_name);
130 cp->h_aliases = (char **) &(((char *) cp) [pos]);
131 pos += num_aliases * sizeof (char *);
132 for (sp = hp->h_aliases, i = 0; i < num_aliases; i++, sp++)
135 cp->h_aliases [i] = (char *) &(((char *) cp) [pos]);
136 strcpy (cp->h_aliases [i], *sp);
137 pos += align (strlen (*sp) + 1, ALIGNMENT);
140 cp->h_aliases [i] = NULL;
142 pos = len_ent + len_name + len_aliases;
143 cp->h_addr_list = (char **) &(((char *) cp) [pos]);
144 pos += num_addr_list * sizeof (char *);
145 for (sp = hp->h_addr_list, i = 0; i < num_addr_list; i++, sp++)
148 cp->h_addr_list [i] = (char *) &(((char *) cp) [pos]);
149 memcpy (cp->h_addr_list [i], *sp, hp->h_length);
150 pos += align (hp->h_length, ALIGNMENT);
153 cp->h_addr_list [i] = *sp;
159 is_hostname(const char *sp)
161 if (*sp == '\0' || *sp == '@')
166 if (*sp == '*' || *sp == '?' || *sp == '[' || *sp == '/')
168 if (*sp == '\\' && sp[1])
176 matchhostname (const char *h1, const char *h2)
178 struct hostent *hp1, *hp2;
181 if (strcasecmp (h1, h2) == 0)
184 if (!is_hostname (h1) || !is_hostname (h2))
187 hp1 = gethostbyname (h1);
191 hp1 = hostent_dup (hp1);
193 hp2 = gethostbyname (h2);
196 if (strcasecmp (hp1->h_name, hp2->h_name) == 0)
203 for (ap1 = hp1->h_addr_list; *ap1 && status == 0; ap1++)
204 for (ap2 = hp2->h_addr_list; *ap2; ap2++)
205 if (memcmp (*ap1, *ap2, sizeof (struct in_addr)) == 0)
221 print_host (struct hostent *hp)
227 printf ("official hostname: %s\n", hp->h_name);
228 printf ("aliases:\n");
229 for (sp = hp->h_aliases; *sp; sp++)
230 printf (" %s\n", *sp);
231 printf ("IP addresses:\n");
232 for (sp = hp->h_addr_list; *sp; sp++)
233 printf (" %s\n", inet_ntoa (*(struct in_addr *) *sp));
236 printf ("Not host information\n");
240 main (int argc, char **argv)
242 struct hostent *hp = gethostbyname (argv [1]);
250 cp = hostent_dup (hp);
254 printf ("127.0.0.1 == %s: %d\n", argv [1],
255 matchhostname ("127.0.0.1", argv [1]));
256 addr.s_addr = inet_addr(argv [2]);
257 printf ("%s\n", inet_ntoa (addr));
258 cp = get_hostent ((const char *)&addr, sizeof(addr), AF_INET);