File: AGNet.h

package info (click to toggle)
agsync 0.2-pre-9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,044 kB
  • ctags: 1,183
  • sloc: ansic: 9,979; sh: 8,390; makefile: 86
file content (379 lines) | stat: -rw-r--r-- 13,371 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Mobile Application Link.
 *
 * The Initial Developer of the Original Code is AvantGo, Inc.
 * Portions created by AvantGo, Inc. are Copyright (C) 1997-1999
 * AvantGo, Inc. All Rights Reserved.
 *
 * Contributor(s):
 */

#ifndef __AG_NET_H__
#define __AG_NET_H__

#include <AGTypes.h>

#ifdef _WIN32
#   ifdef _WIN32_WCE
#       include <windows.h>
#       include <winsock.h>
#   else /* _WIN32_WCE */
/* 
   If this causes you compilation issues, you need to 
   move the includes around so that this file is included
   BEFORE the windows.h header.
 */
#       define FD_SETSIZE 512
#       include <winsock2.h>
#   endif /* _WIN32_WCE */
    typedef struct sockaddr_in AGSockAddr;
    typedef int AGFd;
#else /* _WIN32 */
#   ifdef __palmos__
#       include <Pilot.h>
#       include <sys_socket.h>
        typedef NetSocketAddrINType AGSockAddr;
        typedef Word AGFd;
#   else /* __palmos__ */
#       if (defined(macintosh))
#           if (!defined(__palmos__)) /* Added here for maintainability */
#               include <OpenTransport.h>
#               include <OpenTptInternet.h>
                typedef int AGSockAddr;
                typedef int AGFd;
#           endif /* if (!defined(__palmos__)) */
#       else /* defined(macintosh) */
#           include <netdb.h>
#           include <sys/types.h>
#           include <sys/socket.h>
#           include <unistd.h>
#           include <netinet/in.h>
#           ifdef __sun__
#               include <sys/filio.h>
#               include <arpa/inet.h>
#           else
#               if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(_HPUX_SOURCE)
#                   include <sys/ioctl.h>
#                   include <arpa/inet.h>
#               else
#                   include <asm/ioctls.h>
#               endif /* __FreeBSD__ */
#           endif /* __sun__ */
            typedef struct sockaddr_in AGSockAddr;
            typedef int AGFd;
#       endif /* !defined(macintosh) */
#   endif /* __palmos__ */
#endif /* _WIN32 */


#define AG_NET_UNKNOWN_PROXY_ERROR   -97
#define AG_NET_PROXY_AUTH_ERROR      -98
#define AG_NET_PROXY_FORBIDDEN       -96
#define AG_NET_SOCKS_ERROR_CONNECTTO -99
#define AG_NET_SOCKS_ERROR           -100
#define AG_NET_SOCKS_BAD_ID          -101
#define AG_NET_SOCKS_COULDNT_CONNECT -102
#define AG_NET_WOULDBLOCK            -30
#define AG_NET_IOERR                 -13
#define AG_NET_MEMORYERROR           -12
#define AG_NET_ERROR_BAD_PROXYNAME   -11
#define AG_NET_ERROR_BAD_HOSTNAME    -10
#define AG_NET_ERROR_NO_SOCKET       -9
#define AG_NET_ERROR_NO_CONNECT      -8
#define AG_NET_ERROR_RECV            -7
#define AG_NET_EISCONN               -6
#define AG_NET_ERROR                 -5

struct AGSocket;
struct AGNetCtx;

typedef enum AGSocketState {
    AG_SOCKET_ERROR = 1,
    AG_SOCKET_NEW,
    AG_SOCKET_CONNECTING,
    AG_SOCKET_CONNECTED
} AGSocketState;


#ifdef __cplusplus
extern "C" {
#endif

typedef struct AGSocket *(*AGNetSocketNewFunc)(struct AGNetCtx *ctx);
/*
 * Name:        AGNetSocketNew
 * Function:    Creates a new socket. 
 * Input:       None.
 * Output:      None.
 * Return:      Pointer to socket struture. Must be free 
 *              with AGNetSocketFree
 */
typedef void (*AGNetSocketFreeFunc)(struct AGNetCtx *ctx, 
                                    struct AGSocket *soc);
/*
 * Name:        AGNetSocketFree
 * Function:    Frees memory associated with socket, also
 *              closes socket if necessary. 
 * Input:       None.
 * Output:      None.
 * Return:      None.
 */
typedef sword (*AGNetConnectFunc)(struct AGNetCtx *ctx, 
                                  struct AGSocket *soc, uint32 laddr, short port,  
                                  AGBool block);
/*
 * Name:        AGNetConnect
 * Function:    Function that connects to internet host. For
 *              a blocking call or the first time this is 
 *              laddr and port must be valid. Subsequent calls 
 *              for a non blocking 
 *              connect ignore the name and port an just 
 *              operate on the information in the AGSocket
 *              structure. 
 * Input:       laddr  - address of host to connect to. This
 *                       is typically found by calling 
 *                       AGNetGetHostAddr
 *              port   - port on host to connect to.
 *              block - if this function should block.
 * Output:      None.
 * Return:      for non blocking call will return 
 *              AG_NET_WOULDBLOCK if call would block.
 *              Any other negative number is an error.
 *              0 on sucessful connection.
 */
typedef int32 (*AGNetReadFunc)(struct AGNetCtx *ctx, struct AGSocket *soc, 
                               uint8 *buffer, int32 bytes, 
                               AGBool block);
/*
 * Name:        AGNetRead
 * Function:    Read data from socket into passed in buffer.
 * Input:       soc    - socket
 *              bytes  - bytes to read
 *              block  - if this function should block.
 * Output:      buffer - data from scoket 
 * Return:      for non blocking call will return 
 *              AG_NET_WOULDBLOCK if call would block.
 *              Any other negative number is an error.
 *              0 if no more data to read, postive numbers
 *              equal number of bytes read into buffer.
 */
typedef int32 (*AGNetSendFunc)(struct AGNetCtx *ctx, struct AGSocket *soc, 
                               const uint8 *data, int32 bytes,
                               AGBool block);
/*
 * Name:        AGNetSend
 * Function:    write data from buffer into socket.
 * Input:       soc    - socket
 *              bytes  - bytes to write
 *              block  - if this function should block.
 *              data - data to write 
 * Output:      None.
 * Return:      for non blocking call will return 
 *              AG_NET_WOULDBLOCK if call would block.
 *              Any other negative number is an error.
 *              anything else is number of bytes written.
 */
typedef sword (*AGNetCloseFunc)(struct AGNetCtx *ctx, struct AGSocket *soc);
/*
 * Name:        AGNetSocketClose
 * Function:    Close a socket
 * Input:       soc    - socket
 * Output:      None.
 * Return:      0 on success, anyting else is an error.
 */

typedef int32 (*AGNetReadProtectedFunc)(struct AGNetCtx *ctx, 
                                        struct AGSocket *soc, 
                                        uint8 *buffer, int32 offset,
                                        int32 bytes, AGBool block);
/*
 * Name:        AGNetReadProtected
 * Function:    Read data from socket into passed in protectedbuffer.
 * Input:       soc    - socket
 *              offset - offset within buffer to start writing to
 *              bytes  - bytes to read
 *              block  - if this function should block.
 * Output:      buffer - data from scoket 
 * Return:      for non blocking call will return 
 *              AG_NET_WOULDBLOCK if call would block.
 *              Any other negative number is an error.
 *              0 if no more data to read, postive numbers
 *              equal number of bytes read into buffer.
 */

typedef struct AGSocket {
    AGSocketState state;
    AGFd fd;
    uint32 addr;
    AGSockAddr saddr;
    uint8 *userData; 
#if (defined(macintosh) && !defined(__palmos__))
    AGBool bound;
    EndpointRef ep;
    TCall sndCall;
#endif /* #if (defined(macintosh) && !defined(__palmos__)) */
} AGSocket;

typedef struct AGNetCtx {
    AGNetSendFunc send;
    AGNetConnectFunc connect;
    AGNetReadFunc recv;
    AGNetCloseFunc close;
    AGNetSocketNewFunc socnew;
    AGNetSocketFreeFunc socfree;
    AGNetReadProtectedFunc recvdm;
    uint8 *userData;
#ifdef __palmos__
    Word networkLibRefNum; 
#endif
#if (defined(macintosh) && !defined(__palmos__))
    InetSvcRef inet_services;
#endif /* #if (defined(macintosh) && !defined(__palmos__)) */

} AGNetCtx;

#define AGNETSEND(ctx, a, b, c, d) ( (*(ctx)->send)((ctx), (a), (b), (c), (d)) )
#define AGNETRECV(ctx, a, b, c, d) ( (*(ctx)->recv)((ctx), (a), (b), (c), (d)) )
#define AGNETCONNECT(ctx, a, b, c, d) ( (*(ctx)->connect)((ctx), (a), (b), (c), (d)) )
#define AGNETSOCKETCLOSE(ctx, a) ((*(ctx)->close)((ctx), (a)))
#define AGNETSOCKETNEW(ctx) ((*(ctx)->socnew)(ctx)) 
#define AGNETSOCKETFREE(ctx, a) ((*(ctx)->socfree)((ctx), (a))) 
#define AGNETRECVDM(ctx, a, b, c, d, e) ( (*(ctx)->recvdm)((ctx), (a), (b), (c), (d), (e)) )

ExportFunc 
sword AGNetInit(AGNetCtx *ctx);
/*
 * Name:        AGNetInit
 * Function:    Tells OS that we are going to want to do
 *              network IO
 * Input:       None.
 * Output:      None.
 * Return:      0 on Successs or error code
 */
ExportFunc
void AGNetSetIOFuncs(AGNetCtx *ctx, AGNetSendFunc send,
                     AGNetConnectFunc connect,
                     AGNetReadFunc recv,
                     AGNetCloseFunc close,
                     AGNetSocketNewFunc socnew,
                     AGNetSocketFreeFunc socfree,
                     AGNetReadProtectedFunc recvdm);
ExportFunc 
sword AGNetClose(AGNetCtx *ctx);
/*
 * Name:        AGNetInit
 * Function:    Tells OS that we are going no longer need to
 *              do any network IO
 * Input:       None.
 * Output:      None.
 * Return:      0 on Successs or error code
 */
ExportFunc 
sword  AGSocksConnect(AGNetCtx *ctx, AGSocket *soc, uint32 saddr,
                      int16 socksPort,
                      char *destAddr, int16 destHostPort,
                      AGBool block);
/*
 * Name:        AGNetSOCKSConnect
 * Function:    Function that can tunnel through a SOCKS proxy.
 *              After the first call all other info 
 *              except that in the soc struct is ignored.
 *              After the connect is successful the
 *              standard AGNetRead/AGNetWrite can be used
 *              on soc.
 * Input:       soc    - Socket opened by AGNetSocketNew
 *              destHost - destination of data
 *              destHostPort - port of destination server
 *              totalBytesToSend - size of data to send
 *              socksServer - HTTP server to tunnel through
 *              SocksServerPort - HTTP server port
 *              destHostPort - port of destination server
 *              userid - Pass in NULL for default.
 *              block - should call block
 * Output:      None.
 * Return:      for non blocking call will return 
 *              AG_NET_WOULDBLOCK if call would block.
 *              Any other negative number is an error.
 *              0 on sucessful connection.
 */
ExportFunc 
uint32 AGNetGetHostAddr(AGNetCtx *ctx, char *name);
/*
 * Name:        AGNetGetHostAddr
 * Function:    Get the 4 byte network address of the 
 *              passed in string
 * Input:       name - string to lookup
 * Output:      None.
 * Return:      O is error else network address.
 */
ExportFunc 
int32 AGNetGets(AGNetCtx *ctx, AGSocket *soc, uint8 *buffer, int32 offset,
                int32 bytes, int32 *bytesread, AGBool block);
/*
 * Name:        AGNetGets
 * Function:    Read a line form a socket. Will read from 
 *              socket till a '\n' is reached or until
 *              it has read in the passed in buffer limit.
 * Input:       soc    - socket
 *              bytes  - size of buffer
 * Output:      buffer - data from socket
 * Return:      for non blocking call will return 
 *              AG_NET_WOULDBLOCK if call would block.
 *              Any other negative number is an error.
 *              0 if no more data to read, postive numbers
 *              equal number of bytes read into buffer.
 *              If if couldnt read a whole line and 
 *              the network returned a would block, the 
 *              amount of data read is retuned in the 
 *              bytesread var.
 */

#ifdef __palmos__
ExportFunc
int32 AGNetReadProtected(AGNetCtx *ctx, AGSocket *soc, uint8 *buffer, 
                         int32 offset, int32 bytes, AGBool block);
/*
 * Name:        AGNetReadProtected
 * Function:    Read data from socket into passed in protectedbuffer.
 * Input:       soc    - socket
 *              offset - offset within buffer to start writing to
 *              bytes  - bytes to read
 *              block  - if this function should block.
 * Output:      buffer - data from scoket 
 * Return:      for non blocking call will return 
 *              AG_NET_WOULDBLOCK if call would block.
 *              Any other negative number is an error.
 *              0 if no more data to read, postive numbers
 *              equal number of bytes read into buffer.
 */
ExportFunc

#ifdef __palmos__
sword AGNetCanDoSecure(AGNetCtx *ctx);
int32 AGNetGetCtxSize(void);
void AGNetToggle(AGNetCtx *ctx, AGBool use);
#endif
    
#endif



#ifdef __cplusplus
}
#endif


#endif /* __AG_NET_H__ */