gio: use reentrant getservbyname_r() if available

This commit is contained in:
Thomas Haller
2023-08-11 12:08:42 +02:00
committed by Philip Withnall
parent 8aa889dbf1
commit f738c7f3db
5 changed files with 50 additions and 11 deletions

View File

@@ -23,6 +23,7 @@
#include "config.h"
#include "gnetworking.h"
#include "gnetworkingprivate.h"
/**
* SECTION:gnetworking
@@ -76,3 +77,26 @@ g_networking_init (void)
}
#endif
}
gboolean
g_getservbyname_ntohs (const char *name, const char *proto, guint16 *out_port)
{
struct servent *result;
#ifdef HAVE_GETSERVBYNAME_R
struct servent result_buf;
char buf[2048];
int r;
r = getservbyname_r (name, proto, &result_buf, buf, sizeof (buf), &result);
if (r != 0 || result != &result_buf)
result = NULL;
#else
result = getservbyname (name, proto);
#endif
if (!result)
return FALSE;
*out_port = g_ntohs (result->s_port);
return TRUE;
}