1 /******************************************************************************
2 *
3 * Copyright (C) 2004-2008, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 /*-----------------------------------------------------------------------------
15 * Id: socket L "Socket"
16 *
17 * Summary: Sockets and common internet functions. You must call
18 #a(inet_init) function before using this library. For using this
19 library, it is
20 required to specify the file internet.g (from lib\socket
21 subfolder) with include command. #srcg[
22 |include : $"...\gentee\lib\socket\internet.g"]
23 *
24 * List: *Common internet functions,inet_close,inet_error,inet_init,
25 inet_proxy,inet_proxyenable,inetnotify_func,
26 *Socket methods,socket_close,socket_connect,socket_isproxy,
27 socket_recv,socket_send,socket_urlconnect,
28 *URL strings,str_iencoding,str_ihead,str_ihttpinfo,str_iurl,
29 *#lng/types#,thttpinfo,tinetnotify,tsocket
30 *
31 -----------------------------------------------------------------------------*/
32
33 define
34 {
35 AF_UNIX = 1
36 AF_INET = 2
37
38 IPPROTO_IP = 0 /* dummy for IP */
39 IPPROTO_ICMP = 1 /* control message protocol */
40 IPPROTO_IGMP = 2 /* internet group management protocol */
41 IPPROTO_GGP = 3 /* gateway^2 (deprecated) */
42 IPPROTO_TCP = 6 /* tcp */
43 IPPROTO_PUP = 12 /* pup */
44 IPPROTO_UDP = 17 /* user datagram protocol */
45 IPPROTO_IDP = 22 /* xns idp */
46 IPPROTO_ND = 77 /* UNOFFICIAL net disk proto */
47 IPPROTO_RAW = 255 /* raw IP packet */
48
49 SOCK_STREAM = 1 /* stream socket */
50 SOCK_DGRAM = 2 /* datagram socket */
51 SOCK_RAW = 3 /* raw-protocol interface */
52 SOCK_RDM = 4 /* reliably-delivered message */
53 SOCK_SEQPACKET = 5 /* sequenced packet stream */
54
55 WSADESCRIPTION_LEN = 257 //256
56 WSASYS_STATUS_LEN = 129 //128
57 INVALID_SOCKET = 0xFFFFFFFF
58 SOCKET_ERROR = 0xFFFFFFFF
59 }
60
61 define <export>
62 {
63 /*-----------------------------------------------------------------------------
64 * Id: ineterr D
65 *
66 * Summary: Library error codes.
67 *
68 -----------------------------------------------------------------------------*/
69 ERRINET_DLLVERSION = 0x0001 // Unsupported version of ws2_32.dll.
70 ERRINET_HTTPDATA // Not HTTP data is received.
71 ERRINET_USERBREAK // The process is interrupted by the user.
72 ERRINET_OPENFILE // Cannot open the file.
73 ERRINET_WRITEFILE // Cannot write the file.
74 ERRINET_READFILE // Cannot read the file.
75 ERRFTP_RESPONSE // The wrong response of the server.
76 ERRFTP_QUIT // The wrong QUIT response of the server.
77 ERRFTP_BADUSER // The bad user name.
78 ERRFTP_BADPSW // The wrong password.
79 ERRFTP_PORT // Error PORT.
80
81 //-----------------------------------------------------------------------------
82 // Команды - уведомления
83 // nfyfunc( uint code, nfyinfo ni )
84 /*-----------------------------------------------------------------------------
85 * Id: inetmsg D
86 *
87 * Summary: Library notify codes.
88 *
89 -----------------------------------------------------------------------------*/
90 NFYINET_ERROR = 0x0001 // An error occurred. The code of the error can be /
91 // got with the help of the #a(inet_error) function.
92 NFYINET_CONNECT // Server connection.
93 NFYINET_SEND // Sending a request.
94 NFYINET_POST // Sending data.
95 NFYINET_HEAD // Processing the header. ni.param points to /
96 // #a( thttpinfo ).
97 NFYINET_REDIRECT // Request redirection. ni.sparam contains /
98 // the new URL.
99 NFYINET_GET // Data is received. ni.param contains the total /
100 // size of all data.
101 NFYINET_PUT // Data is sent. ni.param contains the total /
102 // size of all data.
103 NFYINET_END // The connection is terminated.
104 NFYFTP_RESPONSE // Response of the FTP server. The field /
105 // ni.head contains it.
106 NFYFTP_SENDCMD // Sending a command to the FTP server. The field /
107 // ni.head contains it.
108 NFYFTP_NOTPASV // Passive mode with the FTP server is unavailable.
109
110 /*-----------------------------------------------------------------------------
111 * Id: httpflag D
112 *
113 * Summary: HTTP flags for http_get.
114 *
115 -----------------------------------------------------------------------------*/
116 HTTPF_REDIRECT = 0x0001 // If redirection is used, download from the /
117 // new address.
118 HTTPF_STR = 0x0010 // Add 0 to databuf after data is received. /
119 // Use this flag if databuf is a string.
120 HTTPF_CONTINUE = 0x0100 // If the file already exists, resume /
121 // downloading it. It is valid for #a(http_getfile).
122 HTTPF_SETTIME = 0x0200 // Set the same time for the file as it is on /
123 // the server. It is valid for #a(http_getfile).
124 //-----------------------------------------------------------------------------
125 HTTPF_FILE = 0x1000 // databuf contains the filename.
126 }
127
128 define
129 {
130 INET_HTTP = 0
131 INET_FTP
132 }
133
134 type WSAData {
135 ushort wVersion
136 ushort wHighVersion
137 reserved szDescription[ $WSADESCRIPTION_LEN ]
138 reserved szSystemStatus[ $WSASYS_STATUS_LEN ]
139 ushort iMaxSockets
140 ushort iMaxUdpDg
141 uint lpVendorInfo
142 }
143
144 type sockaddr_in
145 {
146 short sin_family
147 ushort sin_port
148 uint sin_addr
149 reserved sin_zero[8]
150 }
151
152 type sockaddr {
153 ushort sa_family
154 reserved sa_data[ 14 ]
155 }
156
157 type hostent
158 {
159 uint h_name /* official name of host */
160 uint h_aliases /* alias list */
161 short h_addrtype /* host address type */
162 short h_length /* length of address */
163 uint h_addr_list /* list of addresses */
164 }
165
166 type proxyinfo
167 {
168 str host // хост прокси
169 uint port // порт
170 uint enable // включено или нет
171 }
172
173 /*-----------------------------------------------------------------------------
174 * Id: thttpinfo T httpinfo
175 *
176 * Summary: HTTP header data. The structure is used to get data from an HTTP
177 header. Depending on the header, some fields may be empty.
178 *
179 -----------------------------------------------------------------------------*/
180
181 type httpinfo
182 {
183 uint code // Message code.
184 datetime dt // Last modified date.
185 str size // File size.
186 str location // New file location.
187 }
188
189 /*-----------------------------------------------------------------------------
190 * Id: tinetnotify T inetnotify
191 *
192 * Summary: Type for handling messages. This structure is passed to the
193 #a(inetnotify_func, message handling function) as a parameter.
194 Additional parameters take various values depending on the message
195 code.
196 *
197 -----------------------------------------------------------------------------*/
198
199 type inetnotify
200 {
201 str url // The URL address being processed.
202 str head // The header of the received packet.
203 uint param // Additional integer parameter.
204 str sparam // Additional string parameter.
205 }
206
207 //-----------------------------------------------------------------------------
208
209 global
210 {
211 uint ineterror // Код последней ошибки
212 arr proxy[2] of proxyinfo // массив proxy
213 str inet_useragent = "User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)"
214 }
215
216 import "ws2_32.dll" {
217 uint accept( uint, uint, uint )
218 uint bind( uint, uint, uint )
219 uint closesocket( uint )
220 uint connect( uint, uint, uint )
221 uint gethostbyname( uint )
222 uint getsockname( uint, uint, uint )
223 ushort htons( ushort )
224 uint inet_addr( uint )
225 uint inet_ntoa( uint )
226 uint listen( uint, uint )
227 uint ntohl( uint )
228 ushort ntohs( ushort )
229 uint recv( uint, uint, uint, uint )
230 uint send( uint, uint, uint, uint )
231 uint shutdown( uint, uint )
232 uint socket( uint, uint, uint ) -> createsocket
233 int WSACleanup()
234 uint WSAGetLastError()
235 int WSAStartup( ushort, WSAData )
236 }
237
238 /*-----------------------------------------------------------------------------
239 * Id: inet_error F1
240 *
241 * Summary: Getting an error code. The function returns the code of the last
242 error. Codes greater than 10000 are codes of errors in the
243 library #b(WinSock 2) ( ws2_32.dll ).
244 *
245 * Return: The code of the last error.$$[ineterr]
246 *
247 -----------------------------------------------------------------------------*/
248
249 func uint inet_error()
250 {
251 return ineterror
252 }
253
254 func uint inet_seterror
255 {
256 ineterror = WSAGetLastError()
257 return 0
258 }
259
260 include
261 {
262 "strinet.g"
263 "socket.g"
264 "proxy.g"
265 }
266
267 /*-----------------------------------------------------------------------------
268 * Id: inet_init F1
269 *
270 * Summary: Library initialization. This function must be called before working
271 with the library.
272 *
273 * Return: #lng/retf#
274 *
275 -----------------------------------------------------------------------------*/
276
277 func uint inet_init()
278 {
279 WSAData wsaData
280
281 ineterror = WSAStartup( 0x0202, wsaData )
282
283 if ineterror : return 0
284 if wsaData.wVersion != 0x0202
285 {
286 WSACleanup( )
287 ineterror = $ERRINET_DLLVERSION
288 return 0
289 }
290 return 1
291 }
292
293 /*-----------------------------------------------------------------------------
294 * Id: inet_close F1
295 *
296 * Summary: Closing the library. This function must be called after the work
297 with the library is finished.
298 *
299 * Return: #lng/retf#
300 *
301 -----------------------------------------------------------------------------*/
302
303 func uint inet_close()
304 {
305 ineterror = WSACleanup()
306 return !ineterror
307 }
308
309 /*-----------------------------------------------------------------------------
310 ** Id: inetnotify_func F
311 *
312 * Summary: Message handling function. When some functions are called, you can
313 specify a function for handing incoming notifications.
314 In particular, it allows you to show the working process to the
315 user. This handling function must have the following parameters.
316 *
317 * Params: code - Message code.$$[inetmsg]
318 ni - The variable of the #a(tinetnotify) type with additional data.
319 *
320 * Return: The function must return 1 to continue working and 0 otherwise.
321 *
322 * Define: func uint inetnotify_func( uint code, inetnotify ni )
323 *
324 -----------------------------------------------------------------------------*/
325
326 //-----------------------------------------------------------------------------