Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

cabal_os_bsd.h

00001 /*
00002    CABAL PROTOCOL
00003    CAched BAlanced LAN
00004 
00005    OS specific cabal extensions
00006 
00007    $Id: cabal_os_bsd.h,v 1.5 2004/03/29 02:15:52 jonnymind Exp $
00008 ---------------------------------------------
00009    Begin      :
00010    Author     : Giancarlo Niccolai
00011 
00012    Last modified because:
00013 
00014 */
00015 
00016 #ifndef CABAL_OS_BSD_H
00017 #define CABAL_OS_BSD_H
00018 
00019 #include <netdb.h>
00020 #include <arpa/inet.h>
00021 #include <sys/types.h>
00022 #include <sys/socket.h>
00023 #include <sys/select.h>
00024 #include <sys/time.h> 
00025 #include <unistd.h>
00026 #include <fcntl.h>
00027 #include <string.h> //memcmp
00028 
00029 #include <list>
00030 
00031 #include <cabal_error.h>
00032 
00033 #define CABAL_INVALID_ADDR INADDR_ANY
00034 #define CABAL_MAX_IP_SIZE INET_ADDRSTRLEN
00035 
00036 namespace Cabal 
00037 {
00038    typedef int RawSocket;
00039    typedef struct sockaddr_in SockAddrInet;
00040    typedef struct sockaddr SockAddr;
00041    
00042    typedef u_int16_t NetPort;
00043    typedef u_int32_t NetAddr;
00044    
00046    inline bool operator <( const SockAddrInet &in1, const SockAddrInet &in2 )
00047    {
00048       return ( memcmp( &in1, &in2, sizeof(in2) ) < 0 );
00049    }
00050    
00052    inline bool operator ==( const SockAddrInet &in1, const SockAddrInet &in2 )
00053    {
00054       return ( memcmp( &in1, &in2, sizeof(in2) ) == 0 );
00055    }   
00056 }
00057 
00058 #include <cabal_netbase.h>
00059 
00060 namespace Cabal
00061 {
00062 
00063 class Net_BSD: public NetBase
00064 {
00065 public:
00066    static bool init() throw( InitError )
00067    {
00068       return true;
00069    }
00070 
00071    static bool exit() throw( DataIncompleteError )
00072    {
00073       return true;
00074    }
00075 
00077    static RawSocket createTCPSocket() { return ::socket( PF_INET, SOCK_STREAM , 0); }
00078    static RawSocket createUDPSocket() { return ::socket( PF_INET, SOCK_DGRAM , 0); }
00079 
00081    static int closeSocket( RawSocket s ) { return ::close(s); }
00082 
00083    static bool bind( const RawSocket s, const SockAddrInet &addr )
00084    {
00085       return ( ::bind( s, reinterpret_cast<const SockAddr *>( &addr ), sizeof( SockAddrInet ) ) != -1 );
00086    }
00087    
00088    static bool connect( const RawSocket s, const SockAddrInet &addr )
00089    {
00090       return ( ::connect( s, reinterpret_cast<const SockAddr *>( &addr ), sizeof( SockAddrInet ) ) != -1 );
00091    }
00092 
00094    static bool listen( const RawSocket s, const int incomingMax )
00095    {
00096       return ( ::listen( s, incomingMax ) == 0 );
00097    }
00098    
00102    static RawSocket accept( const RawSocket s, SockAddrInet *in )
00103    {
00104       socklen_t len = sizeof( SockAddrInet );
00105       return ::accept( s, reinterpret_cast<SockAddr *>(in), &len );
00106    }
00107    
00109    static void initSockAddr( SockAddrInet &attr ) { 
00110       attr.sin_family = AF_INET;
00111       attr.sin_port = 0;
00112       attr.sin_addr.s_addr = 0; 
00113    }
00114    
00115    static void initSockAddr( SockAddrInet &attr, const char *ip ) { 
00116       attr.sin_family = AF_INET;
00117       attr.sin_port = 0;
00118       inet_pton( AF_INET, ip, &attr.sin_addr );
00119    }
00120    
00121    static void initSockAddr( SockAddrInet &attr, const char *ip, const NetPort port ) {
00122       attr.sin_family = AF_INET;
00123       attr.sin_port = hostToNetPort( port );
00124       inet_pton( AF_INET, ip, &attr.sin_addr );
00125    }
00126 
00127    static void initSockAddr( SockAddrInet &attr, const NetAddr address, const NetPort port ) {
00128       attr.sin_family = AF_INET;
00129       attr.sin_port = hostToNetPort( port );
00130       attr.sin_addr.s_addr = hostToNetAddr( address );
00131    };
00132    
00133    static bool SockAddrIsZero( const SockAddrInet &addr ) { return (addr.sin_addr.s_addr == 0); }
00134    static bool SockAddrIsSame( const SockAddrInet &addr, const SockAddrInet &addr1 ) {
00135       if ( addr.sin_addr.s_addr == addr1.sin_addr.s_addr  && addr.sin_port == addr1.sin_port )
00136          return true;
00137       return false;
00138    }
00139    
00140    static void SockAddrSetPort( SockAddrInet &addr, NetPort port ) { 
00141       addr.sin_port = hostToNetPort( port ); 
00142    }
00143    static NetPort SockAddrGetPort( const SockAddrInet &addr ) { return netToHostPort( addr.sin_port ); }
00144 
00145    static bool getHostList(
00146       const std::string &name,
00147       std::string &real_name,
00148       char *real_ip,
00149       NetAddr *other_addrs, int na_count );
00150 
00151    static bool addrToString( const SockAddrInet &addr, char *buf )
00152    {
00153       return ( inet_ntop( AF_INET, &(addr.sin_addr.s_addr), buf, CABAL_MAX_IP_SIZE ) != 0);
00154    }
00155    
00156    static bool addrToString( const NetAddr addr, char *buf )
00157    {
00158       return ( inet_ntop( AF_INET, &addr, buf, CABAL_MAX_IP_SIZE ) != 0);
00159    }
00160    static bool stringToAddr( const char *source, SockAddrInet &addr )
00161    {
00162       return ( inet_pton( AF_INET, source, &addr.sin_addr ) != 0 );
00163    }
00164 
00165    static bool isAddressString( const char *source ) {
00166       SockAddrInet addr;
00167       return stringToAddr( source, addr  );
00168    }
00169 
00171    static int errorCode() { return errno; }
00173    static void resetError() { errno = 0; }
00174    
00176    static std::string errorDescription( const int osCode ) { return strerror( osCode ); }
00177 
00178 
00180    static RawSocket dup( RawSocket s )
00181    {
00182       return ::dup( s );
00183    }
00184 
00185    static bool inProgress() { return errno == EINPROGRESS; }
00186    static bool errorAgain() { return errno == EAGAIN; }
00187    static bool errorAgain( const int error) { return error == EAGAIN; }
00188    static bool errorPipe() { return errno == EPIPE; }
00189 
00190    static void setNonBlocking( RawSocket s )
00191    {
00192       int flags;
00193       flags = fcntl( s , F_GETFL );
00194       flags |= O_NONBLOCK;
00195       fcntl( s, F_SETFL, (long) flags );
00196    }
00197 
00198    static void setBlocking( RawSocket s )
00199    {
00200       int flags;
00201       flags = fcntl( s , F_GETFL );
00202       flags &= ~O_NONBLOCK;
00203       fcntl( s, F_SETFL, (long) flags );
00204    }
00205    
00206    static void setTCPDefaults( const RawSocket s )
00207    {
00208       int iOpt;
00209       setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
00210                (const char *) &iOpt , sizeof( iOpt ));
00211       setsockopt( s, SOL_SOCKET, SO_REUSEADDR,
00212                (const char *) &iOpt, sizeof( iOpt ));
00213    }
00214 
00215 
00216    static bool validSocket( const RawSocket s ) { return (s >= 0); }
00217    static void invalidate( RawSocket &s ) { s = -1; }
00218 
00219    static int getSocketOption( const RawSocket s, const int level, int &iErrval )
00220    {
00221       socklen_t iErrvalLen = sizeof( int );
00222       return getsockopt( s,
00223          SOL_SOCKET,
00224          level,
00225          reinterpret_cast<void *>( &iErrval ),
00226          &iErrvalLen
00227          );
00228    }
00229 
00230 
00231    static int send( const RawSocket s, const void *buffer, const int len, const int params=0 ) {
00232       return ::send( s, static_cast<const void *>(buffer), len, params | MSG_NOSIGNAL );
00233    }
00234 
00235    static int recv( const RawSocket s, void *buffer, const int len, const int params=0 ) {
00236       return ::recv( s, static_cast<void *>(buffer), len, params | MSG_NOSIGNAL );
00237    }
00238 
00239    static int sendto(const RawSocket s, const void *buffer, const int len, const int params,
00240             const SockAddrInet *remote, const int remsize )
00241    {
00242       return ::sendto( s, static_cast<const void *>(buffer), len, params| MSG_NOSIGNAL, 
00243          reinterpret_cast<const struct sockaddr *>(remote), remsize );
00244    }
00245 
00246    static int recvfrom(const RawSocket s, void *buffer, const int len, const int params,
00247              SockAddrInet *remote, int *remsize )
00248    {
00249       socklen_t lent = static_cast<socklen_t>(*remsize);
00250       int ret = ::recvfrom( s, static_cast<void *>(buffer), len, params| MSG_NOSIGNAL, 
00251          reinterpret_cast<struct sockaddr *>(remote), &lent );
00252       *remsize = static_cast<int>(lent);
00253       return ret;
00254    }
00255 
00256    static void shutdown( const RawSocket s ) {
00257       ::shutdown( s, SHUT_RDWR );
00258    }
00259 
00263    static void sleep ( const int msecs ) { usleep( msecs * 1000 ); }
00264       
00265    static NetPort hostToNetPort( const NetPort data ) { return htons( data ) ; }
00266    static NetAddr hostToNetAddr( const NetAddr data ) { return htonl( data ) ; } 
00267    static NetPort netToHostPort( const NetPort data ) { return ntohs( data ) ; }
00268    static NetAddr netToHostAddr( const NetAddr data ) { return ntohl( data ) ; }
00269    
00270    static MSECS timeOfDay() {
00271       struct timeval tv;
00272       gettimeofday( &tv, 0 );
00273       return (MSECS) ( (tv.tv_sec%86400) * 1000 ) + ( tv.tv_usec / 1000 );
00274    }
00275 };
00276 
00277 
00278 typedef Net_BSD Net;
00279 
00280 };
00281 
00282 // being INT, RawSocket's "<" operator is defined, and less<int> is defined.
00283 
00284 #endif

Generated on Sat Apr 10 17:41:48 2004 for Cabal by doxygen 1.3.5