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

Unified Diff: runtime/bin/socket.h

Issue 14469002: Add new InternetAddress class with a static lookup function (including IPv6 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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 | « runtime/bin/secure_socket.cc ('k') | runtime/bin/socket.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/socket.h
diff --git a/runtime/bin/socket.h b/runtime/bin/socket.h
index b68b782e70f883bfe65f3bd965b72cc9eb244f3a..84a8aa67ad264c1437699ee1ed79495cbaa573dc 100644
--- a/runtime/bin/socket.h
+++ b/runtime/bin/socket.h
@@ -24,6 +24,91 @@
#endif
+union RawAddr {
+ // ss_family is always at ofset 0, thus we can add it here.
+ uint16_t ss_family;
+ struct sockaddr_in in;
+ struct sockaddr_in6 in6;
+ struct sockaddr addr;
+};
+
+class SocketAddress {
+ public:
+ enum {
+ TYPE_ANY = -1,
+ TYPE_IPV4,
+ TYPE_IPV6,
+ };
+
+ explicit SocketAddress(struct addrinfo* addrinfo);
+
+ int GetType() {
+ if (addr_.ss_family == AF_INET6) return TYPE_IPV6;
+ return TYPE_IPV4;
+ }
+
+ const char* as_string() const { return as_string_; }
+ const RawAddr& addr() const { return addr_; }
+
+ static intptr_t GetAddrLength(const RawAddr& addr) {
+ return addr.ss_family == AF_INET6 ?
+ sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
+ }
+
+ static int16_t FromType(int type) {
+ if (type == TYPE_ANY) return AF_UNSPEC;
+ if (type == TYPE_IPV4) return AF_INET;
+ ASSERT(type == TYPE_IPV6 && "Invalid type");
+ return AF_INET6;
+ }
+
+ static void SetAddrPort(RawAddr* addr, intptr_t port) {
+ if (addr->ss_family == AF_INET) {
+ addr->in.sin_port = htons(port);
+ } else {
+ addr->in6.sin6_port = htons(port);
+ }
+ }
+
+ static intptr_t GetAddrPort(RawAddr* addr) {
+ if (addr->ss_family == AF_INET) {
+ return ntohs(addr->in.sin_port);
+ } else {
+ return ntohs(addr->in6.sin6_port);
+ }
+ }
+
+ private:
+ char as_string_[INET6_ADDRSTRLEN];
+ RawAddr addr_;
+
+ DISALLOW_COPY_AND_ASSIGN(SocketAddress);
+};
+
+class SocketAddresses {
+ public:
+ explicit SocketAddresses(intptr_t count)
+ : count_(count),
+ addresses_(new SocketAddress*[count_]) {}
+
+ ~SocketAddresses() {
+ for (intptr_t i = 0; i < count_; i++) {
+ delete addresses_[i];
+ }
+ delete[] addresses_;
+ }
+
+ intptr_t count() const { return count_; }
+ SocketAddress* GetAt(intptr_t i) const { return addresses_[i]; }
+ void SetAt(intptr_t i, SocketAddress* addr) { addresses_[i] = addr; }
+
+ private:
+ const intptr_t count_;
+ SocketAddress** addresses_;
+
+ DISALLOW_COPY_AND_ASSIGN(SocketAddresses);
+};
+
class Socket {
public:
enum SocketRequest {
@@ -34,7 +119,8 @@ class Socket {
static intptr_t Available(intptr_t fd);
static int Read(intptr_t fd, void* buffer, intptr_t num_bytes);
static int Write(intptr_t fd, const void* buffer, intptr_t num_bytes);
- static intptr_t CreateConnect(const char* host, const intptr_t port);
+ static intptr_t CreateConnect(RawAddr addr,
+ const intptr_t port);
static intptr_t GetPort(intptr_t fd);
static bool GetRemotePeer(intptr_t fd, char* host, intptr_t* port);
static void GetError(intptr_t fd, OSError* os_error);
@@ -45,9 +131,10 @@ class Socket {
static bool SetBlocking(intptr_t fd);
static bool SetNoDelay(intptr_t fd, bool enabled);
- // Perform a IPv4 hostname lookup. Returns the hostname string in
- // IPv4 dotted-decimal format.
- static const char* LookupIPv4Address(char* host, OSError** os_error);
+ // Perform a hostname lookup. Returns the SocketAddresses.
+ static SocketAddresses* LookupAddress(const char* host,
+ int type,
+ OSError** os_error);
static Dart_Port GetServicePort();
@@ -76,7 +163,7 @@ class ServerSocket {
//
// -1: system error (errno set)
// -5: invalid bindAddress
- static intptr_t CreateBindListen(const char* bindAddress,
+ static intptr_t CreateBindListen(RawAddr addr,
intptr_t port,
intptr_t backlog);
« no previous file with comments | « runtime/bin/secure_socket.cc ('k') | runtime/bin/socket.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698