Subversion Repositories Kolibri OS

Rev

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

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