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