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.
14 #include <sys/types.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <sys/socket.h>
20 int servport(const char *service) {
29 * Check if the service is a number. If so, convert it.
30 * If it isn't a number, call getservbyname to resolve it.
33 u = strtoul(service, &end, 10);
34 if (errno || end[strspn(end, POSIX_space)] != '\0') {
35 struct addrinfo hints, *res;
37 /* hardcode kpop to port 1109 as per fetchmail(1)
38 * manual page, it's not a IANA registered service */
39 if (strcmp(service, "kpop") == 0)
42 memset(&hints, 0, sizeof hints);
43 hints.ai_family = AF_UNSPEC;
44 hints.ai_socktype = SOCK_STREAM;
45 hints.ai_protocol = IPPROTO_TCP;
46 e = fm_getaddrinfo(NULL, service, &hints, &res);
48 report(stderr, GT_("getaddrinfo(NULL, \"%s\") error: %s\n"),
49 service, gai_strerror(e));
52 switch(res->ai_addr->sa_family) {
54 port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
58 port = ntohs(((struct sockaddr_in6 *)res->ai_addr)->sin6_port);
68 if (u == 0 || u > 65535)
75 report(stderr, GT_("Cannot resolve service %s to port number.\n"), service);
76 report(stderr, GT_("Please specify the service as decimal port number.\n"));
79 /* end of servport.c */