Subversion Repositories Kolibri OS

Rev

Rev 431 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
431 serge 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
3
;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;                                                              ;;
7
;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
8
;; Distributed under terms of the GNU General Public License    ;;
9
;;                                                              ;;
10
;;                                                              ;;
11
;;  UDP.INC                                                     ;;
12
;;                                                              ;;
13
;;  UDP Processes for Menuet OS  TCP/IP stack                   ;;
14
;;                                                              ;;
15
;;  Version 0.3  29 August 2002                                 ;;
16
;;                                                              ;;
17
;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net            ;;
18
;;                                                              ;;
19
;;  See file COPYING for details                                ;;
20
;;                                                              ;;
21
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 serge 22
 
593 mikedld 23
$Revision: 593 $
379 serge 24
 
593 mikedld 25
 
1 ha 26
;*******************************************************************
27
;   Interface
28
;
29
;       udp_rx      Handles received IP packets with the UDP protocol
379 serge 30
;
1 ha 31
;*******************************************************************
261 hidnplayr 32
 
33
 
34
;
35
;   UDP Payload ( Data field in IP datagram )
36
;
37
;    0                   1                   2                   3
38
;    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
39
;
40
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41
;   |       Source Port             |      Destination Port         |
42
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43
;   | Length ( UDP Header + Data )  |           Checksum            |
44
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45
;   |       UDP Data                                                |
46
;   +-+-+-..........                                               -+
47
;
48
 
49
struc UDP_PACKET
50
{  .SourcePort       dw  ?  ;+00
51
   .DestinationPort  dw  ?  ;+02
52
   .Length           dw  ?  ;+04 - Length of (UDP Header + Data)
53
   .Checksum         dw  ?  ;+06
54
   .Data             db  ?  ;+08
55
}
56
 
57
virtual at 0
58
  UDP_PACKET UDP_PACKET
59
end virtual
60
 
61
 
1 ha 62
;***************************************************************************
63
;   Function
261 hidnplayr 64
;      udp_rx  [by Johnny_B]
1 ha 65
;
66
;   Description
67
;       UDP protocol handler
68
;       This is a kernel function, called by ip_rx
69
;       IP buffer address given in edx
261 hidnplayr 70
;          IP buffer number in eax
1 ha 71
;          Free up (or re-use) IP buffer when finished
72
;
73
;***************************************************************************
74
udp_rx:
75
    push    eax
379 serge 76
 
1 ha 77
    ; First validate the header & checksum. Discard buffer if error
379 serge 78
 
1 ha 79
    ; Look for a socket where
80
    ; IP Packet UDP Destination Port = local Port
81
    ; IP Packet SA = Remote IP
379 serge 82
 
1 ha 83
    movzx   ebx, word [edx + 22]   ; get the local port from
84
                                  ; the IP packet's UDP header
85
    mov     eax, SOCKETBUFFSIZE * NUM_SOCKETS
86
    mov     ecx, NUM_SOCKETS
379 serge 87
 
1 ha 88
fs1:
89
    sub     eax, SOCKETBUFFSIZE
90
    cmp     [eax + sockets + 12], bx ; bx will hold the 'wrong' value,
91
                                    ; but the comparision is correct
92
    loopnz  fs1                     ; Return back if no match
93
    jz      fs_done
379 serge 94
 
1 ha 95
    ; No match, so exit
96
    jmp     udprx_001
379 serge 97
 
1 ha 98
fs_done:
99
    ; For dhcp, we must allow any remote server to respond.
100
    ; I will accept the first incoming response to be the one
101
    ; I bind to, if the socket is opened with a destination IP address of
102
    ; 255.255.255.255
103
    mov     ebx, [eax + sockets + 16]
104
    cmp     ebx, 0xffffffff
379 serge 105
    je      udprx_002
106
 
1 ha 107
    mov     ebx, [edx + 12]    ; get the Source address from the IP packet
108
    cmp     [eax + sockets + 16], ebx
109
    jne     udprx_001          ; Quit if the source IP is not valid
110
 
379 serge 111
udprx_002:
1 ha 112
    ; OK - we have a valid UDP packet for this socket.
113
    ; First, update the sockets remote port number with the incoming msg
114
    ; - it will have changed
115
    ; from the original ( 69 normally ) to allow further connects
116
    movzx   ebx, word [edx + 20]      ; get the UDP source port
117
                                     ; ( was 69, now new )
118
    mov     [eax + sockets + 20], bx
379 serge 119
 
1 ha 120
    ; Now, copy data to socket. We have socket address as [eax + sockets].
121
    ; We have IP packet in edx
379 serge 122
 
1 ha 123
    ; get # of bytes in ecx
124
    movzx   ecx, byte [edx + 3]  ; total length of IP packet. Subtract
125
    mov     ch, byte [edx + 2]   ; 20 + 8 gives data length
126
    sub     ecx, 28
379 serge 127
 
1 ha 128
    mov     ebx, eax
129
    add     ebx, sockets         ; ebx = address of actual socket
379 serge 130
 
1 ha 131
    mov     eax, [ebx+ 4]       ; get socket owner PID
132
    push    eax
379 serge 133
 
1 ha 134
    mov     eax, [ebx + 24]      ; get # of bytes already in buffer
135
    add     [ebx + 24], ecx      ; increment the count of bytes in buffer
379 serge 136
 
1 ha 137
    ; point to the location to store the data
138
    add     ebx, eax
379 serge 139
    add     ebx, SOCKETHEADERSIZE
1 ha 140
 
141
    ; ebx = location for first byte, ecx has count,
142
    ; edx points to data
379 serge 143
 
1 ha 144
    add     edx, 28        ; edx now points to the data
145
    mov     edi, ebx
146
    mov     esi, edx
379 serge 147
 
1 ha 148
    cld
149
    rep     movsb          ; copy the data across
379 serge 150
 
1 ha 151
    ; flag an event to the application
152
    pop     eax
153
    mov     ecx,1
379 serge 154
    mov     esi,TASK_DATA+TASKDATA.pid
155
 
1 ha 156
newsearch:
157
    cmp     [esi],eax
158
    je      foundPID
159
    inc     ecx
160
    add     esi,0x20
379 serge 161
    cmp     ecx,[TASK_COUNT]
1 ha 162
    jbe     newsearch
379 serge 163
 
164
foundPID:
1 ha 165
    shl     ecx,8
380 serge 166
    or      dword [ecx+SLOT_BASE+APPDATA.event_mask],dword 10000000b ; stack event
1 ha 167
 
379 serge 168
    mov     [check_idle_semaphore],200
1 ha 169
 
170
udprx_001:
171
    pop     eax
172
    call    freeBuff    ; Discard the packet
379 serge 173
    ret