Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1159 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
1196 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2009. All rights reserved.    ;;
1159 hidnplayr 4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  SOCKET.INC                                                     ;;
7
;;                                                                 ;;
8
;;    Written by hidnplayr@kolibrios.org                           ;;
9
;;    based on code by mike.dld                                    ;;
10
;;                                                                 ;;
11
;;          GNU GENERAL PUBLIC LICENSE                             ;;
12
;;             Version 2, June 1991                                ;;
13
;;                                                                 ;;
14
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15
 
1206 hidnplayr 16
$Revision: 1473 $
1159 hidnplayr 17
 
1249 hidnplayr 18
struct	SOCKET_head
1473 hidnplayr 19
	 .NextPtr		dd ? ; pointer to next socket in list
1159 hidnplayr 20
	 .PrevPtr		dd ? ; pointer to previous socket in list
21
	 .Number		dd ? ; socket number (unique within single process)
22
	 .PID			dd ? ; application process id
23
	 .Domain		dd ? ; INET/UNIX/..
24
	 .Type			dd ? ; RAW/UDP/TCP/...
1196 hidnplayr 25
	 .Protocol		dd ? ; ICMP/IPv4/ARP/
1249 hidnplayr 26
	 .lock			dd ? ; lock mutex
1318 hidnplayr 27
	 .errorcode		dd ?
1249 hidnplayr 28
	 .end:
29
ends
30
 
31
struct	IPv4_SOCKET
32
	 .LocalIP		dd ?
33
	 .RemoteIP		dd ?
34
	 .SequenceNumber	dd ?
35
 
36
	; todo: add options (for func 8 and 9)
37
 
38
	 .end:
39
ends
40
 
41
struct	TCP_SOCKET
42
 
43
	 .LocalPort		dw ? ; In INET byte order
44
	 .RemotePort		dw ? ; In INET byte order
45
 
46
	 .backlog		dw ? ; Backlog
1256 clevermous 47
	 .backlog_cur		dw ? ; current size of queue for un-accept-ed connections
48
	 .last_ack_number	dd ? ; used only to let application know that ACK has been received
49
					; todo: may be use SND_UNA instead
50
					; todo: may be use events which allow additional information instead
51
					; todo: may be count acknowledged bytes (at least it has obvious sense)
1274 hidnplayr 52
	 .OrigRemoteIP		dd ? ; original remote IP address (used to reset to LISTEN state)
53
	 .OrigRemotePort	dw ? ; original remote port (used to reset to LISTEN state)
1254 hidnplayr 54
	 .wndsizeTimer		dd ? ; window size timer
55
 
56
	; Transmission control block
57
	 .state 		dd ? ; TCB state
58
	 .timer 		dd ? ; TCB timer (seconds)
1318 hidnplayr 59
 
1254 hidnplayr 60
	 .ISS			dd ? ; initial send sequence number
61
	 .IRS			dd ? ; initial receive sequence number
1196 hidnplayr 62
	 .SND_UNA		dd ? ; sequence number of unack'ed sent Packets
1249 hidnplayr 63
	 .SND_NXT		dd ? ; next send sequence number to use
1196 hidnplayr 64
	 .SND_WND		dd ? ; send window
65
	 .RCV_NXT		dd ? ; next receive sequence number to use
66
	 .RCV_WND		dd ? ; receive window
67
	 .SEG_LEN		dd ? ; segment length
68
	 .SEG_WND		dd ? ; segment window
1249 hidnplayr 69
 
70
	 .flags 		db ? ; packet flags
71
 
72
	 .end:
1159 hidnplayr 73
ends
74
 
1249 hidnplayr 75
struct	UDP_SOCKET
1159 hidnplayr 76
 
1249 hidnplayr 77
	 .LocalPort		dw ? ; In INET byte order
78
	 .RemotePort		dw ? ; In INET byte order
1335 hidnplayr 79
	 .firstpacket		db ?
1159 hidnplayr 80
 
1249 hidnplayr 81
	 .end:
82
ends
83
 
84
struct	ICMP_SOCKET
85
 
86
	.Identifier		dw ? ;
87
 
88
	.end:
89
 
90
ends
91
 
92
struct	IPC_SOCKET
93
 
94
	.ConnectedTo		dd ? ; Socket number of other socket this one is connected to
95
 
96
	.end:
97
 
98
ends
99
 
1274 hidnplayr 100
struct	socket_queue_entry
101
	.data_ptr	dd ?
102
	.data_size	dd ?
103
	.offset 	dd ?
104
	.size:
105
ends
106
 
1249 hidnplayr 107
MAX_backlog		equ 20	     ; backlog for stream sockets
108
SOCKETBUFFSIZE		equ 4096     ; in bytes
109
SOCKET_QUEUE_SIZE	equ 10	     ; maximum number ofincoming packets queued for 1 socket
1257 hidnplayr 110
SOCKET_QUEUE_LOCATION	equ 2048     ; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start
1249 hidnplayr 111
 
1159 hidnplayr 112
uglobal
113
	net_sockets	rd 2
114
	last_UDP_port	dw ? ; These values give the number of the last used ephemeral port
115
	last_TCP_port	dw ? ;
116
endg
117
 
118
 
1257 hidnplayr 119
;-----------------------------------------------------------------
1159 hidnplayr 120
;
121
; SOCKET_init
122
;
123
;  -
124
;
125
;  IN:  /
126
;  OUT: /
127
;
1257 hidnplayr 128
;-----------------------------------------------------------------
1159 hidnplayr 129
align 4
130
socket_init:
131
 
132
	mov	[net_sockets], 0
133
	mov	[net_sockets + 4], 0
134
 
135
	mov	[last_UDP_port], MIN_EPHEMERAL_PORT
136
	mov	[last_TCP_port], MIN_EPHEMERAL_PORT
137
 
138
	ret
139
 
140
 
1257 hidnplayr 141
;-----------------------------------------------------------------
1159 hidnplayr 142
;
143
; Socket API (function 74)
144
;
1257 hidnplayr 145
;-----------------------------------------------------------------
1159 hidnplayr 146
align 4
147
sys_socket:
1254 hidnplayr 148
	and	ebx, 0x000000FF ; should i remove this line ?
1256 clevermous 149
	cmp	bl , 8		; highest possible number
1254 hidnplayr 150
	jg	s_error
151
	lea	ebx, [.table + 4*ebx]
152
	jmp	dword [ebx]
1159 hidnplayr 153
 
1254 hidnplayr 154
.table:
155
	dd	socket_open	; 0
156
	dd	socket_close	; 1
157
	dd	socket_bind	; 2
158
	dd	socket_listen	; 3
159
	dd	socket_connect	; 4
160
	dd	socket_accept	; 5
161
	dd	socket_send	; 6
162
	dd	socket_recv	; 7
1257 hidnplayr 163
	dd	socket_get_opt	; 8
1254 hidnplayr 164
;        dd      socket_set_opt  ; 9
1159 hidnplayr 165
 
1254 hidnplayr 166
 
1185 hidnplayr 167
s_error:
1159 hidnplayr 168
	mov	dword [esp+32],-1
169
 
170
	ret
171
 
172
 
1257 hidnplayr 173
;-----------------------------------------------------------------
1159 hidnplayr 174
;
175
; SOCKET_open
176
;
177
;
178
;  IN:  domain in ecx
179
;       type in edx
1196 hidnplayr 180
;       protocol in esi
1159 hidnplayr 181
;  OUT: eax is socket num, -1 on error
182
;
1257 hidnplayr 183
;-----------------------------------------------------------------
1206 hidnplayr 184
align 4
1159 hidnplayr 185
socket_open:
186
 
187
	DEBUGF	1,"socket_open: domain: %u, type: %u",ecx, edx
188
 
189
	call	net_socket_alloc
190
	or	eax, eax
1185 hidnplayr 191
	jz	s_error
1159 hidnplayr 192
 
1249 hidnplayr 193
	mov	[eax + SOCKET_head.Domain], ecx
194
	mov	[eax + SOCKET_head.Type], edx
195
	mov	[eax + SOCKET_head.Protocol], esi
1159 hidnplayr 196
 
1318 hidnplayr 197
	cmp	ecx, AF_INET4
198
	je	.af_inet4
199
 
200
	jmp	.done
201
 
202
 
203
  .af_inet4:
204
 
205
	cmp	edx, IP_PROTO_TCP
206
	je	.tcp
207
 
208
	jmp	.done
209
 
210
  .tcp:
211
 
212
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_CLOSED
213
 
214
	pseudo_random ebx
215
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.ISS], ebx
216
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT], ebx
217
 
218