Subversion Repositories Kolibri OS

Rev

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

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