skstream
skaddress.h
1 /**************************************************************************
2  FreeSockets - Portable C++ classes for IP(sockets) applications. (v0.3)
3  Copyright (C) 2012 Alistair Riddoch
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 
19 **************************************************************************/
20 
28 #ifndef RGJ_FREE_ADDRESS_H_
29 #define RGJ_FREE_ADDRESS_H_
30 
31 #include <skstream/sksocket.h>
32 
33 #include <string>
34 
35 struct addrinfo;
36 
37 // I am making this inherit from basic_socket, even though it does not
38 // at this time appear to be a socket. This is so that it can ensure
39 // basic_socket::startup is called in the standard way, and so that if in
40 // future we need a custom, non blocking resolver, we can have one.
41 
42 class basic_address : public basic_socket {
43 protected:
44  struct addrinfo * _addrlist;
45 
46  int _type;
47  int _protocol;
48 
49  basic_address(int, int);
50 
51  int resolve(int, const char *, const char *);
52 
53  // FIXME some data structures for non-getaddrinfo legacy systems
54 public:
55  struct addrinfo * takeAddressInfo() {
56  struct addrinfo * t = _addrlist;
57  _addrlist = 0;
58  return t;
59  }
60 
61  virtual ~basic_address();
62 
64  bool isReady() const {
65  return _addrlist != 0;
66  }
67 
68  int resolveListener(const std::string & service);
69 
70  int resolveConnector(const std::string & host, const std::string & service);
71 
72  // FIXME - perhaps we could do this like an iterator, c++11 style
73 
75  std::size_t size() const;
76 
77  class const_iterator;
78 
79  const_iterator begin() const;
80  const_iterator end() const;
81 
83  struct addrinfo * getAddrinfo(std::size_t c) const;
84 
85  virtual SOCKET_TYPE getSocket() const;
86 
87 };
88 
90 private:
91  friend class basic_address;
92 
93  struct addrinfo * _info;
94 
95  explicit const_iterator(struct addrinfo * i) : _info(i) {
96  }
97 
98 public:
99  // FIXME Add move stuff (c++11)
100  const_iterator() : _info(0) {
101  }
102 
103  const_iterator(const const_iterator & rhs) : _info(rhs._info) {
104  }
105 
106  bool operator==(const const_iterator& rhs) {
107  return _info == rhs._info;
108  }
109 
110  bool operator!=(const const_iterator& rhs) {
111  return !this->operator==(rhs);
112  }
113 
114  const_iterator& operator=(const const_iterator& rhs) {
115  _info = rhs._info;
116  return *this;
117  }
118 
119  struct addrinfo * operator*() const {
120  return _info;
121  }
122 
123  const_iterator& operator++();
124 };
125 
126 inline basic_address::const_iterator basic_address::begin() const
127 {
128  return basic_address::const_iterator(_addrlist);
129 }
130 
131 inline basic_address::const_iterator basic_address::end() const
132 {
134 }
135 
136 class tcp_address : public basic_address {
137 public:
138  tcp_address();
139 
140 };
141 
143 public:
145 
146 };
147 
148 #endif // RGJ_FREE_SOCKET_H_
Definition: skaddress.h:42
Definition: skaddress.h:136
basic_address(int, int)
Definition: skaddress.cpp:44
struct addrinfo * getAddrinfo(std::size_t c) const
Get one of the resolved address info records.
Base class for anything that encapsulates a socket.
Definition: sksocket.h:79
std::size_t size() const
Check the number of network address resolved.
Definition: skaddress.cpp:93
Definition: skaddress.h:142
bool isReady() const
Check if an address has been resolved.
Definition: skaddress.h:64
Definition: skaddress.h:89