Permalink
Browse files

net: expose peer address + port

  • Loading branch information...
1 parent a38bda9 commit 12885748a8db8f37dd21b0c13d1dc1249db1dc50 @bnoordhuis committed Aug 12, 2011
Showing with 58 additions and 16 deletions.
  1. +1 −0 deps/uv/include/uv.h
  2. +15 −2 deps/uv/src/uv-unix.c
  3. +15 −2 deps/uv/src/win/tcp.c
  4. +21 −10 lib/net_uv.js
  5. +6 −2 src/tcp_wrap.cc
@@ -348,6 +348,7 @@ int uv_tcp_connect6(uv_connect_t* req, uv_tcp_t* handle,
struct sockaddr_in6 address, uv_connect_cb cb);
int uv_getsockname(uv_tcp_t* handle, struct sockaddr* name, int* namelen);
+int uv_getpeername(uv_tcp_t* handle, struct sockaddr* name, int* namelen);
/*
View
@@ -1115,7 +1115,10 @@ int uv_tcp_connect6(uv_connect_t* req,
}
-int uv_getsockname(uv_tcp_t* handle, struct sockaddr* name, int* namelen) {
+static int uv__getsockname(int (*sockfun)(int, struct sockaddr*, socklen_t*),
+ uv_tcp_t* handle,
+ struct sockaddr* name,
+ int* namelen) {
socklen_t socklen;
int saved_errno;
@@ -1125,7 +1128,7 @@ int uv_getsockname(uv_tcp_t* handle, struct sockaddr* name, int* namelen) {
/* sizeof(socklen_t) != sizeof(int) on some systems. */
socklen = (socklen_t)*namelen;
- if (getsockname(handle->fd, name, &socklen) == -1) {
+ if (sockfun(handle->fd, name, &socklen) == -1) {
uv_err_new((uv_handle_t*)handle, errno);
} else {
*namelen = (int)socklen;
@@ -1136,6 +1139,16 @@ int uv_getsockname(uv_tcp_t* handle, struct sockaddr* name, int* namelen) {
}
+int uv_getsockname(uv_tcp_t* handle, struct sockaddr* name, int* namelen) {
+ return uv__getsockname(getsockname, handle, name, namelen);
+}
+
+
+int uv_getpeername(uv_tcp_t* handle, struct sockaddr* name, int* namelen) {
+ return uv__getsockname(getpeername, handle, name, namelen);
+}
+
+
static size_t uv__buf_count(uv_buf_t bufs[], int bufcnt) {
size_t total = 0;
int i;
View
@@ -667,15 +667,18 @@ int uv_tcp_connect6(uv_connect_t* req, uv_tcp_t* handle,
}
-int uv_getsockname(uv_tcp_t* handle, struct sockaddr* name, int* namelen) {
+static int uv__getsockname(int (*sockfun)(SOCKET, struct sockaddr*, int*),
+ uv_tcp_t* handle,
+ struct sockaddr* name,
+ int* namelen) {
int result;
if (handle->flags & UV_HANDLE_SHUTTING) {
uv_set_sys_error(WSAESHUTDOWN);
return -1;
}
- result = getsockname(handle->socket, name, namelen);
+ result = sockfun(handle->socket, name, namelen);
if (result != 0) {
uv_set_sys_error(WSAGetLastError());
return -1;
@@ -685,6 +688,16 @@ int uv_getsockname(uv_tcp_t* handle, struct sockaddr* name, int* namelen) {
}
+int uv_getsockname(uv_tcp_t* handle, struct sockaddr* name, int* namelen) {
+ return uv__getsockname(getsockname, handle, name, namelen);
+}
+
+
+int uv_getpeername(uv_tcp_t* handle, struct sockaddr* name, int* namelen) {
+ return uv__getsockname(getpeername, handle, name, namelen);
+}
+
+
int uv_tcp_write(uv_write_t* req, uv_tcp_t* handle, uv_buf_t bufs[], int bufcnt,
uv_write_cb cb) {
int result;
View
@@ -69,13 +69,6 @@ function initSocketHandle(self) {
if (self._handle) {
self._handle.socket = self;
self._handle.onread = onread;
-
- if (self._handle.getsockname) {
- var sockname = self._handle.getsockname();
- self.remoteAddress = sockname.address;
- self.remotePort = sockname.port;
- }
- // also export sockname.family?
}
}
@@ -133,10 +126,24 @@ Socket.prototype.setKeepAlive = function(setting, msecs) {
Socket.prototype.address = function() {
- return this._handle.getsockname();
+ if (this._handle)
+ return this._handle.getpeername();
+
+ // legacy raises EBADF but this feels cleaner
+ return {address:null, port:-1, family:-1};
};
+Socket.prototype.__defineGetter__('remoteAddress', function() {
+ return this.address().address;
+});
+
+
+Socket.prototype.__defineGetter__('remotePort', function() {
+ return this.address().port;
+});
+
+
Object.defineProperty(Socket.prototype, 'readyState', {
get: function() {
if (this._connecting) {
@@ -413,7 +420,6 @@ function connect(self, address, port, addressType) {
if (port) {
self.remotePort = port;
}
- self.remoteAddress = address;
// TODO return promise from Socket.prototype.connect which
// wraps _connectReq.
@@ -664,10 +670,15 @@ Server.prototype.listen = function() {
}
};
+
Server.prototype.address = function() {
- return this._handle.getsockname();
+ if (this._handle)
+ return this._handle.getsockname();
+
+ return {address:null, port:-1, family:-1};
};
+
function onconnection(clientHandle) {
var handle = this;
var self = handle.socket;
View
@@ -54,6 +54,8 @@ static Persistent<String> port_symbol;
typedef class ReqWrap<uv_connect_t> ConnectWrap;
+typedef int (*sockfun_t)(uv_tcp_t* handle, sockaddr* addr, int* addrlen);
+
class TCPWrap : public StreamWrap {
public:
@@ -81,7 +83,8 @@ class TCPWrap : public StreamWrap {
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
NODE_SET_PROTOTYPE_METHOD(t, "bind6", Bind6);
NODE_SET_PROTOTYPE_METHOD(t, "connect6", Connect6);
- NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName);
+ NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName<uv_getsockname>);
+ NODE_SET_PROTOTYPE_METHOD(t, "getpeername", GetSockName<uv_getpeername>);
tcpConstructor = Persistent<Function>::New(t->GetFunction());
@@ -118,6 +121,7 @@ class TCPWrap : public StreamWrap {
assert(object_.IsEmpty());
}
+ template <sockfun_t SOCKFUN>
static Handle<Value> GetSockName(const Arguments& args) {
HandleScope scope;
struct sockaddr address;
@@ -128,7 +132,7 @@ class TCPWrap : public StreamWrap {
UNWRAP
int namelen = sizeof(address);
- int r = uv_getsockname(&wrap->handle_, &address, &namelen);
+ int r = SOCKFUN(&wrap->handle_, &address, &namelen);
Local<Object> sockname = Object::New();
if (r != 0) {

0 comments on commit 1288574

Please sign in to comment.