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: getnetbynis.c,v 1.5 1996/03/23 22:16:22 wpaul Exp $"; 28 static char rcsid[] = "$Id: getnetbynis.c,v 1.5 1996/03/23 22:16:22 wpaul Exp $"; 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]; 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 free(result); 87 result = (char *)&ypbuf; 88 89 if ((cp = index(result, '\n'))) 90 *cp = '\0'; 91 92 cp = strpbrk(result, " \t"); 93 *cp++ = '\0'; 94 h.n_name = result; 95 96 while (*cp == ' ' || *cp == '\t') 97 cp++; 98 99 h.n_net = inet_network(cp); 100 h.n_addrtype = AF_INET; 101 102 q = h.n_aliases = host_aliases; 103 cp = strpbrk(cp, " \t"); 104 if (cp != NULL) 105 *cp++ = '\0'; 106 while (cp && *cp) { 107 if (*cp == ' ' || *cp == '\t') { 108 cp++; 109 continue; 110 } 111 if (q < &host_aliases[MAXALIASES - 1]) 112 *q++ = cp; 113 cp = strpbrk(cp, " \t"); 114 if (cp != NULL) 115 *cp++ = '\0'; 116 } 117 *q = NULL; 118 return (&h); 119 #else 120 return (NULL); 121 #endif 122 } 123 124 struct netent * 125 _getnetbynisname(name) 126 const char *name; 127 { 128 return _getnetbynis(name, "networks.byname"); 129 } 130 131 struct netent * 132 _getnetbynisaddr(addr, af) 133 unsigned long addr; 134 int af; 135 { 136 char *str, *cp; 137 unsigned long net2; 138 int nn; 139 unsigned int netbr[4]; 140 char buf[MAXDNAME]; 141 142 if (af != AF_INET) { 143 errno = EAFNOSUPPORT; 144 return (NULL); 145 } 146 147 for (nn = 4, net2 = addr; net2; net2 >>= 8) { 148 netbr[--nn] = net2 & 0xff; 149 } 150 151 switch (nn) { 152 case 3: /* Class A */ 153 sprintf(buf, "%u", netbr[3]); 154 break; 155 case 2: /* Class B */ 156 sprintf(buf, "%u.%u", netbr[2], netbr[3]); 157 break; 158 case 1: /* Class C */ 159 sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]); 160 break; 161 case 0: /* Class D - E */ 162 sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1], 163 netbr[2], netbr[3]); 164 break; 165 } 166 167 str = (char *)&buf; 168 cp = str + (strlen(str) - 2); 169 170 while(!strcmp(cp, ".0")) { 171 *cp = '\0'; 172 cp = str + (strlen(str) - 2); 173 } 174 175 return _getnetbynis(str, "networks.byaddr", af); 176 } 177