mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-11 23:16:14 +01:00
gio: use reentrant getservbyname_r() if available
This commit is contained in:
parent
8aa889dbf1
commit
f738c7f3db
@ -486,10 +486,7 @@ g_network_address_parse (const gchar *host_and_port,
|
|||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
struct servent *entry;
|
if (!g_getservbyname_ntohs (port, "tcp", &portnum))
|
||||||
|
|
||||||
entry = getservbyname (port, "tcp");
|
|
||||||
if (entry == NULL)
|
|
||||||
{
|
{
|
||||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
|
||||||
"Unknown service '%s' specified in hostname '%s'",
|
"Unknown service '%s' specified in hostname '%s'",
|
||||||
@ -501,8 +498,6 @@ g_network_address_parse (const gchar *host_and_port,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
portnum = g_ntohs (entry->s_port);
|
|
||||||
|
|
||||||
#ifdef HAVE_ENDSERVENT
|
#ifdef HAVE_ENDSERVENT
|
||||||
endservent ();
|
endservent ();
|
||||||
#endif
|
#endif
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include "gnetworking.h"
|
#include "gnetworking.h"
|
||||||
|
#include "gnetworkingprivate.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SECTION:gnetworking
|
* SECTION:gnetworking
|
||||||
@ -76,3 +77,26 @@ g_networking_init (void)
|
|||||||
}
|
}
|
||||||
#endif
|
#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;
|
||||||
|
}
|
||||||
|
@ -33,6 +33,8 @@ gint g_socket (gint domain,
|
|||||||
gint protocol,
|
gint protocol,
|
||||||
GError **error);
|
GError **error);
|
||||||
|
|
||||||
|
gboolean g_getservbyname_ntohs (const char *name, const char *proto, guint16 *out_port);
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|
||||||
#endif /* __G_NETWORKINGPRIVATE_H__ */
|
#endif /* __G_NETWORKINGPRIVATE_H__ */
|
||||||
|
@ -364,17 +364,17 @@ static GList *
|
|||||||
g_network_service_fallback_targets (GNetworkService *srv)
|
g_network_service_fallback_targets (GNetworkService *srv)
|
||||||
{
|
{
|
||||||
GSrvTarget *target;
|
GSrvTarget *target;
|
||||||
struct servent *entry;
|
gboolean has_port;
|
||||||
guint16 port;
|
guint16 port;
|
||||||
|
|
||||||
entry = getservbyname (srv->priv->service, "tcp");
|
has_port = g_getservbyname_ntohs (srv->priv->service, "tcp", &port);
|
||||||
port = entry ? g_ntohs (entry->s_port) : 0;
|
|
||||||
#ifdef HAVE_ENDSERVENT
|
#ifdef HAVE_ENDSERVENT
|
||||||
endservent ();
|
endservent ();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (entry == NULL)
|
if (!has_port)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
target = g_srv_target_new (srv->priv->domain, port, 0, 0);
|
target = g_srv_target_new (srv->priv->domain, port, 0, 0);
|
||||||
return g_list_append (NULL, target);
|
return g_list_append (NULL, target);
|
||||||
|
18
meson.build
18
meson.build
@ -756,6 +756,24 @@ if cc.has_function('memalign', prefix: '#include <stdlib.h>\n#include <malloc.h>
|
|||||||
glib_conf.set('HAVE_MEMALIGN', 1)
|
glib_conf.set('HAVE_MEMALIGN', 1)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
# For example on Openbsd, getservbyname_r() has a different signature.
|
||||||
|
# https://man.openbsd.org/getservbyname.3
|
||||||
|
if cc.compiles('''#include <netdb.h>
|
||||||
|
int main (int argc, char ** argv) {
|
||||||
|
int (*fcn)(const char *,
|
||||||
|
const char *,
|
||||||
|
struct servent *,
|
||||||
|
char *,
|
||||||
|
size_t,
|
||||||
|
struct servent **) = getservbyname_r;
|
||||||
|
(void) fcn;
|
||||||
|
return 0;
|
||||||
|
}''',
|
||||||
|
name : 'getservbyname_r()',
|
||||||
|
args: '-Werror=incompatible-pointer-types')
|
||||||
|
glib_conf.set('HAVE_GETSERVBYNAME_R', 1)
|
||||||
|
endif
|
||||||
|
|
||||||
if cc.has_function('_aligned_malloc', prefix: '#include <malloc.h>')
|
if cc.has_function('_aligned_malloc', prefix: '#include <malloc.h>')
|
||||||
glib_conf.set('HAVE__ALIGNED_MALLOC', 1)
|
glib_conf.set('HAVE__ALIGNED_MALLOC', 1)
|
||||||
endif
|
endif
|
||||||
|
Loading…
Reference in New Issue
Block a user