d35c30c1e5687da44979157ece95c806aac9b347
[routeros-api.git] / src / main.c
1 /**
2  * librouteros - src/main.c
3  * Copyright (C) 2009  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 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
24 #endif
25
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
28 #endif
29
30 #include "config.h"
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdint.h>
36 #include <inttypes.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <assert.h>
40
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netdb.h>
44
45 #include <gcrypt.h>
46
47 #include "routeros_api.h"
48
49 #if WITH_DEBUG
50 # define ros_debug(...) fprintf (stdout, __VA_ARGS__)
51 #else
52 # define ros_debug(...) /**/
53 #endif
54
55 #if !__GNUC__
56 # define __attribute__(x) /**/
57 #endif
58
59 /* FIXME */
60 char *strdup (const char *);
61
62 /*
63  * Private structures
64  */
65 struct ros_connection_s
66 {
67         int fd;
68 };
69
70 struct ros_reply_s
71 {
72         unsigned int params_num;
73         char *status;
74         char **keys;
75         char **values;
76
77         ros_reply_t *next;
78 };
79
80 struct ros_login_data_s
81 {
82         const char *username;
83         const char *password;
84 };
85 typedef struct ros_login_data_s ros_login_data_t;
86
87 /*
88  * Private functions
89  */
90 static int read_exact (int fd, void *buffer, size_t buffer_size) /* {{{ */
91 {
92         char *buffer_ptr;
93         size_t have_bytes;
94
95         if ((fd < 0) || (buffer == NULL))
96                 return (EINVAL);
97
98         if (buffer_size == 0)
99                 return (0);
100
101         buffer_ptr = buffer;
102         have_bytes = 0;
103         while (have_bytes < buffer_size)
104         {
105                 size_t want_bytes;
106                 ssize_t status;
107
108                 want_bytes = buffer_size - have_bytes;
109                 errno = 0;
110                 status = read (fd, buffer_ptr, want_bytes);
111                 if (status < 0)
112                 {
113                         if (errno == EAGAIN)
114                                 continue;
115                         else
116                                 return (status);
117                 }
118
119                 assert (((size_t) status) <= want_bytes);
120                 have_bytes += status;
121                 buffer_ptr += status;
122         }
123
124         return (0);
125 } /* }}} int read_exact */
126
127 static ros_reply_t *reply_alloc (void) /* {{{ */
128 {
129         ros_reply_t *r;
130
131         r = malloc (sizeof (*r));
132         if (r == NULL)
133                 return (NULL);
134
135         memset (r, 0, sizeof (*r));
136         r->keys = NULL;
137         r->values = NULL;
138         r->next = NULL;
139
140         return (r);
141 } /* }}} ros_reply_s *reply_alloc */
142
143 static int reply_add_keyval (ros_reply_t *r, const char *key, /* {{{ */
144                 const char *val)
145 {
146         char **tmp;
147
148         tmp = realloc (r->keys, (r->params_num + 1) * sizeof (*tmp));
149         if (tmp == NULL)
150                 return (ENOMEM);
151         r->keys = tmp;
152
153         tmp = realloc (r->values, (r->params_num + 1) * sizeof (*tmp));
154         if (tmp == NULL)
155                 return (ENOMEM);
156         r->values = tmp;
157
158         r->keys[r->params_num] = strdup (key);
159         if (r->keys[r->params_num] == NULL)
160                 return (ENOMEM);
161
162         r->values[r->params_num] = strdup (val);
163         if (r->values[r->params_num] == NULL)
164         {
165                 free (r->keys[r->params_num]);
166                 r->keys[r->params_num] = NULL;
167                 return (ENOMEM);
168         }
169
170         r->params_num++;
171         return (0);
172 } /* }}} int reply_add_keyval */
173
174 #if WITH_DEBUG
175 static void reply_dump (const ros_reply_t *r) /* {{{ */
176 {
177         if (r == NULL)
178                 return;
179
180         printf ("=== BEGIN REPLY ===\n"
181                         "Address: %p\n"
182                         "Status: %s\n",
183                         (void *) r, r->status);
184         if (r->params_num > 0)
185         {
186                 unsigned int i;
187
188                 printf ("Arguments:\n");
189                 for (i = 0; i < r->params_num; i++)
190                         printf (" %3u: %s = %s\n", i, r->keys[i], r->values[i]);
191         }
192         if (r->next != NULL)
193                 printf ("Next: %p\n", (void *) r->next);
194         printf ("=== END REPLY ===\n");
195
196         reply_dump (r->next);
197 } /* }}} void reply_dump */
198 #else
199 # define reply_dump(foo) /**/
200 #endif
201
202 static void reply_free (ros_reply_t *r) /* {{{ */
203 {
204         ros_reply_t *next;
205         unsigned int i;
206
207         if (r == NULL)
208                 return;
209
210         next = r->next;
211
212         for (i = 0; i < r->params_num; i++)
213         {
214                 free (r->keys[i]);
215                 free (r->values[i]);
216         }
217
218         free (r->keys);
219         free (r->values);
220
221         free (r);
222
223         reply_free (next);
224 } /* }}} void reply_free */
225
226 static int buffer_init (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
227 {
228         if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
229                 return (EINVAL);
230
231         if (*ret_buffer_size < 1)
232                 return (EINVAL);
233
234         return (0);
235 } /* }}} int buffer_init */
236
237 static int buffer_add (char **ret_buffer, size_t *ret_buffer_size, /* {{{ */
238                 const char *string)
239 {
240         char *buffer;
241         size_t buffer_size;
242         size_t string_size;
243         size_t req_size;
244
245         if ((ret_buffer == NULL) || (ret_buffer_size == NULL) || (string == NULL))
246                 return (EINVAL);
247
248         buffer = *ret_buffer;
249         buffer_size = *ret_buffer_size;
250
251         string_size = strlen (string);
252         if (string_size == 0)
253                 return (EINVAL);
254
255         if (string_size >= 0x10000000)
256                 req_size = 5 + string_size;
257         else if (string_size >= 0x200000)
258                 req_size = 4 + string_size;
259         else if (string_size >= 0x4000)
260                 req_size = 3 + string_size;
261         else if (string_size >= 0x80)
262                 req_size = 2 + string_size;
263         else
264                 req_size = 1 + string_size;
265
266         if (*ret_buffer_size < req_size)
267                 return (ENOMEM);
268
269         if (string_size >= 0x10000000)
270         {
271                 buffer[0] = 0xF0;
272                 buffer[1] = (string_size >> 24) & 0xff;
273                 buffer[2] = (string_size >> 16) & 0xff;
274                 buffer[3] = (string_size >>  8) & 0xff;
275                 buffer[4] = (string_size      ) & 0xff;
276                 buffer += 5;
277                 buffer_size -= 5;
278         }
279         else if (string_size >= 0x200000)
280         {
281                 buffer[0] = (string_size >> 24) & 0x1f;
282                 buffer[0] |= 0xE0;
283                 buffer[1] = (string_size >> 16) & 0xff;
284                 buffer[2] = (string_size >>  8) & 0xff;
285                 buffer[3] = (string_size      ) & 0xff;
286                 buffer += 4;
287                 buffer_size -= 4;
288         }
289         else if (string_size >= 0x4000)
290         {
291                 buffer[0] = (string_size >> 16) & 0x3f;
292                 buffer[0] |= 0xC0;
293                 buffer[1] = (string_size >>  8) & 0xff;
294                 buffer[2] = (string_size      ) & 0xff;
295                 buffer += 3;
296                 buffer_size -= 3;
297         }
298         else if (string_size >= 0x80)
299         {
300                 buffer[0] = (string_size >>  8) & 0x7f;
301                 buffer[0] |= 0x80;
302                 buffer[1] = (string_size      ) & 0xff;
303                 buffer += 2;
304                 buffer_size -= 2;
305         }
306         else /* if (string_size <= 0x7f) */
307         {
308                 buffer[0] = (char) string_size;
309                 buffer += 1;
310                 buffer_size -= 1;
311         }
312
313         assert (buffer_size >= string_size);
314         memcpy (buffer, string, string_size);
315         buffer += string_size;
316         buffer_size -= string_size;
317
318         *ret_buffer = buffer;
319         *ret_buffer_size = buffer_size;
320
321         return (0);
322 } /* }}} int buffer_add */
323
324 static int buffer_end (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
325 {
326         if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
327                 return (EINVAL);
328
329         if (*ret_buffer_size < 1)
330                 return (EINVAL);
331
332         /* Add empty word. */
333         (*ret_buffer)[0] = 0;
334         (*ret_buffer)++;
335         (*ret_buffer_size)--;
336
337         return (0);
338 } /* }}} int buffer_end */
339
340 static int send_command (ros_connection_t *c, /* {{{ */
341                 const char *command,
342                 size_t args_num, const char * const *args)
343 {
344         char buffer[4096];
345         char *buffer_ptr;
346         size_t buffer_size;
347
348         size_t i;
349         int status;
350
351         /* FIXME: For debugging only */
352         memset (buffer, 0, sizeof (buffer));
353
354         buffer_ptr = buffer;
355         buffer_size = sizeof (buffer);
356
357         status = buffer_init (&buffer_ptr, &buffer_size);
358         if (status != 0)
359                 return (status);
360
361         ros_debug ("send_command: command = %s;\n", command);
362         status = buffer_add (&buffer_ptr, &buffer_size, command);
363         if (status != 0)
364                 return (status);
365
366         for (i = 0; i < args_num; i++)
367         {
368                 if (args[i] == NULL)
369                         return (EINVAL);
370
371                 ros_debug ("send_command: arg[%zu] = %s;\n", i, args[i]);
372                 status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
373                 if (status != 0)
374                         return (status);
375         }
376
377         status = buffer_end (&buffer_ptr, &buffer_size);
378         if (status != 0)
379                 return (status);
380
381         buffer_ptr = buffer;
382         buffer_size = sizeof (buffer) - buffer_size;
383         while (buffer_size > 0)
384         {
385                 ssize_t bytes_written;
386
387                 errno = 0;
388                 bytes_written = write (c->fd, buffer_ptr, buffer_size);
389                 if (bytes_written < 0)
390                 {
391                         if (errno == EAGAIN)
392                                 continue;
393                         else
394                                 return (errno);
395                 }
396                 assert (((size_t) bytes_written) <= buffer_size);
397
398                 buffer_ptr += bytes_written;
399                 buffer_size -= bytes_written;
400         } /* while (buffer_size > 0) */
401
402         return (0);
403 } /* }}} int send_command */
404
405 static int read_word (ros_connection_t *c, /* {{{ */
406                 char *buffer, size_t *buffer_size)
407 {
408         size_t req_size;
409         uint8_t word_length[5];
410         int status;
411
412         if ((buffer == NULL) || (*buffer_size < 1))
413                 return (EINVAL);
414
415         /* read one byte from the socket */
416         status = read_exact (c->fd, word_length, 1);
417         if (status != 0)
418                 return (status);
419
420         /* Calculate `req_size' */
421         if (((unsigned char) word_length[0]) == 0xF0) /* {{{ */
422         {
423                 status = read_exact (c->fd, &word_length[1], 4);
424                 if (status != 0)
425                         return (status);
426
427                 req_size = (word_length[1] << 24)
428                         | (word_length[2] << 16)
429                         | (word_length[3] << 8)
430                         | word_length[4];
431         }
432         else if ((word_length[0] & 0xE0) == 0xE0)
433         {
434                 status = read_exact (c->fd, &word_length[1], 3);
435                 if (status != 0)
436                         return (status);
437
438                 req_size = ((word_length[0] & 0x1F) << 24)
439                         | (word_length[1] << 16)
440                         | (word_length[2] << 8)
441                         | word_length[3];
442         }
443         else if ((word_length[0] & 0xC0) == 0xC0)
444         {
445                 status = read_exact (c->fd, &word_length[1], 2);
446                 if (status != 0)
447                         return (status);
448
449                 req_size = ((word_length[0] & 0x3F) << 16)
450                         | (word_length[1] << 8)
451                         | word_length[2];
452         }
453         else if ((word_length[0] & 0x80) == 0x80)
454         {
455                 status = read_exact (c->fd, &word_length[1], 1);
456                 if (status != 0)
457                         return (status);
458
459                 req_size = ((word_length[0] & 0x7F) << 8)
460                         | word_length[1];
461         }
462         else if ((word_length[0] & 0x80) == 0)
463         {
464                 req_size = (size_t) word_length[0];
465         }
466         else
467         {
468                 /* First nibble is `F' but second nibble is not `0'. */
469                 return (EPROTO);
470         } /* }}} */
471
472         if (*buffer_size < req_size)
473                 return (ENOMEM);
474
475         /* Empty word. This ends a `sentence' and must therefore be valid. */
476         if (req_size == 0)
477         {
478                 buffer[0] = 0;
479                 *buffer_size = 0;
480                 return (0);
481         }
482
483         status = read_exact (c->fd, buffer, req_size);
484         if (status != 0)
485                 return (status);
486         *buffer_size = req_size;
487
488         return (0);
489 } /* }}} int buffer_decode_next */
490
491 static ros_reply_t *receive_sentence (ros_connection_t *c) /* {{{ */
492 {
493         char buffer[4096];
494         size_t buffer_size;
495         int status;
496
497         ros_reply_t *r;
498
499         r = reply_alloc ();
500         if (r == NULL)
501                 return (NULL);
502
503         while (42)
504         {
505                 buffer_size = sizeof (buffer) - 1;
506                 status = read_word (c, buffer, &buffer_size);
507                 if (status != 0)
508                         break;
509                 assert (buffer_size < sizeof (buffer));
510                 buffer[buffer_size] = 0;
511
512                 /* Empty word means end of reply */
513                 if (buffer_size == 0)
514                         break;
515
516                 if (buffer[0] == '!') /* {{{ */
517                 {
518                         if (r->status != NULL)
519                                 free (r->status);
520                         r->status = strdup (&buffer[1]);
521                         if (r->status == NULL)
522                                 break;
523                 } /* }}} if (buffer[0] == '!') */
524                 else if (buffer[0] == '=') /* {{{ */
525                 {
526                         char *key = &buffer[1];
527                         char *val;
528
529                         key = &buffer[1];
530                         val = strchr (key, '=');
531                         if (val == NULL)
532                         {
533                                 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
534                                 continue;
535                         }
536                         *val = 0;
537                         val++;
538
539                         reply_add_keyval (r, key, val);
540                 } /* }}} if (buffer[0] == '=') */
541                 else
542                 {
543                         ros_debug ("receive_sentence: Ignoring unknown word: %s\n", buffer);
544                 }
545         } /* while (42) */
546         
547         if (r->status == NULL)
548         {
549                 reply_free (r);
550                 return (NULL);
551         }
552
553         return (r);
554 } /* }}} ros_reply_t *receive_sentence */
555
556 static ros_reply_t *receive_reply (ros_connection_t *c) /* {{{ */
557 {
558         ros_reply_t *head;
559         ros_reply_t *tail;
560
561         head = NULL;
562         tail = NULL;
563
564         while (42)
565         {
566                 ros_reply_t *tmp;
567
568                 tmp = receive_sentence (c);
569                 if (tmp == NULL)
570                         break;
571
572                 if (tail == NULL)
573                 {
574                         head = tmp;
575                         tail = tmp;
576                 }
577                 else
578                 {
579                         tail->next = tmp;
580                         tail = tmp;
581                 }
582
583                 if (strcmp ("done", tmp->status) == 0)
584                         break;
585         } /* while (42) */
586         
587         return (head);
588 } /* }}} ros_reply_t *receive_reply */
589
590 static int create_socket (const char *node, const char *service) /* {{{ */
591 {
592         struct addrinfo  ai_hint;
593         struct addrinfo *ai_list;
594         struct addrinfo *ai_ptr;
595         int status;
596
597         ros_debug ("create_socket (node = %s, service = %s);\n",
598                         node, service);
599
600         memset (&ai_hint, 0, sizeof (ai_hint));
601 #ifdef AI_ADDRCONFIG
602         ai_hint.ai_flags |= AI_ADDRCONFIG;
603 #endif
604         ai_hint.ai_family = AF_UNSPEC;
605         ai_hint.ai_socktype = SOCK_STREAM;
606
607         ai_list = NULL;
608         status = getaddrinfo (node, service, &ai_hint, &ai_list);
609         if (status != 0)
610                 return (-1);
611         assert (ai_list != NULL);
612
613         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
614         {
615                 int fd;
616                 int status;
617
618                 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
619                                 ai_ptr->ai_protocol);
620                 if (fd < 0)
621                 {
622                         ros_debug ("create_socket: socket(2) failed.\n");
623                         continue;
624                 }
625
626                 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
627                 if (status != 0)
628                 {
629                         ros_debug ("create_socket: connect(2) failed.\n");
630                         close (fd);
631                         continue;
632                 }
633
634                 return (fd);
635         }
636
637         freeaddrinfo (ai_list);
638
639         return (-1);
640 } /* }}} int create_socket */
641
642 static int login2_handler (__attribute__((unused)) ros_connection_t *c, /* {{{ */
643                 const ros_reply_t *r,
644                 __attribute__((unused)) void *user_data)
645 {
646         if (r == NULL)
647                 return (EINVAL);
648
649         reply_dump (r);
650
651         if (strcmp (r->status, "trap") == 0)
652         {
653                 ros_debug ("login2_handler: Logging in failed: %s.\n",
654                                 ros_reply_param_val_by_key (r, "message"));
655                 return (EACCES);
656         }
657         else if (strcmp (r->status, "done") != 0)
658         {
659                 ros_debug ("login2_handler: Unexpected status: %s.\n", r->status);
660                 return (EPROTO);
661         }
662
663         return (0);
664 } /* }}} int login2_handler */
665
666 static void hash_binary_to_hex (char hex[33], uint8_t binary[16]) /* {{{ */
667 {
668         int i;
669
670         for (i = 0; i < 16; i++)
671         {
672                 char tmp[3];
673                 snprintf (tmp, 3, "%02"PRIx8, binary[i]);
674                 tmp[2] = 0;
675                 hex[2*i] = tmp[0];
676                 hex[2*i+1] = tmp[1];
677         }
678         hex[32] = 0;
679 } /* }}} void hash_binary_to_hex */
680
681 static void hash_hex_to_binary (uint8_t binary[16], char hex[33]) /* {{{ */
682 {
683         int i;
684
685         for (i = 0; i < 16; i++)
686         {
687                 char tmp[3];
688
689                 tmp[0] = hex[2*i];
690                 tmp[1] = hex[2*i + 1];
691                 tmp[2] = 0;
692
693                 binary[i] = (uint8_t) strtoul (tmp, /* endptr = */ NULL, /* base = */ 16);
694         }
695 } /* }}} void hash_hex_to_binary */
696
697 static void make_password_hash (char response_hex[33], /* {{{ */
698                 const char *password, size_t password_length, char challenge_hex[33])
699 {
700         uint8_t challenge_bin[16];
701         uint8_t response_bin[16];
702         char data_buffer[password_length+17];
703         gcry_md_hd_t md_handle;
704
705         hash_hex_to_binary (challenge_bin, challenge_hex);
706
707         data_buffer[0] = 0;
708         memcpy (&data_buffer[1], password, password_length);
709         memcpy (&data_buffer[1+password_length], challenge_bin, 16);
710
711         gcry_md_open (&md_handle, GCRY_MD_MD5, /* flags = */ 0);
712         gcry_md_write (md_handle, data_buffer, sizeof (data_buffer));
713         memcpy (response_bin, gcry_md_read (md_handle, GCRY_MD_MD5), 16);
714         gcry_md_close (md_handle);
715
716         hash_binary_to_hex (response_hex, response_bin);
717 } /* }}} void make_password_hash */
718
719 static int login_handler (ros_connection_t *c, const ros_reply_t *r, /* {{{ */
720                 void *user_data)
721 {
722         const char *ret;
723         char challenge_hex[33];
724         char response_hex[33];
725         ros_login_data_t *login_data;
726
727         const char *params[2];
728         char param_name[1024];
729         char param_response[64];
730
731         if (r == NULL)
732                 return (EINVAL);
733
734         /* The expected result looks like this:
735          * -- 8< --
736          *  !done 
737          *  =ret=ebddd18303a54111e2dea05a92ab46b4
738          * -- >8 --
739          */
740         reply_dump (r);
741
742         if (strcmp (r->status, "done") != 0)
743         {
744                 ros_debug ("login_handler: Unexpected status: %s.\n", r->status);
745                 return (EPROTO);
746         }
747
748         login_data = user_data;
749         if (login_data == NULL)
750                 return (EINVAL);
751
752         ret = ros_reply_param_val_by_key (r, "ret");
753         if (ret == NULL)
754         {
755                 ros_debug ("login_handler: Reply does not have parameter \"ret\".\n");
756                 return (EPROTO);
757         }
758         ros_debug ("login_handler: ret = %s;\n", ret);
759
760         if (strlen (ret) != 32)
761         {
762                 ros_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
763                 return (EPROTO);
764         }
765         strcpy (challenge_hex, ret);
766
767         make_password_hash (response_hex, 
768                         login_data->password, strlen (login_data->password),
769                         challenge_hex);
770
771         snprintf (param_name, sizeof (param_name), "=name=%s", login_data->username);
772         snprintf (param_response, sizeof (param_response),
773                         "=response=00%s", response_hex);
774         params[0] = param_name;
775         params[1] = param_response;
776
777         return (ros_query (c, "/login", 2, params, login2_handler,
778                                 /* user data = */ NULL));
779 } /* }}} int login_handler */
780
781 /*
782  * Public functions
783  */
784 ros_connection_t *ros_connect (const char *node, const char *service, /* {{{ */
785                 const char *username, const char *password)
786 {
787         int fd;
788         ros_connection_t *c;
789         int status;
790         ros_login_data_t user_data;
791
792         if ((node == NULL) || (username == NULL) || (password == NULL))
793                 return (NULL);
794
795         fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
796         if (fd < 0)
797                 return (NULL);
798
799         c = malloc (sizeof (*c));
800         if (c == NULL)
801         {
802                 close (fd);
803                 return (NULL);
804         }
805         memset (c, 0, sizeof (*c));
806
807         c->fd = fd;
808
809         user_data.username = username;
810         user_data.password = password;
811         status = ros_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
812                         login_handler, &user_data);
813
814         if (status != 0)
815         {
816                 ros_disconnect (c);
817                 errno = status;
818                 return (NULL);
819         }
820
821         return (c);
822 } /* }}} ros_connection_t *ros_connect */
823
824 int ros_disconnect (ros_connection_t *c) /* {{{ */
825 {
826         if (c == NULL)
827                 return (EINVAL);
828
829         if (c->fd >= 0)
830         {
831                 close (c->fd);
832                 c->fd = -1;
833         }
834
835         free (c);
836
837         return (0);
838 } /* }}} int ros_disconnect */
839
840 int ros_query (ros_connection_t *c, /* {{{ */
841                 const char *command,
842                 size_t args_num, const char * const *args,
843                 ros_reply_handler_t handler, void *user_data)
844 {
845         int status;
846         ros_reply_t *r;
847
848         status = send_command (c, command, args_num, args);
849         if (status != 0)
850                 return (status);
851
852         r = receive_reply (c);
853         if (r == NULL)
854                 return (EPROTO);
855
856         /* Call the callback function with the data we received. */
857         status = (*handler) (c, r, user_data);
858
859         /* Free the allocated memory ... */
860         reply_free (r);
861
862         /* ... and return. */
863         return (status);
864 } /* }}} int ros_query */
865
866 const ros_reply_t *ros_reply_next (const ros_reply_t *r) /* {{{ */
867 {
868         if (r == NULL)
869                 return (NULL);
870
871         return (r->next);
872 } /* }}} ros_reply_t *ros_reply_next */
873
874 int ros_reply_num (const ros_reply_t *r) /* {{{ */
875 {
876         int ret;
877         const ros_reply_t *ptr;
878
879         ret = 0;
880         for (ptr = r; ptr != NULL; ptr = ptr->next)
881                 ret++;
882
883         return (ret);
884 } /* }}} int ros_reply_num */
885
886 const char *ros_reply_status (const ros_reply_t *r) /* {{{ */
887 {
888         if (r == NULL)
889                 return (NULL);
890         return (r->status);
891 } /* }}} char *ros_reply_status */
892
893 const char *ros_reply_param_key_by_index (const ros_reply_t *r, /* {{{ */
894                 unsigned int index)
895 {
896         if (r == NULL)
897                 return (NULL);
898
899         if (index >= r->params_num)
900                 return (NULL);
901
902         return (r->keys[index]);
903 } /* }}} char *ros_reply_param_key_by_index */
904
905 const char *ros_reply_param_val_by_index (const ros_reply_t *r, /* {{{ */
906                 unsigned int index)
907 {
908         if (r == NULL)
909                 return (NULL);
910
911         if (index >= r->params_num)
912                 return (NULL);
913
914         return (r->values[index]);
915 } /* }}} char *ros_reply_param_key_by_index */
916
917 const char *ros_reply_param_val_by_key (const ros_reply_t *r, /* {{{ */
918                 const char *key)
919 {
920         unsigned int i;
921
922         if ((r == NULL) || (key == NULL))
923                 return (NULL);
924
925         for (i = 0; i < r->params_num; i++)
926                 if (strcmp (r->keys[i], key) == 0)
927                         return (r->values[i]);
928
929         return (NULL);
930 } /* }}} char *ros_reply_param_val_by_key */
931
932 int ros_version (void) /* {{{ */
933 {
934         return (ROS_VERSION);
935 } /* }}} int ros_version */
936
937 const char *ros_version_string (void) /* {{{ */
938 {
939         return (ROS_VERSION_STRING);
940 } /* }}} char *ros_version_string */
941
942 /* vim: set ts=2 sw=2 noet fdm=marker : */