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) /**/
54 #include <sys/types.h>
55 #include <sys/socket.h>
64 #define SSTRCPY(d,s) do { \
65 strncpy ((d), (s), sizeof (d)); \
66 (d)[sizeof (d) - 1] = 0; \
69 #define SSTRCAT(d,s) do { \
70 strncat ((d), (s), sizeof (d)); \
71 (d)[sizeof (d) - 1] = 0; \
74 #define SSTRCATF(d, ...) do { \
75 char _b[sizeof (d)]; \
76 snprintf (_b, sizeof (_b), __VA_ARGS__); \
77 _b[sizeof (_b) - 1] = 0; \
82 #define LCC_SET_ERRSTR(c, ...) do { \
83 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
84 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
88 # define LCC_DEBUG(...) printf (__VA_ARGS__)
90 # define LCC_DEBUG(...) /**/
96 struct lcc_connection_s
102 struct lcc_response_s
109 typedef struct lcc_response_s lcc_response_t;
114 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
119 strerror_r (err, c->errbuf, sizeof (c->errbuf));
120 c->errbuf[sizeof (c->errbuf) - 1] = 0;
123 } /* }}} int lcc_set_errno */
125 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
127 __attribute__((malloc, nonnull (1)))
128 static char *lcc_strdup (const char *str) /* {{{ */
133 strsize = strlen (str) + 1;
134 ret = (char *) malloc (strsize);
136 memcpy (ret, str, strsize);
138 } /* }}} char *lcc_strdup */
140 __attribute__((nonnull (1, 2)))
141 static char *lcc_strescape (char *dest, char *src, size_t dest_size) /* {{{ */
149 assert (dest_size >= 3);
151 dest[dest_pos] = '"';
156 if ((dest_pos == (dest_size - 2))
157 || (src[src_pos] == 0))
160 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
162 /* Check if there is enough space for both characters.. */
163 if (dest_pos == (dest_size - 3))
166 dest[dest_pos] = '\\';
170 dest[dest_pos] = src[src_pos];
175 assert (dest_pos <= (dest_size - 2));
177 dest[dest_pos] = '"';
185 } /* }}} char *lcc_strescape */
187 /* lcc_chomp: Removes all control-characters at the end of a string. */
188 static void lcc_chomp (char *str) /* {{{ */
192 str_len = strlen (str);
195 if (str[str_len - 1] >= 32)
197 str[str_len - 1] = 0;
200 } /* }}} void lcc_chomp */
202 static void lcc_response_free (lcc_response_t *res) /* {{{ */
209 for (i = 0; i < res->lines_num; i++)
210 free (res->lines[i]);
213 } /* }}} void lcc_response_free */
215 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
219 LCC_DEBUG ("send: --> %s\n", command);
221 status = fprintf (c->fh, "%s\r\n", command);
224 lcc_set_errno (c, errno);
229 } /* }}} int lcc_send */
231 static int lcc_receive (lcc_connection_t *c, /* {{{ */
232 lcc_response_t *ret_res)
239 memset (&res, 0, sizeof (res));
241 /* Read the first line, containing the status and a message */
242 ptr = fgets (buffer, sizeof (buffer), c->fh);
245 lcc_set_errno (c, errno);
249 LCC_DEBUG ("receive: <-- %s\n", buffer);
251 /* Convert the leading status to an integer and make `ptr' to point to the
252 * beginning of the message. */
255 res.status = strtol (buffer, &ptr, 0);
256 if ((errno != 0) || (ptr == &buffer[0]))
258 lcc_set_errno (c, errno);
262 /* Skip white spaces after the status number */
263 while ((*ptr == ' ') || (*ptr == '\t'))
266 /* Now copy the message. */
267 strncpy (res.message, ptr, sizeof (res.message));
268 res.message[sizeof (res.message) - 1] = 0;
270 /* Error or no lines follow: We're done. */
273 memcpy (ret_res, &res, sizeof (res));
277 /* Allocate space for the char-pointers */
278 res.lines_num = (size_t) res.status;
280 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
281 if (res.lines == NULL)
283 lcc_set_errno (c, ENOMEM);
287 /* Now receive all the lines */
288 for (i = 0; i < res.lines_num; i++)
290 ptr = fgets (buffer, sizeof (buffer), c->fh);
293 lcc_set_errno (c, errno);
297 LCC_DEBUG ("receive: <-- %s\n", buffer);
299 res.lines[i] = lcc_strdup (buffer);
300 if (res.lines[i] == NULL)
302 lcc_set_errno (c, ENOMEM);
307 /* Check if the for-loop exited with an error. */
308 if (i < res.lines_num)
319 memcpy (ret_res, &res, sizeof (res));
321 } /* }}} int lcc_receive */
323 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
324 const char *command, lcc_response_t *ret_res)
329 status = lcc_send (c, command);
333 memset (&res, 0, sizeof (res));
334 status = lcc_receive (c, &res);
336 memcpy (ret_res, &res, sizeof (*ret_res));
339 } /* }}} int lcc_sendreceive */
341 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
343 struct sockaddr_un sa;
348 assert (c->fh == NULL);
349 assert (path != NULL);
351 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
354 lcc_set_errno (c, errno);
358 memset (&sa, 0, sizeof (sa));
359 sa.sun_family = AF_UNIX;
360 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
362 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
365 lcc_set_errno (c, errno);
370 c->fh = fdopen (fd, "r+");
373 lcc_set_errno (c, errno);
379 } /* }}} int lcc_open_unixsocket */
381 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
382 const char *addr_orig)
384 struct addrinfo ai_hints;
385 struct addrinfo *ai_res;
386 struct addrinfo *ai_ptr;
387 char addr_copy[NI_MAXHOST];
394 assert (c->fh == NULL);
395 assert (addr_orig != NULL);
397 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
398 addr_copy[sizeof(addr_copy) - 1] = '\0';
401 memset (&ai_hints, 0, sizeof (ai_hints));
402 ai_hints.ai_flags = 0;
404 ai_hints.ai_flags |= AI_ADDRCONFIG;
406 ai_hints.ai_family = AF_UNSPEC;
407 ai_hints.ai_socktype = SOCK_STREAM;
410 if (*addr == '[') /* IPv6+port format */
412 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
415 port = strchr (addr, ']');
418 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
430 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
433 } /* if (*addr = ']') */
434 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
436 port = strrchr (addr, ':');
445 status = getaddrinfo (addr,
446 port == NULL ? LCC_DEFAULT_PORT : port,
450 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
454 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
456 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
464 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
473 c->fh = fdopen (fd, "r+");
482 assert (status == 0);
488 lcc_set_errno (c, status);
493 } /* }}} int lcc_open_netsocket */
495 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
503 assert (c->fh == NULL);
504 assert (addr != NULL);
506 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
507 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
508 else if (addr[0] == '/')
509 status = lcc_open_unixsocket (c, addr);
511 status = lcc_open_netsocket (c, addr);
514 } /* }}} int lcc_open_socket */
519 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
529 c = (lcc_connection_t *) malloc (sizeof (*c));
532 memset (c, 0, sizeof (*c));
535 return (lcc_open_socket (c, address));
536 } /* }}} int lcc_connect */
538 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
551 } /* }}} int lcc_disconnect */
553 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
554 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
556 char ident_str[6 * LCC_NAME_LEN];
557 char ident_esc[12 * LCC_NAME_LEN];
558 char command[14 * LCC_NAME_LEN];
562 gauge_t *values = NULL;
563 char **values_names = NULL;
573 lcc_set_errno (c, EINVAL);
577 /* Build a commend with an escaped version of the identifier string. */
578 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
582 snprintf (command, sizeof (command), "GETVAL %s",
583 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
584 command[sizeof (command) - 1] = 0;
586 /* Send talk to the daemon.. */
587 status = lcc_sendreceive (c, command, &res);
593 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
594 lcc_response_free (&res);
598 values_num = res.lines_num;
600 #define BAIL_OUT(e) do { \
601 lcc_set_errno (c, (e)); \
603 if (values_names != NULL) { \
604 for (i = 0; i < values_num; i++) { \
605 free (values_names[i]); \
608 free (values_names); \
609 lcc_response_free (&res); \
613 /* If neither the values nor the names are requested, return here.. */
614 if ((ret_values == NULL) && (ret_values_names == NULL))
616 if (ret_values_num != NULL)
617 *ret_values_num = values_num;
618 lcc_response_free (&res);
622 /* Allocate space for the values */
623 if (ret_values != NULL)
625 values = (gauge_t *) malloc (values_num * sizeof (*values));
630 if (ret_values_names != NULL)
632 values_names = (char **) calloc (values_num, sizeof (*values_names));
633 if (values_names == NULL)
637 for (i = 0; i < res.lines_num; i++)
644 value = strchr (key, '=');
655 values[i] = strtod (value, &endptr);
657 if ((endptr == value) || (errno != 0))
661 if (values_names != NULL)
663 values_names[i] = lcc_strdup (key);
664 if (values_names[i] == NULL)
667 } /* for (i = 0; i < res.lines_num; i++) */
669 if (ret_values_num != NULL)
670 *ret_values_num = values_num;
671 if (ret_values != NULL)
672 *ret_values = values;
673 if (ret_values_names != NULL)
674 *ret_values_names = values_names;
677 } /* }}} int lcc_getval */
679 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
681 char ident_str[6 * LCC_NAME_LEN];
682 char ident_esc[12 * LCC_NAME_LEN];
688 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
689 || (vl->values == NULL) || (vl->values_types == NULL))
691 lcc_set_errno (c, EINVAL);
695 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
700 snprintf (command, sizeof (command), "PUTVAL %s",
701 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
702 command[sizeof (command) - 1] = 0;
704 if (vl->interval > 0)
708 snprintf (option, sizeof (option), " interval=%i", vl->interval);
709 option[sizeof (option) - 1] = 0;
711 SSTRCAT (command, option);
715 SSTRCATF (command, "%u", (unsigned int) vl->time);
717 SSTRCAT (command, "N");
719 for (i = 0; i < vl->values_len; i++)
721 if (vl->values_types[i] == LCC_TYPE_COUNTER)
722 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
723 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
725 if (isnan (vl->values[i].gauge))
726 SSTRCPY (command, ":U");
728 SSTRCATF (command, ":%g", vl->values[i].gauge);
730 } /* for (i = 0; i < vl->values_len; i++) */
732 status = lcc_sendreceive (c, command, &res);
738 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
739 lcc_response_free (&res);
743 lcc_response_free (&res);
745 } /* }}} int lcc_putval */
747 /* TODO: Implement lcc_flush */
748 int lcc_flush (lcc_connection_t *c, lcc_identifier_t *ident, int timeout);
750 /* TODO: Implement lcc_putnotif */
752 int lcc_listval (lcc_connection_t *c, /* {{{ */
753 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
759 lcc_identifier_t *ident;
765 if ((ret_ident == NULL) || (ret_ident_num == NULL))
767 lcc_set_errno (c, EINVAL);
771 status = lcc_sendreceive (c, "LISTVAL", &res);
777 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
778 lcc_response_free (&res);
782 ident_num = res.lines_num;
783 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
786 lcc_response_free (&res);
787 lcc_set_errno (c, ENOMEM);
791 for (i = 0; i < res.lines_num; i++)
796 /* First field is the time. */
797 time_str = res.lines[i];
799 /* Set `ident_str' to the beginning of the second field. */
800 ident_str = time_str;
801 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
803 while ((*ident_str == ' ') || (*ident_str == '\t'))
811 lcc_set_errno (c, EPROTO);
816 status = lcc_string_to_identifier (c, ident + i, ident_str);
821 lcc_response_free (&res);
830 *ret_ident_num = ident_num;
833 } /* }}} int lcc_listval */
835 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
838 return ("Invalid object");
840 } /* }}} const char *lcc_strerror */
842 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
843 char *string, size_t string_size, const lcc_identifier_t *ident)
845 if ((string == NULL) || (string_size < 6) || (ident == NULL))
847 lcc_set_errno (c, EINVAL);
851 if (ident->plugin_instance[0] == 0)
853 if (ident->type_instance[0] == 0)
854 snprintf (string, string_size, "%s/%s/%s",
859 snprintf (string, string_size, "%s/%s/%s-%s",
863 ident->type_instance);
867 if (ident->type_instance[0] == 0)
868 snprintf (string, string_size, "%s/%s-%s/%s",
871 ident->plugin_instance,
874 snprintf (string, string_size, "%s/%s-%s/%s-%s",
877 ident->plugin_instance,
879 ident->type_instance);
882 string[string_size - 1] = 0;
884 } /* }}} int lcc_identifier_to_string */
886 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
887 lcc_identifier_t *ident, const char *string)
892 char *plugin_instance;
896 string_copy = lcc_strdup (string);
897 if (string_copy == NULL)
899 lcc_set_errno (c, ENOMEM);
904 plugin = strchr (host, '/');
907 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
914 type = strchr (plugin, '/');
917 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
924 plugin_instance = strchr (plugin, '-');
925 if (plugin_instance != NULL)
927 *plugin_instance = 0;
931 type_instance = strchr (type, '-');
932 if (type_instance != NULL)
938 memset (ident, 0, sizeof (*ident));
940 SSTRCPY (ident->host, host);
941 SSTRCPY (ident->plugin, plugin);
942 if (plugin_instance != NULL)
943 SSTRCPY (ident->plugin_instance, plugin_instance);
944 SSTRCPY (ident->type, type);
945 if (type_instance != NULL)
946 SSTRCPY (ident->type_instance, type_instance);
950 } /* }}} int lcc_string_to_identifier */
952 /* vim: set sw=2 sts=2 et fdm=marker : */