From: Michal Nazarewicz <min...@mina86.com>

If $MPD_PORT is not a number, i.e. does not start with a digit,
attempt to resolve it using getservbyname, i.e. by reading the
/etc/services database.
---
 src/settings.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/settings.c b/src/settings.c
index f2799df..f153452 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -30,9 +30,20 @@
 #include "config.h"
 
 #include <assert.h>
+#include <ctype.h>
 #include <string.h>
 #include <stdlib.h>
 
+#ifdef ENABLE_TCP
+#  ifdef WIN32
+#    include <winsock2.h>
+#  else
+#    include <arpa/inet.h>
+#    include <netdb.h>
+#    include <netinet/in.h>
+#  endif
+#endif
+
 struct mpd_settings {
        char *host;
 
@@ -100,6 +111,26 @@ mpd_check_host(const char *host, char **password_r)
 }
 
 /**
+ * Parse port number (which can be a service name).
+ */
+static unsigned
+mpd_parse_port(const char *str)
+{
+       if (!*str)
+               return 0;
+       if (isdigit(str[0]))
+               return atoi(str);
+
+#ifdef ENABLE_TCP
+       struct servent *servent = getservbyname(str, "tcp");
+       if (servent)
+               return ntohs(servent->s_port);
+#endif
+
+       return 0;
+}
+
+/**
  * Parses the port specification.  If not specified (0), it attempts
  * to load it from the environment variable MPD_PORT.
  */
@@ -109,7 +140,7 @@ mpd_check_port(unsigned port)
        if (port == 0) {
                const char *env_port = getenv("MPD_PORT");
                if (env_port != NULL)
-                       port = atoi(env_port);
+                       port = mpd_parse_port(env_port);
        }
 
        return port;
-- 
2.2.0.rc0.207.ga3a616c

_______________________________________________
mpd-devel mailing list
mpd-devel@musicpd.org
http://mailman.blarg.de/listinfo/mpd-devel

Reply via email to