00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "config.h"
00034
00035 #include <cstdio>
00036 #include <cerrno>
00037 #include <cstring>
00038
00039 #ifdef HAVE_UNISTD_H
00040 #include <unistd.h>
00041 #endif
00042
00043 #include <sys/types.h>
00044 #include <sys/socket.h>
00045 #include <arpa/inet.h>
00046 #ifdef HAVE_UNISTD_H
00047 #include <unistd.h>
00048 #endif
00049
00050 #include "Socket.h"
00051 #include "BESInternalError.h"
00052
00053 Socket::Socket( int socket, struct sockaddr *addr )
00054 : _socket( socket ),
00055 _connected( true ),
00056 _listening( false ),
00057 _addr_set( true )
00058 {
00059 char ip[46];
00060 unsigned int port;
00061
00062 switch (addr->sa_family) {
00063 case AF_INET:
00064 inet_ntop (AF_INET, &(((struct sockaddr_in *)addr)->sin_addr), ip, sizeof (ip));
00065 port = ntohs (((struct sockaddr_in *)addr)->sin_port);
00066 break;
00067 case AF_INET6:
00068 inet_ntop (AF_INET6, &(((struct sockaddr_in6 *)addr)->sin6_addr), ip, sizeof (ip));
00069 port = ntohs (((struct sockaddr_in6 *)addr)->sin6_port);
00070 break;
00071 default:
00072 snprintf (ip, sizeof (ip), "UNKNOWN FAMILY: %d", addr->sa_family);
00073 port = 0;
00074 break;
00075 }
00076 _port = port ;
00077 _ip = ip ;
00078 }
00079
00080 void
00081 Socket::close()
00082 {
00083 if( _connected )
00084 {
00085 ::close( _socket ) ;
00086 _socket = 0 ;
00087 _connected = false ;
00088 _listening = false ;
00089 }
00090 }
00091
00092 void
00093 Socket::send( const string &str, int start, int end )
00094 {
00095 string send_str = str.substr( start, end ) ;
00096 int bytes_written = write( _socket, send_str.c_str(), send_str.length() ) ;
00097 if( bytes_written == -1 )
00098 {
00099 string err( "socket failure, writing on stream socket" ) ;
00100 const char* error_info = strerror( errno ) ;
00101 if( error_info )
00102 err += " " + (string)error_info ;
00103 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00104 }
00105 }
00106
00107 int
00108 Socket::receive( char *inBuff, const int inSize )
00109 {
00110 int bytesRead = 0 ;
00111 if( ( bytesRead = read( _socket, inBuff, inSize ) ) < 1 )
00112 {
00113 string err( "socket failure, reading on stream socket: " ) ;
00114 const char *error_info = strerror( errno ) ;
00115 if( error_info )
00116 err += " " + (string)error_info ;
00117 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00118 }
00119
00120 return bytesRead ;
00121 }
00122
00123 void
00124 Socket::sync()
00125 {
00126 fsync( _socket ) ;
00127 }
00128
00135 void
00136 Socket::dump( ostream &strm ) const
00137 {
00138 strm << BESIndent::LMarg << "Socket::dump - ("
00139 << (void *)this << ")" << endl ;
00140 BESIndent::Indent() ;
00141 strm << BESIndent::LMarg << "socket: " << _socket << endl ;
00142 strm << BESIndent::LMarg << "is connected? " << _connected << endl ;
00143 strm << BESIndent::LMarg << "is listening? " << _listening << endl ;
00144 strm << BESIndent::LMarg << "socket address set? " << _addr_set << endl ;
00145 if( _addr_set )
00146 {
00147 strm << BESIndent::LMarg << "socket port: " << _port << endl;
00148 strm << BESIndent::LMarg << "socket ip: " << _ip << endl;
00149 }
00150 BESIndent::UnIndent() ;
00151 }
00152