]>
Commit | Line | Data |
---|---|---|
d7ce7006 | 1 | /*-\r |
2 | * Copyright (c) 1994, Garrett Wollman\r | |
3 | *\r | |
4 | * Redistribution and use in source and binary forms, with or without\r | |
5 | * modification, are permitted provided that the following conditions\r | |
6 | * are met:\r | |
7 | * 1. Redistributions of source code must retain the above copyright\r | |
8 | * notice, this list of conditions and the following disclaimer.\r | |
9 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
10 | * notice, this list of conditions and the following disclaimer in the\r | |
11 | * documentation and/or other materials provided with the distribution.\r | |
12 | *\r | |
13 | * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND\r | |
14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r | |
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r | |
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r | |
19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r | |
20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r | |
21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r | |
22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r | |
23 | * SUCH DAMAGE.\r | |
24 | */\r | |
25 | \r | |
26 | #if defined(LIBC_SCCS) && !defined(lint)\r | |
27 | static char sccsid[] = "@(#)$Id: gethostbynis.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $";\r | |
28 | static char rcsid[] = "$Id: gethostbynis.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $";\r | |
29 | #endif /* LIBC_SCCS and not lint */\r | |
30 | \r | |
31 | #include <sys/param.h>\r | |
32 | #include <sys/socket.h>\r | |
33 | #include <netinet/in.h>\r | |
34 | #include <arpa/inet.h>\r | |
35 | #include <netdb.h>\r | |
36 | #include <stdio.h>\r | |
37 | #include <stdlib.h>\r | |
38 | #include <ctype.h>\r | |
39 | #include <errno.h>\r | |
40 | #include <string.h>\r | |
41 | #ifdef YP\r | |
42 | #include <rpc/rpc.h>\r | |
43 | #include <rpcsvc/yp_prot.h>\r | |
44 | #include <rpcsvc/ypclnt.h>\r | |
45 | #endif\r | |
46 | \r | |
47 | #define MAXALIASES 35\r | |
48 | #define MAXADDRS 35\r | |
49 | \r | |
50 | #ifdef YP\r | |
51 | static char *host_aliases[MAXALIASES];\r | |
52 | static char hostaddr[MAXADDRS];\r | |
53 | static char *host_addrs[2];\r | |
54 | #endif /* YP */\r | |
55 | \r | |
56 | static struct hostent *\r | |
57 | _gethostbynis(const char *name, char *map, int af)\r | |
58 | {\r | |
59 | #ifdef YP\r | |
60 | register char *cp, **q;\r | |
61 | char *result;\r | |
62 | int resultlen;\r | |
63 | static struct hostent h;\r | |
64 | static char *domain = (char *)NULL;\r | |
65 | static char ypbuf[YPMAXRECORD + 2];\r | |
66 | \r | |
67 | switch(af) {\r | |
68 | case AF_INET:\r | |
69 | break;\r | |
70 | default:\r | |
71 | case AF_INET6:\r | |
72 | errno = EAFNOSUPPORT;\r | |
73 | return NULL;\r | |
74 | }\r | |
75 | \r | |
76 | if (domain == (char *)NULL)\r | |
77 | if (yp_get_default_domain (&domain))\r | |
78 | return ((struct hostent *)NULL);\r | |
79 | \r | |
80 | if (yp_match(domain, map, name, strlen(name), &result, &resultlen))\r | |
81 | return ((struct hostent *)NULL);\r | |
82 | \r | |
83 | /* avoid potential memory leak */\r | |
84 | bcopy((char *)result, (char *)&ypbuf, resultlen);\r | |
85 | ypbuf[resultlen] = '\0';\r | |
86 | free(result);\r | |
87 | result = (char *)&ypbuf;\r | |
88 | \r | |
89 | if ((cp = index(result, '\n')))\r | |
90 | *cp = '\0';\r | |
91 | \r | |
92 | cp = strpbrk(result, " \t");\r | |
93 | *cp++ = '\0';\r | |
94 | h.h_addr_list = host_addrs;\r | |
95 | h.h_addr = hostaddr;\r | |
96 | *((u_long *)h.h_addr) = inet_addr(result);\r | |
97 | h.h_length = sizeof(u_long);\r | |
98 | h.h_addrtype = AF_INET;\r | |
99 | while (*cp == ' ' || *cp == '\t')\r | |
100 | cp++;\r | |
101 | h.h_name = cp;\r | |
102 | q = h.h_aliases = host_aliases;\r | |
103 | cp = strpbrk(cp, " \t");\r | |
104 | if (cp != NULL)\r | |
105 | *cp++ = '\0';\r | |
106 | while (cp && *cp) {\r | |
107 | if (*cp == ' ' || *cp == '\t') {\r | |
108 | cp++;\r | |
109 | continue;\r | |
110 | }\r | |
111 | if (q < &host_aliases[MAXALIASES - 1])\r | |
112 | *q++ = cp;\r | |
113 | cp = strpbrk(cp, " \t");\r | |
114 | if (cp != NULL)\r | |
115 | *cp++ = '\0';\r | |
116 | }\r | |
117 | *q = NULL;\r | |
118 | return (&h);\r | |
119 | #else\r | |
120 | return (NULL);\r | |
121 | #endif /* YP */\r | |
122 | }\r | |
123 | \r | |
124 | struct hostent *\r | |
125 | _gethostbynisname(const char *name, int af)\r | |
126 | {\r | |
127 | return _gethostbynis(name, "hosts.byname", af);\r | |
128 | }\r | |
129 | \r | |
130 | struct hostent *\r | |
131 | _gethostbynisaddr(const char *addr, int len, int af)\r | |
132 | {\r | |
133 | return _gethostbynis(inet_ntoa(*(struct in_addr *)addr),"hosts.byaddr", af);\r | |
134 | }\r |