summaryrefslogtreecommitdiff
path: root/libcult/cult/os/net/ipv4/address.hxx
blob: 9168507f31a1f105cd7f9afa320fb20399844849 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// file      : cult/os/net/ipv4/address.hxx
// author    : Boris Kolpackov <boris@kolpackov.Net>
// copyright : Copyright (c) 2005-2010 Boris Kolpackov
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef CULT_OS_NET_IPV4_ADDRESS_HXX
#define CULT_OS_NET_IPV4_ADDRESS_HXX

#include <cult/types.hxx>

#include <cult/os/net/address.hxx>

#include <netinet/in.h> // IPv4 types (sockaddr_in, etc)
#include <arpa/inet.h>  // hto{n,h}{s,l}, iNet_pton

#include <iosfwd>
#include <cstring> // memset

namespace Cult
{
  namespace OS
  {
    namespace Net
    {
      namespace IPv4
      {
        class Address: public Net::Address
        {
        public:
          Address ()
          {
            std::memset (&addr_, 0, sizeof (addr_));
          }

          Address (sockaddr_in const& addr)
          {
            if (addr.sin_family != AF_INET)
              throw Invalid ();

            std::memset (&addr_, 0, sizeof (addr_));

            addr_.sin_family = AF_INET;
            addr_.sin_addr.s_addr = addr.sin_addr.s_addr;
            addr_.sin_port = addr.sin_port;
          }

          Address (in_addr_t host_addr, in_port_t host_port)
          {
            std::memset (&addr_, 0, sizeof (addr_));

            addr_.sin_family = AF_INET;
            addr_.sin_addr.s_addr = htonl (host_addr);
            addr_.sin_port = htons (host_port);
          }

          Address (String const& host_addr, in_port_t host_port)
          {
            std::memset (&addr_, 0, sizeof (addr_));

            addr_.sin_family = AF_INET;
            addr_.sin_port = htons (host_port);

            if (inet_pton (AF_INET, host_addr.c_str (), &addr_.sin_addr) <= 0)
              throw Invalid ();
          }

        public:
          virtual sa_family_t
          familiy () const
          {
            return AF_INET;
          }

          virtual sockaddr const*
          raw_addr () const
          {
            return reinterpret_cast<sockaddr const*> (&addr_);
          }

          virtual Size
          raw_size () const
          {
            return sizeof (addr_);
          }

        public:
          sockaddr_in const&
          addr () const
          {
            return addr_;
          }

	  in_addr_t
	  ip () const
	  {
	    return ntohl (addr_.sin_addr.s_addr);
	  }

	  in_port_t
	  port () const
	  {
	    return ntohs (addr_.sin_port);
	  }

        public:
          friend
          Boolean
          operator< (Address const& x, Address const& y)
          {
            return (x.addr_.sin_addr.s_addr < y.addr_.sin_addr.s_addr) ||
              ((x.addr_.sin_addr.s_addr == y.addr_.sin_addr.s_addr) &&
               (x.addr_.sin_port < y.addr_.sin_port));
          }

          friend
          Boolean
          operator== (Address const& x, Address const& y)
          {
            return (x.addr_.sin_addr.s_addr == y.addr_.sin_addr.s_addr) &&
              (x.addr_.sin_port == y.addr_.sin_port);
          }

          friend
          Boolean
          operator!= (Address const& x, Address const& y)
          {
            return !(x == y);
          }

          friend
          std::ostream&
          operator<< (std::ostream&, Address const&);

        private:
          sockaddr_in addr_;
        };
      }
    }
  }
}


#endif  // CULT_OS_NET_IPV4_ADDRESS_HXX