From 87e2e08ed7b47f30e458736dbfbb1ede75349d40 Mon Sep 17 00:00:00 2001 From: haller Date: Mon, 28 Nov 2005 00:55:32 +0000 Subject: Deux trois modif sur l'A/A Bidouillage des socket, ca compile mais c'est tout --- i/simulotron/src/Makefile.am | 4 +- i/simulotron/src/socket/address.cc | 93 +++++++++++++++++++++++++++++++ i/simulotron/src/socket/address.hh | 76 +++++++++++++++++++++++++ i/simulotron/src/socket/socket.cc | 21 +++++-- i/simulotron/src/socket/socket.hh | 10 ++-- i/simulotron/src/utils/errno_exception.hh | 48 ++++++++++++++++ 6 files changed, 241 insertions(+), 11 deletions(-) create mode 100644 i/simulotron/src/socket/address.cc create mode 100644 i/simulotron/src/socket/address.hh create mode 100644 i/simulotron/src/utils/errno_exception.hh (limited to 'i/simulotron/src') diff --git a/i/simulotron/src/Makefile.am b/i/simulotron/src/Makefile.am index 2970e77..7302a68 100644 --- a/i/simulotron/src/Makefile.am +++ b/i/simulotron/src/Makefile.am @@ -1,6 +1,8 @@ bin_PROGRAMS = simulotron simulotron_SOURCES = main.cc \ - socket/socket.cc socket/socket.hh + socket/socket.cc socket/socket.hh \ + socket/address.cc socket/address.hh \ + utils/errno_exception.hh simulotron_CXXFLAGS = -g -Wall -fmessage-length=0 simulotron_LDADD = diff --git a/i/simulotron/src/socket/address.cc b/i/simulotron/src/socket/address.cc new file mode 100644 index 0000000..4567e2e --- /dev/null +++ b/i/simulotron/src/socket/address.cc @@ -0,0 +1,93 @@ +// address.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// This program 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; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: %WEB% +// Email: +// }}} +#include "address.hh" + +#include +#include + +/// Constructeur par défaut. +Address::Address (void) + : port_ (0) +{ + resolv (); +} + +/// Constructeur avec le port (serveur) +Address::Address (const int port) + : port_ (port) +{ + resolv (); +} +/// Constructeur avec hôte et port. +Address::Address (const std::string &host, const int port) + : host_ (host), port_ (port) +{ + resolv (); +} + +/// Constructeur à partir de la strcuture d'adresse. +Address::Address (const sockaddr *sa, socklen_t sl) +{ + // On vérifie que c'est vraiment un struct sockaddr_in + if (sl != sizeof (sockaddr_in)) + throw std::invalid_argument ("Invalide Adresse"); + sa_ = * reinterpret_cast (sa); +} + +/// Résolution de la structure d'adresse depuis l'hôte/port +void +Address::resolv (void) +{ + // Cas du serveur, pas d'hôte + if (host_.empty ()) + sa_.sin_addr.s_addr = htonl (INADDR_ANY); + else + { + // Convertion de l'hôte en structure adresse + if (!inet_pton (AF_INET, host_.c_str (), &sa_.sin_addr)) + { + hostent *host_info = gethostbyname (host_.c_str ()); + if (!host_info) + throw std::runtime_error (host_ + " : Hôte inconnue"); + } + } + sa_.sin_family = AF_INET; + // Convertion pour le passage sur le réseau + sa_.sin_port = htons (port_); +} + +/// Résolution de l'hôte/port depuis la structure d'adresse +void +Address::unresolv (void) +{ + hostent *host_info; + // Recherche de l'hôte + host_info = gethostbyaddr (reinterpret_cast (&(sa_.sin_addr)), sizeof sa_.sin_addr, AF_INET); + if (!host_info) + host_ = inet_ntoa (sa_.sin_addr); + else + host_ = host_info->h_name; + // Recherche du port + port_ = ntohs (sa_.sin_port); +} diff --git a/i/simulotron/src/socket/address.hh b/i/simulotron/src/socket/address.hh new file mode 100644 index 0000000..53fca07 --- /dev/null +++ b/i/simulotron/src/socket/address.hh @@ -0,0 +1,76 @@ +#ifndef address_hh +#define address_hh +// address.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Dufour Jérémy +// +// This program 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; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include +#include +#include +#include + +#include +#include + +/// S'occupe de gérer les adresses. +class Address { + /// La structure sockaddr_in de type réseau/AF_INET. + sockaddr_in sa_; + /// Le nom d'hôte de l'adresse. + std::string host_; + /// Le numéro de port de l'adresse. + int port_; + + public: + /// Constructeur par défaut. + Address (void); + /// Constructeur pour le serveur. (port seulement) + Address (const int port); + /// Constructeur avec hôte et port. + Address (const std::string &host, const int port); + /// Constructeur à partir de la structure d'adresse. + Address (const sockaddr *sa, socklen_t sl); + /// Geter sockaddr. + const sockaddr *getSockaddr (void) const { return + reinterpret_cast (&sa_); } + /// Geter hôte. + const std::string &getHost (void) const { return host_; } + /// Geter port. + const int getPort (void) const { return port_; } + + private: + /// Résolution adresse vers hôte/port + void resolv (void); + /// Résolution hôte/port vers adresse + void unresolv (void); + +}; + +/// Surcharge de l'opérateur << pour l'affichage. +inline std::ostream & +operator<< (std::ostream &s, const Address &a) +{ + return s << a.getHost () << ':' << a.getPort (); +} + +#endif // address_hh diff --git a/i/simulotron/src/socket/socket.cc b/i/simulotron/src/socket/socket.cc index 2f7139c..f231cf5 100644 --- a/i/simulotron/src/socket/socket.cc +++ b/i/simulotron/src/socket/socket.cc @@ -1,6 +1,10 @@ #include +#include +#include "socket/socket.hh" +#include "utils/errno_exception.hh" +#include "socket/address.hh" -Socket::Socket(const std::string & address, int port, bool block) +Socket::Socket(const Address & address) { // Crée le file descriptor du socket socket_ = socket (PF_INET, SOCK_STREAM, 0); @@ -8,19 +12,24 @@ Socket::Socket(const std::string & address, int port, bool block) throw errno_exception ("La chaussette n'a pas pu être tricoté", errno); // On bind le socket - if(bind (socket_, address.getSockaddr(), sizeof(*address.getSockaddr())) < 0) + if(::bind (socket_, address.getSockaddr(), sizeof(*address.getSockaddr())) < 0) throw errno_exception("Impossible d'assigner la chausette", errno); } +Socket::Socket(int socket) +{ + socket_ = socket; +} + void Socket::connect(const Address & address) { - if (connect (socket_, address.getSockaddr(), sizeof(*address.getSockaddr()) < 0)) + if (::connect (socket_, address.getSockaddr(), sizeof(*address.getSockaddr()) < 0)) throw errno_exception("Impossible de mettre la chaussette", errno); } void Socket::listen(int maxQueue) { - if( listen(socket_, maxQueue) < 0 ) + if(::listen(socket_, maxQueue) < 0 ) throw errno_exception("Impossible d'écouter la mer dans la chaussette", errno); } @@ -29,11 +38,11 @@ Socket Socket::accept(Address & address) sockaddr addr; socklen_t length; - int socket = accept(socket_, &addr, &length); + int socket = ::accept(socket_, &addr, &length); if (socket < 0) throw errno_exception("Chaussette bloqué à la douane", errno); - address = Address(addr, length); + address = Address(&addr, length); return Socket(socket); } diff --git a/i/simulotron/src/socket/socket.hh b/i/simulotron/src/socket/socket.hh index c1b47df..27f3f5d 100644 --- a/i/simulotron/src/socket/socket.hh +++ b/i/simulotron/src/socket/socket.hh @@ -1,3 +1,5 @@ +class Address; + class Socket { private: @@ -5,15 +7,15 @@ class Socket int socket_; public: /// Constructeur - Socket(const Address & adress, bool block = false); + Socket(const Address & address); /// Constructeur à partir d'un fd Socket(int socket); /// Connect le client au serveur - void connect(void); + void connect(const Address & address); /// Ecoute le port - void listen(void); + void listen(int maxQueue); /// accepte une connection - void accept(void); + Socket accept(Address & adress); /// lit du socket void read(void); /// écrit dans le socket diff --git a/i/simulotron/src/utils/errno_exception.hh b/i/simulotron/src/utils/errno_exception.hh new file mode 100644 index 0000000..58a636e --- /dev/null +++ b/i/simulotron/src/utils/errno_exception.hh @@ -0,0 +1,48 @@ +#ifndef errno_exception_hh +#define errno_exception_hh +// errno_exception.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2005 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program 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; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} + +#include +#include +#include +#include + +/// Exception lancé lorsqu'une erreur est retournée par la libc. +class errno_exception : public std::exception +{ + int errno_; + std::string what_; + public: + errno_exception (const std::string &desc, int errno__) + : errno_ (errno__), what_ (desc + ": " + strerror (errno__)) { } + errno_exception (int errno__) + : errno_ (errno__), what_ (strerror (errno__)) { } + ~errno_exception (void) throw () { } + virtual const char* what () const throw () { return what_.c_str (); } + int getErrno (void) const { return errno_; } +}; + +#endif // errno_exception_hh -- cgit v1.2.3