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 * $FreeBSD: src/lib/libc/net/getnetbynis.c,v 1.11 1999/08/28 00:00:07 peter Exp $ 26 * $DragonFly: src/lib/libc/net/getnetbynis.c,v 1.3 2004/10/25 19:38:01 drhodus Exp $ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/socket.h> 31 #include <netinet/in.h> 32 #include <arpa/inet.h> 33 #include <netdb.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <ctype.h> 37 #include <errno.h> 38 #include <string.h> 39 #include <arpa/nameser.h> 40 #ifdef YP 41 #include <rpc/rpc.h> 42 #include <rpcsvc/yp_prot.h> 43 #include <rpcsvc/ypclnt.h> 44 #endif 45 46 #define MAXALIASES 35 47 #define MAXADDRS 35 48 49 #ifdef YP 50 static char *host_aliases[MAXALIASES]; 51 #endif /* YP */ 52 53 static struct netent * 54 _getnetbynis(name, map, af) 55 const char *name; 56 char *map; 57 int af; 58 { 59 #ifdef YP 60 char *cp, **q; 61 static char *result; 62 int resultlen; 63 static struct netent h; 64 static char *domain = (char *)NULL; 65 static char ypbuf[YPMAXRECORD + 2]; 66 67 switch(af) { 68 case AF_INET: 69 break; 70 default: 71 case AF_INET6: 72 errno = EAFNOSUPPORT; 73 return NULL; 74 } 75 76 if (domain == (char *)NULL) 77 if (yp_get_default_domain (&domain)) 78 return (NULL); 79 80 if (yp_match(domain, map, name, strlen(name), &result, &resultlen)) 81 return (NULL); 82 83 bcopy((char *)result, (char *)&ypbuf, resultlen); 84 ypbuf[resultlen] = '\0'; 85 free(result); 86 result = (char *)&ypbuf; 87 88 if ((cp = index(result, '\n'))) 89 *cp = '\0'; 90 91 cp = strpbrk(result, " \t"); 92 *cp++ = '\0'; 93 h.n_name = result; 94 95 while (*cp == ' ' || *cp == '\t') 96 cp++; 97 98 h.n_net = inet_network(cp); 99 h.n_addrtype = AF_INET; 100 101 q = h.n_aliases = host_aliases; 102 cp = strpbrk(cp, " \t"); 103 if (cp != NULL) 104 *cp++ = '\0'; 105 while (cp && *cp) { 106 if (*cp == ' ' || *cp == '\t') { 107 cp++; 108 continue; 109 } 110 if (q < &host_aliases[MAXALIASES - 1]) 111 *q++ = cp; 112 cp = strpbrk(cp, " \t"); 113 if (cp != NULL) 114 *cp++ = '\0'; 115 } 116 *q = NULL; 117 return (&h); 118 #else 119 return (NULL); 120 #endif 121 } 122 123 struct netent * 124 _getnetbynisname(name) 125 const char *name; 126 { 127 return _getnetbynis(name, "networks.byname", AF_INET); 128 } 129 130 struct netent * 131 _getnetbynisaddr(addr, af) 132 unsigned long addr; 133 int af; 134 { 135 char *str, *cp; 136 unsigned long net2; 137 int nn; 138 unsigned int netbr[4]; 139 char buf[MAXDNAME]; 140 141 if (af != AF_INET) { 142 errno = EAFNOSUPPORT; 143 return (NULL); 144 } 145 146 for (nn = 4, net2 = addr; net2; net2 >>= 8) { 147 netbr[--nn] = net2 & 0xff; 148 } 149 150 switch (nn) { 151 case 3: /* Class A */ 152 sprintf(buf, "%u", netbr[3]); 153 break; 154 case 2: /* Class B */ 155 sprintf(buf, "%u.%u", netbr[2], netbr[3]); 156 break; 157 case 1: /* Class C */ 158 sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]); 159 break; 160 case 0: /* Class D - E */ 161 sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1], 162 netbr[2], netbr[3]); 163 break; 164 } 165 166 str = (char *)&buf; 167 cp = str + (strlen(str) - 2); 168 169 while(!strcmp(cp, ".0")) { 170 *cp = '\0'; 171 cp = str + (strlen(str) - 2); 172 } 173 174 return _getnetbynis(str, "networks.byaddr", af); 175 } 176