Commit 1ee0445b authored by Seblu's avatar Seblu
Browse files

No commit message

No commit message
parent db0966bb
Loading
Loading
Loading
Loading
+9 −115
Original line number Original line Diff line number Diff line
@@ -118,136 +118,25 @@ void SLDaemon::run() {
	  logerr << "!! " << e.message() << "\n";
	  logerr << "!! " << e.message() << "\n";
	}
	}
      }
      }
      //delete[] line;
    }
    }
  }
  }
  // recupere les erreurs de reseaux.
  // recupere les erreurs de reseaux.
  catch (const Error &e) {
  catch (const Error &e) {
    if (e.code() != ERR_NET)
    if (e.code() != ERR_NET && e.code() != ERR_AUTH)
      throw;
      throw;


    // disconnect
    // disconnect
    if (c_.connected())
    if (c_.connected())
      c_.disconnect();
      c_.disconnect();


//     if (socket_fs_ != 0) {
    if (verbose()) // TODO: Print the time at lost
    if (verbose()) // TODO: Print the time at lost
      logerr << "Connexion lost. Retrying in " << options.retrydelay << " seconds.\n";
      logerr << "Connexion lost. Retrying in " << options.retrydelay << " seconds.\n";
//       fclose(socket_fs_);

//       socket_fs_ = 0;
//     }
    sleep(options.retrydelay);
    sleep(options.retrydelay);
    goto net_connect;
    goto net_connect;
  }
  }
}
}


//******************************************************************************
// network functions
//******************************************************************************

// void SLDaemon::connect() {
//   struct sockaddr_in daddr;
//   struct hostent *h;

//   // Check no existing connexion
//   assert(socket_fs_ == 0);

//   // retrieve remote host info
//   h = gethostbyname(options.server.c_str());
//   if (h == 0)
//     throw Error(ERR_NET, hstrerror(h_errno));

//   // create socket
//   socket_fd_ = socket(PF_INET, SOCK_STREAM, 0);
//   if (socket_fd_ == -1)
//     throw Error(ERR_NET, strerror(errno));

//   daddr.sin_family = AF_INET;
//   daddr.sin_port = htons(options.port);
//   daddr.sin_addr = *((struct in_addr *) h->h_addr);
//   memset(daddr.sin_zero, '\0', sizeof daddr.sin_zero);

//   if (verbose())
//     logout << "Connecting to " << h->h_name << " (" << inet_ntoa(*(struct in_addr*)h->h_addr)
// 	   << ") on port " << options.port << "...\n";

//   // connect
//   if (::connect(socket_fd_, (struct sockaddr *) &daddr, sizeof daddr) == -1)
//     throw Error(ERR_NET, strerror(errno));

//   if (verbose())
//     logout << "Connected to " << h->h_name << " (" << inet_ntoa(*(struct in_addr*)h->h_addr)
// 	   << ") on port " << options.port << "...\n";

//   // initialize socket stream
//   if ((socket_fs_ = fdopen(socket_fd_, "r+")) == 0)
//     throw Error(ERR_NET, strerror(errno));
// }

// void SLDaemon::disconnect() {
//   if (!c_.connected())
//     return;
//   c_.disconnect();
//   // TODO: print close time
//   if (verbose())
//     logout << "Connection closed.\n";
// }

// void SLDaemon::send(long int i) {
//   fprintf(socket_fs_, "%li", i);
// }

// void SLDaemon::send(const string str, bool buf) {
//   send(str.c_str(), str.length(), buf);
// }

// void SLDaemon::send(const char *data, size_t len, bool buf) {
//   if (len == 0)
//     return;
//   if (fwrite(data, 1, len, socket_fs_) != len)
//     throw Error(ERR_NET, strerror(errno));
//   if (!buf && fflush(socket_fs_))
//     throw Error(ERR_NET, strerror(errno));
// }

// void SLDaemon::sendln(const string &s) {
//   if (!fprintf(socket_fs_, "%s\n", s.c_str()))
//     throw Error(ERR_NET, strerror(errno));
//   if (verbose())
//     logout << SND_DATA << s << "\n";
// }

// char *SLDaemon::recv(size_t size) {
//   char *data = new char[size];

//   if (fread(data, 1, size, socket_fs_) != size) {
//     delete[] data;
//     throw Error(ERR_NET, strerror(errno));
//   }
//   return data;
// }

// char *SLDaemon::recvln() {
//   char *line = new char[MAX_LINE_SIZE];

//   errno=0;
//   if (fgets(line, MAX_LINE_SIZE, socket_fs_) == 0) {
//     delete[] line;
//     if(feof(socket_fs_))
//       throw Error(ERR_NET, (string) "recvln: Connexion close");
//     throw Error(ERR_FILE, (string) "recvln: " + strerror(errno));
//   }
//   if (verbose())
//     logout << RCV_DATA << line;

//   return line;
// }

// void SLDaemon::flush() {
//   if (fflush(socket_fs_))
//     throw Error(ERR_NET, strerror(errno));
// }

//******************************************************************************
//******************************************************************************
// protocol functions
// protocol functions
//******************************************************************************
//******************************************************************************
@@ -261,6 +150,11 @@ void SLDaemon::auth() {
  snprintf(buf, MAX_LINE_SIZE, "PASS %s",
  snprintf(buf, MAX_LINE_SIZE, "PASS %s",
	   Cypher::sha1_64(options.pass.c_str(), options.pass.length()).c_str());
	   Cypher::sha1_64(options.pass.c_str(), options.pass.length()).c_str());
  c_.sendln(buf);
  c_.sendln(buf);

  string valid = c_.recvln();

  if (valid != "OK")
    throw Error(ERR_AUTH, "Bad Authentification.");
}
}


//******************************************************************************
//******************************************************************************
+0 −14
Original line number Original line Diff line number Diff line
@@ -61,8 +61,6 @@ protected:


  // current socket
  // current socket
  Connection c_;
  Connection c_;
//   int socket_fd_;
//   FILE* socket_fs_;


  // pid list
  // pid list
  typedef std::set<Job, Job> t_job;
  typedef std::set<Job, Job> t_job;
@@ -71,17 +69,6 @@ protected:
  // Options functions
  // Options functions
  void check_options() const;
  void check_options() const;


  // network functions
//   void disconnect();
//   void send(const char *data, size_t len, bool buf = true);
//   void send(const string str, bool buf = true);
//   void send(long int i);
//   void sendln(const string &s);
//   char *recv(size_t size);
//   void recv(size_t size, const string &filename);
//   char *recvln();
//   void flush();

  // protocol functions
  // protocol functions
  void auth();
  void auth();


@@ -97,7 +84,6 @@ protected:
  void cmd_file(const char *line);
  void cmd_file(const char *line);


  // others functions
  // others functions
//   string md5(const string &file) const;
  void recv2file(size_t size, const string &filename);
  void recv2file(size_t size, const string &filename);
  void clean_dir(const string &dir) const;
  void clean_dir(const string &dir) const;


+17 −14
Original line number Original line Diff line number Diff line
@@ -120,6 +120,8 @@ static bool register_signals() {
    return false;
    return false;
  }
  }


  if (SLDaemon::Instance().options.daemon) {

    // register sigint handler
    // register sigint handler
    if (signal(SIGINT, sigint) == SIG_ERR) {
    if (signal(SIGINT, sigint) == SIG_ERR) {
      logerr << "sld: error: " << strerror(errno) << ".\n";
      logerr << "sld: error: " << strerror(errno) << ".\n";
@@ -137,6 +139,7 @@ static bool register_signals() {
      logerr << "sld: error: " << strerror(errno) << ".\n";
      logerr << "sld: error: " << strerror(errno) << ".\n";
      return false;
      return false;
    }
    }
  }


  return true;
  return true;
}
}
+23 −15
Original line number Original line Diff line number Diff line
@@ -106,14 +106,14 @@ void Connection::connect(const char *addr, int port) {
  }
  }


  // take read and write mutex
  // take read and write mutex
  pthread_mutex_lock(&r_mutex_);
  pthread_mutex_lock(&w_mutex_);
  pthread_mutex_lock(&w_mutex_);
  pthread_mutex_lock(&r_mutex_);


  // retrieve remote host info
  // retrieve remote host info
  struct hostent *h = gethostbyname(addr);
  struct hostent *h = gethostbyname(addr);
  if (h == 0) {
  if (h == 0) {
    pthread_mutex_unlock(&w_mutex_);
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&w_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    throw Error(ERR_NET, (string) "Unable to resolve: " + addr  + ": " + hstrerror(h_errno));
    throw Error(ERR_NET, (string) "Unable to resolve: " + addr  + ": " + hstrerror(h_errno));
  }
  }
@@ -121,8 +121,8 @@ void Connection::connect(const char *addr, int port) {
  // create socket
  // create socket
  try { socket_(); }
  try { socket_(); }
  catch (...) {
  catch (...) {
    pthread_mutex_unlock(&w_mutex_);
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&w_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    throw;
    throw;
  }
  }
@@ -138,8 +138,8 @@ void Connection::connect(const char *addr, int port) {
  if (::connect(socket_fd_, (struct sockaddr *) &daddr, sizeof daddr) == -1) {
  if (::connect(socket_fd_, (struct sockaddr *) &daddr, sizeof daddr) == -1) {
    int errno__ = errno;
    int errno__ = errno;
    disconnect_();
    disconnect_();
    pthread_mutex_unlock(&w_mutex_);
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&w_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    throw Error(ERR_NET, (string) "Unable to connect to " + addr  + ": " + strerror(errno__));
    throw Error(ERR_NET, (string) "Unable to connect to " + addr  + ": " + strerror(errno__));
  }
  }
@@ -152,14 +152,14 @@ void Connection::connect(const char *addr, int port) {
    setremoteport_();
    setremoteport_();
  }
  }
  catch (...) {
  catch (...) {
    pthread_mutex_unlock(&w_mutex_);
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&w_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    throw;
    throw;
  }
  }


  pthread_mutex_unlock(&w_mutex_);
  pthread_mutex_unlock(&r_mutex_);
  pthread_mutex_unlock(&r_mutex_);
  pthread_mutex_unlock(&w_mutex_);
  pthread_mutex_unlock(&c_mutex_);
  pthread_mutex_unlock(&c_mutex_);
}
}


@@ -243,26 +243,34 @@ void Connection::listen(int port, int max) {
 * @return null on error, else a new connection
 * @return null on error, else a new connection
 */
 */
Connection *Connection::accept() {
Connection *Connection::accept() {
  // keep the socket fd
  // accept is a read/write op
  pthread_mutex_lock(&c_mutex_);
  pthread_mutex_lock(&c_mutex_);
  pthread_mutex_lock(&r_mutex_);
  pthread_mutex_lock(&w_mutex_);

  // get the socket fd
  int socket_fd = socket_fd_;
  int socket_fd = socket_fd_;
  pthread_mutex_unlock(&c_mutex_);
  pthread_mutex_unlock(&c_mutex_);


  // test socket validity
  // test socket validity
  if (socket_fd < 0)
  if (socket_fd < 0) {
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&w_mutex_);
    throw Error(ERR_NET, "No connection established but trying to accept");
    throw Error(ERR_NET, "No connection established but trying to accept");
  }


  struct sockaddr_in r_addr;
  struct sockaddr_in r_addr;
  socklen_t r_sin_size = sizeof r_addr;
  socklen_t r_sin_size = sizeof r_addr;
  int r_fd;
  int r_fd;


  // accept is a read/write operation
  pthread_mutex_lock(&r_mutex_);
  pthread_mutex_lock(&w_mutex_);

  if ((r_fd = ::accept(socket_fd, (struct sockaddr *) &r_addr, &r_sin_size)) == -1) {
  if ((r_fd = ::accept(socket_fd, (struct sockaddr *) &r_addr, &r_sin_size)) == -1) {
    int errno__ = errno;
    pthread_mutex_lock(&c_mutex_);
    disconnect_();
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&w_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    throw Error(ERR_NET, (string) "accept: " + strerror(errno));
    throw Error(ERR_NET, (string) "accept: " + strerror(errno__));
  }
  }


  pthread_mutex_unlock(&r_mutex_);
  pthread_mutex_unlock(&r_mutex_);
@@ -299,8 +307,8 @@ void Connection::send(const char* buf, size_t len) {
    pthread_mutex_lock(&r_mutex_);
    pthread_mutex_lock(&r_mutex_);
    disconnect_();
    disconnect_();
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&r_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    pthread_mutex_unlock(&w_mutex_);
    pthread_mutex_unlock(&w_mutex_);
    pthread_mutex_unlock(&c_mutex_);
    if (errno__ == ECONNRESET || errno__ == EPIPE)
    if (errno__ == ECONNRESET || errno__ == EPIPE)
      throw Error(ERR_NET, "Connection reset by peer");
      throw Error(ERR_NET, "Connection reset by peer");
    else
    else
@@ -354,9 +362,9 @@ string Connection::recvln() {
      pthread_mutex_lock(&c_mutex_);
      pthread_mutex_lock(&c_mutex_);
      pthread_mutex_lock(&w_mutex_);
      pthread_mutex_lock(&w_mutex_);
      disconnect_();
      disconnect_();
      pthread_mutex_unlock(&w_mutex_);
      pthread_mutex_unlock(&r_mutex_);
      pthread_mutex_unlock(&r_mutex_);
      pthread_mutex_unlock(&c_mutex_);
      pthread_mutex_unlock(&c_mutex_);
      pthread_mutex_unlock(&r_mutex_);


      *local_buf = 0;
      *local_buf = 0;


+1 −1
Original line number Original line Diff line number Diff line
@@ -52,7 +52,7 @@ string Cypher::sha1_64(const char *data, size_t size) {
  BIO_get_mem_ptr(b64, &bptr);
  BIO_get_mem_ptr(b64, &bptr);


  if (bptr->length > 0)
  if (bptr->length > 0)
    ret.insert(0, bptr->data, bptr->length);
    ret.insert(0, bptr->data, bptr->length - 1);
  else
  else
    throw Error(ERR_CYPHER, "Unable to compute sha1_64");
    throw Error(ERR_CYPHER, "Unable to compute sha1_64");