summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorFederico Ceratto <federico.ceratto@gmail.com>2017-03-01 08:44:24 +0000
committerAndreas Rumpf <rumpf_a@web.de>2017-03-01 09:44:24 +0100
commitdd4d47c671827148bdaf478c7a37460931cd242c (patch)
tree7710df57106748763bda35268a6dd8ae05a71c98 /lib
parent78de355ec60322ca33c26641d967bcf67571b5d9 (diff)
downloadNim-dd4d47c671827148bdaf478c7a37460931cd242c.tar.gz
Add gethostname to nativesockets (#5443)
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/nativesockets.nim16
-rw-r--r--lib/windows/winlean.nim3
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/pure/nativesockets.nim b/lib/pure/nativesockets.nim
index d51dbd475..0a7ffb3b3 100644
--- a/lib/pure/nativesockets.nim
+++ b/lib/pure/nativesockets.nim
@@ -374,6 +374,22 @@ proc getHostByName*(name: string): Hostent {.tags: [ReadIOEffect].} =
   result.addrList = cstringArrayToSeq(s.h_addr_list)
   result.length = int(s.h_length)
 
+proc getHostname*(): string {.tags: [ReadIOEffect].} =
+  ## Returns the local hostname (not the FQDN)
+  # https://tools.ietf.org/html/rfc1035#section-2.3.1
+  # https://tools.ietf.org/html/rfc2181#section-11
+  const size = 64
+  result = newString(size)
+  when useWinVersion:
+    let success = winlean.getHostname(result, size)
+  else:
+    # Posix
+    let success = posix.getHostname(result, size)
+  if success != 0.cint:
+    raiseOSError(osLastError())
+  let x = len(cstring(result))
+  result.setLen(x)
+
 proc getSockDomain*(socket: SocketHandle): Domain =
   ## returns the socket's domain (AF_INET or AF_INET6).
   var name: SockAddr
diff --git a/lib/windows/winlean.nim b/lib/windows/winlean.nim
index fa9ce9eed..1a251d0cc 100644
--- a/lib/windows/winlean.nim
+++ b/lib/windows/winlean.nim
@@ -542,6 +542,9 @@ proc gethostbyaddr*(ip: ptr InAddr, len: cuint, theType: cint): ptr Hostent {.
 proc gethostbyname*(name: cstring): ptr Hostent {.
   stdcall, importc: "gethostbyname", dynlib: ws2dll.}
 
+proc gethostname*(hostname: cstring, len: cint): cint {.
+  stdcall, importc: "gethostname", dynlib: ws2dll.}
+
 proc socket*(af, typ, protocol: cint): SocketHandle {.
   stdcall, importc: "socket", dynlib: ws2dll.}