Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: ip.fth | |
4 | \ | |
5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | \ | |
7 | \ - Do no alter or remove copyright notices | |
8 | \ | |
9 | \ - Redistribution and use of this software in source and binary forms, with | |
10 | \ or without modification, are permitted provided that the following | |
11 | \ conditions are met: | |
12 | \ | |
13 | \ - Redistribution of source code must retain the above copyright notice, | |
14 | \ this list of conditions and the following disclaimer. | |
15 | \ | |
16 | \ - Redistribution in binary form must reproduce the above copyright notice, | |
17 | \ this list of conditions and the following disclaimer in the | |
18 | \ documentation and/or other materials provided with the distribution. | |
19 | \ | |
20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors | |
21 | \ may be used to endorse or promote products derived from this software | |
22 | \ without specific prior written permission. | |
23 | \ | |
24 | \ This software is provided "AS IS," without a warranty of any kind. | |
25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
36 | \ | |
37 | \ You acknowledge that this software is not designed, licensed or | |
38 | \ intended for use in the design, construction, operation or maintenance of | |
39 | \ any nuclear facility. | |
40 | \ | |
41 | \ ========== Copyright Header End ============================================ | |
42 | id: @(#)ip.fth 2.12 02/08/22 | |
43 | purpose: Simple Internet Protocol (IP) implementation | |
44 | copyright: Copyright 1990-2002 Sun Microsystems, Inc. All Rights Reserved | |
45 | copyright: Use is subject to license terms. | |
46 | ||
47 | \ Internet protocol (IP). | |
48 | ||
49 | decimal | |
50 | ||
51 | headerless | |
52 | ||
53 | struct ( ip-header ) | |
54 | 1 field >ip-version | |
55 | 1 field >ip-service | |
56 | 2 field >ip-length | |
57 | 2 field >ip-id | |
58 | 2 field >ip-fragment | |
59 | 1 field >ip-ttl | |
60 | 1 field >ip-protocol | |
61 | 2 field >ip-checksum | |
62 | 4 field >ip-source-addr | |
63 | 4 field >ip-dest-addr | |
64 | constant /ip-header | |
65 | ||
66 | : ip-version ( -- adr ) active-struct@ >ip-version ; | |
67 | : ip-service ( -- adr ) active-struct@ >ip-service ; | |
68 | : ip-length ( -- adr ) active-struct@ >ip-length ; | |
69 | : ip-id ( -- adr ) active-struct@ >ip-id ; | |
70 | : ip-fragment ( -- adr ) active-struct@ >ip-fragment ; | |
71 | : ip-ttl ( -- adr ) active-struct@ >ip-ttl ; | |
72 | : ip-protocol ( -- adr ) active-struct@ >ip-protocol ; | |
73 | : ip-checksum ( -- adr ) active-struct@ >ip-checksum ; | |
74 | : ip-source-addr ( -- adr ) active-struct@ >ip-source-addr ; | |
75 | : ip-dest-addr ( -- adr ) active-struct@ >ip-dest-addr ; | |
76 | ||
77 | instance variable ttl d# 64 ttl ! \ RFC 1700 recommended IP default TTL | |
78 | instance variable ip-sequence | |
79 | ||
80 | instance variable my-ip-addr | |
81 | instance variable his-ip-addr | |
82 | ||
83 | instance variable server-ip-addr | |
84 | instance variable router-ip-addr | |
85 | instance variable subnet-mask | |
86 | ||
87 | false instance value use-server? | |
88 | false instance value use-router? | |
89 | ||
90 | decimal | |
91 | h# 800 constant IP_TYPE | |
92 | ||
93 | create broadcast-ip-addr h# ff c, h# ff c, h# ff c, h# ff c, | |
94 | ||
95 | : ip= ( ip-addr1 ip-addr2 -- flag ) 4 comp 0= ; | |
96 | ||
97 | \ either h# ffffffff or h# 0 is broadcast ip addr | |
98 | : broadcast-ip-addr? ( adr-buf -- flag ) | |
99 | dup broadcast-ip-addr ip= swap be-l@ 0= or | |
100 | ; | |
101 | ||
102 | : ip-addr-match? ( -- flag ) | |
103 | \ If we know the server's IP address, silently discard packets | |
104 | \ from other hosts | |
105 | his-ip-addr broadcast-ip-addr? 0= if | |
106 | his-ip-addr ip-source-addr ip= 0= if false exit then | |
107 | then | |
108 | ||
109 | \ Accept IP broadcast packets | |
110 | ip-dest-addr broadcast-ip-addr? if true exit then | |
111 | ||
112 | \ Accept every packet if we dont know our IP address yet | |
113 | my-ip-addr broadcast-ip-addr? if true exit then | |
114 | ||
115 | \ We know our IP address; filter packets to other destinations | |
116 | ip-dest-addr my-ip-addr ip= | |
117 | ; | |
118 | ||
119 | : receive-ip-packet ( -- [ ip-packet-adr ip-len ] flag ) | |
120 | begin | |
121 | IP_TYPE receive-ethernet-packet 0= if false exit then ( adr len ) | |
122 | drop /ether-header + active-struct ! ( ) | |
123 | ip-addr-match? | |
124 | until ( ) | |
125 | active-struct@ ip-length w@ true ( adr len true ) | |
126 | ; | |
127 | ||
128 | : my-netid ( -- netid ) | |
129 | my-ip-addr be-l@ subnet-mask be-l@ and ( netid ) | |
130 | ; | |
131 | ||
132 | : on-my-net? ( ip# -- flag ) | |
133 | subnet-mask broadcast-ip-addr? if drop true exit then ( ip# ) | |
134 | ( ip# ) subnet-mask be-l@ and ( dest-netid ) | |
135 | my-netid = | |
136 | ; | |
137 | ||
138 | : .in ( byte -- ) .d bs emit ." ." ; | |
139 | : .inetaddr ( n -- ) lbsplit .in .in .in .d ; | |
140 | ||
141 | headers |