| 1 | /* |
| 2 | * Copyright (C) 1997-2005 by Objective Systems, Inc. |
| 3 | * |
| 4 | * This software is furnished under an open source license and may be |
| 5 | * used and copied only in accordance with the terms of this license. |
| 6 | * The text of the license may generally be found in the root |
| 7 | * directory of this installation in the COPYING file. It |
| 8 | * can also be viewed online at the following URL: |
| 9 | * |
| 10 | * http://www.obj-sys.com/open/license.html |
| 11 | * |
| 12 | * Any redistributions of this file including modified versions must |
| 13 | * maintain this copyright notice. |
| 14 | * |
| 15 | *****************************************************************************/ |
| 16 | |
| 17 | /** |
| 18 | * @file ooSocket.h |
| 19 | * Common runtime constants, data structure definitions, and run-time functions |
| 20 | * to support the sockets' operations. |
| 21 | */ |
| 22 | #ifndef _OOSOCKET_H_ |
| 23 | #define _OOSOCKET_H_ |
| 24 | |
| 25 | #include "asterisk/poll-compat.h" |
| 26 | #include "asterisk/compiler.h" |
| 27 | |
| 28 | #ifdef _WIN32_WCE |
| 29 | #include <winsock.h> |
| 30 | #elif defined(_WIN32) || defined(_WIN64) |
| 31 | #include <sys/types.h> |
| 32 | #define INCL_WINSOCK_API_TYPEDEFS 1 |
| 33 | #define INCL_WINSOCK_API_PROTOTYPES 0 |
| 34 | #include <winsock2.h> |
| 35 | #else |
| 36 | #include <sys/types.h> |
| 37 | #include "sys/time.h" |
| 38 | #include <sys/socket.h> |
| 39 | #include <sys/ioctl.h> |
| 40 | #include <netinet/in.h> |
| 41 | #include <netdb.h> |
| 42 | #include <unistd.h> |
| 43 | #include <arpa/inet.h> |
| 44 | #include <net/if.h> |
| 45 | #endif |
| 46 | |
| 47 | #include "ooasn1.h" |
| 48 | |
| 49 | #ifdef __cplusplus |
| 50 | extern "C" { |
| 51 | #endif |
| 52 | |
| 53 | #ifndef EXTERN |
| 54 | #ifdef MAKE_DLL |
| 55 | #define EXTERN __declspec(dllexport) |
| 56 | #elif defined (USEASN1DLL) |
| 57 | #define EXTERN __declspec(dllimport) |
| 58 | #else |
| 59 | #define EXTERN |
| 60 | #endif /* MAKE_DLL */ |
| 61 | #endif /* EXTERN */ |
| 62 | |
| 63 | /** |
| 64 | * @defgroup sockets Socket Layer |
| 65 | * @{ |
| 66 | */ |
| 67 | #if defined (_WIN64) |
| 68 | typedef unsigned __int64 OOSOCKET; /**< Socket's handle */ |
| 69 | #elif defined (_WIN32) |
| 70 | typedef unsigned int OOSOCKET; /**< Socket's handle */ |
| 71 | #else |
| 72 | typedef int OOSOCKET; /**< Socket's handle */ |
| 73 | #endif |
| 74 | |
| 75 | #define OOSOCKET_INVALID ((OOSOCKET)-1) |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * The IP address represented as unsigned long value. The most significant 8 |
| 80 | * bits in this unsigned long value represent the first number of the IP |
| 81 | * address. The least significant 8 bits represent the last number of the IP |
| 82 | * address. |
| 83 | */ |
| 84 | /* typedef unsigned long OOIPADDR; */ |
| 85 | typedef struct ast_sockaddr OOIPADDR; |
| 86 | |
| 87 | #define OOIPADDR_ANY ((OOIPADDR)0) |
| 88 | #define OOIPADDR_LOCAL ((OOIPADDR)0x7f000001UL) /* 127.0.0.1 */ |
| 89 | |
| 90 | typedef struct OOInterface{ |
| 91 | char *name; |
| 92 | char *addr; |
| 93 | char *mask; |
| 94 | struct OOInterface *next; |
| 95 | }OOInterface; |
| 96 | |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * This function permits an incoming connection attempt on a socket. It |
| 101 | * extracts the first connection on the queue of pending connections on socket. |
| 102 | * It then creates a new socket and returns a handle to the new socket. The |
| 103 | * newly created socket is the socket that will handle the actual connection |
| 104 | * and has the same properties as original socket. See description of 'accept' |
| 105 | * socket function for further details. |
| 106 | * |
| 107 | * @param socket The socket's handle created by call to ::rtSocketCreate |
| 108 | * function. |
| 109 | * @param pNewSocket The pointer to variable to receive the new socket's |
| 110 | * handle. |
| 111 | * @param destAddr Optional pointer to a buffer that receives the IP |
| 112 | * address of the connecting entity. It may be NULL. |
| 113 | * @param destPort Optional pointer to a buffer that receives the port of |
| 114 | * the connecting entity. It may be NULL. |
| 115 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 116 | * negative return value is error. |
| 117 | */ |
| 118 | EXTERN int ooSocketAccept (OOSOCKET socket, OOSOCKET *pNewSocket, |
| 119 | char* destAddr, int* destPort); |
| 120 | |
| 121 | /** |
| 122 | * This function converts an IP address to its string representation. |
| 123 | * |
| 124 | * @param ipAddr The IP address to be converted. |
| 125 | * @param pbuf Pointer to the buffer to receive a string with the IP |
| 126 | * address. |
| 127 | * @param bufsize Size of the buffer. |
| 128 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 129 | * negative return value is error. |
| 130 | */ |
| 131 | EXTERN int ooSocketAddrToStr (OOIPADDR ipAddr, char* pbuf, int bufsize); |
| 132 | |
| 133 | /** |
| 134 | * This function associates a local address with a socket. It is used on an |
| 135 | * unconnected socket before subsequent calls to the ::rtSocketConnect or |
| 136 | * ::rtSocketListen functions. See description of 'bind' socket function for |
| 137 | * further details. |
| 138 | * |
| 139 | * @param socket The socket's handle created by call to ::rtSocketCreate |
| 140 | * function. |
| 141 | * @param addr The local IP address to assign to the socket. |
| 142 | * @param port The local port number to assign to the socket. |
| 143 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 144 | * negative return value is error. |
| 145 | */ |
| 146 | EXTERN int ooSocketBind (OOSOCKET socket, OOIPADDR addr, int port); |
| 147 | |
| 148 | /** |
| 149 | * This function closes an existing socket. |
| 150 | * |
| 151 | * @param socket The socket's handle created by call to ::rtSocketCreate |
| 152 | * or ::rtSocketAccept function. |
| 153 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 154 | * negative return value is error. |
| 155 | */ |
| 156 | EXTERN int ooSocketClose (OOSOCKET socket); |
| 157 | |
| 158 | /** |
| 159 | * This function establishes a connection to a specified socket. It is used to |
| 160 | * create a connection to the specified destination. When the socket call |
| 161 | * completes successfully, the socket is ready to send and receive data. See |
| 162 | * description of 'connect' socket function for further details. |
| 163 | * |
| 164 | * @param socket The socket's handle created by call to ::rtSocketCreate |
| 165 | * function. |
| 166 | * @param host The null-terminated string with the IP address in the |
| 167 | * following format: "NNN.NNN.NNN.NNN", where NNN is a |
| 168 | * number in the range (0..255). |
| 169 | * @param port The destination port to connect. |
| 170 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 171 | * negative return value is error. |
| 172 | */ |
| 173 | EXTERN int ooSocketConnect (OOSOCKET socket, const char* host, int port); |
| 174 | |
| 175 | /** |
| 176 | * This function creates a socket. The only streaming TCP/IP sockets are |
| 177 | * supported at the moment. |
| 178 | * |
| 179 | * @param psocket The pointer to the socket's handle variable to receive |
| 180 | * the handle of new socket. |
| 181 | * @param family Which family socket will created |
| 182 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 183 | * negative return value is error. |
| 184 | */ |
| 185 | EXTERN int ooSocketCreate (OOSOCKET* psocket, int family); |
| 186 | |
| 187 | /** |
| 188 | * This function creates a UDP datagram socket. |
| 189 | * |
| 190 | * @param psocket The pointer to the socket's handle variable to receive |
| 191 | * the handle of new socket. |
| 192 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 193 | * negative return value is error. |
| 194 | */ |
| 195 | EXTERN int ooSocketCreateUDP (OOSOCKET* psocket, int family); |
| 196 | |
| 197 | /** |
| 198 | * This function initiates use of sockets by an application. This function must |
| 199 | * be called first before use sockets. |
| 200 | * |
| 201 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 202 | * negative return value is error. |
| 203 | */ |
| 204 | EXTERN int ooSocketsInit (void); |
| 205 | |
| 206 | /** |
| 207 | * This function terminates use of sockets by an application. This function |
| 208 | * must be called after done with sockets. |
| 209 | * |
| 210 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 211 | * negative return value is error. |
| 212 | */ |
| 213 | EXTERN int ooSocketsCleanup (void); |
| 214 | |
| 215 | /** |
| 216 | * This function places a socket a state where it is listening for an incoming |
| 217 | * connection. To accept connections, a socket is first created with the |
| 218 | * ::rtSocketCreate function and bound to a local address with the |
| 219 | * ::rtSocketBind function, a maxConnection for incoming connections is |
| 220 | * specified with ::rtSocketListen, and then the connections are accepted with |
| 221 | * the ::rtSocketAccept function. See description of 'listen' socket function |
| 222 | * for further details. |
| 223 | * |
| 224 | * @param socket The socket's handle created by call to |
| 225 | * ::rtSocketCreate function. |
| 226 | * @param maxConnection Maximum length of the queue of pending connections. |
| 227 | * @return Completion status of operation: 0 (ASN_OK) = |
| 228 | * success, negative return value is error. |
| 229 | */ |
| 230 | EXTERN int ooSocketListen (OOSOCKET socket, int maxConnection); |
| 231 | |
| 232 | |
| 233 | /** |
| 234 | * This function is used to peek at the received data without actually removing |
| 235 | * it from the receive socket buffer. A receive call after this will get the |
| 236 | * same data from the socket. |
| 237 | * @param socket The socket's handle created by call to ::rtSocketCreate |
| 238 | * or ::rtSocketAccept function. |
| 239 | * @param pbuf Pointer to the buffer for the incoming data. |
| 240 | * @param bufsize Length of the buffer. |
| 241 | * @return If no error occurs, returns the number of bytes |
| 242 | * received. Otherwise, the negative value is error code. |
| 243 | */ |
| 244 | EXTERN int ooSocketRecvPeek |
| 245 | (OOSOCKET socket, ASN1OCTET* pbuf, ASN1UINT bufsize); |
| 246 | |
| 247 | /** |
| 248 | * This function receives data from a connected socket. It is used to read |
| 249 | * incoming data on sockets. The socket must be connected before calling this |
| 250 | * function. See description of 'recv' socket function for further details. |
| 251 | * |
| 252 | * @param socket The socket's handle created by call to ::rtSocketCreate |
| 253 | * or ::rtSocketAccept function. |
| 254 | * @param pbuf Pointer to the buffer for the incoming data. |
| 255 | * @param bufsize Length of the buffer. |
| 256 | * @return If no error occurs, returns the number of bytes |
| 257 | * received. Otherwise, the negative value is error code. |
| 258 | */ |
| 259 | EXTERN int ooSocketRecv (OOSOCKET socket, ASN1OCTET* pbuf, |
| 260 | ASN1UINT bufsize); |
| 261 | |
| 262 | /** |
| 263 | * This function receives data from a connected/unconnected socket. It is used |
| 264 | * to read incoming data on sockets. It populates the remotehost and |
| 265 | * remoteport parameters with information of remote host. See description of |
| 266 | * 'recvfrom' socket function for further details. |
| 267 | * |
| 268 | * @param socket The socket's handle created by call to ooSocketCreate |
| 269 | * |
| 270 | * @param pbuf Pointer to the buffer for the incoming data. |
| 271 | * @param bufsize Length of the buffer. |
| 272 | * @param remotehost Pointer to a buffer in which remote ip address |
| 273 | * will be returned. |
| 274 | * @param hostBufLen Length of the buffer passed for remote ip address. |
| 275 | * @param remoteport Pointer to an int in which remote port number |
| 276 | * will be returned. |
| 277 | * |
| 278 | * @return If no error occurs, returns the number of bytes |
| 279 | * received. Otherwise, negative value. |
| 280 | */ |
| 281 | EXTERN int ooSocketRecvFrom (OOSOCKET socket, ASN1OCTET* pbuf, |
| 282 | ASN1UINT bufsize, char * remotehost, |
| 283 | ASN1UINT hostBufLen, int * remoteport); |
| 284 | /** |
| 285 | * This function sends data on a connected socket. It is used to write outgoing |
| 286 | * data on a connected socket. See description of 'send' socket function for |
| 287 | * further details. |
| 288 | * |
| 289 | * @param socket The socket's handle created by call to ::rtSocketCreate |
| 290 | * or ::rtSocketAccept function. |
| 291 | * @param pdata Buffer containing the data to be transmitted. |
| 292 | * @param size Length of the data in pdata. |
| 293 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 294 | * negative return value is error. |
| 295 | */ |
| 296 | EXTERN int ooSocketSend (OOSOCKET socket, const ASN1OCTET* pdata, |
| 297 | ASN1UINT size); |
| 298 | |
| 299 | /** |
| 300 | * This function sends data on a connected or unconnected socket. See |
| 301 | * description of 'sendto' socket function for further details. |
| 302 | * |
| 303 | * @param socket The socket's handle created by call to ::rtSocketCreate |
| 304 | * or ::rtSocketAccept function. |
| 305 | * @param pdata Buffer containing the data to be transmitted. |
| 306 | * @param size Length of the data in pdata. |
| 307 | * @param remotehost Remote host ip address to which data has to |
| 308 | * be sent. |
| 309 | * @param remoteport Remote port ip address to which data has to |
| 310 | * be sent. |
| 311 | * |
| 312 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 313 | * negative return value is error. |
| 314 | */ |
| 315 | EXTERN int ooSocketSendTo(OOSOCKET socket, const ASN1OCTET* pdata, |
| 316 | ASN1UINT size, const char* remotehost, |
| 317 | int remoteport); |
| 318 | |
| 319 | /** |
| 320 | * This function is used for synchronous monitoring of multiple sockets. |
| 321 | * For more information refer to documentation of "select" system call. |
| 322 | * |
| 323 | * @param nfds The highest numbered descriptor to be monitored |
| 324 | * plus one. |
| 325 | * @param readfds The descriptors listed in readfds will be watched for |
| 326 | * whether read would block on them. |
| 327 | * @param writefds The descriptors listed in writefds will be watched for |
| 328 | * whether write would block on them. |
| 329 | * @param exceptfds The descriptors listed in exceptfds will be watched for |
| 330 | * exceptions. |
| 331 | * @param timeout Upper bound on amout of time elapsed before select |
| 332 | * returns. |
| 333 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 334 | * negative return value is error. |
| 335 | */ |
| 336 | EXTERN int ooSocketSelect(int nfds, fd_set *readfds, fd_set *writefds, |
| 337 | fd_set *exceptfds, struct timeval * timeout) attribute_deprecated; |
| 338 | |
| 339 | EXTERN int ooSocketPoll(struct pollfd *pfds, int nfds, int timeout); |
| 340 | |
| 341 | EXTERN int ooPDRead(struct pollfd *pfds, int nfds, int fd); |
| 342 | EXTERN int ooPDWrite(struct pollfd *pfds, int nfds, int fd); |
| 343 | |
| 344 | /** |
| 345 | * This function converts the string with IP address to a double word |
| 346 | * representation. The converted address may be used with the ::rtSocketBind |
| 347 | * function. |
| 348 | * |
| 349 | * @param pIPAddrStr The null-terminated string with the IP address in the |
| 350 | * following format: "NNN.NNN.NNN.NNN", where NNN is a |
| 351 | * number in the range (0..255). |
| 352 | * @param pIPAddr Pointer to the converted IP address. |
| 353 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 354 | * negative return value is error. |
| 355 | */ |
| 356 | /* EXTERN int ooSocketStrToAddr (const char* pIPAddrStr, OOIPADDR* pIPAddr); */ |
| 357 | |
| 358 | /** |
| 359 | * This function converts an internet dotted ip address to network address |
| 360 | * |
| 361 | * @param inetIp The null-terminated string with the IP address in the |
| 362 | * following format: "NNN.NNN.NNN.NNN", where NNN is a |
| 363 | * number in the range (0..255). |
| 364 | * @param netIp Buffer in which the converted address will be returned. |
| 365 | |
| 366 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 367 | * negative return value is error. |
| 368 | */ |
| 369 | /* EXTERN int ooSocketConvertIpToNwAddr(char *inetIp, unsigned char *netIp); */ |
| 370 | |
| 371 | /** |
| 372 | * This function retrives the IP address of the local host. |
| 373 | * |
| 374 | * @param pIPAddrs Pointer to a char buffer in which local IP address will be |
| 375 | * returned. |
| 376 | * @return Completion status of operation: 0 (ASN_OK) = success, |
| 377 | * negative return value is error. |
| 378 | */ |
| 379 | EXTERN int ooGetLocalIPAddress(char * pIPAddrs); |
| 380 | |
| 381 | |
| 382 | EXTERN int ooSocketGetSockName(OOSOCKET socket, struct sockaddr_in *name, |
| 383 | socklen_t *size); |
| 384 | |
| 385 | |
| 386 | EXTERN long ooSocketHTONL(long val); |
| 387 | |
| 388 | EXTERN short ooSocketHTONS(short val); |
| 389 | |
| 390 | /** |
| 391 | * This function is used to retrieve the ip and port number used by the socket |
| 392 | * passed as parameter. It internally uses getsockname system call for this |
| 393 | * purpose. |
| 394 | * @param socket Socket for which ip and port has to be determined. |
| 395 | * @param ip Buffer in which ip address will be returned. |
| 396 | * @param len Length of the ip address buffer. |
| 397 | * @param port Pointer to integer in which port number will be returned. |
| 398 | * @param family Pointer to integer in which IP family (4 or 6) will be returned |
| 399 | * |
| 400 | * @return ASN_OK, on success; -ve on failed. |
| 401 | */ |
| 402 | EXTERN int ooSocketGetIpAndPort(OOSOCKET socket, char *ip, int len, int *port, int *family); |
| 403 | |
| 404 | |
| 405 | EXTERN int ooSocketGetInterfaceList(OOCTXT *pctxt, OOInterface **ifList); |
| 406 | /** |
| 407 | * @} |
| 408 | */ |
| 409 | #ifdef __cplusplus |
| 410 | } |
| 411 | #endif |
| 412 | |
| 413 | #endif /* _OOSOCKET_H_ */ |