xref: /csrg-svn/sbin/routed/af.c (revision 7793)
16916Ssam #ifndef lint
2*7793Ssam static char sccsid[] = "@(#)af.c	4.7 08/19/82";
36916Ssam #endif
46916Ssam 
56916Ssam #include <sys/types.h>
66916Ssam #include <sys/socket.h>
76916Ssam #include <net/in.h>
86916Ssam #include "router.h"
96916Ssam #include "rip.h"
106916Ssam 
117143Ssam extern char packet[MAXPACKETSIZE], *sys_errlist[];
127143Ssam extern int trace, errno;
137143Ssam #define	tprintf	if (trace) printf
147143Ssam 
156916Ssam /*
166916Ssam  * Address family support routines
176916Ssam  */
186916Ssam int	null_hash(), null_netmatch(), null_output(),
197026Ssam 	null_portmatch(), null_portcheck(),
207026Ssam 	null_checkhost(), null_canon();
216916Ssam int	inet_hash(), inet_netmatch(), inet_output(),
227026Ssam 	inet_portmatch(), inet_portcheck(),
237026Ssam 	inet_checkhost(), inet_canon();
246916Ssam #define NIL \
256916Ssam 	{ null_hash,		null_netmatch,		null_output, \
267026Ssam 	  null_portmatch,	null_portcheck,		null_checkhost, \
277026Ssam 	  null_canon }
286916Ssam #define	INET \
296916Ssam 	{ inet_hash,		inet_netmatch,		inet_output, \
307026Ssam 	  inet_portmatch,	inet_portcheck,		inet_checkhost, \
317026Ssam 	  inet_canon }
326916Ssam 
336916Ssam struct afswitch afswitch[AF_MAX] =
346916Ssam 	{ NIL, NIL, INET, INET, NIL, NIL, NIL, NIL, NIL, NIL, NIL };
356916Ssam 
366916Ssam inet_hash(sin, hp)
376916Ssam 	register struct sockaddr_in *sin;
386916Ssam 	struct afhash *hp;
396916Ssam {
406916Ssam 
41*7793Ssam 	hp->afh_nethash = in_netof(sin->sin_addr);
427133Swnj 	hp->afh_hosthash = sin->sin_addr.s_addr;
437133Swnj #if vax || pdp11
447133Swnj 	hp->afh_hosthash = ntohl(hp->afh_hosthash);
457133Swnj #endif
467133Swnj 	hp->afh_hosthash &= 0x7fffffff;
476916Ssam }
486916Ssam 
496916Ssam inet_netmatch(sin1, sin2)
506916Ssam 	struct sockaddr_in *sin1, *sin2;
516916Ssam {
526916Ssam 
53*7793Ssam 	return (in_netof(sin1->sin_addr) == in_netof(sin2->sin_addr));
546916Ssam }
556916Ssam 
566916Ssam /*
576916Ssam  * Verify the message is from the right port.
586916Ssam  */
596916Ssam inet_portmatch(sin)
606916Ssam 	struct sockaddr_in *sin;
616916Ssam {
627133Swnj 	int port = sin->sin_port;
637133Swnj 
647133Swnj #if vax || pdp11
657133Swnj 	port = ntohs(port);
667133Swnj #endif
677136Ssam 	return (port == IPPORT_ROUTESERVER || port == IPPORT_ROUTESERVER+1);
686916Ssam }
696916Ssam 
706916Ssam /*
717026Ssam  * Verify the message is from a "trusted" port.
727026Ssam  */
737026Ssam inet_portcheck(sin)
747026Ssam 	struct sockaddr_in *sin;
757026Ssam {
767133Swnj 	int port = sin->sin_port;
777026Ssam 
787133Swnj #if vax || pdp11
797133Swnj 	port = ntohs(port);
807133Swnj #endif
817026Ssam 	return (port <= IPPORT_RESERVED);
827026Ssam }
837026Ssam 
847026Ssam /*
856916Ssam  * Internet output routine.
866916Ssam  */
877133Swnj inet_output(s, sin, size)
887133Swnj 	int s;
896916Ssam 	struct sockaddr_in *sin;
906916Ssam 	int size;
916916Ssam {
926916Ssam 	struct sockaddr_in dst;
936916Ssam 
946916Ssam 	dst = *sin;
956916Ssam 	sin = &dst;
967133Swnj 	if (sin->sin_port == 0) {
977133Swnj 		sin->sin_port = IPPORT_ROUTESERVER;
987133Swnj #if vax || pdp11
997133Swnj 		sin->sin_port = htons(sin->sin_port);
1007133Swnj #endif
1017133Swnj 	}
1026916Ssam 	if (send(s, sin, packet, size) < 0)
1037143Ssam 		tprintf("send to %x: %s\n", sin->sin_addr, sys_errlist[errno]);
1046916Ssam }
1056916Ssam 
1066916Ssam /*
107*7793Ssam  * Return 1 if the address is believed
108*7793Ssam  * for an Internet host -- THIS IS A KLUDGE.
1096916Ssam  */
1106916Ssam inet_checkhost(sin)
1116916Ssam 	struct sockaddr_in *sin;
1126916Ssam {
113*7793Ssam 	return (in_lnaof(sin->sin_addr) != 0);
1146916Ssam }
1156916Ssam 
1166916Ssam inet_canon(sin)
1176916Ssam 	struct sockaddr_in *sin;
1186916Ssam {
1196916Ssam 	sin->sin_port = 0;
1206916Ssam }
1216916Ssam 
1226916Ssam /*ARGSUSED*/
1236916Ssam null_hash(addr, hp)
1246916Ssam 	struct sockaddr *addr;
1256916Ssam 	struct afhash *hp;
1266916Ssam {
1276916Ssam 
1286916Ssam 	hp->afh_nethash = hp->afh_hosthash = 0;
1296916Ssam }
1306916Ssam 
1316916Ssam /*ARGSUSED*/
1326916Ssam null_netmatch(a1, a2)
1336916Ssam 	struct sockaddr *a1, *a2;
1346916Ssam {
1356916Ssam 
1366916Ssam 	return (0);
1376916Ssam }
1386916Ssam 
1396916Ssam /*ARGSUSED*/
1407133Swnj null_output(s, a1, n)
1417133Swnj 	int s;
1426916Ssam 	struct sockaddr *a1;
1436916Ssam 	int n;
1446916Ssam {
1457133Swnj 
1466916Ssam 	;
1476916Ssam }
1486916Ssam 
1496916Ssam /*ARGSUSED*/
1506916Ssam null_portmatch(a1)
1516916Ssam 	struct sockaddr *a1;
1526916Ssam {
1537133Swnj 
1546916Ssam 	return (0);
1556916Ssam }
1566916Ssam 
1576916Ssam /*ARGSUSED*/
1587026Ssam null_portcheck(a1)
1597026Ssam 	struct sockaddr *a1;
1607026Ssam {
1617133Swnj 
1627026Ssam 	return (0);
1637026Ssam }
1647026Ssam 
1657026Ssam /*ARGSUSED*/
1666916Ssam null_checkhost(a1)
1676916Ssam 	struct sockaddr *a1;
1686916Ssam {
1697133Swnj 
1706916Ssam 	return (0);
1716916Ssam }
1726916Ssam 
1736916Ssam /*ARGSUSED*/
1746916Ssam null_canon(a1)
1756916Ssam 	struct sockaddr *a1;
1766916Ssam {
1777133Swnj 
1786916Ssam 	;
1796916Ssam }
180