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)
349 lcc_set_errno (c, EBADF);
353 status = lcc_send (c, command);
357 memset (&res, 0, sizeof (res));
358 status = lcc_receive (c, &res);
360 memcpy (ret_res, &res, sizeof (*ret_res));
363 } /* }}} int lcc_sendreceive */
365 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
367 struct sockaddr_un sa;
372 assert (c->fh == NULL);
373 assert (path != NULL);
375 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
377 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
380 lcc_set_errno (c, errno);
384 memset (&sa, 0, sizeof (sa));
385 sa.sun_family = AF_UNIX;
386 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
388 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
391 lcc_set_errno (c, errno);
396 c->fh = fdopen (fd, "r+");
399 lcc_set_errno (c, errno);
405 } /* }}} int lcc_open_unixsocket */
407 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
408 const char *addr_orig)
410 struct addrinfo ai_hints;
411 struct addrinfo *ai_res;
412 struct addrinfo *ai_ptr;
413 char addr_copy[NI_MAXHOST];
420 assert (c->fh == NULL);
421 assert (addr_orig != NULL);
423 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
424 addr_copy[sizeof(addr_copy) - 1] = '\0';
427 memset (&ai_hints, 0, sizeof (ai_hints));
428 ai_hints.ai_flags = 0;
430 ai_hints.ai_flags |= AI_ADDRCONFIG;
432 ai_hints.ai_family = AF_UNSPEC;
433 ai_hints.ai_socktype = SOCK_STREAM;
436 if (*addr == '[') /* IPv6+port format */
438 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
441 port = strchr (addr, ']');
444 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
456 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
459 } /* if (*addr = ']') */
460 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
462 port = strrchr (addr, ':');
471 status = getaddrinfo (addr,
472 port == NULL ? LCC_DEFAULT_PORT : port,
476 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
480 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
482 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
490 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
499 c->fh = fdopen (fd, "r+");
508 assert (status == 0);
514 lcc_set_errno (c, status);
519 } /* }}} int lcc_open_netsocket */
521 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
529 assert (c->fh == NULL);
530 assert (addr != NULL);
532 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
533 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
534 else if (addr[0] == '/')
535 status = lcc_open_unixsocket (c, addr);
537 status = lcc_open_netsocket (c, addr);
540 } /* }}} int lcc_open_socket */
545 unsigned int lcc_version (void) /* {{{ */
547 return (LCC_VERSION);
548 } /* }}} unsigned int lcc_version */
550 const char *lcc_version_string (void) /* {{{ */
552 return (LCC_VERSION_STRING);
553 } /* }}} const char *lcc_version_string */
555 const char *lcc_version_extra (void) /* {{{ */
557 return (LCC_VERSION_EXTRA);
558 } /* }}} const char *lcc_version_extra */
560 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
571 c = (lcc_connection_t *) malloc (sizeof (*c));
574 memset (c, 0, sizeof (*c));
576 status = lcc_open_socket (c, address);
585 } /* }}} int lcc_connect */
587 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
600 } /* }}} int lcc_disconnect */
602 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
603 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
605 char ident_str[6 * LCC_NAME_LEN];
606 char ident_esc[12 * LCC_NAME_LEN];
607 char command[14 * LCC_NAME_LEN];
611 gauge_t *values = NULL;
612 char **values_names = NULL;
622 lcc_set_errno (c, EINVAL);
626 /* Build a commend with an escaped version of the identifier string. */
627 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
631 snprintf (command, sizeof (command), "GETVAL %s",
632 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
633 command[sizeof (command) - 1] = 0;
635 /* Send talk to the daemon.. */
636 status = lcc_sendreceive (c, command, &res);
642 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
643 lcc_response_free (&res);
647 values_num = res.lines_num;
649 #define BAIL_OUT(e) do { \
650 lcc_set_errno (c, (e)); \
652 if (values_names != NULL) { \
653 for (i = 0; i < values_num; i++) { \
654 free (values_names[i]); \
657 free (values_names); \
658 lcc_response_free (&res); \
662 /* If neither the values nor the names are requested, return here.. */
663 if ((ret_values == NULL) && (ret_values_names == NULL))
665 if (ret_values_num != NULL)
666 *ret_values_num = values_num;
667 lcc_response_free (&res);
671 /* Allocate space for the values */
672 if (ret_values != NULL)
674 values = (gauge_t *) malloc (values_num * sizeof (*values));
679 if (ret_values_names != NULL)
681 values_names = (char **) calloc (values_num, sizeof (*values_names));
682 if (values_names == NULL)
686 for (i = 0; i < res.lines_num; i++)
693 value = strchr (key, '=');
704 values[i] = strtod (value, &endptr);
706 if ((endptr == value) || (errno != 0))
710 if (values_names != NULL)
712 values_names[i] = lcc_strdup (key);
713 if (values_names[i] == NULL)
716 } /* for (i = 0; i < res.lines_num; i++) */
718 if (ret_values_num != NULL)
719 *ret_values_num = values_num;
720 if (ret_values != NULL)
721 *ret_values = values;
722 if (ret_values_names != NULL)
723 *ret_values_names = values_names;
726 } /* }}} int lcc_getval */
728 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
730 char ident_str[6 * LCC_NAME_LEN];
731 char ident_esc[12 * LCC_NAME_LEN];
732 char command[1024] = "";
737 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
738 || (vl->values == NULL) || (vl->values_types == NULL))
740 lcc_set_errno (c, EINVAL);
744 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
749 SSTRCATF (command, "PUTVAL %s",
750 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
752 if (vl->interval > 0)
753 SSTRCATF (command, " interval=%i", vl->interval);
756 SSTRCATF (command, "%u", (unsigned int) vl->time);
758 SSTRCAT (command, "N");
760 for (i = 0; i < vl->values_len; i++)
762 if (vl->values_types[i] == LCC_TYPE_COUNTER)
763 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
764 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
766 if (isnan (vl->values[i].gauge))
767 SSTRCPY (command, ":U");
769 SSTRCATF (command, ":%g", vl->values[i].gauge);
771 } /* for (i = 0; i < vl->values_len; i++) */
773 status = lcc_sendreceive (c, command, &res);
779 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
780 lcc_response_free (&res);
784 lcc_response_free (&res);
786 } /* }}} int lcc_putval */
788 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
789 lcc_identifier_t *ident, int timeout)
791 char command[1024] = "";
797 lcc_set_errno (c, EINVAL);
801 SSTRCPY (command, "FLUSH");
804 SSTRCATF (command, " timeout=%i", timeout);
808 char buffer[2 * LCC_NAME_LEN];
809 SSTRCATF (command, " plugin=%s",
810 lcc_strescape (buffer, plugin, sizeof (buffer)));
815 char ident_str[6 * LCC_NAME_LEN];
816 char ident_esc[12 * LCC_NAME_LEN];
818 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
822 SSTRCATF (command, " identifier=%s",
823 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
826 status = lcc_sendreceive (c, command, &res);
832 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
833 lcc_response_free (&res);
837 lcc_response_free (&res);
839 } /* }}} int lcc_flush */
841 /* TODO: Implement lcc_putnotif */
843 int lcc_listval (lcc_connection_t *c, /* {{{ */
844 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
850 lcc_identifier_t *ident;
856 if ((ret_ident == NULL) || (ret_ident_num == NULL))
858 lcc_set_errno (c, EINVAL);
862 status = lcc_sendreceive (c, "LISTVAL", &res);
868 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
869 lcc_response_free (&res);
873 ident_num = res.lines_num;
874 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
877 lcc_response_free (&res);
878 lcc_set_errno (c, ENOMEM);
882 for (i = 0; i < res.lines_num; i++)
887 /* First field is the time. */
888 time_str = res.lines[i];
890 /* Set `ident_str' to the beginning of the second field. */
891 ident_str = time_str;
892 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
894 while ((*ident_str == ' ') || (*ident_str == '\t'))
902 lcc_set_errno (c, EILSEQ);
907 status = lcc_string_to_identifier (c, ident + i, ident_str);
912 lcc_response_free (&res);
921 *ret_ident_num = ident_num;
924 } /* }}} int lcc_listval */
926 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
929 return ("Invalid object");
931 } /* }}} const char *lcc_strerror */
933 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
934 char *string, size_t string_size, const lcc_identifier_t *ident)
936 if ((string == NULL) || (string_size < 6) || (ident == NULL))
938 lcc_set_errno (c, EINVAL);
942 if (ident->plugin_instance[0] == 0)
944 if (ident->type_instance[0] == 0)
945 snprintf (string, string_size, "%s/%s/%s",
950 snprintf (string, string_size, "%s/%s/%s-%s",
954 ident->type_instance);
958 if (ident->type_instance[0] == 0)
959 snprintf (string, string_size, "%s/%s-%s/%s",
962 ident->plugin_instance,
965 snprintf (string, string_size, "%s/%s-%s/%s-%s",
968 ident->plugin_instance,
970 ident->type_instance);
973 string[string_size - 1] = 0;
975 } /* }}} int lcc_identifier_to_string */
977 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
978 lcc_identifier_t *ident, const char *string)
983 char *plugin_instance;
987 string_copy = lcc_strdup (string);
988 if (string_copy == NULL)
990 lcc_set_errno (c, ENOMEM);
995 plugin = strchr (host, '/');
998 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1005 type = strchr (plugin, '/');
1008 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1015 plugin_instance = strchr (plugin, '-');
1016 if (plugin_instance != NULL)
1018 *plugin_instance = 0;
1022 type_instance = strchr (type, '-');
1023 if (type_instance != NULL)
1029 memset (ident, 0, sizeof (*ident));
1031 SSTRCPY (ident->host, host);
1032 SSTRCPY (ident->plugin, plugin);
1033 if (plugin_instance != NULL)
1034 SSTRCPY (ident->plugin_instance, plugin_instance);
1035 SSTRCPY (ident->type, type);
1036 if (type_instance != NULL)
1037 SSTRCPY (ident->type_instance, type_instance);
1041 } /* }}} int lcc_string_to_identifier */
1043 /* vim: set sw=2 sts=2 et fdm=marker : */