aboutsummaryrefslogtreecommitdiffstats
path: root/addons/ooh323c/src/ooSocket.h
blob: da3dcbf1c6b22e0aacb67266f342cd8edb83a647 (plain)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (C) 1997-2005 by Objective Systems, Inc.
 *
 * This software is furnished under an open source license and may be 
 * used and copied only in accordance with the terms of this license. 
 * The text of the license may generally be found in the root 
 * directory of this installation in the COPYING file.  It 
 * can also be viewed online at the following URL:
 *
 *   http://www.obj-sys.com/open/license.html
 *
 * Any redistributions of this file including modified versions must 
 * maintain this copyright notice.
 *
 *****************************************************************************/

/** 
 * @file ooSocket.h 
 * Common runtime constants, data structure definitions, and run-time functions
 * to support the sockets' operations.
 */
#ifndef _OOSOCKET_H_
#define _OOSOCKET_H_

#include "asterisk/poll-compat.h"
#include "asterisk/compiler.h"

#ifdef _WIN32_WCE
#include <winsock.h>
#elif defined(_WIN32) || defined(_WIN64)
#include <sys/types.h>
#define INCL_WINSOCK_API_TYPEDEFS   1
#define INCL_WINSOCK_API_PROTOTYPES 0
#include <winsock2.h>
#else
#include <sys/types.h>
#include "sys/time.h"
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <net/if.h>
#endif

#include "ooasn1.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifndef EXTERN
#ifdef MAKE_DLL
#define EXTERN __declspec(dllexport)
#elif defined (USEASN1DLL)
#define EXTERN __declspec(dllimport)
#else
#define EXTERN
#endif /* MAKE_DLL */
#endif /* EXTERN */

/** 
 * @defgroup sockets Socket Layer
 * @{
 */
#if defined (_WIN64)
typedef unsigned __int64 OOSOCKET; /**< Socket's handle */
#elif defined (_WIN32)
typedef unsigned int OOSOCKET; /**< Socket's handle */
#else
typedef int OOSOCKET;          /**< Socket's handle */
#endif

#define OOSOCKET_INVALID ((OOSOCKET)-1)


/** 
 * The IP address represented as unsigned long value. The most significant 8
 * bits in this unsigned long value represent the first number of the IP
 * address. The least significant 8 bits represent the last number of the IP
 * address.
 */
typedef unsigned long OOIPADDR;

#define OOIPADDR_ANY   ((OOIPADDR)0)
#define OOIPADDR_LOCAL ((OOIPADDR)0x7f000001UL) /* 127.0.0.1 */

typedef struct OOInterface{
   char *name;
   char *addr;
   char *mask;
   struct OOInterface *next;
}OOInterface;



/**
 * This function permits an incoming connection attempt on a socket. It
 * extracts the first connection on the queue of pending connections on socket.
 * It then creates a new socket and returns a handle to the new socket. The
 * newly created socket is the socket that will handle the actual connection
 * and has the same properties as original socket. See description of 'accept'
 * socket function for further details.
 *
 * @param socket       The socket's handle created by call to ::rtSocketCreate
 *                     function.
 * @param pNewSocket   The pointer to variable to receive the new socket's
 *                     handle.
 * @param destAddr     Optional pointer to a buffer that receives the IP
 *                     address of the connecting entity. It may be NULL.
 * @param destPort     Optional pointer to a buffer that receives the port of
 *                     the connecting entity. It may be NULL.
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketAccept (OOSOCKET socket, OOSOCKET *pNewSocket, 
                             OOIPADDR* destAddr, int* destPort);

/**
 * This function converts an IP address to its string representation.
 *
 * @param ipAddr       The IP address to be converted.
 * @param pbuf         Pointer to the buffer to receive a string with the IP
 *                     address.
 * @param bufsize      Size of the buffer.
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketAddrToStr (OOIPADDR ipAddr, char* pbuf, int bufsize);

/**
 * This function associates a local address with a socket. It is used on an
 * unconnected socket before subsequent calls to the ::rtSocketConnect or
 * ::rtSocketListen functions. See description of 'bind' socket function for
 * further details.
 *
 * @param socket       The socket's handle created by call to ::rtSocketCreate
 *                     function.
 * @param addr         The local IP address to assign to the socket.
 * @param port         The local port number to assign to the socket.
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketBind (OOSOCKET socket, OOIPADDR addr, int port);

/**
 * This function closes an existing socket.
 *
 * @param socket       The socket's handle created by call to ::rtSocketCreate
 *                     or ::rtSocketAccept function.
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketClose (OOSOCKET socket);

/**
 * This function establishes a connection to a specified socket. It is used to
 * create a connection to the specified destination. When the socket call
 * completes successfully, the socket is ready to send and receive data. See
 * description of 'connect' socket function for further details.
 *
 * @param socket       The socket's handle created by call to ::rtSocketCreate
 *                     function.
 * @param host         The null-terminated string with the IP address in the
 *                     following format: "NNN.NNN.NNN.NNN", where NNN is a
 *                     number in the range (0..255).
 * @param port         The destination port to connect.
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketConnect (OOSOCKET socket, const char* host, int port);

/**
 * This function creates a socket. The only streaming TCP/IP sockets are
 * supported at the moment.
 *
 * @param psocket      The pointer to the socket's handle variable to receive
 *                     the handle of new socket.
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketCreate (OOSOCKET* psocket);

/**
 * This function creates a UDP datagram socket. 
 *
 * @param psocket      The pointer to the socket's handle variable to receive
 *                     the handle of new socket.
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketCreateUDP (OOSOCKET* psocket);

/**
 * This function initiates use of sockets by an application. This function must
 * be called first before use sockets.
 *
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketsInit (void);

/**
 * This function terminates use of sockets by an application. This function 
 * must be called after done with sockets.
 *
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketsCleanup (void);

/**
 * This function places a socket a state where it is listening for an incoming
 * connection. To accept connections, a socket is first created with the
 * ::rtSocketCreate function and bound to a local address with the
 * ::rtSocketBind function, a maxConnection for incoming connections is
 * specified with ::rtSocketListen, and then the connections are accepted with
 * the ::rtSocketAccept function. See description of 'listen' socket function
 * for further details.
 *
 * @param socket        The socket's handle created by call to
 *                      ::rtSocketCreate function.
 * @param maxConnection Maximum length of the queue of pending connections.
 * @return              Completion status of operation: 0 (ASN_OK) =
 *                      success, negative return value is error.
 */
EXTERN int ooSocketListen (OOSOCKET socket, int maxConnection);


/**
 * This function is used to peek at the received data without actually removing
 * it from the receive socket buffer. A receive call after this will get the
 * same data from the socket.
 * @param socket       The socket's handle created by call to ::rtSocketCreate
 *                     or ::rtSocketAccept function.
 * @param pbuf         Pointer to the buffer for the incoming data.
 * @param bufsize      Length of the buffer.
 * @return             If no error occurs, returns the number of bytes
 *                     received. Otherwise, the negative value is error code.
 */
EXTERN int ooSocketRecvPeek
   (OOSOCKET socket, ASN1OCTET* pbuf, ASN1UINT bufsize);

/**
 * This function receives data from a connected socket. It is used to read
 * incoming data on sockets. The socket must be connected before calling this
 * function. See description of 'recv' socket function for further details.
 *
 * @param socket       The socket's handle created by call to ::rtSocketCreate
 *                     or ::rtSocketAccept function.
 * @param pbuf         Pointer to the buffer for the incoming data.
 * @param bufsize      Length of the buffer.
 * @return             If no error occurs, returns the number of bytes
 *                     received. Otherwise, the negative value is error code.
 */
EXTERN int ooSocketRecv (OOSOCKET socket, ASN1OCTET* pbuf, 
                           ASN1UINT bufsize);

/**
 * This function receives data from a connected/unconnected socket. It is used
 * to read incoming data on sockets. It populates the remotehost and 
 * remoteport parameters with information of remote host. See description of 
 * 'recvfrom' socket function for further details.
 *
 * @param socket       The socket's handle created by call to ooSocketCreate
 *                     
 * @param pbuf         Pointer to the buffer for the incoming data.
 * @param bufsize      Length of the buffer.
 * @param remotehost   Pointer to a buffer in which remote ip address
 *                     will be returned.
 * @param hostBufLen   Length of the buffer passed for remote ip address.
 * @param remoteport   Pointer to an int in which remote port number
 *                     will be returned.
 *
 * @return             If no error occurs, returns the number of bytes
 *                     received. Otherwise, negative value.
 */
EXTERN int ooSocketRecvFrom (OOSOCKET socket, ASN1OCTET* pbuf,
                             ASN1UINT bufsize, char * remotehost, 
                             ASN1UINT hostBufLen, int * remoteport);
/**
 * This function sends data on a connected socket. It is used to write outgoing
 * data on a connected socket. See description of 'send' socket function for
 * further details.
 *
 * @param socket       The socket's handle created by call to ::rtSocketCreate
 *                     or ::rtSocketAccept function.
 * @param pdata        Buffer containing the data to be transmitted.
 * @param size         Length of the data in pdata.
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketSend (OOSOCKET socket, const ASN1OCTET* pdata, 
                           ASN1UINT size);

/**
 * This function sends data on a connected or unconnected socket. See 
 * description of 'sendto' socket function for further details.
 *
 * @param socket       The socket's handle created by call to ::rtSocketCreate
 *                       or ::rtSocketAccept function.
 * @param pdata        Buffer containing the data to be transmitted.
 * @param size         Length of the data in pdata.
 * @param remotehost   Remote host ip address to which data has to 
 *                     be sent.
 * @param remoteport   Remote port ip address to which data has to 
 *                     be sent.
 *
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketSendTo(OOSOCKET socket, const ASN1OCTET* pdata, 
                            ASN1UINT size, const char* remotehost,
                            int remoteport);

/**
 * This function is used for synchronous monitoring of multiple sockets.
 * For more information refer to documnetation of "select" system call. 
 *
 * @param nfds         The highest numbered descriptor to be monitored 
 *                     plus one.
 * @param readfds      The descriptors listed in readfds will be watched for 
 *                     whether read would block on them.
 * @param writefds     The descriptors listed in writefds will be watched for
 *                     whether write would block on them.
 * @param exceptfds    The descriptors listed in exceptfds will be watched for
 *                     exceptions.
 * @param timeout      Upper bound on amout of time elapsed before select 
 *                     returns. 
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketSelect(int nfds, fd_set *readfds, fd_set *writefds, 
                            fd_set *exceptfds, struct timeval * timeout) __attribute_deprecated__;

EXTERN int ooSocketPoll(struct pollfd *pfds, int nfds, int timeout);

EXTERN int ooPDRead(struct pollfd *pfds, int nfds, int fd);
EXTERN int ooPDWrite(struct pollfd *pfds, int nfds, int fd);

/**
 * This function converts the string with IP address to a double word
 * representation. The converted address may be used with the ::rtSocketBind
 * function.
 *
 * @param pIPAddrStr   The null-terminated string with the IP address in the
 *                     following format: "NNN.NNN.NNN.NNN", where NNN is a
 *                     number in the range (0..255).
 * @param pIPAddr      Pointer to the converted IP address.
 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketStrToAddr (const char* pIPAddrStr, OOIPADDR* pIPAddr);

/**
 * This function converts an internet dotted ip address to network address
 *
 * @param inetIp       The null-terminated string with the IP address in the
 *                     following format: "NNN.NNN.NNN.NNN", where NNN is a
 *                     number in the range (0..255).
 * @param netIp        Buffer in which the converted address will be returned.

 * @return             Completion status of operation: 0 (ASN_OK) = success,
 *                     negative return value is error.
 */
EXTERN int ooSocketConvertIpToNwAddr(char *inetIp, unsigned char *netIp);

/**
 * This function retrives the IP address of the local host.
 *
 * @param pIPAddrs   Pointer to a char buffer in which local IP address will be
 *                   returned.
 * @return           Completion status of operation: 0 (ASN_OK) = success,
 *                   negative return value is error.
 */
EXTERN int ooGetLocalIPAddress(char * pIPAddrs);


EXTERN int ooSocketGetSockName(OOSOCKET socket, struct sockaddr_in *name, 
                                                      socklen_t *size);


EXTERN long ooSocketHTONL(long val);

EXTERN short ooSocketHTONS(short val);

/**
 * This function is used to retrieve the ip and port number used by the socket
 * passed as parameter. It internally uses getsockname system call for this 
 * purpose.
 * @param socket  Socket for which ip and port has to be determined.
 * @param ip      Buffer in which ip address will be returned.
 * @param len     Length of the ip address buffer.
 * @param port    Pointer to integer in which port number will be returned.
 *
 * @return        ASN_OK, on success; -ve on failed.
 */
EXTERN int ooSocketGetIpAndPort(OOSOCKET socket, char *ip, int len, int *port);


EXTERN int ooSocketGetInterfaceList(OOCTXT *pctxt, OOInterface **ifList);
/** 
 * @} 
 */
#ifdef __cplusplus
}
#endif

#endif /* _OOSOCKET_H_ */