1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_BASE_IP_ENDPOINT_H_
6#define NET_BASE_IP_ENDPOINT_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "net/base/net_util.h"
11
12struct sockaddr;
13
14namespace net {
15
16// An IPEndPoint represents the address of a transport endpoint:
17//  * IP address (either v4 or v6)
18//  * Port
19class IPEndPoint {
20 public:
21  IPEndPoint();
22  virtual ~IPEndPoint();
23  IPEndPoint(const IPAddressNumber& address, int port);
24  IPEndPoint(const IPEndPoint& endpoint);
25
26  const IPAddressNumber& address() const { return address_; }
27  int port() const { return port_; }
28
29  // Returns AF_INET or AF_INET6 depending on the type of the address.
30  int GetFamily() const;
31
32  // Convert to a provided sockaddr struct.
33  // |address| is the sockaddr to copy into.  Should be at least
34  //    sizeof(struct sockaddr_storage) bytes.
35  // |address_length| is an input/output parameter.  On input, it is the
36  //    size of data in |address| available.  On output, it is the size of
37  //    the address that was copied into |address|.
38  // Returns true on success, false on failure.
39  bool ToSockAddr(struct sockaddr* address, size_t* address_length) const;
40
41  // Convert from a sockaddr struct.
42  // |address| is the address.
43  // |address_length| is the length of |address|.
44  // Returns true on success, false on failure.
45  bool FromSockAddr(const struct sockaddr* address, size_t address_length);
46
47  // Returns value as a string (e.g. "127.0.0.1:80"). Returns empty
48  // string if the address is invalid, and cannot not be converted to a
49  // string.
50  std::string ToString() const;
51
52  bool operator<(const IPEndPoint& that) const;
53  bool operator==(const IPEndPoint& that) const;
54
55 private:
56  IPAddressNumber address_;
57  int port_;
58};
59
60}  // namespace net
61
62#endif  // NET_BASE_IP_ENDPOINT_H_
63