summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2020-09-09 18:47:22 +0800
committerGitHub <noreply@github.com>2020-09-09 12:47:22 +0200
commit8c82144ba507244084836b38689e4925de982626 (patch)
treea1f6839815344f6f5073ccda33ad4bc7155f18b9 /lib/pure
parent7d4f0df107fdce5d1da93e26e54dd87526a0a74f (diff)
downloadNim-8c82144ba507244084836b38689e4925de982626.tar.gz
add getprotobyname (#15273)
* add getprotobyname

* tests, docs, changelog

* add since
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/nativesockets.nim14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/pure/nativesockets.nim b/lib/pure/nativesockets.nim
index e38dc2ecd..1f597f8eb 100644
--- a/lib/pure/nativesockets.nim
+++ b/lib/pure/nativesockets.nim
@@ -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: