@@ -215,6 +215,22 @@ proc getProtoByName*(name: string): int {.since: (1, 3, 5).} =
215215 # # Returns a protocol code from the database that matches the protocol `name`.
216216 when useWinVersion:
217217 let protoent = winlean.getprotobyname (name.cstring )
218+ elif defined (linux) and not defined (android):
219+ var protoent: ptr posix.Protoent
220+ {.emit : " ;\n #ifdef __GLIBC__" .}
221+ # glibc fix
222+ proc getprotobyname_r (name: cstring , resultBuf: ptr posix.Protoent ,
223+ buf: cstring , buflen: csize_t ,
224+ res: ptr ptr posix.Protoent ): cint
225+ {.importc , header : " <netdb.h>" .}
226+ var pe: posix.Protoent
227+ var buf: array [1024 , char ]
228+ discard getprotobyname_r (name.cstring , addr pe, cast [cstring ](addr buf[0 ]),
229+ csize_t (buf.len), addr protoent)
230+ {.emit : " ;\n #else" .}
231+ # prevent musl regression
232+ protoent = posix.getprotobyname (name.cstring )
233+ {.emit : " ;\n #endif" .}
218234 else :
219235 let protoent = posix.getprotobyname (name.cstring )
220236
@@ -371,6 +387,12 @@ when not useNimNetLite:
371387 # # On posix this will search through the `/etc/services` file.
372388 when useWinVersion:
373389 var s = winlean.getservbyname (name, proto)
390+ elif defined (linux) and not defined (android):
391+ var se: posix.Servent
392+ var buf: array [1024 , char ]
393+ var s: ptr posix.Servent
394+ discard getservbyname_r (name.cstring , proto.cstring , addr se,
395+ cast [cstring ](addr buf[0 ]), csize_t (buf.len), addr s)
374396 else :
375397 var s = posix.getservbyname (name, proto)
376398 if s == nil : raiseOSError (osLastError (), " Service not found." )
@@ -389,6 +411,12 @@ when not useNimNetLite:
389411 # # On posix this will search through the `/etc/services` file.
390412 when useWinVersion:
391413 var s = winlean.getservbyport (uint16 (port).cint , proto)
414+ elif defined (linux) and not defined (android):
415+ var se: posix.Servent
416+ var buf: array [1024 , char ]
417+ var s: ptr posix.Servent
418+ discard getservbyport_r (uint16 (port).cint , proto.cstring , addr se,
419+ cast [cstring ](addr buf[0 ]), csize_t (buf.len), addr s)
392420 else :
393421 var s = posix.getservbyport (uint16 (port).cint , proto)
394422 if s == nil : raiseOSError (osLastError (), " Service not found." )
@@ -424,14 +452,26 @@ when not useNimNetLite:
424452 var s = winlean.gethostbyaddr (cast [ptr InAddr ](myAddr), addrLen.cuint ,
425453 cint (family))
426454 if s == nil : raiseOSError (osLastError ())
455+ elif defined (linux):
456+ var he: posix.Hostent
457+ var buf: array [4096 , char ]
458+ var h_errnop: cint
459+ var s: ptr posix.Hostent
460+ when defined (android4):
461+ discard gethostbyaddr_r (cast [cstring ](myAddr), addrLen.SockLen ,
462+ cint (family), addr he,
463+ cast [cstring ](addr buf[0 ]), csize_t (buf.len),
464+ addr s, addr h_errnop)
465+ else :
466+ discard gethostbyaddr_r (myAddr, addrLen.SockLen ,
467+ cint (family), addr he,
468+ cast [cstring ](addr buf[0 ]), csize_t (buf.len),
469+ addr s, addr h_errnop)
470+ if s == nil :
471+ raiseOSError (osLastError (), $ hstrerror (h_errnop))
427472 else :
428- var s =
429- when defined (android4):
430- posix.gethostbyaddr (cast [cstring ](myAddr), addrLen.cint ,
431- cint (family))
432- else :
433- posix.gethostbyaddr (myAddr, addrLen.SockLen ,
434- cint (family))
473+ # macOS: gethostbyaddr is thread-safe via TLS
474+ var s = posix.gethostbyaddr (myAddr, addrLen.SockLen , cint (family))
435475 if s == nil :
436476 raiseOSError (osLastError (), $ hstrerror (h_errno))
437477
@@ -476,6 +516,14 @@ when not useNimNetLite:
476516 # # This function will lookup the IP address of a hostname.
477517 when useWinVersion:
478518 var s = winlean.gethostbyname (name)
519+ elif defined (linux):
520+ var he: posix.Hostent
521+ var buf: array [4096 , char ]
522+ var h_errnop: cint
523+ var s: ptr posix.Hostent
524+ discard gethostbyname_r (name.cstring , addr he,
525+ cast [cstring ](addr buf[0 ]), csize_t (buf.len),
526+ addr s, addr h_errnop)
479527 else :
480528 var s = posix.gethostbyname (name)
481529 if s == nil : raiseOSError (osLastError ())
0 commit comments