xref: /original-bsd/sbin/routed/af.c (revision 2f60f4d7)
1 #ifndef lint
2 static char sccsid[] = "@(#)af.c	4.3 06/09/82";
3 #endif
4 
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <net/in.h>
8 
9 #include "router.h"
10 #include "rip.h"
11 
12 /*
13  * Address family support routines
14  */
15 int	null_hash(), null_netmatch(), null_output(),
16 	null_portmatch(), null_portcheck(),
17 	null_checkhost(), null_canon();
18 int	inet_hash(), inet_netmatch(), inet_output(),
19 	inet_portmatch(), inet_portcheck(),
20 	inet_checkhost(), inet_canon();
21 #define NIL \
22 	{ null_hash,		null_netmatch,		null_output, \
23 	  null_portmatch,	null_portcheck,		null_checkhost, \
24 	  null_canon }
25 #define	INET \
26 	{ inet_hash,		inet_netmatch,		inet_output, \
27 	  inet_portmatch,	inet_portcheck,		inet_checkhost, \
28 	  inet_canon }
29 
30 struct afswitch afswitch[AF_MAX] =
31 	{ NIL, NIL, INET, INET, NIL, NIL, NIL, NIL, NIL, NIL, NIL };
32 
33 inet_hash(sin, hp)
34 	register struct sockaddr_in *sin;
35 	struct afhash *hp;
36 {
37 
38 	hp->afh_nethash = sin->sin_addr.s_net;
39 	hp->afh_hosthash = sin->sin_addr.s_addr;
40 #if vax || pdp11
41 	hp->afh_hosthash = ntohl(hp->afh_hosthash);
42 #endif
43 	hp->afh_hosthash &= 0x7fffffff;
44 }
45 
46 inet_netmatch(sin1, sin2)
47 	struct sockaddr_in *sin1, *sin2;
48 {
49 
50 	return (sin1->sin_addr.s_net == sin2->sin_addr.s_net);
51 }
52 
53 /*
54  * Verify the message is from the right port.
55  */
56 inet_portmatch(sin)
57 	struct sockaddr_in *sin;
58 {
59 	int port = sin->sin_port;
60 
61 #if vax || pdp11
62 	port = ntohs(port);
63 #endif
64 	return (port == IPPORT_ROUTESERVER);
65 }
66 
67 /*
68  * Verify the message is from a "trusted" port.
69  */
70 inet_portcheck(sin)
71 	struct sockaddr_in *sin;
72 {
73 	int port = sin->sin_port;
74 
75 #if vax || pdp11
76 	port = ntohs(port);
77 #endif
78 	return (port <= IPPORT_RESERVED);
79 }
80 
81 /*
82  * Internet output routine.
83  */
84 inet_output(s, sin, size)
85 	int s;
86 	struct sockaddr_in *sin;
87 	int size;
88 {
89 	extern char packet[MAXPACKETSIZE];
90 	struct sockaddr_in dst;
91 
92 	dst = *sin;
93 	sin = &dst;
94 	if (sin->sin_port == 0) {
95 		sin->sin_port = IPPORT_ROUTESERVER;
96 #if vax || pdp11
97 		sin->sin_port = htons(sin->sin_port);
98 #endif
99 	}
100 	if (send(s, sin, packet, size) < 0)
101 		perror("send");
102 }
103 
104 /*
105  * Return 1 if the address is for an Internet host,
106  * otherwise assume it's a network address (broadcast).
107  */
108 inet_checkhost(sin)
109 	struct sockaddr_in *sin;
110 {
111 	extern struct in_addr if_makeaddr();
112 	struct in_addr netaddr;
113 
114 	netaddr = if_makeaddr((int)sin->sin_addr.s_net, INADDR_ANY);
115 	return (netaddr.s_addr != sin->sin_addr.s_addr);
116 }
117 
118 inet_canon(sin)
119 	struct sockaddr_in *sin;
120 {
121 	sin->sin_port = 0;
122 }
123 
124 /*ARGSUSED*/
125 null_hash(addr, hp)
126 	struct sockaddr *addr;
127 	struct afhash *hp;
128 {
129 
130 	hp->afh_nethash = hp->afh_hosthash = 0;
131 }
132 
133 /*ARGSUSED*/
134 null_netmatch(a1, a2)
135 	struct sockaddr *a1, *a2;
136 {
137 
138 	return (0);
139 }
140 
141 /*ARGSUSED*/
142 null_output(s, a1, n)
143 	int s;
144 	struct sockaddr *a1;
145 	int n;
146 {
147 
148 	;
149 }
150 
151 /*ARGSUSED*/
152 null_portmatch(a1)
153 	struct sockaddr *a1;
154 {
155 
156 	return (0);
157 }
158 
159 /*ARGSUSED*/
160 null_portcheck(a1)
161 	struct sockaddr *a1;
162 {
163 
164 	return (0);
165 }
166 
167 /*ARGSUSED*/
168 null_checkhost(a1)
169 	struct sockaddr *a1;
170 {
171 
172 	return (0);
173 }
174 
175 /*ARGSUSED*/
176 null_canon(a1)
177 	struct sockaddr *a1;
178 {
179 
180 	;
181 }
182