Commit e6ac348e authored by Seblu's avatar Seblu

Class connection complement recodee:

- Thread safe. 
- Gestion complete de toutes les connections

Class client avec des heritier pour gerer les types de connection
parent b4a76767
......@@ -27,7 +27,14 @@ sls_SOURCES= src/sls.hh \
src/cron.hh \
src/cron.cc \
src/server.hh \
src/server.cc
src/server.cc \
src/connection.hh \
src/connection.hxx \
src/connection.cc \
src/client.hh \
src/client.cc \
src/client_console.cc \
src/client_daemon.cc
CLEANFILES= *~ '\#*'
......
Gerer les control-C sur les sockets. Le server ne ferme pas la connection...
Implementer un conn id pour qu'un serveur puisse numeroter les connecxions
/*
This file is part of SLS.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLS 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.
SLS 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 SLS; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "sls.hh"
#include "client.hh"
/*******************************************************************************
** Method of Classes
*******************************************************************************/
Client *Client::login(Connection &c) {
string slogin = c.recvln();
string spass = c.recvln();
// FIXME: find better method than fucking magic number
char loginbuf[512];
char passbuf[512];
if (sscanf(spass.c_str(), "PASS %512s\n", passbuf) != 1)
return login_fail(c);
if (sscanf(slogin.c_str(), "HOST %512s\n", loginbuf) == 1) {
std::cout << "Host " << loginbuf << " logged from " << c.getremoteip()
<< " on port " << c.getremoteport() << ".\n";
return new Daemon(c);
}
else if (sscanf(slogin.c_str(), "USER %512s\n", loginbuf) == 1) {
return new Console(c);
}
return login_fail(c);
}
Client *Client::login_fail(Connection &c) {
std::cout << "Bad authentification from " << c.getremoteip() << " on port "
<< c.getremoteport() << ".\n";
c.sendln("Bad authentification.");
return 0;
}
/*******************************************************************************
** Public Classes
*******************************************************************************/
Client::Client(Connection &c) : c_ (c) {}
Client::~Client() {}
void Client::exec() {
assert(0);
}
/*
This file is part of SLS.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLS 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.
SLS 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 SLS; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef CLIENT_HH
# define CLIENT_HH
# include "connection.hh"
/*!
** Client Class
*/
class Client {
public:
Client(Connection &c);
virtual ~Client();
virtual void exec();
static Client *login(Connection &c);
protected:
Connection c_;
private:
static Client *login_fail(Connection &c);
};
/*!
** Client Daemon Class
*/
class Daemon: public Client {
public:
Daemon(Connection &c);
void exec();
};
/*!
** Client Console Class
*/
class Console : public Client {
public:
Console(Connection &c);
void exec();
};
#endif
/*
This file is part of SLS.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLS 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.
SLS 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 SLS; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "sls.hh"
#include "client.hh"
Console::Console(Connection &c) : Client(c) {}
void Console::exec() {
c_.sendln("You are a console !!");
}
/*
This file is part of SLS.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLS 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.
SLS 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 SLS; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "sls.hh"
#include "client.hh"
Daemon::Daemon(Connection &c) : Client(c) {}
void Daemon::exec() {
c_.sendln("EXIT");
while (1) { c_.sendln("toto"); }
}
This diff is collapsed.
/*
This file is part of SLS.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLS 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.
SLS 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 SLS; 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 {
// public methods
public:
Connection(int fd = -1);
virtual ~Connection();
void connect(const char *addr, int port);
void listen(int port, int max);
void disconnect();
bool connected();
Connection *accept();
// send methods
inline void send(const string &data);
void send(const char* buf, size_t len);
inline void sendln(const string &data);
void sendln(const char* buf, size_t len);
// recv methods
string recvln();
// info methods
string getlocalip();
int getlocalport();
string getremoteip();
int getremoteport();
int getsocket();
// protected methods
protected:
void socket_();
void disconnect_();
inline void setallinfo_();
void setlocalip_();
void setlocalport_();
void setremoteip_();
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 remote_ip_;
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 SLS.
Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>
SLS 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.
SLS 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 SLS; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*******************************************************************************
** Public method
******************************************************************************/
/**
* 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) {
sendln(data.c_str(), data.length());
}
/*******************************************************************************
** Protected method
******************************************************************************/
/**
* Set all information about a connection
*/
void Connection::setallinfo_() {
setlocalip_();
setlocalport_();
setremoteip_();
setremoteport_();
}
......@@ -75,6 +75,23 @@ 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
*/
......@@ -124,3 +141,4 @@ Error &operator>>(Error &e, const string &s)
e.msg_ << buf;
return e;
}
......@@ -32,10 +32,16 @@ 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);
......
......@@ -79,6 +79,7 @@ Option &Option::load(int argc, char *argv[]) {
switch (option) {
case 'h':
usage(*argv, std::cout);
exit(ERR_NO);
break;
case 'p':
port = atoi(optarg);
......@@ -96,7 +97,7 @@ Option &Option::load(int argc, char *argv[]) {
logfile = optarg;
break;
case 'V':
std::cerr << "version : " << VERSION << std::endl;
std::cerr << "Version: " << VERSION << std::endl;
exit(ERR_NO);
case 'D':
daemon = false;
......@@ -157,7 +158,7 @@ void Option::print(ostream &s) const {
s << "Database file: " << database << std::endl
<< "Log file: " << logfile << std::endl
<< "Pid file: " << pidfile << std::endl
<< "TCP Port: " << port << std::endl
<< "TCP port: " << port << std::endl
<< "Max conn: " << maxconn << std::endl
<< "Debug mode: " << !daemon << std::endl;
}
......@@ -18,36 +18,180 @@
#include "sls.hh"
#include "server.hh"
#include "error.hh"
#include "connection.hh"
#include "client.hh"
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
static const int BACKLOG = 10;
/*!
* Constructor
*/
Server::Server() : socket_(0), thread_count_(0) {}
Server::Server() : max_conn_ (0), verbose_(false) {
pthread_mutex_init(&threads_mutex_, 0);
}
/*!
* Start server. Creating socket
* Destructor
*/
void Server::start() {
Server::~Server() {
pthread_mutex_destroy(&threads_mutex_);
}
/*!
* @return if server is started or not
* Start server.
*/
bool Server::started() const {
return socket_ != 0;
void Server::start(int port, size_t max_conn, bool verbose) {
if (max_conn <= 0)
throw Error(ERR_SRV, "Too less max connection allowed");
if (conn_.connected())
throw Error(ERR_SRV, "Server already started");
// Set options
max_conn_ = max_conn;
verbose_ = verbose;
// listen on connection
conn_.listen(port, BACKLOG);
// treat new connections
while (1) {
// accept incoming connection
Connection *nc = conn_.accept();
// check max conn
if (threads_.size() >= max_conn_) {
std::cout << "Connection refused from ip " << nc->getremoteip()
<< " on port " << nc->getremoteport()
<< ": Max connections reached.\n";
nc->disconnect();
continue;
}
// Print connection
std::cout << "New connection from ip " << nc->getremoteip()
<< " on port " << nc->getremoteport();
if (verbose_)
std::cout << " (socket " << nc->getsocket() << ")" ;
std::cout << ".\n";
// Create new thread
pthread_t t;
pthread_attr_t t_attr;
if (pthread_attr_init(&t_attr) != 0) {
nc->disconnect();
delete nc;
throw Error(ERR_THREAD, (string) "Unable to init thread attr: " + strerror(errno));
}
// Set thread detachable. This mean it free ressource when its
// execution is terminated. It's very important !
if (pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED) != 0) {
pthread_attr_destroy(&t_attr);
nc->disconnect();
delete nc;
throw Error(ERR_THREAD, (string) "Unable to set thread attr: " + strerror(errno));
}
if (pthread_create(&t, &t_attr, start_client, nc) != 0) {
pthread_attr_destroy(&t_attr);
nc->disconnect();
delete nc;
throw Error(ERR_THREAD, (string) "Unable to create thread: " + strerror(errno));
}
pthread_attr_destroy(&t_attr);
}
}
/*!
* Start server. Destroy socket
* Stop server.
*/
void Server::stop() {
pthread_mutex_lock(&threads_mutex_);
// close all open thread
for (Threadset::iterator it = threads_.begin(); it != threads_.end(); ++it)
pthread_cancel((*it));
// empty liste
threads_.clear();
pthread_mutex_unlock(&threads_mutex_);
// close connexion
conn_.disconnect();
// reset max_conn_
max_conn_ = 0;
}
/*!
* Server listen routine. This create new thead.
/**
* @return if server is started
*/
void Server::listen(size_t max_thread) {
assert(max_thread > 0);
bool Server::started() {
return conn_.connected();
}
/**
* Entry point for new thread that represent client machine
*
* @param vfd pointer to the server socket
*
* @return error code
*/
void *Server::start_client(void *voidconn) {
extern Server S;
assert(S.started());
const pthread_t t = pthread_self();
Connection *conn = (Connection *) voidconn;
assert(conn);
// add into thread set
pthread_mutex_lock(&S.threads_mutex_);
S.threads_.insert(t);
pthread_mutex_unlock(&S.threads_mutex_);
try {
Client *client = Client::login(*conn);
if (client != 0) {
client->exec();
delete client;
}
}
catch (const Error &e) {
std::cerr << "On connection " << conn->getremoteip() << " port "
<< conn->getremoteport() << ": " << e << ".\n";
}
// stop connexion
conn->disconnect();
// Print closing connection
std::cout << "Disconnected from ip " << conn->getremoteip()
<< " on port " << conn->getremoteport() << ".\n";
// remove from thread set
pthread_mutex_lock(&S.threads_mutex_);
S.threads_.erase(t);
pthread_mutex_unlock(&S.threads_mutex_);
return 0;
}
......@@ -19,28 +19,47 @@
#ifndef SERVER_HH
# define SERVER_HH
# include "connection.hh"
# include <set>
# include <pthread.h>
class Server {
public:
typedef std::set<string> stringset;
typedef std::set<string> Stringset;
typedef std::set<pthread_t> Threadset;
enum e_client {
CLI_DAEMON,
CLI_CONSOLE
};
public:
Server();
~Server();
void start();
void start(int port, size_t max_conn, bool verbose = false);
void stop();
void listen(size_t max_thread);
bool started() const;
bool started();
e_client login();
static void *start_client(void *);
private:
stringset logged_hosts_;
stringset logged_users_;
Stringset logged_hosts_;
Stringset logged_users_;
int socket_;
size_t max_thread_;
size_t thread_count_;
Threadset threads_;
pthread_mutex_t threads_mutex_;
Connection conn_;
size_t max_conn_;
bool verbose_;
};
void *start_client(void *fd);
#endif
......@@ -76,6 +76,8 @@ int main(int argc, char *argv[])
O.print(std::cout);
}
// FIXME: Signals catchers
// Open DB
D.open(O.database);
......@@ -83,8 +85,7 @@ int main(int argc, char *argv[])
C.start();
// start network
S.start();
S.listen(O.maxconn);
S.start(O.port, O.maxconn, O.verbose);
// stop scheduler
C.stop();
......@@ -95,6 +96,9 @@ int main(int argc, char *argv[])
return ERR_NO;
}
catch (const Error &e) {
// Stop cron
C.stop();
// Close server if started
if (S.started())
S.stop();
......
......@@ -42,6 +42,8 @@ enum {
ERR_PROTO = 5,
ERR_AUTH = 6,
ERR_DB = 7,
ERR_SRV = 8,
ERR_THREAD = 9,
ERR_NOMEM = 41,
ERR_UNKNOWN = 42
};
......@@ -50,8 +52,7 @@ enum {
// Gonstant
// -----------------------------------------------------------------------------
static const string VERSION = (string) "Testing.\nBuild at " + __DATE__ + " " + __TIME__ + ".";
static const string VERSION = (string) "testing.\nBuild: " + __DATE__ + " " + __TIME__ + ".";
//static const string VERSION = "1.0";
static const int MAX_LINE_SIZE = 512;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment