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 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
65 * termination. They work for static buffers only, because they use `sizeof'.
66 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
67 * is very useful to add formatted stuff to the end of a buffer. */
68 #define SSTRCPY(d,s) do { \
69 strncpy ((d), (s), sizeof (d)); \
70 (d)[sizeof (d) - 1] = 0; \
71 } while (0)
73 #define SSTRCAT(d,s) do { \
74 strncat ((d), (s), sizeof (d)); \
75 (d)[sizeof (d) - 1] = 0; \
76 } while (0)
78 #define SSTRCATF(d, ...) do { \
79 char _b[sizeof (d)]; \
80 snprintf (_b, sizeof (_b), __VA_ARGS__); \
81 _b[sizeof (_b) - 1] = 0; \
82 SSTRCAT ((d), _b); \
83 } while (0)
86 #define LCC_SET_ERRSTR(c, ...) do { \
87 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
88 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
89 } while (0)
91 #if 1
92 # define LCC_DEBUG(...) printf (__VA_ARGS__)
93 #else
94 # define LCC_DEBUG(...) /**/
95 #endif
97 /*
98 * Types
99 */
100 struct lcc_connection_s
101 {
102 FILE *fh;
103 char errbuf[1024];
104 };
106 struct lcc_response_s
107 {
108 int status;
109 char message[1024];
110 char **lines;
111 size_t lines_num;
112 };
113 typedef struct lcc_response_s lcc_response_t;
115 /*
116 * Private functions
117 */
118 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
119 {
120 if (c == NULL)
121 return (-1);
123 strerror_r (err, c->errbuf, sizeof (c->errbuf));
124 c->errbuf[sizeof (c->errbuf) - 1] = 0;
126 return (0);
127 } /* }}} int lcc_set_errno */
129 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
130 * here. */
131 __attribute__((malloc, nonnull (1)))
132 static char *lcc_strdup (const char *str) /* {{{ */
133 {
134 size_t strsize;
135 char *ret;
137 strsize = strlen (str) + 1;
138 ret = (char *) malloc (strsize);
139 if (ret != NULL)
140 memcpy (ret, str, strsize);
141 return (ret);
142 } /* }}} char *lcc_strdup */
144 __attribute__((nonnull (1, 2)))
145 static char *lcc_strescape (char *dest, char *src, size_t dest_size) /* {{{ */
146 {
147 size_t dest_pos;
148 size_t src_pos;
150 dest_pos = 0;
151 src_pos = 0;
153 assert (dest_size >= 3);
155 dest[dest_pos] = '"';
156 dest_pos++;
158 while (42)
159 {
160 if ((dest_pos == (dest_size - 2))
161 || (src[src_pos] == 0))
162 break;
164 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
165 {
166 /* Check if there is enough space for both characters.. */
167 if (dest_pos == (dest_size - 3))
168 break;
170 dest[dest_pos] = '\\';
171 dest_pos++;
172 }
174 dest[dest_pos] = src[src_pos];
175 dest_pos++;
176 src_pos++;
177 }
179 assert (dest_pos <= (dest_size - 2));
181 dest[dest_pos] = '"';
182 dest_pos++;
184 dest[dest_pos] = 0;
185 dest_pos++;
186 src_pos++;
188 return (dest);
189 } /* }}} char *lcc_strescape */
191 /* lcc_chomp: Removes all control-characters at the end of a string. */
192 static void lcc_chomp (char *str) /* {{{ */
193 {
194 size_t str_len;
196 str_len = strlen (str);
197 while (str_len > 0)
198 {
199 if (str[str_len - 1] >= 32)
200 break;
201 str[str_len - 1] = 0;
202 str_len--;
203 }
204 } /* }}} void lcc_chomp */
206 static void lcc_response_free (lcc_response_t *res) /* {{{ */
207 {
208 size_t i;
210 if (res == NULL)
211 return;
213 for (i = 0; i < res->lines_num; i++)
214 free (res->lines[i]);
215 free (res->lines);
216 res->lines = NULL;
217 } /* }}} void lcc_response_free */
219 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
220 {
221 int status;
223 LCC_DEBUG ("send: --> %s\n", command);
225 status = fprintf (c->fh, "%s\r\n", command);
226 if (status < 0)
227 {
228 lcc_set_errno (c, errno);
229 return (-1);
230 }
232 return (0);
233 } /* }}} int lcc_send */
235 static int lcc_receive (lcc_connection_t *c, /* {{{ */
236 lcc_response_t *ret_res)
237 {
238 lcc_response_t res;
239 char *ptr;
240 char buffer[4096];
241 size_t i;
243 memset (&res, 0, sizeof (res));
245 /* Read the first line, containing the status and a message */
246 ptr = fgets (buffer, sizeof (buffer), c->fh);
247 if (ptr == NULL)
248 {
249 lcc_set_errno (c, errno);
250 return (-1);
251 }
252 lcc_chomp (buffer);
253 LCC_DEBUG ("receive: <-- %s\n", buffer);
255 /* Convert the leading status to an integer and make `ptr' to point to the
256 * beginning of the message. */
257 ptr = NULL;
258 errno = 0;
259 res.status = strtol (buffer, &ptr, 0);
260 if ((errno != 0) || (ptr == &buffer[0]))
261 {
262 lcc_set_errno (c, errno);
263 return (-1);
264 }
266 /* Skip white spaces after the status number */
267 while ((*ptr == ' ') || (*ptr == '\t'))
268 ptr++;
270 /* Now copy the message. */
271 strncpy (res.message, ptr, sizeof (res.message));
272 res.message[sizeof (res.message) - 1] = 0;
274 /* Error or no lines follow: We're done. */
275 if (res.status <= 0)
276 {
277 memcpy (ret_res, &res, sizeof (res));
278 return (0);
279 }
281 /* Allocate space for the char-pointers */
282 res.lines_num = (size_t) res.status;
283 res.status = 0;
284 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
285 if (res.lines == NULL)
286 {
287 lcc_set_errno (c, ENOMEM);
288 return (-1);
289 }
291 /* Now receive all the lines */
292 for (i = 0; i < res.lines_num; i++)
293 {
294 ptr = fgets (buffer, sizeof (buffer), c->fh);
295 if (ptr == NULL)
296 {
297 lcc_set_errno (c, errno);
298 break;
299 }
300 lcc_chomp (buffer);
301 LCC_DEBUG ("receive: <-- %s\n", buffer);
303 res.lines[i] = lcc_strdup (buffer);
304 if (res.lines[i] == NULL)
305 {
306 lcc_set_errno (c, ENOMEM);
307 break;
308 }
309 }
311 /* Check if the for-loop exited with an error. */
312 if (i < res.lines_num)
313 {
314 while (i > 0)
315 {
316 i--;
317 free (res.lines[i]);
318 }
319 free (res.lines);
320 return (-1);
321 }
323 memcpy (ret_res, &res, sizeof (res));
324 return (0);
325 } /* }}} int lcc_receive */
327 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
328 const char *command, lcc_response_t *ret_res)
329 {
330 lcc_response_t res;
331 int status;
333 status = lcc_send (c, command);
334 if (status != 0)
335 return (status);
337 memset (&res, 0, sizeof (res));
338 status = lcc_receive (c, &res);
339 if (status == 0)
340 memcpy (ret_res, &res, sizeof (*ret_res));
342 return (status);
343 } /* }}} int lcc_sendreceive */
345 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
346 {
347 struct sockaddr_un sa;
348 int fd;
349 int status;
351 assert (c != NULL);
352 assert (c->fh == NULL);
353 assert (path != NULL);
355 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
356 if (fd < 0)
357 {
358 lcc_set_errno (c, errno);
359 return (-1);
360 }
362 memset (&sa, 0, sizeof (sa));
363 sa.sun_family = AF_UNIX;
364 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
366 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
367 if (status != 0)
368 {
369 lcc_set_errno (c, errno);
370 close (fd);
371 return (-1);
372 }
374 c->fh = fdopen (fd, "r+");
375 if (c->fh == NULL)
376 {
377 lcc_set_errno (c, errno);
378 close (fd);
379 return (-1);
380 }
382 return (0);
383 } /* }}} int lcc_open_unixsocket */
385 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
386 const char *addr_orig)
387 {
388 struct addrinfo ai_hints;
389 struct addrinfo *ai_res;
390 struct addrinfo *ai_ptr;
391 char addr_copy[NI_MAXHOST];
392 char *addr;
393 char *port;
394 int fd;
395 int status;
397 assert (c != NULL);
398 assert (c->fh == NULL);
399 assert (addr_orig != NULL);
401 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
402 addr_copy[sizeof(addr_copy) - 1] = '\0';
403 addr = addr_copy;
405 memset (&ai_hints, 0, sizeof (ai_hints));
406 ai_hints.ai_flags = 0;
407 #ifdef AI_ADDRCONFIG
408 ai_hints.ai_flags |= AI_ADDRCONFIG;
409 #endif
410 ai_hints.ai_family = AF_UNSPEC;
411 ai_hints.ai_socktype = SOCK_STREAM;
413 port = NULL;
414 if (*addr == '[') /* IPv6+port format */
415 {
416 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
417 addr++;
419 port = strchr (addr, ']');
420 if (port == NULL)
421 {
422 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
423 return (-1);
424 }
425 *port = 0;
426 port++;
428 if (*port == ':')
429 port++;
430 else if (*port == 0)
431 port = NULL;
432 else
433 {
434 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
435 return (-1);
436 }
437 } /* if (*addr = ']') */
438 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
439 {
440 port = strrchr (addr, ':');
441 if (port != NULL)
442 {
443 *port = 0;
444 port++;
445 }
446 }
448 ai_res = NULL;
449 status = getaddrinfo (addr,
450 port == NULL ? LCC_DEFAULT_PORT : port,
451 &ai_hints, &ai_res);
452 if (status != 0)
453 {
454 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
455 return (-1);
456 }
458 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
459 {
460 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
461 if (fd < 0)
462 {
463 status = errno;
464 fd = -1;
465 continue;
466 }
468 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
469 if (status != 0)
470 {
471 status = errno;
472 close (fd);
473 fd = -1;
474 continue;
475 }
477 c->fh = fdopen (fd, "r+");
478 if (c->fh == NULL)
479 {
480 status = errno;
481 close (fd);
482 fd = -1;
483 continue;
484 }
486 assert (status == 0);
487 break;
488 } /* for (ai_ptr) */
490 if (status != 0)
491 {
492 lcc_set_errno (c, status);
493 return (-1);
494 }
496 return (0);
497 } /* }}} int lcc_open_netsocket */
499 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
500 {
501 int status = 0;
503 if (addr == NULL)
504 return (-1);
506 assert (c != NULL);
507 assert (c->fh == NULL);
508 assert (addr != NULL);
510 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
511 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
512 else if (addr[0] == '/')
513 status = lcc_open_unixsocket (c, addr);
514 else
515 status = lcc_open_netsocket (c, addr);
517 return (status);
518 } /* }}} int lcc_open_socket */
520 /*
521 * Public functions
522 */
523 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
524 {
525 lcc_connection_t *c;
527 if (address == NULL)
528 return (-1);
530 if (ret_con == NULL)
531 return (-1);
533 c = (lcc_connection_t *) malloc (sizeof (*c));
534 if (c == NULL)
535 return (-1);
536 memset (c, 0, sizeof (*c));
538 *ret_con = c;
539 return (lcc_open_socket (c, address));
540 } /* }}} int lcc_connect */
542 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
543 {
544 if (c == NULL)
545 return (-1);
547 if (c->fh != NULL)
548 {
549 fclose (c->fh);
550 c->fh = NULL;
551 }
553 free (c);
554 return (0);
555 } /* }}} int lcc_disconnect */
557 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
558 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
559 {
560 char ident_str[6 * LCC_NAME_LEN];
561 char ident_esc[12 * LCC_NAME_LEN];
562 char command[14 * LCC_NAME_LEN];
564 lcc_response_t res;
565 size_t values_num;
566 gauge_t *values = NULL;
567 char **values_names = NULL;
569 size_t i;
570 int status;
572 if (c == NULL)
573 return (-1);
575 if (ident == NULL)
576 {
577 lcc_set_errno (c, EINVAL);
578 return (-1);
579 }
581 /* Build a commend with an escaped version of the identifier string. */
582 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
583 if (status != 0)
584 return (status);
586 snprintf (command, sizeof (command), "GETVAL %s",
587 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
588 command[sizeof (command) - 1] = 0;
590 /* Send talk to the daemon.. */
591 status = lcc_sendreceive (c, command, &res);
592 if (status != 0)
593 return (status);
595 if (res.status != 0)
596 {
597 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
598 lcc_response_free (&res);
599 return (-1);
600 }
602 values_num = res.lines_num;
604 #define BAIL_OUT(e) do { \
605 lcc_set_errno (c, (e)); \
606 free (values); \
607 if (values_names != NULL) { \
608 for (i = 0; i < values_num; i++) { \
609 free (values_names[i]); \
610 } \
611 } \
612 free (values_names); \
613 lcc_response_free (&res); \
614 return (-1); \
615 } while (0)
617 /* If neither the values nor the names are requested, return here.. */
618 if ((ret_values == NULL) && (ret_values_names == NULL))
619 {
620 if (ret_values_num != NULL)
621 *ret_values_num = values_num;
622 lcc_response_free (&res);
623 return (0);
624 }
626 /* Allocate space for the values */
627 if (ret_values != NULL)
628 {
629 values = (gauge_t *) malloc (values_num * sizeof (*values));
630 if (values == NULL)
631 BAIL_OUT (ENOMEM);
632 }
634 if (ret_values_names != NULL)
635 {
636 values_names = (char **) calloc (values_num, sizeof (*values_names));
637 if (values_names == NULL)
638 BAIL_OUT (ENOMEM);
639 }
641 for (i = 0; i < res.lines_num; i++)
642 {
643 char *key;
644 char *value;
645 char *endptr;
647 key = res.lines[i];
648 value = strchr (key, '=');
649 if (value == NULL)
650 BAIL_OUT (EPROTO);
652 *value = 0;
653 value++;
655 if (values != NULL)
656 {
657 endptr = NULL;
658 errno = 0;
659 values[i] = strtod (value, &endptr);
661 if ((endptr == value) || (errno != 0))
662 BAIL_OUT (errno);
663 }
665 if (values_names != NULL)
666 {
667 values_names[i] = lcc_strdup (key);
668 if (values_names[i] == NULL)
669 BAIL_OUT (ENOMEM);
670 }
671 } /* for (i = 0; i < res.lines_num; i++) */
673 if (ret_values_num != NULL)
674 *ret_values_num = values_num;
675 if (ret_values != NULL)
676 *ret_values = values;
677 if (ret_values_names != NULL)
678 *ret_values_names = values_names;
680 return (0);
681 } /* }}} int lcc_getval */
683 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
684 {
685 char ident_str[6 * LCC_NAME_LEN];
686 char ident_esc[12 * LCC_NAME_LEN];
687 char command[1024] = "";
688 lcc_response_t res;
689 int status;
690 size_t i;
692 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
693 || (vl->values == NULL) || (vl->values_types == NULL))
694 {
695 lcc_set_errno (c, EINVAL);
696 return (-1);
697 }
699 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
700 &vl->identifier);
701 if (status != 0)
702 return (status);
704 SSTRCATF (command, "PUTVAL %s",
705 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
707 if (vl->interval > 0)
708 SSTRCATF (command, " interval=%i", vl->interval);
710 if (vl->time > 0)
711 SSTRCATF (command, "%u", (unsigned int) vl->time);
712 else
713 SSTRCAT (command, "N");
715 for (i = 0; i < vl->values_len; i++)
716 {
717 if (vl->values_types[i] == LCC_TYPE_COUNTER)
718 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
719 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
720 {
721 if (isnan (vl->values[i].gauge))
722 SSTRCPY (command, ":U");
723 else
724 SSTRCATF (command, ":%g", vl->values[i].gauge);
725 }
726 } /* for (i = 0; i < vl->values_len; i++) */
728 status = lcc_sendreceive (c, command, &res);
729 if (status != 0)
730 return (status);
732 if (res.status != 0)
733 {
734 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
735 lcc_response_free (&res);
736 return (-1);
737 }
739 lcc_response_free (&res);
740 return (0);
741 } /* }}} int lcc_putval */
743 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
744 lcc_identifier_t *ident, int timeout)
745 {
746 char command[1024];
747 lcc_response_t res;
748 int status;
750 if (c == NULL)
751 {
752 lcc_set_errno (c, EINVAL);
753 return (-1);
754 }
756 SSTRCPY (command, "FLUSH");
758 if (timeout > 0)
759 SSTRCATF (command, " timeout=%i", timeout);
761 if (plugin != NULL)
762 {
763 char buffer[2 * LCC_NAME_LEN];
764 SSTRCATF (command, " plugin=%s",
765 lcc_strescape (buffer, plugin, sizeof (buffer)));
766 }
768 if (ident != NULL)
769 {
770 char ident_str[6 * LCC_NAME_LEN];
771 char ident_esc[12 * LCC_NAME_LEN];
773 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
774 if (status != 0)
775 return (status);
777 SSTRCATF (command, " identifier=%s",
778 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
779 }
781 status = lcc_sendreceive (c, command, &res);
782 if (status != 0)
783 return (status);
785 if (res.status != 0)
786 {
787 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
788 lcc_response_free (&res);
789 return (-1);
790 }
792 lcc_response_free (&res);
793 return (0);
794 } /* }}} int lcc_flush */
796 /* TODO: Implement lcc_putnotif */
798 int lcc_listval (lcc_connection_t *c, /* {{{ */
799 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
800 {
801 lcc_response_t res;
802 size_t i;
803 int status;
805 lcc_identifier_t *ident;
806 size_t ident_num;
808 if (c == NULL)
809 return (-1);
811 if ((ret_ident == NULL) || (ret_ident_num == NULL))
812 {
813 lcc_set_errno (c, EINVAL);
814 return (-1);
815 }
817 status = lcc_sendreceive (c, "LISTVAL", &res);
818 if (status != 0)
819 return (status);
821 if (res.status != 0)
822 {
823 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
824 lcc_response_free (&res);
825 return (-1);
826 }
828 ident_num = res.lines_num;
829 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
830 if (ident == NULL)
831 {
832 lcc_response_free (&res);
833 lcc_set_errno (c, ENOMEM);
834 return (-1);
835 }
837 for (i = 0; i < res.lines_num; i++)
838 {
839 char *time_str;
840 char *ident_str;
842 /* First field is the time. */
843 time_str = res.lines[i];
845 /* Set `ident_str' to the beginning of the second field. */
846 ident_str = time_str;
847 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
848 ident_str++;
849 while ((*ident_str == ' ') || (*ident_str == '\t'))
850 {
851 *ident_str = 0;
852 ident_str++;
853 }
855 if (*ident_str == 0)
856 {
857 lcc_set_errno (c, EPROTO);
858 status = -1;
859 break;
860 }
862 status = lcc_string_to_identifier (c, ident + i, ident_str);
863 if (status != 0)
864 break;
865 }
867 lcc_response_free (&res);
869 if (status != 0)
870 {
871 free (ident);
872 return (-1);
873 }
875 *ret_ident = ident;
876 *ret_ident_num = ident_num;
878 return (0);
879 } /* }}} int lcc_listval */
881 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
882 {
883 if (c == NULL)
884 return ("Invalid object");
885 return (c->errbuf);
886 } /* }}} const char *lcc_strerror */
888 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
889 char *string, size_t string_size, const lcc_identifier_t *ident)
890 {
891 if ((string == NULL) || (string_size < 6) || (ident == NULL))
892 {
893 lcc_set_errno (c, EINVAL);
894 return (-1);
895 }
897 if (ident->plugin_instance[0] == 0)
898 {
899 if (ident->type_instance[0] == 0)
900 snprintf (string, string_size, "%s/%s/%s",
901 ident->host,
902 ident->plugin,
903 ident->type);
904 else
905 snprintf (string, string_size, "%s/%s/%s-%s",
906 ident->host,
907 ident->plugin,
908 ident->type,
909 ident->type_instance);
910 }
911 else
912 {
913 if (ident->type_instance[0] == 0)
914 snprintf (string, string_size, "%s/%s-%s/%s",
915 ident->host,
916 ident->plugin,
917 ident->plugin_instance,
918 ident->type);
919 else
920 snprintf (string, string_size, "%s/%s-%s/%s-%s",
921 ident->host,
922 ident->plugin,
923 ident->plugin_instance,
924 ident->type,
925 ident->type_instance);
926 }
928 string[string_size - 1] = 0;
929 return (0);
930 } /* }}} int lcc_identifier_to_string */
932 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
933 lcc_identifier_t *ident, const char *string)
934 {
935 char *string_copy;
936 char *host;
937 char *plugin;
938 char *plugin_instance;
939 char *type;
940 char *type_instance;
942 string_copy = lcc_strdup (string);
943 if (string_copy == NULL)
944 {
945 lcc_set_errno (c, ENOMEM);
946 return (-1);
947 }
949 host = string_copy;
950 plugin = strchr (host, '/');
951 if (plugin == NULL)
952 {
953 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
954 free (string_copy);
955 return (-1);
956 }
957 *plugin = 0;
958 plugin++;
960 type = strchr (plugin, '/');
961 if (type == NULL)
962 {
963 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
964 free (string_copy);
965 return (-1);
966 }
967 *type = 0;
968 type++;
970 plugin_instance = strchr (plugin, '-');
971 if (plugin_instance != NULL)
972 {
973 *plugin_instance = 0;
974 plugin_instance++;
975 }
977 type_instance = strchr (type, '-');
978 if (type_instance != NULL)
979 {
980 *type_instance = 0;
981 type_instance++;
982 }
984 memset (ident, 0, sizeof (*ident));
986 SSTRCPY (ident->host, host);
987 SSTRCPY (ident->plugin, plugin);
988 if (plugin_instance != NULL)
989 SSTRCPY (ident->plugin_instance, plugin_instance);
990 SSTRCPY (ident->type, type);
991 if (type_instance != NULL)
992 SSTRCPY (ident->type_instance, type_instance);
994 free (string_copy);
995 return (0);
996 } /* }}} int lcc_string_to_identifier */
998 /* vim: set sw=2 sts=2 et fdm=marker : */