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