Rev 3626 | Rev 3908 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3555 | Serge | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3 | ;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;; |
||
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: 3725 $ |
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 |
||
3725 | Serge | 119 | diff16 "UDP packetgfgfgfgfs", 0, $ |
3555 | Serge | 120 | UDP_input: |
261 | hidnplayr | 121 | |
3589 | Serge | 122 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: size=%u\n", ecx |
261 | hidnplayr | 123 | |
3555 | Serge | 124 | ; First validate, checksum |
261 | hidnplayr | 125 | |
3555 | Serge | 126 | neg [esi + UDP_header.Checksum] ; substract checksum from 0 |
127 | jz .no_checksum ; if checksum is zero, it is considered valid |
||
261 | hidnplayr | 128 | |
3555 | Serge | 129 | ; otherwise, we will re-calculate the checksum and add it to this value, thus creating 0 when it is correct |
130 | |||
131 | UDP_checksum (edi), (edi+4) |
||
132 | jnz .checksum_mismatch |
||
133 | |||
134 | .no_checksum: |
||
3589 | Serge | 135 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: checksum ok\n" |
3555 | Serge | 136 | |
137 | ; Convert length to little endian |
||
138 | |||
139 | rol [esi + UDP_header.Length], 8 |
||
140 | |||
141 | ; Look for a socket where |
||
142 | ; IP Packet UDP Destination Port = local Port |
||
143 | ; IP Packet SA = Remote IP |
||
144 | |||
3725 | Serge | 145 | pusha |
146 | mov ecx, socket_mutex |
||
147 | call mutex_lock |
||
148 | popa |
||
149 | |||
3555 | Serge | 150 | mov cx, [esi + UDP_header.SourcePort] |
151 | mov dx, [esi + UDP_header.DestinationPort] |
||
152 | mov edi, [edi + 4] ; ipv4 source address |
||
153 | mov eax, net_sockets |
||
154 | |||
155 | .next_socket: |
||
156 | mov eax, [eax + SOCKET.NextPtr] |
||
157 | or eax, eax |
||
3725 | Serge | 158 | jz .dump_ |
3555 | Serge | 159 | |
160 | cmp [eax + SOCKET.Domain], AF_INET4 |
||
161 | jne .next_socket |
||
162 | |||
163 | cmp [eax + SOCKET.Protocol], IP_PROTO_UDP |
||
164 | jne .next_socket |
||
165 | |||
166 | cmp [eax + UDP_SOCKET.LocalPort], dx |
||
167 | jne .next_socket |
||
168 | |||
3589 | Serge | 169 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: socket=%x\n", eax |
3555 | Serge | 170 | |
3725 | Serge | 171 | pusha |
172 | mov ecx, socket_mutex |
||
173 | call mutex_unlock |
||
174 | popa |
||
175 | |||
3555 | Serge | 176 | ;;; TODO: when packet is processed, check more sockets! |
177 | |||
178 | ; cmp [eax + IP_SOCKET.RemoteIP], 0xffffffff |
||
179 | ; je @f |
||
180 | ; cmp [eax + IP_SOCKET.RemoteIP], edi |
||
181 | ; jne .next_socket |
||
182 | ; @@: |
||
1 | ha | 183 | ; |
3555 | Serge | 184 | ; FIXME: UDP should check remote IP, but not under all circumstances! |
185 | |||
186 | cmp [eax + UDP_SOCKET.firstpacket], 0 |
||
187 | je .updateport |
||
188 | |||
189 | cmp [eax + UDP_SOCKET.RemotePort], cx |
||
190 | jne .dump |
||
191 | |||
192 | pusha |
||
193 | lea ecx, [eax + SOCKET.mutex] |
||
194 | call mutex_lock |
||
195 | popa |
||
196 | |||
197 | .updatesock: |
||
3725 | Serge | 198 | call NET_ptr_to_num4 |
199 | inc [UDP_PACKETS_RX + edi] |
||
3555 | Serge | 200 | |
201 | movzx ecx, [esi + UDP_header.Length] |
||
202 | sub ecx, sizeof.UDP_header |
||
203 | add esi, sizeof.UDP_header |
||
204 | |||
205 | jmp SOCKET_input |
||
206 | |||
207 | .updateport: |
||
208 | pusha |
||
209 | lea ecx, [eax + SOCKET.mutex] |
||
210 | call mutex_lock |
||
211 | popa |
||
212 | |||
3589 | Serge | 213 | 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 | 214 | mov [eax + UDP_SOCKET.RemotePort], cx |
215 | inc [eax + UDP_SOCKET.firstpacket] |
||
216 | |||
217 | jmp .updatesock |
||
218 | |||
3725 | Serge | 219 | .dump_: |
3555 | Serge | 220 | |
3725 | Serge | 221 | pusha |
222 | mov ecx, socket_mutex |
||
223 | call mutex_unlock |
||
224 | popa |
||
225 | |||
226 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: no socket found\n" |
||
227 | |||
228 | jmp .dump |
||
229 | |||
3555 | Serge | 230 | .checksum_mismatch: |
3589 | Serge | 231 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: checksum mismatch\n" |
3555 | Serge | 232 | |
233 | .dump: |
||
234 | call kernel_free |
||
235 | add esp, 4 ; pop (balance stack) |
||
3589 | Serge | 236 | DEBUGF DEBUG_NETWORK_VERBOSE,"UDP_input: dumping\n" |
3555 | Serge | 237 | |
238 | ret |
||
239 | |||
240 | |||
241 | |||
242 | |||
243 | ;----------------------------------------------------------------- |
||
1 | ha | 244 | ; |
3555 | Serge | 245 | ; UDP_output |
246 | ; |
||
247 | ; IN: eax = socket pointer |
||
248 | ; ecx = number of bytes to send |
||
249 | ; esi = pointer to data |
||
250 | ; |
||
251 | ;----------------------------------------------------------------- |
||
379 | serge | 252 | |
3555 | Serge | 253 | align 4 |
254 | UDP_output: |
||
379 | serge | 255 | |
3589 | Serge | 256 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_output: socket=%x bytes=%u data_ptr=%x\n", eax, ecx, esi |
379 | serge | 257 | |
3555 | Serge | 258 | mov dx, [eax + UDP_SOCKET.RemotePort] |
3589 | Serge | 259 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_output: remote port=%x, ", dx ; FIXME: find a way to print big endian values with debugf |
3555 | Serge | 260 | rol edx, 16 |
261 | mov dx, [eax + UDP_SOCKET.LocalPort] |
||
3589 | Serge | 262 | DEBUGF DEBUG_NETWORK_VERBOSE, "local port=%x\n", dx |
379 | serge | 263 | |
3555 | Serge | 264 | sub esp, 8 ; Data ptr and data size will be placed here |
265 | push edx esi |
||
266 | mov ebx, [eax + SOCKET.device] |
||
267 | mov edx, [eax + IP_SOCKET.LocalIP] |
||
268 | mov eax, [eax + IP_SOCKET.RemoteIP] |
||
269 | mov di, IP_PROTO_UDP shl 8 + 128 |
||
270 | add ecx, sizeof.UDP_header |
||
271 | call IPv4_output |
||
272 | jz .fail |
||
273 | mov [esp + 8], eax ; pointer to buffer start |
||
274 | mov [esp + 8 + 4], edx ; buffer size |
||
379 | serge | 275 | |
3555 | Serge | 276 | mov [edi + UDP_header.Length], cx |
277 | rol [edi + UDP_header.Length], 8 |
||
379 | serge | 278 | |
3555 | Serge | 279 | pop esi |
280 | push edi ecx |
||
281 | sub ecx, sizeof.UDP_header |
||
282 | add edi, sizeof.UDP_header |
||
283 | shr ecx, 2 |
||
3725 | Serge | 284 | rep movsd |
3555 | Serge | 285 | mov ecx, [esp] |
286 | and ecx, 3 |
||
3725 | Serge | 287 | rep movsb |
3555 | Serge | 288 | pop ecx edi |
379 | serge | 289 | |
3555 | Serge | 290 | pop dword [edi + UDP_header.SourcePort] |
1 | ha | 291 | |
3555 | Serge | 292 | ; Checksum |
293 | mov esi, edi |
||
294 | mov [edi + UDP_header.Checksum], 0 |
||
295 | UDP_checksum (edi-4), (edi-8) ; FIXME: IPv4 packet could have options.. |
||
379 | serge | 296 | |
3589 | Serge | 297 | DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_output: sending with device %x\n", ebx |
3555 | Serge | 298 | call [ebx + NET_DEVICE.transmit] |
299 | test eax, eax |
||
300 | jnz @f |
||
3725 | Serge | 301 | call NET_ptr_to_num4 |
302 | inc [UDP_PACKETS_TX + edi] |
||
3555 | Serge | 303 | @@: |
379 | serge | 304 | |
3555 | Serge | 305 | ret |
379 | serge | 306 | |
3555 | Serge | 307 | .fail: |
3589 | Serge | 308 | DEBUGF DEBUG_NETWORK_ERROR, "UDP_output: failed\n" |
3555 | Serge | 309 | add esp, 4+4+8 |
310 | or eax, -1 |
||
311 | ret |
||
379 | serge | 312 | |
313 | |||
314 | |||
3555 | Serge | 315 | ;--------------------------------------------------------------------------- |
316 | ; |
||
317 | ; UDP_API |
||
318 | ; |
||
319 | ; This function is called by system function 75 |
||
320 | ; |
||
321 | ; IN: subfunction number in bl |
||
322 | ; device number in bh |
||
323 | ; ecx, edx, .. depends on subfunction |
||
324 | ; |
||
325 | ; OUT: |
||
326 | ; |
||
327 | ;--------------------------------------------------------------------------- |
||
1 | ha | 328 | |
3555 | Serge | 329 | align 4 |
330 | UDP_api: |
||
379 | serge | 331 | |
3555 | Serge | 332 | movzx eax, bh |
333 | shl eax, 2 |
||
379 | serge | 334 | |
3555 | Serge | 335 | test bl, bl |
336 | jz .packets_tx ; 0 |
||
337 | dec bl |
||
338 | jz .packets_rx ; 1 |
||
379 | serge | 339 | |
3555 | Serge | 340 | .error: |
341 | mov eax, -1 |
||
342 | ret |
||
379 | serge | 343 | |
3555 | Serge | 344 | .packets_tx: |
345 | mov eax, [UDP_PACKETS_TX + eax] |
||
346 | ret |
||
379 | serge | 347 | |
3555 | Serge | 348 | .packets_rx: |
349 | mov eax, [UDP_PACKETS_RX + eax] |
||
350 | ret |