add getprotobyname (#15273)

* add getprotobyname

* tests, docs, changelog

* add since
This commit is contained in:
flywind 2020-09-09 18:47:22 +08:00 • committed by GitHub
commit 8c82144ba5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 0 deletions

View file

@ -13,6 +13,8 @@
# TODO: Clean up the exports a bit and everything else in general.
import os, options
import std/private/since
when hostOS == "solaris":
{.passl: "-lsocket -lnsl".}
@ -199,6 +201,18 @@ proc toSockType*(protocol: Protocol): SockType =
of IPPROTO_IP, IPPROTO_IPV6, IPPROTO_RAW, IPPROTO_ICMP, IPPROTO_ICMPV6:
SOCK_RAW
proc getProtoByName*(name: string): int {.since: (1, 3, 5).} =
## Returns a protocol code from the database that matches the protocol ``name``.
when useWinVersion:
let protoent = winlean.getprotobyname(name.cstring)
else:
let protoent = posix.getprotobyname(name.cstring)
if protoent == nil:
raise newException(OsError, "protocol not found")
result = protoent.p_proto.int
proc close*(socket: SocketHandle) =
## Closes a socket.
when useWinVersion:

View file

@ -96,6 +96,12 @@ type
dwPlatformId*: DWORD
szCSDVersion*: array[0..127, WinChar]
Protoent* = object
p_name*: cstring
p_aliases*: cstringArray
p_proto*: cshort
const
STARTF_USESHOWWINDOW* = 1'i32
STARTF_USESTDHANDLES* = 256'i32
@ -573,6 +579,14 @@ proc gethostbyname*(name: cstring): ptr Hostent {.
proc gethostname*(hostname: cstring, len: cint): cint {.
stdcall, importc: "gethostname", dynlib: ws2dll, sideEffect.}
proc getprotobyname*(
name: cstring
): ptr Protoent {.stdcall, importc: "getprotobyname", dynlib: ws2dll, sideEffect.}
proc getprotobynumber*(
proto: cint
): ptr Protoent {.stdcall, importc: "getprotobynumber", dynlib: ws2dll, sideEffect.}
proc socket*(af, typ, protocol: cint): SocketHandle {.
stdcall, importc: "socket", dynlib: ws2dll.}