Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

StaticFunctions.cpp

Go to the documentation of this file.
00001 #include "headers.h"
00002 
00003 //
00004 // Put all static functions in this file
00005 //
00006 
00007 
00008 // Locks for all non-threadsafe functions
00009 static Mutex g_gmtimeLock;
00010 static Mutex g_getHostLock;
00011 static Mutex g_inetNtoaLock;
00012 
00013 struct tm*
00014 threadsafe_gmtime(const time_t* timer) {
00015     Guard guard(&g_gmtimeLock);
00016     return gmtime(timer);
00017 } // fn threadsafe_gmtime
00018 
00019 
00025 int
00026 threadsafe_gethostbyname(struct hostent* he, const char* name) {
00027     Guard guard(&g_getHostLock);
00028     struct hostent* tmp = gethostbyname(name);
00029     if (tmp) {
00030         memcpy(he, tmp, sizeof(struct hostent));
00031         return 1;
00032     }
00033     else {
00034         return 0;
00035     }
00036 } // fn threadsafe_gethostbyname
00037 
00038 
00044 int
00045 threadsafe_gethostbyaddr(struct hostent* he, const char* addr, int len, int type) {
00046     Guard guard(&g_getHostLock);
00047     struct hostent* tmp = gethostbyaddr(addr, len, type);
00048     if (tmp) {
00049         memcpy(he, tmp, sizeof(struct hostent));
00050         return 1;
00051     }
00052     else {
00053         return 0;
00054     }
00055 } // fn threadsafe_gethostbyaddr
00056 
00057 
00058 char*
00059 threadsafe_inet_ntoa(struct in_addr in) {
00060     Guard guard(&g_inetNtoaLock);
00061     return inet_ntoa(in);
00062 } // fn threadsafe_inet_ntoa
00063 
00064 
00068 unsigned int
00069 getPortFromSocket(OS_SPEC_SOCKET_TYPE sock) {
00070     struct sockaddr_in sin;
00071         OS_SPEC_SOCKLEN sin_size = sizeof(struct sockaddr_in);
00072     getsockname(sock, (struct sockaddr *)&sin, &sin_size);
00073         return htons(sin.sin_port);
00074 } // fn getPortFromSocket
00075 
00076 
00080 char* 
00081 boolToString(bool val) {
00082         if (val) {
00083                 return "true";
00084         }
00085         else {
00086                 return "false";
00087         }
00088 }// fn boolToString
00089 
00090 
00094 bool
00095 stringToBool(string value) {
00096     if (!OS_SPEC_STRICMP(value.c_str(), "false")) {
00097         return false;
00098     }
00099     else {
00100         return true;
00101     }
00102 } // fn stringToBool
00103 
00104 
00108 char* 
00109 printWsaErrorCode() {
00110     char* retVal = "";
00111 #ifdef WIN32
00112     int error = WSAGetLastError();
00113     switch (error) {
00114     case 0:
00115         retVal = "No Error";
00116         break;
00117     case 10061: 
00118         retVal = "WSAECONNREFUSED(10061): Connection refused.  No connection could be made because the "
00119             "target machine actively refused it. This usually results from trying to connect to a service "
00120             "that is inactive on the foreign host - that is, one with no server application running. ";
00121         break;
00122     case 10022:
00123         retVal = "WSAEINVAL(10022): Invalid argument. Some invalid argument was supplied "
00124             "(for example, specifying an invalid level to the setsockopt function). In some "
00125             "instances, it also refers to the current state of the socket for instance, calling "
00126             "accept on a socket that is not listening. ";
00127         break;
00128     case 10054:
00129         retVal = "WSAECONNRESET(10054): Connection reset by peer. An existing connection "
00130             "was forcibly closed by the remote host. This normally results if the peer "
00131             "application on the remote host is suddenly stopped, the host is rebooted, "
00132             "or the remote host uses a hard close (see setsockopt for more information "
00133             "on the SO_LINGER option on the remote socket.) This error may also result "
00134             "if a connection was broken due to keep-alive activity detecting a failure "
00135             "while one or more operations are in progress. Operations that were in progress "
00136             "fail with WSAENETRESET. Subsequent operations fail with WSAECONNRESET."; 
00137         break;
00138     case 10038:
00139         retVal = "WSAENOTSOCK(10038): Socket operation on nonsocket. An operation was "
00140             "attempted on something that is not a socket. Either the socket handle "
00141             "parameter did not reference a valid socket, or for select, a member of an fd_set was not valid. ";
00142         break;
00143     case 10014:
00144         retVal = "WSAEFAULT(10014): Bad address. The system detected an invalid "
00145             "pointer address in attempting to use a pointer argument of a call. "
00146             "This error occurs if an application passes an invalid pointer value, "
00147             "or if the length of the buffer is too small. For instance, if the "
00148             "length of an argument, which is a SOCKADDR structure, is smaller than the sizeof(SOCKADDR). ";
00149         break;
00150     case 10057:
00151         retVal = "WSAENOTCONN(10057): Socket is not connected. A request to send "
00152             "or receive data was disallowed because the socket is not connected and "
00153             "(when sending on a datagram socket using sendto) no address was supplied. "
00154             "Any other type of operation might also return this error for example, "
00155             "setsockopt setting SO_KEEPALIVE if the connection has been reset. ";
00156         break;
00157     case 10049:
00158         retVal = "WSAEADDRNOTAVAIL (10049) Cannot assign requested address. "
00159             "The requested address is not valid in its context. This normally "
00160             "results from an attempt to bind to an address that is not valid for "
00161             "the local machine. This can also result from connect, sendto, WSAConnect, "
00162             "WSAJoinLeaf, or WSASendTo when the remote address or port is not "
00163             "valid for a remote machine (for example, address or port 0)."; 
00164         break;
00165         case 10004:
00166                 retVal = "WSAEINTR (10004) Interrupted function call.  A blocking operation "
00167             "was interrupted by a call to WSACancelBlockingCall";
00168                 break;
00169         case 10060:
00170                 retVal = "WSAETIMEDOUT (10060) Connection timed out. A connection attempt "
00171             "failed because the connected party did not properly respond after a "
00172             "period of time, or the established connection failed because the "
00173             "connected host has failed to respond. ";
00174                 break;
00175     case 10055:
00176         retVal = "WSAENOBUFS (10055) No buffer space available. An operation "
00177             "on a socket could not be performed because the system lacked sufficient "
00178             "buffer space or because a queue was full.";
00179         break;
00180     case 10048:
00181         retVal = "WSAEADDRINUSE  (10048) Address already in use. Typically, only one "
00182             "usage of each socket address (protocol/IP address/port) is permitted. "
00183             "This error occurs if an application attempts to bind a socket to an IP "
00184             "address/port that has already been used for an existing socket, or a "
00185             "socket that wasn't closed properly, or one that is still in the process "
00186             "of closing. For server applications that need to bind multiple sockets to "
00187             "the same port number, consider using setsockopt(SO_REUSEADDR). Client "
00188             "applications usually need not call bind at all—connect chooses an unused "
00189             "port automatically. When bind is called with a wildcard address "
00190             "(involving ADDR_ANY), a WSAEADDRINUSE error could be delayed until the "
00191             "specific address is committed. This could happen with a call to another "
00192             "function later, including connect, listen, WSAConnect, or WSAJoinLeaf. ";
00193         break;
00194     default:
00195         retVal = "WSA error not defined";
00196         debug(DEBUG_ERR, "printWsaErrorCode error not defined: %d", error);
00197     }
00198     WSASetLastError(0);
00199 #endif
00200     return retVal;
00201 } // fn printWsaErrorCode
00202 
00203 

Generated at Thu Jul 11 13:31:52 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001