From f37968c228b9454a80300f97f74f109f1024b76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Luttringer?= Date: Thu, 17 Jan 2008 03:53:01 +0000 Subject: [PATCH] General update of sll --- slc/trunk/src/sll/connection.cc | 93 ++++++++++++++++++++++++++------ slc/trunk/src/sll/connection.hh | 9 ++++ slc/trunk/src/sll/connection.hxx | 31 +++++++++-- slc/trunk/src/sll/slm.hh | 1 + sls/trunk/src/sll/connection.cc | 93 ++++++++++++++++++++++++++------ sls/trunk/src/sll/connection.hh | 9 ++++ sls/trunk/src/sll/connection.hxx | 31 +++++++++-- sls/trunk/src/sll/slm.hh | 1 + 8 files changed, 230 insertions(+), 38 deletions(-) diff --git a/slc/trunk/src/sll/connection.cc b/slc/trunk/src/sll/connection.cc index a688f02..d78aa77 100644 --- a/slc/trunk/src/sll/connection.cc +++ b/slc/trunk/src/sll/connection.cc @@ -49,16 +49,8 @@ Connection::Connection(int fd) : socket_fd_(fd), local_port_(-1), remote_port_(- pthread_mutex_init(&r_mutex_, 0); pthread_mutex_init(&w_mutex_, 0); - if (socket_fd_ >= 0) { + if (socket_fd_ >= 0) id_ = getconnid(); - try { - setlocalip_(); - setlocalport_(); - setremoteip_(); - setremoteport_(); - } - catch (...) { } - } } /** @@ -308,7 +300,7 @@ void Connection::send(const char* buf, size_t len) { disconnect_(); pthread_mutex_unlock(&r_mutex_); pthread_mutex_unlock(&c_mutex_); - pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&w_mutex_); if (errno__ == ECONNRESET || errno__ == EPIPE) throw Error(ERR_NET, "Connection reset by peer"); else @@ -319,6 +311,14 @@ void Connection::send(const char* buf, size_t len) { pthread_mutex_unlock(&w_mutex_); } +/** + * Receive raw data + */ +char *Connection::recv(size_t size) { + assert(0); + return 0; +} + /** * Receive a line */ @@ -374,6 +374,10 @@ string Connection::recvln() { assert(1); } +void Connection::flush() { + assert(0); +} + string Connection::getlocalip() { // get local ip is a conn op pthread_mutex_lock(&c_mutex_); @@ -384,12 +388,35 @@ string Connection::getlocalip() { } if (local_ip_.empty()) - setlocalip_(); + try { setlocalip_(); } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } string ip = local_ip_; pthread_mutex_unlock(&c_mutex_); return ip; } +string Connection::getlocalhostname() { + pthread_mutex_lock(&c_mutex_); + if (local_hostname_.empty()) { + char buf[256]; + if (::gethostname(buf, 256) != 0) { + pthread_mutex_unlock(&c_mutex_); + throw Error(ERR_NET, (string) "Unable to get local hostname: " + strerror(errno)); + } + try { local_hostname_ = buf; } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } + } + pthread_mutex_unlock(&c_mutex_); + + return local_hostname_; +} + int Connection::getlocalport() { // get local port is a conn op pthread_mutex_lock(&c_mutex_); @@ -400,14 +427,19 @@ int Connection::getlocalport() { } if (local_port_ == -1) - setlocalport_(); + try { setlocalport_(); } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } + int port = local_port_; pthread_mutex_unlock(&c_mutex_); return port; } string Connection::getremoteip() { - // get remote ip is a conn op + // get remote hostname is a conn op pthread_mutex_lock(&c_mutex_); if (socket_fd_ < 0) { @@ -416,12 +448,38 @@ string Connection::getremoteip() { } if (remote_ip_.empty()) - setremoteip_(); + try { setremoteip_(); } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } + string ip = remote_ip_; pthread_mutex_unlock(&c_mutex_); return ip; } +string Connection::getremotehostname() { + // get remote ip is a conn op + pthread_mutex_lock(&c_mutex_); + + if (socket_fd_ < 0) { + pthread_mutex_unlock(&c_mutex_); + throw Error(ERR_NET, "No connection established but trying to get remote ip"); + } + + if (remote_hostname_.empty()) + try {setremotehostname_(); } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } + + string hostname = remote_hostname_; + pthread_mutex_unlock(&c_mutex_); + return hostname; +} + int Connection::getremoteport() { // get remote port is a conn op pthread_mutex_lock(&c_mutex_); @@ -432,7 +490,12 @@ int Connection::getremoteport() { } if (remote_port_ == -1) - setremoteport_(); + try {setremoteport_(); } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } + int port = remote_port_; pthread_mutex_unlock(&c_mutex_); return port; diff --git a/slc/trunk/src/sll/connection.hh b/slc/trunk/src/sll/connection.hh index 2c89e9b..89bffbc 100644 --- a/slc/trunk/src/sll/connection.hh +++ b/slc/trunk/src/sll/connection.hh @@ -45,13 +45,19 @@ public: inline void sendln(const string &data); // recv methods + char *recv(size_t size); string recvln(); + // buffer methods + void flush(); + // info methods string getlocalip(); + string getlocalhostname(); int getlocalport(); string getremoteip(); + string getremotehostname(); int getremoteport(); unsigned long int getid(); @@ -66,6 +72,7 @@ protected: inline void setlocalip_(); inline void setlocalport_(); inline void setremoteip_(); + inline void setremotehostname_(); inline void setremoteport_(); // Protected datas @@ -77,7 +84,9 @@ protected: int remote_port_; string local_ip_; + string local_hostname_; string remote_ip_; + string remote_hostname_; unsigned long int id_; diff --git a/slc/trunk/src/sll/connection.hxx b/slc/trunk/src/sll/connection.hxx index 3440140..3a606fb 100644 --- a/slc/trunk/src/sll/connection.hxx +++ b/slc/trunk/src/sll/connection.hxx @@ -21,6 +21,7 @@ #include #include #include +#include #include /******************************************************************************* @@ -99,7 +100,7 @@ void Connection::setlocalip_() { socklen_t len = sizeof addr; if (::getsockname(socket_fd_, (struct sockaddr*) &addr, &len) != 0) - throw Error(ERR_NET, (string) "Unable to set local ip: " + strerror(errno)); + throw Error(ERR_NET, (string) "Unable to get local ip: " + strerror(errno)); local_ip_ = inet_ntoa(addr.sin_addr); } @@ -112,7 +113,7 @@ void Connection::setlocalport_() { socklen_t len = sizeof addr; if (::getsockname(socket_fd_, (struct sockaddr*) &addr, &len) != 0) - throw Error(ERR_NET, (string) "Unable to set local port: " + strerror(errno)); + throw Error(ERR_NET, (string) "Unable to get local port: " + strerror(errno)); local_port_ = ntohs(addr.sin_port); } @@ -124,11 +125,33 @@ void Connection::setremoteip_() { struct sockaddr_in addr; socklen_t len = sizeof addr; + // get socket if (::getpeername(socket_fd_, (struct sockaddr*) &addr, &len) != 0) - throw Error(ERR_NET, (string) "Unable to set remote ip: " + strerror(errno)); + throw Error(ERR_NET, (string) "Unable to get remote ip: " + strerror(errno)); remote_ip_ = inet_ntoa(addr.sin_addr); } +/** + * Set remote hostname + * No mutex used ! + */ +void Connection::setremotehostname_() { + struct sockaddr_in addr; + socklen_t len = sizeof addr; + + // get sockaddr_in + if (::getpeername(socket_fd_, (struct sockaddr*) &addr, &len) != 0) + throw Error(ERR_NET, (string) "Unable to get remote ip: " + strerror(errno)); + if (remote_ip_.empty()) + remote_ip_ = inet_ntoa(addr.sin_addr); + + // get hostname + struct hostent *h; + if ((h = ::gethostbyaddr(&addr.sin_addr, sizeof addr.sin_addr, AF_INET)) == 0) + throw Error(ERR_NET, (string) "Unable to get remote hostname: " + hstrerror(h_errno)); + remote_hostname_ = h->h_name; +} + /** * Set remote port * No mutex used ! @@ -138,6 +161,6 @@ void Connection::setremoteport_() { socklen_t len = sizeof addr; if (::getpeername(socket_fd_, (struct sockaddr*) &addr, &len) != 0) - throw Error(ERR_NET, (string) "Unable to set remote port: " + strerror(errno)); + throw Error(ERR_NET, (string) "Unable to get remote port: " + strerror(errno)); remote_port_ = ntohs(addr.sin_port); } diff --git a/slc/trunk/src/sll/slm.hh b/slc/trunk/src/sll/slm.hh index 97d6271..e04a2fd 100644 --- a/slc/trunk/src/sll/slm.hh +++ b/slc/trunk/src/sll/slm.hh @@ -46,6 +46,7 @@ enum { ERR_THREAD = 9, ERR_PARSE = 10, ERR_SCREENSZ = 11, + ERR_SIGNAL = 12, ERR_NOMEM = 41, ERR_UNKNOWN = 42 }; diff --git a/sls/trunk/src/sll/connection.cc b/sls/trunk/src/sll/connection.cc index a688f02..d78aa77 100644 --- a/sls/trunk/src/sll/connection.cc +++ b/sls/trunk/src/sll/connection.cc @@ -49,16 +49,8 @@ Connection::Connection(int fd) : socket_fd_(fd), local_port_(-1), remote_port_(- pthread_mutex_init(&r_mutex_, 0); pthread_mutex_init(&w_mutex_, 0); - if (socket_fd_ >= 0) { + if (socket_fd_ >= 0) id_ = getconnid(); - try { - setlocalip_(); - setlocalport_(); - setremoteip_(); - setremoteport_(); - } - catch (...) { } - } } /** @@ -308,7 +300,7 @@ void Connection::send(const char* buf, size_t len) { disconnect_(); pthread_mutex_unlock(&r_mutex_); pthread_mutex_unlock(&c_mutex_); - pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&w_mutex_); if (errno__ == ECONNRESET || errno__ == EPIPE) throw Error(ERR_NET, "Connection reset by peer"); else @@ -319,6 +311,14 @@ void Connection::send(const char* buf, size_t len) { pthread_mutex_unlock(&w_mutex_); } +/** + * Receive raw data + */ +char *Connection::recv(size_t size) { + assert(0); + return 0; +} + /** * Receive a line */ @@ -374,6 +374,10 @@ string Connection::recvln() { assert(1); } +void Connection::flush() { + assert(0); +} + string Connection::getlocalip() { // get local ip is a conn op pthread_mutex_lock(&c_mutex_); @@ -384,12 +388,35 @@ string Connection::getlocalip() { } if (local_ip_.empty()) - setlocalip_(); + try { setlocalip_(); } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } string ip = local_ip_; pthread_mutex_unlock(&c_mutex_); return ip; } +string Connection::getlocalhostname() { + pthread_mutex_lock(&c_mutex_); + if (local_hostname_.empty()) { + char buf[256]; + if (::gethostname(buf, 256) != 0) { + pthread_mutex_unlock(&c_mutex_); + throw Error(ERR_NET, (string) "Unable to get local hostname: " + strerror(errno)); + } + try { local_hostname_ = buf; } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } + } + pthread_mutex_unlock(&c_mutex_); + + return local_hostname_; +} + int Connection::getlocalport() { // get local port is a conn op pthread_mutex_lock(&c_mutex_); @@ -400,14 +427,19 @@ int Connection::getlocalport() { } if (local_port_ == -1) - setlocalport_(); + try { setlocalport_(); } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } + int port = local_port_; pthread_mutex_unlock(&c_mutex_); return port; } string Connection::getremoteip() { - // get remote ip is a conn op + // get remote hostname is a conn op pthread_mutex_lock(&c_mutex_); if (socket_fd_ < 0) { @@ -416,12 +448,38 @@ string Connection::getremoteip() { } if (remote_ip_.empty()) - setremoteip_(); + try { setremoteip_(); } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } + string ip = remote_ip_; pthread_mutex_unlock(&c_mutex_); return ip; } +string Connection::getremotehostname() { + // get remote ip is a conn op + pthread_mutex_lock(&c_mutex_); + + if (socket_fd_ < 0) { + pthread_mutex_unlock(&c_mutex_); + throw Error(ERR_NET, "No connection established but trying to get remote ip"); + } + + if (remote_hostname_.empty()) + try {setremotehostname_(); } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } + + string hostname = remote_hostname_; + pthread_mutex_unlock(&c_mutex_); + return hostname; +} + int Connection::getremoteport() { // get remote port is a conn op pthread_mutex_lock(&c_mutex_); @@ -432,7 +490,12 @@ int Connection::getremoteport() { } if (remote_port_ == -1) - setremoteport_(); + try {setremoteport_(); } + catch (...) { + pthread_mutex_unlock(&c_mutex_); + throw; + } + int port = remote_port_; pthread_mutex_unlock(&c_mutex_); return port; diff --git a/sls/trunk/src/sll/connection.hh b/sls/trunk/src/sll/connection.hh index 2c89e9b..89bffbc 100644 --- a/sls/trunk/src/sll/connection.hh +++ b/sls/trunk/src/sll/connection.hh @@ -45,13 +45,19 @@ public: inline void sendln(const string &data); // recv methods + char *recv(size_t size); string recvln(); + // buffer methods + void flush(); + // info methods string getlocalip(); + string getlocalhostname(); int getlocalport(); string getremoteip(); + string getremotehostname(); int getremoteport(); unsigned long int getid(); @@ -66,6 +72,7 @@ protected: inline void setlocalip_(); inline void setlocalport_(); inline void setremoteip_(); + inline void setremotehostname_(); inline void setremoteport_(); // Protected datas @@ -77,7 +84,9 @@ protected: int remote_port_; string local_ip_; + string local_hostname_; string remote_ip_; + string remote_hostname_; unsigned long int id_; diff --git a/sls/trunk/src/sll/connection.hxx b/sls/trunk/src/sll/connection.hxx index 3440140..3a606fb 100644 --- a/sls/trunk/src/sll/connection.hxx +++ b/sls/trunk/src/sll/connection.hxx @@ -21,6 +21,7 @@ #include #include #include +#include #include /******************************************************************************* @@ -99,7 +100,7 @@ void Connection::setlocalip_() { socklen_t len = sizeof addr; if (::getsockname(socket_fd_, (struct sockaddr*) &addr, &len) != 0) - throw Error(ERR_NET, (string) "Unable to set local ip: " + strerror(errno)); + throw Error(ERR_NET, (string) "Unable to get local ip: " + strerror(errno)); local_ip_ = inet_ntoa(addr.sin_addr); } @@ -112,7 +113,7 @@ void Connection::setlocalport_() { socklen_t len = sizeof addr; if (::getsockname(socket_fd_, (struct sockaddr*) &addr, &len) != 0) - throw Error(ERR_NET, (string) "Unable to set local port: " + strerror(errno)); + throw Error(ERR_NET, (string) "Unable to get local port: " + strerror(errno)); local_port_ = ntohs(addr.sin_port); } @@ -124,11 +125,33 @@ void Connection::setremoteip_() { struct sockaddr_in addr; socklen_t len = sizeof addr; + // get socket if (::getpeername(socket_fd_, (struct sockaddr*) &addr, &len) != 0) - throw Error(ERR_NET, (string) "Unable to set remote ip: " + strerror(errno)); + throw Error(ERR_NET, (string) "Unable to get remote ip: " + strerror(errno)); remote_ip_ = inet_ntoa(addr.sin_addr); } +/** + * Set remote hostname + * No mutex used ! + */ +void Connection::setremotehostname_() { + struct sockaddr_in addr; + socklen_t len = sizeof addr; + + // get sockaddr_in + if (::getpeername(socket_fd_, (struct sockaddr*) &addr, &len) != 0) + throw Error(ERR_NET, (string) "Unable to get remote ip: " + strerror(errno)); + if (remote_ip_.empty()) + remote_ip_ = inet_ntoa(addr.sin_addr); + + // get hostname + struct hostent *h; + if ((h = ::gethostbyaddr(&addr.sin_addr, sizeof addr.sin_addr, AF_INET)) == 0) + throw Error(ERR_NET, (string) "Unable to get remote hostname: " + hstrerror(h_errno)); + remote_hostname_ = h->h_name; +} + /** * Set remote port * No mutex used ! @@ -138,6 +161,6 @@ void Connection::setremoteport_() { socklen_t len = sizeof addr; if (::getpeername(socket_fd_, (struct sockaddr*) &addr, &len) != 0) - throw Error(ERR_NET, (string) "Unable to set remote port: " + strerror(errno)); + throw Error(ERR_NET, (string) "Unable to get remote port: " + strerror(errno)); remote_port_ = ntohs(addr.sin_port); } diff --git a/sls/trunk/src/sll/slm.hh b/sls/trunk/src/sll/slm.hh index 97d6271..e04a2fd 100644 --- a/sls/trunk/src/sll/slm.hh +++ b/sls/trunk/src/sll/slm.hh @@ -46,6 +46,7 @@ enum { ERR_THREAD = 9, ERR_PARSE = 10, ERR_SCREENSZ = 11, + ERR_SIGNAL = 12, ERR_NOMEM = 41, ERR_UNKNOWN = 42 }; -- GitLab