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