Subversion Repositories Kolibri OS

Rev

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

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