Rev 1485 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1485 | Rev 1514 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2010. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;; UDP.INC ;; |
6 | ;; UDP.INC ;; |
7 | ;; ;; |
7 | ;; ;; |
8 | ;; Part of the tcp/ip network stack for KolibriOS ;; |
8 | ;; Part of the tcp/ip network stack for KolibriOS ;; |
Line 12... | Line 12... | ||
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
13 | ;; Version 2, June 1991 ;; |
13 | ;; Version 2, June 1991 ;; |
14 | ;; ;; |
14 | ;; ;; |
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
Line 16... | Line 16... | ||
16 | 16 | ||
Line 17... | Line 17... | ||
17 | $Revision: 1485 $ |
17 | $Revision: 1514 $ |
18 | 18 | ||
19 | 19 | ||
Line 53... | Line 53... | ||
53 | rep stosd |
53 | rep stosd |
Line 54... | Line 54... | ||
54 | 54 | ||
Line 55... | Line -... | ||
55 | ret |
- | |
56 | 55 | ret |
|
57 | 56 | ||
58 | 57 | ||
59 | ;----------------------------------------------------------------- |
58 | ;----------------------------------------------------------------- |
60 | ; |
59 | ; |
61 | ; UDP_Handler: |
60 | ; UDP_input: |
62 | ; |
61 | ; |
63 | ; Called by IPv4_handler, |
62 | ; Called by IPv4_input, |
64 | ; this procedure will inject the udp data diagrams in the application sockets. |
63 | ; this procedure will inject the udp data diagrams in the application sockets. |
65 | ; |
64 | ; |
Line 74... | Line 73... | ||
74 | ; |
73 | ; |
75 | ; OUT: / |
74 | ; OUT: / |
76 | ; |
75 | ; |
77 | ;----------------------------------------------------------------- |
76 | ;----------------------------------------------------------------- |
78 | align 4 |
77 | align 4 |
79 | UDP_handler: |
78 | UDP_input: |
Line 80... | Line 79... | ||
80 | 79 | ||
Line 81... | Line 80... | ||
81 | DEBUGF 1,"UDP_Handler, checksum:%x, size:%u\n", [edx+UDP_Packet.Checksum]:4, ecx |
80 | DEBUGF 1,"UDP_input, size:%u\n", ecx |
82 | 81 | ||
83 | ; First validate, checksum: |
82 | ; First validate, checksum: |
Line 84... | Line 83... | ||
84 | cmp [edx + UDP_Packet.Checksum], 0 |
83 | cmp [edx + UDP_Packet.Checksum], 0 |
Line 85... | Line 84... | ||
85 | jz .no_checksum |
84 | jz .no_checksum |
86 | - | ||
87 | xchg edi, esi ; save ipv4 source address so we can look it up later |
- | |
88 | 85 | ||
89 | push edx |
86 | xchg edi, esi ; save ipv4 source address to edi so we can use it later |
90 | 87 | ||
91 | push esi |
- | |
92 | push edi |
88 | push edx |
Line 93... | Line 89... | ||
93 | mov esi, edx |
89 | push esi edi |
94 | call UDP_checksum ; this destroys edx, ecx and esi (but not edi! :) |
90 | mov esi, edx |
Line 106... | Line 102... | ||
106 | ; IP Packet SA = Remote IP |
102 | ; IP Packet SA = Remote IP |
Line 107... | Line 103... | ||
107 | 103 | ||
108 | mov eax, net_sockets |
104 | mov eax, net_sockets |
109 | .try_more: |
105 | .try_more: |
- | 106 | mov si , [edx + UDP_Packet.DestinationPort] ; get the local port from the IP Packet's UDP header |
|
110 | mov si , [edx + UDP_Packet.DestinationPort] ; get the local port from the IP Packet's UDP header |
107 | rol si , 8 |
111 | .next_socket: |
108 | .next_socket: |
112 | mov eax, [eax + SOCKET_head.NextPtr] |
109 | mov eax, [eax + SOCKET.NextPtr] |
113 | or eax, eax |
110 | or eax, eax |
114 | jz .dump |
111 | jz .dump |
115 | cmp [eax + SOCKET_head.Domain], AF_INET4 |
112 | cmp [eax + SOCKET.Domain], AF_INET4 |
116 | jne .next_socket |
113 | jne .next_socket |
117 | cmp [eax + SOCKET_head.Type], IP_PROTO_UDP |
114 | cmp [eax + SOCKET.Type], IP_PROTO_UDP |
118 | jne .next_socket |
115 | jne .next_socket |
119 | cmp [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort], si |
116 | cmp [eax + UDP_SOCKET.LocalPort], si |
Line 120... | Line 117... | ||
120 | jne .next_socket |
117 | jne .next_socket |
121 | - | ||
122 | DEBUGF 1,"found socket with matching domain, type and localport\n" |
- | |
123 | - | ||
124 | ; For dhcp, we must allow any remote server to respond. |
- | |
125 | ; I will accept the first incoming response to be the one |
- | |
126 | ; I bind to, if the socket is opened with a destination IP address of |
- | |
127 | ; 255.255.255.255 |
- | |
Line 128... | Line -... | ||
128 | cmp [eax + SOCKET_head.end + IPv4_SOCKET.RemoteIP], 0xffffffff |
- | |
129 | je .ok1 |
118 | |
Line -... | Line 119... | ||
- | 119 | DEBUGF 1,"using socket: %x\n", eax |
|
- | 120 | ||
- | 121 | ;;; TODO: when packet is processed, check more sockets! |
|
- | 122 | ||
- | 123 | cmp [eax + IP_SOCKET.RemoteIP], 0xffffffff |
|
Line 130... | Line -... | ||
130 | - | ||
131 | cmp [eax + SOCKET_head.end + IPv4_SOCKET.RemoteIP], edi ; edi is IPv4 destination address |
- | |
132 | jne .try_more ; Quit if the source IP is not valid, check for more sockets with this IP/PORT combination |
- | |
133 | 124 | je @f |
|
134 | 125 | cmp [eax + IP_SOCKET.RemoteIP], edi ; edi is the packets source address |
|
Line 135... | Line 126... | ||
135 | DEBUGF 1,"Remote Ip matches\n" |
126 | jne .try_more |
- | 127 | @@: |
|
136 | .ok1: |
128 | |
137 | 129 | cmp [eax + UDP_SOCKET.firstpacket], 0 |
|
Line 138... | Line 130... | ||
138 | cmp [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.firstpacket], 0 |
130 | jz .updateport |
139 | jz .updateport |
131 | |
140 | 132 | mov si, [edx + UDP_Packet.SourcePort] |
|
141 | mov si, [edx + UDP_Packet.SourcePort] |
133 | rol si, 8 |
Line 142... | Line 134... | ||
142 | cmp [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.RemotePort], si |
134 | cmp [eax + UDP_SOCKET.RemotePort], si |
143 | jne .dump |
- | |
- | 135 | jne .dump |
|
144 | 136 | ||
145 | push ebx |
137 | push ebx |
146 | lea ebx, [eax + SOCKET_head.lock] |
138 | lea ebx, [eax + SOCKET.lock] |
147 | call wait_mutex |
139 | call wait_mutex |
148 | pop ebx |
140 | pop ebx |
Line 149... | Line 141... | ||
149 | 141 | ||
150 | .ok2: |
- | |
151 | - | ||
152 | DEBUGF 1,"Found valid UDP packet for socket %x\n", eax |
- | |
153 | lea esi, [edx + UDP_Packet.Data] |
- | |
154 | movzx ecx, [edx + UDP_Packet.Length] |
- | |
155 | rol cx , 8 |
- | |
156 | sub cx , UDP_Packet.Data |
- | |
157 | - | ||
Line 158... | Line 142... | ||
158 | inc [UDP_PACKETS_RX] |
142 | .updatesock: |
159 | - | ||
160 | pop edi |
143 | inc [UDP_PACKETS_RX] |
161 | add esp, 4 |
144 | DEBUGF 1,"Found valid UDP packet for socket %x\n", eax |
162 | 145 | lea esi, [edx + UDP_Packet.Data] |
|
163 | sub esi, edi |
146 | movzx ecx, [edx + UDP_Packet.Length] |
Line 164... | Line 147... | ||
164 | xchg esi, edi |
147 | rol cx , 8 |
- | 148 | sub cx , UDP_Packet.Data |
|
165 | jmp socket_internal_receiver |
149 | |
166 | 150 | jmp SOCKET_input |
|
167 | 151 | ||
Line 168... | Line 152... | ||
168 | .updateport: |
152 | .updateport: |
Line 169... | Line 153... | ||
169 | 153 | push ebx |
|
Line 170... | Line 154... | ||
170 | push ebx |
154 | lea ebx, [eax + SOCKET.lock] |
Line 200... | Line 184... | ||
200 | 184 | ||
201 | 185 | ||
202 | 186 | ||
203 | ;----------------------------------------------------------------- |
187 | ;----------------------------------------------------------------- |
204 | ; |
188 | ; |
205 | ; UDP_socket_send |
189 | ; UDP_output |
206 | ; |
190 | ; |
207 | ; IN: eax = socket pointer |
191 | ; IN: eax = socket pointer |
208 | ; ecx = number of bytes to send |
192 | ; ecx = number of bytes to send |
Line 209... | Line 193... | ||
209 | ; esi = pointer to data |
193 | ; esi = pointer to data |
210 | ; |
194 | ; |
Line 211... | Line -... | ||
211 | ;----------------------------------------------------------------- |
- | |
212 | 195 | ;----------------------------------------------------------------- |
|
213 | align 4 |
- | |
214 | UDP_socket_send: |
- | |
215 | - | ||
216 | mov edx, dword [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort] ; load local port and remote port at once |
- | |
Line -... | Line 196... | ||
- | 196 | ||
217 | DEBUGF 1,"local port: %x, remote port: %x\n",\ |
197 | align 4 |
- | 198 | UDP_output: |
|
- | 199 | ||
- | 200 | DEBUGF 1,"UDP_output: socket:%x, bytes: %u, data ptr: %x\n", eax, ecx, esi |
|
- | 201 | ||
- | 202 | mov dx, [eax + UDP_SOCKET.RemotePort] |
|
- | 203 | DEBUGF 1,"remote port: %u\n", dx |
|
- | 204 | rol dx, 8 |
|
- | 205 | rol edx, 16 |
|
- | 206 | mov dx, [eax + UDP_SOCKET.LocalPort] |
|
Line 218... | Line 207... | ||
218 | [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort]:4,\ |
207 | DEBUGF 1,"local port: %u\n", dx |
219 | [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.RemotePort]:4 |
208 | rol dx, 8 |
220 | mov ebx, [eax + SOCKET_head.end + IPv4_SOCKET.LocalIP] |
209 | |
Line 221... | Line 210... | ||
221 | mov eax, [eax + SOCKET_head.end + IPv4_SOCKET.RemoteIP] |
210 | |
Line 222... | Line 211... | ||
222 | 211 | mov ebx, [eax + IP_SOCKET.LocalIP] |
|
223 | DEBUGF 1,"Create UDP Packet (size=%u)\n",ecx |
- | |
224 | 212 | mov eax, [eax + IP_SOCKET.RemoteIP] |
|
225 | mov di , IP_PROTO_UDP |
213 | |
Line 226... | Line 214... | ||
226 | sub esp, 8 ; Data ptr and data size will be placed here |
214 | mov di , IP_PROTO_UDP |
227 | add ecx, UDP_Packet.Data |
215 | sub esp, 8 ; Data ptr and data size will be placed here |
Line 228... | Line 216... | ||
228 | 216 | add ecx, UDP_Packet.Data |
|
Line 249... | Line 237... | ||
249 | mov ecx, [esp] |
237 | mov ecx, [esp] |
250 | and ecx, 3 |
238 | and ecx, 3 |
251 | rep movsb |
239 | rep movsb |
252 | pop ecx edi |
240 | pop ecx edi |
Line 253... | Line 241... | ||
253 | 241 | ||
254 | pop dword [edi + UDP_Packet.SourcePort] ; fill in both portnumbers |
242 | pop dword [edi + UDP_Packet.SourcePort] |
Line 255... | Line 243... | ||
255 | mov [edi + UDP_Packet.Checksum], 0 ; set it to zero, to calculate checksum |
243 | mov [edi + UDP_Packet.Checksum], 0 ; set it to zero, to calculate checksum |
256 | 244 | ||
257 | ; Checksum |
245 | ; Checksum |
Line 261... | Line 249... | ||
261 | call UDP_checksum |
249 | call UDP_checksum |
Line 262... | Line 250... | ||
262 | 250 | ||
Line 263... | Line 251... | ||
263 | inc [UDP_PACKETS_TX] |
251 | inc [UDP_PACKETS_TX] |
- | 252 | ||
264 | 253 | DEBUGF 1,"Sending UDP Packet to device %x\n", ebx |
|
Line 265... | Line 254... | ||
265 | DEBUGF 1,"Sending UDP Packet to device %x\n", ebx |
254 | |
266 | jmp ETH_sender |
- | |
267 | 255 | jmp NET_send |
|
268 | .fail: |
256 | |
Line 269... | Line 257... | ||
269 | ; todo: queue the packet |
257 | .fail: |
270 | add esp, 8+8 |
258 | add esp, 8+8 |
271 | ret |
259 | ret |
272 | 260 | ||
273 | 261 | ||
274 | 262 | ||
275 | 263 | ||
276 | ;----------------------------------------------------------------- |
264 | ;----------------------------------------------------------------- |