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 LCC_SET_ERRSTR(c, ...) do { \
70 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
71 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
75 # define LCC_DEBUG(...) printf (__VA_ARGS__)
77 # define LCC_DEBUG(...) /**/
83 struct lcc_connection_s
96 typedef struct lcc_response_s lcc_response_t;
101 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
106 strerror_r (err, c->errbuf, sizeof (c->errbuf));
107 c->errbuf[sizeof (c->errbuf) - 1] = 0;
110 } /* }}} int lcc_set_errno */
112 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
114 __attribute__((malloc, nonnull (1)))
115 static char *lcc_strdup (const char *str) /* {{{ */
120 strsize = strlen (str) + 1;
121 ret = (char *) malloc (strsize);
123 memcpy (ret, str, strsize);
125 } /* }}} char *lcc_strdup */
127 __attribute__((nonnull (1, 2)))
128 static char *lcc_strescape (char *dest, char *src, size_t dest_size) /* {{{ */
136 assert (dest_size >= 3);
138 dest[dest_pos] = '"';
143 if ((dest_pos == (dest_size - 2))
144 || (src[src_pos] == 0))
147 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
149 /* Check if there is enough space for both characters.. */
150 if (dest_pos == (dest_size - 3))
153 dest[dest_pos] = '\\';
157 dest[dest_pos] = src[src_pos];
162 assert (dest_pos <= (dest_size - 2));
164 dest[dest_pos] = '"';
172 } /* }}} char *lcc_strescape */
174 /* lcc_chomp: Removes all control-characters at the end of a string. */
175 static void lcc_chomp (char *str) /* {{{ */
179 str_len = strlen (str);
182 if (str[str_len - 1] >= 32)
184 str[str_len - 1] = 0;
187 } /* }}} void lcc_chomp */
189 static void lcc_response_free (lcc_response_t *res) /* {{{ */
196 for (i = 0; i < res->lines_num; i++)
197 free (res->lines[i]);
200 } /* }}} void lcc_response_free */
202 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
206 LCC_DEBUG ("send: --> %s\n", command);
208 status = fprintf (c->fh, "%s\r\n", command);
211 lcc_set_errno (c, errno);
216 } /* }}} int lcc_send */
218 static int lcc_receive (lcc_connection_t *c, /* {{{ */
219 lcc_response_t *ret_res)
226 memset (&res, 0, sizeof (res));
228 /* Read the first line, containing the status and a message */
229 ptr = fgets (buffer, sizeof (buffer), c->fh);
232 lcc_set_errno (c, errno);
236 LCC_DEBUG ("receive: <-- %s\n", buffer);
238 /* Convert the leading status to an integer and make `ptr' to point to the
239 * beginning of the message. */
242 res.status = strtol (buffer, &ptr, 0);
243 if ((errno != 0) || (ptr == &buffer[0]))
245 lcc_set_errno (c, errno);
249 /* Skip white spaces after the status number */
250 while ((*ptr == ' ') || (*ptr == '\t'))
253 /* Now copy the message. */
254 strncpy (res.message, ptr, sizeof (res.message));
255 res.message[sizeof (res.message) - 1] = 0;
257 /* Error or no lines follow: We're done. */
260 memcpy (ret_res, &res, sizeof (res));
264 /* Allocate space for the char-pointers */
265 res.lines_num = (size_t) res.status;
267 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
268 if (res.lines == NULL)
270 lcc_set_errno (c, ENOMEM);
274 /* Now receive all the lines */
275 for (i = 0; i < res.lines_num; i++)
277 ptr = fgets (buffer, sizeof (buffer), c->fh);
280 lcc_set_errno (c, errno);
284 LCC_DEBUG ("receive: <-- %s\n", buffer);
286 res.lines[i] = lcc_strdup (buffer);
287 if (res.lines[i] == NULL)
289 lcc_set_errno (c, ENOMEM);
294 /* Check if the for-loop exited with an error. */
295 if (i < res.lines_num)
306 memcpy (ret_res, &res, sizeof (res));
308 } /* }}} int lcc_receive */
310 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
311 const char *command, lcc_response_t *ret_res)
316 status = lcc_send (c, command);
320 memset (&res, 0, sizeof (res));
321 status = lcc_receive (c, &res);
323 memcpy (ret_res, &res, sizeof (*ret_res));
326 } /* }}} int lcc_sendreceive */
328 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
330 struct sockaddr_un sa;
335 assert (c->fh == NULL);
336 assert (path != NULL);
338 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
341 lcc_set_errno (c, errno);
345 memset (&sa, 0, sizeof (sa));
346 sa.sun_family = AF_UNIX;
347 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
349 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
352 lcc_set_errno (c, errno);
357 c->fh = fdopen (fd, "r+");
360 lcc_set_errno (c, errno);
366 } /* }}} int lcc_open_unixsocket */
368 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
369 const char *addr_orig)
371 struct addrinfo ai_hints;
372 struct addrinfo *ai_res;
373 struct addrinfo *ai_ptr;
374 char addr_copy[NI_MAXHOST];
381 assert (c->fh == NULL);
382 assert (addr_orig != NULL);
384 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
385 addr_copy[sizeof(addr_copy) - 1] = '\0';
388 memset (&ai_hints, 0, sizeof (ai_hints));
389 ai_hints.ai_flags = 0;
391 ai_hints.ai_flags |= AI_ADDRCONFIG;
393 ai_hints.ai_family = AF_UNSPEC;
394 ai_hints.ai_socktype = SOCK_STREAM;
397 if (*addr == '[') /* IPv6+port format */
399 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
402 port = strchr (addr, ']');
405 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
417 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
420 } /* if (*addr = ']') */
421 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
423 port = strrchr (addr, ':');
432 status = getaddrinfo (addr,
433 port == NULL ? LCC_DEFAULT_PORT : port,
437 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
441 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
443 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
451 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
460 c->fh = fdopen (fd, "r+");
469 assert (status == 0);
475 lcc_set_errno (c, status);
480 } /* }}} int lcc_open_netsocket */
482 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
490 assert (c->fh == NULL);
491 assert (addr != NULL);
493 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
494 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
495 else if (addr[0] == '/')
496 status = lcc_open_unixsocket (c, addr);
498 status = lcc_open_netsocket (c, addr);
501 } /* }}} int lcc_open_socket */
506 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
516 c = (lcc_connection_t *) malloc (sizeof (*c));
519 memset (c, 0, sizeof (*c));
522 return (lcc_open_socket (c, address));
523 } /* }}} int lcc_connect */
525 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
538 } /* }}} int lcc_disconnect */
540 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
541 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
543 char ident_str[6 * LCC_NAME_LEN];
544 char ident_esc[12 * LCC_NAME_LEN];
545 char command[14 * LCC_NAME_LEN];
549 gauge_t *values = NULL;
550 char **values_names = NULL;
560 lcc_set_errno (c, EINVAL);
564 /* Build a commend with an escaped version of the identifier string. */
565 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
569 snprintf (command, sizeof (command), "GETVAL %s",
570 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
571 command[sizeof (command) - 1] = 0;
573 /* Send talk to the daemon.. */
574 status = lcc_sendreceive (c, command, &res);
580 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
584 values_num = res.lines_num;
586 #define BAIL_OUT(e) do { \
587 lcc_set_errno (c, (e)); \
589 if (values_names != NULL) { \
590 for (i = 0; i < values_num; i++) { \
591 free (values_names[i]); \
594 free (values_names); \
595 lcc_response_free (&res); \
599 /* If neither the values nor the names are requested, return here.. */
600 if ((ret_values == NULL) && (ret_values_names == NULL))
602 if (ret_values_num != NULL)
603 *ret_values_num = values_num;
604 lcc_response_free (&res);
608 /* Allocate space for the values */
609 if (ret_values != NULL)
611 values = (gauge_t *) malloc (values_num * sizeof (*values));
616 if (ret_values_names != NULL)
618 values_names = (char **) calloc (values_num, sizeof (*values_names));
619 if (values_names == NULL)
623 for (i = 0; i < res.lines_num; i++)
630 value = strchr (key, '=');
641 values[i] = strtod (value, &endptr);
643 if ((endptr == value) || (errno != 0))
647 if (values_names != NULL)
649 values_names[i] = lcc_strdup (key);
650 if (values_names[i] == NULL)
653 } /* for (i = 0; i < res.lines_num; i++) */
655 if (ret_values_num != NULL)
656 *ret_values_num = values_num;
657 if (ret_values != NULL)
658 *ret_values = values;
659 if (ret_values_names != NULL)
660 *ret_values_names = values_names;
663 } /* }}} int lcc_getval */
665 /* TODO: Implement lcc_putval */
666 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl);
668 /* TODO: Implement lcc_flush */
669 int lcc_flush (lcc_connection_t *c, lcc_identifier_t *ident, int timeout);
671 /* TODO: Implement lcc_putnotif */
673 int lcc_listval (lcc_connection_t *c, /* {{{ */
674 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
680 lcc_identifier_t *ident;
686 if ((ret_ident == NULL) || (ret_ident_num == NULL))
688 lcc_set_errno (c, EINVAL);
692 status = lcc_sendreceive (c, "LISTVAL", &res);
698 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
702 ident_num = res.lines_num;
703 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
706 lcc_response_free (&res);
707 lcc_set_errno (c, ENOMEM);
711 for (i = 0; i < res.lines_num; i++)
716 /* First field is the time. */
717 time_str = res.lines[i];
719 /* Set `ident_str' to the beginning of the second field. */
720 ident_str = time_str;
721 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
723 while ((*ident_str == ' ') || (*ident_str == '\t'))
731 lcc_set_errno (c, EPROTO);
736 status = lcc_string_to_identifier (c, ident + i, ident_str);
741 lcc_response_free (&res);
750 *ret_ident_num = ident_num;
753 } /* }}} int lcc_listval */
755 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
758 return ("Invalid object");
760 } /* }}} const char *lcc_strerror */
762 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
763 char *string, size_t string_size, const lcc_identifier_t *ident)
765 if ((string == NULL) || (string_size < 6) || (ident == NULL))
767 lcc_set_errno (c, EINVAL);
771 if (ident->plugin_instance[0] == 0)
773 if (ident->type_instance[0] == 0)
774 snprintf (string, string_size, "%s/%s/%s",
779 snprintf (string, string_size, "%s/%s/%s-%s",
783 ident->type_instance);
787 if (ident->type_instance[0] == 0)
788 snprintf (string, string_size, "%s/%s-%s/%s",
791 ident->plugin_instance,
794 snprintf (string, string_size, "%s/%s-%s/%s-%s",
797 ident->plugin_instance,
799 ident->type_instance);
802 string[string_size - 1] = 0;
804 } /* }}} int lcc_identifier_to_string */
806 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
807 lcc_identifier_t *ident, const char *string)
812 char *plugin_instance;
816 string_copy = lcc_strdup (string);
817 if (string_copy == NULL)
819 lcc_set_errno (c, ENOMEM);
824 plugin = strchr (host, '/');
827 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
834 type = strchr (plugin, '/');
837 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
844 plugin_instance = strchr (plugin, '-');
845 if (plugin_instance != NULL)
847 *plugin_instance = 0;
851 type_instance = strchr (type, '-');
852 if (type_instance != NULL)
858 memset (ident, 0, sizeof (*ident));
860 SSTRCPY (ident->host, host);
861 SSTRCPY (ident->plugin, plugin);
862 if (plugin_instance != NULL)
863 SSTRCPY (ident->plugin_instance, plugin_instance);
864 SSTRCPY (ident->type, type);
865 if (type_instance != NULL)
866 SSTRCPY (ident->type_instance, type_instance);
870 } /* }}} int lcc_string_to_identifier */
872 /* vim: set sw=2 sts=2 et fdm=marker : */