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