1 /*- 2 * Copyright (c) 1994, Garrett Wollman 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #if defined(LIBC_SCCS) && !defined(lint) 27 static char sccsid[] = "@(#)$Id$"; 28 static char rcsid[] = "$Id$"; 29 #endif /* LIBC_SCCS and not lint */ 30 31 #include <sys/param.h> 32 #include <sys/socket.h> 33 #include <netinet/in.h> 34 #include <arpa/inet.h> 35 #include <netdb.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <ctype.h> 39 #include <errno.h> 40 #include <string.h> 41 #include <arpa/nameser.h> 42 #ifdef YP 43 #include <rpc/rpc.h> 44 #include <rpcsvc/yp_prot.h> 45 #include <rpcsvc/ypclnt.h> 46 #endif 47 48 #define MAXALIASES 35 49 #define MAXADDRS 35 50 51 #ifdef YP 52 static char *host_aliases[MAXALIASES]; 53 #endif /* YP */ 54 55 static struct netent * 56 _getnetbynis(name, map, af) 57 const char *name; 58 char *map; 59 int af; 60 { 61 #ifdef YP 62 register char *cp, **q; 63 static char *result; 64 int resultlen; 65 static struct netent h; 66 static char *domain = (char *)NULL; 67 static char ypbuf[YPMAXRECORD + 2]; 68 69 switch(af) { 70 case AF_INET: 71 break; 72 default: 73 case AF_INET6: 74 errno = EAFNOSUPPORT; 75 return NULL; 76 } 77 78 if (domain == (char *)NULL) 79 if (yp_get_default_domain (&domain)) 80 return (NULL); 81 82 if (yp_match(domain, map, name, strlen(name), &result, &resultlen)) 83 return (NULL); 84 85 bcopy((char *)result, (char *)&ypbuf, resultlen); 86 ypbuf[resultlen] = '\0'; 87 free(result); 88 result = (char *)&ypbuf; 89 90 if ((cp = index(result, '\n'))) 91 *cp = '\0'; 92 93 cp = strpbrk(result, " \t"); 94 *cp++ = '\0'; 95 h.n_name = result; 96 97 while (*cp == ' ' || *cp == '\t') 98 cp++; 99 100 h.n_net = inet_network(cp); 101 h.n_addrtype = AF_INET; 102 103 q = h.n_aliases = host_aliases; 104 cp = strpbrk(cp, " \t"); 105 if (cp != NULL) 106 *cp++ = '\0'; 107 while (cp && *cp) { 108 if (*cp == ' ' || *cp == '\t') { 109 cp++; 110 continue; 111 } 112 if (q < &host_aliases[MAXALIASES - 1]) 113 *q++ = cp; 114 cp = strpbrk(cp, " \t"); 115 if (cp != NULL) 116 *cp++ = '\0'; 117 } 118 *q = NULL; 119 return (&h); 120 #else 121 return (NULL); 122 #endif 123 } 124 125 struct netent * 126 _getnetbynisname(name) 127 const char *name; 128 { 129 return _getnetbynis(name, "networks.byname", AF_INET); 130 } 131 132 struct netent * 133 _getnetbynisaddr(addr, af) 134 unsigned long addr; 135 int af; 136 { 137 char *str, *cp; 138 unsigned long net2; 139 int nn; 140 unsigned int netbr[4]; 141 char buf[MAXDNAME]; 142 143 if (af != AF_INET) { 144 errno = EAFNOSUPPORT; 145 return (NULL); 146 } 147 148 for (nn = 4, net2 = addr; net2; net2 >>= 8) { 149 netbr[--nn] = net2 & 0xff; 150 } 151 152 switch (nn) { 153 case 3: /* Class A */ 154 sprintf(buf, "%u", netbr[3]); 155 break; 156 case 2: /* Class B */ 157 sprintf(buf, "%u.%u", netbr[2], netbr[3]); 158 break; 159 case 1: /* Class C */ 160 sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]); 161 break; 162 case 0: /* Class D - E */ 163 sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1], 164 netbr[2], netbr[3]); 165 break; 166 } 167 168 str = (char *)&buf; 169 cp = str + (strlen(str) - 2); 170 171 while(!strcmp(cp, ".0")) { 172 *cp = '\0'; 173 cp = str + (strlen(str) - 2); 174 } 175 176 return _getnetbynis(str, "networks.byaddr", af); 177 } 178