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