xref: /original-bsd/sbin/routed/af.c (revision b7261a4b)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)af.c	5.10 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 #include "defs.h"
13 
14 /*
15  * Address family support routines
16  */
17 int	inet_hash(), inet_netmatch(), inet_output(),
18 	inet_portmatch(), inet_portcheck(),
19 	inet_checkhost(), inet_rtflags(), inet_sendroute(), inet_canon();
20 char	*inet_format();
21 
22 #define NIL	{ 0 }
23 #define	INET \
24 	{ inet_hash,		inet_netmatch,		inet_output, \
25 	  inet_portmatch,	inet_portcheck,		inet_checkhost, \
26 	  inet_rtflags,		inet_sendroute,		inet_canon, \
27 	  inet_format \
28 	}
29 
30 struct afswitch afswitch[AF_MAX] = {
31 	NIL,		/* 0- unused */
32 	NIL,		/* 1- Unix domain, unused */
33 	INET,		/* Internet */
34 };
35 
36 int af_max = sizeof(afswitch) / sizeof(afswitch[0]);
37 
38 struct sockaddr_in inet_default = {
39 #ifdef RTM_ADD
40 	sizeof (inet_default),
41 #endif
42 	AF_INET, INADDR_ANY };
43 
44 inet_hash(sin, hp)
45 	register struct sockaddr_in *sin;
46 	struct afhash *hp;
47 {
48 	register u_long n;
49 
50 	n = inet_netof(sin->sin_addr);
51 	if (n)
52 	    while ((n & 0xff) == 0)
53 		n >>= 8;
54 	hp->afh_nethash = n;
55 	hp->afh_hosthash = ntohl(sin->sin_addr.s_addr);
56 	hp->afh_hosthash &= 0x7fffffff;
57 }
58 
59 inet_netmatch(sin1, sin2)
60 	struct sockaddr_in *sin1, *sin2;
61 {
62 
63 	return (inet_netof(sin1->sin_addr) == inet_netof(sin2->sin_addr));
64 }
65 
66 /*
67  * Verify the message is from the right port.
68  */
69 inet_portmatch(sin)
70 	register struct sockaddr_in *sin;
71 {
72 
73 	return (sin->sin_port == sp->s_port);
74 }
75 
76 /*
77  * Verify the message is from a "trusted" port.
78  */
79 inet_portcheck(sin)
80 	struct sockaddr_in *sin;
81 {
82 
83 	return (ntohs(sin->sin_port) <= IPPORT_RESERVED);
84 }
85 
86 /*
87  * Internet output routine.
88  */
89 inet_output(s, flags, sin, size)
90 	int s, flags;
91 	struct sockaddr_in *sin;
92 	int size;
93 {
94 	struct sockaddr_in dst;
95 
96 	dst = *sin;
97 	sin = &dst;
98 	if (sin->sin_port == 0)
99 		sin->sin_port = sp->s_port;
100 	if (sin->sin_len == 0)
101 		sin->sin_len = sizeof (*sin);
102 	if (sendto(s, packet, size, flags, sin, sizeof (*sin)) < 0)
103 		perror("sendto");
104 }
105 
106 /*
107  * Return 1 if the address is believed
108  * for an Internet host -- THIS IS A KLUDGE.
109  */
110 inet_checkhost(sin)
111 	struct sockaddr_in *sin;
112 {
113 	u_long i = ntohl(sin->sin_addr.s_addr);
114 
115 #ifndef IN_EXPERIMENTAL
116 #define	IN_EXPERIMENTAL(i)	(((long) (i) & 0xe0000000) == 0xe0000000)
117 #endif
118 
119 	if (IN_EXPERIMENTAL(i) || sin->sin_port != 0)
120 		return (0);
121 	if (i != 0 && (i & 0xff000000) == 0)
122 		return (0);
123 	for (i = 0; i < sizeof(sin->sin_zero)/sizeof(sin->sin_zero[0]); i++)
124 		if (sin->sin_zero[i])
125 			return (0);
126 	return (1);
127 }
128 
129 inet_canon(sin)
130 	struct sockaddr_in *sin;
131 {
132 
133 	sin->sin_port = 0;
134 	sin->sin_len = sizeof(*sin);
135 }
136 
137 char *
138 inet_format(sin)
139 	struct sockaddr_in *sin;
140 {
141 	char *inet_ntoa();
142 
143 	return (inet_ntoa(sin->sin_addr));
144 }
145