/** \file servport.c Resolve service name to port number.
* \author Matthias Andree
- * \date 2005
+ * \date 2005 - 2006
*
* Copyright (C) 2005 by Matthias Andree
* For license terms, see the file COPYING in this directory.
#include <sys/socket.h>
int servport(const char *service) {
- int port;
+ int port, e;
unsigned long u;
char *end;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
- if (getaddrinfo(NULL, service, &hints, &res)) {
+ hints.ai_protocol = IPPROTO_TCP;
+ e = fm_getaddrinfo(NULL, service, &hints, &res);
+ if (e) {
+ report(stderr, GT_("getaddrinfo(NULL, \"%s\") error: %s\n"),
+ service, gai_strerror(e));
goto err;
} else {
switch(res->ai_addr->sa_family) {
case AF_INET:
port = ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port);
+ break;
#ifdef AF_INET6
case AF_INET6:
port = ntohs(((struct sockaddr_in6 *)res->ai_addr)->sin6_port);
+ break;
#endif
default:
+ fm_freeaddrinfo(res);
goto err;
}
- freeaddrinfo(res);
+ fm_freeaddrinfo(res);
}
} else {
if (u == 0 || u > 65535)
return port;
err:
- report(stderr, GT_("Cannot resolve service %s to port. Please specify the service as decimal port number.\n"), service);
+ report(stderr, GT_("Cannot resolve service %s to port number.\n"), service);
+ report(stderr, GT_("Please specify the service as decimal port number.\n"));
return -1;
}
/* end of servport.c */