summaryrefslogtreecommitdiff
path: root/i/marvin/src/socket/address.hh
blob: 53fca07d6a0b656c22225f2835258f4e566a1a04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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: <dufourj@efrei.fr>
// }}}

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <string>
#include <ostream>

/// 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<const sockaddr *> (&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