Subversion Repositories Kolibri OS

Rev

Rev 4035 | Rev 4850 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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