summaryrefslogtreecommitdiff
path: root/deps/v8/src/platform/socket.cc
blob: f4b33873b413d7f5f95e74ea7dbe69783d2f9eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "platform/socket.h"

#if V8_OS_POSIX
#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <netdb.h>

#include <unistd.h>
#endif

#include <errno.h>

#include "checks.h"
#include "once.h"

namespace v8 {
namespace internal {

#if V8_OS_WIN

static V8_DECLARE_ONCE(initialize_winsock) = V8_ONCE_INIT;


static void InitializeWinsock() {
  WSADATA wsa_data;
  int result = WSAStartup(MAKEWORD(1, 0), &wsa_data);
  CHECK_EQ(0, result);
}

#endif  // V8_OS_WIN


Socket::Socket() {
#if V8_OS_WIN
  // Be sure to initialize the WinSock DLL first.
  CallOnce(&initialize_winsock, &InitializeWinsock);
#endif  // V8_OS_WIN

  // Create the native socket handle.
  native_handle_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}


bool Socket::Bind(int port) {
  ASSERT_GE(port, 0);
  ASSERT_LT(port, 65536);
  if (!IsValid()) return false;
  struct sockaddr_in sin;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  sin.sin_port = htons(static_cast<uint16_t>(port));
  int result = ::bind(
      native_handle_, reinterpret_cast<struct sockaddr*>(&sin), sizeof(sin));
  return result == 0;
}


bool Socket::Listen(int backlog) {
  if (!IsValid()) return false;
  int result = ::listen(native_handle_, backlog);
  return result == 0;
}


Socket* Socket::Accept() {
  if (!IsValid()) return NULL;
  while (true) {
    NativeHandle native_handle = ::accept(native_handle_, NULL, NULL);
    if (native_handle == kInvalidNativeHandle) {
#if V8_OS_POSIX
      if (errno == EINTR) continue;  // Retry after signal.
#endif
      return NULL;
    }
    return new Socket(native_handle);
  }
}


bool Socket::Connect(const char* host, const char* port) {
  ASSERT_NE(NULL, host);
  ASSERT_NE(NULL, port);
  if (!IsValid()) return false;

  // Lookup host and port.
  struct addrinfo* info = NULL;
  struct addrinfo hint;
  memset(&hint, 0, sizeof(hint));
  hint.ai_family = AF_INET;
  hint.ai_socktype = SOCK_STREAM;
  hint.ai_protocol = IPPROTO_TCP;
  int result = ::getaddrinfo(host, port, &hint, &info);
  if (result != 0) {
    return false;
  }

  // Connect to the host on the given port.
  for (struct addrinfo* ai = info; ai != NULL; ai = ai->ai_next) {
    // Try to connect using this addr info.
    while (true) {
      result = ::connect(
          native_handle_, ai->ai_addr, static_cast<int>(ai->ai_addrlen));
      if (result == 0) {
        freeaddrinfo(info);
        return true;
      }
#if V8_OS_POSIX
      if (errno == EINTR) continue;  // Retry after signal.
#endif
      break;
    }
  }
  freeaddrinfo(info);
  return false;
}


bool Socket::Shutdown() {
  if (!IsValid()) return false;
  // Shutdown socket for both read and write.
#if V8_OS_POSIX
  int result = ::shutdown(native_handle_, SHUT_RDWR);
  ::close(native_handle_);
#elif V8_OS_WIN
  int result = ::shutdown(native_handle_, SD_BOTH);
  ::closesocket(native_handle_);
#endif
  native_handle_ = kInvalidNativeHandle;
  return result == 0;
}


int Socket::Send(const char* buffer, int length) {
  ASSERT(length <= 0 || buffer != NULL);
  if (!IsValid()) return 0;
  int offset = 0;
  while (offset < length) {
    int result = ::send(native_handle_, buffer + offset, length - offset, 0);
    if (result == 0) {
      break;
    } else if (result > 0) {
      ASSERT(result <= length - offset);
      offset += result;
    } else {
#if V8_OS_POSIX
      if (errno == EINTR) continue;  // Retry after signal.
#endif
      return 0;
    }
  }
  return offset;
}


int Socket::Receive(char* buffer, int length) {
  if (!IsValid()) return 0;
  if (length <= 0) return 0;
  ASSERT_NE(NULL, buffer);
  while (true) {
    int result = ::recv(native_handle_, buffer, length, 0);
    if (result < 0) {
#if V8_OS_POSIX
      if (errno == EINTR) continue;  // Retry after signal.
#endif
      return 0;
    }
    return result;
  }
}


bool Socket::SetReuseAddress(bool reuse_address) {
  if (!IsValid()) return 0;
  int v = reuse_address ? 1 : 0;
  int result = ::setsockopt(native_handle_, SOL_SOCKET, SO_REUSEADDR,
                            reinterpret_cast<char*>(&v), sizeof(v));
  return result == 0;
}


// static
int Socket::GetLastError() {
#if V8_OS_POSIX
  return errno;
#elif V8_OS_WIN
  // Be sure to initialize the WinSock DLL first.
  CallOnce(&initialize_winsock, &InitializeWinsock);

  // Now we can safely perform WSA calls.
  return ::WSAGetLastError();
#endif
}

} }  // namespace v8::internal