1 #ifndef lint
2 static char	sccsid[] = "@(#)access_inet.c	1.4	(Berkeley) 1/9/88";
3 #endif
4 
5 #include "common.h"
6 
7 #include <stdio.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #ifndef EXCELAN
11 #include <netdb.h>
12 #ifndef hpux
13 #include <arpa/inet.h>
14 #endif
15 #endif
16 
17 /*
18  * inet_netnames -- return the network, subnet, and host names of
19  * our peer process for the Internet domain.
20  *
21  *	Parameters:	"sock" is our socket, which we don't need.
22  *			"sin" is a pointer to the result of
23  *			a getpeername() call.
24  *			"net_name", "subnet_name", and "host_name"
25  *			are filled in by this routine with the
26  *			corresponding ASCII names of our peer.
27  *	Returns:	Nothing.
28  *	Side effects:	None.
29  */
30 
31 void
inet_netnames(sock,sin,net_name,subnet_name,host_name)32 inet_netnames(sock, sin, net_name, subnet_name, host_name)
33 	int			sock;
34 	struct sockaddr_in	*sin;
35 	char			*net_name;
36 	char			*subnet_name;
37 	char			*host_name;
38 {
39 #ifdef EXCELAN
40 	void excelan_netnames();
41 
42 	excelan_netnames(sock, sin, net_name, subnet_name, host_name);
43 #else
44 	static int		gotsubnetconf;
45 	u_long			net_addr;
46 	u_long			subnet_addr;
47 	struct hostent		*hp;
48 	struct netent		*np;
49 
50 #ifdef SUBNET
51 	if (!gotsubnetconf) {
52 		if (getifconf() < 0) {
53 #ifdef SYSLOG
54 			syslog(LOG_ERR, "host_access: getifconf: %m");
55 #endif
56 			return;
57 		}
58 		gotsubnetconf = 1;
59 	}
60 #endif
61 
62 	net_addr = inet_netof(sin->sin_addr);	/* net_addr in host order */
63 	np = getnetbyaddr(net_addr, AF_INET);
64 	if (np != NULL)
65 		(void) strcpy(net_name, np->n_name);
66 	else
67 		(void) strcpy(net_name,inet_ntoa(*(struct in_addr *)&net_addr));
68 
69 #ifdef SUBNET
70 	subnet_addr = inet_snetof(sin->sin_addr.s_addr);
71 	if (subnet_addr == 0)
72 		subnet_name[0] = '\0';
73 	else {
74 		np = getnetbyaddr(subnet_addr, AF_INET);
75 		if (np != NULL)
76 			(void) strcpy(subnet_name, np->n_name);
77 		else
78 			(void) strcpy(subnet_name,
79 			    inet_ntoa(*(struct in_addr *)&subnet_addr));
80 	}
81 #else
82 	subnet_name[0] = '\0';
83 #endif /* SUBNET */
84 
85 	hp = gethostbyaddr((char *) &sin->sin_addr.s_addr,
86 		sizeof (sin->sin_addr.s_addr), AF_INET);
87 	if (hp != NULL)
88 		(void) strcpy(host_name, hp->h_name);
89 	else
90 		(void) strcpy(host_name, inet_ntoa(sin->sin_addr));
91 #endif
92 }
93 
94 
95 #ifdef EXCELAN
96 void
excelan_netnames(sock,sin,net_name,subnet_name,host_name)97 excelan_netnames(sock, sin, net_name, subnet_name, host_name)
98 	int			sock;
99 	struct sockaddr_in	*sin;
100 	char			*net_name;
101 	char			*subnet_name;
102 	char			*host_name;
103 {
104 	char *hp, *raddr();
105 	int octet[4];
106 
107 	/* assumes sizeof addr is long */
108 	octet[0] = (sin->sin_addr.s_addr>>24)&0xff;
109 	octet[1] = (sin->sin_addr.s_addr>>16)&0xff;
110 	octet[2] = (sin->sin_addr.s_addr>>8 )&0xff;
111 	octet[3] = (sin->sin_addr.s_addr    )&0xff;
112 
113 	hp = raddr(sin->sin_addr.s_addr);
114 	if (hp != NULL)
115 		(void) strcpy(host_name,hp);
116 	else
117 
118 		(void) sprintf(host_name,"%d.%d.%d.%d",
119 			octet[0],octet[1],octet[2],octet[3]);
120 	/* No inet* routines here, so we fake it. */
121 	if (octet[0] < 128)		/* CLASS A address */
122 		(void) sprintf(net_name,"%d",octet[0]);
123 	else if(octet[0] < 192)		/* CLASS B address */
124 			(void) sprintf(net_name,"%d.%d",
125 						octet[0],octet[1]);
126 		else 			/* CLASS C address */
127 			(void) sprintf(net_name,"%d.%d.%d",
128 					octet[0],octet[1],octet[2]);
129 
130 /* hack to cover the subnet stuff */
131 	(void) sprintf(subnet_name,"%d.%d.%d",octet[0],octet[1],octet[2]);
132 }
133 #endif
134