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 strncat ((d), (s), sizeof (d)); \
88 (d)[sizeof (d) - 1] = 0; \
91 #define SSTRCATF(d, ...) do { \
92 char _b[sizeof (d)]; \
93 snprintf (_b, sizeof (_b), __VA_ARGS__); \
94 _b[sizeof (_b) - 1] = 0; \
99 #define LCC_SET_ERRSTR(c, ...) do { \
100 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
101 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
105 # define LCC_DEBUG(...) printf (__VA_ARGS__)
107 # define LCC_DEBUG(...) /**/
113 struct lcc_connection_s
119 struct lcc_response_s
126 typedef struct lcc_response_s lcc_response_t;
131 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
136 strerror_r (err, c->errbuf, sizeof (c->errbuf));
137 c->errbuf[sizeof (c->errbuf) - 1] = 0;
140 } /* }}} int lcc_set_errno */
142 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
144 __attribute__((malloc, nonnull (1)))
145 static char *lcc_strdup (const char *str) /* {{{ */
150 strsize = strlen (str) + 1;
151 ret = (char *) malloc (strsize);
153 memcpy (ret, str, strsize);
155 } /* }}} char *lcc_strdup */
157 __attribute__((nonnull (1, 2)))
158 static char *lcc_strescape (char *dest, const char *src, size_t dest_size) /* {{{ */
166 assert (dest_size >= 3);
168 dest[dest_pos] = '"';
173 if ((dest_pos == (dest_size - 2))
174 || (src[src_pos] == 0))
177 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
179 /* Check if there is enough space for both characters.. */
180 if (dest_pos == (dest_size - 3))
183 dest[dest_pos] = '\\';
187 dest[dest_pos] = src[src_pos];
192 assert (dest_pos <= (dest_size - 2));
194 dest[dest_pos] = '"';
202 } /* }}} char *lcc_strescape */
204 /* lcc_chomp: Removes all control-characters at the end of a string. */
205 static void lcc_chomp (char *str) /* {{{ */
209 str_len = strlen (str);
212 if (str[str_len - 1] >= 32)
214 str[str_len - 1] = 0;
217 } /* }}} void lcc_chomp */
219 static void lcc_response_free (lcc_response_t *res) /* {{{ */
226 for (i = 0; i < res->lines_num; i++)
227 free (res->lines[i]);
230 } /* }}} void lcc_response_free */
232 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
236 LCC_DEBUG ("send: --> %s\n", command);
238 status = fprintf (c->fh, "%s\r\n", command);
241 lcc_set_errno (c, errno);
246 } /* }}} int lcc_send */
248 static int lcc_receive (lcc_connection_t *c, /* {{{ */
249 lcc_response_t *ret_res)
256 memset (&res, 0, sizeof (res));
258 /* Read the first line, containing the status and a message */
259 ptr = fgets (buffer, sizeof (buffer), c->fh);
262 lcc_set_errno (c, errno);
266 LCC_DEBUG ("receive: <-- %s\n", buffer);
268 /* Convert the leading status to an integer and make `ptr' to point to the
269 * beginning of the message. */
272 res.status = strtol (buffer, &ptr, 0);
273 if ((errno != 0) || (ptr == &buffer[0]))
275 lcc_set_errno (c, errno);
279 /* Skip white spaces after the status number */
280 while ((*ptr == ' ') || (*ptr == '\t'))
283 /* Now copy the message. */
284 strncpy (res.message, ptr, sizeof (res.message));
285 res.message[sizeof (res.message) - 1] = 0;
287 /* Error or no lines follow: We're done. */
290 memcpy (ret_res, &res, sizeof (res));
294 /* Allocate space for the char-pointers */
295 res.lines_num = (size_t) res.status;
297 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
298 if (res.lines == NULL)
300 lcc_set_errno (c, ENOMEM);
304 /* Now receive all the lines */
305 for (i = 0; i < res.lines_num; i++)
307 ptr = fgets (buffer, sizeof (buffer), c->fh);
310 lcc_set_errno (c, errno);
314 LCC_DEBUG ("receive: <-- %s\n", buffer);
316 res.lines[i] = lcc_strdup (buffer);
317 if (res.lines[i] == NULL)
319 lcc_set_errno (c, ENOMEM);
324 /* Check if the for-loop exited with an error. */
325 if (i < res.lines_num)
336 memcpy (ret_res, &res, sizeof (res));
338 } /* }}} int lcc_receive */
340 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
341 const char *command, lcc_response_t *ret_res)
346 status = lcc_send (c, command);
350 memset (&res, 0, sizeof (res));
351 status = lcc_receive (c, &res);
353 memcpy (ret_res, &res, sizeof (*ret_res));
356 } /* }}} int lcc_sendreceive */
358 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
360 struct sockaddr_un sa;
365 assert (c->fh == NULL);
366 assert (path != NULL);
368 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
370 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
373 lcc_set_errno (c, errno);
377 memset (&sa, 0, sizeof (sa));
378 sa.sun_family = AF_UNIX;
379 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
381 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
384 lcc_set_errno (c, errno);
389 c->fh = fdopen (fd, "r+");
392 lcc_set_errno (c, errno);
398 } /* }}} int lcc_open_unixsocket */
400 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
401 const char *addr_orig)
403 struct addrinfo ai_hints;
404 struct addrinfo *ai_res;
405 struct addrinfo *ai_ptr;
406 char addr_copy[NI_MAXHOST];
413 assert (c->fh == NULL);
414 assert (addr_orig != NULL);
416 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
417 addr_copy[sizeof(addr_copy) - 1] = '\0';
420 memset (&ai_hints, 0, sizeof (ai_hints));
421 ai_hints.ai_flags = 0;
423 ai_hints.ai_flags |= AI_ADDRCONFIG;
425 ai_hints.ai_family = AF_UNSPEC;
426 ai_hints.ai_socktype = SOCK_STREAM;
429 if (*addr == '[') /* IPv6+port format */
431 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
434 port = strchr (addr, ']');
437 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
449 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
452 } /* if (*addr = ']') */
453 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
455 port = strrchr (addr, ':');
464 status = getaddrinfo (addr,
465 port == NULL ? LCC_DEFAULT_PORT : port,
469 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
473 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
475 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
483 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
492 c->fh = fdopen (fd, "r+");
501 assert (status == 0);
507 lcc_set_errno (c, status);
512 } /* }}} int lcc_open_netsocket */
514 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
522 assert (c->fh == NULL);
523 assert (addr != NULL);
525 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
526 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
527 else if (addr[0] == '/')
528 status = lcc_open_unixsocket (c, addr);
530 status = lcc_open_netsocket (c, addr);
533 } /* }}} int lcc_open_socket */
538 unsigned int lcc_version (void) /* {{{ */
540 return (LCC_VERSION);
541 } /* }}} unsigned int lcc_version */
543 const char *lcc_version_string (void) /* {{{ */
545 return (LCC_VERSION_STRING);
546 } /* }}} const char *lcc_version_string */
548 const char *lcc_version_extra (void) /* {{{ */
550 return (LCC_VERSION_EXTRA);
551 } /* }}} const char *lcc_version_extra */
553 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
563 c = (lcc_connection_t *) malloc (sizeof (*c));
566 memset (c, 0, sizeof (*c));
569 return (lcc_open_socket (c, address));
570 } /* }}} int lcc_connect */
572 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
585 } /* }}} int lcc_disconnect */
587 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
588 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
590 char ident_str[6 * LCC_NAME_LEN];
591 char ident_esc[12 * LCC_NAME_LEN];
592 char command[14 * LCC_NAME_LEN];
596 gauge_t *values = NULL;
597 char **values_names = NULL;
607 lcc_set_errno (c, EINVAL);
611 /* Build a commend with an escaped version of the identifier string. */
612 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
616 snprintf (command, sizeof (command), "GETVAL %s",
617 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
618 command[sizeof (command) - 1] = 0;
620 /* Send talk to the daemon.. */
621 status = lcc_sendreceive (c, command, &res);
627 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
628 lcc_response_free (&res);
632 values_num = res.lines_num;
634 #define BAIL_OUT(e) do { \
635 lcc_set_errno (c, (e)); \
637 if (values_names != NULL) { \
638 for (i = 0; i < values_num; i++) { \
639 free (values_names[i]); \
642 free (values_names); \
643 lcc_response_free (&res); \
647 /* If neither the values nor the names are requested, return here.. */
648 if ((ret_values == NULL) && (ret_values_names == NULL))
650 if (ret_values_num != NULL)
651 *ret_values_num = values_num;
652 lcc_response_free (&res);
656 /* Allocate space for the values */
657 if (ret_values != NULL)
659 values = (gauge_t *) malloc (values_num * sizeof (*values));
664 if (ret_values_names != NULL)
666 values_names = (char **) calloc (values_num, sizeof (*values_names));
667 if (values_names == NULL)
671 for (i = 0; i < res.lines_num; i++)
678 value = strchr (key, '=');
689 values[i] = strtod (value, &endptr);
691 if ((endptr == value) || (errno != 0))
695 if (values_names != NULL)
697 values_names[i] = lcc_strdup (key);
698 if (values_names[i] == NULL)
701 } /* for (i = 0; i < res.lines_num; i++) */
703 if (ret_values_num != NULL)
704 *ret_values_num = values_num;
705 if (ret_values != NULL)
706 *ret_values = values;
707 if (ret_values_names != NULL)
708 *ret_values_names = values_names;
711 } /* }}} int lcc_getval */
713 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
715 char ident_str[6 * LCC_NAME_LEN];
716 char ident_esc[12 * LCC_NAME_LEN];
717 char command[1024] = "";
722 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
723 || (vl->values == NULL) || (vl->values_types == NULL))
725 lcc_set_errno (c, EINVAL);
729 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
734 SSTRCATF (command, "PUTVAL %s",
735 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
737 if (vl->interval > 0)
738 SSTRCATF (command, " interval=%i", vl->interval);
741 SSTRCATF (command, "%u", (unsigned int) vl->time);
743 SSTRCAT (command, "N");
745 for (i = 0; i < vl->values_len; i++)
747 if (vl->values_types[i] == LCC_TYPE_COUNTER)
748 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
749 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
751 if (isnan (vl->values[i].gauge))
752 SSTRCPY (command, ":U");
754 SSTRCATF (command, ":%g", vl->values[i].gauge);
756 } /* for (i = 0; i < vl->values_len; i++) */
758 status = lcc_sendreceive (c, command, &res);
764 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
765 lcc_response_free (&res);
769 lcc_response_free (&res);
771 } /* }}} int lcc_putval */
773 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
774 lcc_identifier_t *ident, int timeout)
782 lcc_set_errno (c, EINVAL);
786 SSTRCPY (command, "FLUSH");
789 SSTRCATF (command, " timeout=%i", timeout);
793 char buffer[2 * LCC_NAME_LEN];
794 SSTRCATF (command, " plugin=%s",
795 lcc_strescape (buffer, plugin, sizeof (buffer)));
800 char ident_str[6 * LCC_NAME_LEN];
801 char ident_esc[12 * LCC_NAME_LEN];
803 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
807 SSTRCATF (command, " identifier=%s",
808 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
811 status = lcc_sendreceive (c, command, &res);
817 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
818 lcc_response_free (&res);
822 lcc_response_free (&res);
824 } /* }}} int lcc_flush */
826 /* TODO: Implement lcc_putnotif */
828 int lcc_listval (lcc_connection_t *c, /* {{{ */
829 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
835 lcc_identifier_t *ident;
841 if ((ret_ident == NULL) || (ret_ident_num == NULL))
843 lcc_set_errno (c, EINVAL);
847 status = lcc_sendreceive (c, "LISTVAL", &res);
853 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
854 lcc_response_free (&res);
858 ident_num = res.lines_num;
859 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
862 lcc_response_free (&res);
863 lcc_set_errno (c, ENOMEM);
867 for (i = 0; i < res.lines_num; i++)
872 /* First field is the time. */
873 time_str = res.lines[i];
875 /* Set `ident_str' to the beginning of the second field. */
876 ident_str = time_str;
877 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
879 while ((*ident_str == ' ') || (*ident_str == '\t'))
887 lcc_set_errno (c, EPROTO);
892 status = lcc_string_to_identifier (c, ident + i, ident_str);
897 lcc_response_free (&res);
906 *ret_ident_num = ident_num;
909 } /* }}} int lcc_listval */
911 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
914 return ("Invalid object");
916 } /* }}} const char *lcc_strerror */
918 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
919 char *string, size_t string_size, const lcc_identifier_t *ident)
921 if ((string == NULL) || (string_size < 6) || (ident == NULL))
923 lcc_set_errno (c, EINVAL);
927 if (ident->plugin_instance[0] == 0)
929 if (ident->type_instance[0] == 0)
930 snprintf (string, string_size, "%s/%s/%s",
935 snprintf (string, string_size, "%s/%s/%s-%s",
939 ident->type_instance);
943 if (ident->type_instance[0] == 0)
944 snprintf (string, string_size, "%s/%s-%s/%s",
947 ident->plugin_instance,
950 snprintf (string, string_size, "%s/%s-%s/%s-%s",
953 ident->plugin_instance,
955 ident->type_instance);
958 string[string_size - 1] = 0;
960 } /* }}} int lcc_identifier_to_string */
962 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
963 lcc_identifier_t *ident, const char *string)
968 char *plugin_instance;
972 string_copy = lcc_strdup (string);
973 if (string_copy == NULL)
975 lcc_set_errno (c, ENOMEM);
980 plugin = strchr (host, '/');
983 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
990 type = strchr (plugin, '/');
993 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1000 plugin_instance = strchr (plugin, '-');
1001 if (plugin_instance != NULL)
1003 *plugin_instance = 0;
1007 type_instance = strchr (type, '-');
1008 if (type_instance != NULL)
1014 memset (ident, 0, sizeof (*ident));
1016 SSTRCPY (ident->host, host);
1017 SSTRCPY (ident->plugin, plugin);
1018 if (plugin_instance != NULL)
1019 SSTRCPY (ident->plugin_instance, plugin_instance);
1020 SSTRCPY (ident->type, type);
1021 if (type_instance != NULL)
1022 SSTRCPY (ident->type_instance, type_instance);
1026 } /* }}} int lcc_string_to_identifier */
1028 /* vim: set sw=2 sts=2 et fdm=marker : */