| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BIN_SOCKET_H_ | 5 #ifndef BIN_SOCKET_H_ |
| 6 #define BIN_SOCKET_H_ | 6 #define BIN_SOCKET_H_ |
| 7 | 7 |
| 8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
| 9 #include "bin/utils.h" | 9 #include "bin/utils.h" |
| 10 | 10 |
| 11 #include "platform/globals.h" | 11 #include "platform/globals.h" |
| 12 #include "platform/thread.h" | 12 #include "platform/thread.h" |
| 13 // Declare the OS-specific types ahead of defining the generic class. | 13 // Declare the OS-specific types ahead of defining the generic class. |
| 14 #if defined(TARGET_OS_ANDROID) | 14 #if defined(TARGET_OS_ANDROID) |
| 15 #include "bin/socket_android.h" | 15 #include "bin/socket_android.h" |
| 16 #elif defined(TARGET_OS_LINUX) | 16 #elif defined(TARGET_OS_LINUX) |
| 17 #include "bin/socket_linux.h" | 17 #include "bin/socket_linux.h" |
| 18 #elif defined(TARGET_OS_MACOS) | 18 #elif defined(TARGET_OS_MACOS) |
| 19 #include "bin/socket_macos.h" | 19 #include "bin/socket_macos.h" |
| 20 #elif defined(TARGET_OS_WINDOWS) | 20 #elif defined(TARGET_OS_WINDOWS) |
| 21 #include "bin/socket_win.h" | 21 #include "bin/socket_win.h" |
| 22 #else | 22 #else |
| 23 #error Unknown target os. | 23 #error Unknown target os. |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 | 26 |
| 27 union RawAddr { | 27 union RawAddr { |
| 28 // ss_family is always at ofset 0, thus we can add it here. | |
| 29 uint16_t ss_family; | |
| 30 struct sockaddr_in in; | 28 struct sockaddr_in in; |
| 31 struct sockaddr_in6 in6; | 29 struct sockaddr_in6 in6; |
| 30 struct sockaddr_storage ss; |
| 32 struct sockaddr addr; | 31 struct sockaddr addr; |
| 33 }; | 32 }; |
| 34 | 33 |
| 35 class SocketAddress { | 34 class SocketAddress { |
| 36 public: | 35 public: |
| 37 enum { | 36 enum { |
| 38 TYPE_ANY = -1, | 37 TYPE_ANY = -1, |
| 39 TYPE_IPV4, | 38 TYPE_IPV4, |
| 40 TYPE_IPV6, | 39 TYPE_IPV6, |
| 41 }; | 40 }; |
| 42 | 41 |
| 43 explicit SocketAddress(struct addrinfo* addrinfo); | 42 explicit SocketAddress(struct addrinfo* addrinfo); |
| 44 | 43 |
| 45 int GetType() { | 44 int GetType() { |
| 46 if (addr_.ss_family == AF_INET6) return TYPE_IPV6; | 45 if (addr_.ss.ss_family == AF_INET6) return TYPE_IPV6; |
| 47 return TYPE_IPV4; | 46 return TYPE_IPV4; |
| 48 } | 47 } |
| 49 | 48 |
| 50 const char* as_string() const { return as_string_; } | 49 const char* as_string() const { return as_string_; } |
| 51 const RawAddr& addr() const { return addr_; } | 50 const RawAddr& addr() const { return addr_; } |
| 52 | 51 |
| 53 static intptr_t GetAddrLength(const RawAddr& addr) { | 52 static intptr_t GetAddrLength(const RawAddr& addr) { |
| 54 return addr.ss_family == AF_INET6 ? | 53 return addr.ss.ss_family == AF_INET6 ? |
| 55 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); | 54 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); |
| 56 } | 55 } |
| 57 | 56 |
| 58 static int16_t FromType(int type) { | 57 static int16_t FromType(int type) { |
| 59 if (type == TYPE_ANY) return AF_UNSPEC; | 58 if (type == TYPE_ANY) return AF_UNSPEC; |
| 60 if (type == TYPE_IPV4) return AF_INET; | 59 if (type == TYPE_IPV4) return AF_INET; |
| 61 ASSERT(type == TYPE_IPV6 && "Invalid type"); | 60 ASSERT(type == TYPE_IPV6 && "Invalid type"); |
| 62 return AF_INET6; | 61 return AF_INET6; |
| 63 } | 62 } |
| 64 | 63 |
| 65 static void SetAddrPort(RawAddr* addr, intptr_t port) { | 64 static void SetAddrPort(RawAddr* addr, intptr_t port) { |
| 66 if (addr->ss_family == AF_INET) { | 65 if (addr->ss.ss_family == AF_INET) { |
| 67 addr->in.sin_port = htons(port); | 66 addr->in.sin_port = htons(port); |
| 68 } else { | 67 } else { |
| 69 addr->in6.sin6_port = htons(port); | 68 addr->in6.sin6_port = htons(port); |
| 70 } | 69 } |
| 71 } | 70 } |
| 72 | 71 |
| 73 static intptr_t GetAddrPort(RawAddr* addr) { | 72 static intptr_t GetAddrPort(RawAddr* addr) { |
| 74 if (addr->ss_family == AF_INET) { | 73 if (addr->ss.ss_family == AF_INET) { |
| 75 return ntohs(addr->in.sin_port); | 74 return ntohs(addr->in.sin_port); |
| 76 } else { | 75 } else { |
| 77 return ntohs(addr->in6.sin6_port); | 76 return ntohs(addr->in6.sin6_port); |
| 78 } | 77 } |
| 79 } | 78 } |
| 80 | 79 |
| 81 private: | 80 private: |
| 82 char as_string_[INET6_ADDRSTRLEN]; | 81 char as_string_[INET6_ADDRSTRLEN]; |
| 83 RawAddr addr_; | 82 RawAddr addr_; |
| 84 | 83 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 // -5: invalid bindAddress | 164 // -5: invalid bindAddress |
| 166 static intptr_t CreateBindListen(RawAddr addr, | 165 static intptr_t CreateBindListen(RawAddr addr, |
| 167 intptr_t port, | 166 intptr_t port, |
| 168 intptr_t backlog); | 167 intptr_t backlog); |
| 169 | 168 |
| 170 DISALLOW_ALLOCATION(); | 169 DISALLOW_ALLOCATION(); |
| 171 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); | 170 DISALLOW_IMPLICIT_CONSTRUCTORS(ServerSocket); |
| 172 }; | 171 }; |
| 173 | 172 |
| 174 #endif // BIN_SOCKET_H_ | 173 #endif // BIN_SOCKET_H_ |
| OLD | NEW |