|
@ -283,16 +283,7 @@ Socket.prototype.connect = function () { |
|
|
var self = this; |
|
|
var self = this; |
|
|
if (self.fd) throw new Error('Socket already opened'); |
|
|
if (self.fd) throw new Error('Socket already opened'); |
|
|
|
|
|
|
|
|
if (typeof(arguments[0]) == 'string' && arguments.length == 1) { |
|
|
function doConnect () { |
|
|
self.fd = socket('unix'); |
|
|
|
|
|
self.type = 'unix'; |
|
|
|
|
|
// TODO check if sockfile exists?
|
|
|
|
|
|
} else { |
|
|
|
|
|
self.fd = socket('tcp'); |
|
|
|
|
|
self.type = 'tcp'; |
|
|
|
|
|
// TODO dns resolution on arguments[1]
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
connect(self.fd, arguments[0], arguments[1]); |
|
|
connect(self.fd, arguments[0], arguments[1]); |
|
|
} catch (e) { |
|
|
} catch (e) { |
|
@ -325,6 +316,22 @@ Socket.prototype.connect = function () { |
|
|
self.forceClose(e); |
|
|
self.forceClose(e); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (typeof(arguments[0]) == 'string' && arguments.length == 1) { |
|
|
|
|
|
self.fd = socket('unix'); |
|
|
|
|
|
self.type = 'unix'; |
|
|
|
|
|
// TODO check if sockfile exists?
|
|
|
|
|
|
doConnect(arguments[0]); |
|
|
|
|
|
} else { |
|
|
|
|
|
self.fd = socket('tcp'); |
|
|
|
|
|
self.type = 'tcp'; |
|
|
|
|
|
// TODO dns resolution on arguments[1]
|
|
|
|
|
|
var port = arguments[0]; |
|
|
|
|
|
lookupDomainName(arguments[1], function (ip) { |
|
|
|
|
|
doConnect(port, ip); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -387,6 +394,34 @@ exports.createServer = function (listener) { |
|
|
return new Server(listener); |
|
|
return new Server(listener); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/* This function does both an ipv4 and ipv6 look up. |
|
|
|
|
|
* It first tries the ipv4 look up, if that fails, then it does the ipv6. |
|
|
|
|
|
*/ |
|
|
|
|
|
function lookupDomainName (dn, callback) { |
|
|
|
|
|
if (!needsLookup(dn)) { |
|
|
|
|
|
callback(dn); |
|
|
|
|
|
} else { |
|
|
|
|
|
debug("getaddrinfo 4 " + dn); |
|
|
|
|
|
getaddrinfo(dn, 4, function (r4) { |
|
|
|
|
|
if (r4 instanceof Error) throw r4; |
|
|
|
|
|
if (r4.length > 0) { |
|
|
|
|
|
debug("getaddrinfo 4 found " + r4); |
|
|
|
|
|
callback(r4[0]); |
|
|
|
|
|
} else { |
|
|
|
|
|
debug("getaddrinfo 6 " + dn); |
|
|
|
|
|
getaddrinfo(dn, 6, function (r6) { |
|
|
|
|
|
if (r6 instanceof Error) throw r6; |
|
|
|
|
|
if (r6.length < 0) { |
|
|
|
|
|
throw new Error("No address associated with hostname " + dn); |
|
|
|
|
|
} |
|
|
|
|
|
debug("getaddrinfo 6 found " + r6); |
|
|
|
|
|
callback(r6[0]); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Listen on a UNIX socket
|
|
|
// Listen on a UNIX socket
|
|
|
// server.listen("/tmp/socket");
|
|
|
// server.listen("/tmp/socket");
|
|
@ -400,6 +435,13 @@ Server.prototype.listen = function () { |
|
|
var self = this; |
|
|
var self = this; |
|
|
if (self.fd) throw new Error('Server already opened'); |
|
|
if (self.fd) throw new Error('Server already opened'); |
|
|
|
|
|
|
|
|
|
|
|
function doListen () { |
|
|
|
|
|
listen(self.fd, 128); |
|
|
|
|
|
self.watcher.set(self.fd, true, false); |
|
|
|
|
|
self.watcher.start(); |
|
|
|
|
|
self.emit("listening"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (typeof(arguments[0]) == 'string' && arguments.length == 1) { |
|
|
if (typeof(arguments[0]) == 'string' && arguments.length == 1) { |
|
|
// the first argument specifies a path
|
|
|
// the first argument specifies a path
|
|
|
self.fd = socket('unix'); |
|
|
self.fd = socket('unix'); |
|
@ -415,23 +457,17 @@ Server.prototype.listen = function () { |
|
|
self.type = 'tcp'; |
|
|
self.type = 'tcp'; |
|
|
// Don't bind(). OS will assign a port with INADDR_ANY. The port will be
|
|
|
// Don't bind(). OS will assign a port with INADDR_ANY. The port will be
|
|
|
// passed to the 'listening' event.
|
|
|
// passed to the 'listening' event.
|
|
|
|
|
|
doListen(); |
|
|
} else { |
|
|
} else { |
|
|
// the first argument is the port, the second an IP
|
|
|
// the first argument is the port, the second an IP
|
|
|
self.fd = socket('tcp'); |
|
|
self.fd = socket('tcp'); |
|
|
self.type = 'tcp'; |
|
|
self.type = 'tcp'; |
|
|
if (needsLookup(arguments[1])) { |
|
|
var port = arguments[0]; |
|
|
getaddrinfo(arguments[1], function (ip) { |
|
|
lookupDomainName(arguments[1], function (ip) { |
|
|
|
|
|
bind(self.fd, port, ip); |
|
|
|
|
|
doListen(); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
// TODO dns resolution on arguments[1]
|
|
|
|
|
|
bind(self.fd, arguments[0], arguments[1]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
listen(self.fd, 128); |
|
|
|
|
|
self.emit("listening"); |
|
|
|
|
|
|
|
|
|
|
|
self.watcher.set(self.fd, true, false); |
|
|
|
|
|
self.watcher.start(); |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|