• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ADDRESS_LIST_H_
6 #define NET_BASE_ADDRESS_LIST_H_
7 #pragma once
8 
9 #include <string>
10 
11 #include "base/memory/ref_counted.h"
12 #include "net/base/net_util.h"
13 
14 struct addrinfo;
15 
16 namespace net {
17 
18 // An AddressList object contains a linked list of addrinfo structures.  This
19 // class is designed to be copied around by value.
20 class AddressList {
21  public:
22   // Constructs an empty address list.
23   AddressList();
24 
25   // Constructs an address list for a single IP literal.  If
26   // |canonicalize_name| is true, fill the ai_canonname field with the
27   // canonicalized IP address.
28   AddressList(const IPAddressNumber& address, int port, bool canonicalize_name);
29 
30   AddressList(const AddressList& addresslist);
31   ~AddressList();
32   AddressList& operator=(const AddressList& addresslist);
33 
34   // Adopt the given addrinfo list (assumed to have been created by
35   // the system, e.g. returned by getaddrinfo()) in place of the
36   // existing one if any.  This hands over responsibility for freeing
37   // the addrinfo list to the AddressList object.
38   void Adopt(struct addrinfo* head);
39 
40   // Copies the given addrinfo rather than adopting it. If |recursive| is true,
41   // all linked struct addrinfos will be copied as well. Otherwise only the head
42   // will be copied, and the rest of linked entries will be ignored.
43   void Copy(const struct addrinfo* head, bool recursive);
44 
45   // Appends a copy of |head| and all its linked addrinfos to the stored
46   // addrinfo.
47   void Append(const struct addrinfo* head);
48 
49   // Sets the port of all addresses in the list to |port| (that is the
50   // sin[6]_port field for the sockaddrs).
51   void SetPort(int port);
52 
53   // Retrieves the port number of the first sockaddr in the list. (If SetPort()
54   // was previously used on this list, then all the addresses will have this
55   // same port number.)
56   int GetPort() const;
57 
58   // Sets the address to match |src|, and have each sockaddr's port be |port|.
59   // If |src| already has the desired port this operation is cheap (just adds
60   // a reference to |src|'s data.) Otherwise we will make a copy.
61   void SetFrom(const AddressList& src, int port);
62 
63   // Gets the canonical name for the address.
64   // If the canonical name exists, |*canonical_name| is filled in with the
65   // value and true is returned. If it does not exist, |*canonical_name| is
66   // not altered and false is returned.
67   // |canonical_name| must be a non-null value.
68   bool GetCanonicalName(std::string* canonical_name) const;
69 
70   // Clears all data from this address list. This leaves the list in the same
71   // empty state as when first constructed.
72   void Reset();
73 
74   // Get access to the head of the addrinfo list.
75   const struct addrinfo* head() const;
76 
77   // Constructs an address list for a single socket address.
78   // |address| the sockaddr to copy.
79   // |socket_type| is either SOCK_STREAM or SOCK_DGRAM.
80   // |protocol| is either IPPROTO_TCP or IPPROTO_UDP.
81   static AddressList* CreateAddressListFromSockaddr(
82       const struct sockaddr* address,
83       socklen_t address_length,
84       int socket_type,
85       int protocol);
86 
87  private:
88   struct Data;
89 
90   explicit AddressList(Data* data);
91 
92   scoped_refptr<Data> data_;
93 };
94 
95 }  // namespace net
96 
97 #endif  // NET_BASE_ADDRESS_LIST_H_
98