/**
* collectd - src/common.c
- * Copyright (C) 2005-2008 Florian octo Forster
+ * Copyright (C) 2005-2009 Florian octo Forster
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
# include <arpa/inet.h>
#endif
+/* for getaddrinfo */
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+
#ifdef HAVE_LIBKSTAT
extern kstat_ctl_t *kc;
#endif
return (diff);
} /* counter_t counter_to_gauge */
+
+int service_name_to_port_number (const char *service_name)
+{
+ struct addrinfo *ai_list;
+ struct addrinfo *ai_ptr;
+ struct addrinfo ai_hints;
+ int status;
+ int service_number;
+
+ if (service_name == NULL)
+ return (-1);
+
+ ai_list = NULL;
+ memset (&ai_hints, 0, sizeof (ai_hints));
+ ai_hints.ai_family = AF_UNSPEC;
+
+ status = getaddrinfo (/* node = */ NULL, service_name,
+ &ai_hints, &ai_list);
+ if (status != 0)
+ {
+ ERROR ("service_name_to_port_number: getaddrinfo failed: %s",
+ gai_strerror (status));
+ return (-1);
+ }
+
+ service_number = -1;
+ for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
+ {
+ if (ai_ptr->ai_family == AF_INET)
+ {
+ struct sockaddr_in *sa;
+
+ sa = (void *) ai_ptr->ai_addr;
+ service_number = (int) ntohs (sa->sin_port);
+ }
+ else if (ai_ptr->ai_family == AF_INET6)
+ {
+ struct sockaddr_in6 *sa;
+
+ sa = (void *) ai_ptr->ai_addr;
+ service_number = (int) ntohs (sa->sin6_port);
+ }
+
+ if ((service_number > 0) && (service_number <= 65535))
+ break;
+ }
+
+ freeaddrinfo (ai_list);
+
+ if ((service_number > 0) && (service_number <= 65535))
+ return (service_number);
+ return (-1);
+} /* int service_name_to_port_number */
/**
* collectd - src/common.h
- * Copyright (C) 2005-2008 Florian octo Forster
+ * Copyright (C) 2005-2009 Florian octo Forster
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
counter_t counter_diff (counter_t old_value, counter_t new_value);
+/* Converts a service name (a string) to a port number
+ * (in the range [1-65535]). Returns less than zero on error. */
+int service_name_to_port_number (const char *service_name);
+
#endif /* COMMON_H */
static TCRDB *rdb = NULL;
-static int parse_service_name (const char *service_name)
-{
- struct addrinfo *ai_list;
- struct addrinfo *ai_ptr;
- struct addrinfo ai_hints;
- int status;
- int service_number;
-
- ai_list = NULL;
- memset (&ai_hints, 0, sizeof (ai_hints));
- ai_hints.ai_family = AF_UNSPEC;
-
- status = getaddrinfo (/* node = */ NULL, service_name,
- &ai_hints, &ai_list);
- if (status != 0)
- {
- ERROR ("tokyotyrant plugin: getaddrinfo failed: %s",
- gai_strerror (status));
- return (-1);
- }
-
- service_number = -1;
- for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
- {
- if (ai_ptr->ai_family == AF_INET)
- {
- struct sockaddr_in *sa;
-
- sa = (void *) ai_ptr->ai_addr;
- service_number = (int) ntohs (sa->sin_port);
- }
- else if (ai_ptr->ai_family == AF_INET6)
- {
- struct sockaddr_in6 *sa;
-
- sa = (void *) ai_ptr->ai_addr;
- service_number = (int) ntohs (sa->sin6_port);
- }
-
- if ((service_number > 0) && (service_number <= 65535))
- break;
- }
-
- freeaddrinfo (ai_list);
-
- if ((service_number > 0) && (service_number <= 65535))
- return (service_number);
- return (-1);
-} /* int parse_service_name */
-
static int tt_config (const char *key, const char *value)
{
if (strcasecmp ("Host", key) == 0)
if (config_port != NULL)
{
- port = parse_service_name (config_port);
+ port = service_name_to_port_number (config_port);
if (port <= 0)
return;
}