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