Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_BASE_ADDRESS_LIST_H_ | 5 #ifndef NET_BASE_ADDRESS_LIST_H_ |
| 6 #define NET_BASE_ADDRESS_LIST_H_ | 6 #define NET_BASE_ADDRESS_LIST_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | |
| 10 | 11 |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/basictypes.h" |
| 13 #include "net/base/ip_endpoint.h" | |
| 12 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
| 13 #include "net/base/net_util.h" | 15 #include "net/base/net_util.h" |
| 14 | 16 |
| 15 struct addrinfo; | 17 struct addrinfo; |
| 16 | 18 |
| 17 namespace net { | 19 namespace net { |
| 18 | 20 |
| 19 // An AddressList object contains a linked list of addrinfo structures. This | 21 class NET_EXPORT AddressList : private std::vector<IPEndPoint> { |
| 20 // class is designed to be copied around by value. | |
| 21 class NET_EXPORT AddressList { | |
| 22 public: | 22 public: |
| 23 // Constructs an invalid address list. Should not call any methods on this | |
| 24 // other than assignment. | |
| 25 AddressList(); | 23 AddressList(); |
| 24 ~AddressList(); | |
| 26 | 25 |
| 27 AddressList(const AddressList& addresslist); | 26 // Creates an address list for a single IP literal. |
| 28 ~AddressList(); | 27 explicit AddressList(const IPEndPoint& endpoint); |
| 29 AddressList& operator=(const AddressList& addresslist); | |
| 30 | 28 |
| 31 // Creates an address list for a list of IP literals. | 29 static AddressList CreateFromIPAddress(const IPAddressNumber& address, |
| 30 uint16 port); | |
| 31 | |
| 32 static AddressList CreateFromIPAddressList( | 32 static AddressList CreateFromIPAddressList( |
| 33 const IPAddressList& addresses, | 33 const IPAddressList& addresses, |
| 34 const std::string& canonical_name); | 34 const std::string& canonical_name); |
| 35 | 35 |
| 36 // Creates an address list for a single IP literal. | 36 // Copies the data from |head| and the chained list into an AddressList. |
| 37 static AddressList CreateFromIPAddress( | 37 static AddressList CreateFromAddrinfo(const struct addrinfo* head); |
| 38 const IPAddressNumber& address, | |
| 39 uint16 port); | |
| 40 | 38 |
| 41 // Creates an address list for a single IP literal. If | 39 // TODO(szym): Remove all three. http://crbug.com/126134 |
| 42 // |canonicalize_name| is true, fill the ai_canonname field with the | 40 const std::string& get_canonical_name() const { |
| 43 // canonicalized IP address. | 41 return canonical_name_; |
| 44 static AddressList CreateFromIPAddressWithCname( | 42 } |
| 45 const IPAddressNumber& address, | |
| 46 uint16 port, | |
| 47 bool canonicalize_name); | |
| 48 | 43 |
| 49 // Adopts the given addrinfo list (assumed to have been created by | 44 void set_canonical_name(const std::string& canonical_name) { |
| 50 // the system, e.g. returned by getaddrinfo()) in place of the | 45 canonical_name_ = canonical_name; |
| 51 // existing one if any. This hands over responsibility for freeing | 46 } |
| 52 // the addrinfo list to the AddressList object. | |
| 53 static AddressList CreateByAdoptingFromSystem(struct addrinfo* head); | |
| 54 | 47 |
| 55 // Creates a new address list with a copy of |head|. This includes the | 48 // Sets canonical name to the literal of the first IP address on the list. |
| 56 // entire linked list. | 49 void SetDefaultCanonicalName(); |
| 57 static AddressList CreateByCopying(const struct addrinfo* head); | |
| 58 | 50 |
| 59 // Creates a new address list wich has a single address, |head|. If there | 51 // Exposed methods from std::vector. |
| 60 // are other addresses in |head| they will be ignored. | 52 using std::vector<IPEndPoint>::size; |
| 61 static AddressList CreateByCopyingFirstAddress(const struct addrinfo* head); | 53 using std::vector<IPEndPoint>::empty; |
| 62 | 54 using std::vector<IPEndPoint>::clear; |
| 63 // Creates an address list for a single socket address. | 55 using std::vector<IPEndPoint>::operator[]; |
| 64 // |address| the sockaddr to copy. | 56 using std::vector<IPEndPoint>::front; |
| 65 // |socket_type| is either SOCK_STREAM or SOCK_DGRAM. | 57 using std::vector<IPEndPoint>::push_back; |
| 66 // |protocol| is either IPPROTO_TCP or IPPROTO_UDP. | 58 using std::vector<IPEndPoint>::iterator; |
| 67 static AddressList CreateFromSockaddr( | 59 using std::vector<IPEndPoint>::const_iterator; |
| 68 const struct sockaddr* address, | 60 using std::vector<IPEndPoint>::begin; |
| 69 socklen_t address_length, | 61 using std::vector<IPEndPoint>::end; |
|
eroman
2012/05/04 19:37:36
What selection criteria did you use for these?
For
szym
2012/05/04 20:20:21
Whatever was already in use (needed to compile).
| |
| 70 int socket_type, | |
| 71 int protocol); | |
| 72 | |
| 73 // Appends a copy of |head| and all its linked addrinfos to the stored | |
| 74 // addrinfo. Note that this will cause a reallocation of the linked list, | |
| 75 // which invalidates the head pointer. | |
| 76 void Append(const struct addrinfo* head); | |
| 77 | |
| 78 // Sets the port of all addresses in the list to |port| (that is the | |
| 79 // sin[6]_port field for the sockaddrs). Note that this will cause a | |
| 80 // reallocation of the linked list, which invalidates the head pointer. | |
| 81 void SetPort(uint16 port); | |
| 82 | |
| 83 // Retrieves the port number of the first sockaddr in the list. (If SetPort() | |
| 84 // was previously used on this list, then all the addresses will have this | |
| 85 // same port number.) | |
| 86 uint16 GetPort() const; | |
| 87 | |
| 88 // Gets the canonical name for the address. | |
| 89 // If the canonical name exists, |*canonical_name| is filled in with the | |
| 90 // value and true is returned. If it does not exist, |*canonical_name| is | |
| 91 // not altered and false is returned. | |
| 92 // |canonical_name| must be a non-null value. | |
| 93 bool GetCanonicalName(std::string* canonical_name) const; | |
| 94 | |
| 95 // Gets access to the head of the addrinfo list. | |
| 96 // | |
| 97 // IMPORTANT: Callers SHOULD NOT mutate the addrinfo chain, since under the | |
| 98 // hood this data might be shared by other AddressLists, which | |
| 99 // might even be running on other threads. | |
| 100 // | |
| 101 // Additionally, non-const methods like SetPort() and Append() can | |
| 102 // cause the head to be reallocated, so do not cache the return | |
| 103 // value of head() across such calls. | |
| 104 const struct addrinfo* head() const; | |
| 105 | 62 |
| 106 private: | 63 private: |
| 107 struct Data; | 64 // TODO(szym): Remove. http://crbug.com/126134 |
| 108 | 65 std::string canonical_name_; |
| 109 explicit AddressList(Data* data); | |
| 110 | |
| 111 scoped_refptr<Data> data_; | |
| 112 }; | 66 }; |
| 113 | 67 |
| 114 // Helper to create an AddressList that has a particular port. It has an | 68 // Sets the port on each element in |list| to |port|. |
| 115 // optimization to avoid allocating a new address linked list when the | 69 void NET_EXPORT SetPortOnAddressList(uint16 port, AddressList* list); |
| 116 // port is already what we want. | |
| 117 AddressList NET_EXPORT CreateAddressListUsingPort(const AddressList& src, | |
| 118 int port); | |
| 119 | 70 |
| 120 } // namespace net | 71 } // namespace net |
| 121 | 72 |
| 122 #endif // NET_BASE_ADDRESS_LIST_H_ | 73 #endif // NET_BASE_ADDRESS_LIST_H_ |
| OLD | NEW |