Add gethostname to nativesockets (#5443)
This commit is contained in:
parent
78de355ec6
commit
dd4d47c671
3 changed files with 27 additions and 0 deletions
|
|
@ -374,6 +374,22 @@ proc getHostByName*(name: string): Hostent {.tags: [ReadIOEffect].} =
|
||||||
result.addrList = cstringArrayToSeq(s.h_addr_list)
|
result.addrList = cstringArrayToSeq(s.h_addr_list)
|
||||||
result.length = int(s.h_length)
|
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 =
|
proc getSockDomain*(socket: SocketHandle): Domain =
|
||||||
## returns the socket's domain (AF_INET or AF_INET6).
|
## returns the socket's domain (AF_INET or AF_INET6).
|
||||||
var name: SockAddr
|
var name: SockAddr
|
||||||
|
|
|
||||||
|
|
@ -542,6 +542,9 @@ proc gethostbyaddr*(ip: ptr InAddr, len: cuint, theType: cint): ptr Hostent {.
|
||||||
proc gethostbyname*(name: cstring): ptr Hostent {.
|
proc gethostbyname*(name: cstring): ptr Hostent {.
|
||||||
stdcall, importc: "gethostbyname", dynlib: ws2dll.}
|
stdcall, importc: "gethostbyname", dynlib: ws2dll.}
|
||||||
|
|
||||||
|
proc gethostname*(hostname: cstring, len: cint): cint {.
|
||||||
|
stdcall, importc: "gethostname", dynlib: ws2dll.}
|
||||||
|
|
||||||
proc socket*(af, typ, protocol: cint): SocketHandle {.
|
proc socket*(af, typ, protocol: cint): SocketHandle {.
|
||||||
stdcall, importc: "socket", dynlib: ws2dll.}
|
stdcall, importc: "socket", dynlib: ws2dll.}
|
||||||
|
|
||||||
|
|
|
||||||
8
tests/stdlib/tnativesockets.nim
Normal file
8
tests/stdlib/tnativesockets.nim
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import nativesockets, unittest
|
||||||
|
|
||||||
|
suite "nativesockets":
|
||||||
|
test "getHostname":
|
||||||
|
let hostname = getHostname()
|
||||||
|
check hostname.len > 0
|
||||||
|
check hostname.len < 64
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue