OLD | NEW |
(Empty) | |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 // Platform specific code for POSIX goes here. This is not a platform on its |
| 29 // own but contains the parts which are the same across POSIX platforms Linux, |
| 30 // Mac OS and FreeBSD. |
| 31 |
| 32 #include <unistd.h> |
| 33 #include <errno.h> |
| 34 |
| 35 #include <sys/socket.h> |
| 36 #include <sys/types.h> |
| 37 |
| 38 #include <arpa/inet.h> |
| 39 #include <netinet/in.h> |
| 40 #include <netdb.h> |
| 41 |
| 42 #include "v8.h" |
| 43 |
| 44 #include "platform.h" |
| 45 |
| 46 |
| 47 namespace v8 { namespace internal { |
| 48 |
| 49 |
| 50 // ---------------------------------------------------------------------------- |
| 51 // POSIX socket support. |
| 52 // |
| 53 |
| 54 class POSIXSocket : public Socket { |
| 55 public: |
| 56 explicit POSIXSocket() { |
| 57 // Create the socket. |
| 58 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 59 } |
| 60 explicit POSIXSocket(int socket): socket_(socket) { } |
| 61 virtual ~POSIXSocket() { Shutdown(); } |
| 62 |
| 63 // Server initialization. |
| 64 bool Bind(const int port); |
| 65 bool Listen(int backlog) const; |
| 66 Socket* Accept() const; |
| 67 |
| 68 // Client initialization. |
| 69 bool Connect(const char* host, const char* port); |
| 70 |
| 71 // Shutdown socket for both read and write. |
| 72 bool Shutdown(); |
| 73 |
| 74 // Data Transimission |
| 75 int Send(const char* data, int len) const; |
| 76 int Receive(char* data, int len) const; |
| 77 |
| 78 bool SetReuseAddress(bool reuse_address); |
| 79 |
| 80 bool IsValid() const { return socket_ != -1; } |
| 81 |
| 82 private: |
| 83 int socket_; |
| 84 }; |
| 85 |
| 86 |
| 87 bool POSIXSocket::Bind(const int port) { |
| 88 if (!IsValid()) { |
| 89 return false; |
| 90 } |
| 91 |
| 92 sockaddr_in addr; |
| 93 memset(&addr, 0, sizeof(addr)); |
| 94 addr.sin_family = AF_INET; |
| 95 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 96 addr.sin_port = htons(port); |
| 97 int status = bind(socket_, |
| 98 reinterpret_cast<struct sockaddr *>(&addr), |
| 99 sizeof(addr)); |
| 100 return status == 0; |
| 101 } |
| 102 |
| 103 |
| 104 bool POSIXSocket::Listen(int backlog) const { |
| 105 if (!IsValid()) { |
| 106 return false; |
| 107 } |
| 108 |
| 109 int status = listen(socket_, backlog); |
| 110 return status == 0; |
| 111 } |
| 112 |
| 113 |
| 114 Socket* POSIXSocket::Accept() const { |
| 115 if (!IsValid()) { |
| 116 return NULL; |
| 117 } |
| 118 |
| 119 int socket = accept(socket_, NULL, NULL); |
| 120 if (socket == -1) { |
| 121 return NULL; |
| 122 } else { |
| 123 return new POSIXSocket(socket); |
| 124 } |
| 125 } |
| 126 |
| 127 |
| 128 bool POSIXSocket::Connect(const char* host, const char* port) { |
| 129 if (!IsValid()) { |
| 130 return false; |
| 131 } |
| 132 |
| 133 // Lookup host and port. |
| 134 struct addrinfo *result = NULL; |
| 135 struct addrinfo hints; |
| 136 memset(&hints, 0, sizeof(addrinfo)); |
| 137 hints.ai_family = AF_INET; |
| 138 hints.ai_socktype = SOCK_STREAM; |
| 139 hints.ai_protocol = IPPROTO_TCP; |
| 140 int status = getaddrinfo(host, port, &hints, &result); |
| 141 if (status != 0) { |
| 142 return false; |
| 143 } |
| 144 |
| 145 // Connect. |
| 146 status = connect(socket_, result->ai_addr, result->ai_addrlen); |
| 147 freeaddrinfo(result); |
| 148 return status == 0; |
| 149 } |
| 150 |
| 151 |
| 152 bool POSIXSocket::Shutdown() { |
| 153 if (IsValid()) { |
| 154 // Shutdown socket for both read and write. |
| 155 int status = shutdown(socket_, SHUT_RDWR); |
| 156 close(socket_); |
| 157 socket_ = -1; |
| 158 return status == 0; |
| 159 } |
| 160 return true; |
| 161 } |
| 162 |
| 163 |
| 164 int POSIXSocket::Send(const char* data, int len) const { |
| 165 int status = send(socket_, data, len, 0); |
| 166 return status; |
| 167 } |
| 168 |
| 169 |
| 170 int POSIXSocket::Receive(char* data, int len) const { |
| 171 int status = recv(socket_, data, len, 0); |
| 172 return status; |
| 173 } |
| 174 |
| 175 |
| 176 bool POSIXSocket::SetReuseAddress(bool reuse_address) { |
| 177 int on = reuse_address ? 1 : 0; |
| 178 int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 179 return status == 0; |
| 180 } |
| 181 |
| 182 |
| 183 bool Socket::Setup() { |
| 184 // Nothing to do on POSIX. |
| 185 return true; |
| 186 } |
| 187 |
| 188 |
| 189 int Socket::LastError() { |
| 190 return errno; |
| 191 } |
| 192 |
| 193 |
| 194 uint16_t Socket::HToN(uint16_t value) { |
| 195 return htons(value); |
| 196 } |
| 197 |
| 198 |
| 199 uint16_t Socket::NToH(uint16_t value) { |
| 200 return ntohs(value); |
| 201 } |
| 202 |
| 203 |
| 204 uint32_t Socket::HToN(uint32_t value) { |
| 205 return htonl(value); |
| 206 } |
| 207 |
| 208 |
| 209 uint32_t Socket::NToH(uint32_t value) { |
| 210 return ntohl(value); |
| 211 } |
| 212 |
| 213 |
| 214 Socket* OS::CreateSocket() { |
| 215 return new POSIXSocket(); |
| 216 } |
| 217 |
| 218 |
| 219 } } // namespace v8::internal |
OLD | NEW |