Subversion Repositories Kolibri OS

Rev

Rev 837 | 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: 914 $
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
;***************************************************************************
914 serge 74
 
75
proc udp_rx stdcall
1 ha 76
    push    eax
379 serge 77
 
1 ha 78
    ; First validate the header & checksum. Discard buffer if error
379 serge 79
 
1 ha 80
    ; Look for a socket where
81
    ; IP Packet UDP Destination Port = local Port
82
    ; IP Packet SA = Remote IP
379 serge 83
 
914 serge 84
	mov	ax, [edx + 20 + UDP_PACKET.DestinationPort]   ; get the local port from
1 ha 85
                                  ; the IP packet's UDP header
379 serge 86
 
914 serge 87
	mov	ebx, net_sockets
88
 
89
  .next_socket:
90
	mov	ebx, [ebx + SOCKET.NextPtr]
91
	or	ebx, ebx
92
	jz	.exit					; No match, so exit
93
	cmp	[ebx + SOCKET.LocalPort], ax		; ax will hold the 'wrong' value,
1 ha 94
                                    ; but the comparision is correct
914 serge 95
	jne	.next_socket				; Return back if no match
379 serge 96
 
1 ha 97
    ; For dhcp, we must allow any remote server to respond.
98
    ; I will accept the first incoming response to be the one
99
    ; I bind to, if the socket is opened with a destination IP address of
100
    ; 255.255.255.255
914 serge 101
	cmp	[ebx + SOCKET.RemoteIP], 0xffffffff
102
	je	@f
379 serge 103
 
914 serge 104
	mov	eax, [edx + IP_PACKET.SourceAddress]	; get the Source address from the IP packet
105
	cmp	[ebx + SOCKET.RemoteIP], ebx
106
	jne	.exit		   ; Quit if the source IP is not valid
1 ha 107
 
914 serge 108
    @@: ; OK - we have a valid UDP packet for this socket.
1 ha 109
    ; First, update the sockets remote port number with the incoming msg
110
    ; - it will have changed
111
    ; from the original ( 69 normally ) to allow further connects
914 serge 112
	mov	ax, [edx + 20 + UDP_PACKET.SourcePort]		; get the UDP source port
1 ha 113
                                     ; ( was 69, now new )
914 serge 114
	mov	[ebx + SOCKET.RemotePort], ax
379 serge 115
 
1 ha 116
    ; Now, copy data to socket. We have socket address as [eax + sockets].
117
    ; We have IP packet in edx
379 serge 118
 
1 ha 119
    ; get # of bytes in ecx
914 serge 120
	movzx	ecx, [edx + IP_PACKET.TotalLength]	; total length of IP packet. Subtract
121
	xchg	cl, ch					; 20 + 8 gives data length
1 ha 122
    sub     ecx, 28
379 serge 123
 
914 serge 124
	mov	eax, [ebx + SOCKET.rxDataCount] 	; get # of bytes already in buffer
125
	add	[ebx + SOCKET.rxDataCount], ecx 	; increment the count of bytes in buffer
379 serge 126
 
914 serge 127
	; ecx has count, edx points to data
379 serge 128
 
1 ha 129
    add     edx, 28        ; edx now points to the data
914 serge 130
	lea	edi, [ebx + eax + SOCKETHEADERSIZE]
1 ha 131
    mov     esi, edx
379 serge 132
 
1 ha 133
    cld
134
    rep     movsb          ; copy the data across
379 serge 135
 
1 ha 136
    ; flag an event to the application
914 serge 137
	mov	eax, [ebx + SOCKET.PID] 		; get socket owner PID
138
	mov	ecx, 1
139
	mov	esi, TASK_DATA + TASKDATA.pid
379 serge 140
 
914 serge 141
  .next_pid:
142
	cmp	[esi], eax
143
	je	.found_pid
1 ha 144
    inc     ecx
914 serge 145
	add	esi, 0x20
146
	cmp	ecx, [TASK_COUNT]
147
	jbe	.next_pid
379 serge 148
 
914 serge 149
	jmp	.exit
1 ha 150
 
914 serge 151
  .found_pid:
152
	shl	ecx, 8
153
	or	[ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK ; stack event
1 ha 154
 
914 serge 155
	mov	[check_idle_semaphore], 200
156
 
157
  .exit:
1 ha 158
    pop     eax
159
    call    freeBuff    ; Discard the packet
379 serge 160
    ret
914 serge 161
endp