Rev 425 | Rev 465 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
425 | victor | 1 | $Revision: 431 $ |
431 | serge | 2 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
3 | ;; ;; |
||
4 | ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;; |
||
5 | ;; Distributed under terms of the GNU General Public License ;; |
||
6 | ;; ;; |
||
7 | ;; SOCKET.INC ;; |
||
8 | ;; ;; |
||
9 | ;; Sockets constants, structures and functions ;; |
||
10 | ;; ;; |
||
11 | ;; Last revision: 11.11.2006 ;; |
||
12 | ;; ;; |
||
13 | ;; This file contains the following: ;; |
||
14 | ;; is_localport_unused ;; |
||
15 | ;; get_free_socket ;; |
||
16 | ;; socket_open ;; |
||
17 | ;; socket_open_tcp ;; |
||
18 | ;; socket_close ;; |
||
19 | ;; socket_close_tcp ;; |
||
20 | ;; socket_poll ;; |
||
21 | ;; socket_status ;; |
||
22 | ;; socket_read ;; |
||
23 | ;; socket_write ;; |
||
24 | ;; socket_write_tcp ;; |
||
25 | ;; ;; |
||
26 | ;; ;; |
||
27 | ;; Changes history: ;; |
||
28 | ;; 22.09.2003 - [Mike Hibbett] : mikeh@oceanfree.net ;; |
||
29 | ;; 11.11.2006 - [Johnny_B] and [smb] ;; |
||
30 | ;; ;; |
||
31 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
261 | hidnplayr | 32 | |
33 | ; |
||
34 | ; Socket Descriptor + Buffer |
||
35 | ; |
||
36 | ; 0 1 2 3 |
||
37 | ; 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 |
||
38 | ; |
||
39 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
40 | ; 0| Status ( of this buffer ) | |
||
41 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
42 | ; 4| Application Process ID | |
||
43 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
44 | ; 8| Local IP Address | |
||
45 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
46 | ; 12| Local IP Port | Unused ( set to 0 ) | |
||
47 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
48 | ; 16| Remote IP Address | |
||
49 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
50 | ; 20| Remote IP Port | Unused ( set to 0 ) | |
||
51 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
52 | ; 24| Rx Data Count INTEL format| |
||
53 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
54 | ; 28| TCB STATE INTEL format| |
||
55 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
56 | ; 32| TCB Timer (seconds) INTEL format| |
||
57 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
58 | ; 36| ISS (Inital Sequence # used by this connection ) INET format| |
||
59 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
60 | ; 40| IRS ( Inital Receive Sequence # ) INET format| |
||
61 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
62 | ; 44| SND.UNA Seq # of unack'ed sent packets INET format| |
||
63 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
64 | ; 48| SND.NXT Next send seq # to use INET format| |
||
65 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
66 | ; 52| SND.WND Send window INET format| |
||
67 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
68 | ; 56| RCV.NXT Next expected receive sequence # INET format| |
||
69 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
70 | ; 60| RCV.WND Receive window INET format| |
||
71 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
72 | ; 64| SEG.LEN Segment length INTEL format| |
||
73 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
74 | ; 68| SEG.WND Segment window INTEL format| |
||
75 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
76 | ; 72| Retransmit queue # NOW WINDOW SIZE TIMER INTEL format| |
||
77 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
379 | serge | 78 | ; 76| RX offset from |
261 | hidnplayr | 79 | ; 76| RX Data | |
80 | ; +-+-+-.......... -+ |
||
81 | |||
82 | |||
83 | ; so, define struct |
||
84 | struc SOCKET |
||
379 | serge | 85 | { .Status dd ? ;+00 - Status ( of this buffer ) |
86 | .PID dd ? ;+04 - Application Process ID |
||
323 | hidnplayr | 87 | .LocalIP dd ? ;+08 - Local IP Address |
88 | .LocalPort dw ? ;+12 - Local Port |
||
89 | .UnusedL dw ? ;+14 - may be removed in future |
||
90 | .RemoteIP dd ? ;+16 - Remote IP Address |
||
91 | .RemotePort dw ? ;+20 - Remote Port |
||
92 | .UnusedR dw ? ;+22 - may be removed in future |
||
93 | .rxDataCount dd ? ;+24 - Rx Data Count |
||
94 | .TCBState dd ? ;+28 - TCB STATE |
||
379 | serge | 95 | .TCBTimer dd ? ;+32 - TCB Timer (seconds) |
323 | hidnplayr | 96 | .ISS dd ? ;+36 - Initial Send Sequence |
97 | .IRS dd ? ;+40 - Initial Receive Sequence |
||
98 | .SND_UNA dd ? ;+44 - Sequence number of unack'ed sent packets |
||
379 | serge | 99 | .SND_NXT dd ? ;+48 - Next send sequence number to use |
323 | hidnplayr | 100 | .SND_WND dd ? ;+52 - Send window |
101 | .RCV_NXT dd ? ;+56 - Next receive sequence number to use |
||
102 | .RCV_WND dd ? ;+60 - Receive window |
||
103 | .SEG_LEN dd ? ;+64 - Segment length |
||
104 | .SEG_WND dd ? ;+68 - Segment window |
||
379 | serge | 105 | .wndsizeTimer dd ? ;+72 - Retransmit queue # NOW WINDOW SIZE TIMER |
323 | hidnplayr | 106 | .rxData dd ? ;+76 - receive data buffer here |
261 | hidnplayr | 107 | } |
108 | |||
109 | virtual at 0 |
||
110 | SOCKET SOCKET |
||
111 | end virtual |
||
112 | |||
379 | serge | 113 | ; simple macro calcing real memory address of SOCKET struct by socket's |
261 | hidnplayr | 114 | macro Index2RealAddr reg |
115 | { |
||
323 | hidnplayr | 116 | shl reg, 12 |
379 | serge | 117 | add reg, sockets |
261 | hidnplayr | 118 | } |
119 | |||
120 | ;Constants |
||
121 | ; current socket statuses |
||
323 | hidnplayr | 122 | SOCK_EMPTY equ 0 ; socket not in use |
123 | SOCK_OPEN equ 1 ; open issued, but no data sent |
||
261 | hidnplayr | 124 | |
125 | ; TCP opening modes |
||
323 | hidnplayr | 126 | SOCKET_PASSIVE equ 0 |
127 | SOCKET_ACTIVE equ 1 |
||
261 | hidnplayr | 128 | |
129 | ;*************************************************************************** |
||
130 | ; Function |
||
131 | ; is_localport_unused |
||
132 | ; |
||
133 | ; Description |
||
134 | ; scans through all the active sockets , looking to see if the |
||
135 | ; port number specified in bx is in use as a localport number. |
||
136 | ; This is useful when you want a to generate a unique local port |
||
137 | ; number. |
||
138 | ; On return, eax = 1 for free, 0 for in use |
||
139 | ; |
||
140 | ;*************************************************************************** |
||
141 | is_localport_unused: |
||
142 | mov al, bh |
||
143 | mov ah, bl |
||
144 | mov bx, ax |
||
145 | |||
146 | mov edx, SOCKETBUFFSIZE * NUM_SOCKETS |
||
147 | mov ecx, NUM_SOCKETS |
||
323 | hidnplayr | 148 | mov eax, 0 ; Assume the return value is 'in use' |
261 | hidnplayr | 149 | |
150 | ilu1: |
||
151 | sub edx, SOCKETBUFFSIZE |
||
152 | cmp [edx + sockets + SOCKET.LocalPort], bx |
||
323 | hidnplayr | 153 | loopnz ilu1 ; Return back if the socket is occupied |
261 | hidnplayr | 154 | |
323 | hidnplayr | 155 | jz ilu_exit |
156 | inc eax ; return port not in use |
||
261 | hidnplayr | 157 | |
158 | ilu_exit: |
||
159 | ret |
||
160 | |||
161 | |||
162 | |||
163 | ;*************************************************************************** |
||
164 | ; Function |
||
165 | ; get_free_socket |
||
166 | ; |
||
167 | ; Description |
||
168 | ; |
||
169 | ;*************************************************************************** |
||
170 | get_free_socket: |
||
171 | push ecx |
||
172 | mov eax, SOCKETBUFFSIZE * NUM_SOCKETS |
||
173 | mov ecx, NUM_SOCKETS |
||
174 | |||
175 | gfs1: |
||
176 | sub eax, SOCKETBUFFSIZE |
||
177 | cmp [eax + sockets + SOCKET.Status], dword SOCK_EMPTY |
||
323 | hidnplayr | 178 | loopnz gfs1 ; Return back if the socket is occupied |
261 | hidnplayr | 179 | mov eax, ecx |
180 | pop ecx |
||
323 | hidnplayr | 181 | jz gfs_exit |
261 | hidnplayr | 182 | mov eax, 0xFFFFFFFF |
183 | |||
184 | gfs_exit: |
||
185 | ret |
||
186 | |||
187 | |||
188 | ;*************************************************************************** |
||
189 | ; Function |
||
190 | ; socket_open |
||
191 | ; |
||
192 | ; Description |
||
193 | ; find a free socket |
||
194 | ; local port in ebx |
||
195 | ; remote port in ecx |
||
196 | ; remote ip in edx |
||
197 | ; return socket # in eax, -1 if none available |
||
198 | ; |
||
199 | ;*************************************************************************** |
||
200 | socket_open: |
||
201 | call get_free_socket |
||
202 | |||
203 | cmp eax, 0xFFFFFFFF |
||
323 | hidnplayr | 204 | jz so_exit |
261 | hidnplayr | 205 | |
206 | ; ax holds the socket number that is free. Get real address |
||
207 | push eax |
||
208 | Index2RealAddr eax |
||
209 | |||
210 | mov [eax + SOCKET.Status], dword SOCK_OPEN |
||
211 | |||
323 | hidnplayr | 212 | xchg bh, bl |
213 | mov [eax + SOCKET.LocalPort], bx |
||
214 | xchg ch, cl |
||
215 | mov [eax + SOCKET.RemotePort], cx |
||
261 | hidnplayr | 216 | |
217 | mov ebx, [stack_ip] |
||
379 | serge | 218 | mov [eax + SOCKET.LocalIP], ebx |
219 | mov [eax + SOCKET.RemoteIP], edx |
||
261 | hidnplayr | 220 | mov [eax + SOCKET.rxDataCount], dword 0 ; recieved data count |