From 5528a3df5cc55d083b5aa1926843e810467070a5 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 24 Sep 2009 17:30:56 +0200 Subject: [PATCH] src/common.[ch]: Implement "service_name_to_port_number". It returns the numeric representation of a service name. The implementation has been taken from the tokyotyrant plugin. --- src/common.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++- src/common.h | 6 ++++- src/tokyotyrant.c | 52 +--------------------------------------- 3 files changed, 65 insertions(+), 53 deletions(-) diff --git a/src/common.c b/src/common.c index 7c2c30ec..1ddb71dd 100644 --- a/src/common.c +++ b/src/common.c @@ -1,6 +1,6 @@ /** * 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 @@ -43,6 +43,11 @@ # include #endif +/* for getaddrinfo */ +#include +#include +#include + #ifdef HAVE_LIBKSTAT extern kstat_ctl_t *kc; #endif @@ -1071,3 +1076,56 @@ counter_t counter_diff (counter_t old_value, counter_t new_value) 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 6682e1c8..019e8b69 100644 --- a/src/common.h +++ b/src/common.h @@ -1,6 +1,6 @@ /** * 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 @@ -285,4 +285,8 @@ int read_file_contents (const char *filename, char *buf, int bufsize); 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 26366c92..6618dc1a 100644 --- a/src/tokyotyrant.c +++ b/src/tokyotyrant.c @@ -46,56 +46,6 @@ static char *config_port = NULL; 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) @@ -171,7 +121,7 @@ static void tt_open_db (void) if (config_port != NULL) { - port = parse_service_name (config_port); + port = service_name_to_port_number (config_port); if (port <= 0) return; } -- 2.30.2