aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile.am2
-rw-r--r--servport.c62
2 files changed, 63 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am
index 64587c70..39c8064b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -41,7 +41,7 @@ endif
fetchmail_SOURCES= fetchmail.h getopt.h \
i18n.h kerberos.h md5.h mx.h netrc.h ntlm.h \
smbbyteorder.h smbdes.h smbmd4.h smbencrypt.h smtp.h \
- socket.h tunable.h \
+ socket.h tunable.h servport.c \
socket.c getpass.c pop2.c pop3.c imap.c etrn.c \
odmr.c fetchmail.c env.c idle.c options.c daemon.c \
driver.c transact.c sink.c smtp.c \
diff --git a/servport.c b/servport.c
new file mode 100644
index 00000000..2f69faba
--- /dev/null
+++ b/servport.c
@@ -0,0 +1,62 @@
+/** \file servport.c Resolve service name to port number.
+ * \author Matthias Andree
+ * \date 2005
+ *
+ * Copyright (C) 2005 by Matthias Andree
+ * For license terms, see the file COPYING in this directory.
+ */
+#include "fetchmail.h"
+#include "i18n.h"
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <netdb.h>
+#ifdef HAVE_ARPA_INET_H
+#include <arpa/inet.h>
+#elif defined(HAVE_NETINET_IN_H)
+#include <netinet/in.h>
+#endif
+
+int servport(const char *service) {
+ int port;
+ unsigned long u;
+ char *end;
+
+ if (service == 0)
+ return -1;
+
+ /*
+ * Check if the service is a number. If so, convert it.
+ * If it isn't a number, call getservbyname to resolve it.
+ */
+ errno = 0;
+ u = strtoul(service, &end, 10);
+ if (errno || end[strspn(end, POSIX_space)] != '\0') {
+ struct servent *se;
+
+ /* hardcode kpop to port 1109 as per fetchmail(1)
+ * manual page, it's not a IANA registered service */
+ if (strcmp(service, "kpop") == 0)
+ return 1109;
+
+ se = getservbyname(service, "tcp");
+ if (se == NULL) {
+ endservent();
+ goto err;
+ } else {
+ port = ntohs(se->s_port);
+ endservent();
+ }
+ } else {
+ if (u == 0 || u > 65535)
+ goto err;
+ port = u;
+ }
+
+ return port;
+err:
+ report(stderr, GT_("Cannot resolve service %s to port. Please specify the service as decimal port number.\n"), service);
+ return -1;
+}
+/* end of servport.c */