Commit 815dc267 authored by Seblu's avatar Seblu
Browse files

Ajout des fonctions pour sld

parent 28a8022e
Loading
Loading
Loading
Loading
+78 −15
Original line number Diff line number Diff line
@@ -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;
+9 −0
Original line number Diff line number Diff line
@@ -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_;

+27 −4
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>

/*******************************************************************************
@@ -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);
}