bb7ee95628506b99470fc99885e7009433978703
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 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #if !defined(__GNUC__) || !__GNUC__
27 # define __attribute__(x) /**/
28 #endif
30 #include "lcc_features.h"
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <string.h>
39 #include <assert.h>
40 #include <errno.h>
41 #include <math.h>
42 #include <netdb.h>
44 #include "client.h"
46 /* NI_MAXHOST has been obsoleted by RFC 3493 which is a reason for SunOS 5.11
47 * to no longer define it. We'll use the old, RFC 2553 value here. */
48 #ifndef NI_MAXHOST
49 # define NI_MAXHOST 1025
50 #endif
52 /* OpenBSD doesn't have EPROTO, FreeBSD doesn't have EILSEQ. Oh what joy! */
53 #ifndef EILSEQ
54 # ifdef EPROTO
55 # define EILSEQ EPROTO
56 # else
57 # define EILSEQ EINVAL
58 # endif
59 #endif
61 /* Secure/static macros. They work like `strcpy' and `strcat', but assure null
62 * termination. They work for static buffers only, because they use `sizeof'.
63 * The `SSTRCATF' combines the functionality of `snprintf' and `strcat' which
64 * is very useful to add formatted stuff to the end of a buffer. */
65 #define SSTRCPY(d,s) do { \
66 strncpy ((d), (s), sizeof (d)); \
67 (d)[sizeof (d) - 1] = 0; \
68 } while (0)
70 #define SSTRCAT(d,s) do { \
71 size_t _l = strlen (d); \
72 strncpy ((d) + _l, (s), sizeof (d) - _l); \
73 (d)[sizeof (d) - 1] = 0; \
74 } while (0)
76 #define SSTRCATF(d, ...) do { \
77 char _b[sizeof (d)]; \
78 snprintf (_b, sizeof (_b), __VA_ARGS__); \
79 _b[sizeof (_b) - 1] = 0; \
80 SSTRCAT ((d), _b); \
81 } while (0)
84 #define LCC_SET_ERRSTR(c, ...) do { \
85 snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
86 (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
87 } while (0)
89 #if COLLECT_DEBUG
90 # define LCC_DEBUG(...) printf (__VA_ARGS__)
91 #else
92 # define LCC_DEBUG(...) /**/
93 #endif
95 /*
96 * Types
97 */
98 struct lcc_connection_s
99 {
100 FILE *fh;
101 char errbuf[1024];
102 };
104 struct lcc_response_s
105 {
106 int status;
107 char message[1024];
108 char **lines;
109 size_t lines_num;
110 };
111 typedef struct lcc_response_s lcc_response_t;
113 /*
114 * Private functions
115 */
116 /* Even though Posix requires "strerror_r" to return an "int",
117 * some systems (e.g. the GNU libc) return a "char *" _and_
118 * ignore the second argument ... -tokkee */
119 char *sstrerror (int errnum, char *buf, size_t buflen)
120 {
121 buf[0] = 0;
123 #if !HAVE_STRERROR_R
124 snprintf (buf, buflen, "Error #%i; strerror_r is not available.", errnum);
125 /* #endif !HAVE_STRERROR_R */
127 #elif STRERROR_R_CHAR_P
128 {
129 char *temp;
130 temp = strerror_r (errnum, buf, buflen);
131 if (buf[0] == 0)
132 {
133 if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
134 strncpy (buf, temp, buflen);
135 else
136 strncpy (buf, "strerror_r did not return "
137 "an error message", buflen);
138 }
139 }
140 /* #endif STRERROR_R_CHAR_P */
142 #else
143 if (strerror_r (errnum, buf, buflen) != 0)
144 {
145 snprintf (buf, buflen, "Error #%i; "
146 "Additionally, strerror_r failed.",
147 errnum);
148 }
149 #endif /* STRERROR_R_CHAR_P */
151 buf[buflen - 1] = 0;
153 return (buf);
154 } /* char *sstrerror */
156 static int lcc_set_errno (lcc_connection_t *c, int err) /* {{{ */
157 {
158 if (c == NULL)
159 return (-1);
161 sstrerror (err, c->errbuf, sizeof (c->errbuf));
162 c->errbuf[sizeof (c->errbuf) - 1] = 0;
164 return (0);
165 } /* }}} int lcc_set_errno */
167 /* lcc_strdup: Since `strdup' is an XSI extension, we provide our own version
168 * here. */
169 __attribute__((malloc, nonnull (1)))
170 static char *lcc_strdup (const char *str) /* {{{ */
171 {
172 size_t strsize;
173 char *ret;
175 strsize = strlen (str) + 1;
176 ret = (char *) malloc (strsize);
177 if (ret != NULL)
178 memcpy (ret, str, strsize);
179 return (ret);
180 } /* }}} char *lcc_strdup */
182 __attribute__((nonnull (1, 2)))
183 static char *lcc_strescape (char *dest, const char *src, size_t dest_size) /* {{{ */
184 {
185 size_t dest_pos;
186 size_t src_pos;
188 dest_pos = 0;
189 src_pos = 0;
191 assert (dest_size >= 3);
193 dest[dest_pos] = '"';
194 dest_pos++;
196 while (42)
197 {
198 if ((dest_pos == (dest_size - 2))
199 || (src[src_pos] == 0))
200 break;
202 if ((src[src_pos] == '"') || (src[src_pos] == '\\'))
203 {
204 /* Check if there is enough space for both characters.. */
205 if (dest_pos == (dest_size - 3))
206 break;
208 dest[dest_pos] = '\\';
209 dest_pos++;
210 }
212 dest[dest_pos] = src[src_pos];
213 dest_pos++;
214 src_pos++;
215 }
217 assert (dest_pos <= (dest_size - 2));
219 dest[dest_pos] = '"';
220 dest_pos++;
222 dest[dest_pos] = 0;
223 dest_pos++;
224 src_pos++;
226 return (dest);
227 } /* }}} char *lcc_strescape */
229 /* lcc_chomp: Removes all control-characters at the end of a string. */
230 static void lcc_chomp (char *str) /* {{{ */
231 {
232 size_t str_len;
234 str_len = strlen (str);
235 while (str_len > 0)
236 {
237 if (str[str_len - 1] >= 32)
238 break;
239 str[str_len - 1] = 0;
240 str_len--;
241 }
242 } /* }}} void lcc_chomp */
244 static void lcc_response_free (lcc_response_t *res) /* {{{ */
245 {
246 size_t i;
248 if (res == NULL)
249 return;
251 for (i = 0; i < res->lines_num; i++)
252 free (res->lines[i]);
253 free (res->lines);
254 res->lines = NULL;
255 } /* }}} void lcc_response_free */
257 static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */
258 {
259 int status;
261 LCC_DEBUG ("send: --> %s\n", command);
263 status = fprintf (c->fh, "%s\r\n", command);
264 if (status < 0)
265 {
266 lcc_set_errno (c, errno);
267 return (-1);
268 }
270 return (0);
271 } /* }}} int lcc_send */
273 static int lcc_receive (lcc_connection_t *c, /* {{{ */
274 lcc_response_t *ret_res)
275 {
276 lcc_response_t res;
277 char *ptr;
278 char buffer[4096];
279 size_t i;
281 memset (&res, 0, sizeof (res));
283 /* Read the first line, containing the status and a message */
284 ptr = fgets (buffer, sizeof (buffer), c->fh);
285 if (ptr == NULL)
286 {
287 lcc_set_errno (c, errno);
288 return (-1);
289 }
290 lcc_chomp (buffer);
291 LCC_DEBUG ("receive: <-- %s\n", buffer);
293 /* Convert the leading status to an integer and make `ptr' to point to the
294 * beginning of the message. */
295 ptr = NULL;
296 errno = 0;
297 res.status = strtol (buffer, &ptr, 0);
298 if ((errno != 0) || (ptr == &buffer[0]))
299 {
300 lcc_set_errno (c, errno);
301 return (-1);
302 }
304 /* Skip white spaces after the status number */
305 while ((*ptr == ' ') || (*ptr == '\t'))
306 ptr++;
308 /* Now copy the message. */
309 strncpy (res.message, ptr, sizeof (res.message));
310 res.message[sizeof (res.message) - 1] = 0;
312 /* Error or no lines follow: We're done. */
313 if (res.status <= 0)
314 {
315 memcpy (ret_res, &res, sizeof (res));
316 return (0);
317 }
319 /* Allocate space for the char-pointers */
320 res.lines_num = (size_t) res.status;
321 res.status = 0;
322 res.lines = (char **) malloc (res.lines_num * sizeof (char *));
323 if (res.lines == NULL)
324 {
325 lcc_set_errno (c, ENOMEM);
326 return (-1);
327 }
329 /* Now receive all the lines */
330 for (i = 0; i < res.lines_num; i++)
331 {
332 ptr = fgets (buffer, sizeof (buffer), c->fh);
333 if (ptr == NULL)
334 {
335 lcc_set_errno (c, errno);
336 break;
337 }
338 lcc_chomp (buffer);
339 LCC_DEBUG ("receive: <-- %s\n", buffer);
341 res.lines[i] = lcc_strdup (buffer);
342 if (res.lines[i] == NULL)
343 {
344 lcc_set_errno (c, ENOMEM);
345 break;
346 }
347 }
349 /* Check if the for-loop exited with an error. */
350 if (i < res.lines_num)
351 {
352 while (i > 0)
353 {
354 i--;
355 free (res.lines[i]);
356 }
357 free (res.lines);
358 return (-1);
359 }
361 memcpy (ret_res, &res, sizeof (res));
362 return (0);
363 } /* }}} int lcc_receive */
365 static int lcc_sendreceive (lcc_connection_t *c, /* {{{ */
366 const char *command, lcc_response_t *ret_res)
367 {
368 lcc_response_t res;
369 int status;
371 if (c->fh == NULL)
372 {
373 lcc_set_errno (c, EBADF);
374 return (-1);
375 }
377 status = lcc_send (c, command);
378 if (status != 0)
379 return (status);
381 memset (&res, 0, sizeof (res));
382 status = lcc_receive (c, &res);
383 if (status == 0)
384 memcpy (ret_res, &res, sizeof (*ret_res));
386 return (status);
387 } /* }}} int lcc_sendreceive */
389 static int lcc_open_unixsocket (lcc_connection_t *c, const char *path) /* {{{ */
390 {
391 struct sockaddr_un sa;
392 int fd;
393 int status;
395 assert (c != NULL);
396 assert (c->fh == NULL);
397 assert (path != NULL);
399 /* Don't use PF_UNIX here, because it's broken on Mac OS X (10.4, possibly
400 * others). */
401 fd = socket (AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
402 if (fd < 0)
403 {
404 lcc_set_errno (c, errno);
405 return (-1);
406 }
408 memset (&sa, 0, sizeof (sa));
409 sa.sun_family = AF_UNIX;
410 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
412 status = connect (fd, (struct sockaddr *) &sa, sizeof (sa));
413 if (status != 0)
414 {
415 lcc_set_errno (c, errno);
416 close (fd);
417 return (-1);
418 }
420 c->fh = fdopen (fd, "r+");
421 if (c->fh == NULL)
422 {
423 lcc_set_errno (c, errno);
424 close (fd);
425 return (-1);
426 }
428 return (0);
429 } /* }}} int lcc_open_unixsocket */
431 static int lcc_open_netsocket (lcc_connection_t *c, /* {{{ */
432 const char *addr_orig)
433 {
434 struct addrinfo ai_hints;
435 struct addrinfo *ai_res;
436 struct addrinfo *ai_ptr;
437 char addr_copy[NI_MAXHOST];
438 char *addr;
439 char *port;
440 int fd;
441 int status;
443 assert (c != NULL);
444 assert (c->fh == NULL);
445 assert (addr_orig != NULL);
447 strncpy(addr_copy, addr_orig, sizeof(addr_copy));
448 addr_copy[sizeof(addr_copy) - 1] = '\0';
449 addr = addr_copy;
451 memset (&ai_hints, 0, sizeof (ai_hints));
452 ai_hints.ai_flags = 0;
453 #ifdef AI_ADDRCONFIG
454 ai_hints.ai_flags |= AI_ADDRCONFIG;
455 #endif
456 ai_hints.ai_family = AF_UNSPEC;
457 ai_hints.ai_socktype = SOCK_STREAM;
459 port = NULL;
460 if (*addr == '[') /* IPv6+port format */
461 {
462 /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
463 addr++;
465 port = strchr (addr, ']');
466 if (port == NULL)
467 {
468 LCC_SET_ERRSTR (c, "malformed address: %s", addr_orig);
469 return (-1);
470 }
471 *port = 0;
472 port++;
474 if (*port == ':')
475 port++;
476 else if (*port == 0)
477 port = NULL;
478 else
479 {
480 LCC_SET_ERRSTR (c, "garbage after address: %s", port);
481 return (-1);
482 }
483 } /* if (*addr = ']') */
484 else if (strchr (addr, '.') != NULL) /* Hostname or IPv4 */
485 {
486 port = strrchr (addr, ':');
487 if (port != NULL)
488 {
489 *port = 0;
490 port++;
491 }
492 }
494 ai_res = NULL;
495 status = getaddrinfo (addr,
496 port == NULL ? LCC_DEFAULT_PORT : port,
497 &ai_hints, &ai_res);
498 if (status != 0)
499 {
500 LCC_SET_ERRSTR (c, "getaddrinfo: %s", gai_strerror (status));
501 return (-1);
502 }
504 for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
505 {
506 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
507 if (fd < 0)
508 {
509 status = errno;
510 fd = -1;
511 continue;
512 }
514 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
515 if (status != 0)
516 {
517 status = errno;
518 close (fd);
519 fd = -1;
520 continue;
521 }
523 c->fh = fdopen (fd, "r+");
524 if (c->fh == NULL)
525 {
526 status = errno;
527 close (fd);
528 fd = -1;
529 continue;
530 }
532 assert (status == 0);
533 break;
534 } /* for (ai_ptr) */
536 if (status != 0)
537 {
538 lcc_set_errno (c, status);
539 return (-1);
540 }
542 return (0);
543 } /* }}} int lcc_open_netsocket */
545 static int lcc_open_socket (lcc_connection_t *c, const char *addr) /* {{{ */
546 {
547 int status = 0;
549 if (addr == NULL)
550 return (-1);
552 assert (c != NULL);
553 assert (c->fh == NULL);
554 assert (addr != NULL);
556 if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
557 status = lcc_open_unixsocket (c, addr + strlen ("unix:"));
558 else if (addr[0] == '/')
559 status = lcc_open_unixsocket (c, addr);
560 else
561 status = lcc_open_netsocket (c, addr);
563 return (status);
564 } /* }}} int lcc_open_socket */
566 /*
567 * Public functions
568 */
569 unsigned int lcc_version (void) /* {{{ */
570 {
571 return (LCC_VERSION);
572 } /* }}} unsigned int lcc_version */
574 const char *lcc_version_string (void) /* {{{ */
575 {
576 return (LCC_VERSION_STRING);
577 } /* }}} const char *lcc_version_string */
579 const char *lcc_version_extra (void) /* {{{ */
580 {
581 return (LCC_VERSION_EXTRA);
582 } /* }}} const char *lcc_version_extra */
584 int lcc_connect (const char *address, lcc_connection_t **ret_con) /* {{{ */
585 {
586 lcc_connection_t *c;
587 int status;
589 if (address == NULL)
590 return (-1);
592 if (ret_con == NULL)
593 return (-1);
595 c = (lcc_connection_t *) malloc (sizeof (*c));
596 if (c == NULL)
597 return (-1);
598 memset (c, 0, sizeof (*c));
600 status = lcc_open_socket (c, address);
601 if (status != 0)
602 {
603 lcc_disconnect (c);
604 return (status);
605 }
607 *ret_con = c;
608 return (0);
609 } /* }}} int lcc_connect */
611 int lcc_disconnect (lcc_connection_t *c) /* {{{ */
612 {
613 if (c == NULL)
614 return (-1);
616 if (c->fh != NULL)
617 {
618 fclose (c->fh);
619 c->fh = NULL;
620 }
622 free (c);
623 return (0);
624 } /* }}} int lcc_disconnect */
626 int lcc_getval (lcc_connection_t *c, lcc_identifier_t *ident, /* {{{ */
627 size_t *ret_values_num, gauge_t **ret_values, char ***ret_values_names)
628 {
629 char ident_str[6 * LCC_NAME_LEN];
630 char ident_esc[12 * LCC_NAME_LEN];
631 char command[14 * LCC_NAME_LEN];
633 lcc_response_t res;
634 size_t values_num;
635 gauge_t *values = NULL;
636 char **values_names = NULL;
638 size_t i;
639 int status;
641 if (c == NULL)
642 return (-1);
644 if (ident == NULL)
645 {
646 lcc_set_errno (c, EINVAL);
647 return (-1);
648 }
650 /* Build a commend with an escaped version of the identifier string. */
651 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
652 if (status != 0)
653 return (status);
655 snprintf (command, sizeof (command), "GETVAL %s",
656 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
657 command[sizeof (command) - 1] = 0;
659 /* Send talk to the daemon.. */
660 status = lcc_sendreceive (c, command, &res);
661 if (status != 0)
662 return (status);
664 if (res.status != 0)
665 {
666 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
667 lcc_response_free (&res);
668 return (-1);
669 }
671 values_num = res.lines_num;
673 #define BAIL_OUT(e) do { \
674 lcc_set_errno (c, (e)); \
675 free (values); \
676 if (values_names != NULL) { \
677 for (i = 0; i < values_num; i++) { \
678 free (values_names[i]); \
679 } \
680 } \
681 free (values_names); \
682 lcc_response_free (&res); \
683 return (-1); \
684 } while (0)
686 /* If neither the values nor the names are requested, return here.. */
687 if ((ret_values == NULL) && (ret_values_names == NULL))
688 {
689 if (ret_values_num != NULL)
690 *ret_values_num = values_num;
691 lcc_response_free (&res);
692 return (0);
693 }
695 /* Allocate space for the values */
696 if (ret_values != NULL)
697 {
698 values = (gauge_t *) malloc (values_num * sizeof (*values));
699 if (values == NULL)
700 BAIL_OUT (ENOMEM);
701 }
703 if (ret_values_names != NULL)
704 {
705 values_names = (char **) calloc (values_num, sizeof (*values_names));
706 if (values_names == NULL)
707 BAIL_OUT (ENOMEM);
708 }
710 for (i = 0; i < res.lines_num; i++)
711 {
712 char *key;
713 char *value;
714 char *endptr;
716 key = res.lines[i];
717 value = strchr (key, '=');
718 if (value == NULL)
719 BAIL_OUT (EILSEQ);
721 *value = 0;
722 value++;
724 if (values != NULL)
725 {
726 endptr = NULL;
727 errno = 0;
728 values[i] = strtod (value, &endptr);
730 if ((endptr == value) || (errno != 0))
731 BAIL_OUT (errno);
732 }
734 if (values_names != NULL)
735 {
736 values_names[i] = lcc_strdup (key);
737 if (values_names[i] == NULL)
738 BAIL_OUT (ENOMEM);
739 }
740 } /* for (i = 0; i < res.lines_num; i++) */
742 if (ret_values_num != NULL)
743 *ret_values_num = values_num;
744 if (ret_values != NULL)
745 *ret_values = values;
746 if (ret_values_names != NULL)
747 *ret_values_names = values_names;
749 return (0);
750 } /* }}} int lcc_getval */
752 int lcc_putval (lcc_connection_t *c, const lcc_value_list_t *vl) /* {{{ */
753 {
754 char ident_str[6 * LCC_NAME_LEN];
755 char ident_esc[12 * LCC_NAME_LEN];
756 char command[1024] = "";
757 lcc_response_t res;
758 int status;
759 size_t i;
761 if ((c == NULL) || (vl == NULL) || (vl->values_len < 1)
762 || (vl->values == NULL) || (vl->values_types == NULL))
763 {
764 lcc_set_errno (c, EINVAL);
765 return (-1);
766 }
768 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str),
769 &vl->identifier);
770 if (status != 0)
771 return (status);
773 SSTRCATF (command, "PUTVAL %s",
774 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
776 if (vl->interval > 0)
777 SSTRCATF (command, " interval=%i", vl->interval);
779 if (vl->time > 0)
780 SSTRCATF (command, "%u", (unsigned int) vl->time);
781 else
782 SSTRCAT (command, "N");
784 for (i = 0; i < vl->values_len; i++)
785 {
786 if (vl->values_types[i] == LCC_TYPE_COUNTER)
787 SSTRCATF (command, ":%"PRIu64, vl->values[i].counter);
788 else if (vl->values_types[i] == LCC_TYPE_GAUGE)
789 {
790 if (isnan (vl->values[i].gauge))
791 SSTRCPY (command, ":U");
792 else
793 SSTRCATF (command, ":%g", vl->values[i].gauge);
794 }
795 else if (vl->values_types[i] == LCC_TYPE_DERIVE)
796 SSTRCATF (command, ":%"PRIu64, vl->values[i].derive);
797 else if (vl->values_types[i] == LCC_TYPE_ABSOLUTE)
798 SSTRCATF (command, ":%"PRIu64, vl->values[i].absolute);
800 } /* for (i = 0; i < vl->values_len; i++) */
802 status = lcc_sendreceive (c, command, &res);
803 if (status != 0)
804 return (status);
806 if (res.status != 0)
807 {
808 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
809 lcc_response_free (&res);
810 return (-1);
811 }
813 lcc_response_free (&res);
814 return (0);
815 } /* }}} int lcc_putval */
817 int lcc_flush (lcc_connection_t *c, const char *plugin, /* {{{ */
818 lcc_identifier_t *ident, int timeout)
819 {
820 char command[1024] = "";
821 lcc_response_t res;
822 int status;
824 if (c == NULL)
825 {
826 lcc_set_errno (c, EINVAL);
827 return (-1);
828 }
830 SSTRCPY (command, "FLUSH");
832 if (timeout > 0)
833 SSTRCATF (command, " timeout=%i", timeout);
835 if (plugin != NULL)
836 {
837 char buffer[2 * LCC_NAME_LEN];
838 SSTRCATF (command, " plugin=%s",
839 lcc_strescape (buffer, plugin, sizeof (buffer)));
840 }
842 if (ident != NULL)
843 {
844 char ident_str[6 * LCC_NAME_LEN];
845 char ident_esc[12 * LCC_NAME_LEN];
847 status = lcc_identifier_to_string (c, ident_str, sizeof (ident_str), ident);
848 if (status != 0)
849 return (status);
851 SSTRCATF (command, " identifier=%s",
852 lcc_strescape (ident_esc, ident_str, sizeof (ident_esc)));
853 }
855 status = lcc_sendreceive (c, command, &res);
856 if (status != 0)
857 return (status);
859 if (res.status != 0)
860 {
861 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
862 lcc_response_free (&res);
863 return (-1);
864 }
866 lcc_response_free (&res);
867 return (0);
868 } /* }}} int lcc_flush */
870 /* TODO: Implement lcc_putnotif */
872 int lcc_listval (lcc_connection_t *c, /* {{{ */
873 lcc_identifier_t **ret_ident, size_t *ret_ident_num)
874 {
875 lcc_response_t res;
876 size_t i;
877 int status;
879 lcc_identifier_t *ident;
880 size_t ident_num;
882 if (c == NULL)
883 return (-1);
885 if ((ret_ident == NULL) || (ret_ident_num == NULL))
886 {
887 lcc_set_errno (c, EINVAL);
888 return (-1);
889 }
891 status = lcc_sendreceive (c, "LISTVAL", &res);
892 if (status != 0)
893 return (status);
895 if (res.status != 0)
896 {
897 LCC_SET_ERRSTR (c, "Server error: %s", res.message);
898 lcc_response_free (&res);
899 return (-1);
900 }
902 ident_num = res.lines_num;
903 ident = (lcc_identifier_t *) malloc (ident_num * sizeof (*ident));
904 if (ident == NULL)
905 {
906 lcc_response_free (&res);
907 lcc_set_errno (c, ENOMEM);
908 return (-1);
909 }
911 for (i = 0; i < res.lines_num; i++)
912 {
913 char *time_str;
914 char *ident_str;
916 /* First field is the time. */
917 time_str = res.lines[i];
919 /* Set `ident_str' to the beginning of the second field. */
920 ident_str = time_str;
921 while ((*ident_str != ' ') && (*ident_str != '\t') && (*ident_str != 0))
922 ident_str++;
923 while ((*ident_str == ' ') || (*ident_str == '\t'))
924 {
925 *ident_str = 0;
926 ident_str++;
927 }
929 if (*ident_str == 0)
930 {
931 lcc_set_errno (c, EILSEQ);
932 status = -1;
933 break;
934 }
936 status = lcc_string_to_identifier (c, ident + i, ident_str);
937 if (status != 0)
938 break;
939 }
941 lcc_response_free (&res);
943 if (status != 0)
944 {
945 free (ident);
946 return (-1);
947 }
949 *ret_ident = ident;
950 *ret_ident_num = ident_num;
952 return (0);
953 } /* }}} int lcc_listval */
955 const char *lcc_strerror (lcc_connection_t *c) /* {{{ */
956 {
957 if (c == NULL)
958 return ("Invalid object");
959 return (c->errbuf);
960 } /* }}} const char *lcc_strerror */
962 int lcc_identifier_to_string (lcc_connection_t *c, /* {{{ */
963 char *string, size_t string_size, const lcc_identifier_t *ident)
964 {
965 if ((string == NULL) || (string_size < 6) || (ident == NULL))
966 {
967 lcc_set_errno (c, EINVAL);
968 return (-1);
969 }
971 if (ident->plugin_instance[0] == 0)
972 {
973 if (ident->type_instance[0] == 0)
974 snprintf (string, string_size, "%s/%s/%s",
975 ident->host,
976 ident->plugin,
977 ident->type);
978 else
979 snprintf (string, string_size, "%s/%s/%s-%s",
980 ident->host,
981 ident->plugin,
982 ident->type,
983 ident->type_instance);
984 }
985 else
986 {
987 if (ident->type_instance[0] == 0)
988 snprintf (string, string_size, "%s/%s-%s/%s",
989 ident->host,
990 ident->plugin,
991 ident->plugin_instance,
992 ident->type);
993 else
994 snprintf (string, string_size, "%s/%s-%s/%s-%s",
995 ident->host,
996 ident->plugin,
997 ident->plugin_instance,
998 ident->type,
999 ident->type_instance);
1000 }
1002 string[string_size - 1] = 0;
1003 return (0);
1004 } /* }}} int lcc_identifier_to_string */
1006 int lcc_string_to_identifier (lcc_connection_t *c, /* {{{ */
1007 lcc_identifier_t *ident, const char *string)
1008 {
1009 char *string_copy;
1010 char *host;
1011 char *plugin;
1012 char *plugin_instance;
1013 char *type;
1014 char *type_instance;
1016 string_copy = lcc_strdup (string);
1017 if (string_copy == NULL)
1018 {
1019 lcc_set_errno (c, ENOMEM);
1020 return (-1);
1021 }
1023 host = string_copy;
1024 plugin = strchr (host, '/');
1025 if (plugin == NULL)
1026 {
1027 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1028 free (string_copy);
1029 return (-1);
1030 }
1031 *plugin = 0;
1032 plugin++;
1034 type = strchr (plugin, '/');
1035 if (type == NULL)
1036 {
1037 LCC_SET_ERRSTR (c, "Malformed identifier string: %s", string);
1038 free (string_copy);
1039 return (-1);
1040 }
1041 *type = 0;
1042 type++;
1044 plugin_instance = strchr (plugin, '-');
1045 if (plugin_instance != NULL)
1046 {
1047 *plugin_instance = 0;
1048 plugin_instance++;
1049 }
1051 type_instance = strchr (type, '-');
1052 if (type_instance != NULL)
1053 {
1054 *type_instance = 0;
1055 type_instance++;
1056 }
1058 memset (ident, 0, sizeof (*ident));
1060 SSTRCPY (ident->host, host);
1061 SSTRCPY (ident->plugin, plugin);
1062 if (plugin_instance != NULL)
1063 SSTRCPY (ident->plugin_instance, plugin_instance);
1064 SSTRCPY (ident->type, type);
1065 if (type_instance != NULL)
1066 SSTRCPY (ident->type_instance, type_instance);
1068 free (string_copy);
1069 return (0);
1070 } /* }}} int lcc_string_to_identifier */
1072 /* vim: set sw=2 sts=2 et fdm=marker : */