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

Unified Diff: net/base/address_list.cc

Issue 6598040: Add constructor for creating an AddressList from a struct sockaddr. This is... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/address_list.h ('k') | net/base/address_list_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/address_list.cc
===================================================================
--- net/base/address_list.cc (revision 76440)
+++ net/base/address_list.cc (working copy)
@@ -219,11 +219,55 @@
}
const struct addrinfo* AddressList::head() const {
+ if (!data_)
+ return NULL;
return data_->head;
}
AddressList::AddressList(Data* data) : data_(data) {}
+// static
+AddressList* AddressList::CreateAddressListFromSockaddr(
+ const struct sockaddr* address,
+ socklen_t address_length,
+ int socket_type,
+ int protocol) {
+ // Do sanity checking on socket_type and protocol.
+ DCHECK(socket_type == SOCK_DGRAM || socket_type == SOCK_STREAM);
+ DCHECK(protocol == IPPROTO_TCP || protocol == IPPROTO_UDP);
+
+ struct addrinfo* ai = new addrinfo;
+ memset(ai, 0, sizeof(addrinfo));
+ switch (address_length) {
+ case sizeof(struct sockaddr_in):
+ {
+ const struct sockaddr_in* sin =
+ reinterpret_cast<const struct sockaddr_in*>(address);
+ ai->ai_family = sin->sin_family;
+ DCHECK_EQ(AF_INET, ai->ai_family);
+ }
+ break;
+ case sizeof(struct sockaddr_in6):
+ {
+ const struct sockaddr_in6* sin6 =
+ reinterpret_cast<const struct sockaddr_in6*>(address);
+ ai->ai_family = sin6->sin6_family;
+ DCHECK_EQ(AF_INET6, ai->ai_family);
+ }
+ break;
+ default:
+ NOTREACHED() << "Bad IP address";
+ break;
+ }
+ ai->ai_socktype = socket_type;
+ ai->ai_protocol = protocol;
+ ai->ai_addrlen = address_length;
+ ai->ai_addr = reinterpret_cast<struct sockaddr*>(new char[address_length]);
+ memcpy(ai->ai_addr, address, address_length);
+ return new AddressList(new Data(ai, false /*is_system_created*/));
+}
+
+
AddressList::Data::Data(struct addrinfo* ai, bool is_system_created)
: head(ai), is_system_created(is_system_created) {
DCHECK(head);
« 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