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>
26 #if !defined(__GNUC__) || !__GNUC__
27 # define __attribute__(x) /**/
30 #include "lcc_features.h"
35 #include <sys/types.h>
36 #include <sys/socket.h>
46 /* NI_MAXHOST has been obsoleted by RFC 3493 which is a reason for SunOS 5.11
47 * to no longer define it. We'll use the old, RFC 2553 value here. */
49 # define NI_MAXHOST 1025
52 /* OpenBSD doesn't have EPROTO, FreeBSD doesn't have EILSEQ. Oh what joy! */
55 # define EILSEQ EPROTO
57 # define EILSEQ EINVAL
61 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
62 * termination. They work for static buffers only, because they use `sizeof'.
63 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
64 * is very useful to add formatted stuff to the end of a buffer. */
65 #define SSTRCPY(d,s) do { \
66 strncpy ((d), (s), sizeof (d)); \
67 (d)[sizeof (d) - 1] = 0; \
70 #define SSTRCAT(d,s) do { \
71 size_t _l = strlen (d); \
72 strncpy ((d) + _l, (s), sizeof (d) - _l); \
73 (d)[sizeof (d) - 1] = 0; \
76 #define SSTRCATF(d, ...) do { \
77 char _b[sizeof (d)]; \
78 snprintf (_b, sizeof (_b), __VA_ARGS__); \
79 _b[sizeof (_b) - 1] = 0; \
84 #define LCC_SET_ERRSTR(c, ...) do { \
85 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
86 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
90 # define LCC_DEBUG(...) printf (__VA_ARGS__)
92 # define LCC_DEBUG(...) /**/
98 struct lcc_connection_s
104 struct lcc_response_s
111 typedef struct lcc_response_s lcc_response_t;
116 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
121 strerror_r (err, c->errbuf, sizeof (c->errbuf));
122 c->errbuf[sizeof (c->errbuf) - 1] = 0;
125 } /* }}} int lcc_set_errno */
127 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
129 __attribute__((malloc, nonnull (1)))
130 static char *lcc_strdup (const char *str) /* {{{ */
135 strsize = strlen (str) + 1;
136 ret = (char *) malloc (strsize);
138 memcpy (ret, str, strsize);
140 } /* }}} char *lcc_strdup */
142 __attribute__((nonnull (1, 2)))
143 static char *lcc_strescape (char *dest, const char *src, size_t dest_size) /* {{{ */
151 assert (dest_size >= 3);
153 dest[dest_pos] = '"';
158 if ((dest_pos == (dest_size - 2))
159 || (src[src_pos] == 0))
162 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
164 /* Check if there is enough space for both characters.. */
165 if (dest_pos == (dest_size - 3))
168 dest[dest_pos] = '\\';
172 dest[dest_pos] = src[src_pos];
177 assert (dest_pos <= (dest_size - 2));
179 dest[dest_pos] = '"';
187 } /* }}} char *lcc_strescape */
189 /* lcc_chomp: Removes all control-characters at the end of a string. */
190 static void lcc_chomp (char *str) /* {{{ */
194 str_len = strlen (str);
197 if (str[str_len - 1] >= 32)
199 str[str_len - 1] = 0;
202 } /* }}} void lcc_chomp */
204 static void lcc_response_free (lcc_response_t *res) /* {{{ */
211 for (i = 0; i < res->lines_num; i++)
212 free (res->lines[i]);
215 } /* }}} void lcc_response_free */
217 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
221 LCC_DEBUG ("send: --> %s\n", command);
223 status = fprintf (c->fh, "%s\r\n", command);
226 lcc_set_errno (c, errno);
231 } /* }}} int lcc_send */
233 static int lcc_receive (lcc_connection_t *c, /* {{{ */
234 lcc_response_t *ret_res)
241 memset (&res, 0, sizeof (res));
243 /* Read the first line, containing the status and a message */
244 ptr = fgets (buffer, sizeof (buffer), c->fh);
247 lcc_set_errno (c, errno);
251 LCC_DEBUG ("receive: <-- %s\n", buffer);
253 /* Convert the leading status to an integer and make `ptr' to point to the
254 * beginning of the message. */
257 res.status = strtol (buffer, &ptr, 0);
258 if ((errno != 0) || (ptr == &buffer[0]))
260 lcc_set_errno (c, errno);
264 /* Skip white spaces after the status number */
265 while ((*ptr == ' ') || (*ptr == '\t'))
268 /* Now copy the message. */
269 strncpy (res.message, ptr, sizeof (res.message));
270 res.message[sizeof (res.message) - 1] = 0;
272 /* Error or no lines follow: We're done. */
275 memcpy (ret_res, &res, sizeof (res));
279 /* Allocate space for the char-pointers */
280 res.lines_num = (size_t) res.status;
282 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
283 if (res.lines == NULL)
285 lcc_set_errno (c, ENOMEM);
289 /* Now receive all the lines */
290 for (i = 0; i < res.lines_num; i++)
292 ptr = fgets (buffer, sizeof (buffer), c->fh);
295 lcc_set_errno (c, errno);
299 LCC_DEBUG ("receive: <-- %s\n", buffer);
301 res.lines[i] = lcc_strdup (buffer);
302 if (res.lines[i] == NULL)
304 lcc_set_errno (c, ENOMEM);
309 /* Check if the for-loop exited with an error. */
310 if (i < res.lines_num)
321 memcpy (ret_res, &res, sizeof (res));
323 } /* }}} int lcc_receive */
325 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
326 const char *command, lcc_response_t *ret_res)
333 lcc_set_errno (c, EBADF);
337 status = lcc_send (c, command);
341 memset (&res, 0, sizeof (res));
342 status = lcc_receive (c, &res);
344 memcpy (ret_res, &res, sizeof (*ret_res));
347 } /* }}} int lcc_sendreceive */
349 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
351 struct sockaddr_un sa;
356 assert (c->fh == NULL);
357 assert (path != NULL);
359 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
361 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
364 lcc_set_errno (c, errno);
368 memset (&sa, 0, sizeof (sa));
369 sa.sun_family = AF_UNIX;
370 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
372 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
375 lcc_set_errno (c, errno);
380 c->fh = fdopen (fd, "r+");
383 lcc_set_errno (c, errno);
389 } /* }}} int lcc_open_unixsocket */
391 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
392 const char *addr_orig)
394 struct addrinfo ai_hints;
395 struct addrinfo *ai_res;
396 struct addrinfo *ai_ptr;
397 char addr_copy[NI_MAXHOST];
404 assert (c->fh == NULL);
405 assert (addr_orig != NULL);
407 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
408 addr_copy[sizeof(addr_copy) - 1] = '\0';
411 memset (&ai_hints, 0, sizeof (ai_hints));
412 ai_hints.ai_flags = 0;
414 ai_hints.ai_flags |= AI_ADDRCONFIG;
416 ai_hints.ai_family = AF_UNSPEC;
417 ai_hints.ai_socktype = SOCK_STREAM;
420 if (*addr == '[') /* IPv6+port format */
422 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
425 port = strchr (addr, ']');
428 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
440 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
443 } /* if (*addr = ']') */
444 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
446 port = strrchr (addr, ':');
455 status = getaddrinfo (addr,
456 port == NULL ? LCC_DEFAULT_PORT : port,
460 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
464 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
466 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
474 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
483 c->fh = fdopen (fd, "r+");
492 assert (status == 0);
498 lcc_set_errno (c, status);
503 } /* }}} int lcc_open_netsocket */
505 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
513 assert (c->fh == NULL);
514 assert (addr != NULL);
516 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
517 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
518 else if (addr[0] == '/')
519 status = lcc_open_unixsocket (c, addr);
521 status = lcc_open_netsocket (c, addr);
524 } /* }}} int lcc_open_socket */
529 unsigned int lcc_version (void) /* {{{ */
531 return (LCC_VERSION);
532 } /* }}} unsigned int lcc_version */
534 const char *lcc_version_string (void) /* {{{ */
536 return (LCC_VERSION_STRING);
537 } /* }}} const char *lcc_version_string */
539 const char *lcc_version_extra (void) /* {{{ */
541 return (LCC_VERSION_EXTRA);
542 } /* }}} const char *lcc_version_extra */
544 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
555 c = (lcc_connection_t *) malloc (sizeof (*c));
558 memset (c, 0, sizeof (*c));
560 status = lcc_open_socket (c, address);
569 } /* }}} int lcc_connect */
571 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
584 } /* }}} int lcc_disconnect */
586 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
587 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
589 char ident_str[6 * LCC_NAME_LEN];
590 char ident_esc[12 * LCC_NAME_LEN];
591 char command[14 * LCC_NAME_LEN];
595 gauge_t *values = NULL;
596 char **values_names = NULL;
606 lcc_set_errno (c, EINVAL);
610 /* Build a commend with an escaped version of the identifier string. */
611 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
615 snprintf (command, sizeof (command), "GETVAL %s",
616 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
617 command[sizeof (command) - 1] = 0;
619 /* Send talk to the daemon.. */
620 status = lcc_sendreceive (c, command, &res);
626 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
627 lcc_response_free (&res);
631 values_num = res.lines_num;
633 #define BAIL_OUT(e) do { \
634 lcc_set_errno (c, (e)); \
636 if (values_names != NULL) { \
637 for (i = 0; i < values_num; i++) { \
638 free (values_names[i]); \
641 free (values_names); \
642 lcc_response_free (&res); \
646 /* If neither the values nor the names are requested, return here.. */
647 if ((ret_values == NULL) && (ret_values_names == NULL))
649 if (ret_values_num != NULL)
650 *ret_values_num = values_num;
651 lcc_response_free (&res);
655 /* Allocate space for the values */
656 if (ret_values != NULL)
658 values = (gauge_t *) malloc (values_num * sizeof (*values));
663 if (ret_values_names != NULL)
665 values_names = (char **) calloc (values_num, sizeof (*values_names));
666 if (values_names == NULL)
670 for (i = 0; i < res.lines_num; i++)
677 value = strchr (key, '=');
688 values[i] = strtod (value, &endptr);
690 if ((endptr == value) || (errno != 0))
694 if (values_names != NULL)
696 values_names[i] = lcc_strdup (key);
697 if (values_names[i] == NULL)
700 } /* for (i = 0; i < res.lines_num; i++) */
702 if (ret_values_num != NULL)
703 *ret_values_num = values_num;
704 if (ret_values != NULL)
705 *ret_values = values;
706 if (ret_values_names != NULL)
707 *ret_values_names = values_names;
710 } /* }}} int lcc_getval */
712 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
714 char ident_str[6 * LCC_NAME_LEN];
715 char ident_esc[12 * LCC_NAME_LEN];
716 char command[1024] = "";
721 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
722 || (vl->values == NULL) || (vl->values_types == NULL))
724 lcc_set_errno (c, EINVAL);
728 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
733 SSTRCATF (command, "PUTVAL %s",
734 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
736 if (vl->interval > 0)
737 SSTRCATF (command, " interval=%i", vl->interval);
740 SSTRCATF (command, "%u", (unsigned int) vl->time);
742 SSTRCAT (command, "N");
744 for (i = 0; i < vl->values_len; i++)
746 if (vl->values_types[i] == LCC_TYPE_COUNTER)
747 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
748 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
750 if (isnan (vl->values[i].gauge))
751 SSTRCPY (command, ":U");
753 SSTRCATF (command, ":%g", vl->values[i].gauge);
755 } /* for (i = 0; i < vl->values_len; i++) */
757 status = lcc_sendreceive (c, command, &res);
763 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
764 lcc_response_free (&res);
768 lcc_response_free (&res);
770 } /* }}} int lcc_putval */
772 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
773 lcc_identifier_t *ident, int timeout)
775 char command[1024] = "";
781 lcc_set_errno (c, EINVAL);
785 SSTRCPY (command, "FLUSH");
788 SSTRCATF (command, " timeout=%i", timeout);
792 char buffer[2 * LCC_NAME_LEN];
793 SSTRCATF (command, " plugin=%s",
794 lcc_strescape (buffer, plugin, sizeof (buffer)));
799 char ident_str[6 * LCC_NAME_LEN];
800 char ident_esc[12 * LCC_NAME_LEN];
802 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
806 SSTRCATF (command, " identifier=%s",
807 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
810 status = lcc_sendreceive (c, command, &res);
816 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
817 lcc_response_free (&res);
821 lcc_response_free (&res);
823 } /* }}} int lcc_flush */
825 /* TODO: Implement lcc_putnotif */
827 int lcc_listval (lcc_connection_t *c, /* {{{ */
828 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
834 lcc_identifier_t *ident;
840 if ((ret_ident == NULL) || (ret_ident_num == NULL))
842 lcc_set_errno (c, EINVAL);
846 status = lcc_sendreceive (c, "LISTVAL", &res);
852 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
853 lcc_response_free (&res);
857 ident_num = res.lines_num;
858 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
861 lcc_response_free (&res);
862 lcc_set_errno (c, ENOMEM);
866 for (i = 0; i < res.lines_num; i++)
871 /* First field is the time. */
872 time_str = res.lines[i];
874 /* Set `ident_str' to the beginning of the second field. */
875 ident_str = time_str;
876 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
878 while ((*ident_str == ' ') || (*ident_str == '\t'))
886 lcc_set_errno (c, EILSEQ);
891 status = lcc_string_to_identifier (c, ident + i, ident_str);
896 lcc_response_free (&res);
905 *ret_ident_num = ident_num;
908 } /* }}} int lcc_listval */
910 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
913 return ("Invalid object");
915 } /* }}} const char *lcc_strerror */
917 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
918 char *string, size_t string_size, const lcc_identifier_t *ident)
920 if ((string == NULL) || (string_size < 6) || (ident == NULL))
922 lcc_set_errno (c, EINVAL);
926 if (ident->plugin_instance[0] == 0)
928 if (ident->type_instance[0] == 0)
929 snprintf (string, string_size, "%s/%s/%s",
934 snprintf (string, string_size, "%s/%s/%s-%s",
938 ident->type_instance);
942 if (ident->type_instance[0] == 0)
943 snprintf (string, string_size, "%s/%s-%s/%s",
946 ident->plugin_instance,
949 snprintf (string, string_size, "%s/%s-%s/%s-%s",
952 ident->plugin_instance,
954 ident->type_instance);
957 string[string_size - 1] = 0;
959 } /* }}} int lcc_identifier_to_string */
961 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
962 lcc_identifier_t *ident, const char *string)
967 char *plugin_instance;
971 string_copy = lcc_strdup (string);
972 if (string_copy == NULL)
974 lcc_set_errno (c, ENOMEM);
979 plugin = strchr (host, '/');
982 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
989 type = strchr (plugin, '/');
992 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
999 plugin_instance = strchr (plugin, '-');
1000 if (plugin_instance != NULL)
1002 *plugin_instance = 0;
1006 type_instance = strchr (type, '-');
1007 if (type_instance != NULL)
1013 memset (ident, 0, sizeof (*ident));
1015 SSTRCPY (ident->host, host);
1016 SSTRCPY (ident->plugin, plugin);
1017 if (plugin_instance != NULL)
1018 SSTRCPY (ident->plugin_instance, plugin_instance);
1019 SSTRCPY (ident->type, type);
1020 if (type_instance != NULL)
1021 SSTRCPY (ident->type_instance, type_instance);
1025 } /* }}} int lcc_string_to_identifier */
1027 /* vim: set sw=2 sts=2 et fdm=marker : */