#ifndef KeefLibSockets_H #define KeefLibSockets_H //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // libSockets.cpp/h : keef's sockets library //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~PA:keef~~ #include // for printf(), etc. #include // for open(), close(), etc. #include // for atoi() #include // for bzero() #include #include // for FIONBIO #include //extern int errno; #ifdef UNIX #include // for gethostbyaddr() and gethostbyname() #include #include // for sleep() #include // for UCB socket support #include // for UCB socket support #include // for UCB socket support enum {TRUE=1,FALSE=0}; typedef unsigned long DWORD; #else #include // to satisfy winbase.h #include // for Sleep() #include // for windows equiv. of UCB sockets #define WINSOCK_MAJVER 2 #define WINSOCK_MINVER 2 #endif #ifndef BOOL typedef int BOOL; #endif #define pszACK "OK" #define cEOBATCH 4 // control-D enum { g_iNoConnectionYet = -4321 }; extern char pszEOBATCH[2]; #define MAXLEN_WEB_URL 2000 #define MAXLEN_WEB_HOSTNAME 200 #define MAXLEN_WEB_PROTOCOL 30 enum { WP_HTTP = 80, WP_HTTPS = 433, WP_FTP = 21, WP_GOPHER = 70, WP_NEWS = 2009, WP_UNKNOWN = 0 }; typedef struct { char szWebFullURL[MAXLEN_WEB_URL+1]; // - - - - - - - - - - - - - - - - - - - - - char szWebProtocol[MAXLEN_WEB_PROTOCOL+1]; int iWebProtocol; char szWebHostname[MAXLEN_WEB_HOSTNAME+1]; char szWebRestURL[MAXLEN_WEB_URL+1]; int iPortNum; } WEBURL; #if 0 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // this API should write to some application-specific error log and // possibly present a dialog to the user, if appropriate: // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void /*global*/ ErrorLog (char* pszFormat, ...); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // this API should call IsDebugLog() and conditionally output to a log file: // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void /*global*/ DebugLog (char* pszFormat, ...); #else #define ErrorLog printf #define DebugLog if (g_fDebug) printf extern int g_fDebug; #endif // KEEF Macros: #define InAddrB1(Unsigned32BitInt) ((int)(((Unsigned32BitInt) << 24) >> 24)) #define InAddrB2(Unsigned32BitInt) ((int)(((Unsigned32BitInt) << 16) >> 24)) #define InAddrB3(Unsigned32BitInt) ((int)(((Unsigned32BitInt) << 8) >> 24)) #define InAddrB4(Unsigned32BitInt) ((int)(((Unsigned32BitInt) ) >> 24)) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // KEEF's Global Sockets API's for UNIX and Win32 (see notes in libsockets.cpp) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - BOOL ParseWebURL (char* pszFullURL, WEBURL* pWebURL); BOOL ValidateHost (struct sockaddr_in* pAddrServer, char* pszHostname, char* szCanonical=NULL, int iLenCanonical=0); int SocketOpenForListen (unsigned short iPortNum, BOOL fDebug=FALSE); int SocketWaitForNextConnection (int fdListen, int iMSecDelay=250, BOOL fDebug=FALSE); int SocketPeekForNextConnection (int fdListen, BOOL fDebug=FALSE); int SocketAcceptConnection (int fdListen, BOOL fDebug/*=FALSE*/); BOOL SocketReadLine (int fdSocket, char* szLine, int iMaxBytes); int SocketRead (int fdSocket, char* pszBuffer, int iMaxBytes, int iMinBytes); int SendCommand (struct sockaddr_in* pAddrServer, char* pszHostname, char* pszCommand); int SocketWrite (int fdSocket, char* pszBuffer); void TranslateErrno (); #ifdef WIN32 LPCTSTR BindToWinsockDLL (); #endif // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #ifdef sun4 #ifdef __cplusplus extern "C" { #endif // these API's are somehow missing from /usr/include/sys/socket.h on sun4!!! extern int accept(int, struct sockaddr *, int *); extern int bind(int, struct sockaddr *, int); extern int connect(int, struct sockaddr *, int); extern int getpeername(int, struct sockaddr *, int *); extern int getsockname(int, struct sockaddr *, int *); extern int getsockopt(int, int, int, char *, int *); extern int listen(int, int); extern int recv(int, char *, int, int); extern int recvfrom(int, char *, int, int, struct sockaddr *, int *); extern int send(int, const char *, int, int); extern int sendto(int, const char *, int, int, const struct sockaddr *, int); extern int setsockopt(int, int, int, const char *, int); extern int socket(int, int, int); extern int recvmsg(int, struct msghdr *, int); extern int sendmsg(int, const struct msghdr *, int); extern int shutdown(int, int); extern int socketpair(int, int, int, int *); #ifdef __cplusplus } #endif #endif #endif // KeefLibSockets_H