summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBill Paul <wpaul@FreeBSD.org>1996-06-25 03:22:25 +0000
committerBill Paul <wpaul@FreeBSD.org>1996-06-25 03:22:25 +0000
commitdcda7677a963645b1a84acbe209d62463adf3fef (patch)
treee66aaf742097081e8bb09ea5fa282bb8c712d3cd /lib
parent3903ba4a4bf013eea754b6295b9f7f09ed9b5bf9 (diff)
Notes
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/net/gethostbynis.c23
-rw-r--r--lib/libc/net/getnetbynis.c55
-rw-r--r--lib/libc/net/getservbyname.c14
-rw-r--r--lib/libc/net/getservbyport.c14
-rw-r--r--lib/libc/net/getservent.c155
5 files changed, 243 insertions, 18 deletions
diff --git a/lib/libc/net/gethostbynis.c b/lib/libc/net/gethostbynis.c
index 397e2fa4308c..7d0960673c38 100644
--- a/lib/libc/net/gethostbynis.c
+++ b/lib/libc/net/gethostbynis.c
@@ -24,8 +24,8 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)$Id: gethostbynis.c,v 1.1 1994/09/25 02:12:14 pst Exp $";
-static char rcsid[] = "$Id: gethostbynis.c,v 1.1 1994/09/25 02:12:14 pst Exp $";
+static char sccsid[] = "@(#)$Id: gethostbynis.c,v 1.2 1996/03/16 21:25:58 wpaul Exp $";
+static char rcsid[] = "$Id: gethostbynis.c,v 1.2 1996/03/16 21:25:58 wpaul Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
@@ -37,6 +37,11 @@ static char rcsid[] = "$Id: gethostbynis.c,v 1.1 1994/09/25 02:12:14 pst Exp $";
#include <ctype.h>
#include <errno.h>
#include <string.h>
+#ifdef YP
+#include <rpc/rpc.h>
+#include <rpcsvc/yp_prot.h>
+#include <rpcsvc/ypclnt.h>
+#endif
#define MAXALIASES 35
#define MAXADDRS 35
@@ -57,6 +62,7 @@ _gethostbynis(name, map)
int resultlen;
static struct hostent h;
static char *domain = (char *)NULL;
+ static char ypbuf[YPMAXRECORD];
if (domain == (char *)NULL)
if (yp_get_default_domain (&domain))
@@ -65,6 +71,11 @@ _gethostbynis(name, map)
if (yp_match(domain, map, name, strlen(name), &result, &resultlen))
return ((struct hostent *)NULL);
+ /* avoid potential memory leak */
+ bcopy((char *)result, (char *)&ypbuf, resultlen);
+ free(result);
+ result = (char *)&ypbuf;
+
if ((cp = index(result, '\n')))
*cp = '\0';
@@ -108,8 +119,10 @@ _gethostbynisname(name)
}
struct hostent *
-_gethostbynisaddr(name)
- char *name;
+_gethostbynisaddr(addr, len, type)
+ char *addr;
+ int len;
+ int type;
{
- return _gethostbynis(name, "hosts.byaddr");
+ return _gethostbynis(inet_ntoa(*(struct in_addr *)addr),"hosts.byaddr");
}
diff --git a/lib/libc/net/getnetbynis.c b/lib/libc/net/getnetbynis.c
index 85c719eb1b15..013b08aba279 100644
--- a/lib/libc/net/getnetbynis.c
+++ b/lib/libc/net/getnetbynis.c
@@ -24,8 +24,8 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)$Id: getnetbynis.c,v 1.3 1995/05/30 05:40:47 rgrimes Exp $";
-static char rcsid[] = "$Id: getnetbynis.c,v 1.3 1995/05/30 05:40:47 rgrimes Exp $";
+static char sccsid[] = "@(#)$Id: getnetbynis.c,v 1.5 1996/03/23 22:16:22 wpaul Exp $";
+static char rcsid[] = "$Id: getnetbynis.c,v 1.5 1996/03/23 22:16:22 wpaul Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
@@ -34,9 +34,16 @@ static char rcsid[] = "$Id: getnetbynis.c,v 1.3 1995/05/30 05:40:47 rgrimes Exp
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
+#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
+#include <arpa/nameser.h>
+#ifdef YP
+#include <rpc/rpc.h>
+#include <rpcsvc/yp_prot.h>
+#include <rpcsvc/ypclnt.h>
+#endif
#define MAXALIASES 35
#define MAXADDRS 35
@@ -55,19 +62,19 @@ _getnetbynis(name, map)
int resultlen;
static struct netent h;
static char *domain = (char *)NULL;
+ static char ypbuf[YPMAXRECORD];
if (domain == (char *)NULL)
if (yp_get_default_domain (&domain))
return (NULL);
- if (result) {
- free(result);
- result = 0;
- }
-
if (yp_match(domain, map, name, strlen(name), &result, &resultlen))
return (NULL);
+ bcopy((char *)result, (char *)&ypbuf, resultlen);
+ free(result);
+ result = (char *)&ypbuf;
+
if ((cp = index(result, '\n')))
*cp = '\0';
@@ -115,19 +122,41 @@ _getnetbynisaddr(addr, type)
long addr;
int type;
{
- struct in_addr in;
char *str, *cp;
- struct netent *np;
+ unsigned long net2;
+ int nn;
+ unsigned int netbr[4];
+ char buf[MAXDNAME];
if (type != AF_INET)
return (NULL);
- in.s_addr = addr;
- str = inet_ntoa(in);
- cp = str + strlen(str) - 2;
+ for (nn = 4, net2 = addr; net2; net2 >>= 8) {
+ netbr[--nn] = net2 & 0xff;
+ }
+
+ switch (nn) {
+ case 3: /* Class A */
+ sprintf(buf, "%u", netbr[3]);
+ break;
+ case 2: /* Class B */
+ sprintf(buf, "%u.%u", netbr[2], netbr[3]);
+ break;
+ case 1: /* Class C */
+ sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);
+ break;
+ case 0: /* Class D - E */
+ sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],
+ netbr[2], netbr[3]);
+ break;
+ }
+
+ str = (char *)&buf;
+ cp = str + (strlen(str) - 2);
+
while(!strcmp(cp, ".0")) {
*cp = '\0';
- cp = str + strlen(str) - 2;
+ cp = str + (strlen(str) - 2);
}
return _getnetbynis(str, "networks.byaddr");
diff --git a/lib/libc/net/getservbyname.c b/lib/libc/net/getservbyname.c
index 77bf81377750..fcff7cf6b7ee 100644
--- a/lib/libc/net/getservbyname.c
+++ b/lib/libc/net/getservbyname.c
@@ -47,6 +47,14 @@ getservbyname(name, proto)
register struct servent *p;
register char **cp;
+#ifdef YP
+ extern char *___getservbyname_yp;
+ extern char *___getservbyproto_yp;
+
+ ___getservbyname_yp = (char *)name;
+ ___getservbyproto_yp = (char *)proto;
+#endif
+
setservent(_serv_stayopen);
while (p = getservent()) {
if (strcmp(name, p->s_name) == 0)
@@ -61,5 +69,11 @@ gotname:
}
if (!_serv_stayopen)
endservent();
+
+#ifdef YP
+ ___getservbyname_yp = NULL;
+ ___getservbyproto_yp = NULL;
+#endif
+
return (p);
}
diff --git a/lib/libc/net/getservbyport.c b/lib/libc/net/getservbyport.c
index 0acb31bb084e..a24f8df64132 100644
--- a/lib/libc/net/getservbyport.c
+++ b/lib/libc/net/getservbyport.c
@@ -47,6 +47,14 @@ getservbyport(port, proto)
{
register struct servent *p;
+#ifdef YP
+ extern int ___getservbyport_yp;
+ extern char *___getservbyproto_yp;
+
+ ___getservbyport_yp = port;
+ ___getservbyproto_yp = (char *)proto;
+#endif
+
setservent(_serv_stayopen);
while (p = getservent()) {
if (p->s_port != port)
@@ -56,5 +64,11 @@ getservbyport(port, proto)
}
if (!_serv_stayopen)
endservent();
+
+#ifdef YP
+ ___getservbyport_yp = 0;
+ ___getservbyproto_yp = NULL;
+#endif
+
return (p);
}
diff --git a/lib/libc/net/getservent.c b/lib/libc/net/getservent.c
index 6bee5d3a8beb..6797b5afec98 100644
--- a/lib/libc/net/getservent.c
+++ b/lib/libc/net/getservent.c
@@ -41,6 +41,12 @@ static char sccsid[] = "@(#)getservent.c 8.1 (Berkeley) 6/4/93";
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#ifdef YP
+#include <rpc/rpc.h>
+#include <rpcsvc/yp_prot.h>
+#include <rpcsvc/ypclnt.h>
+static int serv_stepping_yp = 0;
+#endif
#define MAXALIASES 35
@@ -50,6 +56,133 @@ static struct servent serv;
static char *serv_aliases[MAXALIASES];
int _serv_stayopen;
+#ifdef YP
+char *___getservbyname_yp = NULL;
+char *___getservbyproto_yp = NULL;
+int ___getservbyport_yp = 0;
+static char *yp_domain = NULL;
+
+static int
+_getservbyport_yp(line)
+ char *line;
+{
+ char *result;
+ int resultlen;
+ char buf[YPMAXRECORD];
+ int rv;
+
+ snprintf(buf, sizeof(buf), "%d/%s", ntohs(___getservbyport_yp),
+ ___getservbyproto_yp);
+
+ ___getservbyport_yp = 0;
+ ___getservbyproto_yp = NULL;
+
+ if(!yp_domain) {
+ if(yp_get_default_domain(&yp_domain))
+ return (0);
+ }
+
+ /*
+ * We have to be a little flexible here. Ideally you're supposed
+ * to have both a services.byname and a services.byport map, but
+ * some systems have only services.byname. FreeBSD cheats a little
+ * by putting the services.byport information in the same map as
+ * services.byname so that either case will work. We allow for both
+ * possibilities here: if there is no services.byport map, we try
+ * services.byname instead.
+ */
+ if ((rv = yp_match(yp_domain, "services.byport", buf, strlen(buf),
+ &result, &resultlen))) {
+ if (rv == YPERR_MAP) {
+ if (yp_match(yp_domain, "services.byname", buf,
+ strlen(buf), &result, &resultlen))
+ return(0);
+ } else
+ return(0);
+ }
+
+ /* getservent() expects lines terminated with \n -- make it happy */
+ snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
+
+ free(result);
+ return(1);
+}
+
+static int
+_getservbyname_yp(line)
+ char *line;
+{
+ char *result;
+ int resultlen;
+ char buf[YPMAXRECORD];
+
+ if(!yp_domain) {
+ if(yp_get_default_domain(&yp_domain))
+ return (0);
+ }
+
+ snprintf(buf, sizeof(buf), "%s/%s", ___getservbyname_yp,
+ ___getservbyproto_yp);
+
+ ___getservbyname_yp = 0;
+ ___getservbyproto_yp = NULL;
+
+ if (yp_match(yp_domain, "services.byname", buf, strlen(buf),
+ &result, &resultlen)) {
+ return(0);
+ }
+
+ /* getservent() expects lines terminated with \n -- make it happy */
+ snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
+
+ free(result);
+ return(1);
+}
+
+static int
+_getservent_yp(line)
+ char *line;
+{
+ static char *key = NULL;
+ static int keylen;
+ char *lastkey, *result;
+ int resultlen;
+ int rv;
+
+ if(!yp_domain) {
+ if(yp_get_default_domain(&yp_domain))
+ return (0);
+ }
+
+ if (!serv_stepping_yp) {
+ if (key)
+ free(key);
+ if ((rv = yp_first(yp_domain, "services.byname", &key, &keylen,
+ &result, &resultlen))) {
+ serv_stepping_yp = 0;
+ return(0);
+ }
+ serv_stepping_yp = 1;
+ } else {
+ lastkey = key;
+ rv = yp_next(yp_domain, "services.byname", key, keylen, &key,
+ &keylen, &result, &resultlen);
+ free(lastkey);
+ if (rv) {
+ serv_stepping_yp = 0;
+ return (0);
+ }
+ }
+
+ /* getservent() expects lines terminated with \n -- make it happy */
+ snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
+
+ free(result);
+
+ return(1);
+}
+#endif
+
void
setservent(f)
int f;
@@ -77,11 +210,33 @@ getservent()
char *p;
register char *cp, **q;
+#ifdef YP
+ if (serv_stepping_yp && _getservent_yp(line)) {
+ p = (char *)&line;
+ goto unpack;
+ }
+tryagain:
+#endif
if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
return (NULL);
again:
if ((p = fgets(line, BUFSIZ, servf)) == NULL)
return (NULL);
+#ifdef YP
+ if (*p == '+' && _yp_check(NULL)) {
+ if (___getservbyname_yp != NULL) {
+ if (!_getservbyname_yp(&line))
+ goto tryagain;
+ }
+ else if (___getservbyport_yp != 0) {
+ if (!_getservbyport_yp(&line))
+ goto tryagain;
+ }
+ else if (!_getservent_yp(&line))
+ goto tryagain;
+ }
+unpack:
+#endif
if (*p == '#')
goto again;
cp = strpbrk(p, "#\n");