1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be
3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file.
4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef NET_BASE_ADDRESS_LIST_H_
6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define NET_BASE_ADDRESS_LIST_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <string>
10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
11ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/ref_counted.h"
123345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "net/base/net_util.h"
13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstruct addrinfo;
15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace net {
17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// An AddressList object contains a linked list of addrinfo structures.  This
19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// class is designed to be copied around by value.
20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass AddressList {
21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Constructs an empty address list.
233345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  AddressList();
24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
253345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Constructs an address list for a single IP literal.  If
263345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // |canonicalize_name| is true, fill the ai_canonname field with the
273345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // canonicalized IP address.
283345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  AddressList(const IPAddressNumber& address, int port, bool canonicalize_name);
293345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
303345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  AddressList(const AddressList& addresslist);
313345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  ~AddressList();
323345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  AddressList& operator=(const AddressList& addresslist);
333345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
343345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Adopt the given addrinfo list (assumed to have been created by
353345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // the system, e.g. returned by getaddrinfo()) in place of the
363345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // existing one if any.  This hands over responsibility for freeing
373345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // the addrinfo list to the AddressList object.
38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void Adopt(struct addrinfo* head);
39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Copies the given addrinfo rather than adopting it. If |recursive| is true,
41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // all linked struct addrinfos will be copied as well. Otherwise only the head
42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // will be copied, and the rest of linked entries will be ignored.
43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Copy(const struct addrinfo* head, bool recursive);
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Appends a copy of |head| and all its linked addrinfos to the stored
46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // addrinfo.
47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Append(const struct addrinfo* head);
48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Sets the port of all addresses in the list to |port| (that is the
50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // sin[6]_port field for the sockaddrs).
51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void SetPort(int port);
52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Retrieves the port number of the first sockaddr in the list. (If SetPort()
54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // was previously used on this list, then all the addresses will have this
55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // same port number.)
56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  int GetPort() const;
57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Sets the address to match |src|, and have each sockaddr's port be |port|.
59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // If |src| already has the desired port this operation is cheap (just adds
60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // a reference to |src|'s data.) Otherwise we will make a copy.
61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void SetFrom(const AddressList& src, int port);
62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Gets the canonical name for the address.
64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // If the canonical name exists, |*canonical_name| is filled in with the
65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // value and true is returned. If it does not exist, |*canonical_name| is
66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // not altered and false is returned.
67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // |canonical_name| must be a non-null value.
68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool GetCanonicalName(std::string* canonical_name) const;
69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Clears all data from this address list. This leaves the list in the same
71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // empty state as when first constructed.
72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void Reset();
73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Get access to the head of the addrinfo list.
753345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  const struct addrinfo* head() const;
76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
77dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Constructs an address list for a single socket address.
78dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // |address| the sockaddr to copy.
79dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // |socket_type| is either SOCK_STREAM or SOCK_DGRAM.
80dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // |protocol| is either IPPROTO_TCP or IPPROTO_UDP.
81dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  static AddressList* CreateAddressListFromSockaddr(
82dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      const struct sockaddr* address,
83dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      socklen_t address_length,
84dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      int socket_type,
85dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      int protocol);
86dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
883345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  struct Data;
89c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
903345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  explicit AddressList(Data* data);
91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
92c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  scoped_refptr<Data> data_;
93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
95c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}  // namespace net
96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif  // NET_BASE_ADDRESS_LIST_H_
98