0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
InetAddr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
28 #ifndef Hypertable_Common_InetAddr_h
29 #define Hypertable_Common_InetAddr_h
30 
31 #include <Common/String.h>
32 
33 #include <cstring>
34 
35 extern "C" {
36 #include <netinet/in.h>
37 }
38 
39 namespace Hypertable {
40 
44  struct Endpoint {
45  Endpoint(const String &host, uint16_t port)
46  : host(host), port(port) {
47  }
48 
50  : port(0) {
51  }
52 
54  uint16_t port;
55  };
56 
58  std::ostream &operator<<(std::ostream &, const Endpoint &);
59 
66  class InetAddr : public sockaddr_in {
67 
68  public:
69 
71  InetAddr();
72 
80  InetAddr(const String &host, uint16_t port);
81 
88  InetAddr(const String &endpoint);
89 
96  InetAddr(uint32_t ip32, uint16_t port);
97 
104  InetAddr(const sockaddr_in &addr) { operator=(addr); }
105 
107  InetAddr &operator=(const sockaddr_in &addr) {
108  if (this != &addr)
109  memcpy(this, &addr, sizeof(sockaddr_in));
110 
111  return *this;
112  }
113 
114  bool operator==(const InetAddr &other) const {
115  return (bool)!memcmp(this, &other, sizeof(InetAddr));
116  }
117 
118  bool operator!=(const InetAddr &other) const {
119  return !(*this == other);
120  }
121 
122  bool operator<(const InetAddr &other) const {
123  if (sin_family != other.sin_family)
124  return sin_family < other.sin_family;
125  if (sin_addr.s_addr != other.sin_addr.s_addr)
126  return sin_addr.s_addr < other.sin_addr.s_addr;
127  return sin_port < other.sin_port;
128  }
129 
132  String format(int sep = ':') const { return InetAddr::format(*this, sep); }
133 
137 
140  String hex(int sep = ':') { return InetAddr::hex(*this, sep); }
141 
142  // convenient/legacy static methods
150  static bool initialize(sockaddr_in *addr, const char *host, uint16_t port);
151 
158  static bool initialize(sockaddr_in *addr, const char *addr_str);
159 
167  static bool initialize(sockaddr_in *addr, uint32_t haddr, uint16_t port);
168 
176  static Endpoint parse_endpoint(const char *endpoint, int defport = 0);
177 
185  static Endpoint parse_endpoint(const String &endpoint, int defport = 0) {
186  return parse_endpoint(endpoint.c_str(), defport);
187  }
188 
198  static bool parse_ipv4(const char *ip, uint16_t port, sockaddr_in &addr,
199  int base = 0);
200 
206  static bool is_ipv4(const char *ip);
207 
214  static String format(const sockaddr_in &addr, int sep = ':');
215 
221  static String format_ipaddress(const sockaddr_in &addr);
222 
230  static String hex(const sockaddr_in &addr, int sep = ':');
231 
236  size_t encoded_length() const;
237 
244  void encode(uint8_t **bufp) const;
245 
252  void decode(const uint8_t **bufp, size_t *remainp);
253 
255  void legacy_decode(const uint8_t **bufp, size_t *remainp);
256 
257  private:
258 
261  uint8_t encoding_version() const;
262 
268  size_t encoded_length_internal() const;
269 
274  void encode_internal(uint8_t **bufp) const;
275 
283  void decode_internal(uint8_t version, const uint8_t **bufp, size_t *remainp);
284 
285 
286  };
287 
290  std::ostream &operator<<(std::ostream &, const sockaddr_in &);
291 
294 }
295 
296 #endif // Hypertable_Common_InetAddr_h
String format_ipaddress()
Returns a string with a dotted notation ("127.0.0.1") without! the port.
Definition: InetAddr.h:136
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
static Endpoint parse_endpoint(const char *endpoint, int defport=0)
Parse an endpoint string in (host:port) format.
Definition: InetAddr.cc:181
void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: InetAddr.cc:261
void decode_internal(uint8_t version, const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: InetAddr.cc:311
bool operator<(const InetAddr &other) const
Definition: InetAddr.h:122
static Endpoint parse_endpoint(const String &endpoint, int defport=0)
Parse an endpoint string in (host:port) format.
Definition: InetAddr.h:185
String hex(int sep= ':')
Returns a compact String representation ("0x2387646:80") including the port.
Definition: InetAddr.h:140
uint8_t encoding_version() const
Returns encoding version.
Definition: InetAddr.cc:284
Encapsulate an internet address.
Definition: InetAddr.h:66
static bool is_ipv4(const char *ip)
Tests whether the input string in n.n.n.n format (base 10)
Definition: InetAddr.cc:113
InetAddr()
Constructor creates an empty internet address.
Definition: InetAddr.cc:48
std::ostream & operator<<(std::ostream &os, const crontab_entry &entry)
Helper function to write crontab_entry to an ostream.
Definition: Crontab.cc:301
void encode(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
Definition: InetAddr.cc:255
String format(int sep= ':') const
Returns a string with a dotted notation ("127.0.0.1:8080") including the port.
Definition: InetAddr.h:132
bool operator==(const InetAddr &other) const
Definition: InetAddr.h:114
Hypertable definitions
static bool initialize(sockaddr_in *addr, const char *host, uint16_t port)
Initialize a sockaddr_in structure from host:port.
Definition: InetAddr.cc:68
Endpoint(const String &host, uint16_t port)
Definition: InetAddr.h:45
InetAddr & operator=(const sockaddr_in &addr)
Assigns a unix sockaddr_in address structure to this object.
Definition: InetAddr.h:107
size_t encoded_length_internal() const
Returns internal serialized length.
Definition: InetAddr.cc:288
size_t encoded_length() const
Returns serialized object length.
Definition: InetAddr.cc:228
void legacy_decode(const uint8_t **bufp, size_t *remainp)
Deserializes object from legacy serialization format.
Definition: InetAddr.cc:277
void encode_internal(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
Definition: InetAddr.cc:304
A String class based on std::string.
static bool parse_ipv4(const char *ip, uint16_t port, sockaddr_in &addr, int base=0)
Parses an ipv4 address string in dotted notiation ("n.n.n.n") or as a number and initializes a sockad...
Definition: InetAddr.cc:135
InetAddr(const sockaddr_in &addr)
Constructor creates an internet address from a unix sockaddr_in structure.
Definition: InetAddr.h:104
bool operator!=(const InetAddr &other) const
Definition: InetAddr.h:118
High-level entry point to a service; wraps a host:port pair.
Definition: InetAddr.h:44