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