summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 96d86b5)
raw | patch | inline | side by side (parent: 96d86b5)
author | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Thu, 24 Sep 2009 15:30:56 +0000 (17:30 +0200) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Thu, 24 Sep 2009 15:30:56 +0000 (17:30 +0200) |
It returns the numeric representation of a service name. The implementation
has been taken from the tokyotyrant plugin.
has been taken from the tokyotyrant plugin.
src/common.c | patch | blob | history | |
src/common.h | patch | blob | history | |
src/tokyotyrant.c | patch | blob | history |
diff --git a/src/common.c b/src/common.c
index 7c2c30eccf38b1bcc498264727679b6be3270031..1ddb71ddcd1077f211eeb5b904748ddc8204fc22 100644 (file)
--- a/src/common.c
+++ b/src/common.c
/**
* 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 */
diff --git a/src/common.h b/src/common.h
index 6682e1c852ffd3328253f347a54fa23de78f630b..019e8b69f9817e673df2b7f3f673401d91e7579f 100644 (file)
--- a/src/common.h
+++ b/src/common.h
/**
* 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 */
diff --git a/src/tokyotyrant.c b/src/tokyotyrant.c
index 26366c928373aee0c4c12e91d3013d30b34d3c4f..6618dc1a9422d4d3053039c0315f669cef4a65e6 100644 (file)
--- a/src/tokyotyrant.c
+++ b/src/tokyotyrant.c
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;
}