From c2ffa5b95f768f14ea3b1e30eed621c97ca1a025 Mon Sep 17 00:00:00 2001 From: Mikko Lehto Date: Tue, 26 Jan 2016 00:20:01 +0200 Subject: [PATCH 1/4] modules/statsd: fixes documented module parameter type --- modules/statsd/README | 8 ++++---- modules/statsd/doc/statsd_admin.xml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/statsd/README b/modules/statsd/README index 4d33f554926..06dd6988fb6 100644 --- a/modules/statsd/README +++ b/modules/statsd/README @@ -21,7 +21,7 @@ Eloy Coto Pereiro 2. Parameters 2.1. ip(string) - 2.2. port(int) + 2.2. port(string) 3. Functions @@ -51,7 +51,7 @@ Chapter 1. Admin Guide 2. Parameters 2.1. ip(string) - 2.2. port(int) + 2.2. port(string) 3. Functions @@ -75,7 +75,7 @@ Chapter 1. Admin Guide 2. Parameters 2.1. ip(string) - 2.2. port(int) + 2.2. port(string) 2.1. ip(string) @@ -86,7 +86,7 @@ Chapter 1. Admin Guide modparam("statsd", "ip", "127.0.0.1") ... -2.2. port(int) +2.2. port(string) Statsd server ip diff --git a/modules/statsd/doc/statsd_admin.xml b/modules/statsd/doc/statsd_admin.xml index 4e299d23496..80d9f92a24b 100644 --- a/modules/statsd/doc/statsd_admin.xml +++ b/modules/statsd/doc/statsd_admin.xml @@ -45,7 +45,7 @@ modparam("statsd", "ip", "127.0.0.1")
- <varname>port</varname>(int) + <varname>port</varname>(string) Statsd server ip From 20cd36c671972d9ebb180884b70e1fd91a92a1cc Mon Sep 17 00:00:00 2001 From: Mikko Lehto Date: Mon, 25 Jan 2016 23:19:06 +0200 Subject: [PATCH 2/4] modules/statsd: enables IPv6 by using result from getaddrinfo() --- modules/statsd/lib_statsd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/statsd/lib_statsd.c b/modules/statsd/lib_statsd.c index 6492ef219b9..dedb41b6555 100644 --- a/modules/statsd/lib_statsd.c +++ b/modules/statsd/lib_statsd.c @@ -44,7 +44,7 @@ bool statsd_connect(void){ return false; } - statsd_socket.sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + statsd_socket.sock = socket(serverAddr->ai_family, SOCK_DGRAM, IPPROTO_UDP); if (statsd_socket.sock == 0 ){ LM_ERR("Statsd: could not initiate a connect to statsd\n"); return false; @@ -52,6 +52,7 @@ bool statsd_connect(void){ rc = connect( statsd_socket.sock, serverAddr->ai_addr, serverAddr->ai_addrlen); + freeaddrinfo(serverAddr); if (rc < 0){ LM_ERR("Statsd: could not initiate a connect to statsd\n"); return false; From 8ead5ae6f4cd0b64226a838fcc03c9beca219d98 Mon Sep 17 00:00:00 2001 From: Mikko Lehto Date: Tue, 26 Jan 2016 00:36:01 +0200 Subject: [PATCH 3/4] modules/statsd: combine sock structure and error tracking variable --- modules/statsd/lib_statsd.c | 31 ++++++++++++------------------- modules/statsd/lib_statsd.h | 9 +-------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/modules/statsd/lib_statsd.c b/modules/statsd/lib_statsd.c index dedb41b6555..76744f0173f 100644 --- a/modules/statsd/lib_statsd.c +++ b/modules/statsd/lib_statsd.c @@ -11,47 +11,40 @@ #include "../../sr_module.h" #include "lib_statsd.h" -static StatsdSocket statsd_socket = { - "/var/run/statsd/statsd.sock", - -1, - 500, // timeout 500ms if no answer - 0, - "" -}; - static StatsConnection statsd_connection = { "127.0.0.1", - "8125" + "8125", + -1 }; bool statsd_connect(void){ struct addrinfo *serverAddr; - int rc, error; + int rc; - if (statsd_socket.sock > 0){ + if (statsd_connection.sock > 0){ return true; } - error = getaddrinfo( + rc = getaddrinfo( statsd_connection.ip, statsd_connection.port, NULL, &serverAddr); - if (error != 0) + if (rc != 0) { LM_ERR( "Statsd: could not initiate server information (%s)\n", - gai_strerror(error)); + gai_strerror(rc)); return false; } - statsd_socket.sock = socket(serverAddr->ai_family, SOCK_DGRAM, IPPROTO_UDP); - if (statsd_socket.sock == 0 ){ + statsd_connection.sock = socket(serverAddr->ai_family, SOCK_DGRAM, IPPROTO_UDP); + if (statsd_connection.sock == 0 ){ LM_ERR("Statsd: could not initiate a connect to statsd\n"); return false; } rc = connect( - statsd_socket.sock, serverAddr->ai_addr, serverAddr->ai_addrlen); + statsd_connection.sock, serverAddr->ai_addr, serverAddr->ai_addrlen); freeaddrinfo(serverAddr); if (rc < 0){ LM_ERR("Statsd: could not initiate a connect to statsd\n"); @@ -67,7 +60,7 @@ bool send_command(char *command){ return false; } - send_result = send(statsd_socket.sock, command, strlen(command), 0); + send_result = send(statsd_connection.sock, command, strlen(command), 0); if ( send_result < 0){ LM_ERR("could not send the correct info to statsd (%i| %s)\n", send_result, strerror(errno)); @@ -129,6 +122,6 @@ bool statsd_init(char *ip, char *port){ } bool statsd_destroy(void){ - statsd_socket.sock = 0; + statsd_connection.sock = 0; return true; } diff --git a/modules/statsd/lib_statsd.h b/modules/statsd/lib_statsd.h index d3064c21ea2..a80482cad77 100644 --- a/modules/statsd/lib_statsd.h +++ b/modules/statsd/lib_statsd.h @@ -5,16 +5,9 @@ typedef struct StatsConnection{ char *ip; char *port; + int sock; } StatsConnection; -typedef struct StatsdSocket { - char *name; // name - int sock; // socket - int timeout; // how many miliseconds to wait for an answer - time_t last_failure; // time of the last failure - char data[BUFFER_SIZE]; // buffer for the answer data -} StatsdSocket; - bool statsd_connect(void); bool send_command(char *command); bool statsd_set(char *key, char *value); From 06a9516af72c87b9aa6e709f72eb6472951f398b Mon Sep 17 00:00:00 2001 From: Mikko Lehto Date: Tue, 26 Jan 2016 00:38:20 +0200 Subject: [PATCH 4/4] modules/statsd: fixes incorrect socket() return value check and log message --- modules/statsd/lib_statsd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/statsd/lib_statsd.c b/modules/statsd/lib_statsd.c index 76744f0173f..878b45239b9 100644 --- a/modules/statsd/lib_statsd.c +++ b/modules/statsd/lib_statsd.c @@ -38,8 +38,8 @@ bool statsd_connect(void){ } statsd_connection.sock = socket(serverAddr->ai_family, SOCK_DGRAM, IPPROTO_UDP); - if (statsd_connection.sock == 0 ){ - LM_ERR("Statsd: could not initiate a connect to statsd\n"); + if (statsd_connection.sock < 0 ){ + LM_ERR("Statsd: could not create a socket for statsd connection\n"); return false; }