Rev 3725 | Rev 4265 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3555 | Serge | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3908 | Serge | 3 | ;; Copyright (C) KolibriOS team 2004-2013. 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: 3908 $ |
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 | |||
39 | ;----------------------------------------------------------------- |
||
1 | ha | 40 | ; |
3555 | Serge | 41 | ; UDP_init |
379 | serge | 42 | ; |
3555 | Serge | 43 | ; This function resets all UDP variables |
44 | ; |
||
45 | ;----------------------------------------------------------------- |
||
46 | macro UDP_init { |
||
261 | hidnplayr | 47 | |
3555 | Serge | 48 | xor eax, eax |
49 | mov edi, UDP_PACKETS_TX |
||
3626 | Serge | 50 | mov ecx, 2*NET_DEVICES_MAX |
3725 | Serge | 51 | rep stosd |
3555 | Serge | 52 | } |
261 | hidnplayr | 53 | |
3555 | Serge | 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 | ;----------------------------------------------------------------- |
||
261 | hidnplayr | 102 | ; |
3555 | Serge | 103 | ; UDP_input: |
261 | hidnplayr | 104 | ; |
3555 | Serge | 105 | ; Called by IPv4_input, |
106 | ; this procedure will inject the udp data diagrams in the application sockets. |
||
261 | hidnplayr | 107 | ; |
3555 | Serge | 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 |
||
261 | hidnplayr | 114 | ; |
3555 | Serge | 115 | ; OUT: / |
116 | ; |
||
117 | ;----------------------------------------------------------------- |
||
118 | align 4 |
||
119 | UDP_input: |
||
261 | hidnplayr | 120 | |
3589 | Serge | 121 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: size=%u\n", ecx |
261 | hidnplayr | 122 | |
3555 | Serge | 123 | ; First validate, checksum |
261 | hidnplayr | 124 | |
3555 | Serge | 125 | neg [esi + UDP_header.Checksum] ; substract checksum from 0 |
126 | jz .no_checksum ; if checksum is zero, it is considered valid |
||
261 | hidnplayr | 127 | |
3555 | Serge | 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: |
||
3589 | Serge | 134 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: checksum ok\n" |
3555 | Serge | 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 | |||
3725 | Serge | 144 | pusha |
145 | mov ecx, socket_mutex |
||
146 | call mutex_lock |
||
147 | popa |
||
148 | |||
3555 | Serge | 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 |
||
3725 | Serge | 157 | jz .dump_ |
3555 | Serge | 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 | |||
3589 | Serge | 168 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: socket=%x\n", eax |
3555 | Serge | 169 | |
3725 | Serge | 170 | pusha |
171 | mov ecx, socket_mutex |
||
172 | call mutex_unlock |
||
173 | popa |
||
174 | |||
3555 | Serge | 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 | ; @@: |
||
1 | ha | 182 | ; |
3555 | Serge | 183 | ; FIXME: UDP should check remote IP, but not under all circumstances! |
184 | |||
185 | cmp [eax + UDP_SOCKET.firstpacket], 0 |
||
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: |
||
3725 | Serge | 197 | call NET_ptr_to_num4 |
198 | inc [UDP_PACKETS_RX + edi] |
||
3555 | Serge | 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 | |||
3589 | Serge | 212 | 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 | 213 | mov [eax + UDP_SOCKET.RemotePort], cx |
214 | inc [eax + UDP_SOCKET.firstpacket] |
||
215 | |||
216 | jmp .updatesock |
||
217 | |||
3725 | Serge | 218 | .dump_: |
3555 | Serge | 219 | |
3725 | Serge | 220 | pusha |
221 | mov ecx, socket_mutex |
||
222 | call mutex_unlock |
||
223 | popa |
||
224 | |||
225 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: no socket found\n" |
||
226 | |||
227 | jmp .dump |
||
228 | |||
3555 | Serge | 229 | .checksum_mismatch: |
3589 | Serge | 230 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: checksum mismatch\n" |
3555 | Serge | 231 | |
232 | .dump: |
||
3908 | Serge | 233 | call NET_packet_free |
3555 | Serge | 234 | add esp, 4 ; pop (balance stack) |
3589 | Serge | 235 | DEBUGF DEBUG_NETWORK_VERBOSE,"UDP_input: dumping\n" |
3555 | Serge | 236 | |
237 | ret |
||
238 | |||
239 | |||
240 | |||
241 | |||
242 | ;----------------------------------------------------------------- |
||
1 | ha | 243 | ; |
3555 | Serge | 244 | ; UDP_output |
245 | ; |
||
246 | ; IN: eax = socket pointer |
||
247 | ; ecx = number of bytes to send |
||
248 | ; esi = pointer to data |
||
249 | ; |
||
250 | ;----------------------------------------------------------------- |
||
379 | serge | 251 | |
3555 | Serge | 252 | align 4 |
253 | UDP_output: |
||
379 | serge | 254 | |
3589 | Serge | 255 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_output: socket=%x bytes=%u data_ptr=%x\n", eax, ecx, esi |
379 | serge | 256 | |
3555 | Serge | 257 | mov dx, [eax + UDP_SOCKET.RemotePort] |
3589 | Serge | 258 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_output: remote port=%x, ", dx ; FIXME: find a way to print big endian values with debugf |
3555 | Serge | 259 | rol edx, 16 |
260 | mov dx, [eax + UDP_SOCKET.LocalPort] |
||
3589 | Serge | 261 | DEBUGF DEBUG_NETWORK_VERBOSE, "local port=%x\n", dx |
379 | serge | 262 | |
3555 | Serge | 263 | sub esp, 8 ; Data ptr and data size will be placed here |
264 | push edx esi |
||
265 | mov ebx, [eax + SOCKET.device] |
||
266 | mov edx, [eax + IP_SOCKET.LocalIP] |
||
267 | mov eax, [eax + IP_SOCKET.RemoteIP] |
||
268 | mov di, IP_PROTO_UDP shl 8 + 128 |
||
269 | add ecx, sizeof.UDP_header |
||
270 | call IPv4_output |
||
271 | jz .fail |
||
272 | mov [esp + 8], eax ; pointer to buffer start |
||
273 | mov [esp + 8 + 4], edx ; buffer size |
||
379 | serge | 274 | |
3555 | Serge | 275 | mov [edi + UDP_header.Length], cx |
276 | rol [edi + UDP_header.Length], 8 |
||
379 | serge | 277 | |
3555 | Serge | 278 | pop esi |
279 | push edi ecx |
||
280 | sub ecx, sizeof.UDP_header |
||
281 | add edi, sizeof.UDP_header |
||
282 | shr ecx, 2 |
||
3725 | Serge | 283 | rep movsd |
3555 | Serge | 284 | mov ecx, [esp] |
285 | and ecx, 3 |
||
3725 | Serge | 286 | rep movsb |
3555 | Serge | 287 | pop ecx edi |
379 | serge | 288 | |
3555 | Serge | 289 | pop dword [edi + UDP_header.SourcePort] |
1 | ha | 290 | |
3555 | Serge | 291 | ; Checksum |
292 | mov esi, edi |
||
293 | mov [edi + UDP_header.Checksum], 0 |
||
294 | UDP_checksum (edi-4), (edi-8) ; FIXME: IPv4 packet could have options.. |
||
379 | serge | 295 | |
3589 | Serge | 296 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_output: sending with device %x\n", ebx |
3555 | Serge | 297 | call [ebx + NET_DEVICE.transmit] |
298 | test eax, eax |
||
299 | jnz @f |
||
3725 | Serge | 300 | call NET_ptr_to_num4 |
301 | inc [UDP_PACKETS_TX + edi] |
||
3555 | Serge | 302 | @@: |
379 | serge | 303 | |
3555 | Serge | 304 | ret |
379 | serge | 305 | |
3555 | Serge | 306 | .fail: |
3589 | Serge | 307 | DEBUGF DEBUG_NETWORK_ERROR, "UDP_output: failed\n" |
3555 | Serge | 308 | add esp, 4+4+8 |
309 | or eax, -1 |
||
310 | ret |
||
379 | serge | 311 | |
312 | |||
313 | |||
3555 | Serge | 314 | ;--------------------------------------------------------------------------- |
315 | ; |
||
316 | ; UDP_API |
||
317 | ; |
||
318 | ; This function is called by system function 75 |
||
319 | ; |
||
320 | ; IN: subfunction number in bl |
||
321 | ; device number in bh |
||
322 | ; ecx, edx, .. depends on subfunction |
||
323 | ; |
||
324 | ; OUT: |
||
325 | ; |
||
326 | ;--------------------------------------------------------------------------- |
||
1 | ha | 327 | |
3555 | Serge | 328 | align 4 |
329 | UDP_api: |
||
379 | serge | 330 | |
3555 | Serge | 331 | movzx eax, bh |
332 | shl eax, 2 |
||
379 | serge | 333 | |
3555 | Serge | 334 | test bl, bl |
335 | jz .packets_tx ; 0 |
||
336 | dec bl |
||
337 | jz .packets_rx ; 1 |
||
379 | serge | 338 | |
3555 | Serge | 339 | .error: |
340 | mov eax, -1 |
||
341 | ret |
||
379 | serge | 342 | |
3555 | Serge | 343 | .packets_tx: |
344 | mov eax, [UDP_PACKETS_TX + eax] |
||
345 | ret |
||
379 | serge | 346 | |
3555 | Serge | 347 | .packets_rx: |
348 | mov eax, [UDP_PACKETS_RX + eax] |
||
349 | ret |