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 #include <stdlib.h>
52 #include <stdio.h>
53 #include <unistd.h>
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <sys/un.h>
57 #include <string.h>
58 #include <assert.h>
59 #include <errno.h>
60 #include <netdb.h>
62 #include "client.h"
64 #define SSTRCPY(d,s) do { \
65 strncpy ((d), (s), sizeof (d)); \
66 (d)[sizeof (d) - 1] = 0; \
67 } while (0)
69 #define SSTRCAT(d,s) do { \
70 strncat ((d), (s), sizeof (d)); \
71 (d)[sizeof (d) - 1] = 0; \
72 } while (0)
74 #define SSTRCATF(d, ...) do { \
75 char _b[sizeof (d)]; \
76 snprintf (_b, sizeof (_b), __VA_ARGS__); \
77 _b[sizeof (_b) - 1] = 0; \
78 SSTRCAT ((d), _b); \
79 } while (0)
82 #define LCC_SET_ERRSTR(c, ...) do { \
83 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
84 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
85 } while (0)
87 #if 1
88 # define LCC_DEBUG(...) printf (__VA_ARGS__)
89 #else
90 # define LCC_DEBUG(...) /**/
91 #endif
93 /*
94 * Types
95 */
96 struct lcc_connection_s
97 {
98 FILE *fh;
99 char errbuf[1024];
100 };
102 struct lcc_response_s
103 {
104 int status;
105 char message[1024];
106 char **lines;
107 size_t lines_num;
108 };
109 typedef struct lcc_response_s lcc_response_t;
111 /*
112 * Private functions
113 */
114 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
115 {
116 if (c == NULL)
117 return (-1);
119 strerror_r (err, c->errbuf, sizeof (c->errbuf));
120 c->errbuf[sizeof (c->errbuf) - 1] = 0;
122 return (0);
123 } /* }}} int lcc_set_errno */
125 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
126 * here. */
127 __attribute__((malloc, nonnull (1)))
128 static char *lcc_strdup (const char *str) /* {{{ */
129 {
130 size_t strsize;
131 char *ret;
133 strsize = strlen (str) + 1;
134 ret = (char *) malloc (strsize);
135 if (ret != NULL)
136 memcpy (ret, str, strsize);
137 return (ret);
138 } /* }}} char *lcc_strdup */
140 __attribute__((nonnull (1, 2)))
141 static char *lcc_strescape (char *dest, char *src, size_t dest_size) /* {{{ */
142 {
143 size_t dest_pos;
144 size_t src_pos;
146 dest_pos = 0;
147 src_pos = 0;
149 assert (dest_size >= 3);
151 dest[dest_pos] = '"';
152 dest_pos++;
154 while (42)
155 {
156 if ((dest_pos == (dest_size - 2))
157 || (src[src_pos] == 0))
158 break;
160 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
161 {
162 /* Check if there is enough space for both characters.. */
163 if (dest_pos == (dest_size - 3))
164 break;
166 dest[dest_pos] = '\\';
167 dest_pos++;
168 }
170 dest[dest_pos] = src[src_pos];
171 dest_pos++;
172 src_pos++;
173 }
175 assert (dest_pos <= (dest_size - 2));
177 dest[dest_pos] = '"';
178 dest_pos++;
180 dest[dest_pos] = 0;
181 dest_pos++;
182 src_pos++;
184 return (dest);
185 } /* }}} char *lcc_strescape */
187 /* lcc_chomp: Removes all control-characters at the end of a string. */
188 static void lcc_chomp (char *str) /* {{{ */
189 {
190 size_t str_len;
192 str_len = strlen (str);
193 while (str_len > 0)
194 {
195 if (str[str_len - 1] >= 32)
196 break;
197 str[str_len - 1] = 0;
198 str_len--;
199 }
200 } /* }}} void lcc_chomp */
202 static void lcc_response_free (lcc_response_t *res) /* {{{ */
203 {
204 size_t i;
206 if (res == NULL)
207 return;
209 for (i = 0; i < res->lines_num; i++)
210 free (res->lines[i]);
211 free (res->lines);
212 res->lines = NULL;
213 } /* }}} void lcc_response_free */
215 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
216 {
217 int status;
219 LCC_DEBUG ("send: --> %s\n", command);
221 status = fprintf (c->fh, "%s\r\n", command);
222 if (status < 0)
223 {
224 lcc_set_errno (c, errno);
225 return (-1);
226 }
228 return (0);
229 } /* }}} int lcc_send */
231 static int lcc_receive (lcc_connection_t *c, /* {{{ */
232 lcc_response_t *ret_res)
233 {
234 lcc_response_t res;
235 char *ptr;
236 char buffer[4096];
237 size_t i;
239 memset (&res, 0, sizeof (res));
241 /* Read the first line, containing the status and a message */
242 ptr = fgets (buffer, sizeof (buffer), c->fh);
243 if (ptr == NULL)
244 {
245 lcc_set_errno (c, errno);
246 return (-1);
247 }
248 lcc_chomp (buffer);
249 LCC_DEBUG ("receive: <-- %s\n", buffer);
251 /* Convert the leading status to an integer and make `ptr' to point to the
252 * beginning of the message. */
253 ptr = NULL;
254 errno = 0;
255 res.status = strtol (buffer, &ptr, 0);
256 if ((errno != 0) || (ptr == &buffer[0]))
257 {
258 lcc_set_errno (c, errno);
259 return (-1);
260 }
262 /* Skip white spaces after the status number */
263 while ((*ptr == ' ') || (*ptr == '\t'))
264 ptr++;
266 /* Now copy the message. */
267 strncpy (res.message, ptr, sizeof (res.message));
268 res.message[sizeof (res.message) - 1] = 0;
270 /* Error or no lines follow: We're done. */
271 if (res.status <= 0)
272 {
273 memcpy (ret_res, &res, sizeof (res));
274 return (0);
275 }
277 /* Allocate space for the char-pointers */
278 res.lines_num = (size_t) res.status;
279 res.status = 0;
280 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
281 if (res.lines == NULL)
282 {
283 lcc_set_errno (c, ENOMEM);
284 return (-1);
285 }
287 /* Now receive all the lines */
288 for (i = 0; i < res.lines_num; i++)
289 {
290 ptr = fgets (buffer, sizeof (buffer), c->fh);
291 if (ptr == NULL)
292 {
293 lcc_set_errno (c, errno);
294 break;
295 }
296 lcc_chomp (buffer);
297 LCC_DEBUG ("receive: <-- %s\n", buffer);
299 res.lines[i] = lcc_strdup (buffer);
300 if (res.lines[i] == NULL)
301 {
302 lcc_set_errno (c, ENOMEM);
303 break;
304 }
305 }
307 /* Check if the for-loop exited with an error. */
308 if (i < res.lines_num)
309 {
310 while (i > 0)
311 {
312 i--;
313 free (res.lines[i]);
314 }
315 free (res.lines);
316 return (-1);
317 }
319 memcpy (ret_res, &res, sizeof (res));
320 return (0);
321 } /* }}} int lcc_receive */
323 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
324 const char *command, lcc_response_t *ret_res)
325 {
326 lcc_response_t res;
327 int status;
329 status = lcc_send (c, command);
330 if (status != 0)
331 return (status);
333 memset (&res, 0, sizeof (res));
334 status = lcc_receive (c, &res);
335 if (status == 0)
336 memcpy (ret_res, &res, sizeof (*ret_res));
338 return (status);
339 } /* }}} int lcc_sendreceive */
341 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
342 {
343 struct sockaddr_un sa;
344 int fd;
345 int status;
347 assert (c != NULL);
348 assert (c->fh == NULL);
349 assert (path != NULL);
351 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
352 if (fd < 0)
353 {
354 lcc_set_errno (c, errno);
355 return (-1);
356 }
358 memset (&sa, 0, sizeof (sa));
359 sa.sun_family = AF_UNIX;
360 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
362 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
363 if (status != 0)
364 {
365 lcc_set_errno (c, errno);
366 close (fd);
367 return (-1);
368 }
370 c->fh = fdopen (fd, "r+");
371 if (c->fh == NULL)
372 {
373 lcc_set_errno (c, errno);
374 close (fd);
375 return (-1);
376 }
378 return (0);
379 } /* }}} int lcc_open_unixsocket */
381 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
382 const char *addr_orig)
383 {
384 struct addrinfo ai_hints;
385 struct addrinfo *ai_res;
386 struct addrinfo *ai_ptr;
387 char addr_copy[NI_MAXHOST];
388 char *addr;
389 char *port;
390 int fd;
391 int status;
393 assert (c != NULL);
394 assert (c->fh == NULL);
395 assert (addr_orig != NULL);
397 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
398 addr_copy[sizeof(addr_copy) - 1] = '\0';
399 addr = addr_copy;
401 memset (&ai_hints, 0, sizeof (ai_hints));
402 ai_hints.ai_flags = 0;
403 #ifdef AI_ADDRCONFIG
404 ai_hints.ai_flags |= AI_ADDRCONFIG;
405 #endif
406 ai_hints.ai_family = AF_UNSPEC;
407 ai_hints.ai_socktype = SOCK_STREAM;
409 port = NULL;
410 if (*addr == '[') /* IPv6+port format */
411 {
412 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
413 addr++;
415 port = strchr (addr, ']');
416 if (port == NULL)
417 {
418 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
419 return (-1);
420 }
421 *port = 0;
422 port++;
424 if (*port == ':')
425 port++;
426 else if (*port == 0)
427 port = NULL;
428 else
429 {
430 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
431 return (-1);
432 }
433 } /* if (*addr = ']') */
434 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
435 {
436 port = strrchr (addr, ':');
437 if (port != NULL)
438 {
439 *port = 0;
440 port++;
441 }
442 }
444 ai_res = NULL;
445 status = getaddrinfo (addr,
446 port == NULL ? LCC_DEFAULT_PORT : port,
447 &ai_hints, &ai_res);
448 if (status != 0)
449 {
450 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
451 return (-1);
452 }
454 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
455 {
456 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
457 if (fd < 0)
458 {
459 status = errno;
460 fd = -1;
461 continue;
462 }
464 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
465 if (status != 0)
466 {
467 status = errno;
468 close (fd);
469 fd = -1;
470 continue;
471 }
473 c->fh = fdopen (fd, "r+");
474 if (c->fh == NULL)
475 {
476 status = errno;
477 close (fd);
478 fd = -1;
479 continue;
480 }
482 assert (status == 0);
483 break;
484 } /* for (ai_ptr) */
486 if (status != 0)
487 {
488 lcc_set_errno (c, status);
489 return (-1);
490 }
492 return (0);
493 } /* }}} int lcc_open_netsocket */
495 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
496 {
497 int status = 0;
499 if (addr == NULL)
500 return (-1);
502 assert (c != NULL);
503 assert (c->fh == NULL);
504 assert (addr != NULL);
506 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
507 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
508 else if (addr[0] == '/')
509 status = lcc_open_unixsocket (c, addr);
510 else
511 status = lcc_open_netsocket (c, addr);
513 return (status);
514 } /* }}} int lcc_open_socket */
516 /*
517 * Public functions
518 */
519 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
520 {
521 lcc_connection_t *c;
523 if (address == NULL)
524 return (-1);
526 if (ret_con == NULL)
527 return (-1);
529 c = (lcc_connection_t *) malloc (sizeof (*c));
530 if (c == NULL)
531 return (-1);
532 memset (c, 0, sizeof (*c));
534 *ret_con = c;
535 return (lcc_open_socket (c, address));
536 } /* }}} int lcc_connect */
538 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
539 {
540 if (c == NULL)
541 return (-1);
543 if (c->fh != NULL)
544 {
545 fclose (c->fh);
546 c->fh = NULL;
547 }
549 free (c);
550 return (0);
551 } /* }}} int lcc_disconnect */
553 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
554 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
555 {
556 char ident_str[6 * LCC_NAME_LEN];
557 char ident_esc[12 * LCC_NAME_LEN];
558 char command[14 * LCC_NAME_LEN];
560 lcc_response_t res;
561 size_t values_num;
562 gauge_t *values = NULL;
563 char **values_names = NULL;
565 size_t i;
566 int status;
568 if (c == NULL)
569 return (-1);
571 if (ident == NULL)
572 {
573 lcc_set_errno (c, EINVAL);
574 return (-1);
575 }
577 /* Build a commend with an escaped version of the identifier string. */
578 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
579 if (status != 0)
580 return (status);
582 snprintf (command, sizeof (command), "GETVAL %s",
583 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
584 command[sizeof (command) - 1] = 0;
586 /* Send talk to the daemon.. */
587 status = lcc_sendreceive (c, command, &res);
588 if (status != 0)
589 return (status);
591 if (res.status != 0)
592 {
593 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
594 lcc_response_free (&res);
595 return (-1);
596 }
598 values_num = res.lines_num;
600 #define BAIL_OUT(e) do { \
601 lcc_set_errno (c, (e)); \
602 free (values); \
603 if (values_names != NULL) { \
604 for (i = 0; i < values_num; i++) { \
605 free (values_names[i]); \
606 } \
607 } \
608 free (values_names); \
609 lcc_response_free (&res); \
610 return (-1); \
611 } while (0)
613 /* If neither the values nor the names are requested, return here.. */
614 if ((ret_values == NULL) && (ret_values_names == NULL))
615 {
616 if (ret_values_num != NULL)
617 *ret_values_num = values_num;
618 lcc_response_free (&res);
619 return (0);
620 }
622 /* Allocate space for the values */
623 if (ret_values != NULL)
624 {
625 values = (gauge_t *) malloc (values_num * sizeof (*values));
626 if (values == NULL)
627 BAIL_OUT (ENOMEM);
628 }
630 if (ret_values_names != NULL)
631 {
632 values_names = (char **) calloc (values_num, sizeof (*values_names));
633 if (values_names == NULL)
634 BAIL_OUT (ENOMEM);
635 }
637 for (i = 0; i < res.lines_num; i++)
638 {
639 char *key;
640 char *value;
641 char *endptr;
643 key = res.lines[i];
644 value = strchr (key, '=');
645 if (value == NULL)
646 BAIL_OUT (EPROTO);
648 *value = 0;
649 value++;
651 if (values != NULL)
652 {
653 endptr = NULL;
654 errno = 0;
655 values[i] = strtod (value, &endptr);
657 if ((endptr == value) || (errno != 0))
658 BAIL_OUT (errno);
659 }
661 if (values_names != NULL)
662 {
663 values_names[i] = lcc_strdup (key);
664 if (values_names[i] == NULL)
665 BAIL_OUT (ENOMEM);
666 }
667 } /* for (i = 0; i < res.lines_num; i++) */
669 if (ret_values_num != NULL)
670 *ret_values_num = values_num;
671 if (ret_values != NULL)
672 *ret_values = values;
673 if (ret_values_names != NULL)
674 *ret_values_names = values_names;
676 return (0);
677 } /* }}} int lcc_getval */
679 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
680 {
681 char ident_str[6 * LCC_NAME_LEN];
682 char ident_esc[12 * LCC_NAME_LEN];
683 char command[1024];
684 lcc_response_t res;
685 int status;
686 size_t i;
688 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
689 || (vl->values == NULL) || (vl->values_types == NULL))
690 {
691 lcc_set_errno (c, EINVAL);
692 return (-1);
693 }
695 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
696 &vl->identifier);
697 if (status != 0)
698 return (status);
700 snprintf (command, sizeof (command), "PUTVAL %s",
701 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
702 command[sizeof (command) - 1] = 0;
704 if (vl->interval > 0)
705 {
706 char option[64];
708 snprintf (option, sizeof (option), " interval=%i", vl->interval);
709 option[sizeof (option) - 1] = 0;
711 SSTRCAT (command, option);
712 }
714 if (vl->time > 0)
715 SSTRCATF (command, "%u", (unsigned int) vl->time);
716 else
717 SSTRCAT (command, "N");
719 for (i = 0; i < vl->values_len; i++)
720 {
721 if (vl->values_types[i] == LCC_TYPE_COUNTER)
722 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
723 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
724 {
725 if (isnan (vl->values[i].gauge))
726 SSTRCPY (command, ":U");
727 else
728 SSTRCATF (command, ":%g", vl->values[i].gauge);
729 }
730 } /* for (i = 0; i < vl->values_len; i++) */
732 status = lcc_sendreceive (c, command, &res);
733 if (status != 0)
734 return (status);
736 if (res.status != 0)
737 {
738 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
739 lcc_response_free (&res);
740 return (-1);
741 }
743 lcc_response_free (&res);
744 return (0);
745 } /* }}} int lcc_putval */
747 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
748 lcc_identifier_t *ident, int timeout)
749 {
750 char command[1024];
751 lcc_response_t res;
752 int status;
754 if (c == NULL)
755 {
756 lcc_set_errno (c, EINVAL);
757 return (-1);
758 }
760 SSTRCPY (command, "FLUSH");
762 if (timeout > 0)
763 SSTRCATF (command, " timeout=%i", timeout);
765 if (plugin != NULL)
766 {
767 char buffer[2 * LCC_NAME_LEN];
768 SSTRCATF (command, " plugin=%s",
769 lcc_strescape (buffer, plugin, sizeof (buffer)));
770 }
772 if (ident != NULL)
773 {
774 char ident_str[6 * LCC_NAME_LEN];
775 char ident_esc[12 * LCC_NAME_LEN];
777 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
778 if (status != 0)
779 return (status);
781 SSTRCATF (command, " identifier=%s",
782 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
783 }
785 status = lcc_sendreceive (c, command, &res);
786 if (status != 0)
787 return (status);
789 if (res.status != 0)
790 {
791 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
792 lcc_response_free (&res);
793 return (-1);
794 }
796 lcc_response_free (&res);
797 return (0);
798 } /* }}} int lcc_flush */
800 /* TODO: Implement lcc_putnotif */
802 int lcc_listval (lcc_connection_t *c, /* {{{ */
803 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
804 {
805 lcc_response_t res;
806 size_t i;
807 int status;
809 lcc_identifier_t *ident;
810 size_t ident_num;
812 if (c == NULL)
813 return (-1);
815 if ((ret_ident == NULL) || (ret_ident_num == NULL))
816 {
817 lcc_set_errno (c, EINVAL);
818 return (-1);
819 }
821 status = lcc_sendreceive (c, "LISTVAL", &res);
822 if (status != 0)
823 return (status);
825 if (res.status != 0)
826 {
827 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
828 lcc_response_free (&res);
829 return (-1);
830 }
832 ident_num = res.lines_num;
833 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
834 if (ident == NULL)
835 {
836 lcc_response_free (&res);
837 lcc_set_errno (c, ENOMEM);
838 return (-1);
839 }
841 for (i = 0; i < res.lines_num; i++)
842 {
843 char *time_str;
844 char *ident_str;
846 /* First field is the time. */
847 time_str = res.lines[i];
849 /* Set `ident_str' to the beginning of the second field. */
850 ident_str = time_str;
851 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
852 ident_str++;
853 while ((*ident_str == ' ') || (*ident_str == '\t'))
854 {
855 *ident_str = 0;
856 ident_str++;
857 }
859 if (*ident_str == 0)
860 {
861 lcc_set_errno (c, EPROTO);
862 status = -1;
863 break;
864 }
866 status = lcc_string_to_identifier (c, ident + i, ident_str);
867 if (status != 0)
868 break;
869 }
871 lcc_response_free (&res);
873 if (status != 0)
874 {
875 free (ident);
876 return (-1);
877 }
879 *ret_ident = ident;
880 *ret_ident_num = ident_num;
882 return (0);
883 } /* }}} int lcc_listval */
885 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
886 {
887 if (c == NULL)
888 return ("Invalid object");
889 return (c->errbuf);
890 } /* }}} const char *lcc_strerror */
892 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
893 char *string, size_t string_size, const lcc_identifier_t *ident)
894 {
895 if ((string == NULL) || (string_size < 6) || (ident == NULL))
896 {
897 lcc_set_errno (c, EINVAL);
898 return (-1);
899 }
901 if (ident->plugin_instance[0] == 0)
902 {
903 if (ident->type_instance[0] == 0)
904 snprintf (string, string_size, "%s/%s/%s",
905 ident->host,
906 ident->plugin,
907 ident->type);
908 else
909 snprintf (string, string_size, "%s/%s/%s-%s",
910 ident->host,
911 ident->plugin,
912 ident->type,
913 ident->type_instance);
914 }
915 else
916 {
917 if (ident->type_instance[0] == 0)
918 snprintf (string, string_size, "%s/%s-%s/%s",
919 ident->host,
920 ident->plugin,
921 ident->plugin_instance,
922 ident->type);
923 else
924 snprintf (string, string_size, "%s/%s-%s/%s-%s",
925 ident->host,
926 ident->plugin,
927 ident->plugin_instance,
928 ident->type,
929 ident->type_instance);
930 }
932 string[string_size - 1] = 0;
933 return (0);
934 } /* }}} int lcc_identifier_to_string */
936 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
937 lcc_identifier_t *ident, const char *string)
938 {
939 char *string_copy;
940 char *host;
941 char *plugin;
942 char *plugin_instance;
943 char *type;
944 char *type_instance;
946 string_copy = lcc_strdup (string);
947 if (string_copy == NULL)
948 {
949 lcc_set_errno (c, ENOMEM);
950 return (-1);
951 }
953 host = string_copy;
954 plugin = strchr (host, '/');
955 if (plugin == NULL)
956 {
957 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
958 free (string_copy);
959 return (-1);
960 }
961 *plugin = 0;
962 plugin++;
964 type = strchr (plugin, '/');
965 if (type == NULL)
966 {
967 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
968 free (string_copy);
969 return (-1);
970 }
971 *type = 0;
972 type++;
974 plugin_instance = strchr (plugin, '-');
975 if (plugin_instance != NULL)
976 {
977 *plugin_instance = 0;
978 plugin_instance++;
979 }
981 type_instance = strchr (type, '-');
982 if (type_instance != NULL)
983 {
984 *type_instance = 0;
985 type_instance++;
986 }
988 memset (ident, 0, sizeof (*ident));
990 SSTRCPY (ident->host, host);
991 SSTRCPY (ident->plugin, plugin);
992 if (plugin_instance != NULL)
993 SSTRCPY (ident->plugin_instance, plugin_instance);
994 SSTRCPY (ident->type, type);
995 if (type_instance != NULL)
996 SSTRCPY (ident->type_instance, type_instance);
998 free (string_copy);
999 return (0);
1000 } /* }}} int lcc_string_to_identifier */
1002 /* vim: set sw=2 sts=2 et fdm=marker : */