changes to allow subnets to remain local, propogate net route
[unix-history] / usr / src / sbin / routed / af.c
CommitLineData
5ff67f98
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
85bbbfa4 7#ifndef lint
4fad5a6e 8static char sccsid[] = "@(#)af.c 5.4 (Berkeley) %G%";
5ff67f98 9#endif not lint
85bbbfa4 10
7fe7fe74 11#include "defs.h"
ecce71aa 12
85bbbfa4
SL
13/*
14 * Address family support routines
15 */
85bbbfa4 16int inet_hash(), inet_netmatch(), inet_output(),
a773b026 17 inet_portmatch(), inet_portcheck(),
4fad5a6e 18 inet_checkhost(), inet_rtflags(), inet_sendsubnet(), inet_canon();
17fe297f 19char *inet_format();
4fad5a6e 20
17fe297f 21#define NIL { 0 }
85bbbfa4
SL
22#define INET \
23 { inet_hash, inet_netmatch, inet_output, \
a773b026 24 inet_portmatch, inet_portcheck, inet_checkhost, \
4fad5a6e
MK
25 inet_rtflags, inet_sendsubnet, inet_canon, \
26 inet_format \
27 }
85bbbfa4 28
4fad5a6e
MK
29struct afswitch afswitch[AF_MAX] = {
30 NIL, /* 0- unused */
31 NIL, /* 1- Unix domain, unused */
32 INET, /* Internet */
33};
17fe297f
MK
34
35int af_max = sizeof(afswitch) / sizeof(afswitch[0]);
85bbbfa4 36
5562f0a3
MK
37struct sockaddr_in inet_default = { AF_INET, INADDR_ANY };
38
85bbbfa4
SL
39inet_hash(sin, hp)
40 register struct sockaddr_in *sin;
41 struct afhash *hp;
42{
6dcf6dbc 43 register u_long n;
85bbbfa4 44
6dcf6dbc
MK
45 n = inet_netof(sin->sin_addr);
46 if (n)
47 while ((n & 0xff) == 0)
48 n >>= 8;
49 hp->afh_nethash = n;
7fe7fe74 50 hp->afh_hosthash = ntohl(sin->sin_addr.s_addr);
6db0b3a4 51 hp->afh_hosthash &= 0x7fffffff;
85bbbfa4
SL
52}
53
54inet_netmatch(sin1, sin2)
55 struct sockaddr_in *sin1, *sin2;
56{
57
68a24a8f 58 return (inet_netof(sin1->sin_addr) == inet_netof(sin2->sin_addr));
85bbbfa4
SL
59}
60
61/*
62 * Verify the message is from the right port.
63 */
64inet_portmatch(sin)
c8c4156e 65 register struct sockaddr_in *sin;
85bbbfa4 66{
6db0b3a4 67
7fe7fe74 68 return (sin->sin_port == sp->s_port);
85bbbfa4
SL
69}
70
a773b026
SL
71/*
72 * Verify the message is from a "trusted" port.
73 */
74inet_portcheck(sin)
7fe7fe74 75 struct sockaddr_in *sin;
a773b026 76{
a773b026 77
7fe7fe74 78 return (ntohs(sin->sin_port) <= IPPORT_RESERVED);
a773b026
SL
79}
80
85bbbfa4
SL
81/*
82 * Internet output routine.
83 */
7fe7fe74
SL
84inet_output(s, flags, sin, size)
85 int s, flags;
85bbbfa4
SL
86 struct sockaddr_in *sin;
87 int size;
88{
85bbbfa4
SL
89 struct sockaddr_in dst;
90
91 dst = *sin;
92 sin = &dst;
c8c4156e 93 if (sin->sin_port == 0)
7fe7fe74
SL
94 sin->sin_port = sp->s_port;
95 if (sendto(s, packet, size, flags, sin, sizeof (*sin)) < 0)
96 perror("sendto");
85bbbfa4
SL
97}
98
99/*
0ad851d4
SL
100 * Return 1 if the address is believed
101 * for an Internet host -- THIS IS A KLUDGE.
85bbbfa4
SL
102 */
103inet_checkhost(sin)
104 struct sockaddr_in *sin;
105{
e2f67b08 106 u_long i = ntohl(sin->sin_addr.s_addr);
49139b80 107
d5568f13
MK
108#define IN_BADCLASS(i) (((long) (i) & 0xe0000000) == 0xe0000000)
109
6dcf6dbc 110 if (IN_BADCLASS(i) || sin->sin_port != 0)
5562f0a3 111 return (0);
49139b80 112 if (i != 0 && (i & 0xff000000) == 0)
5562f0a3 113 return (0);
a7cec344
MK
114 for (i = 0; i < sizeof(sin->sin_zero)/sizeof(sin->sin_zero[0]); i++)
115 if (sin->sin_zero[i])
116 return (0);
5562f0a3 117 return (1);
d5568f13
MK
118}
119
85bbbfa4
SL
120inet_canon(sin)
121 struct sockaddr_in *sin;
122{
7fe7fe74 123
85bbbfa4
SL
124 sin->sin_port = 0;
125}
126
17fe297f
MK
127char *
128inet_format(sin)
129 struct sockaddr_in *sin;
85bbbfa4 130{
17fe297f 131 char *inet_ntoa();
6db0b3a4 132
17fe297f 133 return (inet_ntoa(sin->sin_addr));
85bbbfa4 134}