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