f2c0a1e591ee021cd520c2752fdd63f5c7e49954
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 <stdlib.h>
56 #include <stdio.h>
57 #include <unistd.h>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <sys/un.h>
61 #include <string.h>
62 #include <assert.h>
63 #include <errno.h>
64 #include <netdb.h>
66 #include "client.h"
68 /* NI_MAXHOST has been obsoleted by RFC 3493 which is a reason for SunOS 5.11
69 * to no longer define it. We'll use the old, RFC 2553 value here. */
70 #ifndef NI_MAXHOST
71 # define NI_MAXHOST 1025
72 #endif
74 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
75 * termination. They work for static buffers only, because they use `sizeof'.
76 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
77 * is very useful to add formatted stuff to the end of a buffer. */
78 #define SSTRCPY(d,s) do { \
79 strncpy ((d), (s), sizeof (d)); \
80 (d)[sizeof (d) - 1] = 0; \
81 } while (0)
83 #define SSTRCAT(d,s) do { \
84 strncat ((d), (s), sizeof (d)); \
85 (d)[sizeof (d) - 1] = 0; \
86 } while (0)
88 #define SSTRCATF(d, ...) do { \
89 char _b[sizeof (d)]; \
90 snprintf (_b, sizeof (_b), __VA_ARGS__); \
91 _b[sizeof (_b) - 1] = 0; \
92 SSTRCAT ((d), _b); \
93 } while (0)
96 #define LCC_SET_ERRSTR(c, ...) do { \
97 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
98 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
99 } while (0)
101 #if COLLECT_DEBUG
102 # define LCC_DEBUG(...) printf (__VA_ARGS__)
103 #else
104 # define LCC_DEBUG(...) /**/
105 #endif
107 /*
108 * Types
109 */
110 struct lcc_connection_s
111 {
112 FILE *fh;
113 char errbuf[1024];
114 };
116 struct lcc_response_s
117 {
118 int status;
119 char message[1024];
120 char **lines;
121 size_t lines_num;
122 };
123 typedef struct lcc_response_s lcc_response_t;
125 /*
126 * Private functions
127 */
128 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
129 {
130 if (c == NULL)
131 return (-1);
133 strerror_r (err, c->errbuf, sizeof (c->errbuf));
134 c->errbuf[sizeof (c->errbuf) - 1] = 0;
136 return (0);
137 } /* }}} int lcc_set_errno */
139 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
140 * here. */
141 __attribute__((malloc, nonnull (1)))
142 static char *lcc_strdup (const char *str) /* {{{ */
143 {
144 size_t strsize;
145 char *ret;
147 strsize = strlen (str) + 1;
148 ret = (char *) malloc (strsize);
149 if (ret != NULL)
150 memcpy (ret, str, strsize);
151 return (ret);
152 } /* }}} char *lcc_strdup */
154 __attribute__((nonnull (1, 2)))
155 static char *lcc_strescape (char *dest, char *src, size_t dest_size) /* {{{ */
156 {
157 size_t dest_pos;
158 size_t src_pos;
160 dest_pos = 0;
161 src_pos = 0;
163 assert (dest_size >= 3);
165 dest[dest_pos] = '"';
166 dest_pos++;
168 while (42)
169 {
170 if ((dest_pos == (dest_size - 2))
171 || (src[src_pos] == 0))
172 break;
174 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
175 {
176 /* Check if there is enough space for both characters.. */
177 if (dest_pos == (dest_size - 3))
178 break;
180 dest[dest_pos] = '\\';
181 dest_pos++;
182 }
184 dest[dest_pos] = src[src_pos];
185 dest_pos++;
186 src_pos++;
187 }
189 assert (dest_pos <= (dest_size - 2));
191 dest[dest_pos] = '"';
192 dest_pos++;
194 dest[dest_pos] = 0;
195 dest_pos++;
196 src_pos++;
198 return (dest);
199 } /* }}} char *lcc_strescape */
201 /* lcc_chomp: Removes all control-characters at the end of a string. */
202 static void lcc_chomp (char *str) /* {{{ */
203 {
204 size_t str_len;
206 str_len = strlen (str);
207 while (str_len > 0)
208 {
209 if (str[str_len - 1] >= 32)
210 break;
211 str[str_len - 1] = 0;
212 str_len--;
213 }
214 } /* }}} void lcc_chomp */
216 static void lcc_response_free (lcc_response_t *res) /* {{{ */
217 {
218 size_t i;
220 if (res == NULL)
221 return;
223 for (i = 0; i < res->lines_num; i++)
224 free (res->lines[i]);
225 free (res->lines);
226 res->lines = NULL;
227 } /* }}} void lcc_response_free */
229 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
230 {
231 int status;
233 LCC_DEBUG ("send: --> %s\n", command);
235 status = fprintf (c->fh, "%s\r\n", command);
236 if (status < 0)
237 {
238 lcc_set_errno (c, errno);
239 return (-1);
240 }
242 return (0);
243 } /* }}} int lcc_send */
245 static int lcc_receive (lcc_connection_t *c, /* {{{ */
246 lcc_response_t *ret_res)
247 {
248 lcc_response_t res;
249 char *ptr;
250 char buffer[4096];
251 size_t i;
253 memset (&res, 0, sizeof (res));
255 /* Read the first line, containing the status and a message */
256 ptr = fgets (buffer, sizeof (buffer), c->fh);
257 if (ptr == NULL)
258 {
259 lcc_set_errno (c, errno);
260 return (-1);
261 }
262 lcc_chomp (buffer);
263 LCC_DEBUG ("receive: <-- %s\n", buffer);
265 /* Convert the leading status to an integer and make `ptr' to point to the
266 * beginning of the message. */
267 ptr = NULL;
268 errno = 0;
269 res.status = strtol (buffer, &ptr, 0);
270 if ((errno != 0) || (ptr == &buffer[0]))
271 {
272 lcc_set_errno (c, errno);
273 return (-1);
274 }
276 /* Skip white spaces after the status number */
277 while ((*ptr == ' ') || (*ptr == '\t'))
278 ptr++;
280 /* Now copy the message. */
281 strncpy (res.message, ptr, sizeof (res.message));
282 res.message[sizeof (res.message) - 1] = 0;
284 /* Error or no lines follow: We're done. */
285 if (res.status <= 0)
286 {
287 memcpy (ret_res, &res, sizeof (res));
288 return (0);
289 }
291 /* Allocate space for the char-pointers */
292 res.lines_num = (size_t) res.status;
293 res.status = 0;
294 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
295 if (res.lines == NULL)
296 {
297 lcc_set_errno (c, ENOMEM);
298 return (-1);
299 }
301 /* Now receive all the lines */
302 for (i = 0; i < res.lines_num; i++)
303 {
304 ptr = fgets (buffer, sizeof (buffer), c->fh);
305 if (ptr == NULL)
306 {
307 lcc_set_errno (c, errno);
308 break;
309 }
310 lcc_chomp (buffer);
311 LCC_DEBUG ("receive: <-- %s\n", buffer);
313 res.lines[i] = lcc_strdup (buffer);
314 if (res.lines[i] == NULL)
315 {
316 lcc_set_errno (c, ENOMEM);
317 break;
318 }
319 }
321 /* Check if the for-loop exited with an error. */
322 if (i < res.lines_num)
323 {
324 while (i > 0)
325 {
326 i--;
327 free (res.lines[i]);
328 }
329 free (res.lines);
330 return (-1);
331 }
333 memcpy (ret_res, &res, sizeof (res));
334 return (0);
335 } /* }}} int lcc_receive */
337 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
338 const char *command, lcc_response_t *ret_res)
339 {
340 lcc_response_t res;
341 int status;
343 status = lcc_send (c, command);
344 if (status != 0)
345 return (status);
347 memset (&res, 0, sizeof (res));
348 status = lcc_receive (c, &res);
349 if (status == 0)
350 memcpy (ret_res, &res, sizeof (*ret_res));
352 return (status);
353 } /* }}} int lcc_sendreceive */
355 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
356 {
357 struct sockaddr_un sa;
358 int fd;
359 int status;
361 assert (c != NULL);
362 assert (c->fh == NULL);
363 assert (path != NULL);
365 fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
366 if (fd < 0)
367 {
368 lcc_set_errno (c, errno);
369 return (-1);
370 }
372 memset (&sa, 0, sizeof (sa));
373 sa.sun_family = AF_UNIX;
374 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
376 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
377 if (status != 0)
378 {
379 lcc_set_errno (c, errno);
380 close (fd);
381 return (-1);
382 }
384 c->fh = fdopen (fd, "r+");
385 if (c->fh == NULL)
386 {
387 lcc_set_errno (c, errno);
388 close (fd);
389 return (-1);
390 }
392 return (0);
393 } /* }}} int lcc_open_unixsocket */
395 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
396 const char *addr_orig)
397 {
398 struct addrinfo ai_hints;
399 struct addrinfo *ai_res;
400 struct addrinfo *ai_ptr;
401 char addr_copy[NI_MAXHOST];
402 char *addr;
403 char *port;
404 int fd;
405 int status;
407 assert (c != NULL);
408 assert (c->fh == NULL);
409 assert (addr_orig != NULL);
411 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
412 addr_copy[sizeof(addr_copy) - 1] = '\0';
413 addr = addr_copy;
415 memset (&ai_hints, 0, sizeof (ai_hints));
416 ai_hints.ai_flags = 0;
417 #ifdef AI_ADDRCONFIG
418 ai_hints.ai_flags |= AI_ADDRCONFIG;
419 #endif
420 ai_hints.ai_family = AF_UNSPEC;
421 ai_hints.ai_socktype = SOCK_STREAM;
423 port = NULL;
424 if (*addr == '[') /* IPv6+port format */
425 {
426 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
427 addr++;
429 port = strchr (addr, ']');
430 if (port == NULL)
431 {
432 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
433 return (-1);
434 }
435 *port = 0;
436 port++;
438 if (*port == ':')
439 port++;
440 else if (*port == 0)
441 port = NULL;
442 else
443 {
444 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
445 return (-1);
446 }
447 } /* if (*addr = ']') */
448 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
449 {
450 port = strrchr (addr, ':');
451 if (port != NULL)
452 {
453 *port = 0;
454 port++;
455 }
456 }
458 ai_res = NULL;
459 status = getaddrinfo (addr,
460 port == NULL ? LCC_DEFAULT_PORT : port,
461 &ai_hints, &ai_res);
462 if (status != 0)
463 {
464 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
465 return (-1);
466 }
468 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
469 {
470 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
471 if (fd < 0)
472 {
473 status = errno;
474 fd = -1;
475 continue;
476 }
478 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
479 if (status != 0)
480 {
481 status = errno;
482 close (fd);
483 fd = -1;
484 continue;
485 }
487 c->fh = fdopen (fd, "r+");
488 if (c->fh == NULL)
489 {
490 status = errno;
491 close (fd);
492 fd = -1;
493 continue;
494 }
496 assert (status == 0);
497 break;
498 } /* for (ai_ptr) */
500 if (status != 0)
501 {
502 lcc_set_errno (c, status);
503 return (-1);
504 }
506 return (0);
507 } /* }}} int lcc_open_netsocket */
509 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
510 {
511 int status = 0;
513 if (addr == NULL)
514 return (-1);
516 assert (c != NULL);
517 assert (c->fh == NULL);
518 assert (addr != NULL);
520 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
521 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
522 else if (addr[0] == '/')
523 status = lcc_open_unixsocket (c, addr);
524 else
525 status = lcc_open_netsocket (c, addr);
527 return (status);
528 } /* }}} int lcc_open_socket */
530 /*
531 * Public functions
532 */
533 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
534 {
535 lcc_connection_t *c;
537 if (address == NULL)
538 return (-1);
540 if (ret_con == NULL)
541 return (-1);
543 c = (lcc_connection_t *) malloc (sizeof (*c));
544 if (c == NULL)
545 return (-1);
546 memset (c, 0, sizeof (*c));
548 *ret_con = c;
549 return (lcc_open_socket (c, address));
550 } /* }}} int lcc_connect */
552 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
553 {
554 if (c == NULL)
555 return (-1);
557 if (c->fh != NULL)
558 {
559 fclose (c->fh);
560 c->fh = NULL;
561 }
563 free (c);
564 return (0);
565 } /* }}} int lcc_disconnect */
567 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
568 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
569 {
570 char ident_str[6 * LCC_NAME_LEN];
571 char ident_esc[12 * LCC_NAME_LEN];
572 char command[14 * LCC_NAME_LEN];
574 lcc_response_t res;
575 size_t values_num;
576 gauge_t *values = NULL;
577 char **values_names = NULL;
579 size_t i;
580 int status;
582 if (c == NULL)
583 return (-1);
585 if (ident == NULL)
586 {
587 lcc_set_errno (c, EINVAL);
588 return (-1);
589 }
591 /* Build a commend with an escaped version of the identifier string. */
592 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
593 if (status != 0)
594 return (status);
596 snprintf (command, sizeof (command), "GETVAL %s",
597 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
598 command[sizeof (command) - 1] = 0;
600 /* Send talk to the daemon.. */
601 status = lcc_sendreceive (c, command, &res);
602 if (status != 0)
603 return (status);
605 if (res.status != 0)
606 {
607 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
608 lcc_response_free (&res);
609 return (-1);
610 }
612 values_num = res.lines_num;
614 #define BAIL_OUT(e) do { \
615 lcc_set_errno (c, (e)); \
616 free (values); \
617 if (values_names != NULL) { \
618 for (i = 0; i < values_num; i++) { \
619 free (values_names[i]); \
620 } \
621 } \
622 free (values_names); \
623 lcc_response_free (&res); \
624 return (-1); \
625 } while (0)
627 /* If neither the values nor the names are requested, return here.. */
628 if ((ret_values == NULL) && (ret_values_names == NULL))
629 {
630 if (ret_values_num != NULL)
631 *ret_values_num = values_num;
632 lcc_response_free (&res);
633 return (0);
634 }
636 /* Allocate space for the values */
637 if (ret_values != NULL)
638 {
639 values = (gauge_t *) malloc (values_num * sizeof (*values));
640 if (values == NULL)
641 BAIL_OUT (ENOMEM);
642 }
644 if (ret_values_names != NULL)
645 {
646 values_names = (char **) calloc (values_num, sizeof (*values_names));
647 if (values_names == NULL)
648 BAIL_OUT (ENOMEM);
649 }
651 for (i = 0; i < res.lines_num; i++)
652 {
653 char *key;
654 char *value;
655 char *endptr;
657 key = res.lines[i];
658 value = strchr (key, '=');
659 if (value == NULL)
660 BAIL_OUT (EPROTO);
662 *value = 0;
663 value++;
665 if (values != NULL)
666 {
667 endptr = NULL;
668 errno = 0;
669 values[i] = strtod (value, &endptr);
671 if ((endptr == value) || (errno != 0))
672 BAIL_OUT (errno);
673 }
675 if (values_names != NULL)
676 {
677 values_names[i] = lcc_strdup (key);
678 if (values_names[i] == NULL)
679 BAIL_OUT (ENOMEM);
680 }
681 } /* for (i = 0; i < res.lines_num; i++) */
683 if (ret_values_num != NULL)
684 *ret_values_num = values_num;
685 if (ret_values != NULL)
686 *ret_values = values;
687 if (ret_values_names != NULL)
688 *ret_values_names = values_names;
690 return (0);
691 } /* }}} int lcc_getval */
693 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
694 {
695 char ident_str[6 * LCC_NAME_LEN];
696 char ident_esc[12 * LCC_NAME_LEN];
697 char command[1024] = "";
698 lcc_response_t res;
699 int status;
700 size_t i;
702 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
703 || (vl->values == NULL) || (vl->values_types == NULL))
704 {
705 lcc_set_errno (c, EINVAL);
706 return (-1);
707 }
709 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
710 &vl->identifier);
711 if (status != 0)
712 return (status);
714 SSTRCATF (command, "PUTVAL %s",
715 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
717 if (vl->interval > 0)
718 SSTRCATF (command, " interval=%i", vl->interval);
720 if (vl->time > 0)
721 SSTRCATF (command, "%u", (unsigned int) vl->time);
722 else
723 SSTRCAT (command, "N");
725 for (i = 0; i < vl->values_len; i++)
726 {
727 if (vl->values_types[i] == LCC_TYPE_COUNTER)
728 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
729 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
730 {
731 if (isnan (vl->values[i].gauge))
732 SSTRCPY (command, ":U");
733 else
734 SSTRCATF (command, ":%g", vl->values[i].gauge);
735 }
736 } /* for (i = 0; i < vl->values_len; i++) */
738 status = lcc_sendreceive (c, command, &res);
739 if (status != 0)
740 return (status);
742 if (res.status != 0)
743 {
744 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
745 lcc_response_free (&res);
746 return (-1);
747 }
749 lcc_response_free (&res);
750 return (0);
751 } /* }}} int lcc_putval */
753 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
754 lcc_identifier_t *ident, int timeout)
755 {
756 char command[1024];
757 lcc_response_t res;
758 int status;
760 if (c == NULL)
761 {
762 lcc_set_errno (c, EINVAL);
763 return (-1);
764 }
766 SSTRCPY (command, "FLUSH");
768 if (timeout > 0)
769 SSTRCATF (command, " timeout=%i", timeout);
771 if (plugin != NULL)
772 {
773 char buffer[2 * LCC_NAME_LEN];
774 SSTRCATF (command, " plugin=%s",
775 lcc_strescape (buffer, plugin, sizeof (buffer)));
776 }
778 if (ident != NULL)
779 {
780 char ident_str[6 * LCC_NAME_LEN];
781 char ident_esc[12 * LCC_NAME_LEN];
783 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
784 if (status != 0)
785 return (status);
787 SSTRCATF (command, " identifier=%s",
788 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
789 }
791 status = lcc_sendreceive (c, command, &res);
792 if (status != 0)
793 return (status);
795 if (res.status != 0)
796 {
797 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
798 lcc_response_free (&res);
799 return (-1);
800 }
802 lcc_response_free (&res);
803 return (0);
804 } /* }}} int lcc_flush */
806 /* TODO: Implement lcc_putnotif */
808 int lcc_listval (lcc_connection_t *c, /* {{{ */
809 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
810 {
811 lcc_response_t res;
812 size_t i;
813 int status;
815 lcc_identifier_t *ident;
816 size_t ident_num;
818 if (c == NULL)
819 return (-1);
821 if ((ret_ident == NULL) || (ret_ident_num == NULL))
822 {
823 lcc_set_errno (c, EINVAL);
824 return (-1);
825 }
827 status = lcc_sendreceive (c, "LISTVAL", &res);
828 if (status != 0)
829 return (status);
831 if (res.status != 0)
832 {
833 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
834 lcc_response_free (&res);
835 return (-1);
836 }
838 ident_num = res.lines_num;
839 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
840 if (ident == NULL)
841 {
842 lcc_response_free (&res);
843 lcc_set_errno (c, ENOMEM);
844 return (-1);
845 }
847 for (i = 0; i < res.lines_num; i++)
848 {
849 char *time_str;
850 char *ident_str;
852 /* First field is the time. */
853 time_str = res.lines[i];
855 /* Set `ident_str' to the beginning of the second field. */
856 ident_str = time_str;
857 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
858 ident_str++;
859 while ((*ident_str == ' ') || (*ident_str == '\t'))
860 {
861 *ident_str = 0;
862 ident_str++;
863 }
865 if (*ident_str == 0)
866 {
867 lcc_set_errno (c, EPROTO);
868 status = -1;
869 break;
870 }
872 status = lcc_string_to_identifier (c, ident + i, ident_str);
873 if (status != 0)
874 break;
875 }
877 lcc_response_free (&res);
879 if (status != 0)
880 {
881 free (ident);
882 return (-1);
883 }
885 *ret_ident = ident;
886 *ret_ident_num = ident_num;
888 return (0);
889 } /* }}} int lcc_listval */
891 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
892 {
893 if (c == NULL)
894 return ("Invalid object");
895 return (c->errbuf);
896 } /* }}} const char *lcc_strerror */
898 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
899 char *string, size_t string_size, const lcc_identifier_t *ident)
900 {
901 if ((string == NULL) || (string_size < 6) || (ident == NULL))
902 {
903 lcc_set_errno (c, EINVAL);
904 return (-1);
905 }
907 if (ident->plugin_instance[0] == 0)
908 {
909 if (ident->type_instance[0] == 0)
910 snprintf (string, string_size, "%s/%s/%s",
911 ident->host,
912 ident->plugin,
913 ident->type);
914 else
915 snprintf (string, string_size, "%s/%s/%s-%s",
916 ident->host,
917 ident->plugin,
918 ident->type,
919 ident->type_instance);
920 }
921 else
922 {
923 if (ident->type_instance[0] == 0)
924 snprintf (string, string_size, "%s/%s-%s/%s",
925 ident->host,
926 ident->plugin,
927 ident->plugin_instance,
928 ident->type);
929 else
930 snprintf (string, string_size, "%s/%s-%s/%s-%s",
931 ident->host,
932 ident->plugin,
933 ident->plugin_instance,
934 ident->type,
935 ident->type_instance);
936 }
938 string[string_size - 1] = 0;
939 return (0);
940 } /* }}} int lcc_identifier_to_string */
942 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
943 lcc_identifier_t *ident, const char *string)
944 {
945 char *string_copy;
946 char *host;
947 char *plugin;
948 char *plugin_instance;
949 char *type;
950 char *type_instance;
952 string_copy = lcc_strdup (string);
953 if (string_copy == NULL)
954 {
955 lcc_set_errno (c, ENOMEM);
956 return (-1);
957 }
959 host = string_copy;
960 plugin = strchr (host, '/');
961 if (plugin == NULL)
962 {
963 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
964 free (string_copy);
965 return (-1);
966 }
967 *plugin = 0;
968 plugin++;
970 type = strchr (plugin, '/');
971 if (type == NULL)
972 {
973 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
974 free (string_copy);
975 return (-1);
976 }
977 *type = 0;
978 type++;
980 plugin_instance = strchr (plugin, '-');
981 if (plugin_instance != NULL)
982 {
983 *plugin_instance = 0;
984 plugin_instance++;
985 }
987 type_instance = strchr (type, '-');
988 if (type_instance != NULL)
989 {
990 *type_instance = 0;
991 type_instance++;
992 }
994 memset (ident, 0, sizeof (*ident));
996 SSTRCPY (ident->host, host);
997 SSTRCPY (ident->plugin, plugin);
998 if (plugin_instance != NULL)
999 SSTRCPY (ident->plugin_instance, plugin_instance);
1000 SSTRCPY (ident->type, type);
1001 if (type_instance != NULL)
1002 SSTRCPY (ident->type_instance, type_instance);
1004 free (string_copy);
1005 return (0);
1006 } /* }}} int lcc_string_to_identifier */
1008 /* vim: set sw=2 sts=2 et fdm=marker : */