1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if defined(LIBC_SCCS) && !defined(lint) 31 static char rcsid[] = "$OpenBSD: getnetent.c,v 1.10 2005/03/25 13:24:12 otto Exp $"; 32 #endif /* LIBC_SCCS and not lint */ 33 34 #include <sys/types.h> 35 #include <sys/socket.h> 36 #include <netinet/in.h> 37 #include <arpa/inet.h> 38 #include <netdb.h> 39 #include <stdio.h> 40 #include <string.h> 41 42 #define MAXALIASES 35 43 44 static FILE *netf; 45 static char line[BUFSIZ+1]; 46 static struct netent net; 47 static char *net_aliases[MAXALIASES]; 48 int _net_stayopen; 49 50 void 51 setnetent(int f) 52 { 53 if (netf == NULL) 54 netf = fopen(_PATH_NETWORKS, "r" ); 55 else 56 rewind(netf); 57 _net_stayopen |= f; 58 } 59 60 void 61 endnetent(void) 62 { 63 if (netf) { 64 fclose(netf); 65 netf = NULL; 66 } 67 _net_stayopen = 0; 68 } 69 70 struct netent * 71 getnetent(void) 72 { 73 char *p, *cp, **q; 74 size_t len; 75 76 if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) 77 return (NULL); 78 again: 79 if ((p = fgetln(netf, &len)) == NULL) 80 return (NULL); 81 if (p[len-1] == '\n') 82 len--; 83 if (len >= sizeof(line) || len == 0) 84 goto again; 85 p = memcpy(line, p, len); 86 line[len] = '\0'; 87 if (*p == '#') 88 goto again; 89 if ((cp = strchr(p, '#')) != NULL) 90 *cp = '\0'; 91 net.n_name = p; 92 if (strlen(net.n_name) >= MAXHOSTNAMELEN-1) 93 net.n_name[MAXHOSTNAMELEN-1] = '\0'; 94 cp = strpbrk(p, " \t"); 95 if (cp == NULL) 96 goto again; 97 *cp++ = '\0'; 98 while (*cp == ' ' || *cp == '\t') 99 cp++; 100 p = strpbrk(cp, " \t"); 101 if (p != NULL) 102 *p++ = '\0'; 103 net.n_net = inet_network(cp); 104 net.n_addrtype = AF_INET; 105 q = net.n_aliases = net_aliases; 106 if (p != NULL) 107 cp = p; 108 while (cp && *cp) { 109 if (*cp == ' ' || *cp == '\t') { 110 cp++; 111 continue; 112 } 113 if (q < &net_aliases[MAXALIASES - 1]) { 114 *q++ = cp; 115 if (strlen(cp) >= MAXHOSTNAMELEN-1) 116 cp[MAXHOSTNAMELEN-1] = '\0'; 117 } 118 cp = strpbrk(cp, " \t"); 119 if (cp != NULL) 120 *cp++ = '\0'; 121 } 122 *q = NULL; 123 return (&net); 124 } 125