+++ /dev/null
-/*-\r
- * Copyright (c) 1994, Garrett Wollman\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND\r
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
- * SUCH DAMAGE.\r
- */\r
-\r
-#if defined(LIBC_SCCS) && !defined(lint)\r
-static char rcsid[] = "$Id: getnetnamadr.c,v 1.1.1.1 2003/11/19 01:51:28 kyu3 Exp $";\r
-#endif /* LIBC_SCCS and not lint */\r
-\r
-#include <sys/param.h>\r
-#include <sys/socket.h>\r
-#include <netinet/in.h>\r
-#include <arpa/inet.h>\r
-#include <netdb.h>\r
-#include <stdio.h>\r
-#include <ctype.h>\r
-#include <errno.h>\r
-#include <paths.h>\r
-#include <string.h>\r
-\r
-#include "Socklib_internals.h"\r
-\r
-enum service_type {\r
- SERVICE_NONE = 0,\r
- SERVICE_BIND,\r
- SERVICE_TABLE,\r
- SERVICE_NIS };\r
-#define SERVICE_MAX SERVICE_NIS\r
-\r
-static struct {\r
- const char *name;\r
- enum service_type type;\r
-} service_names[] = {\r
- { "hosts", SERVICE_TABLE },\r
- { _PATH_HOSTS, SERVICE_TABLE },\r
- { "hosttable", SERVICE_TABLE },\r
- { "htable", SERVICE_TABLE },\r
- { "bind", SERVICE_BIND },\r
- { "dns", SERVICE_BIND },\r
- { "domain", SERVICE_BIND },\r
- { "yp", SERVICE_NIS },\r
- { "yellowpages", SERVICE_NIS },\r
- { "nis", SERVICE_NIS },\r
- { 0, SERVICE_NONE }\r
-};\r
-\r
-static enum service_type service_order[SERVICE_MAX + 1];\r
-static int service_done = 0;\r
-\r
-static enum service_type\r
-get_service_name(const char *name) {\r
- int i;\r
- for(i = 0; service_names[i].type != SERVICE_NONE; i++) {\r
- if(!strcasecmp(name, service_names[i].name)) {\r
- return service_names[i].type;\r
- }\r
- }\r
- return SERVICE_NONE;\r
-}\r
-\r
-static void\r
-init_services()\r
-{\r
- char *cp, *p, buf[BUFSIZ];\r
- register int cc = 0;\r
- FILE *fd;\r
-\r
- if ((fd = (FILE *)fopen(_PATH_NETCONF, "r")) == NULL) {\r
- /* make some assumptions */\r
- service_order[0] = SERVICE_TABLE;\r
- service_order[1] = SERVICE_NONE;\r
- } else {\r
- while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) {\r
- if(buf[0] == '#')\r
- continue;\r
-\r
- p = buf;\r
- while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')\r
- ;\r
- if (cp == NULL)\r
- continue;\r
- do {\r
- if (isalpha(cp[0])) {\r
- service_order[cc] = get_service_name(cp);\r
- if(service_order[cc] != SERVICE_NONE)\r
- cc++;\r
- }\r
- while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')\r
- ;\r
- } while(cp != NULL && cc < SERVICE_MAX);\r
- }\r
- service_order[cc] = SERVICE_NONE;\r
- fclose(fd);\r
- }\r
- service_done = 1;\r
-}\r
-\r
-struct netent *\r
-getnetbyname(const char *name)\r
-{\r
- struct netent *hp = 0;\r
- int nserv = 0;\r
-\r
- if (!service_done)\r
- init_services();\r
-\r
- while (!hp) {\r
- switch (service_order[nserv]) {\r
- case SERVICE_NONE:\r
- return NULL;\r
- case SERVICE_TABLE:\r
- hp = _getnetbyhtname(name);\r
- break;\r
- case SERVICE_BIND:\r
- hp = _getnetbydnsname(name);\r
- break;\r
- case SERVICE_NIS:\r
- hp = _getnetbynisname(name);\r
- break;\r
- }\r
- nserv++;\r
- }\r
- return hp;\r
-}\r
-\r
-struct netent *\r
-getnetbyaddr(uint32_t addr, int af)\r
-{\r
- struct netent *hp = 0;\r
- int nserv = 0;\r
-\r
- if (!service_done)\r
- init_services();\r
-\r
- while (!hp) {\r
- switch (service_order[nserv]) {\r
- case SERVICE_NONE:\r
- return 0;\r
- case SERVICE_TABLE:\r
- hp = _getnetbyhtaddr(addr, af);\r
- break;\r
- case SERVICE_BIND:\r
- hp = _getnetbydnsaddr(addr, af);\r
- break;\r
- case SERVICE_NIS:\r
- hp = _getnetbynisaddr(addr, af);\r
- break;\r
- }\r
- nserv++;\r
- }\r
- return hp;\r
-}\r
-\r
-void\r
-setnetent(int stayopen)\r
-{\r
- _setnethtent(stayopen);\r
- _setnetdnsent(stayopen);\r
-}\r
-\r
-void\r
-endnetent()\r
-{\r
- _endnethtent();\r
- _endnetdnsent();\r
-}\r