66
77#include < cstring>
88#include < netdb.h>
9- #include < netinet/ip.h>
109#include < sys/socket.h>
10+ #include < arpa/inet.h>
1111#include < unistd.h>
1212
1313#include " util/gcc_util.h"
@@ -21,8 +21,10 @@ ENABLE_WARNING(effc++)
2121#include " comm/Variables.h"
2222
2323using namespace comm ;
24-
25- TCPSocket::TCPSocket (int socket_fd) : _socket_fd(socket_fd) {}
24+ TCPSocket::TCPSocket (int socket_fd, sockaddr_in address)
25+ : _socket_fd(socket_fd), _source_family(AF_INET), _source(std::move(address))
26+ {
27+ }
2628
2729TCPSocket::~TCPSocket ()
2830{
@@ -66,7 +68,7 @@ std::unique_ptr< TCPSocket > TCPSocket::accept(const std::unique_ptr< TCPSocket
6668 }
6769 // MAGICK can be done here
6870
69- return std::unique_ptr< TCPSocket >(new TCPSocket (connected_socket));
71+ return std::unique_ptr< TCPSocket >(new TCPSocket (connected_socket, std::move (clnt_addr) ));
7072}
7173
7274bool TCPSocket::bind (int port)
@@ -104,12 +106,14 @@ bool TCPSocket::connect(const Address& address)
104106std::unique_ptr< TCPSocket > TCPSocket::create ()
105107{
106108 int tcp_socket = ::socket (AF_INET, SOCK_STREAM, 0 );
109+ sockaddr_in source;
110+ memset (&source, 0 , sizeof (_source));
107111
108112 if (tcp_socket < 0 ) {
109113 THROW_EXCEPTION (SocketFail);
110114 }
111115
112- return std::unique_ptr< TCPSocket >(new TCPSocket (tcp_socket));
116+ return std::unique_ptr< TCPSocket >(new TCPSocket (tcp_socket, std::move (source) ));
113117}
114118
115119bool TCPSocket::listen () { return ::listen (_socket_fd, MAX_CONN_QUEUE) == 0 ; }
@@ -127,3 +131,14 @@ bool TCPSocket::set_timeval_option(int level, int option_name, timeval value)
127131}
128132
129133void TCPSocket::shutdown () { ::shutdown (_socket_fd, SHUT_RDWR); }
134+
135+ std::string TCPSocket::print_source ()
136+ {
137+ if (_source_family == AF_UNSPEC) {
138+ return " " ;
139+ } else {
140+ return std::string (inet_ntoa (_source.sin_addr ));
141+ }
142+ }
143+
144+ short TCPSocket::source_family () { return _source_family; }
0 commit comments