Subject: getnetent, getnetbyaddr, getnetbyname YP addition
To: None <current-users@sun-lamp.cs.berkeley.edu>
From: Christos Zoulas <christos@deshaw.com>
List: current-users
Date: 01/22/1994 23:59:51
*** getnetbyaddr.c.dist	Sat Jan 22 23:24:02 1994
--- getnetbyaddr.c	Sat Jan 22 23:52:18 1994
***************
*** 37,42 ****
--- 37,46 ----
  #endif /* LIBC_SCCS and not lint */
  
  #include <netdb.h>
+ #include <sys/types.h>
+ #include <sys/socket.h>
+ #include <netinet/in.h>
+ #include <arpa/inet.h>
  
  extern int _net_stayopen;
  
***************
*** 53,57 ****
--- 57,95 ----
  			break;
  	if (!_net_stayopen)
  		endnetent();
+ #ifdef YP
+ 	if (p == 0 && type == AF_INET) {
+ 		/* Always look at yp */
+ 		char *ypbuf, *ypdom;
+ 		int ypbuflen;
+ 		extern struct netent *_net_line();
+ 		char trybuf[sizeof "XXX.XXX.XXX.XXX"];
+ 		int trylen;
+ 
+ #define C(i)	((i) & 0xff)
+ 		if ((net & 0xff000000) != 0)
+ 			sprintf(trybuf, "%u.%u.%u.%u", C(net >> 24),
+ 				C(net >> 16), C(net >> 8), C(net));
+ 		else if ((net & 0xffff0000) != 0)
+ 			sprintf(trybuf, "%u.%u.%u", C(net >> 16), C(net >> 8),
+ 				C(net));
+ 		else if ((net & 0xffffff00) != 0)
+ 			sprintf(trybuf, "%u.%u", C(net >> 8), C(net));
+ 		else
+ 			sprintf(trybuf, "%u", C(net));
+ 
+ 		trylen = strlen(trybuf);
+ 
+ 		if (yp_get_default_domain(&ypdom))
+ 			return 0;
+ 		if (yp_match(ypdom, "networks.byaddr", trybuf,
+ 		    trylen, &ypbuf, &ypbuflen))
+ 			return 0;
+ 		p = _net_line(ypbuf);
+ 		free(ypbuf);
+ 		if (p && (p->n_addrtype != type || p->n_net != net))
+ 			p = 0;
+ 	}
+ #endif
  	return (p);
  }
*** getnetbyname.c.dist	Sat Jan 22 23:14:20 1994
--- getnetbyname.c	Sat Jan 22 23:27:30 1994
***************
*** 59,63 ****
--- 59,81 ----
  found:
  	if (!_net_stayopen)
  		endnetent();
+ #ifdef YP
+ 	if (p == 0) {
+ 		/* Always look at yp */
+ 		char *ypbuf, *ypdom;
+ 		int ypbuflen;
+ 		extern struct netent *_net_line();
+ 		char *trybuf = (char *) name;
+ 		int trylen = strlen(name);
+ 
+ 		if (yp_get_default_domain(&ypdom))
+ 			return 0;
+ 		if (yp_match(ypdom, "networks.name", trybuf,
+ 		    trylen, &ypbuf, &ypbuflen))
+ 			return 0;
+ 		p = _net_line(ypbuf);
+ 		free(ypbuf);
+ 	}
+ #endif
  	return (p);
  }
*** getnetent.c.dist	Fri Dec 17 01:24:52 1993
--- getnetent.c	Sat Jan 22 23:54:32 1994
***************
*** 73,100 ****
  	_net_stayopen = 0;
  }
  
  struct netent *
! getnetent()
! {
  	char *p;
  	register char *cp, **q;
  
- 	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
- 		return (NULL);
- again:
- 	p = fgets(line, BUFSIZ, netf);
- 	if (p == NULL)
- 		return (NULL);
- 	if (*p == '#')
- 		goto again;
- 	cp = strpbrk(p, "#\n");
- 	if (cp == NULL)
- 		goto again;
- 	*cp = '\0';
  	net.n_name = p;
  	cp = strpbrk(p, " \t");
  	if (cp == NULL)
! 		goto again;
  	*cp++ = '\0';
  	while (*cp == ' ' || *cp == '\t')
  		cp++;
--- 73,89 ----
  	_net_stayopen = 0;
  }
  
+ 
  struct netent *
! _net_line(p)
  	char *p;
+ {
  	register char *cp, **q;
  
  	net.n_name = p;
  	cp = strpbrk(p, " \t");
  	if (cp == NULL)
! 		return NULL;
  	*cp++ = '\0';
  	while (*cp == ' ' || *cp == '\t')
  		cp++;
***************
*** 119,122 ****
--- 108,134 ----
  	}
  	*q = NULL;
  	return (&net);
+ }
+ 
+ struct netent *
+ getnetent()
+ {
+ 	char *p, *cp;
+ 	struct netent *n;
+ 
+ 	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
+ 		return (NULL);
+ 
+ 	while ((p = fgets(line, BUFSIZ, netf)) != NULL) {
+ 		if (*p == '#')
+ 		    continue;
+ 		cp = strpbrk(p, "#\n");
+ 		if (cp == NULL)
+ 			return NULL;
+ 		*cp = '\0';
+ 		if ((n = _net_line(p)) == NULL)
+ 		    continue;
+ 		return n;
+ 	}
+ 	return (NULL);
  }

------------------------------------------------------------------------------