1 /** \file servport.c Resolve service name to port number.
2 * \author Matthias Andree
5 * Copyright (C) 2005 by Matthias Andree
6 * For license terms, see the file COPYING in this directory.
9 #include "getaddrinfo.h"
15 #include <sys/types.h>
17 #if defined(HAVE_NETINET_IN_H)
18 #include <netinet/in.h>
20 #ifdef HAVE_ARPA_INET_H
21 #include <arpa/inet.h>
23 #include <sys/socket.h>
25 int servport(const char *service) {
34 * Check if the service is a number. If so, convert it.
35 * If it isn't a number, call getservbyname to resolve it.
38 u = strtoul(service, &end, 10);
39 if (errno || end[strspn(end, POSIX_space)] != '\0') {
40 struct addrinfo hints, *res;
42 /* hardcode kpop to port 1109 as per fetchmail(1)
43 * manual page, it's not a IANA registered service */
44 if (strcmp(service, "kpop") == 0)
47 memset(&hints, 0, sizeof hints);
48 hints.ai_family = AF_UNSPEC;
49 hints.ai_socktype = SOCK_STREAM;
50 hints.ai_protocol = IPPROTO_TCP;
51 e = fm_getaddrinfo(NULL, service, &hints, &res);
53 report(stderr, GT_("getaddrinfo(NULL, \"%s\") error: %s\n"),
54 service, gai_strerror(e));
57 switch(res->ai_addr->sa_family) {
59 port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
63 port = ntohs(((struct sockaddr_in6 *)res->ai_addr)->sin6_port);
73 if (u == 0 || u > 65535)
80 report(stderr, GT_("Cannot resolve service %s to port number.\n"), service);
81 report(stderr, GT_("Please specify the service as decimal port number.\n"));
84 /* end of servport.c */