c5be3b90cec373ad1d6469fb2a9e1faa0d5e5154
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 strncat ((d), (s), sizeof (d)); \
88 (d)[sizeof (d) - 1] = 0; \
89 } while (0)
91 #define SSTRCATF(d, ...) do { \
92 char _b[sizeof (d)]; \
93 snprintf (_b, sizeof (_b), __VA_ARGS__); \
94 _b[sizeof (_b) - 1] = 0; \
95 SSTRCAT ((d), _b); \
96 } while (0)
99 #define LCC_SET_ERRSTR(c, ...) do { \
100 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
101 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
102 } while (0)
104 #if COLLECT_DEBUG
105 # define LCC_DEBUG(...) printf (__VA_ARGS__)
106 #else
107 # define LCC_DEBUG(...) /**/
108 #endif
110 /*
111 * Types
112 */
113 struct lcc_connection_s
114 {
115 FILE *fh;
116 char errbuf[1024];
117 };
119 struct lcc_response_s
120 {
121 int status;
122 char message[1024];
123 char **lines;
124 size_t lines_num;
125 };
126 typedef struct lcc_response_s lcc_response_t;
128 /*
129 * Private functions
130 */
131 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
132 {
133 if (c == NULL)
134 return (-1);
136 strerror_r (err, c->errbuf, sizeof (c->errbuf));
137 c->errbuf[sizeof (c->errbuf) - 1] = 0;
139 return (0);
140 } /* }}} int lcc_set_errno */
142 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
143 * here. */
144 __attribute__((malloc, nonnull (1)))
145 static char *lcc_strdup (const char *str) /* {{{ */
146 {
147 size_t strsize;
148 char *ret;
150 strsize = strlen (str) + 1;
151 ret = (char *) malloc (strsize);
152 if (ret != NULL)
153 memcpy (ret, str, strsize);
154 return (ret);
155 } /* }}} char *lcc_strdup */
157 __attribute__((nonnull (1, 2)))
158 static char *lcc_strescape (char *dest, const char *src, size_t dest_size) /* {{{ */
159 {
160 size_t dest_pos;
161 size_t src_pos;
163 dest_pos = 0;
164 src_pos = 0;
166 assert (dest_size >= 3);
168 dest[dest_pos] = '"';
169 dest_pos++;
171 while (42)
172 {
173 if ((dest_pos == (dest_size - 2))
174 || (src[src_pos] == 0))
175 break;
177 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
178 {
179 /* Check if there is enough space for both characters.. */
180 if (dest_pos == (dest_size - 3))
181 break;
183 dest[dest_pos] = '\\';
184 dest_pos++;
185 }
187 dest[dest_pos] = src[src_pos];
188 dest_pos++;
189 src_pos++;
190 }
192 assert (dest_pos <= (dest_size - 2));
194 dest[dest_pos] = '"';
195 dest_pos++;
197 dest[dest_pos] = 0;
198 dest_pos++;
199 src_pos++;
201 return (dest);
202 } /* }}} char *lcc_strescape */
204 /* lcc_chomp: Removes all control-characters at the end of a string. */
205 static void lcc_chomp (char *str) /* {{{ */
206 {
207 size_t str_len;
209 str_len = strlen (str);
210 while (str_len > 0)
211 {
212 if (str[str_len - 1] >= 32)
213 break;
214 str[str_len - 1] = 0;
215 str_len--;
216 }
217 } /* }}} void lcc_chomp */
219 static void lcc_response_free (lcc_response_t *res) /* {{{ */
220 {
221 size_t i;
223 if (res == NULL)
224 return;
226 for (i = 0; i < res->lines_num; i++)
227 free (res->lines[i]);
228 free (res->lines);
229 res->lines = NULL;
230 } /* }}} void lcc_response_free */
232 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
233 {
234 int status;
236 LCC_DEBUG ("send: --> %s\n", command);
238 status = fprintf (c->fh, "%s\r\n", command);
239 if (status < 0)
240 {
241 lcc_set_errno (c, errno);
242 return (-1);
243 }
245 return (0);
246 } /* }}} int lcc_send */
248 static int lcc_receive (lcc_connection_t *c, /* {{{ */
249 lcc_response_t *ret_res)
250 {
251 lcc_response_t res;
252 char *ptr;
253 char buffer[4096];
254 size_t i;
256 memset (&res, 0, sizeof (res));
258 /* Read the first line, containing the status and a message */
259 ptr = fgets (buffer, sizeof (buffer), c->fh);
260 if (ptr == NULL)
261 {
262 lcc_set_errno (c, errno);
263 return (-1);
264 }
265 lcc_chomp (buffer);
266 LCC_DEBUG ("receive: <-- %s\n", buffer);
268 /* Convert the leading status to an integer and make `ptr' to point to the
269 * beginning of the message. */
270 ptr = NULL;
271 errno = 0;
272 res.status = strtol (buffer, &ptr, 0);
273 if ((errno != 0) || (ptr == &buffer[0]))
274 {
275 lcc_set_errno (c, errno);
276 return (-1);
277 }
279 /* Skip white spaces after the status number */
280 while ((*ptr == ' ') || (*ptr == '\t'))
281 ptr++;
283 /* Now copy the message. */
284 strncpy (res.message, ptr, sizeof (res.message));
285 res.message[sizeof (res.message) - 1] = 0;
287 /* Error or no lines follow: We're done. */
288 if (res.status <= 0)
289 {
290 memcpy (ret_res, &res, sizeof (res));
291 return (0);
292 }
294 /* Allocate space for the char-pointers */
295 res.lines_num = (size_t) res.status;
296 res.status = 0;
297 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
298 if (res.lines == NULL)
299 {
300 lcc_set_errno (c, ENOMEM);
301 return (-1);
302 }
304 /* Now receive all the lines */
305 for (i = 0; i < res.lines_num; i++)
306 {
307 ptr = fgets (buffer, sizeof (buffer), c->fh);
308 if (ptr == NULL)
309 {
310 lcc_set_errno (c, errno);
311 break;
312 }
313 lcc_chomp (buffer);
314 LCC_DEBUG ("receive: <-- %s\n", buffer);
316 res.lines[i] = lcc_strdup (buffer);
317 if (res.lines[i] == NULL)
318 {
319 lcc_set_errno (c, ENOMEM);
320 break;
321 }
322 }
324 /* Check if the for-loop exited with an error. */
325 if (i < res.lines_num)
326 {
327 while (i > 0)
328 {
329 i--;
330 free (res.lines[i]);
331 }
332 free (res.lines);
333 return (-1);
334 }
336 memcpy (ret_res, &res, sizeof (res));
337 return (0);
338 } /* }}} int lcc_receive */
340 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
341 const char *command, lcc_response_t *ret_res)
342 {
343 lcc_response_t res;
344 int status;
346 status = lcc_send (c, command);
347 if (status != 0)
348 return (status);
350 memset (&res, 0, sizeof (res));
351 status = lcc_receive (c, &res);
352 if (status == 0)
353 memcpy (ret_res, &res, sizeof (*ret_res));
355 return (status);
356 } /* }}} int lcc_sendreceive */
358 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
359 {
360 struct sockaddr_un sa;
361 int fd;
362 int status;
364 assert (c != NULL);
365 assert (c->fh == NULL);
366 assert (path != NULL);
368 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
369 * others). */
370 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
371 if (fd < 0)
372 {
373 lcc_set_errno (c, errno);
374 return (-1);
375 }
377 memset (&sa, 0, sizeof (sa));
378 sa.sun_family = AF_UNIX;
379 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
381 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
382 if (status != 0)
383 {
384 lcc_set_errno (c, errno);
385 close (fd);
386 return (-1);
387 }
389 c->fh = fdopen (fd, "r+");
390 if (c->fh == NULL)
391 {
392 lcc_set_errno (c, errno);
393 close (fd);
394 return (-1);
395 }
397 return (0);
398 } /* }}} int lcc_open_unixsocket */
400 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
401 const char *addr_orig)
402 {
403 struct addrinfo ai_hints;
404 struct addrinfo *ai_res;
405 struct addrinfo *ai_ptr;
406 char addr_copy[NI_MAXHOST];
407 char *addr;
408 char *port;
409 int fd;
410 int status;
412 assert (c != NULL);
413 assert (c->fh == NULL);
414 assert (addr_orig != NULL);
416 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
417 addr_copy[sizeof(addr_copy) - 1] = '\0';
418 addr = addr_copy;
420 memset (&ai_hints, 0, sizeof (ai_hints));
421 ai_hints.ai_flags = 0;
422 #ifdef AI_ADDRCONFIG
423 ai_hints.ai_flags |= AI_ADDRCONFIG;
424 #endif
425 ai_hints.ai_family = AF_UNSPEC;
426 ai_hints.ai_socktype = SOCK_STREAM;
428 port = NULL;
429 if (*addr == '[') /* IPv6+port format */
430 {
431 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
432 addr++;
434 port = strchr (addr, ']');
435 if (port == NULL)
436 {
437 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
438 return (-1);
439 }
440 *port = 0;
441 port++;
443 if (*port == ':')
444 port++;
445 else if (*port == 0)
446 port = NULL;
447 else
448 {
449 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
450 return (-1);
451 }
452 } /* if (*addr = ']') */
453 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
454 {
455 port = strrchr (addr, ':');
456 if (port != NULL)
457 {
458 *port = 0;
459 port++;
460 }
461 }
463 ai_res = NULL;
464 status = getaddrinfo (addr,
465 port == NULL ? LCC_DEFAULT_PORT : port,
466 &ai_hints, &ai_res);
467 if (status != 0)
468 {
469 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
470 return (-1);
471 }
473 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
474 {
475 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
476 if (fd < 0)
477 {
478 status = errno;
479 fd = -1;
480 continue;
481 }
483 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
484 if (status != 0)
485 {
486 status = errno;
487 close (fd);
488 fd = -1;
489 continue;
490 }
492 c->fh = fdopen (fd, "r+");
493 if (c->fh == NULL)
494 {
495 status = errno;
496 close (fd);
497 fd = -1;
498 continue;
499 }
501 assert (status == 0);
502 break;
503 } /* for (ai_ptr) */
505 if (status != 0)
506 {
507 lcc_set_errno (c, status);
508 return (-1);
509 }
511 return (0);
512 } /* }}} int lcc_open_netsocket */
514 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
515 {
516 int status = 0;
518 if (addr == NULL)
519 return (-1);
521 assert (c != NULL);
522 assert (c->fh == NULL);
523 assert (addr != NULL);
525 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
526 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
527 else if (addr[0] == '/')
528 status = lcc_open_unixsocket (c, addr);
529 else
530 status = lcc_open_netsocket (c, addr);
532 return (status);
533 } /* }}} int lcc_open_socket */
535 /*
536 * Public functions
537 */
538 unsigned int lcc_version (void) /* {{{ */
539 {
540 return (LCC_VERSION);
541 } /* }}} unsigned int lcc_version */
543 const char *lcc_version_string (void) /* {{{ */
544 {
545 return (LCC_VERSION_STRING);
546 } /* }}} const char *lcc_version_string */
548 const char *lcc_version_extra (void) /* {{{ */
549 {
550 return (LCC_VERSION_EXTRA);
551 } /* }}} const char *lcc_version_extra */
553 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
554 {
555 lcc_connection_t *c;
557 if (address == NULL)
558 return (-1);
560 if (ret_con == NULL)
561 return (-1);
563 c = (lcc_connection_t *) malloc (sizeof (*c));
564 if (c == NULL)
565 return (-1);
566 memset (c, 0, sizeof (*c));
568 *ret_con = c;
569 return (lcc_open_socket (c, address));
570 } /* }}} int lcc_connect */
572 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
573 {
574 if (c == NULL)
575 return (-1);
577 if (c->fh != NULL)
578 {
579 fclose (c->fh);
580 c->fh = NULL;
581 }
583 free (c);
584 return (0);
585 } /* }}} int lcc_disconnect */
587 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
588 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
589 {
590 char ident_str[6 * LCC_NAME_LEN];
591 char ident_esc[12 * LCC_NAME_LEN];
592 char command[14 * LCC_NAME_LEN];
594 lcc_response_t res;
595 size_t values_num;
596 gauge_t *values = NULL;
597 char **values_names = NULL;
599 size_t i;
600 int status;
602 if (c == NULL)
603 return (-1);
605 if (ident == NULL)
606 {
607 lcc_set_errno (c, EINVAL);
608 return (-1);
609 }
611 /* Build a commend with an escaped version of the identifier string. */
612 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
613 if (status != 0)
614 return (status);
616 snprintf (command, sizeof (command), "GETVAL %s",
617 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
618 command[sizeof (command) - 1] = 0;
620 /* Send talk to the daemon.. */
621 status = lcc_sendreceive (c, command, &res);
622 if (status != 0)
623 return (status);
625 if (res.status != 0)
626 {
627 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
628 lcc_response_free (&res);
629 return (-1);
630 }
632 values_num = res.lines_num;
634 #define BAIL_OUT(e) do { \
635 lcc_set_errno (c, (e)); \
636 free (values); \
637 if (values_names != NULL) { \
638 for (i = 0; i < values_num; i++) { \
639 free (values_names[i]); \
640 } \
641 } \
642 free (values_names); \
643 lcc_response_free (&res); \
644 return (-1); \
645 } while (0)
647 /* If neither the values nor the names are requested, return here.. */
648 if ((ret_values == NULL) && (ret_values_names == NULL))
649 {
650 if (ret_values_num != NULL)
651 *ret_values_num = values_num;
652 lcc_response_free (&res);
653 return (0);
654 }
656 /* Allocate space for the values */
657 if (ret_values != NULL)
658 {
659 values = (gauge_t *) malloc (values_num * sizeof (*values));
660 if (values == NULL)
661 BAIL_OUT (ENOMEM);
662 }
664 if (ret_values_names != NULL)
665 {
666 values_names = (char **) calloc (values_num, sizeof (*values_names));
667 if (values_names == NULL)
668 BAIL_OUT (ENOMEM);
669 }
671 for (i = 0; i < res.lines_num; i++)
672 {
673 char *key;
674 char *value;
675 char *endptr;
677 key = res.lines[i];
678 value = strchr (key, '=');
679 if (value == NULL)
680 BAIL_OUT (EPROTO);
682 *value = 0;
683 value++;
685 if (values != NULL)
686 {
687 endptr = NULL;
688 errno = 0;
689 values[i] = strtod (value, &endptr);
691 if ((endptr == value) || (errno != 0))
692 BAIL_OUT (errno);
693 }
695 if (values_names != NULL)
696 {
697 values_names[i] = lcc_strdup (key);
698 if (values_names[i] == NULL)
699 BAIL_OUT (ENOMEM);
700 }
701 } /* for (i = 0; i < res.lines_num; i++) */
703 if (ret_values_num != NULL)
704 *ret_values_num = values_num;
705 if (ret_values != NULL)
706 *ret_values = values;
707 if (ret_values_names != NULL)
708 *ret_values_names = values_names;
710 return (0);
711 } /* }}} int lcc_getval */
713 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
714 {
715 char ident_str[6 * LCC_NAME_LEN];
716 char ident_esc[12 * LCC_NAME_LEN];
717 char command[1024] = "";
718 lcc_response_t res;
719 int status;
720 size_t i;
722 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
723 || (vl->values == NULL) || (vl->values_types == NULL))
724 {
725 lcc_set_errno (c, EINVAL);
726 return (-1);
727 }
729 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
730 &vl->identifier);
731 if (status != 0)
732 return (status);
734 SSTRCATF (command, "PUTVAL %s",
735 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
737 if (vl->interval > 0)
738 SSTRCATF (command, " interval=%i", vl->interval);
740 if (vl->time > 0)
741 SSTRCATF (command, "%u", (unsigned int) vl->time);
742 else
743 SSTRCAT (command, "N");
745 for (i = 0; i < vl->values_len; i++)
746 {
747 if (vl->values_types[i] == LCC_TYPE_COUNTER)
748 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
749 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
750 {
751 if (isnan (vl->values[i].gauge))
752 SSTRCPY (command, ":U");
753 else
754 SSTRCATF (command, ":%g", vl->values[i].gauge);
755 }
756 } /* for (i = 0; i < vl->values_len; i++) */
758 status = lcc_sendreceive (c, command, &res);
759 if (status != 0)
760 return (status);
762 if (res.status != 0)
763 {
764 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
765 lcc_response_free (&res);
766 return (-1);
767 }
769 lcc_response_free (&res);
770 return (0);
771 } /* }}} int lcc_putval */
773 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
774 lcc_identifier_t *ident, int timeout)
775 {
776 char command[1024];
777 lcc_response_t res;
778 int status;
780 if (c == NULL)
781 {
782 lcc_set_errno (c, EINVAL);
783 return (-1);
784 }
786 SSTRCPY (command, "FLUSH");
788 if (timeout > 0)
789 SSTRCATF (command, " timeout=%i", timeout);
791 if (plugin != NULL)
792 {
793 char buffer[2 * LCC_NAME_LEN];
794 SSTRCATF (command, " plugin=%s",
795 lcc_strescape (buffer, plugin, sizeof (buffer)));
796 }
798 if (ident != NULL)
799 {
800 char ident_str[6 * LCC_NAME_LEN];
801 char ident_esc[12 * LCC_NAME_LEN];
803 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
804 if (status != 0)
805 return (status);
807 SSTRCATF (command, " identifier=%s",
808 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
809 }
811 status = lcc_sendreceive (c, command, &res);
812 if (status != 0)
813 return (status);
815 if (res.status != 0)
816 {
817 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
818 lcc_response_free (&res);
819 return (-1);
820 }
822 lcc_response_free (&res);
823 return (0);
824 } /* }}} int lcc_flush */
826 /* TODO: Implement lcc_putnotif */
828 int lcc_listval (lcc_connection_t *c, /* {{{ */
829 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
830 {
831 lcc_response_t res;
832 size_t i;
833 int status;
835 lcc_identifier_t *ident;
836 size_t ident_num;
838 if (c == NULL)
839 return (-1);
841 if ((ret_ident == NULL) || (ret_ident_num == NULL))
842 {
843 lcc_set_errno (c, EINVAL);
844 return (-1);
845 }
847 status = lcc_sendreceive (c, "LISTVAL", &res);
848 if (status != 0)
849 return (status);
851 if (res.status != 0)
852 {
853 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
854 lcc_response_free (&res);
855 return (-1);
856 }
858 ident_num = res.lines_num;
859 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
860 if (ident == NULL)
861 {
862 lcc_response_free (&res);
863 lcc_set_errno (c, ENOMEM);
864 return (-1);
865 }
867 for (i = 0; i < res.lines_num; i++)
868 {
869 char *time_str;
870 char *ident_str;
872 /* First field is the time. */
873 time_str = res.lines[i];
875 /* Set `ident_str' to the beginning of the second field. */
876 ident_str = time_str;
877 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
878 ident_str++;
879 while ((*ident_str == ' ') || (*ident_str == '\t'))
880 {
881 *ident_str = 0;
882 ident_str++;
883 }
885 if (*ident_str == 0)
886 {
887 lcc_set_errno (c, EPROTO);
888 status = -1;
889 break;
890 }
892 status = lcc_string_to_identifier (c, ident + i, ident_str);
893 if (status != 0)
894 break;
895 }
897 lcc_response_free (&res);
899 if (status != 0)
900 {
901 free (ident);
902 return (-1);
903 }
905 *ret_ident = ident;
906 *ret_ident_num = ident_num;
908 return (0);
909 } /* }}} int lcc_listval */
911 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
912 {
913 if (c == NULL)
914 return ("Invalid object");
915 return (c->errbuf);
916 } /* }}} const char *lcc_strerror */
918 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
919 char *string, size_t string_size, const lcc_identifier_t *ident)
920 {
921 if ((string == NULL) || (string_size < 6) || (ident == NULL))
922 {
923 lcc_set_errno (c, EINVAL);
924 return (-1);
925 }
927 if (ident->plugin_instance[0] == 0)
928 {
929 if (ident->type_instance[0] == 0)
930 snprintf (string, string_size, "%s/%s/%s",
931 ident->host,
932 ident->plugin,
933 ident->type);
934 else
935 snprintf (string, string_size, "%s/%s/%s-%s",
936 ident->host,
937 ident->plugin,
938 ident->type,
939 ident->type_instance);
940 }
941 else
942 {
943 if (ident->type_instance[0] == 0)
944 snprintf (string, string_size, "%s/%s-%s/%s",
945 ident->host,
946 ident->plugin,
947 ident->plugin_instance,
948 ident->type);
949 else
950 snprintf (string, string_size, "%s/%s-%s/%s-%s",
951 ident->host,
952 ident->plugin,
953 ident->plugin_instance,
954 ident->type,
955 ident->type_instance);
956 }
958 string[string_size - 1] = 0;
959 return (0);
960 } /* }}} int lcc_identifier_to_string */
962 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
963 lcc_identifier_t *ident, const char *string)
964 {
965 char *string_copy;
966 char *host;
967 char *plugin;
968 char *plugin_instance;
969 char *type;
970 char *type_instance;
972 string_copy = lcc_strdup (string);
973 if (string_copy == NULL)
974 {
975 lcc_set_errno (c, ENOMEM);
976 return (-1);
977 }
979 host = string_copy;
980 plugin = strchr (host, '/');
981 if (plugin == NULL)
982 {
983 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
984 free (string_copy);
985 return (-1);
986 }
987 *plugin = 0;
988 plugin++;
990 type = strchr (plugin, '/');
991 if (type == NULL)
992 {
993 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
994 free (string_copy);
995 return (-1);
996 }
997 *type = 0;
998 type++;
1000 plugin_instance = strchr (plugin, '-');
1001 if (plugin_instance != NULL)
1002 {
1003 *plugin_instance = 0;
1004 plugin_instance++;
1005 }
1007 type_instance = strchr (type, '-');
1008 if (type_instance != NULL)
1009 {
1010 *type_instance = 0;
1011 type_instance++;
1012 }
1014 memset (ident, 0, sizeof (*ident));
1016 SSTRCPY (ident->host, host);
1017 SSTRCPY (ident->plugin, plugin);
1018 if (plugin_instance != NULL)
1019 SSTRCPY (ident->plugin_instance, plugin_instance);
1020 SSTRCPY (ident->type, type);
1021 if (type_instance != NULL)
1022 SSTRCPY (ident->type_instance, type_instance);
1024 free (string_copy);
1025 return (0);
1026 } /* }}} int lcc_string_to_identifier */
1028 /* vim: set sw=2 sts=2 et fdm=marker : */