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