1 /******************************************************************************* 2 * Copyright (c) 2009, 2014 IBM Corp. 3 * 4 * All rights reserved. This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License v1.0 6 * and Eclipse Distribution License v1.0 which accompany this distribution. 7 * 8 * The Eclipse Public License is available at 9 * http://www.eclipse.org/legal/epl-v10.html 10 * and the Eclipse Distribution License is available at 11 * http://www.eclipse.org/org/documents/edl-v10.php. 12 * 13 * Contributors: 14 * Ian Craggs - initial implementation and documentation 15 * Ian Craggs - async client updates 16 *******************************************************************************/ 17 18 #if !defined(SOCKET_H) 19 #define SOCKET_H 20 21 #include <sys/types.h> 22 23 #if defined(WIN32) || defined(WIN64) 24 #include <winsock2.h> 25 #include <ws2tcpip.h> 26 #define MAXHOSTNAMELEN 256 27 #if !defined(SSLSOCKET_H) 28 #undef EAGAIN 29 #define EAGAIN WSAEWOULDBLOCK 30 #undef EINTR 31 #define EINTR WSAEINTR 32 #undef EINPROGRESS 33 #define EINPROGRESS WSAEINPROGRESS 34 #undef EWOULDBLOCK 35 #define EWOULDBLOCK WSAEWOULDBLOCK 36 #undef ENOTCONN 37 #define ENOTCONN WSAENOTCONN 38 #undef ECONNRESET 39 #define ECONNRESET WSAECONNRESET 40 #undef ETIMEDOUT 41 #define ETIMEDOUT WAIT_TIMEOUT 42 #endif 43 #define ioctl ioctlsocket 44 #define socklen_t int 45 #else 46 #define INVALID_SOCKET SOCKET_ERROR 47 #include <sys/socket.h> 48 #if !defined(_WRS_KERNEL) 49 #include <sys/param.h> 50 #include <sys/time.h> 51 #include <sys/select.h> 52 #include <sys/uio.h> 53 #else 54 #include <selectLib.h> 55 #endif 56 #include <netinet/in.h> 57 #include <netinet/tcp.h> 58 #include <arpa/inet.h> 59 #include <netdb.h> 60 #include <stdio.h> 61 #include <unistd.h> 62 #include <errno.h> 63 #include <fcntl.h> 64 #include <unistd.h> 65 #define ULONG size_t 66 #endif 67 68 #include "mutex_type.h" /* Needed for mutex_type */ 69 70 /** socket operation completed successfully */ 71 #define TCPSOCKET_COMPLETE 0 72 #if !defined(SOCKET_ERROR) 73 /** error in socket operation */ 74 #define SOCKET_ERROR -1 75 #endif 76 /** must be the same as SOCKETBUFFER_INTERRUPTED */ 77 #define TCPSOCKET_INTERRUPTED -22 78 #define SSL_FATAL -3 79 80 #if !defined(INET6_ADDRSTRLEN) 81 #define INET6_ADDRSTRLEN 46 /** only needed for gcc/cygwin on windows */ 82 #endif 83 84 85 #if !defined(max) 86 #define max(A,B) ( (A) > (B) ? (A):(B)) 87 #endif 88 89 #include "LinkedList.h" 90 91 /*BE 92 def FD_SET 93 { 94 128 n8 "data" 95 } 96 97 def SOCKETS 98 { 99 FD_SET "rset" 100 FD_SET "rset_saved" 101 n32 dec "maxfdp1" 102 n32 ptr INTList "clientsds" 103 n32 ptr INTItem "cur_clientsds" 104 n32 ptr INTList "connect_pending" 105 n32 ptr INTList "write_pending" 106 FD_SET "pending_wset" 107 } 108 BE*/ 109 110 111 /** 112 * Structure to hold all socket data for the module 113 */ 114 typedef struct 115 { 116 fd_set rset, /**< socket read set (see select doc) */ 117 rset_saved; /**< saved socket read set */ 118 int maxfdp1; /**< max descriptor used +1 (again see select doc) */ 119 List* clientsds; /**< list of client socket descriptors */ 120 ListElement* cur_clientsds; /**< current client socket descriptor (iterator) */ 121 List* connect_pending; /**< list of sockets for which a connect is pending */ 122 List* write_pending; /**< list of sockets for which a write is pending */ 123 fd_set pending_wset; /**< socket pending write set for select */ 124 } Sockets; 125 126 127 void Socket_outInitialize(void); 128 void Socket_outTerminate(void); 129 int Socket_getReadySocket(int more_work, struct timeval *tp, mutex_type mutex); 130 int Socket_getch(int socket, char* c); 131 char *Socket_getdata(int socket, size_t bytes, size_t* actual_len); 132 int Socket_putdatas(int socket, char* buf0, size_t buf0len, int count, char** buffers, size_t* buflens, int* frees); 133 void Socket_close(int socket); 134 int Socket_new(const char* addr, size_t addr_len, int port, int* socket); 135 136 int Socket_noPendingWrites(int socket); 137 char* Socket_getpeer(int sock); 138 139 void Socket_addPendingWrite(int socket); 140 void Socket_clearPendingWrite(int socket); 141 142 typedef void Socket_writeComplete(int socket, int rc); 143 void Socket_setWriteCompleteCallback(Socket_writeComplete*); 144 145 #endif /* SOCKET_H */ 146