1 /**
2 * libcollectdclient - src/libcollectdclient/client.c
3 * Copyright (C) 2008 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #if !defined(__GNUC__) || !__GNUC__
27 # define __attribute__(x) /**/
28 #endif
30 #include "lcc_features.h"
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <string.h>
39 #include <assert.h>
40 #include <errno.h>
41 #include <math.h>
42 #include <netdb.h>
44 #include "client.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. */
48 #ifndef NI_MAXHOST
49 # define NI_MAXHOST 1025
50 #endif
52 /* OpenBSD doesn't have EPROTO, FreeBSD doesn't have EILSEQ. Oh what joy! */
53 #ifndef EILSEQ
54 # ifdef EPROTO
55 # define EILSEQ EPROTO
56 # else
57 # define EILSEQ EINVAL
58 # endif
59 #endif
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; \
68 } while (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; \
74 } while (0)
76 #define SSTRCATF(d, ...) do { \
77 char _b[sizeof (d)]; \
78 snprintf (_b, sizeof (_b), __VA_ARGS__); \
79 _b[sizeof (_b) - 1] = 0; \
80 SSTRCAT ((d), _b); \
81 } while (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; \
87 } while (0)
89 #if COLLECT_DEBUG
90 # define LCC_DEBUG(...) printf (__VA_ARGS__)
91 #else
92 # define LCC_DEBUG(...) /**/
93 #endif
95 /*
96 * Types
97 */
98 struct lcc_connection_s
99 {
100 FILE *fh;
101 char errbuf[1024];
102 };
104 struct lcc_response_s
105 {
106 int status;
107 char message[1024];
108 char **lines;
109 size_t lines_num;
110 };
111 typedef struct lcc_response_s lcc_response_t;
113 /*
114 * Private functions
115 */
116 /* Even though Posix requires "strerror_r" to return an "int",
117 * some systems (e.g. the GNU libc) return a "char *" _and_
118 * ignore the second argument ... -tokkee */
119 static char *sstrerror (int errnum, char *buf, size_t buflen)
120 {
121 buf[0] = 0;
123 #if !HAVE_STRERROR_R
124 snprintf (buf, buflen, "Error #%i; strerror_r is not available.", errnum);
125 /* #endif !HAVE_STRERROR_R */
127 #elif STRERROR_R_CHAR_P
128 {
129 char *temp;
130 temp = strerror_r (errnum, buf, buflen);
131 if (buf[0] == 0)
132 {
133 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
134 strncpy (buf, temp, buflen);
135 else
136 strncpy (buf, "strerror_r did not return "
137 "an error message", buflen);
138 }
139 }
140 /* #endif STRERROR_R_CHAR_P */
142 #else
143 if (strerror_r (errnum, buf, buflen) != 0)
144 {
145 snprintf (buf, buflen, "Error #%i; "
146 "Additionally, strerror_r failed.",
147 errnum);
148 }
149 #endif /* STRERROR_R_CHAR_P */
151 buf[buflen - 1] = 0;
153 return (buf);
154 } /* char *sstrerror */
156 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
157 {
158 if (c == NULL)
159 return (-1);
161 sstrerror (err, c->errbuf, sizeof (c->errbuf));
162 c->errbuf[sizeof (c->errbuf) - 1] = 0;
164 return (0);
165 } /* }}} int lcc_set_errno */
167 static char *lcc_strescape (char *dest, const char *src, size_t dest_size) /* {{{ */
168 {
169 size_t dest_pos;
170 size_t src_pos;
172 if ((dest == NULL) || (src == NULL))
173 return (NULL);
175 dest_pos = 0;
176 src_pos = 0;
178 assert (dest_size >= 3);
180 dest[dest_pos] = '"';
181 dest_pos++;
183 while (42)
184 {
185 if ((dest_pos == (dest_size - 2))
186 || (src[src_pos] == 0))
187 break;
189 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
190 {
191 /* Check if there is enough space for both characters.. */
192 if (dest_pos == (dest_size - 3))
193 break;
195 dest[dest_pos] = '\\';
196 dest_pos++;
197 }
199 dest[dest_pos] = src[src_pos];
200 dest_pos++;
201 src_pos++;
202 }
204 assert (dest_pos <= (dest_size - 2));
206 dest[dest_pos] = '"';
207 dest_pos++;
209 dest[dest_pos] = 0;
210 dest_pos++;
211 src_pos++;
213 return (dest);
214 } /* }}} char *lcc_strescape */
216 /* lcc_chomp: Removes all control-characters at the end of a string. */
217 static void lcc_chomp (char *str) /* {{{ */
218 {
219 size_t str_len;
221 str_len = strlen (str);
222 while (str_len > 0)
223 {
224 if (str[str_len - 1] >= 32)
225 break;
226 str[str_len - 1] = 0;
227 str_len--;
228 }
229 } /* }}} void lcc_chomp */
231 static void lcc_response_free (lcc_response_t *res) /* {{{ */
232 {
233 size_t i;
235 if (res == NULL)
236 return;
238 for (i = 0; i < res->lines_num; i++)
239 free (res->lines[i]);
240 free (res->lines);
241 res->lines = NULL;
242 } /* }}} void lcc_response_free */
244 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
245 {
246 int status;
248 LCC_DEBUG ("send: --> %s\n", command);
250 status = fprintf (c->fh, "%s\r\n", command);
251 if (status < 0)
252 {
253 lcc_set_errno (c, errno);
254 return (-1);
255 }
257 return (0);
258 } /* }}} int lcc_send */
260 static int lcc_receive (lcc_connection_t *c, /* {{{ */
261 lcc_response_t *ret_res)
262 {
263 lcc_response_t res;
264 char *ptr;
265 char buffer[4096];
266 size_t i;
268 memset (&res, 0, sizeof (res));
270 /* Read the first line, containing the status and a message */
271 ptr = fgets (buffer, sizeof (buffer), c->fh);
272 if (ptr == NULL)
273 {
274 lcc_set_errno (c, errno);
275 return (-1);
276 }
277 lcc_chomp (buffer);
278 LCC_DEBUG ("receive: <-- %s\n", buffer);
280 /* Convert the leading status to an integer and make `ptr' to point to the
281 * beginning of the message. */
282 ptr = NULL;
283 errno = 0;
284 res.status = strtol (buffer, &ptr, 0);
285 if ((errno != 0) || (ptr == &buffer[0]))
286 {
287 lcc_set_errno (c, errno);
288 return (-1);
289 }
291 /* Skip white spaces after the status number */
292 while ((*ptr == ' ') || (*ptr == '\t'))
293 ptr++;
295 /* Now copy the message. */
296 strncpy (res.message, ptr, sizeof (res.message));
297 res.message[sizeof (res.message) - 1] = 0;
299 /* Error or no lines follow: We're done. */
300 if (res.status <= 0)
301 {
302 memcpy (ret_res, &res, sizeof (res));
303 return (0);
304 }
306 /* Allocate space for the char-pointers */
307 res.lines_num = (size_t) res.status;
308 res.status = 0;
309 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
310 if (res.lines == NULL)
311 {
312 lcc_set_errno (c, ENOMEM);
313 return (-1);
314 }
316 /* Now receive all the lines */
317 for (i = 0; i < res.lines_num; i++)
318 {
319 ptr = fgets (buffer, sizeof (buffer), c->fh);
320 if (ptr == NULL)
321 {
322 lcc_set_errno (c, errno);
323 break;
324 }
325 lcc_chomp (buffer);
326 LCC_DEBUG ("receive: <-- %s\n", buffer);
328 res.lines[i] = strdup (buffer);
329 if (res.lines[i] == NULL)
330 {
331 lcc_set_errno (c, ENOMEM);
332 break;
333 }
334 }
336 /* Check if the for-loop exited with an error. */
337 if (i < res.lines_num)
338 {
339 while (i > 0)
340 {
341 i--;
342 free (res.lines[i]);
343 }
344 free (res.lines);
345 return (-1);
346 }
348 memcpy (ret_res, &res, sizeof (res));
349 return (0);
350 } /* }}} int lcc_receive */
352 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
353 const char *command, lcc_response_t *ret_res)
354 {
355 lcc_response_t res;
356 int status;
358 if (c->fh == NULL)
359 {
360 lcc_set_errno (c, EBADF);
361 return (-1);
362 }
364 status = lcc_send (c, command);
365 if (status != 0)
366 return (status);
368 memset (&res, 0, sizeof (res));
369 status = lcc_receive (c, &res);
370 if (status == 0)
371 memcpy (ret_res, &res, sizeof (*ret_res));
373 return (status);
374 } /* }}} int lcc_sendreceive */
376 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
377 {
378 struct sockaddr_un sa;
379 int fd;
380 int status;
382 assert (c != NULL);
383 assert (c->fh == NULL);
384 assert (path != NULL);
386 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
387 * others). */
388 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
389 if (fd < 0)
390 {
391 lcc_set_errno (c, errno);
392 return (-1);
393 }
395 memset (&sa, 0, sizeof (sa));
396 sa.sun_family = AF_UNIX;
397 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
399 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
400 if (status != 0)
401 {
402 lcc_set_errno (c, errno);
403 close (fd);
404 return (-1);
405 }
407 c->fh = fdopen (fd, "r+");
408 if (c->fh == NULL)
409 {
410 lcc_set_errno (c, errno);
411 close (fd);
412 return (-1);
413 }
415 return (0);
416 } /* }}} int lcc_open_unixsocket */
418 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
419 const char *addr_orig)
420 {
421 struct addrinfo ai_hints;
422 struct addrinfo *ai_res;
423 struct addrinfo *ai_ptr;
424 char addr_copy[NI_MAXHOST];
425 char *addr;
426 char *port;
427 int fd;
428 int status;
430 assert (c != NULL);
431 assert (c->fh == NULL);
432 assert (addr_orig != NULL);
434 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
435 addr_copy[sizeof(addr_copy) - 1] = '\0';
436 addr = addr_copy;
438 memset (&ai_hints, 0, sizeof (ai_hints));
439 ai_hints.ai_flags = 0;
440 #ifdef AI_ADDRCONFIG
441 ai_hints.ai_flags |= AI_ADDRCONFIG;
442 #endif
443 ai_hints.ai_family = AF_UNSPEC;
444 ai_hints.ai_socktype = SOCK_STREAM;
446 port = NULL;
447 if (*addr == '[') /* IPv6+port format */
448 {
449 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
450 addr++;
452 port = strchr (addr, ']');
453 if (port == NULL)
454 {
455 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
456 return (-1);
457 }
458 *port = 0;
459 port++;
461 if (*port == ':')
462 port++;
463 else if (*port == 0)
464 port = NULL;
465 else
466 {
467 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
468 return (-1);
469 }
470 } /* if (*addr = ']') */
471 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
472 {
473 port = strrchr (addr, ':');
474 if (port != NULL)
475 {
476 *port = 0;
477 port++;
478 }
479 }
481 ai_res = NULL;
482 status = getaddrinfo (addr,
483 port == NULL ? LCC_DEFAULT_PORT : port,
484 &ai_hints, &ai_res);
485 if (status != 0)
486 {
487 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
488 return (-1);
489 }
491 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
492 {
493 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
494 if (fd < 0)
495 {
496 status = errno;
497 fd = -1;
498 continue;
499 }
501 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
502 if (status != 0)
503 {
504 status = errno;
505 close (fd);
506 fd = -1;
507 continue;
508 }
510 c->fh = fdopen (fd, "r+");
511 if (c->fh == NULL)
512 {
513 status = errno;
514 close (fd);
515 fd = -1;
516 continue;
517 }
519 assert (status == 0);
520 break;
521 } /* for (ai_ptr) */
523 if (status != 0)
524 {
525 lcc_set_errno (c, status);
526 return (-1);
527 }
529 return (0);
530 } /* }}} int lcc_open_netsocket */
532 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
533 {
534 int status = 0;
536 if (addr == NULL)
537 return (-1);
539 assert (c != NULL);
540 assert (c->fh == NULL);
541 assert (addr != NULL);
543 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
544 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
545 else if (addr[0] == '/')
546 status = lcc_open_unixsocket (c, addr);
547 else
548 status = lcc_open_netsocket (c, addr);
550 return (status);
551 } /* }}} int lcc_open_socket */
553 /*
554 * Public functions
555 */
556 unsigned int lcc_version (void) /* {{{ */
557 {
558 return (LCC_VERSION);
559 } /* }}} unsigned int lcc_version */
561 const char *lcc_version_string (void) /* {{{ */
562 {
563 return (LCC_VERSION_STRING);
564 } /* }}} const char *lcc_version_string */
566 const char *lcc_version_extra (void) /* {{{ */
567 {
568 return (LCC_VERSION_EXTRA);
569 } /* }}} const char *lcc_version_extra */
571 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
572 {
573 lcc_connection_t *c;
574 int status;
576 if (address == NULL)
577 return (-1);
579 if (ret_con == NULL)
580 return (-1);
582 c = (lcc_connection_t *) malloc (sizeof (*c));
583 if (c == NULL)
584 return (-1);
585 memset (c, 0, sizeof (*c));
587 status = lcc_open_socket (c, address);
588 if (status != 0)
589 {
590 lcc_disconnect (c);
591 return (status);
592 }
594 *ret_con = c;
595 return (0);
596 } /* }}} int lcc_connect */
598 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
599 {
600 if (c == NULL)
601 return (-1);
603 if (c->fh != NULL)
604 {
605 fclose (c->fh);
606 c->fh = NULL;
607 }
609 free (c);
610 return (0);
611 } /* }}} int lcc_disconnect */
613 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
614 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
615 {
616 char ident_str[6 * LCC_NAME_LEN];
617 char ident_esc[12 * LCC_NAME_LEN];
618 char command[14 * LCC_NAME_LEN];
620 lcc_response_t res;
621 size_t values_num;
622 gauge_t *values = NULL;
623 char **values_names = NULL;
625 size_t i;
626 int status;
628 if (c == NULL)
629 return (-1);
631 if (ident == NULL)
632 {
633 lcc_set_errno (c, EINVAL);
634 return (-1);
635 }
637 /* Build a commend with an escaped version of the identifier string. */
638 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
639 if (status != 0)
640 return (status);
642 snprintf (command, sizeof (command), "GETVAL %s",
643 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
644 command[sizeof (command) - 1] = 0;
646 /* Send talk to the daemon.. */
647 status = lcc_sendreceive (c, command, &res);
648 if (status != 0)
649 return (status);
651 if (res.status != 0)
652 {
653 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
654 lcc_response_free (&res);
655 return (-1);
656 }
658 values_num = res.lines_num;
660 #define BAIL_OUT(e) do { \
661 lcc_set_errno (c, (e)); \
662 free (values); \
663 if (values_names != NULL) { \
664 for (i = 0; i < values_num; i++) { \
665 free (values_names[i]); \
666 } \
667 } \
668 free (values_names); \
669 lcc_response_free (&res); \
670 return (-1); \
671 } while (0)
673 /* If neither the values nor the names are requested, return here.. */
674 if ((ret_values == NULL) && (ret_values_names == NULL))
675 {
676 if (ret_values_num != NULL)
677 *ret_values_num = values_num;
678 lcc_response_free (&res);
679 return (0);
680 }
682 /* Allocate space for the values */
683 if (ret_values != NULL)
684 {
685 values = (gauge_t *) malloc (values_num * sizeof (*values));
686 if (values == NULL)
687 BAIL_OUT (ENOMEM);
688 }
690 if (ret_values_names != NULL)
691 {
692 values_names = (char **) calloc (values_num, sizeof (*values_names));
693 if (values_names == NULL)
694 BAIL_OUT (ENOMEM);
695 }
697 for (i = 0; i < res.lines_num; i++)
698 {
699 char *key;
700 char *value;
701 char *endptr;
703 key = res.lines[i];
704 value = strchr (key, '=');
705 if (value == NULL)
706 BAIL_OUT (EILSEQ);
708 *value = 0;
709 value++;
711 if (values != NULL)
712 {
713 endptr = NULL;
714 errno = 0;
715 values[i] = strtod (value, &endptr);
717 if ((endptr == value) || (errno != 0))
718 BAIL_OUT (errno);
719 }
721 if (values_names != NULL)
722 {
723 values_names[i] = strdup (key);
724 if (values_names[i] == NULL)
725 BAIL_OUT (ENOMEM);
726 }
727 } /* for (i = 0; i < res.lines_num; i++) */
729 if (ret_values_num != NULL)
730 *ret_values_num = values_num;
731 if (ret_values != NULL)
732 *ret_values = values;
733 if (ret_values_names != NULL)
734 *ret_values_names = values_names;
736 lcc_response_free (&res);
738 return (0);
739 } /* }}} int lcc_getval */
741 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
742 {
743 char ident_str[6 * LCC_NAME_LEN];
744 char ident_esc[12 * LCC_NAME_LEN];
745 char command[1024] = "";
746 lcc_response_t res;
747 int status;
748 size_t i;
750 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
751 || (vl->values == NULL) || (vl->values_types == NULL))
752 {
753 lcc_set_errno (c, EINVAL);
754 return (-1);
755 }
757 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
758 &vl->identifier);
759 if (status != 0)
760 return (status);
762 SSTRCATF (command, "PUTVAL %s",
763 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
765 if (vl->interval > 0)
766 SSTRCATF (command, " interval=%i", vl->interval);
768 if (vl->time > 0)
769 SSTRCATF (command, " %u", (unsigned int) vl->time);
770 else
771 SSTRCAT (command, " N");
773 for (i = 0; i < vl->values_len; i++)
774 {
775 if (vl->values_types[i] == LCC_TYPE_COUNTER)
776 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
777 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
778 {
779 if (isnan (vl->values[i].gauge))
780 SSTRCATF (command, ":U");
781 else
782 SSTRCATF (command, ":%g", vl->values[i].gauge);
783 }
784 else if (vl->values_types[i] == LCC_TYPE_DERIVE)
785 SSTRCATF (command, ":%"PRIu64, vl->values[i].derive);
786 else if (vl->values_types[i] == LCC_TYPE_ABSOLUTE)
787 SSTRCATF (command, ":%"PRIu64, vl->values[i].absolute);
789 } /* for (i = 0; i < vl->values_len; i++) */
791 status = lcc_sendreceive (c, command, &res);
792 if (status != 0)
793 return (status);
795 if (res.status != 0)
796 {
797 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
798 lcc_response_free (&res);
799 return (-1);
800 }
802 lcc_response_free (&res);
803 return (0);
804 } /* }}} int lcc_putval */
806 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
807 lcc_identifier_t *ident, int timeout)
808 {
809 char command[1024] = "";
810 lcc_response_t res;
811 int status;
813 if (c == NULL)
814 {
815 lcc_set_errno (c, EINVAL);
816 return (-1);
817 }
819 SSTRCPY (command, "FLUSH");
821 if (timeout > 0)
822 SSTRCATF (command, " timeout=%i", timeout);
824 if (plugin != NULL)
825 {
826 char buffer[2 * LCC_NAME_LEN];
827 SSTRCATF (command, " plugin=%s",
828 lcc_strescape (buffer, plugin, sizeof (buffer)));
829 }
831 if (ident != NULL)
832 {
833 char ident_str[6 * LCC_NAME_LEN];
834 char ident_esc[12 * LCC_NAME_LEN];
836 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
837 if (status != 0)
838 return (status);
840 SSTRCATF (command, " identifier=%s",
841 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
842 }
844 status = lcc_sendreceive (c, command, &res);
845 if (status != 0)
846 return (status);
848 if (res.status != 0)
849 {
850 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
851 lcc_response_free (&res);
852 return (-1);
853 }
855 lcc_response_free (&res);
856 return (0);
857 } /* }}} int lcc_flush */
859 /* TODO: Implement lcc_putnotif */
861 int lcc_listval (lcc_connection_t *c, /* {{{ */
862 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
863 {
864 lcc_response_t res;
865 size_t i;
866 int status;
868 lcc_identifier_t *ident;
869 size_t ident_num;
871 if (c == NULL)
872 return (-1);
874 if ((ret_ident == NULL) || (ret_ident_num == NULL))
875 {
876 lcc_set_errno (c, EINVAL);
877 return (-1);
878 }
880 status = lcc_sendreceive (c, "LISTVAL", &res);
881 if (status != 0)
882 return (status);
884 if (res.status != 0)
885 {
886 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
887 lcc_response_free (&res);
888 return (-1);
889 }
891 ident_num = res.lines_num;
892 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
893 if (ident == NULL)
894 {
895 lcc_response_free (&res);
896 lcc_set_errno (c, ENOMEM);
897 return (-1);
898 }
900 for (i = 0; i < res.lines_num; i++)
901 {
902 char *time_str;
903 char *ident_str;
905 /* First field is the time. */
906 time_str = res.lines[i];
908 /* Set `ident_str' to the beginning of the second field. */
909 ident_str = time_str;
910 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
911 ident_str++;
912 while ((*ident_str == ' ') || (*ident_str == '\t'))
913 {
914 *ident_str = 0;
915 ident_str++;
916 }
918 if (*ident_str == 0)
919 {
920 lcc_set_errno (c, EILSEQ);
921 status = -1;
922 break;
923 }
925 status = lcc_string_to_identifier (c, ident + i, ident_str);
926 if (status != 0)
927 break;
928 }
930 lcc_response_free (&res);
932 if (status != 0)
933 {
934 free (ident);
935 return (-1);
936 }
938 *ret_ident = ident;
939 *ret_ident_num = ident_num;
941 return (0);
942 } /* }}} int lcc_listval */
944 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
945 {
946 if (c == NULL)
947 return ("Invalid object");
948 return (c->errbuf);
949 } /* }}} const char *lcc_strerror */
951 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
952 char *string, size_t string_size, const lcc_identifier_t *ident)
953 {
954 if ((string == NULL) || (string_size < 6) || (ident == NULL))
955 {
956 lcc_set_errno (c, EINVAL);
957 return (-1);
958 }
960 if (ident->plugin_instance[0] == 0)
961 {
962 if (ident->type_instance[0] == 0)
963 snprintf (string, string_size, "%s/%s/%s",
964 ident->host,
965 ident->plugin,
966 ident->type);
967 else
968 snprintf (string, string_size, "%s/%s/%s-%s",
969 ident->host,
970 ident->plugin,
971 ident->type,
972 ident->type_instance);
973 }
974 else
975 {
976 if (ident->type_instance[0] == 0)
977 snprintf (string, string_size, "%s/%s-%s/%s",
978 ident->host,
979 ident->plugin,
980 ident->plugin_instance,
981 ident->type);
982 else
983 snprintf (string, string_size, "%s/%s-%s/%s-%s",
984 ident->host,
985 ident->plugin,
986 ident->plugin_instance,
987 ident->type,
988 ident->type_instance);
989 }
991 string[string_size - 1] = 0;
992 return (0);
993 } /* }}} int lcc_identifier_to_string */
995 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
996 lcc_identifier_t *ident, const char *string)
997 {
998 char *string_copy;
999 char *host;
1000 char *plugin;
1001 char *plugin_instance;
1002 char *type;
1003 char *type_instance;
1005 string_copy = strdup (string);
1006 if (string_copy == NULL)
1007 {
1008 lcc_set_errno (c, ENOMEM);
1009 return (-1);
1010 }
1012 host = string_copy;
1013 plugin = strchr (host, '/');
1014 if (plugin == NULL)
1015 {
1016 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1017 free (string_copy);
1018 return (-1);
1019 }
1020 *plugin = 0;
1021 plugin++;
1023 type = strchr (plugin, '/');
1024 if (type == NULL)
1025 {
1026 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1027 free (string_copy);
1028 return (-1);
1029 }
1030 *type = 0;
1031 type++;
1033 plugin_instance = strchr (plugin, '-');
1034 if (plugin_instance != NULL)
1035 {
1036 *plugin_instance = 0;
1037 plugin_instance++;
1038 }
1040 type_instance = strchr (type, '-');
1041 if (type_instance != NULL)
1042 {
1043 *type_instance = 0;
1044 type_instance++;
1045 }
1047 memset (ident, 0, sizeof (*ident));
1049 SSTRCPY (ident->host, host);
1050 SSTRCPY (ident->plugin, plugin);
1051 if (plugin_instance != NULL)
1052 SSTRCPY (ident->plugin_instance, plugin_instance);
1053 SSTRCPY (ident->type, type);
1054 if (type_instance != NULL)
1055 SSTRCPY (ident->type_instance, type_instance);
1057 free (string_copy);
1058 return (0);
1059 } /* }}} int lcc_string_to_identifier */
1061 /* vim: set sw=2 sts=2 et fdm=marker : */