1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "handle_wrap.h"
27 #include "node_buffer.h"
28 #include "node_wrap.h"
30 #include "stream_wrap.h"
40 using v8::EscapableHandleScope;
42 using v8::FunctionCallbackInfo;
43 using v8::FunctionTemplate;
45 using v8::HandleScope;
49 using v8::PropertyAttribute;
54 typedef class ReqWrap<uv_connect_t> ConnectWrap;
57 Local<Object> TCPWrap::Instantiate(Environment* env) {
58 EscapableHandleScope handle_scope(env->isolate());
59 assert(env->tcp_constructor_template().IsEmpty() == false);
60 Local<Function> constructor = env->tcp_constructor_template()->GetFunction();
61 assert(constructor.IsEmpty() == false);
62 Local<Object> instance = constructor->NewInstance();
63 assert(instance.IsEmpty() == false);
64 return handle_scope.Escape(instance);
68 void TCPWrap::Initialize(Handle<Object> target,
70 Handle<Context> context) {
71 Environment* env = Environment::GetCurrent(context);
73 Local<FunctionTemplate> t = FunctionTemplate::New(env->isolate(), New);
74 t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TCP"));
75 t->InstanceTemplate()->SetInternalFieldCount(1);
77 enum PropertyAttribute attributes =
78 static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
79 t->InstanceTemplate()->SetAccessor(env->fd_string(),
86 NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
88 NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);
89 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
91 NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
92 NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
93 NODE_SET_PROTOTYPE_METHOD(t, "shutdown", StreamWrap::Shutdown);
95 NODE_SET_PROTOTYPE_METHOD(t, "writeBuffer", StreamWrap::WriteBuffer);
96 NODE_SET_PROTOTYPE_METHOD(t,
98 StreamWrap::WriteAsciiString);
99 NODE_SET_PROTOTYPE_METHOD(t, "writeUtf8String", StreamWrap::WriteUtf8String);
100 NODE_SET_PROTOTYPE_METHOD(t, "writeUcs2String", StreamWrap::WriteUcs2String);
101 NODE_SET_PROTOTYPE_METHOD(t, "writev", StreamWrap::Writev);
103 NODE_SET_PROTOTYPE_METHOD(t, "open", Open);
104 NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
105 NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen);
106 NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
107 NODE_SET_PROTOTYPE_METHOD(t, "bind6", Bind6);
108 NODE_SET_PROTOTYPE_METHOD(t, "connect6", Connect6);
109 NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName);
110 NODE_SET_PROTOTYPE_METHOD(t, "getpeername", GetPeerName);
111 NODE_SET_PROTOTYPE_METHOD(t, "setNoDelay", SetNoDelay);
112 NODE_SET_PROTOTYPE_METHOD(t, "setKeepAlive", SetKeepAlive);
115 NODE_SET_PROTOTYPE_METHOD(t,
116 "setSimultaneousAccepts",
117 SetSimultaneousAccepts);
120 target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TCP"), t->GetFunction());
121 env->set_tcp_constructor_template(t);
125 uv_tcp_t* TCPWrap::UVHandle() {
130 void TCPWrap::New(const FunctionCallbackInfo<Value>& args) {
131 // This constructor should not be exposed to public javascript.
132 // Therefore we assert that we are not trying to call this as a
134 assert(args.IsConstructCall());
135 HandleScope handle_scope(args.GetIsolate());
136 Environment* env = Environment::GetCurrent(args.GetIsolate());
137 TCPWrap* wrap = new TCPWrap(env, args.This());
142 TCPWrap::TCPWrap(Environment* env, Handle<Object> object)
145 reinterpret_cast<uv_stream_t*>(&handle_),
146 AsyncWrap::PROVIDER_TCPWRAP) {
147 int r = uv_tcp_init(env->event_loop(), &handle_);
148 assert(r == 0); // How do we proxy this error up to javascript?
149 // Suggestion: uv_tcp_init() returns void.
150 UpdateWriteQueueSize();
154 TCPWrap::~TCPWrap() {
155 assert(persistent().IsEmpty());
159 void TCPWrap::GetSockName(const FunctionCallbackInfo<Value>& args) {
160 HandleScope handle_scope(args.GetIsolate());
161 Environment* env = Environment::GetCurrent(args.GetIsolate());
162 struct sockaddr_storage address;
164 TCPWrap* wrap = Unwrap<TCPWrap>(args.This());
166 assert(args[0]->IsObject());
167 Local<Object> out = args[0].As<Object>();
169 int addrlen = sizeof(address);
170 int err = uv_tcp_getsockname(&wrap->handle_,
171 reinterpret_cast<sockaddr*>(&address),
174 const sockaddr* addr = reinterpret_cast<const sockaddr*>(&address);
175 AddressToJS(env, addr, out);
178 args.GetReturnValue().Set(err);
182 void TCPWrap::GetPeerName(const FunctionCallbackInfo<Value>& args) {
183 HandleScope handle_scope(args.GetIsolate());
184 Environment* env = Environment::GetCurrent(args.GetIsolate());
185 struct sockaddr_storage address;
187 TCPWrap* wrap = Unwrap<TCPWrap>(args.This());
189 assert(args[0]->IsObject());
190 Local<Object> out = args[0].As<Object>();
192 int addrlen = sizeof(address);
193 int err = uv_tcp_getpeername(&wrap->handle_,
194 reinterpret_cast<sockaddr*>(&address),
197 const sockaddr* addr = reinterpret_cast<const sockaddr*>(&address);
198 AddressToJS(env, addr, out);
201 args.GetReturnValue().Set(err);
205 void TCPWrap::SetNoDelay(const FunctionCallbackInfo<Value>& args) {
206 Environment* env = Environment::GetCurrent(args.GetIsolate());
207 HandleScope scope(env->isolate());
209 TCPWrap* wrap = Unwrap<TCPWrap>(args.This());
211 int enable = static_cast<int>(args[0]->BooleanValue());
212 int err = uv_tcp_nodelay(&wrap->handle_, enable);
213 args.GetReturnValue().Set(err);
217 void TCPWrap::SetKeepAlive(const FunctionCallbackInfo<Value>& args) {
218 Environment* env = Environment::GetCurrent(args.GetIsolate());
219 HandleScope scope(env->isolate());
221 TCPWrap* wrap = Unwrap<TCPWrap>(args.This());
223 int enable = args[0]->Int32Value();
224 unsigned int delay = args[1]->Uint32Value();
226 int err = uv_tcp_keepalive(&wrap->handle_, enable, delay);
227 args.GetReturnValue().Set(err);
232 void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) {
233 Environment* env = Environment::GetCurrent(args.GetIsolate());
234 HandleScope scope(env->isolate());
236 TCPWrap* wrap = Unwrap<TCPWrap>(args.This());
238 bool enable = args[0]->BooleanValue();
239 int err = uv_tcp_simultaneous_accepts(&wrap->handle_, enable);
240 args.GetReturnValue().Set(err);
245 void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
246 Environment* env = Environment::GetCurrent(args.GetIsolate());
247 HandleScope scope(env->isolate());
248 TCPWrap* wrap = Unwrap<TCPWrap>(args.This());
249 int fd = args[0]->IntegerValue();
250 uv_tcp_open(&wrap->handle_, fd);
254 void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
255 Environment* env = Environment::GetCurrent(args.GetIsolate());
256 HandleScope scope(env->isolate());
258 TCPWrap* wrap = Unwrap<TCPWrap>(args.This());
260 String::Utf8Value ip_address(args[0]);
261 int port = args[1]->Int32Value();
264 int err = uv_ip4_addr(*ip_address, port, &addr);
266 err = uv_tcp_bind(&wrap->handle_,
267 reinterpret_cast<const sockaddr*>(&addr),
271 args.GetReturnValue().Set(err);
275 void TCPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
276 Environment* env = Environment::GetCurrent(args.GetIsolate());
277 HandleScope scope(env->isolate());
279 TCPWrap* wrap = Unwrap<TCPWrap>(args.This());
281 String::Utf8Value ip6_address(args[0]);
282 int port = args[1]->Int32Value();
285 int err = uv_ip6_addr(*ip6_address, port, &addr);
287 err = uv_tcp_bind(&wrap->handle_,
288 reinterpret_cast<const sockaddr*>(&addr),
292 args.GetReturnValue().Set(err);
296 void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
297 Environment* env = Environment::GetCurrent(args.GetIsolate());
298 HandleScope scope(env->isolate());
300 TCPWrap* wrap = Unwrap<TCPWrap>(args.This());
302 int backlog = args[0]->Int32Value();
303 int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
306 args.GetReturnValue().Set(err);
310 void TCPWrap::OnConnection(uv_stream_t* handle, int status) {
311 TCPWrap* tcp_wrap = static_cast<TCPWrap*>(handle->data);
312 assert(&tcp_wrap->handle_ == reinterpret_cast<uv_tcp_t*>(handle));
313 Environment* env = tcp_wrap->env();
315 HandleScope handle_scope(env->isolate());
316 Context::Scope context_scope(env->context());
318 // We should not be getting this callback if someone as already called
319 // uv_close() on the handle.
320 assert(tcp_wrap->persistent().IsEmpty() == false);
322 Local<Value> argv[2] = {
323 Integer::New(env->isolate(), status),
324 Undefined(env->isolate())
328 // Instantiate the client javascript object and handle.
329 Local<Object> client_obj = Instantiate(env);
331 // Unwrap the client javascript object.
332 TCPWrap* wrap = Unwrap<TCPWrap>(client_obj);
333 uv_stream_t* client_handle = reinterpret_cast<uv_stream_t*>(&wrap->handle_);
334 if (uv_accept(handle, client_handle))
337 // Successful accept. Call the onconnection callback in JavaScript land.
338 argv[1] = client_obj;
341 tcp_wrap->MakeCallback(env->onconnection_string(), ARRAY_SIZE(argv), argv);
345 void TCPWrap::AfterConnect(uv_connect_t* req, int status) {
346 ConnectWrap* req_wrap = static_cast<ConnectWrap*>(req->data);
347 TCPWrap* wrap = static_cast<TCPWrap*>(req->handle->data);
348 assert(req_wrap->env() == wrap->env());
349 Environment* env = wrap->env();
351 HandleScope handle_scope(env->isolate());
352 Context::Scope context_scope(env->context());
354 // The wrap and request objects should still be there.
355 assert(req_wrap->persistent().IsEmpty() == false);
356 assert(wrap->persistent().IsEmpty() == false);
358 Local<Object> req_wrap_obj = req_wrap->object();
359 Local<Value> argv[5] = {
360 Integer::New(env->isolate(), status),
363 v8::True(env->isolate()),
364 v8::True(env->isolate())
367 req_wrap->MakeCallback(env->oncomplete_string(), ARRAY_SIZE(argv), argv);
373 void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
374 HandleScope handle_scope(args.GetIsolate());
375 Environment* env = Environment::GetCurrent(args.GetIsolate());
377 TCPWrap* wrap = Unwrap<TCPWrap>(args.This());
379 assert(args[0]->IsObject());
380 assert(args[1]->IsString());
381 assert(args[2]->Uint32Value());
383 Local<Object> req_wrap_obj = args[0].As<Object>();
384 String::Utf8Value ip_address(args[1]);
385 int port = args[2]->Uint32Value();
388 int err = uv_ip4_addr(*ip_address, port, &addr);
391 ConnectWrap* req_wrap = new ConnectWrap(env,
393 AsyncWrap::PROVIDER_CONNECTWRAP);
394 err = uv_tcp_connect(&req_wrap->req_,
396 reinterpret_cast<const sockaddr*>(&addr),
398 req_wrap->Dispatched();
403 args.GetReturnValue().Set(err);
407 void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) {
408 HandleScope handle_scope(args.GetIsolate());
409 Environment* env = Environment::GetCurrent(args.GetIsolate());
411 TCPWrap* wrap = Unwrap<TCPWrap>(args.This());
413 assert(args[0]->IsObject());
414 assert(args[1]->IsString());
415 assert(args[2]->Uint32Value());
417 Local<Object> req_wrap_obj = args[0].As<Object>();
418 String::Utf8Value ip_address(args[1]);
419 int port = args[2]->Int32Value();
422 int err = uv_ip6_addr(*ip_address, port, &addr);
425 ConnectWrap* req_wrap = new ConnectWrap(env,
427 AsyncWrap::PROVIDER_CONNECTWRAP);
428 err = uv_tcp_connect(&req_wrap->req_,
430 reinterpret_cast<const sockaddr*>(&addr),
432 req_wrap->Dispatched();
437 args.GetReturnValue().Set(err);
441 // also used by udp_wrap.cc
442 Local<Object> AddressToJS(Environment* env,
443 const sockaddr* addr,
444 Local<Object> info) {
445 EscapableHandleScope scope(env->isolate());
446 char ip[INET6_ADDRSTRLEN];
447 const sockaddr_in *a4;
448 const sockaddr_in6 *a6;
452 info = Object::New(env->isolate());
454 switch (addr->sa_family) {
456 a6 = reinterpret_cast<const sockaddr_in6*>(addr);
457 uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip);
458 port = ntohs(a6->sin6_port);
459 info->Set(env->address_string(), OneByteString(env->isolate(), ip));
460 info->Set(env->family_string(), env->ipv6_string());
461 info->Set(env->port_string(), Integer::New(env->isolate(), port));
465 a4 = reinterpret_cast<const sockaddr_in*>(addr);
466 uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip);
467 port = ntohs(a4->sin_port);
468 info->Set(env->address_string(), OneByteString(env->isolate(), ip));
469 info->Set(env->family_string(), env->ipv4_string());
470 info->Set(env->port_string(), Integer::New(env->isolate(), port));
474 info->Set(env->address_string(), String::Empty(env->isolate()));
477 return scope.Escape(info);
483 NODE_MODULE_CONTEXT_AWARE_BUILTIN(tcp_wrap, node::TCPWrap::Initialize)