Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: net/base/address_list.cc

Issue 3115014: Revert 56384 - Don't resolve IP literals.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/address_list.h ('k') | net/base/address_list_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 #include "net/base/address_list.h" 5 #include "net/base/address_list.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "net/base/net_util.h" 10 #include "net/base/net_util.h"
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 if (port_field) 78 if (port_field)
79 *port_field = htons(port); 79 *port_field = htons(port);
80 80
81 // Assign recursively. 81 // Assign recursively.
82 if (info->ai_next) 82 if (info->ai_next)
83 SetPortRecursive(info->ai_next, port); 83 SetPortRecursive(info->ai_next, port);
84 } 84 }
85 85
86 } // namespace 86 } // namespace
87 87
88 AddressList::AddressList(const IPAddressNumber& address, int port,
89 bool canonicalize_name) {
90 struct addrinfo* ai = new addrinfo;
91 memset(ai, 0, sizeof(addrinfo));
92 ai->ai_socktype = SOCK_STREAM;
93
94 switch (address.size()) {
95 case 4: {
96 ai->ai_family = AF_INET;
97 const size_t sockaddr_in_size = sizeof(struct sockaddr_in);
98 ai->ai_addrlen = sockaddr_in_size;
99
100 struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(
101 new char[sockaddr_in_size]);
102 memset(addr, 0, sockaddr_in_size);
103 addr->sin_family = AF_INET;
104 memcpy(&addr->sin_addr, &address[0], 4);
105 ai->ai_addr = reinterpret_cast<struct sockaddr*>(addr);
106 break;
107 }
108 case 16: {
109 ai->ai_family = AF_INET6;
110 const size_t sockaddr_in6_size = sizeof(struct sockaddr_in6);
111 ai->ai_addrlen = sockaddr_in6_size;
112
113 struct sockaddr_in6* addr6 = reinterpret_cast<struct sockaddr_in6*>(
114 new char[sockaddr_in6_size]);
115 memset(addr6, 0, sockaddr_in6_size);
116 addr6->sin6_family = AF_INET6;
117 memcpy(&addr6->sin6_addr, &address[0], 16);
118 ai->ai_addr = reinterpret_cast<struct sockaddr*>(addr6);
119 break;
120 }
121 default: {
122 NOTREACHED() << "Bad IP address";
123 break;
124 }
125 }
126
127 if (canonicalize_name) {
128 std::string name = NetAddressToString(ai);
129 ai->ai_canonname = do_strdup(name.c_str());
130 }
131 data_ = new Data(ai, false /*is_system_created*/);
132 SetPort(port);
133 }
134
135 void AddressList::Adopt(struct addrinfo* head) { 88 void AddressList::Adopt(struct addrinfo* head) {
136 data_ = new Data(head, true /*is_system_created*/); 89 data_ = new Data(head, true /*is_system_created*/);
137 } 90 }
138 91
139 void AddressList::Copy(const struct addrinfo* head, bool recursive) { 92 void AddressList::Copy(const struct addrinfo* head, bool recursive) {
140 data_ = new Data(CreateCopyOfAddrinfo(head, recursive), 93 data_ = new Data(CreateCopyOfAddrinfo(head, recursive),
141 false /*is_system_created*/); 94 false /*is_system_created*/);
142 } 95 }
143 96
144 void AddressList::Append(const struct addrinfo* head) { 97 void AddressList::Append(const struct addrinfo* head) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 // Otherwise we need to make a copy in order to change the port number. 136 // Otherwise we need to make a copy in order to change the port number.
184 Copy(src.head(), true); 137 Copy(src.head(), true);
185 SetPort(port); 138 SetPort(port);
186 } 139 }
187 } 140 }
188 141
189 void AddressList::Reset() { 142 void AddressList::Reset() {
190 data_ = NULL; 143 data_ = NULL;
191 } 144 }
192 145
146 // static
147 AddressList AddressList::CreateIPv4Address(unsigned char data[4],
148 const std::string& canonical_name) {
149 struct addrinfo* ai = new addrinfo;
150 memset(ai, 0, sizeof(addrinfo));
151 ai->ai_family = AF_INET;
152 ai->ai_socktype = SOCK_STREAM;
153 const size_t sockaddr_in_size = sizeof(struct sockaddr_in);
154 ai->ai_addrlen = sockaddr_in_size;
155 if (!canonical_name.empty())
156 ai->ai_canonname = do_strdup(canonical_name.c_str());
157
158 struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(
159 new char[sockaddr_in_size]);
160 memset(addr, 0, sockaddr_in_size);
161 addr->sin_family = AF_INET;
162 memcpy(&addr->sin_addr, data, 4);
163 ai->ai_addr = reinterpret_cast<struct sockaddr*>(addr);
164
165 return AddressList(new Data(ai, false /*is_system_created*/));
166 }
167
168 // static
169 AddressList AddressList::CreateIPv6Address(unsigned char data[16],
170 const std::string& canonical_name) {
171 struct addrinfo* ai = new addrinfo;
172 memset(ai, 0, sizeof(addrinfo));
173 ai->ai_family = AF_INET6;
174 ai->ai_socktype = SOCK_STREAM;
175 const size_t sockaddr_in6_size = sizeof(struct sockaddr_in6);
176 ai->ai_addrlen = sockaddr_in6_size;
177 if (!canonical_name.empty())
178 ai->ai_canonname = do_strdup(canonical_name.c_str());
179
180 struct sockaddr_in6* addr6 = reinterpret_cast<struct sockaddr_in6*>(
181 new char[sockaddr_in6_size]);
182 memset(addr6, 0, sockaddr_in6_size);
183 addr6->sin6_family = AF_INET6;
184 memcpy(&addr6->sin6_addr, data, 16);
185 ai->ai_addr = reinterpret_cast<struct sockaddr*>(addr6);
186
187 return AddressList(new Data(ai, false /*is_system_created*/));
188 }
189
193 AddressList::Data::Data(struct addrinfo* ai, bool is_system_created) 190 AddressList::Data::Data(struct addrinfo* ai, bool is_system_created)
194 : head(ai), is_system_created(is_system_created) { 191 : head(ai), is_system_created(is_system_created) {
195 DCHECK(head); 192 DCHECK(head);
196 } 193 }
197 194
198 AddressList::Data::~Data() { 195 AddressList::Data::~Data() {
199 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who 196 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who
200 // created the data. 197 // created the data.
201 if (is_system_created) 198 if (is_system_created)
202 freeaddrinfo(head); 199 freeaddrinfo(head);
203 else 200 else
204 FreeMyAddrinfo(head); 201 FreeMyAddrinfo(head);
205 } 202 }
206 203
207 } // namespace net 204 } // namespace net
OLDNEW
« no previous file with comments | « net/base/address_list.h ('k') | net/base/address_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698