You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by jd...@apache.org on 2020/07/31 20:40:32 UTC

[qpid-dispatch] branch master updated: DISPATCH-960 Use getservbyname on macOS (getservbyname_r not available) (#804)

This is an automated email from the ASF dual-hosted git repository.

jdanek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-dispatch.git


The following commit(s) were added to refs/heads/master by this push:
     new 97ac134  DISPATCH-960 Use getservbyname on macOS (getservbyname_r not available) (#804)
97ac134 is described below

commit 97ac13417d77cd2f8a90acb837747abee438ed20
Author: Jiri Daněk <jd...@redhat.com>
AuthorDate: Fri Jul 31 22:40:22 2020 +0200

    DISPATCH-960 Use getservbyname on macOS (getservbyname_r not available) (#804)
---
 src/amqp.c | 46 ++++++++++++++++++++++++++++++++++++----------
 1 file changed, 36 insertions(+), 10 deletions(-)

diff --git a/src/amqp.c b/src/amqp.c
index dbe1885..97aec78 100644
--- a/src/amqp.c
+++ b/src/amqp.c
@@ -95,6 +95,35 @@ const char * const QD_AMQPS_PORT_STR = "5671";
 
 const char * const QD_AMQP_DFLT_PROTO = "tcp";
 
+/// Wrapper for getservbyname/getservbyname_r macOS compatibility.
+/// Needed because getservbyname is thread safe on macOS, and getservbyname_r is not defined there.
+static inline int qd_getservbyname(const char *name, const char *proto);
+
+#ifdef __APPLE__
+static inline int qd_getservbyname(const char *name, const char *proto) {
+    struct servent *serv_info = getservbyname(name, proto);
+    if (serv_info) {
+        return ntohs(serv_info->s_port);
+    } else {
+        return -1;
+    }
+}
+#else
+static inline int qd_getservbyname(const char *name, const char *proto) {
+    struct servent  serv_info;
+    struct servent *serv_info_res;
+    enum { buf_len = 4096 };
+    char buf[buf_len];
+
+    int r = getservbyname_r(name, proto, &serv_info, buf, buf_len, &serv_info_res);
+    if (r == 0 && serv_info_res != NULL) {
+        return ntohs(serv_info.s_port);
+    } else {
+        return -1;
+    }
+}
+#endif
+
 int qd_port_int(const char *port_str) {
     char *endptr;
     unsigned long n;
@@ -116,15 +145,12 @@ int qd_port_int(const char *port_str) {
     if (endptr != port_str) return -1;
 
     // resolve service port
-    struct servent  serv_info;
-    struct servent *serv_info_res;
-    enum { buf_len = 4096 };
-    char buf[buf_len];
+    const int r = qd_getservbyname(port_str, QD_AMQP_DFLT_PROTO);
+    if (r != -1) return r;
 
-    int r = getservbyname_r(port_str, QD_AMQP_DFLT_PROTO, &serv_info, buf, buf_len, &serv_info_res);
-    if (r == 0 && serv_info_res != NULL) {
-        return ntohs(serv_info.s_port);
-    } else {
-        return -1;
-    }
+    // amqp(s) not defined in /etc/services?
+    if (!strcmp(port_str, "amqp")) return QD_AMQP_PORT_INT;
+    if (!strcmp(port_str, "amqps")) return QD_AMQPS_PORT_INT;
+
+    return -1;
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org