f62994c6d02b5abd4101f15fb8511c508d21b04f
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 /* Set to C99 and POSIX code */
23 #ifndef _ISOC99_SOURCE
24 # define _ISOC99_SOURCE
25 #endif
26 #ifndef _POSIX_SOURCE
27 # define _POSIX_SOURCE
28 #endif
29 #ifndef _POSIX_C_SOURCE
30 # define _POSIX_C_SOURCE 200112L
31 #endif
32 #ifndef _REENTRANT
33 # define _REENTRANT
34 #endif
36 /* Disable non-standard extensions */
37 #ifdef _BSD_SOURCE
38 # undef _BSD_SOURCE
39 #endif
40 #ifdef _SVID_SOURCE
41 # undef _SVID_SOURCE
42 #endif
43 #ifdef _GNU_SOURCE
44 # undef _GNU_SOURCE
45 #endif
47 #if !defined(__GNUC__) || !__GNUC__
48 # define __attribute__(x) /**/
49 #endif
51 #if HAVE_CONFIG_H
52 # include "config.h"
53 #endif
55 #include "lcc_features.h"
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <unistd.h>
60 #include <sys/types.h>
61 #include <sys/socket.h>
62 #include <sys/un.h>
63 #include <string.h>
64 #include <assert.h>
65 #include <errno.h>
66 #include <math.h>
67 #include <netdb.h>
69 #include "client.h"
71 /* NI_MAXHOST has been obsoleted by RFC 3493 which is a reason for SunOS 5.11
72 * to no longer define it. We'll use the old, RFC 2553 value here. */
73 #ifndef NI_MAXHOST
74 # define NI_MAXHOST 1025
75 #endif
77 /* OpenBSD doesn't have EPROTO, FreeBSD doesn't have EILSEQ. Oh what joy! */
78 #ifndef EILSEQ
79 # ifdef EPROTO
80 # define EILSEQ EPROTO
81 # else
82 # define EILSEQ EINVAL
83 # endif
84 #endif
86 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
87 * termination. They work for static buffers only, because they use `sizeof'.
88 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
89 * is very useful to add formatted stuff to the end of a buffer. */
90 #define SSTRCPY(d,s) do { \
91 strncpy ((d), (s), sizeof (d)); \
92 (d)[sizeof (d) - 1] = 0; \
93 } while (0)
95 #define SSTRCAT(d,s) do { \
96 size_t _l = strlen (d); \
97 strncpy ((d) + _l, (s), sizeof (d) - _l); \
98 (d)[sizeof (d) - 1] = 0; \
99 } while (0)
101 #define SSTRCATF(d, ...) do { \
102 char _b[sizeof (d)]; \
103 snprintf (_b, sizeof (_b), __VA_ARGS__); \
104 _b[sizeof (_b) - 1] = 0; \
105 SSTRCAT ((d), _b); \
106 } while (0)
109 #define LCC_SET_ERRSTR(c, ...) do { \
110 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
111 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
112 } while (0)
114 #if COLLECT_DEBUG
115 # define LCC_DEBUG(...) printf (__VA_ARGS__)
116 #else
117 # define LCC_DEBUG(...) /**/
118 #endif
120 /*
121 * Types
122 */
123 struct lcc_connection_s
124 {
125 FILE *fh;
126 char errbuf[1024];
127 };
129 struct lcc_response_s
130 {
131 int status;
132 char message[1024];
133 char **lines;
134 size_t lines_num;
135 };
136 typedef struct lcc_response_s lcc_response_t;
138 /*
139 * Private functions
140 */
141 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
142 {
143 if (c == NULL)
144 return (-1);
146 strerror_r (err, c->errbuf, sizeof (c->errbuf));
147 c->errbuf[sizeof (c->errbuf) - 1] = 0;
149 return (0);
150 } /* }}} int lcc_set_errno */
152 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
153 * here. */
154 __attribute__((malloc, nonnull (1)))
155 static char *lcc_strdup (const char *str) /* {{{ */
156 {
157 size_t strsize;
158 char *ret;
160 strsize = strlen (str) + 1;
161 ret = (char *) malloc (strsize);
162 if (ret != NULL)
163 memcpy (ret, str, strsize);
164 return (ret);
165 } /* }}} char *lcc_strdup */
167 __attribute__((nonnull (1, 2)))
168 static char *lcc_strescape (char *dest, const char *src, size_t dest_size) /* {{{ */
169 {
170 size_t dest_pos;
171 size_t src_pos;
173 dest_pos = 0;
174 src_pos = 0;
176 assert (dest_size >= 3);
178 dest[dest_pos] = '"';
179 dest_pos++;
181 while (42)
182 {
183 if ((dest_pos == (dest_size - 2))
184 || (src[src_pos] == 0))
185 break;
187 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
188 {
189 /* Check if there is enough space for both characters.. */
190 if (dest_pos == (dest_size - 3))
191 break;
193 dest[dest_pos] = '\\';
194 dest_pos++;
195 }
197 dest[dest_pos] = src[src_pos];
198 dest_pos++;
199 src_pos++;
200 }
202 assert (dest_pos <= (dest_size - 2));
204 dest[dest_pos] = '"';
205 dest_pos++;
207 dest[dest_pos] = 0;
208 dest_pos++;
209 src_pos++;
211 return (dest);
212 } /* }}} char *lcc_strescape */
214 /* lcc_chomp: Removes all control-characters at the end of a string. */
215 static void lcc_chomp (char *str) /* {{{ */
216 {
217 size_t str_len;
219 str_len = strlen (str);
220 while (str_len > 0)
221 {
222 if (str[str_len - 1] >= 32)
223 break;
224 str[str_len - 1] = 0;
225 str_len--;
226 }
227 } /* }}} void lcc_chomp */
229 static void lcc_response_free (lcc_response_t *res) /* {{{ */
230 {
231 size_t i;
233 if (res == NULL)
234 return;
236 for (i = 0; i < res->lines_num; i++)
237 free (res->lines[i]);
238 free (res->lines);
239 res->lines = NULL;
240 } /* }}} void lcc_response_free */
242 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
243 {
244 int status;
246 LCC_DEBUG ("send: --> %s\n", command);
248 status = fprintf (c->fh, "%s\r\n", command);
249 if (status < 0)
250 {
251 lcc_set_errno (c, errno);
252 return (-1);
253 }
255 return (0);
256 } /* }}} int lcc_send */
258 static int lcc_receive (lcc_connection_t *c, /* {{{ */
259 lcc_response_t *ret_res)
260 {
261 lcc_response_t res;
262 char *ptr;
263 char buffer[4096];
264 size_t i;
266 memset (&res, 0, sizeof (res));
268 /* Read the first line, containing the status and a message */
269 ptr = fgets (buffer, sizeof (buffer), c->fh);
270 if (ptr == NULL)
271 {
272 lcc_set_errno (c, errno);
273 return (-1);
274 }
275 lcc_chomp (buffer);
276 LCC_DEBUG ("receive: <-- %s\n", buffer);
278 /* Convert the leading status to an integer and make `ptr' to point to the
279 * beginning of the message. */
280 ptr = NULL;
281 errno = 0;
282 res.status = strtol (buffer, &ptr, 0);
283 if ((errno != 0) || (ptr == &buffer[0]))
284 {
285 lcc_set_errno (c, errno);
286 return (-1);
287 }
289 /* Skip white spaces after the status number */
290 while ((*ptr == ' ') || (*ptr == '\t'))
291 ptr++;
293 /* Now copy the message. */
294 strncpy (res.message, ptr, sizeof (res.message));
295 res.message[sizeof (res.message) - 1] = 0;
297 /* Error or no lines follow: We're done. */
298 if (res.status <= 0)
299 {
300 memcpy (ret_res, &res, sizeof (res));
301 return (0);
302 }
304 /* Allocate space for the char-pointers */
305 res.lines_num = (size_t) res.status;
306 res.status = 0;
307 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
308 if (res.lines == NULL)
309 {
310 lcc_set_errno (c, ENOMEM);
311 return (-1);
312 }
314 /* Now receive all the lines */
315 for (i = 0; i < res.lines_num; i++)
316 {
317 ptr = fgets (buffer, sizeof (buffer), c->fh);
318 if (ptr == NULL)
319 {
320 lcc_set_errno (c, errno);
321 break;
322 }
323 lcc_chomp (buffer);
324 LCC_DEBUG ("receive: <-- %s\n", buffer);
326 res.lines[i] = lcc_strdup (buffer);
327 if (res.lines[i] == NULL)
328 {
329 lcc_set_errno (c, ENOMEM);
330 break;
331 }
332 }
334 /* Check if the for-loop exited with an error. */
335 if (i < res.lines_num)
336 {
337 while (i > 0)
338 {
339 i--;
340 free (res.lines[i]);
341 }
342 free (res.lines);
343 return (-1);
344 }
346 memcpy (ret_res, &res, sizeof (res));
347 return (0);
348 } /* }}} int lcc_receive */
350 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
351 const char *command, lcc_response_t *ret_res)
352 {
353 lcc_response_t res;
354 int status;
356 if (c->fh == NULL)
357 {
358 lcc_set_errno (c, EBADF);
359 return (-1);
360 }
362 status = lcc_send (c, command);
363 if (status != 0)
364 return (status);
366 memset (&res, 0, sizeof (res));
367 status = lcc_receive (c, &res);
368 if (status == 0)
369 memcpy (ret_res, &res, sizeof (*ret_res));
371 return (status);
372 } /* }}} int lcc_sendreceive */
374 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
375 {
376 struct sockaddr_un sa;
377 int fd;
378 int status;
380 assert (c != NULL);
381 assert (c->fh == NULL);
382 assert (path != NULL);
384 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
385 * others). */
386 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
387 if (fd < 0)
388 {
389 lcc_set_errno (c, errno);
390 return (-1);
391 }
393 memset (&sa, 0, sizeof (sa));
394 sa.sun_family = AF_UNIX;
395 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
397 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
398 if (status != 0)
399 {
400 lcc_set_errno (c, errno);
401 close (fd);
402 return (-1);
403 }
405 c->fh = fdopen (fd, "r+");
406 if (c->fh == NULL)
407 {
408 lcc_set_errno (c, errno);
409 close (fd);
410 return (-1);
411 }
413 return (0);
414 } /* }}} int lcc_open_unixsocket */
416 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
417 const char *addr_orig)
418 {
419 struct addrinfo ai_hints;
420 struct addrinfo *ai_res;
421 struct addrinfo *ai_ptr;
422 char addr_copy[NI_MAXHOST];
423 char *addr;
424 char *port;
425 int fd;
426 int status;
428 assert (c != NULL);
429 assert (c->fh == NULL);
430 assert (addr_orig != NULL);
432 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
433 addr_copy[sizeof(addr_copy) - 1] = '\0';
434 addr = addr_copy;
436 memset (&ai_hints, 0, sizeof (ai_hints));
437 ai_hints.ai_flags = 0;
438 #ifdef AI_ADDRCONFIG
439 ai_hints.ai_flags |= AI_ADDRCONFIG;
440 #endif
441 ai_hints.ai_family = AF_UNSPEC;
442 ai_hints.ai_socktype = SOCK_STREAM;
444 port = NULL;
445 if (*addr == '[') /* IPv6+port format */
446 {
447 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
448 addr++;
450 port = strchr (addr, ']');
451 if (port == NULL)
452 {
453 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
454 return (-1);
455 }
456 *port = 0;
457 port++;
459 if (*port == ':')
460 port++;
461 else if (*port == 0)
462 port = NULL;
463 else
464 {
465 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
466 return (-1);
467 }
468 } /* if (*addr = ']') */
469 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
470 {
471 port = strrchr (addr, ':');
472 if (port != NULL)
473 {
474 *port = 0;
475 port++;
476 }
477 }
479 ai_res = NULL;
480 status = getaddrinfo (addr,
481 port == NULL ? LCC_DEFAULT_PORT : port,
482 &ai_hints, &ai_res);
483 if (status != 0)
484 {
485 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
486 return (-1);
487 }
489 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
490 {
491 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
492 if (fd < 0)
493 {
494 status = errno;
495 fd = -1;
496 continue;
497 }
499 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
500 if (status != 0)
501 {
502 status = errno;
503 close (fd);
504 fd = -1;
505 continue;
506 }
508 c->fh = fdopen (fd, "r+");
509 if (c->fh == NULL)
510 {
511 status = errno;
512 close (fd);
513 fd = -1;
514 continue;
515 }
517 assert (status == 0);
518 break;
519 } /* for (ai_ptr) */
521 if (status != 0)
522 {
523 lcc_set_errno (c, status);
524 return (-1);
525 }
527 return (0);
528 } /* }}} int lcc_open_netsocket */
530 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
531 {
532 int status = 0;
534 if (addr == NULL)
535 return (-1);
537 assert (c != NULL);
538 assert (c->fh == NULL);
539 assert (addr != NULL);
541 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
542 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
543 else if (addr[0] == '/')
544 status = lcc_open_unixsocket (c, addr);
545 else
546 status = lcc_open_netsocket (c, addr);
548 return (status);
549 } /* }}} int lcc_open_socket */
551 /*
552 * Public functions
553 */
554 unsigned int lcc_version (void) /* {{{ */
555 {
556 return (LCC_VERSION);
557 } /* }}} unsigned int lcc_version */
559 const char *lcc_version_string (void) /* {{{ */
560 {
561 return (LCC_VERSION_STRING);
562 } /* }}} const char *lcc_version_string */
564 const char *lcc_version_extra (void) /* {{{ */
565 {
566 return (LCC_VERSION_EXTRA);
567 } /* }}} const char *lcc_version_extra */
569 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
570 {
571 lcc_connection_t *c;
572 int status;
574 if (address == NULL)
575 return (-1);
577 if (ret_con == NULL)
578 return (-1);
580 c = (lcc_connection_t *) malloc (sizeof (*c));
581 if (c == NULL)
582 return (-1);
583 memset (c, 0, sizeof (*c));
585 status = lcc_open_socket (c, address);
586 if (status != 0)
587 {
588 lcc_disconnect (c);
589 return (status);
590 }
592 *ret_con = c;
593 return (0);
594 } /* }}} int lcc_connect */
596 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
597 {
598 if (c == NULL)
599 return (-1);
601 if (c->fh != NULL)
602 {
603 fclose (c->fh);
604 c->fh = NULL;
605 }
607 free (c);
608 return (0);
609 } /* }}} int lcc_disconnect */
611 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
612 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
613 {
614 char ident_str[6 * LCC_NAME_LEN];
615 char ident_esc[12 * LCC_NAME_LEN];
616 char command[14 * LCC_NAME_LEN];
618 lcc_response_t res;
619 size_t values_num;
620 gauge_t *values = NULL;
621 char **values_names = NULL;
623 size_t i;
624 int status;
626 if (c == NULL)
627 return (-1);
629 if (ident == NULL)
630 {
631 lcc_set_errno (c, EINVAL);
632 return (-1);
633 }
635 /* Build a commend with an escaped version of the identifier string. */
636 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
637 if (status != 0)
638 return (status);
640 snprintf (command, sizeof (command), "GETVAL %s",
641 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
642 command[sizeof (command) - 1] = 0;
644 /* Send talk to the daemon.. */
645 status = lcc_sendreceive (c, command, &res);
646 if (status != 0)
647 return (status);
649 if (res.status != 0)
650 {
651 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
652 lcc_response_free (&res);
653 return (-1);
654 }
656 values_num = res.lines_num;
658 #define BAIL_OUT(e) do { \
659 lcc_set_errno (c, (e)); \
660 free (values); \
661 if (values_names != NULL) { \
662 for (i = 0; i < values_num; i++) { \
663 free (values_names[i]); \
664 } \
665 } \
666 free (values_names); \
667 lcc_response_free (&res); \
668 return (-1); \
669 } while (0)
671 /* If neither the values nor the names are requested, return here.. */
672 if ((ret_values == NULL) && (ret_values_names == NULL))
673 {
674 if (ret_values_num != NULL)
675 *ret_values_num = values_num;
676 lcc_response_free (&res);
677 return (0);
678 }
680 /* Allocate space for the values */
681 if (ret_values != NULL)
682 {
683 values = (gauge_t *) malloc (values_num * sizeof (*values));
684 if (values == NULL)
685 BAIL_OUT (ENOMEM);
686 }
688 if (ret_values_names != NULL)
689 {
690 values_names = (char **) calloc (values_num, sizeof (*values_names));
691 if (values_names == NULL)
692 BAIL_OUT (ENOMEM);
693 }
695 for (i = 0; i < res.lines_num; i++)
696 {
697 char *key;
698 char *value;
699 char *endptr;
701 key = res.lines[i];
702 value = strchr (key, '=');
703 if (value == NULL)
704 BAIL_OUT (EILSEQ);
706 *value = 0;
707 value++;
709 if (values != NULL)
710 {
711 endptr = NULL;
712 errno = 0;
713 values[i] = strtod (value, &endptr);
715 if ((endptr == value) || (errno != 0))
716 BAIL_OUT (errno);
717 }
719 if (values_names != NULL)
720 {
721 values_names[i] = lcc_strdup (key);
722 if (values_names[i] == NULL)
723 BAIL_OUT (ENOMEM);
724 }
725 } /* for (i = 0; i < res.lines_num; i++) */
727 if (ret_values_num != NULL)
728 *ret_values_num = values_num;
729 if (ret_values != NULL)
730 *ret_values = values;
731 if (ret_values_names != NULL)
732 *ret_values_names = values_names;
734 return (0);
735 } /* }}} int lcc_getval */
737 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
738 {
739 char ident_str[6 * LCC_NAME_LEN];
740 char ident_esc[12 * LCC_NAME_LEN];
741 char command[1024] = "";
742 lcc_response_t res;
743 int status;
744 size_t i;
746 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
747 || (vl->values == NULL) || (vl->values_types == NULL))
748 {
749 lcc_set_errno (c, EINVAL);
750 return (-1);
751 }
753 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
754 &vl->identifier);
755 if (status != 0)
756 return (status);
758 SSTRCATF (command, "PUTVAL %s",
759 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
761 if (vl->interval > 0)
762 SSTRCATF (command, " interval=%i", vl->interval);
764 if (vl->time > 0)
765 SSTRCATF (command, "%u", (unsigned int) vl->time);
766 else
767 SSTRCAT (command, "N");
769 for (i = 0; i < vl->values_len; i++)
770 {
771 if (vl->values_types[i] == LCC_TYPE_COUNTER)
772 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
773 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
774 {
775 if (isnan (vl->values[i].gauge))
776 SSTRCPY (command, ":U");
777 else
778 SSTRCATF (command, ":%g", vl->values[i].gauge);
779 }
780 } /* for (i = 0; i < vl->values_len; i++) */
782 status = lcc_sendreceive (c, command, &res);
783 if (status != 0)
784 return (status);
786 if (res.status != 0)
787 {
788 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
789 lcc_response_free (&res);
790 return (-1);
791 }
793 lcc_response_free (&res);
794 return (0);
795 } /* }}} int lcc_putval */
797 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
798 lcc_identifier_t *ident, int timeout)
799 {
800 char command[1024] = "";
801 lcc_response_t res;
802 int status;
804 if (c == NULL)
805 {
806 lcc_set_errno (c, EINVAL);
807 return (-1);
808 }
810 SSTRCPY (command, "FLUSH");
812 if (timeout > 0)
813 SSTRCATF (command, " timeout=%i", timeout);
815 if (plugin != NULL)
816 {
817 char buffer[2 * LCC_NAME_LEN];
818 SSTRCATF (command, " plugin=%s",
819 lcc_strescape (buffer, plugin, sizeof (buffer)));
820 }
822 if (ident != NULL)
823 {
824 char ident_str[6 * LCC_NAME_LEN];
825 char ident_esc[12 * LCC_NAME_LEN];
827 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
828 if (status != 0)
829 return (status);
831 SSTRCATF (command, " identifier=%s",
832 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
833 }
835 status = lcc_sendreceive (c, command, &res);
836 if (status != 0)
837 return (status);
839 if (res.status != 0)
840 {
841 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
842 lcc_response_free (&res);
843 return (-1);
844 }
846 lcc_response_free (&res);
847 return (0);
848 } /* }}} int lcc_flush */
850 /* TODO: Implement lcc_putnotif */
852 int lcc_listval (lcc_connection_t *c, /* {{{ */
853 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
854 {
855 lcc_response_t res;
856 size_t i;
857 int status;
859 lcc_identifier_t *ident;
860 size_t ident_num;
862 if (c == NULL)
863 return (-1);
865 if ((ret_ident == NULL) || (ret_ident_num == NULL))
866 {
867 lcc_set_errno (c, EINVAL);
868 return (-1);
869 }
871 status = lcc_sendreceive (c, "LISTVAL", &res);
872 if (status != 0)
873 return (status);
875 if (res.status != 0)
876 {
877 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
878 lcc_response_free (&res);
879 return (-1);
880 }
882 ident_num = res.lines_num;
883 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
884 if (ident == NULL)
885 {
886 lcc_response_free (&res);
887 lcc_set_errno (c, ENOMEM);
888 return (-1);
889 }
891 for (i = 0; i < res.lines_num; i++)
892 {
893 char *time_str;
894 char *ident_str;
896 /* First field is the time. */
897 time_str = res.lines[i];
899 /* Set `ident_str' to the beginning of the second field. */
900 ident_str = time_str;
901 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
902 ident_str++;
903 while ((*ident_str == ' ') || (*ident_str == '\t'))
904 {
905 *ident_str = 0;
906 ident_str++;
907 }
909 if (*ident_str == 0)
910 {
911 lcc_set_errno (c, EILSEQ);
912 status = -1;
913 break;
914 }
916 status = lcc_string_to_identifier (c, ident + i, ident_str);
917 if (status != 0)
918 break;
919 }
921 lcc_response_free (&res);
923 if (status != 0)
924 {
925 free (ident);
926 return (-1);
927 }
929 *ret_ident = ident;
930 *ret_ident_num = ident_num;
932 return (0);
933 } /* }}} int lcc_listval */
935 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
936 {
937 if (c == NULL)
938 return ("Invalid object");
939 return (c->errbuf);
940 } /* }}} const char *lcc_strerror */
942 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
943 char *string, size_t string_size, const lcc_identifier_t *ident)
944 {
945 if ((string == NULL) || (string_size < 6) || (ident == NULL))
946 {
947 lcc_set_errno (c, EINVAL);
948 return (-1);
949 }
951 if (ident->plugin_instance[0] == 0)
952 {
953 if (ident->type_instance[0] == 0)
954 snprintf (string, string_size, "%s/%s/%s",
955 ident->host,
956 ident->plugin,
957 ident->type);
958 else
959 snprintf (string, string_size, "%s/%s/%s-%s",
960 ident->host,
961 ident->plugin,
962 ident->type,
963 ident->type_instance);
964 }
965 else
966 {
967 if (ident->type_instance[0] == 0)
968 snprintf (string, string_size, "%s/%s-%s/%s",
969 ident->host,
970 ident->plugin,
971 ident->plugin_instance,
972 ident->type);
973 else
974 snprintf (string, string_size, "%s/%s-%s/%s-%s",
975 ident->host,
976 ident->plugin,
977 ident->plugin_instance,
978 ident->type,
979 ident->type_instance);
980 }
982 string[string_size - 1] = 0;
983 return (0);
984 } /* }}} int lcc_identifier_to_string */
986 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
987 lcc_identifier_t *ident, const char *string)
988 {
989 char *string_copy;
990 char *host;
991 char *plugin;
992 char *plugin_instance;
993 char *type;
994 char *type_instance;
996 string_copy = lcc_strdup (string);
997 if (string_copy == NULL)
998 {
999 lcc_set_errno (c, ENOMEM);
1000 return (-1);
1001 }
1003 host = string_copy;
1004 plugin = strchr (host, '/');
1005 if (plugin == NULL)
1006 {
1007 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1008 free (string_copy);
1009 return (-1);
1010 }
1011 *plugin = 0;
1012 plugin++;
1014 type = strchr (plugin, '/');
1015 if (type == NULL)
1016 {
1017 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1018 free (string_copy);
1019 return (-1);
1020 }
1021 *type = 0;
1022 type++;
1024 plugin_instance = strchr (plugin, '-');
1025 if (plugin_instance != NULL)
1026 {
1027 *plugin_instance = 0;
1028 plugin_instance++;
1029 }
1031 type_instance = strchr (type, '-');
1032 if (type_instance != NULL)
1033 {
1034 *type_instance = 0;
1035 type_instance++;
1036 }
1038 memset (ident, 0, sizeof (*ident));
1040 SSTRCPY (ident->host, host);
1041 SSTRCPY (ident->plugin, plugin);
1042 if (plugin_instance != NULL)
1043 SSTRCPY (ident->plugin_instance, plugin_instance);
1044 SSTRCPY (ident->type, type);
1045 if (type_instance != NULL)
1046 SSTRCPY (ident->type_instance, type_instance);
1048 free (string_copy);
1049 return (0);
1050 } /* }}} int lcc_string_to_identifier */
1052 /* vim: set sw=2 sts=2 et fdm=marker : */