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