2 * libcollectdclient - src/libcollectdclient/client.c
3 * Copyright (C) 2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
22 /* Set to C99 and POSIX code */
23 #ifndef _ISOC99_SOURCE
24 # define _ISOC99_SOURCE
27 # define _POSIX_SOURCE
29 #ifndef _POSIX_C_SOURCE
30 # define _POSIX_C_SOURCE 200112L
36 /* Disable non-standard extensions */
47 #if !defined(__GNUC__) || !__GNUC__
48 # define __attribute__(x) /**/
55 #include "lcc_features.h"
60 #include <sys/types.h>
61 #include <sys/socket.h>
71 /* NI_MAXHOST has been obsoleted by RFC 3493 which is a reason for SunOS 5.11
72 * to no longer define it. We'll use the old, RFC 2553 value here. */
74 # define NI_MAXHOST 1025
77 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
78 * termination. They work for static buffers only, because they use `sizeof'.
79 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
80 * is very useful to add formatted stuff to the end of a buffer. */
81 #define SSTRCPY(d,s) do { \
82 strncpy ((d), (s), sizeof (d)); \
83 (d)[sizeof (d) - 1] = 0; \
86 #define SSTRCAT(d,s) do { \
87 size_t _l = strlen (d); \
88 strncpy ((d) + _l, (s), sizeof (d) - _l); \
89 (d)[sizeof (d) - 1] = 0; \
92 #define SSTRCATF(d, ...) do { \
93 char _b[sizeof (d)]; \
94 snprintf (_b, sizeof (_b), __VA_ARGS__); \
95 _b[sizeof (_b) - 1] = 0; \
100 #define LCC_SET_ERRSTR(c, ...) do { \
101 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
102 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
106 # define LCC_DEBUG(...) printf (__VA_ARGS__)
108 # define LCC_DEBUG(...) /**/
114 struct lcc_connection_s
120 struct lcc_response_s
127 typedef struct lcc_response_s lcc_response_t;
132 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
137 strerror_r (err, c->errbuf, sizeof (c->errbuf));
138 c->errbuf[sizeof (c->errbuf) - 1] = 0;
141 } /* }}} int lcc_set_errno */
143 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
145 __attribute__((malloc, nonnull (1)))
146 static char *lcc_strdup (const char *str) /* {{{ */
151 strsize = strlen (str) + 1;
152 ret = (char *) malloc (strsize);
154 memcpy (ret, str, strsize);
156 } /* }}} char *lcc_strdup */
158 __attribute__((nonnull (1, 2)))
159 static char *lcc_strescape (char *dest, const char *src, size_t dest_size) /* {{{ */
167 assert (dest_size >= 3);
169 dest[dest_pos] = '"';
174 if ((dest_pos == (dest_size - 2))
175 || (src[src_pos] == 0))
178 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
180 /* Check if there is enough space for both characters.. */
181 if (dest_pos == (dest_size - 3))
184 dest[dest_pos] = '\\';
188 dest[dest_pos] = src[src_pos];
193 assert (dest_pos <= (dest_size - 2));
195 dest[dest_pos] = '"';
203 } /* }}} char *lcc_strescape */
205 /* lcc_chomp: Removes all control-characters at the end of a string. */
206 static void lcc_chomp (char *str) /* {{{ */
210 str_len = strlen (str);
213 if (str[str_len - 1] >= 32)
215 str[str_len - 1] = 0;
218 } /* }}} void lcc_chomp */
220 static void lcc_response_free (lcc_response_t *res) /* {{{ */
227 for (i = 0; i < res->lines_num; i++)
228 free (res->lines[i]);
231 } /* }}} void lcc_response_free */
233 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
237 LCC_DEBUG ("send: --> %s\n", command);
239 status = fprintf (c->fh, "%s\r\n", command);
242 lcc_set_errno (c, errno);
247 } /* }}} int lcc_send */
249 static int lcc_receive (lcc_connection_t *c, /* {{{ */
250 lcc_response_t *ret_res)
257 memset (&res, 0, sizeof (res));
259 /* Read the first line, containing the status and a message */
260 ptr = fgets (buffer, sizeof (buffer), c->fh);
263 lcc_set_errno (c, errno);
267 LCC_DEBUG ("receive: <-- %s\n", buffer);
269 /* Convert the leading status to an integer and make `ptr' to point to the
270 * beginning of the message. */
273 res.status = strtol (buffer, &ptr, 0);
274 if ((errno != 0) || (ptr == &buffer[0]))
276 lcc_set_errno (c, errno);
280 /* Skip white spaces after the status number */
281 while ((*ptr == ' ') || (*ptr == '\t'))
284 /* Now copy the message. */
285 strncpy (res.message, ptr, sizeof (res.message));
286 res.message[sizeof (res.message) - 1] = 0;
288 /* Error or no lines follow: We're done. */
291 memcpy (ret_res, &res, sizeof (res));
295 /* Allocate space for the char-pointers */
296 res.lines_num = (size_t) res.status;
298 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
299 if (res.lines == NULL)
301 lcc_set_errno (c, ENOMEM);
305 /* Now receive all the lines */
306 for (i = 0; i < res.lines_num; i++)
308 ptr = fgets (buffer, sizeof (buffer), c->fh);
311 lcc_set_errno (c, errno);
315 LCC_DEBUG ("receive: <-- %s\n", buffer);
317 res.lines[i] = lcc_strdup (buffer);
318 if (res.lines[i] == NULL)
320 lcc_set_errno (c, ENOMEM);
325 /* Check if the for-loop exited with an error. */
326 if (i < res.lines_num)
337 memcpy (ret_res, &res, sizeof (res));
339 } /* }}} int lcc_receive */
341 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
342 const char *command, lcc_response_t *ret_res)
347 status = lcc_send (c, command);
351 memset (&res, 0, sizeof (res));
352 status = lcc_receive (c, &res);
354 memcpy (ret_res, &res, sizeof (*ret_res));
357 } /* }}} int lcc_sendreceive */
359 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
361 struct sockaddr_un sa;
366 assert (c->fh == NULL);
367 assert (path != NULL);
369 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
371 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
374 lcc_set_errno (c, errno);
378 memset (&sa, 0, sizeof (sa));
379 sa.sun_family = AF_UNIX;
380 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
382 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
385 lcc_set_errno (c, errno);
390 c->fh = fdopen (fd, "r+");
393 lcc_set_errno (c, errno);
399 } /* }}} int lcc_open_unixsocket */
401 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
402 const char *addr_orig)
404 struct addrinfo ai_hints;
405 struct addrinfo *ai_res;
406 struct addrinfo *ai_ptr;
407 char addr_copy[NI_MAXHOST];
414 assert (c->fh == NULL);
415 assert (addr_orig != NULL);
417 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
418 addr_copy[sizeof(addr_copy) - 1] = '\0';
421 memset (&ai_hints, 0, sizeof (ai_hints));
422 ai_hints.ai_flags = 0;
424 ai_hints.ai_flags |= AI_ADDRCONFIG;
426 ai_hints.ai_family = AF_UNSPEC;
427 ai_hints.ai_socktype = SOCK_STREAM;
430 if (*addr == '[') /* IPv6+port format */
432 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
435 port = strchr (addr, ']');
438 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
450 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
453 } /* if (*addr = ']') */
454 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
456 port = strrchr (addr, ':');
465 status = getaddrinfo (addr,
466 port == NULL ? LCC_DEFAULT_PORT : port,
470 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
474 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
476 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
484 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
493 c->fh = fdopen (fd, "r+");
502 assert (status == 0);
508 lcc_set_errno (c, status);
513 } /* }}} int lcc_open_netsocket */
515 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
523 assert (c->fh == NULL);
524 assert (addr != NULL);
526 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
527 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
528 else if (addr[0] == '/')
529 status = lcc_open_unixsocket (c, addr);
531 status = lcc_open_netsocket (c, addr);
534 } /* }}} int lcc_open_socket */
539 unsigned int lcc_version (void) /* {{{ */
541 return (LCC_VERSION);
542 } /* }}} unsigned int lcc_version */
544 const char *lcc_version_string (void) /* {{{ */
546 return (LCC_VERSION_STRING);
547 } /* }}} const char *lcc_version_string */
549 const char *lcc_version_extra (void) /* {{{ */
551 return (LCC_VERSION_EXTRA);
552 } /* }}} const char *lcc_version_extra */
554 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
564 c = (lcc_connection_t *) malloc (sizeof (*c));
567 memset (c, 0, sizeof (*c));
570 return (lcc_open_socket (c, address));
571 } /* }}} int lcc_connect */
573 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
586 } /* }}} int lcc_disconnect */
588 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
589 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
591 char ident_str[6 * LCC_NAME_LEN];
592 char ident_esc[12 * LCC_NAME_LEN];
593 char command[14 * LCC_NAME_LEN];
597 gauge_t *values = NULL;
598 char **values_names = NULL;
608 lcc_set_errno (c, EINVAL);
612 /* Build a commend with an escaped version of the identifier string. */
613 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
617 snprintf (command, sizeof (command), "GETVAL %s",
618 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
619 command[sizeof (command) - 1] = 0;
621 /* Send talk to the daemon.. */
622 status = lcc_sendreceive (c, command, &res);
628 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
629 lcc_response_free (&res);
633 values_num = res.lines_num;
635 #define BAIL_OUT(e) do { \
636 lcc_set_errno (c, (e)); \
638 if (values_names != NULL) { \
639 for (i = 0; i < values_num; i++) { \
640 free (values_names[i]); \
643 free (values_names); \
644 lcc_response_free (&res); \
648 /* If neither the values nor the names are requested, return here.. */
649 if ((ret_values == NULL) && (ret_values_names == NULL))
651 if (ret_values_num != NULL)
652 *ret_values_num = values_num;
653 lcc_response_free (&res);
657 /* Allocate space for the values */
658 if (ret_values != NULL)
660 values = (gauge_t *) malloc (values_num * sizeof (*values));
665 if (ret_values_names != NULL)
667 values_names = (char **) calloc (values_num, sizeof (*values_names));
668 if (values_names == NULL)
672 for (i = 0; i < res.lines_num; i++)
679 value = strchr (key, '=');
690 values[i] = strtod (value, &endptr);
692 if ((endptr == value) || (errno != 0))
696 if (values_names != NULL)
698 values_names[i] = lcc_strdup (key);
699 if (values_names[i] == NULL)
702 } /* for (i = 0; i < res.lines_num; i++) */
704 if (ret_values_num != NULL)
705 *ret_values_num = values_num;
706 if (ret_values != NULL)
707 *ret_values = values;
708 if (ret_values_names != NULL)
709 *ret_values_names = values_names;
712 } /* }}} int lcc_getval */
714 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
716 char ident_str[6 * LCC_NAME_LEN];
717 char ident_esc[12 * LCC_NAME_LEN];
718 char command[1024] = "";
723 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
724 || (vl->values == NULL) || (vl->values_types == NULL))
726 lcc_set_errno (c, EINVAL);
730 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
735 SSTRCATF (command, "PUTVAL %s",
736 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
738 if (vl->interval > 0)
739 SSTRCATF (command, " interval=%i", vl->interval);
742 SSTRCATF (command, "%u", (unsigned int) vl->time);
744 SSTRCAT (command, "N");
746 for (i = 0; i < vl->values_len; i++)
748 if (vl->values_types[i] == LCC_TYPE_COUNTER)
749 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
750 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
752 if (isnan (vl->values[i].gauge))
753 SSTRCPY (command, ":U");
755 SSTRCATF (command, ":%g", vl->values[i].gauge);
757 } /* for (i = 0; i < vl->values_len; i++) */
759 status = lcc_sendreceive (c, command, &res);
765 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
766 lcc_response_free (&res);
770 lcc_response_free (&res);
772 } /* }}} int lcc_putval */
774 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
775 lcc_identifier_t *ident, int timeout)
777 char command[1024] = "";
783 lcc_set_errno (c, EINVAL);
787 SSTRCPY (command, "FLUSH");
790 SSTRCATF (command, " timeout=%i", timeout);
794 char buffer[2 * LCC_NAME_LEN];
795 SSTRCATF (command, " plugin=%s",
796 lcc_strescape (buffer, plugin, sizeof (buffer)));
801 char ident_str[6 * LCC_NAME_LEN];
802 char ident_esc[12 * LCC_NAME_LEN];
804 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
808 SSTRCATF (command, " identifier=%s",
809 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
812 status = lcc_sendreceive (c, command, &res);
818 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
819 lcc_response_free (&res);
823 lcc_response_free (&res);
825 } /* }}} int lcc_flush */
827 /* TODO: Implement lcc_putnotif */
829 int lcc_listval (lcc_connection_t *c, /* {{{ */
830 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
836 lcc_identifier_t *ident;
842 if ((ret_ident == NULL) || (ret_ident_num == NULL))
844 lcc_set_errno (c, EINVAL);
848 status = lcc_sendreceive (c, "LISTVAL", &res);
854 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
855 lcc_response_free (&res);
859 ident_num = res.lines_num;
860 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
863 lcc_response_free (&res);
864 lcc_set_errno (c, ENOMEM);
868 for (i = 0; i < res.lines_num; i++)
873 /* First field is the time. */
874 time_str = res.lines[i];
876 /* Set `ident_str' to the beginning of the second field. */
877 ident_str = time_str;
878 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
880 while ((*ident_str == ' ') || (*ident_str == '\t'))
888 lcc_set_errno (c, EPROTO);
893 status = lcc_string_to_identifier (c, ident + i, ident_str);
898 lcc_response_free (&res);
907 *ret_ident_num = ident_num;
910 } /* }}} int lcc_listval */
912 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
915 return ("Invalid object");
917 } /* }}} const char *lcc_strerror */
919 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
920 char *string, size_t string_size, const lcc_identifier_t *ident)
922 if ((string == NULL) || (string_size < 6) || (ident == NULL))
924 lcc_set_errno (c, EINVAL);
928 if (ident->plugin_instance[0] == 0)
930 if (ident->type_instance[0] == 0)
931 snprintf (string, string_size, "%s/%s/%s",
936 snprintf (string, string_size, "%s/%s/%s-%s",
940 ident->type_instance);
944 if (ident->type_instance[0] == 0)
945 snprintf (string, string_size, "%s/%s-%s/%s",
948 ident->plugin_instance,
951 snprintf (string, string_size, "%s/%s-%s/%s-%s",
954 ident->plugin_instance,
956 ident->type_instance);
959 string[string_size - 1] = 0;
961 } /* }}} int lcc_identifier_to_string */
963 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
964 lcc_identifier_t *ident, const char *string)
969 char *plugin_instance;
973 string_copy = lcc_strdup (string);
974 if (string_copy == NULL)
976 lcc_set_errno (c, ENOMEM);
981 plugin = strchr (host, '/');
984 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
991 type = strchr (plugin, '/');
994 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1001 plugin_instance = strchr (plugin, '-');
1002 if (plugin_instance != NULL)
1004 *plugin_instance = 0;
1008 type_instance = strchr (type, '-');
1009 if (type_instance != NULL)
1015 memset (ident, 0, sizeof (*ident));
1017 SSTRCPY (ident->host, host);
1018 SSTRCPY (ident->plugin, plugin);
1019 if (plugin_instance != NULL)
1020 SSTRCPY (ident->plugin_instance, plugin_instance);
1021 SSTRCPY (ident->type, type);
1022 if (type_instance != NULL)
1023 SSTRCPY (ident->type_instance, type_instance);
1027 } /* }}} int lcc_string_to_identifier */
1029 /* vim: set sw=2 sts=2 et fdm=marker : */