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