Added getBoundAddress() function to Socket::Connection to get the locally bound address for a socket.
This commit is contained in:
parent
6c01a591d3
commit
6727a6e37a
2 changed files with 34 additions and 0 deletions
|
@ -372,6 +372,29 @@ void Socket::Buffer::clear(){
|
||||||
data.clear();
|
data.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Socket::Connection::setBoundAddr(){
|
||||||
|
if (!isTrueSocket){
|
||||||
|
boundaddr = "";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
struct sockaddr_in6 tmpaddr;
|
||||||
|
socklen_t len = sizeof(tmpaddr);
|
||||||
|
if (!getsockname(sSend, (sockaddr*)&tmpaddr, &len)){
|
||||||
|
static char addrconv[INET6_ADDRSTRLEN];
|
||||||
|
if (tmpaddr.sin6_family == AF_INET6){
|
||||||
|
boundaddr = inet_ntop(AF_INET6, &(tmpaddr.sin6_addr), addrconv, INET6_ADDRSTRLEN);
|
||||||
|
if (boundaddr.substr(0, 7) == "::ffff:"){
|
||||||
|
boundaddr = boundaddr.substr(7);
|
||||||
|
}
|
||||||
|
HIGH_MSG("Local IPv6 addr [%s]", boundaddr.c_str());
|
||||||
|
}
|
||||||
|
if (tmpaddr.sin6_family == AF_INET){
|
||||||
|
boundaddr = inet_ntop(AF_INET, &(((sockaddr_in *)&tmpaddr)->sin_addr), addrconv, INET6_ADDRSTRLEN);
|
||||||
|
HIGH_MSG("Local IPv4 addr [%s]", boundaddr.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a new base socket. This is a basic constructor for converting any valid socket to a
|
/// Create a new base socket. This is a basic constructor for converting any valid socket to a
|
||||||
/// Socket::Connection. \param sockNo Integer representing the socket to convert.
|
/// Socket::Connection. \param sockNo Integer representing the socket to convert.
|
||||||
Socket::Connection::Connection(int sockNo){
|
Socket::Connection::Connection(int sockNo){
|
||||||
|
@ -380,6 +403,7 @@ Socket::Connection::Connection(int sockNo){
|
||||||
isTrueSocket = false;
|
isTrueSocket = false;
|
||||||
struct stat sBuf;
|
struct stat sBuf;
|
||||||
if (sSend != -1 && !fstat(sSend, &sBuf)){isTrueSocket = S_ISSOCK(sBuf.st_mode);}
|
if (sSend != -1 && !fstat(sSend, &sBuf)){isTrueSocket = S_ISSOCK(sBuf.st_mode);}
|
||||||
|
setBoundAddr();
|
||||||
up = 0;
|
up = 0;
|
||||||
down = 0;
|
down = 0;
|
||||||
conntime = Util::epoch();
|
conntime = Util::epoch();
|
||||||
|
@ -401,6 +425,7 @@ Socket::Connection::Connection(int write, int read){
|
||||||
isTrueSocket = false;
|
isTrueSocket = false;
|
||||||
struct stat sBuf;
|
struct stat sBuf;
|
||||||
if (sSend != -1 && !fstat(sSend, &sBuf)){isTrueSocket = S_ISSOCK(sBuf.st_mode);}
|
if (sSend != -1 && !fstat(sSend, &sBuf)){isTrueSocket = S_ISSOCK(sBuf.st_mode);}
|
||||||
|
setBoundAddr();
|
||||||
up = 0;
|
up = 0;
|
||||||
down = 0;
|
down = 0;
|
||||||
conntime = Util::epoch();
|
conntime = Util::epoch();
|
||||||
|
@ -600,6 +625,7 @@ Socket::Connection::Connection(std::string host, int port, bool nonblock){
|
||||||
int optval = 1;
|
int optval = 1;
|
||||||
int optlen = sizeof(optval);
|
int optlen = sizeof(optval);
|
||||||
setsockopt(sSend, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);
|
setsockopt(sSend, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);
|
||||||
|
setBoundAddr();
|
||||||
}
|
}
|
||||||
}// Socket::Connection TCP Constructor
|
}// Socket::Connection TCP Constructor
|
||||||
|
|
||||||
|
@ -796,6 +822,11 @@ std::string Socket::Connection::getHost() const{
|
||||||
return remotehost;
|
return remotehost;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets locally bound host for connection, if available.
|
||||||
|
std::string Socket::Connection::getBoundAddress() const{
|
||||||
|
return boundaddr;
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets binary IPv6 address for connection, if available.
|
/// Gets binary IPv6 address for connection, if available.
|
||||||
/// Guaranteed to be either empty or 16 bytes long.
|
/// Guaranteed to be either empty or 16 bytes long.
|
||||||
std::string Socket::Connection::getBinHost(){
|
std::string Socket::Connection::getBinHost(){
|
||||||
|
|
|
@ -74,6 +74,7 @@ namespace Socket{
|
||||||
int sSend; ///< Write end of socket.
|
int sSend; ///< Write end of socket.
|
||||||
int sRecv; ///< Read end of socket.
|
int sRecv; ///< Read end of socket.
|
||||||
std::string remotehost; ///< Stores remote host address.
|
std::string remotehost; ///< Stores remote host address.
|
||||||
|
std::string boundaddr; ///< Stores bound interface address.
|
||||||
struct sockaddr_in6 remoteaddr;///< Stores remote host address.
|
struct sockaddr_in6 remoteaddr;///< Stores remote host address.
|
||||||
uint64_t up;
|
uint64_t up;
|
||||||
uint64_t down;
|
uint64_t down;
|
||||||
|
@ -83,6 +84,7 @@ namespace Socket{
|
||||||
virtual unsigned int iwrite(const void *buffer, int len); ///< Incremental write call.
|
virtual unsigned int iwrite(const void *buffer, int len); ///< Incremental write call.
|
||||||
bool iread(Buffer &buffer, int flags = 0); ///< Incremental write call that is compatible with Socket::Buffer.
|
bool iread(Buffer &buffer, int flags = 0); ///< Incremental write call that is compatible with Socket::Buffer.
|
||||||
bool iwrite(std::string &buffer); ///< Write call that is compatible with std::string.
|
bool iwrite(std::string &buffer); ///< Write call that is compatible with std::string.
|
||||||
|
void setBoundAddr();
|
||||||
public:
|
public:
|
||||||
// friends
|
// friends
|
||||||
friend class ::Buffer::user;
|
friend class ::Buffer::user;
|
||||||
|
@ -100,6 +102,7 @@ namespace Socket{
|
||||||
std::string getHost() const; ///< Gets hostname for connection, if available.
|
std::string getHost() const; ///< Gets hostname for connection, if available.
|
||||||
std::string getBinHost();
|
std::string getBinHost();
|
||||||
void setHost(std::string host); ///< Sets hostname for connection manually.
|
void setHost(std::string host); ///< Sets hostname for connection manually.
|
||||||
|
std::string getBoundAddress() const;
|
||||||
int getSocket(); ///< Returns internal socket number.
|
int getSocket(); ///< Returns internal socket number.
|
||||||
int getPureSocket(); ///< Returns non-piped internal socket number.
|
int getPureSocket(); ///< Returns non-piped internal socket number.
|
||||||
std::string getError(); ///< Returns a string describing the last error that occured.
|
std::string getError(); ///< Returns a string describing the last error that occured.
|
||||||
|
|
Loading…
Add table
Reference in a new issue