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 /* NI_MAXHOST has been obsoleted by RFC 3493 which is a reason for SunOS 5.11
65 * to no longer define it. We'll use the old, RFC 2553 value here. */
67 # define NI_MAXHOST 1025
70 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
71 * termination. They work for static buffers only, because they use `sizeof'.
72 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
73 * is very useful to add formatted stuff to the end of a buffer. */
74 #define SSTRCPY(d,s) do { \
75 strncpy ((d), (s), sizeof (d)); \
76 (d)[sizeof (d) - 1] = 0; \
79 #define SSTRCAT(d,s) do { \
80 strncat ((d), (s), sizeof (d)); \
81 (d)[sizeof (d) - 1] = 0; \
84 #define SSTRCATF(d, ...) do { \
85 char _b[sizeof (d)]; \
86 snprintf (_b, sizeof (_b), __VA_ARGS__); \
87 _b[sizeof (_b) - 1] = 0; \
92 #define LCC_SET_ERRSTR(c, ...) do { \
93 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
94 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
98 # define LCC_DEBUG(...) printf (__VA_ARGS__)
100 # define LCC_DEBUG(...) /**/
106 struct lcc_connection_s
112 struct lcc_response_s
119 typedef struct lcc_response_s lcc_response_t;
124 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
129 strerror_r (err, c->errbuf, sizeof (c->errbuf));
130 c->errbuf[sizeof (c->errbuf) - 1] = 0;
133 } /* }}} int lcc_set_errno */
135 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
137 __attribute__((malloc, nonnull (1)))
138 static char *lcc_strdup (const char *str) /* {{{ */
143 strsize = strlen (str) + 1;
144 ret = (char *) malloc (strsize);
146 memcpy (ret, str, strsize);
148 } /* }}} char *lcc_strdup */
150 __attribute__((nonnull (1, 2)))
151 static char *lcc_strescape (char *dest, char *src, size_t dest_size) /* {{{ */
159 assert (dest_size >= 3);
161 dest[dest_pos] = '"';
166 if ((dest_pos == (dest_size - 2))
167 || (src[src_pos] == 0))
170 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
172 /* Check if there is enough space for both characters.. */
173 if (dest_pos == (dest_size - 3))
176 dest[dest_pos] = '\\';
180 dest[dest_pos] = src[src_pos];
185 assert (dest_pos <= (dest_size - 2));
187 dest[dest_pos] = '"';
195 } /* }}} char *lcc_strescape */
197 /* lcc_chomp: Removes all control-characters at the end of a string. */
198 static void lcc_chomp (char *str) /* {{{ */
202 str_len = strlen (str);
205 if (str[str_len - 1] >= 32)
207 str[str_len - 1] = 0;
210 } /* }}} void lcc_chomp */
212 static void lcc_response_free (lcc_response_t *res) /* {{{ */
219 for (i = 0; i < res->lines_num; i++)
220 free (res->lines[i]);
223 } /* }}} void lcc_response_free */
225 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
229 LCC_DEBUG ("send: --> %s\n", command);
231 status = fprintf (c->fh, "%s\r\n", command);
234 lcc_set_errno (c, errno);
239 } /* }}} int lcc_send */
241 static int lcc_receive (lcc_connection_t *c, /* {{{ */
242 lcc_response_t *ret_res)
249 memset (&res, 0, sizeof (res));
251 /* Read the first line, containing the status and a message */
252 ptr = fgets (buffer, sizeof (buffer), c->fh);
255 lcc_set_errno (c, errno);
259 LCC_DEBUG ("receive: <-- %s\n", buffer);
261 /* Convert the leading status to an integer and make `ptr' to point to the
262 * beginning of the message. */
265 res.status = strtol (buffer, &ptr, 0);
266 if ((errno != 0) || (ptr == &buffer[0]))
268 lcc_set_errno (c, errno);
272 /* Skip white spaces after the status number */
273 while ((*ptr == ' ') || (*ptr == '\t'))
276 /* Now copy the message. */
277 strncpy (res.message, ptr, sizeof (res.message));
278 res.message[sizeof (res.message) - 1] = 0;
280 /* Error or no lines follow: We're done. */
283 memcpy (ret_res, &res, sizeof (res));
287 /* Allocate space for the char-pointers */
288 res.lines_num = (size_t) res.status;
290 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
291 if (res.lines == NULL)
293 lcc_set_errno (c, ENOMEM);
297 /* Now receive all the lines */
298 for (i = 0; i < res.lines_num; i++)
300 ptr = fgets (buffer, sizeof (buffer), c->fh);
303 lcc_set_errno (c, errno);
307 LCC_DEBUG ("receive: <-- %s\n", buffer);
309 res.lines[i] = lcc_strdup (buffer);
310 if (res.lines[i] == NULL)
312 lcc_set_errno (c, ENOMEM);
317 /* Check if the for-loop exited with an error. */
318 if (i < res.lines_num)
329 memcpy (ret_res, &res, sizeof (res));
331 } /* }}} int lcc_receive */
333 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
334 const char *command, lcc_response_t *ret_res)
339 status = lcc_send (c, command);
343 memset (&res, 0, sizeof (res));
344 status = lcc_receive (c, &res);
346 memcpy (ret_res, &res, sizeof (*ret_res));
349 } /* }}} int lcc_sendreceive */
351 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
353 struct sockaddr_un sa;
358 assert (c->fh == NULL);
359 assert (path != NULL);
361 fd = socket (PF_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 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
539 c = (lcc_connection_t *) malloc (sizeof (*c));
542 memset (c, 0, sizeof (*c));
545 return (lcc_open_socket (c, address));
546 } /* }}} int lcc_connect */
548 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
561 } /* }}} int lcc_disconnect */
563 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
564 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
566 char ident_str[6 * LCC_NAME_LEN];
567 char ident_esc[12 * LCC_NAME_LEN];
568 char command[14 * LCC_NAME_LEN];
572 gauge_t *values = NULL;
573 char **values_names = NULL;
583 lcc_set_errno (c, EINVAL);
587 /* Build a commend with an escaped version of the identifier string. */
588 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
592 snprintf (command, sizeof (command), "GETVAL %s",
593 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
594 command[sizeof (command) - 1] = 0;
596 /* Send talk to the daemon.. */
597 status = lcc_sendreceive (c, command, &res);
603 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
604 lcc_response_free (&res);
608 values_num = res.lines_num;
610 #define BAIL_OUT(e) do { \
611 lcc_set_errno (c, (e)); \
613 if (values_names != NULL) { \
614 for (i = 0; i < values_num; i++) { \
615 free (values_names[i]); \
618 free (values_names); \
619 lcc_response_free (&res); \
623 /* If neither the values nor the names are requested, return here.. */
624 if ((ret_values == NULL) && (ret_values_names == NULL))
626 if (ret_values_num != NULL)
627 *ret_values_num = values_num;
628 lcc_response_free (&res);
632 /* Allocate space for the values */
633 if (ret_values != NULL)
635 values = (gauge_t *) malloc (values_num * sizeof (*values));
640 if (ret_values_names != NULL)
642 values_names = (char **) calloc (values_num, sizeof (*values_names));
643 if (values_names == NULL)
647 for (i = 0; i < res.lines_num; i++)
654 value = strchr (key, '=');
665 values[i] = strtod (value, &endptr);
667 if ((endptr == value) || (errno != 0))
671 if (values_names != NULL)
673 values_names[i] = lcc_strdup (key);
674 if (values_names[i] == NULL)
677 } /* for (i = 0; i < res.lines_num; i++) */
679 if (ret_values_num != NULL)
680 *ret_values_num = values_num;
681 if (ret_values != NULL)
682 *ret_values = values;
683 if (ret_values_names != NULL)
684 *ret_values_names = values_names;
687 } /* }}} int lcc_getval */
689 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
691 char ident_str[6 * LCC_NAME_LEN];
692 char ident_esc[12 * LCC_NAME_LEN];
693 char command[1024] = "";
698 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
699 || (vl->values == NULL) || (vl->values_types == NULL))
701 lcc_set_errno (c, EINVAL);
705 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
710 SSTRCATF (command, "PUTVAL %s",
711 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
713 if (vl->interval > 0)
714 SSTRCATF (command, " interval=%i", vl->interval);
717 SSTRCATF (command, "%u", (unsigned int) vl->time);
719 SSTRCAT (command, "N");
721 for (i = 0; i < vl->values_len; i++)
723 if (vl->values_types[i] == LCC_TYPE_COUNTER)
724 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
725 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
727 if (isnan (vl->values[i].gauge))
728 SSTRCPY (command, ":U");
730 SSTRCATF (command, ":%g", vl->values[i].gauge);
732 } /* for (i = 0; i < vl->values_len; i++) */
734 status = lcc_sendreceive (c, command, &res);
740 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
741 lcc_response_free (&res);
745 lcc_response_free (&res);
747 } /* }}} int lcc_putval */
749 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
750 lcc_identifier_t *ident, int timeout)
758 lcc_set_errno (c, EINVAL);
762 SSTRCPY (command, "FLUSH");
765 SSTRCATF (command, " timeout=%i", timeout);
769 char buffer[2 * LCC_NAME_LEN];
770 SSTRCATF (command, " plugin=%s",
771 lcc_strescape (buffer, plugin, sizeof (buffer)));
776 char ident_str[6 * LCC_NAME_LEN];
777 char ident_esc[12 * LCC_NAME_LEN];
779 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
783 SSTRCATF (command, " identifier=%s",
784 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
787 status = lcc_sendreceive (c, command, &res);
793 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
794 lcc_response_free (&res);
798 lcc_response_free (&res);
800 } /* }}} int lcc_flush */
802 /* TODO: Implement lcc_putnotif */
804 int lcc_listval (lcc_connection_t *c, /* {{{ */
805 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
811 lcc_identifier_t *ident;
817 if ((ret_ident == NULL) || (ret_ident_num == NULL))
819 lcc_set_errno (c, EINVAL);
823 status = lcc_sendreceive (c, "LISTVAL", &res);
829 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
830 lcc_response_free (&res);
834 ident_num = res.lines_num;
835 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
838 lcc_response_free (&res);
839 lcc_set_errno (c, ENOMEM);
843 for (i = 0; i < res.lines_num; i++)
848 /* First field is the time. */
849 time_str = res.lines[i];
851 /* Set `ident_str' to the beginning of the second field. */
852 ident_str = time_str;
853 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
855 while ((*ident_str == ' ') || (*ident_str == '\t'))
863 lcc_set_errno (c, EPROTO);
868 status = lcc_string_to_identifier (c, ident + i, ident_str);
873 lcc_response_free (&res);
882 *ret_ident_num = ident_num;
885 } /* }}} int lcc_listval */
887 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
890 return ("Invalid object");
892 } /* }}} const char *lcc_strerror */
894 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
895 char *string, size_t string_size, const lcc_identifier_t *ident)
897 if ((string == NULL) || (string_size < 6) || (ident == NULL))
899 lcc_set_errno (c, EINVAL);
903 if (ident->plugin_instance[0] == 0)
905 if (ident->type_instance[0] == 0)
906 snprintf (string, string_size, "%s/%s/%s",
911 snprintf (string, string_size, "%s/%s/%s-%s",
915 ident->type_instance);
919 if (ident->type_instance[0] == 0)
920 snprintf (string, string_size, "%s/%s-%s/%s",
923 ident->plugin_instance,
926 snprintf (string, string_size, "%s/%s-%s/%s-%s",
929 ident->plugin_instance,
931 ident->type_instance);
934 string[string_size - 1] = 0;
936 } /* }}} int lcc_identifier_to_string */
938 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
939 lcc_identifier_t *ident, const char *string)
944 char *plugin_instance;
948 string_copy = lcc_strdup (string);
949 if (string_copy == NULL)
951 lcc_set_errno (c, ENOMEM);
956 plugin = strchr (host, '/');
959 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
966 type = strchr (plugin, '/');
969 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
976 plugin_instance = strchr (plugin, '-');
977 if (plugin_instance != NULL)
979 *plugin_instance = 0;
983 type_instance = strchr (type, '-');
984 if (type_instance != NULL)
990 memset (ident, 0, sizeof (*ident));
992 SSTRCPY (ident->host, host);
993 SSTRCPY (ident->plugin, plugin);
994 if (plugin_instance != NULL)
995 SSTRCPY (ident->plugin_instance, plugin_instance);
996 SSTRCPY (ident->type, type);
997 if (type_instance != NULL)
998 SSTRCPY (ident->type_instance, type_instance);
1002 } /* }}} int lcc_string_to_identifier */
1004 /* vim: set sw=2 sts=2 et fdm=marker : */