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 status = lcc_send (c, command);
348 if (status != 0)
349 return (status);
351 memset (&res, 0, sizeof (res));
352 status = lcc_receive (c, &res);
353 if (status == 0)
354 memcpy (ret_res, &res, sizeof (*ret_res));
356 return (status);
357 } /* }}} int lcc_sendreceive */
359 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
360 {
361 struct sockaddr_un sa;
362 int fd;
363 int status;
365 assert (c != NULL);
366 assert (c->fh == NULL);
367 assert (path != NULL);
369 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
370 * others). */
371 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
372 if (fd < 0)
373 {
374 lcc_set_errno (c, errno);
375 return (-1);
376 }
378 memset (&sa, 0, sizeof (sa));
379 sa.sun_family = AF_UNIX;
380 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
382 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
383 if (status != 0)
384 {
385 lcc_set_errno (c, errno);
386 close (fd);
387 return (-1);
388 }
390 c->fh = fdopen (fd, "r+");
391 if (c->fh == NULL)
392 {
393 lcc_set_errno (c, errno);
394 close (fd);
395 return (-1);
396 }
398 return (0);
399 } /* }}} int lcc_open_unixsocket */
401 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
402 const char *addr_orig)
403 {
404 struct addrinfo ai_hints;
405 struct addrinfo *ai_res;
406 struct addrinfo *ai_ptr;
407 char addr_copy[NI_MAXHOST];
408 char *addr;
409 char *port;
410 int fd;
411 int status;
413 assert (c != NULL);
414 assert (c->fh == NULL);
415 assert (addr_orig != NULL);
417 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
418 addr_copy[sizeof(addr_copy) - 1] = '\0';
419 addr = addr_copy;
421 memset (&ai_hints, 0, sizeof (ai_hints));
422 ai_hints.ai_flags = 0;
423 #ifdef AI_ADDRCONFIG
424 ai_hints.ai_flags |= AI_ADDRCONFIG;
425 #endif
426 ai_hints.ai_family = AF_UNSPEC;
427 ai_hints.ai_socktype = SOCK_STREAM;
429 port = NULL;
430 if (*addr == '[') /* IPv6+port format */
431 {
432 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
433 addr++;
435 port = strchr (addr, ']');
436 if (port == NULL)
437 {
438 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
439 return (-1);
440 }
441 *port = 0;
442 port++;
444 if (*port == ':')
445 port++;
446 else if (*port == 0)
447 port = NULL;
448 else
449 {
450 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
451 return (-1);
452 }
453 } /* if (*addr = ']') */
454 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
455 {
456 port = strrchr (addr, ':');
457 if (port != NULL)
458 {
459 *port = 0;
460 port++;
461 }
462 }
464 ai_res = NULL;
465 status = getaddrinfo (addr,
466 port == NULL ? LCC_DEFAULT_PORT : port,
467 &ai_hints, &ai_res);
468 if (status != 0)
469 {
470 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
471 return (-1);
472 }
474 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
475 {
476 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
477 if (fd < 0)
478 {
479 status = errno;
480 fd = -1;
481 continue;
482 }
484 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
485 if (status != 0)
486 {
487 status = errno;
488 close (fd);
489 fd = -1;
490 continue;
491 }
493 c->fh = fdopen (fd, "r+");
494 if (c->fh == NULL)
495 {
496 status = errno;
497 close (fd);
498 fd = -1;
499 continue;
500 }
502 assert (status == 0);
503 break;
504 } /* for (ai_ptr) */
506 if (status != 0)
507 {
508 lcc_set_errno (c, status);
509 return (-1);
510 }
512 return (0);
513 } /* }}} int lcc_open_netsocket */
515 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
516 {
517 int status = 0;
519 if (addr == NULL)
520 return (-1);
522 assert (c != NULL);
523 assert (c->fh == NULL);
524 assert (addr != NULL);
526 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
527 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
528 else if (addr[0] == '/')
529 status = lcc_open_unixsocket (c, addr);
530 else
531 status = lcc_open_netsocket (c, addr);
533 return (status);
534 } /* }}} int lcc_open_socket */
536 /*
537 * Public functions
538 */
539 unsigned int lcc_version (void) /* {{{ */
540 {
541 return (LCC_VERSION);
542 } /* }}} unsigned int lcc_version */
544 const char *lcc_version_string (void) /* {{{ */
545 {
546 return (LCC_VERSION_STRING);
547 } /* }}} const char *lcc_version_string */
549 const char *lcc_version_extra (void) /* {{{ */
550 {
551 return (LCC_VERSION_EXTRA);
552 } /* }}} const char *lcc_version_extra */
554 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
555 {
556 lcc_connection_t *c;
558 if (address == NULL)
559 return (-1);
561 if (ret_con == NULL)
562 return (-1);
564 c = (lcc_connection_t *) malloc (sizeof (*c));
565 if (c == NULL)
566 return (-1);
567 memset (c, 0, sizeof (*c));
569 *ret_con = c;
570 return (lcc_open_socket (c, address));
571 } /* }}} int lcc_connect */
573 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
574 {
575 if (c == NULL)
576 return (-1);
578 if (c->fh != NULL)
579 {
580 fclose (c->fh);
581 c->fh = NULL;
582 }
584 free (c);
585 return (0);
586 } /* }}} int lcc_disconnect */
588 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
589 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
590 {
591 char ident_str[6 * LCC_NAME_LEN];
592 char ident_esc[12 * LCC_NAME_LEN];
593 char command[14 * LCC_NAME_LEN];
595 lcc_response_t res;
596 size_t values_num;
597 gauge_t *values = NULL;
598 char **values_names = NULL;
600 size_t i;
601 int status;
603 if (c == NULL)
604 return (-1);
606 if (ident == NULL)
607 {
608 lcc_set_errno (c, EINVAL);
609 return (-1);
610 }
612 /* Build a commend with an escaped version of the identifier string. */
613 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
614 if (status != 0)
615 return (status);
617 snprintf (command, sizeof (command), "GETVAL %s",
618 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
619 command[sizeof (command) - 1] = 0;
621 /* Send talk to the daemon.. */
622 status = lcc_sendreceive (c, command, &res);
623 if (status != 0)
624 return (status);
626 if (res.status != 0)
627 {
628 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
629 lcc_response_free (&res);
630 return (-1);
631 }
633 values_num = res.lines_num;
635 #define BAIL_OUT(e) do { \
636 lcc_set_errno (c, (e)); \
637 free (values); \
638 if (values_names != NULL) { \
639 for (i = 0; i < values_num; i++) { \
640 free (values_names[i]); \
641 } \
642 } \
643 free (values_names); \
644 lcc_response_free (&res); \
645 return (-1); \
646 } while (0)
648 /* If neither the values nor the names are requested, return here.. */
649 if ((ret_values == NULL) && (ret_values_names == NULL))
650 {
651 if (ret_values_num != NULL)
652 *ret_values_num = values_num;
653 lcc_response_free (&res);
654 return (0);
655 }
657 /* Allocate space for the values */
658 if (ret_values != NULL)
659 {
660 values = (gauge_t *) malloc (values_num * sizeof (*values));
661 if (values == NULL)
662 BAIL_OUT (ENOMEM);
663 }
665 if (ret_values_names != NULL)
666 {
667 values_names = (char **) calloc (values_num, sizeof (*values_names));
668 if (values_names == NULL)
669 BAIL_OUT (ENOMEM);
670 }
672 for (i = 0; i < res.lines_num; i++)
673 {
674 char *key;
675 char *value;
676 char *endptr;
678 key = res.lines[i];
679 value = strchr (key, '=');
680 if (value == NULL)
681 BAIL_OUT (EILSEQ);
683 *value = 0;
684 value++;
686 if (values != NULL)
687 {
688 endptr = NULL;
689 errno = 0;
690 values[i] = strtod (value, &endptr);
692 if ((endptr == value) || (errno != 0))
693 BAIL_OUT (errno);
694 }
696 if (values_names != NULL)
697 {
698 values_names[i] = lcc_strdup (key);
699 if (values_names[i] == NULL)
700 BAIL_OUT (ENOMEM);
701 }
702 } /* for (i = 0; i < res.lines_num; i++) */
704 if (ret_values_num != NULL)
705 *ret_values_num = values_num;
706 if (ret_values != NULL)
707 *ret_values = values;
708 if (ret_values_names != NULL)
709 *ret_values_names = values_names;
711 return (0);
712 } /* }}} int lcc_getval */
714 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
715 {
716 char ident_str[6 * LCC_NAME_LEN];
717 char ident_esc[12 * LCC_NAME_LEN];
718 char command[1024] = "";
719 lcc_response_t res;
720 int status;
721 size_t i;
723 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
724 || (vl->values == NULL) || (vl->values_types == NULL))
725 {
726 lcc_set_errno (c, EINVAL);
727 return (-1);
728 }
730 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
731 &vl->identifier);
732 if (status != 0)
733 return (status);
735 SSTRCATF (command, "PUTVAL %s",
736 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
738 if (vl->interval > 0)
739 SSTRCATF (command, " interval=%i", vl->interval);
741 if (vl->time > 0)
742 SSTRCATF (command, "%u", (unsigned int) vl->time);
743 else
744 SSTRCAT (command, "N");
746 for (i = 0; i < vl->values_len; i++)
747 {
748 if (vl->values_types[i] == LCC_TYPE_COUNTER)
749 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
750 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
751 {
752 if (isnan (vl->values[i].gauge))
753 SSTRCPY (command, ":U");
754 else
755 SSTRCATF (command, ":%g", vl->values[i].gauge);
756 }
757 } /* for (i = 0; i < vl->values_len; i++) */
759 status = lcc_sendreceive (c, command, &res);
760 if (status != 0)
761 return (status);
763 if (res.status != 0)
764 {
765 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
766 lcc_response_free (&res);
767 return (-1);
768 }
770 lcc_response_free (&res);
771 return (0);
772 } /* }}} int lcc_putval */
774 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
775 lcc_identifier_t *ident, int timeout)
776 {
777 char command[1024] = "";
778 lcc_response_t res;
779 int status;
781 if (c == NULL)
782 {
783 lcc_set_errno (c, EINVAL);
784 return (-1);
785 }
787 SSTRCPY (command, "FLUSH");
789 if (timeout > 0)
790 SSTRCATF (command, " timeout=%i", timeout);
792 if (plugin != NULL)
793 {
794 char buffer[2 * LCC_NAME_LEN];
795 SSTRCATF (command, " plugin=%s",
796 lcc_strescape (buffer, plugin, sizeof (buffer)));
797 }
799 if (ident != NULL)
800 {
801 char ident_str[6 * LCC_NAME_LEN];
802 char ident_esc[12 * LCC_NAME_LEN];
804 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
805 if (status != 0)
806 return (status);
808 SSTRCATF (command, " identifier=%s",
809 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
810 }
812 status = lcc_sendreceive (c, command, &res);
813 if (status != 0)
814 return (status);
816 if (res.status != 0)
817 {
818 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
819 lcc_response_free (&res);
820 return (-1);
821 }
823 lcc_response_free (&res);
824 return (0);
825 } /* }}} int lcc_flush */
827 /* TODO: Implement lcc_putnotif */
829 int lcc_listval (lcc_connection_t *c, /* {{{ */
830 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
831 {
832 lcc_response_t res;
833 size_t i;
834 int status;
836 lcc_identifier_t *ident;
837 size_t ident_num;
839 if (c == NULL)
840 return (-1);
842 if ((ret_ident == NULL) || (ret_ident_num == NULL))
843 {
844 lcc_set_errno (c, EINVAL);
845 return (-1);
846 }
848 status = lcc_sendreceive (c, "LISTVAL", &res);
849 if (status != 0)
850 return (status);
852 if (res.status != 0)
853 {
854 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
855 lcc_response_free (&res);
856 return (-1);
857 }
859 ident_num = res.lines_num;
860 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
861 if (ident == NULL)
862 {
863 lcc_response_free (&res);
864 lcc_set_errno (c, ENOMEM);
865 return (-1);
866 }
868 for (i = 0; i < res.lines_num; i++)
869 {
870 char *time_str;
871 char *ident_str;
873 /* First field is the time. */
874 time_str = res.lines[i];
876 /* Set `ident_str' to the beginning of the second field. */
877 ident_str = time_str;
878 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
879 ident_str++;
880 while ((*ident_str == ' ') || (*ident_str == '\t'))
881 {
882 *ident_str = 0;
883 ident_str++;
884 }
886 if (*ident_str == 0)
887 {
888 lcc_set_errno (c, EILSEQ);
889 status = -1;
890 break;
891 }
893 status = lcc_string_to_identifier (c, ident + i, ident_str);
894 if (status != 0)
895 break;
896 }
898 lcc_response_free (&res);
900 if (status != 0)
901 {
902 free (ident);
903 return (-1);
904 }
906 *ret_ident = ident;
907 *ret_ident_num = ident_num;
909 return (0);
910 } /* }}} int lcc_listval */
912 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
913 {
914 if (c == NULL)
915 return ("Invalid object");
916 return (c->errbuf);
917 } /* }}} const char *lcc_strerror */
919 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
920 char *string, size_t string_size, const lcc_identifier_t *ident)
921 {
922 if ((string == NULL) || (string_size < 6) || (ident == NULL))
923 {
924 lcc_set_errno (c, EINVAL);
925 return (-1);
926 }
928 if (ident->plugin_instance[0] == 0)
929 {
930 if (ident->type_instance[0] == 0)
931 snprintf (string, string_size, "%s/%s/%s",
932 ident->host,
933 ident->plugin,
934 ident->type);
935 else
936 snprintf (string, string_size, "%s/%s/%s-%s",
937 ident->host,
938 ident->plugin,
939 ident->type,
940 ident->type_instance);
941 }
942 else
943 {
944 if (ident->type_instance[0] == 0)
945 snprintf (string, string_size, "%s/%s-%s/%s",
946 ident->host,
947 ident->plugin,
948 ident->plugin_instance,
949 ident->type);
950 else
951 snprintf (string, string_size, "%s/%s-%s/%s-%s",
952 ident->host,
953 ident->plugin,
954 ident->plugin_instance,
955 ident->type,
956 ident->type_instance);
957 }
959 string[string_size - 1] = 0;
960 return (0);
961 } /* }}} int lcc_identifier_to_string */
963 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
964 lcc_identifier_t *ident, const char *string)
965 {
966 char *string_copy;
967 char *host;
968 char *plugin;
969 char *plugin_instance;
970 char *type;
971 char *type_instance;
973 string_copy = lcc_strdup (string);
974 if (string_copy == NULL)
975 {
976 lcc_set_errno (c, ENOMEM);
977 return (-1);
978 }
980 host = string_copy;
981 plugin = strchr (host, '/');
982 if (plugin == NULL)
983 {
984 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
985 free (string_copy);
986 return (-1);
987 }
988 *plugin = 0;
989 plugin++;
991 type = strchr (plugin, '/');
992 if (type == NULL)
993 {
994 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
995 free (string_copy);
996 return (-1);
997 }
998 *type = 0;
999 type++;
1001 plugin_instance = strchr (plugin, '-');
1002 if (plugin_instance != NULL)
1003 {
1004 *plugin_instance = 0;
1005 plugin_instance++;
1006 }
1008 type_instance = strchr (type, '-');
1009 if (type_instance != NULL)
1010 {
1011 *type_instance = 0;
1012 type_instance++;
1013 }
1015 memset (ident, 0, sizeof (*ident));
1017 SSTRCPY (ident->host, host);
1018 SSTRCPY (ident->plugin, plugin);
1019 if (plugin_instance != NULL)
1020 SSTRCPY (ident->plugin_instance, plugin_instance);
1021 SSTRCPY (ident->type, type);
1022 if (type_instance != NULL)
1023 SSTRCPY (ident->type_instance, type_instance);
1025 free (string_copy);
1026 return (0);
1027 } /* }}} int lcc_string_to_identifier */
1029 /* vim: set sw=2 sts=2 et fdm=marker : */