Commit 28a8022e authored by Seblu's avatar Seblu

utilisation de sll => correction du bug sur les connections

amelioration de sll avec ajout de getlocalhostname et getremotehostname
parent 09aa09d0
......@@ -16,16 +16,22 @@
bin_PROGRAMS= sld
sld_SOURCES= src/sld.hh \
src/sld.cc \
src/options.hh \
sld_SOURCES= src/sld.cc \
src/options.cc \
src/daemon.hh \
src/daemon.cc \
src/log.hh \
src/log.cc \
src/error.hh \
src/error.cc
src/sll/error.cc \
src/sll/connection.cc
noinst_HEADER= src/sld.hh \
src/options.hh \
src/daemon.hh \
src/log.hh \
src/sll/slm.hh \
src/sll/error.hh \
src/sll/connection.hh \
src/sll/connection.hxx
CLEANFILES= *~ '\#*' .*.swp .*~
......
IMPROVMENTS:
faire les fonctions recv et flush de connection dans sll
remove static magic MAX_LINE_SIZE in sscanf
Print start date and stop date in log file
effacer les scripts avant de quitter
Ajout une fonction qui delete un script
ajouter la commande ping/pong
utiliser le gestionnaire d'option generique
BUG:
-
echo -e "EXIT\ntoto" |nc -p 18136 -l localhost
when server send data after exit, daemon segfault
-
......@@ -24,7 +24,7 @@ AC_INIT([sld],[1.0],[contact@seblu.net],[sld])
AC_CONFIG_AUX_DIR([build])
# Auto Make init
AM_INIT_AUTOMAKE([foreign dist-bzip2 no-dist-gzip])
AM_INIT_AUTOMAKE([foreign dist-bzip2 no-dist-gzip subdir-objects])
# Check platform
AC_CANONICAL_HOST
......@@ -68,6 +68,7 @@ AC_PROG_MAKE_SET
# Check for libs
AC_CHECK_LIB([ssl], [SSL_library_init],,[AC_MSG_ERROR([Library ssl not found!])])
AC_CHECK_LIB([pthread], [pthread_create],,[AC_MSG_ERROR([Library pthread not found!])])
# Set efence library
AS_IF([test "x$with_efence" = "xyes" ],
......
......@@ -4,7 +4,7 @@
# host login
# Default = see the result of hostname command
host=trinity
# host=raptor
# host password
# Default = none
......
This diff is collapsed.
......@@ -19,12 +19,11 @@
#ifndef DAEMON_HH
# define DAEMON_HH
# include "sld.hh"
# include "sll/connection.hh"
# include "options.hh"
typedef std::string string;
typedef std::stringstream sstream;
typedef std::ostream ostream;
typedef std::ifstream ifstream;
# include <set>
// -----------------------------------------------------------------------------
// SLDAEMON CLASS
......@@ -61,8 +60,9 @@ public:
protected:
// current socket
int socket_fd_;
FILE* socket_fs_;
Connection c_;
// int socket_fd_;
// FILE* socket_fs_;
// pid list
typedef std::set<Job, Job> t_job;
......@@ -72,16 +72,15 @@ protected:
void check_options() const;
// network functions
void connect();
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();
// 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
void auth();
......@@ -99,12 +98,13 @@ protected:
// others functions
string md5(const string &file) const;
void recv2file(size_t size, const string &filename);
void clean_dir(const string &dir) const;
private:
static SLDaemon *instance_;
SLDaemon();
SLDaemon() {}
~SLDaemon() {}
SLDaemon(const SLDaemon &) { assert(true); }
SLDaemon &operator=(const SLDaemon &) { assert(true); return *instance_; }
......
......@@ -30,7 +30,7 @@ void Options::usage(const char *argv0) const {
<< "usage: "
<< argv0
<< " [-f conffile] [-d scriptdir] [-h] [-v] [-l login]"
<< "[-p pass] [-H host] [-P port] [-r] [-V]"
<< " [-p pass] [-H host] [-P port] [-r] [-V]"
<< std::endl
<< " -f file : read and load conf file (def: /etc/sldrc)." << std::endl
<< " -d dir : Scripts directory." << std::endl
......@@ -79,7 +79,7 @@ Options &Options::getoptions(int argc, char *argv[]) {
}
else if (!strcmp(argv[i], "-V")) {
std::cout << "sl daemon version : " << VERSION << std::endl;
exit(ERR_OK);
exit(ERR_NO);
}
else if (!strcmp(argv[i], "-H")) {
if (++i >= argc)
......@@ -130,7 +130,7 @@ Options &Options::getoptions(int argc, char *argv[]) {
this->pidfile = argv[i];
}
else
throw Error(ERR_USAGE, (string) "Invalid options : " + argv[i]);
throw Error(ERR_USAGE, (string) "Invalid option : " + argv[i]);
}
return *this;
}
......
......@@ -71,7 +71,6 @@ int main(int argc, char *argv[])
logerr.file(daemon.options.logfile);
}
if (!register_signals())
return ERR_SIGNAL;
......
......@@ -19,44 +19,18 @@
#ifndef SLD_HH
# define SLD_HH
# include <cassert>
# include <ios>
# include <iostream>
# include <fstream>
# include <sstream>
# include <map>
# include <time.h>
# include <set>
# include "sll/slm.hh"
# include "sll/error.hh"
# include "error.hh"
# include "log.hh"
# include "options.hh"
# include "daemon.hh"
typedef std::string string;
typedef std::stringstream sstream;
typedef std::ostream ostream;
typedef std::ifstream ifstream;
enum {
ERR_OK = 0,
ERR_USAGE = 1,
ERR_BADPARAM = 2,
ERR_FILE = 3,
ERR_NET = 4,
ERR_PROTO = 5,
ERR_AUTH = 6,
ERR_SIGNAL = 7,
ERR_NOMEM = 41,
ERR_UNKNOWN = 42
};
// -----------------------------------------------------------------------------
// Gonstant
// -----------------------------------------------------------------------------
static const string VERSION = "1";
static const int MAX_LINE_SIZE = 512;
static const int MAX_CONF_LINE_SIZE = 2048;
static const string RCV_DATA = "<< ";
static const string SND_DATA = ">> ";
......
This diff is collapsed.
/*
This file is part of SLS.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLL is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
SLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SLL; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef CONNECTION_HH
# define CONNECTION_HH
# include <pthread.h>
class Connection {
// class methods
public:
unsigned long int getconnid();
// public methods
public:
Connection(int fd = -1);
virtual ~Connection();
void connect(const char *addr, int port);
void listen(int port, int max);
void disconnect();
inline bool connected();
Connection *accept();
// send methods
void send(const char* buf, size_t len);
inline void send(const string &data);
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();
int getsocket();
// protected methods
protected:
inline void socket_();
inline void disconnect_();
inline void setlocalip_();
inline void setlocalport_();
inline void setremoteip_();
inline void setremotehostname_();
inline void setremoteport_();
// Protected datas
protected:
int socket_fd_; // connection socket
// storage of info about connection
int local_port_;
int remote_port_;
string local_ip_;
string local_hostname_;
string remote_ip_;
string remote_hostname_;
unsigned long int id_;
string rbuf_; // read buffer
string wbuf_; // write buffer
pthread_mutex_t c_mutex_; // connection mutex
pthread_mutex_t r_mutex_; // read mutex
pthread_mutex_t w_mutex_; // write mutex
};
# include "connection.hxx"
#endif
/*
This file is part of SLL.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLL is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
SLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SLLo; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
# include "error.hh"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
/*******************************************************************************
** Public method
******************************************************************************/
/**
* Return the connection sate
* Cannot be const, du to mutex locking
*
* @return see description
*/
bool Connection::connected() {
// get connection mutex
pthread_mutex_lock(&c_mutex_);
bool ret = (socket_fd_ >= 0);
pthread_mutex_unlock(&c_mutex_);
return ret;
}
/**
* Send @param data on socket
*/
void Connection::send(const string &data) {
send(data.c_str(), data.length());
}
/**
* Send @param data followed by '\n' on socket
*/
void Connection::sendln(const string &data) {
string tosend = data + "\n";
send(tosend.c_str(), tosend.length());
}
/*******************************************************************************
** Protected method
******************************************************************************/
/**
* Free a socket
* No mutex used !
*/
void Connection::disconnect_() {
assert(socket_fd_ >= 0);
close(socket_fd_);
socket_fd_ = -1;
}
/**
* Reserve a socket
* No mutex used !
*/
void Connection::socket_() {
assert(socket_fd_ == -1);
int yes = 1;
if ((socket_fd_ = ::socket(AF_INET, SOCK_STREAM, 0)) == -1)
throw Error(ERR_NET, (string) "Unable to open socket: " + strerror(errno));
if (::setsockopt(socket_fd_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
throw Error(ERR_NET, (string) "Unable to set socket options: " + strerror(errno));
}
/**
* Set local ip
* No mutex used !
*/
void Connection::setlocalip_() {
struct sockaddr_in addr;
socklen_t len = sizeof addr;
if (::getsockname(socket_fd_, (struct sockaddr*) &addr, &len) != 0)
throw Error(ERR_NET, (string) "Unable to get local ip: " + strerror(errno));
local_ip_ = inet_ntoa(addr.sin_addr);
}
/**
* Set local port
* No mutex used !
*/
void Connection::setlocalport_() {
struct sockaddr_in addr;
socklen_t len = sizeof addr;
if (::getsockname(socket_fd_, (struct sockaddr*) &addr, &len) != 0)
throw Error(ERR_NET, (string) "Unable to get local port: " + strerror(errno));
local_port_ = ntohs(addr.sin_port);
}
/**
* Set remote ip
* No mutex used !
*/
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 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 !
*/
void Connection::setremoteport_() {
struct sockaddr_in addr;
socklen_t len = sizeof addr;
if (::getpeername(socket_fd_, (struct sockaddr*) &addr, &len) != 0)
throw Error(ERR_NET, (string) "Unable to get remote port: " + strerror(errno));
remote_port_ = ntohs(addr.sin_port);
}
/*
This file is part of SLD.
This file is part of SLL.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLD is free software; you can redistribute it and/or modify
SLL is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
SLD is distributed in the hope that it will be useful,
SLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SLD; if not, write to the Free Software
along with SLL; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "error.hh"
#include "log.hh"
/*!
** Constructor
**
** @param i error code
** @param s error message
*/
Error::Error(int i, const string s)
{
val_ = i;
msg_ << s;
}
/*!
** Constructor by copy
**
** @param e
*/
Error::Error(const Error &e)
{
val_ = e.val_;
msg_ << e.msg_.str();
}
/*!
** Print and error msg on std err
**
*/
void Error::print() const
{
if (msg_.str() != "")
logerr << msg_.str() << ".\n";
std::cerr << msg_.str() << "." << std::endl;
}
/*!
** @return a copy of message description
*/
string Error::message() const
{
return msg_.str();
}
/*!
** @return the error code
*/
int Error::code() const
{
return val_;
}
/*!
** Set error code to @param v
*/
void Error::code(int v)
{
val_ = v;
}
/*!
** Concatenate @param s before error message
*/
void Error::prefix(const string &s) {
string buf = s + msg_.str();
msg_.str("");
msg_ << buf;
}
/*!
** Concatenate @param s to errror message
*/
void Error::suffix(const string &s) {
msg_ << s;
}
/*!
** Copy operator
*/
Error &Error::operator=(const Error &rhs)
{
val_ = rhs.val_;
......@@ -60,24 +103,36 @@ Error &Error::operator=(const Error &rhs)
return *this;
}
/*!
** Print error message to @param o stream
*/
ostream &operator<<(ostream &o, const Error &e)
{
o << e.msg_.str();
return o;
}
/*!
** Concatenate @param val to errror message
*/
Error &operator<<(Error &e, int val)
{
e.msg_ << val;
return e;
}
/*!
** Concatenate @param s to errror message
*/
Error &operator<<(Error &e, const string &s)
{
e.msg_ << s;
return e;
}
/*!
** Concatenate @param s before error message
*/
Error &operator>>(Error &e, const string &s)
{
string buf = s + e.msg_.str();
......@@ -86,3 +141,4 @@ Error &operator>>(Error &e, const string &s)
e.msg_ << buf;
return e;
}
/*
This file is part of SLD.
This file is part of SLL.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLD is free software; you can redistribute it and/or modify
SLL is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
SLD is distributed in the hope that it will be useful,
SLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SLD; if not, write to the Free Software
along with SLL; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef ERROR_HH
# define ERROR_HH
# include <ios>
# include <iostream>
# include <sstream>
typedef std::string string;
typedef std::stringstream sstream;
typedef std::ostream ostream;
# include "slm.hh"
class Error
{
public:
Error(int i, string s = "");
Error(const Error &e);
void print() const;
string message() const;
void suffix(const string &);
void prefix(const string &);
int code() const;
void code(int);
Error &operator=(const Error &rhs);
friend ostream &operator<<(ostream &o, const Error &e);
friend Error &operator<<(Error &e, const string &s);
friend Error &operator>>(Error &e, const string &s);
friend Error &operator<<(Error &e, int val);
protected:
sstream msg_;
int val_;
......
/*
This file is part of SLL.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLL is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
SLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SLL; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SLM_HH
# define SLM_HH
# define _REENTRANT
# include <cassert>
# include <ios>
# include <iostream>
# include <fstream>
# include <sstream>
typedef std::string string;
typedef std::stringstream sstream;
typedef std::ostream ostream;
typedef std::ifstream ifstream;
typedef std::ofstream ofstream;
enum {
ERR_NO = 0,
ERR_USAGE = 1,
ERR_BADPARAM = 2,
ERR_FILE = 3,
ERR_NET = 4,
ERR_PROTO = 5,
ERR_AUTH = 6,
ERR_DB = 7,
ERR_SRV = 8,