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