Rev 1299 | 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: 1318 $ |
1159 | hidnplayr | 17 | |
1249 | hidnplayr | 18 | struct SOCKET_head |
1159 | hidnplayr | 19 | .PrevPtr dd ? ; pointer to previous socket in list |
20 | .NextPtr dd ? ; pointer to next 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 |
||
1159 | hidnplayr | 79 | |
1249 | hidnplayr | 80 | .end: |
81 | ends |
||
82 | |||
83 | struct ICMP_SOCKET |
||
84 | |||
85 | .Identifier dw ? ; |
||
86 | |||
87 | .end: |
||
88 | |||
89 | ends |
||
90 | |||
91 | struct IPC_SOCKET |
||
92 | |||
93 | .ConnectedTo dd ? ; Socket number of other socket this one is connected to |
||
94 | |||
95 | .end: |
||
96 | |||
97 | ends |
||
98 | |||
1274 | hidnplayr | 99 | struct socket_queue_entry |
100 | .data_ptr dd ? |
||
101 | .data_size dd ? |
||
102 | .offset dd ? |
||
103 | .size: |
||
104 | ends |
||
105 | |||
1249 | hidnplayr | 106 | MAX_backlog equ 20 ; backlog for stream sockets |
107 | SOCKETBUFFSIZE equ 4096 ; in bytes |
||
108 | SOCKET_QUEUE_SIZE equ 10 ; maximum number ofincoming packets queued for 1 socket |
||
1257 | hidnplayr | 109 | 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 | 110 | |
1159 | hidnplayr | 111 | uglobal |
112 | net_sockets rd 2 |
||
113 | last_UDP_port dw ? ; These values give the number of the last used ephemeral port |
||
114 | last_TCP_port dw ? ; |
||
115 | endg |
||
116 | |||
117 | |||
1257 | hidnplayr | 118 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 119 | ; |
120 | ; SOCKET_init |
||
121 | ; |
||
122 | ; - |
||
123 | ; |
||
124 | ; IN: / |
||
125 | ; OUT: / |
||
126 | ; |
||
1257 | hidnplayr | 127 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 128 | align 4 |
129 | socket_init: |
||
130 | |||
131 | mov [net_sockets], 0 |
||
132 | mov [net_sockets + 4], 0 |
||
133 | |||
134 | mov [last_UDP_port], MIN_EPHEMERAL_PORT |
||
135 | mov [last_TCP_port], MIN_EPHEMERAL_PORT |
||
136 | |||
137 | ret |
||
138 | |||
139 | |||
1257 | hidnplayr | 140 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 141 | ; |
142 | ; Socket API (function 74) |
||
143 | ; |
||
1257 | hidnplayr | 144 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 145 | align 4 |
146 | sys_socket: |
||
1254 | hidnplayr | 147 | and ebx, 0x000000FF ; should i remove this line ? |
1256 | clevermous | 148 | cmp bl , 8 ; highest possible number |
1254 | hidnplayr | 149 | jg s_error |
150 | lea ebx, [.table + 4*ebx] |
||
151 | jmp dword [ebx] |
||
1159 | hidnplayr | 152 | |
1254 | hidnplayr | 153 | .table: |
154 | dd socket_open ; 0 |
||
155 | dd socket_close ; 1 |
||
156 | dd socket_bind ; 2 |
||
157 | dd socket_listen ; 3 |
||
158 | dd socket_connect ; 4 |
||
159 | dd socket_accept ; 5 |
||
160 | dd socket_send ; 6 |
||
161 | dd socket_recv ; 7 |
||
1257 | hidnplayr | 162 | dd socket_get_opt ; 8 |
1254 | hidnplayr | 163 | ; dd socket_set_opt ; 9 |
1159 | hidnplayr | 164 | |
1254 | hidnplayr | 165 | |
1185 | hidnplayr | 166 | s_error: |
1159 | hidnplayr | 167 | mov dword [esp+32],-1 |
168 | |||
169 | ret |
||
170 | |||
171 | |||
1257 | hidnplayr | 172 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 173 | ; |
174 | ; SOCKET_open |
||
175 | ; |
||
176 | ; |
||
177 | ; IN: domain in ecx |
||
178 | ; type in edx |
||
1196 | hidnplayr | 179 | ; protocol in esi |
1159 | hidnplayr | 180 | ; OUT: eax is socket num, -1 on error |
181 | ; |
||
1257 | hidnplayr | 182 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 183 | align 4 |
1159 | hidnplayr | 184 | socket_open: |
185 | |||
186 | DEBUGF 1,"socket_open: domain: %u, type: %u",ecx, edx |
||
187 | |||
188 | call net_socket_alloc |
||
189 | or eax, eax |
||
1185 | hidnplayr | 190 | jz s_error |
1159 | hidnplayr | 191 | |
1249 | hidnplayr | 192 | mov [eax + SOCKET_head.Domain], ecx |
193 | mov [eax + SOCKET_head.Type], edx |
||
194 | mov [eax + SOCKET_head.Protocol], esi |
||
1159 | hidnplayr | 195 | |
1318 | hidnplayr | 196 | cmp ecx, AF_INET4 |
197 | je .af_inet4 |
||
198 | |||
199 | jmp .done |
||
200 | |||
201 | |||
202 | .af_inet4: |
||
203 | |||
204 | cmp edx, IP_PROTO_TCP |
||
205 | je .tcp |
||
206 | |||
207 | jmp .done |
||
208 | |||
209 | .tcp: |
||
210 | |||
211 | mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_CLOSED |
||
212 | |||
213 | pseudo_random ebx |
||
214 | mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.ISS], ebx |
||
215 | mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT], ebx |
||
216 | |||
217 | .done: |
||
1159 | hidnplayr | 218 | stdcall net_socket_addr_to_num, eax |