1 /* sio.h */ 2 3 #ifndef _sio_h_ 4 #define _sio_h_ 1 5 6 typedef struct SelectSet { 7 fd_set fds; 8 struct timeval timeout; 9 int maxfd; 10 int numfds; 11 } SelectSet, *SelectSetPtr; 12 13 /* For SReadline */ 14 #ifndef _SReadlineInfo_ 15 #define _SReadlineInfo_ 1 16 typedef struct SReadlineInfo { 17 char *buf; /* Pointer to beginning of buffer. */ 18 char *bufPtr; /* Pointer to current position in buffer. */ 19 char *bufLim; /* Pointer to end of buffer. */ 20 size_t bufSize; /* Current size of buffer block. */ 21 size_t bufSizeMax; /* Maximum size available for buffer. */ 22 int malloc; /* If non-zero, malloc() was used for buf. */ 23 int fd; /* File descriptor to use for I/O. */ 24 int timeoutLen; /* Timeout to use, in seconds. */ 25 int requireEOLN; /* When buffer is full, continue reading and discarding until \n? */ 26 } SReadlineInfo; 27 #endif 28 29 #ifndef forever 30 # define forever for ( ; ; ) 31 #endif 32 33 /* Private decl; only for use when compiling sio code. */ 34 #ifdef HAVE_SIGSETJMP 35 # define SSetjmp(a) sigsetjmp(a, 1) 36 # define SLongjmp(a,b) siglongjmp(a, b) 37 # define Sjmp_buf sigjmp_buf 38 #else 39 # define SSetjmp(a) setjmp(a) 40 # define SLongjmp(a,b) longjmp(a, b) 41 # define Sjmp_buf jmp_buf 42 #endif 43 44 /* Parameter to SBind */ 45 #define kReUseAddrYes 1 46 #define kReUseAddrNo 0 47 48 /* Parameter to SRead/SWrite */ 49 #define kFullBufferNotRequired 00000 50 #define kFullBufferRequired 00001 51 #define kFullBufferRequiredExceptLast 00002 52 #define kNoFirstSelect 00010 53 54 /* Parameter to AddrToAddrStr */ 55 #define kUseDNSYes 1 56 #define kUseDNSNo 0 57 58 #define kTimeoutErr (-2) 59 #define kBrokenPipeErr (-3) 60 61 #define kAddrStrToAddrMiscErr (-4) 62 #define kAddrStrToAddrBadHost (-5) 63 64 #define kSNewFailed (-6) 65 #define kSBindFailed (-7) 66 #define kSListenFailed (-8) 67 68 #define kSrlBufSize 2048 69 70 #define kNoTimeLimit 0 71 72 #if 1 /* %config2% -- set by configure script -- do not modify */ 73 # ifndef NO_SIGNALS 74 # define NO_SIGNALS 1 75 # endif 76 #else 77 /* #undef NO_SIGNALS */ 78 #endif 79 80 /* Don't change the following line -- it is modified by the Configure script. */ 81 #define SAccept SAcceptS 82 83 #ifndef SAccept 84 # if defined(NO_SIGNALS) || defined(WIN32) || defined(_WINDOWS) 85 # define SAccept SAcceptS 86 # else 87 # define SAccept SAcceptA 88 # endif 89 #endif 90 91 #if !defined(ETIMEDOUT) && defined(WSAETIMEDOUT) 92 # define ETIMEDOUT WSAETIMEDOUT 93 #endif 94 95 #if !defined(EADDRNOTAVAIL) && defined(WSAEADDRNOTAVAIL) 96 # define EADDRNOTAVAIL WSAEADDRNOTAVAIL 97 #endif 98 99 #if !defined(EWOULDBLOCK) && defined(WSAEWOULDBLOCK) 100 # define EWOULDBLOCK WSAEWOULDBLOCK 101 #endif 102 103 #if !defined(EINPROGRESS) && defined(WSAEINPROGRESS) 104 # define EINPROGRESS WSAEINPROGRESS 105 #endif 106 107 #if !defined(WIN32) && !defined(_WINDOWS) && !defined(closesocket) 108 # define closesocket close 109 #endif 110 111 #if !defined(WIN32) && !defined(_WINDOWS) && !defined(ioctlsocket) 112 # define ioctlsocket ioctl 113 #endif 114 115 #if defined(WIN32) || defined(_WINDOWS) 116 # define SETERRNO errno = WSAGetLastError(); 117 # define SETWSATIMEOUTERR WSASetLastError(WSAETIMEDOUT); 118 #else 119 # define SETERRNO 120 # define SETWSATIMEOUTERR 121 #endif 122 123 typedef void (*sio_sigproc_t)(int); 124 typedef volatile sio_sigproc_t vsio_sigproc_t; 125 126 #ifdef __cplusplus 127 extern "C" { 128 #endif 129 130 #ifndef _sio_main_c 131 extern int gLibSio_Uses_Me_To_Quiet_Variable_Unused_Warnings; 132 #endif 133 134 #if (defined(__GNUC__)) && (__GNUC__ >= 2) 135 # ifndef UNUSED 136 # define UNUSED(a) a __attribute__ ((unused)) 137 # endif 138 # define LIBSIO_USE_VAR(a) 139 #else 140 # define LIBSIO_USE_VAR(a) gLibSio_Uses_Me_To_Quiet_Variable_Unused_Warnings = (a == 0) 141 # ifndef UNUSED 142 # define UNUSED(a) a 143 # endif 144 #endif 145 146 /* PRead.c */ 147 int PRead(int, char *const, size_t, int); 148 149 /* PWrite.c */ 150 int PWrite(int, const char *const, size_t); 151 152 /* SAcceptA.c */ 153 int SAcceptA(int, struct sockaddr_in *const, int); 154 155 /* SAcceptS.c */ 156 int SAcceptS(int, struct sockaddr_in *const, int); 157 158 /* SBind.c */ 159 int SBind(int, const int, const int, const int); 160 int SListen(int, int); 161 162 /* SClose.c */ 163 int SClose(int, int); 164 165 /* SConnect.c */ 166 int SConnect(int, const struct sockaddr_in *const, int); 167 168 /* SConnectByName.c */ 169 int SConnectByName(int, const char *const, const int); 170 171 /* SNew.c */ 172 int SNewStreamClient(void); 173 int SNewDatagramClient(void); 174 int SNewStreamServer(const int, const int, const int, int); 175 int SNewDatagramServer(const int, const int, const int); 176 177 /* SRead.c */ 178 int SRead(int, char *const, size_t, int, int); 179 180 /* SReadline.c */ 181 void FlushSReadlineInfo(SReadlineInfo *); 182 int InitSReadlineInfo(SReadlineInfo *, int, char *, size_t, int, int); 183 void DisposeSReadlineInfo(SReadlineInfo *); 184 int SReadline(SReadlineInfo *, char *const, size_t); 185 186 /* SRecv.c */ 187 int SRecv(int, char *const, size_t, int, int, int); 188 189 /* SRecvfrom.c */ 190 int SRecvfrom(int, char *const, size_t, int, struct sockaddr_in *const, int); 191 192 /* SRecvmsg.c */ 193 int SRecvmsg(int, void *const, int, int); 194 195 /* SSelect.c */ 196 void SelectSetInit(SelectSetPtr const, const double); 197 void SelectSetAdd(SelectSetPtr const, const int); 198 void SelectSetRemove(SelectSetPtr const, const int); 199 int SelectW(SelectSetPtr, SelectSetPtr); 200 int SelectR(SelectSetPtr, SelectSetPtr); 201 202 /* SSend.c */ 203 int SSend(int, char *, size_t, int, int); 204 205 /* SSendto.c */ 206 int SSendto(int, const char *const, size_t, int, const struct sockaddr_in *const, int); 207 int Sendto(int, const char *const, size_t, const struct sockaddr_in *const); 208 209 /* SSendtoByName.c */ 210 int SSendtoByName(int, const char *const, size_t, int, const char *const, int); 211 int SendtoByName(int, const char *const, size_t, const char *const); 212 213 /* SWait.c */ 214 int SWaitUntilReadyForReading(const int sfd, const int tlen); 215 int SWaitUntilReadyForWriting(const int sfd, const int tlen); 216 217 /* SWrite.c */ 218 int SWrite(int, const char *const, size_t, int, int); 219 220 /* SocketUtil.c */ 221 int GetSocketBufSize(int, size_t *const, size_t *const); 222 int SetSocketBufSize(int, size_t, size_t); 223 int GetSocketNagleAlgorithm(const int); 224 int SetSocketNagleAlgorithm(const int, const int); 225 int GetSocketLinger(const int, int *const); 226 int SetSocketLinger(const int, const int, const int); 227 228 /* StrAddr.c */ 229 int AddrStrToAddr(const char *const, struct sockaddr_in *const, const int); 230 char *AddrToAddrStr(char *const dst, size_t dsize, struct sockaddr_in * const saddrp, int dns, const char *fmt); 231 232 /* SError.c */ 233 const char *SError(int e); 234 235 /* main.c */ 236 void SIOHandler(int); 237 void (*SSignal(int signum, void (*handler)(int)))(int); 238 239 #ifdef __cplusplus 240 } 241 #endif 242 243 #endif /* _sio_h_ */ 244