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 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
65 * termination. They work for static buffers only, because they use `sizeof'.
66 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
67 * is very useful to add formatted stuff to the end of a buffer. */
68 #define SSTRCPY(d,s) do { \
69 strncpy ((d), (s), sizeof (d)); \
70 (d)[sizeof (d) - 1] = 0; \
73 #define SSTRCAT(d,s) do { \
74 strncat ((d), (s), sizeof (d)); \
75 (d)[sizeof (d) - 1] = 0; \
78 #define SSTRCATF(d, ...) do { \
79 char _b[sizeof (d)]; \
80 snprintf (_b, sizeof (_b), __VA_ARGS__); \
81 _b[sizeof (_b) - 1] = 0; \
86 #define LCC_SET_ERRSTR(c, ...) do { \
87 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
88 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
92 # define LCC_DEBUG(...) printf (__VA_ARGS__)
94 # define LCC_DEBUG(...) /**/
100 struct lcc_connection_s
106 struct lcc_response_s
113 typedef struct lcc_response_s lcc_response_t;
118 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
123 strerror_r (err, c->errbuf, sizeof (c->errbuf));
124 c->errbuf[sizeof (c->errbuf) - 1] = 0;
127 } /* }}} int lcc_set_errno */
129 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
131 __attribute__((malloc, nonnull (1)))
132 static char *lcc_strdup (const char *str) /* {{{ */
137 strsize = strlen (str) + 1;
138 ret = (char *) malloc (strsize);
140 memcpy (ret, str, strsize);
142 } /* }}} char *lcc_strdup */
144 __attribute__((nonnull (1, 2)))
145 static char *lcc_strescape (char *dest, char *src, size_t dest_size) /* {{{ */
153 assert (dest_size >= 3);
155 dest[dest_pos] = '"';
160 if ((dest_pos == (dest_size - 2))
161 || (src[src_pos] == 0))
164 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
166 /* Check if there is enough space for both characters.. */
167 if (dest_pos == (dest_size - 3))
170 dest[dest_pos] = '\\';
174 dest[dest_pos] = src[src_pos];
179 assert (dest_pos <= (dest_size - 2));
181 dest[dest_pos] = '"';
189 } /* }}} char *lcc_strescape */
191 /* lcc_chomp: Removes all control-characters at the end of a string. */
192 static void lcc_chomp (char *str) /* {{{ */
196 str_len = strlen (str);
199 if (str[str_len - 1] >= 32)
201 str[str_len - 1] = 0;
204 } /* }}} void lcc_chomp */
206 static void lcc_response_free (lcc_response_t *res) /* {{{ */
213 for (i = 0; i < res->lines_num; i++)
214 free (res->lines[i]);
217 } /* }}} void lcc_response_free */
219 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
223 LCC_DEBUG ("send: --> %s\n", command);
225 status = fprintf (c->fh, "%s\r\n", command);
228 lcc_set_errno (c, errno);
233 } /* }}} int lcc_send */
235 static int lcc_receive (lcc_connection_t *c, /* {{{ */
236 lcc_response_t *ret_res)
243 memset (&res, 0, sizeof (res));
245 /* Read the first line, containing the status and a message */
246 ptr = fgets (buffer, sizeof (buffer), c->fh);
249 lcc_set_errno (c, errno);
253 LCC_DEBUG ("receive: <-- %s\n", buffer);
255 /* Convert the leading status to an integer and make `ptr' to point to the
256 * beginning of the message. */
259 res.status = strtol (buffer, &ptr, 0);
260 if ((errno != 0) || (ptr == &buffer[0]))
262 lcc_set_errno (c, errno);
266 /* Skip white spaces after the status number */
267 while ((*ptr == ' ') || (*ptr == '\t'))
270 /* Now copy the message. */
271 strncpy (res.message, ptr, sizeof (res.message));
272 res.message[sizeof (res.message) - 1] = 0;
274 /* Error or no lines follow: We're done. */
277 memcpy (ret_res, &res, sizeof (res));
281 /* Allocate space for the char-pointers */
282 res.lines_num = (size_t) res.status;
284 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
285 if (res.lines == NULL)
287 lcc_set_errno (c, ENOMEM);
291 /* Now receive all the lines */
292 for (i = 0; i < res.lines_num; i++)
294 ptr = fgets (buffer, sizeof (buffer), c->fh);
297 lcc_set_errno (c, errno);
301 LCC_DEBUG ("receive: <-- %s\n", buffer);
303 res.lines[i] = lcc_strdup (buffer);
304 if (res.lines[i] == NULL)
306 lcc_set_errno (c, ENOMEM);
311 /* Check if the for-loop exited with an error. */
312 if (i < res.lines_num)
323 memcpy (ret_res, &res, sizeof (res));
325 } /* }}} int lcc_receive */
327 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
328 const char *command, lcc_response_t *ret_res)
333 status = lcc_send (c, command);
337 memset (&res, 0, sizeof (res));
338 status = lcc_receive (c, &res);
340 memcpy (ret_res, &res, sizeof (*ret_res));
343 } /* }}} int lcc_sendreceive */
345 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
347 struct sockaddr_un sa;
352 assert (c->fh == NULL);
353 assert (path != NULL);
355 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
358 lcc_set_errno (c, errno);
362 memset (&sa, 0, sizeof (sa));
363 sa.sun_family = AF_UNIX;
364 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
366 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
369 lcc_set_errno (c, errno);
374 c->fh = fdopen (fd, "r+");
377 lcc_set_errno (c, errno);
383 } /* }}} int lcc_open_unixsocket */
385 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
386 const char *addr_orig)
388 struct addrinfo ai_hints;
389 struct addrinfo *ai_res;
390 struct addrinfo *ai_ptr;
391 char addr_copy[NI_MAXHOST];
398 assert (c->fh == NULL);
399 assert (addr_orig != NULL);
401 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
402 addr_copy[sizeof(addr_copy) - 1] = '\0';
405 memset (&ai_hints, 0, sizeof (ai_hints));
406 ai_hints.ai_flags = 0;
408 ai_hints.ai_flags |= AI_ADDRCONFIG;
410 ai_hints.ai_family = AF_UNSPEC;
411 ai_hints.ai_socktype = SOCK_STREAM;
414 if (*addr == '[') /* IPv6+port format */
416 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
419 port = strchr (addr, ']');
422 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
434 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
437 } /* if (*addr = ']') */
438 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
440 port = strrchr (addr, ':');
449 status = getaddrinfo (addr,
450 port == NULL ? LCC_DEFAULT_PORT : port,
454 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
458 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
460 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
468 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
477 c->fh = fdopen (fd, "r+");
486 assert (status == 0);
492 lcc_set_errno (c, status);
497 } /* }}} int lcc_open_netsocket */
499 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
507 assert (c->fh == NULL);
508 assert (addr != NULL);
510 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
511 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
512 else if (addr[0] == '/')
513 status = lcc_open_unixsocket (c, addr);
515 status = lcc_open_netsocket (c, addr);
518 } /* }}} int lcc_open_socket */
523 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
533 c = (lcc_connection_t *) malloc (sizeof (*c));
536 memset (c, 0, sizeof (*c));
539 return (lcc_open_socket (c, address));
540 } /* }}} int lcc_connect */
542 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
555 } /* }}} int lcc_disconnect */
557 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
558 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
560 char ident_str[6 * LCC_NAME_LEN];
561 char ident_esc[12 * LCC_NAME_LEN];
562 char command[14 * LCC_NAME_LEN];
566 gauge_t *values = NULL;
567 char **values_names = NULL;
577 lcc_set_errno (c, EINVAL);
581 /* Build a commend with an escaped version of the identifier string. */
582 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
586 snprintf (command, sizeof (command), "GETVAL %s",
587 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
588 command[sizeof (command) - 1] = 0;
590 /* Send talk to the daemon.. */
591 status = lcc_sendreceive (c, command, &res);
597 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
598 lcc_response_free (&res);
602 values_num = res.lines_num;
604 #define BAIL_OUT(e) do { \
605 lcc_set_errno (c, (e)); \
607 if (values_names != NULL) { \
608 for (i = 0; i < values_num; i++) { \
609 free (values_names[i]); \
612 free (values_names); \
613 lcc_response_free (&res); \
617 /* If neither the values nor the names are requested, return here.. */
618 if ((ret_values == NULL) && (ret_values_names == NULL))
620 if (ret_values_num != NULL)
621 *ret_values_num = values_num;
622 lcc_response_free (&res);
626 /* Allocate space for the values */
627 if (ret_values != NULL)
629 values = (gauge_t *) malloc (values_num * sizeof (*values));
634 if (ret_values_names != NULL)
636 values_names = (char **) calloc (values_num, sizeof (*values_names));
637 if (values_names == NULL)
641 for (i = 0; i < res.lines_num; i++)
648 value = strchr (key, '=');
659 values[i] = strtod (value, &endptr);
661 if ((endptr == value) || (errno != 0))
665 if (values_names != NULL)
667 values_names[i] = lcc_strdup (key);
668 if (values_names[i] == NULL)
671 } /* for (i = 0; i < res.lines_num; i++) */
673 if (ret_values_num != NULL)
674 *ret_values_num = values_num;
675 if (ret_values != NULL)
676 *ret_values = values;
677 if (ret_values_names != NULL)
678 *ret_values_names = values_names;
681 } /* }}} int lcc_getval */
683 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
685 char ident_str[6 * LCC_NAME_LEN];
686 char ident_esc[12 * LCC_NAME_LEN];
692 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
693 || (vl->values == NULL) || (vl->values_types == NULL))
695 lcc_set_errno (c, EINVAL);
699 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
704 snprintf (command, sizeof (command), "PUTVAL %s",
705 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
706 command[sizeof (command) - 1] = 0;
708 if (vl->interval > 0)
712 snprintf (option, sizeof (option), " interval=%i", vl->interval);
713 option[sizeof (option) - 1] = 0;
715 SSTRCAT (command, option);
719 SSTRCATF (command, "%u", (unsigned int) vl->time);
721 SSTRCAT (command, "N");
723 for (i = 0; i < vl->values_len; i++)
725 if (vl->values_types[i] == LCC_TYPE_COUNTER)
726 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
727 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
729 if (isnan (vl->values[i].gauge))
730 SSTRCPY (command, ":U");
732 SSTRCATF (command, ":%g", vl->values[i].gauge);
734 } /* for (i = 0; i < vl->values_len; i++) */
736 status = lcc_sendreceive (c, command, &res);
742 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
743 lcc_response_free (&res);
747 lcc_response_free (&res);
749 } /* }}} int lcc_putval */
751 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
752 lcc_identifier_t *ident, int timeout)
760 lcc_set_errno (c, EINVAL);
764 SSTRCPY (command, "FLUSH");
767 SSTRCATF (command, " timeout=%i", timeout);
771 char buffer[2 * LCC_NAME_LEN];
772 SSTRCATF (command, " plugin=%s",
773 lcc_strescape (buffer, plugin, sizeof (buffer)));
778 char ident_str[6 * LCC_NAME_LEN];
779 char ident_esc[12 * LCC_NAME_LEN];
781 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
785 SSTRCATF (command, " identifier=%s",
786 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
789 status = lcc_sendreceive (c, command, &res);
795 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
796 lcc_response_free (&res);
800 lcc_response_free (&res);
802 } /* }}} int lcc_flush */
804 /* TODO: Implement lcc_putnotif */
806 int lcc_listval (lcc_connection_t *c, /* {{{ */
807 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
813 lcc_identifier_t *ident;
819 if ((ret_ident == NULL) || (ret_ident_num == NULL))
821 lcc_set_errno (c, EINVAL);
825 status = lcc_sendreceive (c, "LISTVAL", &res);
831 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
832 lcc_response_free (&res);
836 ident_num = res.lines_num;
837 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
840 lcc_response_free (&res);
841 lcc_set_errno (c, ENOMEM);
845 for (i = 0; i < res.lines_num; i++)
850 /* First field is the time. */
851 time_str = res.lines[i];
853 /* Set `ident_str' to the beginning of the second field. */
854 ident_str = time_str;
855 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
857 while ((*ident_str == ' ') || (*ident_str == '\t'))
865 lcc_set_errno (c, EPROTO);
870 status = lcc_string_to_identifier (c, ident + i, ident_str);
875 lcc_response_free (&res);
884 *ret_ident_num = ident_num;
887 } /* }}} int lcc_listval */
889 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
892 return ("Invalid object");
894 } /* }}} const char *lcc_strerror */
896 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
897 char *string, size_t string_size, const lcc_identifier_t *ident)
899 if ((string == NULL) || (string_size < 6) || (ident == NULL))
901 lcc_set_errno (c, EINVAL);
905 if (ident->plugin_instance[0] == 0)
907 if (ident->type_instance[0] == 0)
908 snprintf (string, string_size, "%s/%s/%s",
913 snprintf (string, string_size, "%s/%s/%s-%s",
917 ident->type_instance);
921 if (ident->type_instance[0] == 0)
922 snprintf (string, string_size, "%s/%s-%s/%s",
925 ident->plugin_instance,
928 snprintf (string, string_size, "%s/%s-%s/%s-%s",
931 ident->plugin_instance,
933 ident->type_instance);
936 string[string_size - 1] = 0;
938 } /* }}} int lcc_identifier_to_string */
940 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
941 lcc_identifier_t *ident, const char *string)
946 char *plugin_instance;
950 string_copy = lcc_strdup (string);
951 if (string_copy == NULL)
953 lcc_set_errno (c, ENOMEM);
958 plugin = strchr (host, '/');
961 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
968 type = strchr (plugin, '/');
971 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
978 plugin_instance = strchr (plugin, '-');
979 if (plugin_instance != NULL)
981 *plugin_instance = 0;
985 type_instance = strchr (type, '-');
986 if (type_instance != NULL)
992 memset (ident, 0, sizeof (*ident));
994 SSTRCPY (ident->host, host);
995 SSTRCPY (ident->plugin, plugin);
996 if (plugin_instance != NULL)
997 SSTRCPY (ident->plugin_instance, plugin_instance);
998 SSTRCPY (ident->type, type);
999 if (type_instance != NULL)
1000 SSTRCPY (ident->type_instance, type_instance);
1004 } /* }}} int lcc_string_to_identifier */
1006 /* vim: set sw=2 sts=2 et fdm=marker : */