source: trunk/minix/servers/inet/generic/ip_lib.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 4.5 KB
Line 
1/*
2ip_lib.c
3
4Copyright 1995 Philip Homburg
5*/
6
7#include "inet.h"
8#include "buf.h"
9#include "event.h"
10#include "type.h"
11
12#include "assert.h"
13#include "io.h"
14#include "ip_int.h"
15
16THIS_FILE
17
18PUBLIC ipaddr_t ip_get_netmask (hostaddr)
19ipaddr_t hostaddr;
20{
21 return ip_netmask(ip_nettype(hostaddr));
22}
23
24PUBLIC int ip_chk_hdropt (opt, optlen)
25u8_t *opt;
26int optlen;
27{
28 int i, security_present= FALSE, lose_source_present= FALSE,
29 strict_source_present= FALSE, record_route_present= FALSE,
30 timestamp_present= FALSE;
31
32 assert (!(optlen & 3));
33 i= 0;
34 while (i<optlen)
35 {
36 DBLOCK(2, printf("*opt= %d\n", *opt));
37
38 switch (*opt)
39 {
40 case IP_OPT_EOL: /* End of Option list */
41 return NW_OK;
42 case IP_OPT_NOP: /* No Operation */
43 i++;
44 opt++;
45 break;
46 case IP_OPT_SEC: /* Security */
47 if (security_present)
48 return EINVAL;
49 security_present= TRUE;
50 if (opt[1] != 11)
51 return EINVAL;
52 i += opt[1];
53 opt += opt[1];
54 break;
55 case IP_OPT_LSRR: /* Lose Source and Record Route */
56 if (lose_source_present)
57 {
58 DBLOCK(1, printf("2nd lose soruce route\n"));
59 return EINVAL;
60 }
61 lose_source_present= TRUE;
62 if (opt[1]<3)
63 {
64 DBLOCK(1,
65 printf("wrong length in source route\n"));
66 return EINVAL;
67 }
68 i += opt[1];
69 opt += opt[1];
70 break;
71 case IP_OPT_SSRR: /* Strict Source and Record Route */
72 if (strict_source_present)
73 return EINVAL;
74 strict_source_present= TRUE;
75 if (opt[1]<3)
76 return EINVAL;
77 i += opt[1];
78 opt += opt[1];
79 break;
80 case IP_OPT_RR: /* Record Route */
81 if (record_route_present)
82 return EINVAL;
83 record_route_present= TRUE;
84 if (opt[1]<3)
85 return EINVAL;
86 i += opt[1];
87 opt += opt[1];
88 break;
89 case IP_OPT_TS: /* Timestamp */
90 if (timestamp_present)
91 return EINVAL;
92 timestamp_present= TRUE;
93 if (opt[1] != 4)
94 return EINVAL;
95 switch (opt[3] & 0xff)
96 {
97 case 0:
98 case 1:
99 case 3:
100 break;
101 default:
102 return EINVAL;
103 }
104 i += opt[1];
105 opt += opt[1];
106 break;
107 case IP_OPT_RTRALT:
108 if (opt[1] != 4)
109 return EINVAL;
110 i += opt[1];
111 opt += opt[1];
112 break;
113 default:
114 return EINVAL;
115 }
116 }
117 if (i > optlen)
118 {
119 DBLOCK(1, printf("option of wrong length\n"));
120 return EINVAL;
121 }
122 return NW_OK;
123}
124
125PUBLIC void ip_print_frags(acc)
126acc_t *acc;
127{
128#if DEBUG
129 ip_hdr_t *ip_hdr;
130 int first;
131
132 if (!acc)
133 printf("(null)");
134
135 for (first= 1; acc; acc= acc->acc_ext_link, first= 0)
136 {
137assert (acc->acc_length >= IP_MIN_HDR_SIZE);
138 ip_hdr= (ip_hdr_t *)ptr2acc_data(acc);
139 if (first)
140 {
141 writeIpAddr(ip_hdr->ih_src);
142 printf(" > ");
143 writeIpAddr(ip_hdr->ih_dst);
144 }
145 printf(" {%x:%d@%d%c}", ntohs(ip_hdr->ih_id),
146 ntohs(ip_hdr->ih_length),
147 (ntohs(ip_hdr->ih_flags_fragoff) & IH_FRAGOFF_MASK)*8,
148 (ntohs(ip_hdr->ih_flags_fragoff) & IH_MORE_FRAGS) ?
149 '+' : '\0');
150 }
151#endif
152}
153
154PUBLIC ipaddr_t ip_get_ifaddr(port_nr)
155int port_nr;
156{
157 assert(port_nr >= 0 && port_nr < ip_conf_nr);
158
159 return ip_port_table[port_nr].ip_ipaddr;
160}
161
162PUBLIC nettype_t ip_nettype(ipaddr)
163ipaddr_t ipaddr;
164{
165 u8_t highbyte;
166 nettype_t nettype;
167
168 ipaddr= ntohl(ipaddr);
169 highbyte= (ipaddr >> 24) & 0xff;
170 if (highbyte == 0)
171 {
172 if (ipaddr == 0)
173 nettype= IPNT_ZERO;
174 else
175 nettype= IPNT_MARTIAN;
176 }
177 else if (highbyte < 127)
178 nettype= IPNT_CLASS_A;
179 else if (highbyte == 127)
180 nettype= IPNT_LOCAL;
181 else if (highbyte < 192)
182 nettype= IPNT_CLASS_B;
183 else if (highbyte < 224)
184 nettype= IPNT_CLASS_C;
185 else if (highbyte < 240)
186 nettype= IPNT_CLASS_D;
187 else if (highbyte < 248)
188 nettype= IPNT_CLASS_E;
189 else if (highbyte < 255)
190 nettype= IPNT_MARTIAN;
191 else
192 {
193 if (ipaddr == (ipaddr_t)-1)
194 nettype= IPNT_BROADCAST;
195 else
196 nettype= IPNT_MARTIAN;
197 }
198 return nettype;
199}
200
201PUBLIC ipaddr_t ip_netmask(nettype)
202nettype_t nettype;
203{
204 switch(nettype)
205 {
206 case IPNT_ZERO: return HTONL(0x00000000);
207 case IPNT_CLASS_A:
208 case IPNT_LOCAL: return HTONL(0xff000000);
209 case IPNT_CLASS_B: return HTONL(0xffff0000);
210 case IPNT_CLASS_C: return HTONL(0xffffff00);
211 default: return HTONL(0xffffffff);
212 }
213}
214
215#if 0
216PUBLIC char *ip_nettoa(nettype)
217nettype_t nettype;
218{
219 switch(nettype)
220 {
221 case IPNT_ZERO: return "zero";
222 case IPNT_CLASS_A: return "class A";
223 case IPNT_LOCAL: return "local";
224 case IPNT_CLASS_B: return "class B";
225 case IPNT_CLASS_C: return "class C";
226 case IPNT_CLASS_D: return "class D";
227 case IPNT_CLASS_E: return "class E";
228 case IPNT_MARTIAN: return "martian";
229 case IPNT_BROADCAST: return "broadcast";
230 default: return "<unknown>";
231 }
232}
233#endif
234
235/*
236 * $PchId: ip_lib.c,v 1.10 2002/06/08 21:35:52 philip Exp $
237 */
Note: See TracBrowser for help on using the repository browser.