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