phys addr arg of 0 must be possible for pt_writemap too (instead of meaning
[minix.git] / servers / inet / generic / ip_lib.c
bloba9f6114c8ca435003571c245559298b493f8acc3
1 /*
2 ip_lib.c
4 Copyright 1995 Philip Homburg
5 */
7 #include "inet.h"
8 #include "buf.h"
9 #include "event.h"
10 #include "type.h"
12 #include "assert.h"
13 #include "io.h"
14 #include "ip_int.h"
16 THIS_FILE
18 PUBLIC ipaddr_t ip_get_netmask (hostaddr)
19 ipaddr_t hostaddr;
21 return ip_netmask(ip_nettype(hostaddr));
24 PUBLIC int ip_chk_hdropt (opt, optlen)
25 u8_t *opt;
26 int optlen;
28 int i, security_present= FALSE, lose_source_present= FALSE,
29 strict_source_present= FALSE, record_route_present= FALSE,
30 timestamp_present= FALSE;
32 assert (!(optlen & 3));
33 i= 0;
34 while (i<optlen)
36 DBLOCK(2, printf("*opt= %d\n", *opt));
38 switch (*opt)
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)
58 DBLOCK(1, printf("2nd lose soruce route\n"));
59 return EINVAL;
61 lose_source_present= TRUE;
62 if (opt[1]<3)
64 DBLOCK(1,
65 printf("wrong length in source route\n"));
66 return EINVAL;
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)
97 case 0:
98 case 1:
99 case 3:
100 break;
101 default:
102 return EINVAL;
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;
117 if (i > optlen)
119 DBLOCK(1, printf("option of wrong length\n"));
120 return EINVAL;
122 return NW_OK;
125 PUBLIC void ip_print_frags(acc)
126 acc_t *acc;
128 #if DEBUG
129 ip_hdr_t *ip_hdr;
130 int first;
132 if (!acc)
133 printf("(null)");
135 for (first= 1; acc; acc= acc->acc_ext_link, first= 0)
137 assert (acc->acc_length >= IP_MIN_HDR_SIZE);
138 ip_hdr= (ip_hdr_t *)ptr2acc_data(acc);
139 if (first)
141 writeIpAddr(ip_hdr->ih_src);
142 printf(" > ");
143 writeIpAddr(ip_hdr->ih_dst);
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');
151 #endif
154 PUBLIC ipaddr_t ip_get_ifaddr(port_nr)
155 int port_nr;
157 assert(port_nr >= 0 && port_nr < ip_conf_nr);
159 return ip_port_table[port_nr].ip_ipaddr;
162 PUBLIC nettype_t ip_nettype(ipaddr)
163 ipaddr_t ipaddr;
165 u8_t highbyte;
166 nettype_t nettype;
168 ipaddr= ntohl(ipaddr);
169 highbyte= (ipaddr >> 24) & 0xff;
170 if (highbyte == 0)
172 if (ipaddr == 0)
173 nettype= IPNT_ZERO;
174 else
175 nettype= IPNT_MARTIAN;
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
193 if (ipaddr == (ipaddr_t)-1)
194 nettype= IPNT_BROADCAST;
195 else
196 nettype= IPNT_MARTIAN;
198 return nettype;
201 PUBLIC ipaddr_t ip_netmask(nettype)
202 nettype_t nettype;
204 switch(nettype)
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);
215 #if 0
216 PUBLIC char *ip_nettoa(nettype)
217 nettype_t nettype;
219 switch(nettype)
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>";
233 #endif
236 * $PchId: ip_lib.c,v 1.10 2002/06/08 21:35:52 philip Exp $