1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "platform/socket.h"
9 #include <sys/socket.h>
11 #include <netinet/in.h>
27 static V8_DECLARE_ONCE(initialize_winsock) = V8_ONCE_INIT;
30 static void InitializeWinsock() {
32 int result = WSAStartup(MAKEWORD(1, 0), &wsa_data);
41 // Be sure to initialize the WinSock DLL first.
42 CallOnce(&initialize_winsock, &InitializeWinsock);
45 // Create the native socket handle.
46 native_handle_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
50 bool Socket::Bind(int port) {
52 ASSERT_LT(port, 65536);
53 if (!IsValid()) return false;
54 struct sockaddr_in sin;
55 memset(&sin, 0, sizeof(sin));
56 sin.sin_family = AF_INET;
57 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
58 sin.sin_port = htons(static_cast<uint16_t>(port));
60 native_handle_, reinterpret_cast<struct sockaddr*>(&sin), sizeof(sin));
65 bool Socket::Listen(int backlog) {
66 if (!IsValid()) return false;
67 int result = ::listen(native_handle_, backlog);
72 Socket* Socket::Accept() {
73 if (!IsValid()) return NULL;
75 NativeHandle native_handle = ::accept(native_handle_, NULL, NULL);
76 if (native_handle == kInvalidNativeHandle) {
78 if (errno == EINTR) continue; // Retry after signal.
82 return new Socket(native_handle);
87 bool Socket::Connect(const char* host, const char* port) {
88 ASSERT_NE(NULL, host);
89 ASSERT_NE(NULL, port);
90 if (!IsValid()) return false;
92 // Lookup host and port.
93 struct addrinfo* info = NULL;
95 memset(&hint, 0, sizeof(hint));
96 hint.ai_family = AF_INET;
97 hint.ai_socktype = SOCK_STREAM;
98 hint.ai_protocol = IPPROTO_TCP;
99 int result = ::getaddrinfo(host, port, &hint, &info);
104 // Connect to the host on the given port.
105 for (struct addrinfo* ai = info; ai != NULL; ai = ai->ai_next) {
106 // Try to connect using this addr info.
109 native_handle_, ai->ai_addr, static_cast<int>(ai->ai_addrlen));
115 if (errno == EINTR) continue; // Retry after signal.
125 bool Socket::Shutdown() {
126 if (!IsValid()) return false;
127 // Shutdown socket for both read and write.
129 int result = ::shutdown(native_handle_, SHUT_RDWR);
130 ::close(native_handle_);
132 int result = ::shutdown(native_handle_, SD_BOTH);
133 ::closesocket(native_handle_);
135 native_handle_ = kInvalidNativeHandle;
140 int Socket::Send(const char* buffer, int length) {
141 ASSERT(length <= 0 || buffer != NULL);
142 if (!IsValid()) return 0;
144 while (offset < length) {
145 int result = ::send(native_handle_, buffer + offset, length - offset, 0);
148 } else if (result > 0) {
149 ASSERT(result <= length - offset);
153 if (errno == EINTR) continue; // Retry after signal.
162 int Socket::Receive(char* buffer, int length) {
163 if (!IsValid()) return 0;
164 if (length <= 0) return 0;
165 ASSERT_NE(NULL, buffer);
167 int result = ::recv(native_handle_, buffer, length, 0);
170 if (errno == EINTR) continue; // Retry after signal.
179 bool Socket::SetReuseAddress(bool reuse_address) {
180 if (!IsValid()) return 0;
181 int v = reuse_address ? 1 : 0;
182 int result = ::setsockopt(native_handle_, SOL_SOCKET, SO_REUSEADDR,
183 reinterpret_cast<char*>(&v), sizeof(v));
189 int Socket::GetLastError() {
193 // Be sure to initialize the WinSock DLL first.
194 CallOnce(&initialize_winsock, &InitializeWinsock);
196 // Now we can safely perform WSA calls.
197 return ::WSAGetLastError();
201 } } // namespace v8::internal