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 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
78 * termination. They work for static buffers only, because they use `sizeof'.
79 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
80 * is very useful to add formatted stuff to the end of a buffer. */
81 #define SSTRCPY(d,s) do { \
82 strncpy ((d), (s), sizeof (d)); \
83 (d)[sizeof (d) - 1] = 0; \
84 } while (0)
86 #define SSTRCAT(d,s) do { \
87 size_t _l = strlen (d); \
88 strncpy ((d) + _l, (s), sizeof (d) - _l); \
89 (d)[sizeof (d) - 1] = 0; \
90 } while (0)
92 #define SSTRCATF(d, ...) do { \
93 char _b[sizeof (d)]; \
94 snprintf (_b, sizeof (_b), __VA_ARGS__); \
95 _b[sizeof (_b) - 1] = 0; \
96 SSTRCAT ((d), _b); \
97 } while (0)
100 #define LCC_SET_ERRSTR(c, ...) do { \
101 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
102 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
103 } while (0)
105 #if COLLECT_DEBUG
106 # define LCC_DEBUG(...) printf (__VA_ARGS__)
107 #else
108 # define LCC_DEBUG(...) /**/
109 #endif
111 /*
112 * Types
113 */
114 struct lcc_connection_s
115 {
116 FILE *fh;
117 char errbuf[1024];
118 };
120 struct lcc_response_s
121 {
122 int status;
123 char message[1024];
124 char **lines;
125 size_t lines_num;
126 };
127 typedef struct lcc_response_s lcc_response_t;
129 /*
130 * Private functions
131 */
132 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
133 {
134 if (c == NULL)
135 return (-1);
137 strerror_r (err, c->errbuf, sizeof (c->errbuf));
138 c->errbuf[sizeof (c->errbuf) - 1] = 0;
140 return (0);
141 } /* }}} int lcc_set_errno */
143 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
144 * here. */
145 __attribute__((malloc, nonnull (1)))
146 static char *lcc_strdup (const char *str) /* {{{ */
147 {
148 size_t strsize;
149 char *ret;
151 strsize = strlen (str) + 1;
152 ret = (char *) malloc (strsize);
153 if (ret != NULL)
154 memcpy (ret, str, strsize);
155 return (ret);
156 } /* }}} char *lcc_strdup */
158 __attribute__((nonnull (1, 2)))
159 static char *lcc_strescape (char *dest, const char *src, size_t dest_size) /* {{{ */
160 {
161 size_t dest_pos;
162 size_t src_pos;
164 dest_pos = 0;
165 src_pos = 0;
167 assert (dest_size >= 3);
169 dest[dest_pos] = '"';
170 dest_pos++;
172 while (42)
173 {
174 if ((dest_pos == (dest_size - 2))
175 || (src[src_pos] == 0))
176 break;
178 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
179 {
180 /* Check if there is enough space for both characters.. */
181 if (dest_pos == (dest_size - 3))
182 break;
184 dest[dest_pos] = '\\';
185 dest_pos++;
186 }
188 dest[dest_pos] = src[src_pos];
189 dest_pos++;
190 src_pos++;
191 }
193 assert (dest_pos <= (dest_size - 2));
195 dest[dest_pos] = '"';
196 dest_pos++;
198 dest[dest_pos] = 0;
199 dest_pos++;
200 src_pos++;
202 return (dest);
203 } /* }}} char *lcc_strescape */
205 /* lcc_chomp: Removes all control-characters at the end of a string. */
206 static void lcc_chomp (char *str) /* {{{ */
207 {
208 size_t str_len;
210 str_len = strlen (str);
211 while (str_len > 0)
212 {
213 if (str[str_len - 1] >= 32)
214 break;
215 str[str_len - 1] = 0;
216 str_len--;
217 }
218 } /* }}} void lcc_chomp */
220 static void lcc_response_free (lcc_response_t *res) /* {{{ */
221 {
222 size_t i;
224 if (res == NULL)
225 return;
227 for (i = 0; i < res->lines_num; i++)
228 free (res->lines[i]);
229 free (res->lines);
230 res->lines = NULL;
231 } /* }}} void lcc_response_free */
233 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
234 {
235 int status;
237 LCC_DEBUG ("send: --> %s\n", command);
239 status = fprintf (c->fh, "%s\r\n", command);
240 if (status < 0)
241 {
242 lcc_set_errno (c, errno);
243 return (-1);
244 }
246 return (0);
247 } /* }}} int lcc_send */
249 static int lcc_receive (lcc_connection_t *c, /* {{{ */
250 lcc_response_t *ret_res)
251 {
252 lcc_response_t res;
253 char *ptr;
254 char buffer[4096];
255 size_t i;
257 memset (&res, 0, sizeof (res));
259 /* Read the first line, containing the status and a message */
260 ptr = fgets (buffer, sizeof (buffer), c->fh);
261 if (ptr == NULL)
262 {
263 lcc_set_errno (c, errno);
264 return (-1);
265 }
266 lcc_chomp (buffer);
267 LCC_DEBUG ("receive: <-- %s\n", buffer);
269 /* Convert the leading status to an integer and make `ptr' to point to the
270 * beginning of the message. */
271 ptr = NULL;
272 errno = 0;
273 res.status = strtol (buffer, &ptr, 0);
274 if ((errno != 0) || (ptr == &buffer[0]))
275 {
276 lcc_set_errno (c, errno);
277 return (-1);
278 }
280 /* Skip white spaces after the status number */
281 while ((*ptr == ' ') || (*ptr == '\t'))
282 ptr++;
284 /* Now copy the message. */
285 strncpy (res.message, ptr, sizeof (res.message));
286 res.message[sizeof (res.message) - 1] = 0;
288 /* Error or no lines follow: We're done. */
289 if (res.status <= 0)
290 {
291 memcpy (ret_res, &res, sizeof (res));
292 return (0);
293 }
295 /* Allocate space for the char-pointers */
296 res.lines_num = (size_t) res.status;
297 res.status = 0;
298 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
299 if (res.lines == NULL)
300 {
301 lcc_set_errno (c, ENOMEM);
302 return (-1);
303 }
305 /* Now receive all the lines */
306 for (i = 0; i < res.lines_num; i++)
307 {
308 ptr = fgets (buffer, sizeof (buffer), c->fh);
309 if (ptr == NULL)
310 {
311 lcc_set_errno (c, errno);
312 break;
313 }
314 lcc_chomp (buffer);
315 LCC_DEBUG ("receive: <-- %s\n", buffer);
317 res.lines[i] = lcc_strdup (buffer);
318 if (res.lines[i] == NULL)
319 {
320 lcc_set_errno (c, ENOMEM);
321 break;
322 }
323 }
325 /* Check if the for-loop exited with an error. */
326 if (i < res.lines_num)
327 {
328 while (i > 0)
329 {
330 i--;
331 free (res.lines[i]);
332 }
333 free (res.lines);
334 return (-1);
335 }
337 memcpy (ret_res, &res, sizeof (res));
338 return (0);
339 } /* }}} int lcc_receive */
341 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
342 const char *command, lcc_response_t *ret_res)
343 {
344 lcc_response_t res;
345 int status;
347 if (c->fh == NULL)
348 {
349 lcc_set_errno (c, EBADF);
350 return (-1);
351 }
353 status = lcc_send (c, command);
354 if (status != 0)
355 return (status);
357 memset (&res, 0, sizeof (res));
358 status = lcc_receive (c, &res);
359 if (status == 0)
360 memcpy (ret_res, &res, sizeof (*ret_res));
362 return (status);
363 } /* }}} int lcc_sendreceive */
365 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
366 {
367 struct sockaddr_un sa;
368 int fd;
369 int status;
371 assert (c != NULL);
372 assert (c->fh == NULL);
373 assert (path != NULL);
375 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
376 * others). */
377 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
378 if (fd < 0)
379 {
380 lcc_set_errno (c, errno);
381 return (-1);
382 }
384 memset (&sa, 0, sizeof (sa));
385 sa.sun_family = AF_UNIX;
386 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
388 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
389 if (status != 0)
390 {
391 lcc_set_errno (c, errno);
392 close (fd);
393 return (-1);
394 }
396 c->fh = fdopen (fd, "r+");
397 if (c->fh == NULL)
398 {
399 lcc_set_errno (c, errno);
400 close (fd);
401 return (-1);
402 }
404 return (0);
405 } /* }}} int lcc_open_unixsocket */
407 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
408 const char *addr_orig)
409 {
410 struct addrinfo ai_hints;
411 struct addrinfo *ai_res;
412 struct addrinfo *ai_ptr;
413 char addr_copy[NI_MAXHOST];
414 char *addr;
415 char *port;
416 int fd;
417 int status;
419 assert (c != NULL);
420 assert (c->fh == NULL);
421 assert (addr_orig != NULL);
423 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
424 addr_copy[sizeof(addr_copy) - 1] = '\0';
425 addr = addr_copy;
427 memset (&ai_hints, 0, sizeof (ai_hints));
428 ai_hints.ai_flags = 0;
429 #ifdef AI_ADDRCONFIG
430 ai_hints.ai_flags |= AI_ADDRCONFIG;
431 #endif
432 ai_hints.ai_family = AF_UNSPEC;
433 ai_hints.ai_socktype = SOCK_STREAM;
435 port = NULL;
436 if (*addr == '[') /* IPv6+port format */
437 {
438 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
439 addr++;
441 port = strchr (addr, ']');
442 if (port == NULL)
443 {
444 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
445 return (-1);
446 }
447 *port = 0;
448 port++;
450 if (*port == ':')
451 port++;
452 else if (*port == 0)
453 port = NULL;
454 else
455 {
456 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
457 return (-1);
458 }
459 } /* if (*addr = ']') */
460 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
461 {
462 port = strrchr (addr, ':');
463 if (port != NULL)
464 {
465 *port = 0;
466 port++;
467 }
468 }
470 ai_res = NULL;
471 status = getaddrinfo (addr,
472 port == NULL ? LCC_DEFAULT_PORT : port,
473 &ai_hints, &ai_res);
474 if (status != 0)
475 {
476 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
477 return (-1);
478 }
480 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
481 {
482 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
483 if (fd < 0)
484 {
485 status = errno;
486 fd = -1;
487 continue;
488 }
490 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
491 if (status != 0)
492 {
493 status = errno;
494 close (fd);
495 fd = -1;
496 continue;
497 }
499 c->fh = fdopen (fd, "r+");
500 if (c->fh == NULL)
501 {
502 status = errno;
503 close (fd);
504 fd = -1;
505 continue;
506 }
508 assert (status == 0);
509 break;
510 } /* for (ai_ptr) */
512 if (status != 0)
513 {
514 lcc_set_errno (c, status);
515 return (-1);
516 }
518 return (0);
519 } /* }}} int lcc_open_netsocket */
521 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
522 {
523 int status = 0;
525 if (addr == NULL)
526 return (-1);
528 assert (c != NULL);
529 assert (c->fh == NULL);
530 assert (addr != NULL);
532 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
533 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
534 else if (addr[0] == '/')
535 status = lcc_open_unixsocket (c, addr);
536 else
537 status = lcc_open_netsocket (c, addr);
539 return (status);
540 } /* }}} int lcc_open_socket */
542 /*
543 * Public functions
544 */
545 unsigned int lcc_version (void) /* {{{ */
546 {
547 return (LCC_VERSION);
548 } /* }}} unsigned int lcc_version */
550 const char *lcc_version_string (void) /* {{{ */
551 {
552 return (LCC_VERSION_STRING);
553 } /* }}} const char *lcc_version_string */
555 const char *lcc_version_extra (void) /* {{{ */
556 {
557 return (LCC_VERSION_EXTRA);
558 } /* }}} const char *lcc_version_extra */
560 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
561 {
562 lcc_connection_t *c;
563 int status;
565 if (address == NULL)
566 return (-1);
568 if (ret_con == NULL)
569 return (-1);
571 c = (lcc_connection_t *) malloc (sizeof (*c));
572 if (c == NULL)
573 return (-1);
574 memset (c, 0, sizeof (*c));
576 status = lcc_open_socket (c, address);
577 if (status != 0)
578 {
579 lcc_disconnect (c);
580 return (status);
581 }
583 *ret_con = c;
584 return (0);
585 } /* }}} int lcc_connect */
587 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
588 {
589 if (c == NULL)
590 return (-1);
592 if (c->fh != NULL)
593 {
594 fclose (c->fh);
595 c->fh = NULL;
596 }
598 free (c);
599 return (0);
600 } /* }}} int lcc_disconnect */
602 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
603 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
604 {
605 char ident_str[6 * LCC_NAME_LEN];
606 char ident_esc[12 * LCC_NAME_LEN];
607 char command[14 * LCC_NAME_LEN];
609 lcc_response_t res;
610 size_t values_num;
611 gauge_t *values = NULL;
612 char **values_names = NULL;
614 size_t i;
615 int status;
617 if (c == NULL)
618 return (-1);
620 if (ident == NULL)
621 {
622 lcc_set_errno (c, EINVAL);
623 return (-1);
624 }
626 /* Build a commend with an escaped version of the identifier string. */
627 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
628 if (status != 0)
629 return (status);
631 snprintf (command, sizeof (command), "GETVAL %s",
632 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
633 command[sizeof (command) - 1] = 0;
635 /* Send talk to the daemon.. */
636 status = lcc_sendreceive (c, command, &res);
637 if (status != 0)
638 return (status);
640 if (res.status != 0)
641 {
642 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
643 lcc_response_free (&res);
644 return (-1);
645 }
647 values_num = res.lines_num;
649 #define BAIL_OUT(e) do { \
650 lcc_set_errno (c, (e)); \
651 free (values); \
652 if (values_names != NULL) { \
653 for (i = 0; i < values_num; i++) { \
654 free (values_names[i]); \
655 } \
656 } \
657 free (values_names); \
658 lcc_response_free (&res); \
659 return (-1); \
660 } while (0)
662 /* If neither the values nor the names are requested, return here.. */
663 if ((ret_values == NULL) && (ret_values_names == NULL))
664 {
665 if (ret_values_num != NULL)
666 *ret_values_num = values_num;
667 lcc_response_free (&res);
668 return (0);
669 }
671 /* Allocate space for the values */
672 if (ret_values != NULL)
673 {
674 values = (gauge_t *) malloc (values_num * sizeof (*values));
675 if (values == NULL)
676 BAIL_OUT (ENOMEM);
677 }
679 if (ret_values_names != NULL)
680 {
681 values_names = (char **) calloc (values_num, sizeof (*values_names));
682 if (values_names == NULL)
683 BAIL_OUT (ENOMEM);
684 }
686 for (i = 0; i < res.lines_num; i++)
687 {
688 char *key;
689 char *value;
690 char *endptr;
692 key = res.lines[i];
693 value = strchr (key, '=');
694 if (value == NULL)
695 BAIL_OUT (EILSEQ);
697 *value = 0;
698 value++;
700 if (values != NULL)
701 {
702 endptr = NULL;
703 errno = 0;
704 values[i] = strtod (value, &endptr);
706 if ((endptr == value) || (errno != 0))
707 BAIL_OUT (errno);
708 }
710 if (values_names != NULL)
711 {
712 values_names[i] = lcc_strdup (key);
713 if (values_names[i] == NULL)
714 BAIL_OUT (ENOMEM);
715 }
716 } /* for (i = 0; i < res.lines_num; i++) */
718 if (ret_values_num != NULL)
719 *ret_values_num = values_num;
720 if (ret_values != NULL)
721 *ret_values = values;
722 if (ret_values_names != NULL)
723 *ret_values_names = values_names;
725 return (0);
726 } /* }}} int lcc_getval */
728 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
729 {
730 char ident_str[6 * LCC_NAME_LEN];
731 char ident_esc[12 * LCC_NAME_LEN];
732 char command[1024] = "";
733 lcc_response_t res;
734 int status;
735 size_t i;
737 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
738 || (vl->values == NULL) || (vl->values_types == NULL))
739 {
740 lcc_set_errno (c, EINVAL);
741 return (-1);
742 }
744 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
745 &vl->identifier);
746 if (status != 0)
747 return (status);
749 SSTRCATF (command, "PUTVAL %s",
750 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
752 if (vl->interval > 0)
753 SSTRCATF (command, " interval=%i", vl->interval);
755 if (vl->time > 0)
756 SSTRCATF (command, "%u", (unsigned int) vl->time);
757 else
758 SSTRCAT (command, "N");
760 for (i = 0; i < vl->values_len; i++)
761 {
762 if (vl->values_types[i] == LCC_TYPE_COUNTER)
763 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
764 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
765 {
766 if (isnan (vl->values[i].gauge))
767 SSTRCPY (command, ":U");
768 else
769 SSTRCATF (command, ":%g", vl->values[i].gauge);
770 }
771 } /* for (i = 0; i < vl->values_len; i++) */
773 status = lcc_sendreceive (c, command, &res);
774 if (status != 0)
775 return (status);
777 if (res.status != 0)
778 {
779 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
780 lcc_response_free (&res);
781 return (-1);
782 }
784 lcc_response_free (&res);
785 return (0);
786 } /* }}} int lcc_putval */
788 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
789 lcc_identifier_t *ident, int timeout)
790 {
791 char command[1024] = "";
792 lcc_response_t res;
793 int status;
795 if (c == NULL)
796 {
797 lcc_set_errno (c, EINVAL);
798 return (-1);
799 }
801 SSTRCPY (command, "FLUSH");
803 if (timeout > 0)
804 SSTRCATF (command, " timeout=%i", timeout);
806 if (plugin != NULL)
807 {
808 char buffer[2 * LCC_NAME_LEN];
809 SSTRCATF (command, " plugin=%s",
810 lcc_strescape (buffer, plugin, sizeof (buffer)));
811 }
813 if (ident != NULL)
814 {
815 char ident_str[6 * LCC_NAME_LEN];
816 char ident_esc[12 * LCC_NAME_LEN];
818 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
819 if (status != 0)
820 return (status);
822 SSTRCATF (command, " identifier=%s",
823 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
824 }
826 status = lcc_sendreceive (c, command, &res);
827 if (status != 0)
828 return (status);
830 if (res.status != 0)
831 {
832 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
833 lcc_response_free (&res);
834 return (-1);
835 }
837 lcc_response_free (&res);
838 return (0);
839 } /* }}} int lcc_flush */
841 /* TODO: Implement lcc_putnotif */
843 int lcc_listval (lcc_connection_t *c, /* {{{ */
844 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
845 {
846 lcc_response_t res;
847 size_t i;
848 int status;
850 lcc_identifier_t *ident;
851 size_t ident_num;
853 if (c == NULL)
854 return (-1);
856 if ((ret_ident == NULL) || (ret_ident_num == NULL))
857 {
858 lcc_set_errno (c, EINVAL);
859 return (-1);
860 }
862 status = lcc_sendreceive (c, "LISTVAL", &res);
863 if (status != 0)
864 return (status);
866 if (res.status != 0)
867 {
868 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
869 lcc_response_free (&res);
870 return (-1);
871 }
873 ident_num = res.lines_num;
874 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
875 if (ident == NULL)
876 {
877 lcc_response_free (&res);
878 lcc_set_errno (c, ENOMEM);
879 return (-1);
880 }
882 for (i = 0; i < res.lines_num; i++)
883 {
884 char *time_str;
885 char *ident_str;
887 /* First field is the time. */
888 time_str = res.lines[i];
890 /* Set `ident_str' to the beginning of the second field. */
891 ident_str = time_str;
892 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
893 ident_str++;
894 while ((*ident_str == ' ') || (*ident_str == '\t'))
895 {
896 *ident_str = 0;
897 ident_str++;
898 }
900 if (*ident_str == 0)
901 {
902 lcc_set_errno (c, EILSEQ);
903 status = -1;
904 break;
905 }
907 status = lcc_string_to_identifier (c, ident + i, ident_str);
908 if (status != 0)
909 break;
910 }
912 lcc_response_free (&res);
914 if (status != 0)
915 {
916 free (ident);
917 return (-1);
918 }
920 *ret_ident = ident;
921 *ret_ident_num = ident_num;
923 return (0);
924 } /* }}} int lcc_listval */
926 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
927 {
928 if (c == NULL)
929 return ("Invalid object");
930 return (c->errbuf);
931 } /* }}} const char *lcc_strerror */
933 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
934 char *string, size_t string_size, const lcc_identifier_t *ident)
935 {
936 if ((string == NULL) || (string_size < 6) || (ident == NULL))
937 {
938 lcc_set_errno (c, EINVAL);
939 return (-1);
940 }
942 if (ident->plugin_instance[0] == 0)
943 {
944 if (ident->type_instance[0] == 0)
945 snprintf (string, string_size, "%s/%s/%s",
946 ident->host,
947 ident->plugin,
948 ident->type);
949 else
950 snprintf (string, string_size, "%s/%s/%s-%s",
951 ident->host,
952 ident->plugin,
953 ident->type,
954 ident->type_instance);
955 }
956 else
957 {
958 if (ident->type_instance[0] == 0)
959 snprintf (string, string_size, "%s/%s-%s/%s",
960 ident->host,
961 ident->plugin,
962 ident->plugin_instance,
963 ident->type);
964 else
965 snprintf (string, string_size, "%s/%s-%s/%s-%s",
966 ident->host,
967 ident->plugin,
968 ident->plugin_instance,
969 ident->type,
970 ident->type_instance);
971 }
973 string[string_size - 1] = 0;
974 return (0);
975 } /* }}} int lcc_identifier_to_string */
977 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
978 lcc_identifier_t *ident, const char *string)
979 {
980 char *string_copy;
981 char *host;
982 char *plugin;
983 char *plugin_instance;
984 char *type;
985 char *type_instance;
987 string_copy = lcc_strdup (string);
988 if (string_copy == NULL)
989 {
990 lcc_set_errno (c, ENOMEM);
991 return (-1);
992 }
994 host = string_copy;
995 plugin = strchr (host, '/');
996 if (plugin == NULL)
997 {
998 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
999 free (string_copy);
1000 return (-1);
1001 }
1002 *plugin = 0;
1003 plugin++;
1005 type = strchr (plugin, '/');
1006 if (type == NULL)
1007 {
1008 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1009 free (string_copy);
1010 return (-1);
1011 }
1012 *type = 0;
1013 type++;
1015 plugin_instance = strchr (plugin, '-');
1016 if (plugin_instance != NULL)
1017 {
1018 *plugin_instance = 0;
1019 plugin_instance++;
1020 }
1022 type_instance = strchr (type, '-');
1023 if (type_instance != NULL)
1024 {
1025 *type_instance = 0;
1026 type_instance++;
1027 }
1029 memset (ident, 0, sizeof (*ident));
1031 SSTRCPY (ident->host, host);
1032 SSTRCPY (ident->plugin, plugin);
1033 if (plugin_instance != NULL)
1034 SSTRCPY (ident->plugin_instance, plugin_instance);
1035 SSTRCPY (ident->type, type);
1036 if (type_instance != NULL)
1037 SSTRCPY (ident->type_instance, type_instance);
1039 free (string_copy);
1040 return (0);
1041 } /* }}} int lcc_string_to_identifier */
1043 /* vim: set sw=2 sts=2 et fdm=marker : */