Subversion Repositories Kolibri OS

Rev

Rev 1197 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1197 Rev 1206
Line 12... Line 12...
12
;;          GNU GENERAL PUBLIC LICENSE                             ;;
12
;;          GNU GENERAL PUBLIC LICENSE                             ;;
13
;;             Version 2, June 1991                                ;;
13
;;             Version 2, June 1991                                ;;
14
;;                                                                 ;;
14
;;                                                                 ;;
15
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 16... Line 16...
16
 
16
 
Line 17... Line 17...
17
$Revision: 983 $
17
$Revision: 1206 $
18
 
18
 
19
 
19
 
Line 42... Line 42...
42
;
42
;
43
;  IN:  /
43
;  IN:  /
44
;  OUT: /
44
;  OUT: /
45
;
45
;
46
;-----------------------------------------------------------------
46
;-----------------------------------------------------------------
47
 
-
 
48
align 4
47
align 4
49
UDP_init:
48
UDP_init:
Line 50... Line 49...
50
 
49
 
51
	xor	eax, eax
50
	xor	eax, eax
Line 70... Line 69...
70
;       UDP Packet size in ecx
69
;       UDP Packet size in ecx
71
;       pointer to UDP Packet data in edx
70
;       pointer to UDP Packet data in edx
72
;  OUT: /
71
;  OUT: /
73
;
72
;
74
;-----------------------------------------------------------------
73
;-----------------------------------------------------------------
75
 
74
align 4
76
UDP_handler:
75
UDP_handler:
Line 77... Line 76...
77
 
76
 
78
	DEBUGF 1,"UDP_Handler\n"
77
	DEBUGF 1,"UDP_Handler\n"
Line 79... Line 78...
79
	; TODO: First validate the header & checksum. Discard buffer if error
78
	; TODO: First validate the header & checksum!
80
 
79
 
81
	; Look for a socket where
80
	; Look for a socket where
Line 82... Line 81...
82
	; IP Packet UDP Destination Port = local Port
81
	; IP Packet UDP Destination Port = local Port
83
	; IP Packet SA = Remote IP
82
	; IP Packet SA = Remote IP
84
 
83
 
85
	mov	esi, net_sockets
-
 
86
  .try_more:
84
	mov	eax, net_sockets
87
	mov	ax , [edx + UDP_Packet.DestinationPort]   ; get the local port from the IP Packet's UDP header
85
  .try_more:
88
	rol	ax , 8
86
	mov	bx , [edx + UDP_Packet.DestinationPort]   ; get the local port from the IP Packet's UDP header
89
  .next_socket:
87
  .next_socket:
90
	mov	esi, [esi + SOCKET.NextPtr]
88
	mov	eax, [eax + SOCKET.NextPtr]
91
	or	esi, esi
89
	or	eax, eax
-
 
90
	jz	.dump
-
 
91
	cmp	[eax + SOCKET.Domain], AF_INET4
92
	jz	.dump
92
	jne	.next_socket
93
	cmp	[esi + SOCKET.Type], IP_PROTO_UDP
93
	cmp	[eax + SOCKET.Type], IP_PROTO_UDP
Line 94... Line 94...
94
	jne	.next_socket
94
	jne	.next_socket
95
	cmp	[esi + SOCKET.LocalPort], ax
95
	cmp	[eax + SOCKET.LocalPort], bx
96
	jne	.next_socket
96
	jne	.next_socket
97
 
97
 
98
	; For dhcp, we must allow any remote server to respond.
98
	; For dhcp, we must allow any remote server to respond.
99
	; I will accept the first incoming response to be the one
99
	; I will accept the first incoming response to be the one
Line 100... Line 100...
100
	; I bind to, if the socket is opened with a destination IP address of
100
	; I bind to, if the socket is opened with a destination IP address of
101
	; 255.255.255.255
101
	; 255.255.255.255
102
	cmp	[esi + SOCKET.RemoteIP], 0xffffffff
102
	cmp	[eax + SOCKET.RemoteIP], 0xffffffff
103
	je	@f
103
	je	.ok1
Line 104... Line 104...
104
 
104
 
105
	mov	eax, [esp]
-
 
Line 106... Line 105...
106
	mov	eax, [eax + ETH_FRAME.Data + IPv4_Packet.SourceAddress] ; get the Source address from the IP Packet
105
	mov	ebx, [esp]
107
	cmp	[esi + SOCKET.RemoteIP], eax
-
 
Line 108... Line 106...
108
	jne	.try_more					      ; Quit if the source IP is not valid, check for more sockets with this IP/PORT combination
106
	mov	ebx, [ebx + ETH_FRAME.Data + IPv4_Packet.SourceAddress] ; get the Source address from the IP Packet
109
 
107
	cmp	[eax + SOCKET.RemoteIP], eax
Line 110... Line -...
110
    @@:
-
 
111
	DEBUGF 1,"Found valid UDP packet for socket %x\n", esi
108
	jne	.try_more					      ; Quit if the source IP is not valid, check for more sockets with this IP/PORT combination
-
 
109
 
Line 112... Line -...
112
 
-
 
113
;        sub     ecx, UDP_Packet.Data                    ; get # of bytes in ecx
110
  .ok1:
Line 114... Line 111...
114
;        mov     eax, ecx
111
 
115
 
-
 
116
	movzx	ecx, [edx + UDP_Packet.Length]
112
	mov	bx, [edx + UDP_Packet.SourcePort]		      ; Remote port must be 0, or equal to sourceport of packet
117
	xchg	cl , ch
-
 
118
 
113
 
119
;        cmp     ecx, eax                                ; If UDP packet size is bigger then IP packet told us,
114
	cmp	[eax + SOCKET.RemotePort],0
120
;        jg      .error                                  ; Something must went wrong!
-
 
121
 
-
 
122
	lea	ebx, [esi + SOCKET.lock]
-
 
123
	call	wait_mutex
-
 
124
 
-
 
125
	; OK - we have a valid UDP Packet for this socket.
115
	je	.ok2
126
	; First, update the sockets remote port number with the incoming msg
-
 
127
	; - it will have changed
-
 
128
	; from the original ( 69 normally ) to allow further connects
-
 
129
	mov	ax, [edx + UDP_Packet.SourcePort]	   ; get the UDP source port
-
 
130
	xchg	al, ah
-
 
131
	mov	[esi + SOCKET.RemotePort], ax
-
 
132
 
-
 
133
	; Now, copy data to socket. We have socket address as [eax + sockets].
-
 
134
	; We have IP Packet in edx
-
 
135
 
-
 
136
	add	edx, UDP_Packet.Data
-
 
137
	mov	eax, [esi + SOCKET.rxDataCount] 	; get # of bytes already in buffer
-
 
138
	DEBUGF 1,"bytes in socket: %u ", eax
-
 
139
	lea	edi, [ecx + eax]			; check for buffer overflow
116
 
140
	cmp	edi, SOCKETBUFFSIZE - SOCKETHEADERSIZE	;
-
 
141
	jg	.dump					;
-
 
142
	add	[esi + SOCKET.rxDataCount], ecx 	; increment the count of bytes in buffer
-
 
143
	DEBUGF 1,"adding %u bytes\n", ecx
-
 
144
 
-
 
145
	; ecx has count, edx points to data
-
 
146
 
-
 
147
	lea	edi, [esi + eax + SOCKETHEADERSIZE]
-
 
148
	push	esi
-
 
149
	push	ecx
-
 
Line 150... Line 117...
150
	mov	esi, edx
117
	cmp	[eax + SOCKET.RemotePort], bx
151
	shr	ecx, 2
-
 
152
	rep	movsd	       ; copy the data across
-
 
153
	pop	ecx
-
 
154
	and	ecx, 3
-
 
155
	rep	movsb
-
 
156
	pop	esi
-
 
157
 
-
 
158
	DEBUGF 1,"UDP socket updated\n"
-
 
159
 
-
 
160
	mov	[esi + SOCKET.lock], 0
-
 
161
 
-
 
162
	; flag an event to the application
-
 
163
	mov	eax, [esi + SOCKET.PID] 		; get socket owner PID
-
 
164
	mov	ecx, 1
-
 
165
	mov	esi, TASK_DATA + TASKDATA.pid
-
 
166
 
-
 
167
       .next_pid:
-
 
Line 168... Line 118...
168
	cmp	[esi], eax
118
	jne	.dump
Line 169... Line 119...
169
	je	.found_pid
119
 
170
	inc	ecx
120
  .ok2:
171
	add	esi, 0x20
-
 
172
	cmp	ecx, [TASK_COUNT]
121
 
173
	jbe	.next_pid
122
	DEBUGF 1,"Found valid UDP packet for socket %x\n", eax
Line 174... Line 123...
174
 
123
	lea	esi, [edx + UDP_Packet.Data]
Line 203... Line 152...
203
;
152
;
204
;-----------------------------------------------------------------
153
;-----------------------------------------------------------------
Line 205... Line 154...
205
 
154
 
Line 206... Line 155...
206
UDP_create_packet:
155
UDP_create_packet:
Line 207... Line 156...
207
 
156
 
Line 208... Line 157...
208
	DEBUGF 1,"Create UDP Packet\n"
157
	DEBUGF 1,"Create UDP Packet (size=%u)\n",ecx
209
 
158
 
Line 210... Line 159...
210
	push	edx esi
159
	push	edx esi
Line 211... Line 160...
211
 
160
 
212
	add	ecx, UDP_Packet.Data
161
	add	ecx, UDP_Packet.Data
213
	mov	di , IP_PROTO_UDP
162
	mov	di , IP_PROTO_UDP
Line 214... Line 163...
214
 
163
 
215
;       dx  = fragment id
164
;       dx  = fragment id
216
 
165
 
Line 232... Line 181...
232
	and	cx , 3
181
	and	cx , 3
233
	rep	movsb
182
	rep	movsb
234
	pop	edi
183
	pop	edi
Line 235... Line 184...
235
 
184
 
236
	pop	ecx
-
 
237
;        bswap   ecx                                             ; convert little endian - big endian
-
 
238
;        rol     ecx, 16                                         ;
185
	pop	ecx
Line 239... Line -...
239
	mov	dword [edi + UDP_Packet.SourcePort], ecx	; notice: we write both port's at once
-
 
240
 
186
	mov	dword [edi + UDP_Packet.SourcePort], ecx	; notice: we write both port's at once
Line 241... Line 187...
241
 
187
 
Line -... Line 188...
-
 
188
	mov	[edi + UDP_Packet.Checksum], 0
-
 
189
 
242
	mov	[edi + UDP_Packet.Checksum], 0
190
	; TODO: calculate checksum using Pseudo-header  (However, using a 0 as checksum shouldnt generate any errors :)
243
 
-
 
244
	; TODO: calculate checksum using Pseudo-header  (However, using a 0 as checksum shouldnt generate any errors :)
191
 
245
 
192
	inc	[UDP_PACKETS_TX]
Line 246... Line 193...
246
	push	ebx eax 		     ; TODO: make this work on other protocols besides ethernet
193
 
247
	mov	ebx,edx 		     ;
194
	push	edx eax 		     ; TODO: make this work on other protocols besides ethernet
Line -... Line 195...
-
 
195
	DEBUGF 1,"Sending UDP Packet to device %x\n", ebx      ;
-
 
196
	jmp	ETH_Sender		     ;
-
 
197
 
-
 
198
  .exit:
-
 
199
	ret
Line 248... Line 200...
248
	DEBUGF 1,"Sending UDP Packet to device %x\n", ebx      ;
200
 
249
	jmp	ETH_Sender		     ;
201
  .fail:
250
 
202
	; todo: queue the packet