Subversion Repositories Kolibri OS

Rev

Rev 909 | 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: 915 $
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
907 mikedld 50
{  .SourcePort	     dw  ?  ;+00
261 hidnplayr 51
   .DestinationPort  dw  ?  ;+02
907 mikedld 52
   .Length	     dw  ?  ;+04 - Length of (UDP Header + Data)
53
   .Checksum	     dw  ?  ;+06
54
   .Data	     db  ?  ;+08
261 hidnplayr 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
;***************************************************************************
379 serge 74
 
907 mikedld 75
proc udp_rx stdcall
76
	push	eax
379 serge 77
 
907 mikedld 78
	; First validate the header & checksum. Discard buffer if error
379 serge 79
 
907 mikedld 80
	; Look for a socket where
81
	; IP Packet UDP Destination Port = local Port
82
	; IP Packet SA = Remote IP
379 serge 83
 
907 mikedld 84
	mov	ax, [edx + 20 + UDP_PACKET.DestinationPort]   ; get the local port from
85
				       ; the IP packet's UDP header
379 serge 86
 
907 mikedld 87
	mov	ebx, net_sockets
379 serge 88
 
907 mikedld 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,
94
							; but the comparision is correct
95
	jne	.next_socket				; Return back if no match
379 serge 96
 
907 mikedld 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
101
	cmp	[ebx + SOCKET.RemoteIP], 0xffffffff
102
	je	@f
1 ha 103
 
907 mikedld 104
	mov	eax, [edx + IP_PACKET.SourceAddress]	; get the Source address from the IP packet
915 mikedld 105
	cmp	[ebx + SOCKET.RemoteIP], eax
907 mikedld 106
	jne	.exit		   ; Quit if the source IP is not valid
379 serge 107
 
907 mikedld 108
    @@: ; OK - we have a valid UDP packet for this socket.
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
112
	mov	ax, [edx + 20 + UDP_PACKET.SourcePort]		; get the UDP source port
113
								; ( was 69, now new )
114
	mov	[ebx + SOCKET.RemotePort], ax
379 serge 115
 
907 mikedld 116
	; Now, copy data to socket. We have socket address as [eax + sockets].
117
	; We have IP packet in edx
379 serge 118
 
907 mikedld 119
	; get # of bytes in ecx
120
	movzx	ecx, [edx + IP_PACKET.TotalLength]	; total length of IP packet. Subtract
121
	xchg	cl, ch					; 20 + 8 gives data length
122
	sub	ecx, 28
379 serge 123
 
907 mikedld 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
 
907 mikedld 127
	; ecx has count, edx points to data
379 serge 128
 
907 mikedld 129
	add	edx, 28        ; edx now points to the data
130
	lea	edi, [ebx + eax + SOCKETHEADERSIZE]
131
	mov	esi, edx
1 ha 132
 
907 mikedld 133
	cld
134
	rep	movsb	       ; copy the data across
379 serge 135
 
907 mikedld 136
	; flag an event to the application
137
	mov	eax, [ebx + SOCKET.PID] 		; get socket owner PID
138
	mov	ecx, 1
139
	mov	esi, TASK_DATA + TASKDATA.pid
379 serge 140
 
907 mikedld 141
  .next_pid:
142
	cmp	[esi], eax
143
	je	.found_pid
144
	inc	ecx
145
	add	esi, 0x20
146
	cmp	ecx, [TASK_COUNT]
147
	jbe	.next_pid
379 serge 148
 
907 mikedld 149
	jmp	.exit
379 serge 150
 
907 mikedld 151
  .found_pid:
152
	shl	ecx, 8
153
	or	[ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK ; stack event
379 serge 154
 
907 mikedld 155
	mov	[check_idle_semaphore], 200
1 ha 156
 
907 mikedld 157
  .exit:
158
	pop	eax
159
	call	freeBuff    ; Discard the packet
160
	ret
161
endp