summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFederico Ceratto <federico.ceratto@gmail.com>2019-02-13 19:57:25 +0000
committerDominik Picheta <dominikpicheta@googlemail.com>2019-02-17 17:04:57 +0000
commitd8ff25f03250556c3ed99dbb7ad65d1cc627e1d3 (patch)
tree5ed3cdb945194533e0b385a2a73045bf53adeda3
parenta0fb77dfd5305b552b45fbe2f0a8c38ee8133b3a (diff)
downloadNim-d8ff25f03250556c3ed99dbb7ad65d1cc627e1d3.tar.gz
Provide access to getsockname()/getpeername()
Port of #3323 with added tests
-rw-r--r--lib/pure/asyncnet.nim12
-rw-r--r--tests/async/tasyncawait.nim4
2 files changed, 16 insertions, 0 deletions
diff --git a/lib/pure/asyncnet.nim b/lib/pure/asyncnet.nim
index 1c0681fad..56bda737a 100644
--- a/lib/pure/asyncnet.nim
+++ b/lib/pure/asyncnet.nim
@@ -166,6 +166,18 @@ proc newAsyncSocket*(domain: Domain = AF_INET, sockType: SockType = SOCK_STREAM,
     raiseOSError(osLastError())
   result = newAsyncSocket(fd, domain, sockType, protocol, buffered)
 
+proc getLocalAddr*(socket: AsyncSocket): (string, Port) =
+  ## Get the socket's local address and port number.
+  ##
+  ## This is high-level interface for `getsockname`:idx:.
+  getLocalAddr(socket.fd, socket.domain)
+
+proc getPeerAddr*(socket: AsyncSocket): (string, Port) =
+  ## Get the socket's peer address and port number.
+  ##
+  ## This is high-level interface for `getpeername`:idx:.
+  getPeerAddr(socket.fd, socket.domain)
+
 proc newAsyncSocket*(domain, sockType, protocol: cint,
     buffered = true): AsyncSocket =
   ## Creates a new asynchronous socket.
diff --git a/tests/async/tasyncawait.nim b/tests/async/tasyncawait.nim
index 1e6cf3761..063c8317c 100644
--- a/tests/async/tasyncawait.nim
+++ b/tests/async/tasyncawait.nim
@@ -26,6 +26,10 @@ proc launchSwarm(port: Port) {.async.} =
 proc readMessages(client: AsyncFD) {.async.} =
   # wrapping the AsyncFd into a AsyncSocket object
   var sockObj = newAsyncSocket(client)
+  var (ipaddr, port) = sockObj.getPeerAddr()
+  doAssert ipaddr == "127.0.0.1"
+  (ipaddr, port) = sockObj.getLocalAddr()
+  doAssert ipaddr == "127.0.0.1"
   while true:
     var line = await recvLine(sockObj)
     if line == "":