Rev 5594 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3555 | Serge | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
5565 | serge | 3 | ;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;; |
3555 | Serge | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;; UDP.INC ;; |
||
7 | ;; ;; |
||
8 | ;; Part of the tcp/ip network stack for KolibriOS ;; |
||
9 | ;; ;; |
||
10 | ;; Written by hidnplayr@kolibrios.org ;; |
||
11 | ;; ;; |
||
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
13 | ;; Version 2, June 1991 ;; |
||
14 | ;; ;; |
||
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
379 | serge | 16 | |
593 | mikedld | 17 | $Revision: 5984 $ |
379 | serge | 18 | |
593 | mikedld | 19 | |
3555 | Serge | 20 | struct UDP_header |
21 | |||
22 | SourcePort dw ? |
||
23 | DestinationPort dw ? |
||
24 | Length dw ? ; Length of (UDP Header + Data) |
||
25 | Checksum dw ? |
||
26 | |||
27 | ends |
||
28 | |||
29 | |||
3725 | Serge | 30 | uglobal |
3555 | Serge | 31 | align 4 |
3725 | Serge | 32 | |
3626 | Serge | 33 | UDP_PACKETS_TX rd NET_DEVICES_MAX |
34 | UDP_PACKETS_RX rd NET_DEVICES_MAX |
||
3725 | Serge | 35 | |
3555 | Serge | 36 | endg |
37 | |||
38 | |||
5984 | serge | 39 | ;-----------------------------------------------------------------; |
40 | ; ; |
||
41 | ; UDP_init: This function resets all UDP variables ; |
||
42 | ; ; |
||
43 | ;-----------------------------------------------------------------; |
||
3555 | Serge | 44 | macro UDP_init { |
261 | hidnplayr | 45 | |
3555 | Serge | 46 | xor eax, eax |
47 | mov edi, UDP_PACKETS_TX |
||
3626 | Serge | 48 | mov ecx, 2*NET_DEVICES_MAX |
3725 | Serge | 49 | rep stosd |
3555 | Serge | 50 | } |
261 | hidnplayr | 51 | |
3555 | Serge | 52 | |
53 | macro UDP_checksum IP1, IP2 { ; esi = ptr to udp packet, ecx = packet size, destroys: ecx, edx |
||
54 | |||
55 | ; Pseudoheader |
||
56 | mov edx, IP_PROTO_UDP |
||
57 | |||
5984 | serge | 58 | add dl, byte[IP1+1] |
59 | adc dh, byte[IP1+0] |
||
60 | adc dl, byte[IP1+3] |
||
61 | adc dh, byte[IP1+2] |
||
3555 | Serge | 62 | |
5984 | serge | 63 | adc dl, byte[IP2+1] |
64 | adc dh, byte[IP2+0] |
||
65 | adc dl, byte[IP2+3] |
||
66 | adc dh, byte[IP2+2] |
||
3555 | Serge | 67 | |
68 | adc dl, cl ; byte[esi+UDP_header.Length+1] |
||
69 | adc dh, ch ; byte[esi+UDP_header.Length+0] |
||
70 | |||
71 | ; Done with pseudoheader, now do real header |
||
72 | adc dl, byte[esi+UDP_header.SourcePort+1] |
||
73 | adc dh, byte[esi+UDP_header.SourcePort+0] |
||
74 | |||
75 | adc dl, byte[esi+UDP_header.DestinationPort+1] |
||
76 | adc dh, byte[esi+UDP_header.DestinationPort+0] |
||
77 | |||
78 | adc dl, byte[esi+UDP_header.Length+1] |
||
79 | adc dh, byte[esi+UDP_header.Length+0] |
||
80 | |||
81 | adc edx, 0 |
||
82 | |||
83 | ; Done with header, now do data |
||
84 | push esi |
||
85 | movzx ecx, [esi+UDP_header.Length] |
||
86 | rol cx , 8 |
||
87 | sub cx , sizeof.UDP_header |
||
88 | add esi, sizeof.UDP_header |
||
89 | |||
90 | call checksum_1 |
||
91 | call checksum_2 |
||
92 | pop esi |
||
93 | |||
94 | add [esi+UDP_header.Checksum], dx ; this final instruction will set or clear ZF :) |
||
95 | |||
96 | } |
||
97 | |||
98 | |||
5984 | serge | 99 | ;-----------------------------------------------------------------; |
100 | ; ; |
||
101 | ; UDP_input: Inject the UDP data in the application sockets. ; |
||
102 | ; ; |
||
103 | ; IN: [esp] = ptr to buffer ; |
||
104 | ; ebx = ptr to device struct ; |
||
105 | ; ecx = UDP packet size ; |
||
106 | ; edx = ptr to IPv4 header ; |
||
107 | ; esi = ptr to UDP packet data ; |
||
108 | ; edi = interface number*4 ; |
||
109 | ; ; |
||
110 | ; OUT: / ; |
||
111 | ; ; |
||
112 | ;-----------------------------------------------------------------; |
||
3555 | Serge | 113 | align 4 |
114 | UDP_input: |
||
261 | hidnplayr | 115 | |
3589 | Serge | 116 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: size=%u\n", ecx |
261 | hidnplayr | 117 | |
3555 | Serge | 118 | ; First validate, checksum |
261 | hidnplayr | 119 | |
3555 | Serge | 120 | neg [esi + UDP_header.Checksum] ; substract checksum from 0 |
121 | jz .no_checksum ; if checksum is zero, it is considered valid |
||
261 | hidnplayr | 122 | |
3555 | Serge | 123 | ; otherwise, we will re-calculate the checksum and add it to this value, thus creating 0 when it is correct |
124 | |||
5984 | serge | 125 | mov eax, edx |
126 | UDP_checksum (eax+IPv4_header.SourceAddress), (eax+IPv4_header.DestinationAddress) |
||
3555 | Serge | 127 | jnz .checksum_mismatch |
128 | |||
129 | .no_checksum: |
||
3589 | Serge | 130 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: checksum ok\n" |
3555 | Serge | 131 | |
132 | ; Convert length to little endian |
||
133 | |||
134 | rol [esi + UDP_header.Length], 8 |
||
135 | |||
136 | ; Look for a socket where |
||
137 | ; IP Packet UDP Destination Port = local Port |
||
138 | ; IP Packet SA = Remote IP |
||
139 | |||
3725 | Serge | 140 | pusha |
141 | mov ecx, socket_mutex |
||
142 | call mutex_lock |
||
143 | popa |
||
144 | |||
3555 | Serge | 145 | mov cx, [esi + UDP_header.SourcePort] |
146 | mov dx, [esi + UDP_header.DestinationPort] |
||
147 | mov eax, net_sockets |
||
148 | .next_socket: |
||
149 | mov eax, [eax + SOCKET.NextPtr] |
||
150 | or eax, eax |
||
5565 | serge | 151 | jz .unlock_dump |
3555 | Serge | 152 | |
153 | cmp [eax + SOCKET.Domain], AF_INET4 |
||
154 | jne .next_socket |
||
155 | |||
156 | cmp [eax + SOCKET.Protocol], IP_PROTO_UDP |
||
157 | jne .next_socket |
||
158 | |||
159 | cmp [eax + UDP_SOCKET.LocalPort], dx |
||
160 | jne .next_socket |
||
161 | |||
3589 | Serge | 162 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: socket=%x\n", eax |
3555 | Serge | 163 | |
3725 | Serge | 164 | pusha |
165 | mov ecx, socket_mutex |
||
166 | call mutex_unlock |
||
167 | popa |
||
168 | |||
5984 | serge | 169 | ;;; TODO: when packet is processed, check more sockets?! |
3555 | Serge | 170 | |
5984 | serge | 171 | ; FIXME: check remote IP if possible |
172 | ; |
||
3555 | Serge | 173 | ; cmp [eax + IP_SOCKET.RemoteIP], 0xffffffff |
174 | ; je @f |
||
5984 | serge | 175 | ; cmp [eax + IP_SOCKET.RemoteIP], |
3555 | Serge | 176 | ; jne .next_socket |
177 | ; @@: |
||
178 | |||
4265 | Serge | 179 | cmp [eax + UDP_SOCKET.RemotePort], 0 |
3555 | Serge | 180 | je .updateport |
181 | |||
182 | cmp [eax + UDP_SOCKET.RemotePort], cx |
||
183 | jne .dump |
||
184 | |||
185 | pusha |
||
186 | lea ecx, [eax + SOCKET.mutex] |
||
187 | call mutex_lock |
||
188 | popa |
||
189 | |||
190 | .updatesock: |
||
3725 | Serge | 191 | inc [UDP_PACKETS_RX + edi] |
3555 | Serge | 192 | |
193 | movzx ecx, [esi + UDP_header.Length] |
||
194 | sub ecx, sizeof.UDP_header |
||
195 | add esi, sizeof.UDP_header |
||
196 | |||
197 | jmp SOCKET_input |
||
198 | |||
199 | .updateport: |
||
200 | pusha |
||
201 | lea ecx, [eax + SOCKET.mutex] |
||
202 | call mutex_lock |
||
203 | popa |
||
204 | |||
3589 | Serge | 205 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: new remote port=%x\n", cx ; FIXME: find a way to print big endian values with debugf |
3555 | Serge | 206 | mov [eax + UDP_SOCKET.RemotePort], cx |
207 | jmp .updatesock |
||
208 | |||
5565 | serge | 209 | .unlock_dump: |
3725 | Serge | 210 | pusha |
211 | mov ecx, socket_mutex |
||
212 | call mutex_unlock |
||
213 | popa |
||
214 | |||
215 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: no socket found\n" |
||
216 | jmp .dump |
||
217 | |||
3555 | Serge | 218 | .checksum_mismatch: |
3589 | Serge | 219 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: checksum mismatch\n" |
3555 | Serge | 220 | |
221 | .dump: |
||
5565 | serge | 222 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: dumping\n" |
223 | call NET_BUFF_free |
||
3555 | Serge | 224 | ret |
225 | |||
226 | |||
227 | |||
5984 | serge | 228 | ;-----------------------------------------------------------------; |
229 | ; ; |
||
230 | ; UDP_output: Create an UDP packet. ; |
||
231 | ; ; |
||
232 | ; IN: eax = socket pointer ; |
||
233 | ; ecx = number of bytes to send ; |
||
234 | ; esi = pointer to data ; |
||
235 | ; ; |
||
236 | ; OUT: eax = -1 on error ; |
||
237 | ; ; |
||
238 | ;-----------------------------------------------------------------; |
||
379 | serge | 239 | |
3555 | Serge | 240 | align 4 |
241 | UDP_output: |
||
379 | serge | 242 | |
3589 | Serge | 243 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_output: socket=%x bytes=%u data_ptr=%x\n", eax, ecx, esi |
379 | serge | 244 | |
3555 | Serge | 245 | mov dx, [eax + UDP_SOCKET.RemotePort] |
3589 | Serge | 246 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_output: remote port=%x, ", dx ; FIXME: find a way to print big endian values with debugf |
3555 | Serge | 247 | rol edx, 16 |
248 | mov dx, [eax + UDP_SOCKET.LocalPort] |
||
3589 | Serge | 249 | DEBUGF DEBUG_NETWORK_VERBOSE, "local port=%x\n", dx |
379 | serge | 250 | |
5565 | serge | 251 | sub esp, 4 ; Data ptr will be placed here |
3555 | Serge | 252 | push edx esi |
5984 | serge | 253 | mov ebx, [eax + IP_SOCKET.device] |
3555 | Serge | 254 | mov edx, [eax + IP_SOCKET.LocalIP] |
5984 | serge | 255 | mov edi, [eax + IP_SOCKET.RemoteIP] |
256 | mov al, [eax + IP_SOCKET.ttl] |
||
257 | mov ah, IP_PROTO_UDP |
||
3555 | Serge | 258 | add ecx, sizeof.UDP_header |
259 | call IPv4_output |
||
260 | jz .fail |
||
261 | mov [esp + 8], eax ; pointer to buffer start |
||
379 | serge | 262 | |
3555 | Serge | 263 | mov [edi + UDP_header.Length], cx |
264 | rol [edi + UDP_header.Length], 8 |
||
379 | serge | 265 | |
3555 | Serge | 266 | pop esi |
267 | push edi ecx |
||
268 | sub ecx, sizeof.UDP_header |
||
269 | add edi, sizeof.UDP_header |
||
270 | shr ecx, 2 |
||
3725 | Serge | 271 | rep movsd |
3555 | Serge | 272 | mov ecx, [esp] |
273 | and ecx, 3 |
||
3725 | Serge | 274 | rep movsb |
3555 | Serge | 275 | pop ecx edi |
379 | serge | 276 | |
3555 | Serge | 277 | pop dword [edi + UDP_header.SourcePort] |
1 | ha | 278 | |
3555 | Serge | 279 | ; Checksum |
280 | mov esi, edi |
||
281 | mov [edi + UDP_header.Checksum], 0 |
||
282 | UDP_checksum (edi-4), (edi-8) ; FIXME: IPv4 packet could have options.. |
||
379 | serge | 283 | |
3589 | Serge | 284 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_output: sending with device %x\n", ebx |
3555 | Serge | 285 | call [ebx + NET_DEVICE.transmit] |
286 | test eax, eax |
||
287 | jnz @f |
||
3725 | Serge | 288 | call NET_ptr_to_num4 |
289 | inc [UDP_PACKETS_TX + edi] |
||
3555 | Serge | 290 | @@: |
379 | serge | 291 | |
3555 | Serge | 292 | ret |
379 | serge | 293 | |
3555 | Serge | 294 | .fail: |
3589 | Serge | 295 | DEBUGF DEBUG_NETWORK_ERROR, "UDP_output: failed\n" |
3555 | Serge | 296 | add esp, 4+4+8 |
297 | or eax, -1 |
||
298 | ret |
||
379 | serge | 299 | |
300 | |||
301 | |||
4265 | Serge | 302 | |
5984 | serge | 303 | ;-----------------------------------------------------------------; |
304 | ; ; |
||
305 | ; UDP_connect ; |
||
306 | ; ; |
||
307 | ; IN: eax = socket pointer ; |
||
308 | ; ; |
||
309 | ; OUT: eax = 0 on success ; |
||
310 | ; eax = -1 on error ; |
||
311 | ; ebx = error code on error ; |
||
312 | ; ; |
||
313 | ;-----------------------------------------------------------------; |
||
4265 | Serge | 314 | align 4 |
315 | UDP_connect: |
||
316 | |||
317 | test [eax + SOCKET.state], SS_ISCONNECTED |
||
318 | jz @f |
||
319 | call UDP_disconnect |
||
320 | @@: |
||
321 | |||
322 | push eax edx |
||
323 | lea ecx, [eax + SOCKET.mutex] |
||
324 | call mutex_lock |
||
325 | pop edx eax |
||
326 | |||
327 | ; Fill in local IP |
||
328 | cmp [eax + IP_SOCKET.LocalIP], 0 |
||
329 | jne @f |
||
330 | push [IP_LIST + 4] ; FIXME: use correct local IP |
||
331 | pop [eax + IP_SOCKET.LocalIP] |
||
332 | |||
333 | ; Fill in remote port and IP, overwriting eventually previous values |
||
334 | pushw [edx + 2] |
||
335 | pop [eax + UDP_SOCKET.RemotePort] |
||
336 | |||
337 | pushd [edx + 4] |
||
338 | pop [eax + IP_SOCKET.RemoteIP] |
||
339 | |||
340 | ; Find a local port, if user didnt define one |
||
341 | cmp [eax + UDP_SOCKET.LocalPort], 0 |
||
342 | jne @f |
||
343 | call SOCKET_find_port |
||
344 | @@: |
||
345 | |||
346 | push eax |
||
347 | lea ecx, [eax + SOCKET.mutex] |
||
348 | call mutex_unlock |
||
349 | pop eax |
||
350 | |||
351 | call SOCKET_is_connected |
||
352 | |||
353 | xor eax, eax |
||
354 | ret |
||
355 | |||
356 | |||
5984 | serge | 357 | ;-----------------------------------------------------------------; |
358 | ; ; |
||
359 | ; UDP_disconnect ; |
||
360 | ; ; |
||
361 | ; IN: eax = socket pointer ; |
||
362 | ; ; |
||
363 | ; OUT: eax = socket pointer ; |
||
364 | ; ; |
||
365 | ;-----------------------------------------------------------------; |
||
4265 | Serge | 366 | align 4 |
367 | UDP_disconnect: |
||
368 | |||
369 | ; TODO: remove the pending received data |
||
370 | |||
371 | call SOCKET_is_disconnected |
||
372 | |||
373 | ret |
||
374 | |||
375 | |||
376 | |||
377 | |||
378 | |||
5984 | serge | 379 | ;-----------------------------------------------------------------; |
380 | ; ; |
||
381 | ; UDP_api: This function is called by system function 76 ; |
||
382 | ; ; |
||
383 | ; IN: bl = subfunction number in bl ; |
||
384 | ; bh = device number in bh ; |
||
385 | ; ecx, edx, .. depends on subfunction ; |
||
386 | ; ; |
||
387 | ; OUT: depends on subfunction ; |
||
388 | ; ; |
||
389 | ;-----------------------------------------------------------------; |
||
3555 | Serge | 390 | align 4 |
391 | UDP_api: |
||
379 | serge | 392 | |
3555 | Serge | 393 | movzx eax, bh |
394 | shl eax, 2 |
||
379 | serge | 395 | |
3555 | Serge | 396 | test bl, bl |
397 | jz .packets_tx ; 0 |
||
398 | dec bl |
||
399 | jz .packets_rx ; 1 |
||
379 | serge | 400 | |
3555 | Serge | 401 | .error: |
402 | mov eax, -1 |
||
403 | ret |
||
379 | serge | 404 | |
3555 | Serge | 405 | .packets_tx: |
406 | mov eax, [UDP_PACKETS_TX + eax] |
||
407 | ret |
||
379 | serge | 408 | |
3555 | Serge | 409 | .packets_rx: |
410 | mov eax, [UDP_PACKETS_RX + eax] |
||
411 | ret |