9232fd9b178822341fb350f45eafeb50ab0d6119
[routeros-api.git] / src / main.c
1 /**
2  * libmikrotik - 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 mt_debug(...) fprintf (stdout, __VA_ARGS__)
49 #else
50 # define mt_debug(...) /**/
51 #endif
52
53 /* FIXME */
54 char *strdup (const char *);
55
56 /*
57  * Private structures
58  */
59 struct mt_connection_s
60 {
61         int fd;
62 };
63
64 struct mt_reply_s
65 {
66         unsigned int params_num;
67         char *status;
68         char **keys;
69         char **values;
70
71         mt_reply_t *next;
72 };
73
74 struct mt_login_data_s
75 {
76         const char *username;
77         const char *password;
78 };
79 typedef struct mt_login_data_s mt_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 mt_reply_t *reply_alloc (void) /* {{{ */
122 {
123         mt_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 } /* }}} mt_reply_s *reply_alloc */
136
137 static int reply_add_keyval (mt_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 mt_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 (mt_reply_t *r) /* {{{ */
193 {
194         mt_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 (mt_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         mt_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                 mt_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 (mt_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 mt_reply_t *receive_sentence (mt_connection_t *c) /* {{{ */
482 {
483         char buffer[4096];
484         size_t buffer_size;
485         int status;
486
487         mt_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                         mt_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 } /* }}} mt_reply_t *receive_sentence */
545
546 static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
547 {
548         mt_reply_t *head;
549         mt_reply_t *tail;
550
551         head = NULL;
552         tail = NULL;
553
554         while (42)
555         {
556                 mt_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 } /* }}} mt_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         mt_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                         mt_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                         mt_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 (mt_connection_t *c, const mt_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, "done") != 0)
642         {
643                 mt_debug ("login2_handler: Unexpected status: %s.\n", r->status);
644                 return (EPROTO);
645         }
646
647         return (0);
648 } /* }}} int login2_handler */
649
650 static void hash_binary_to_hex (char hex[33], uint8_t binary[16]) /* {{{ */
651 {
652         int i;
653
654         for (i = 0; i < 16; i++)
655         {
656                 char tmp[3];
657                 snprintf (tmp, 3, "%02"PRIx8, binary[i]);
658                 tmp[2] = 0;
659                 hex[2*i] = tmp[0];
660                 hex[2*i+1] = tmp[1];
661         }
662         hex[32] = 0;
663 } /* }}} void hash_binary_to_hex */
664
665 static void hash_hex_to_binary (uint8_t binary[16], char hex[33]) /* {{{ */
666 {
667         int i;
668
669         for (i = 0; i < 16; i++)
670         {
671                 char tmp[3];
672
673                 tmp[0] = hex[2*i];
674                 tmp[1] = hex[2*i + 1];
675                 tmp[2] = 0;
676
677                 binary[i] = (uint8_t) strtoul (tmp, /* endptr = */ NULL, /* base = */ 16);
678         }
679 } /* }}} void hash_hex_to_binary */
680
681 static void make_password_hash (char response_hex[33], /* {{{ */
682                 const char *password, size_t password_length, char challenge_hex[33])
683 {
684         uint8_t challenge_bin[16];
685         uint8_t response_bin[16];
686         char data_buffer[password_length+17];
687         gcry_md_hd_t md_handle;
688
689         hash_hex_to_binary (challenge_bin, challenge_hex);
690
691         data_buffer[0] = 0;
692         memcpy (&data_buffer[1], password, password_length);
693         memcpy (&data_buffer[1+password_length], challenge_bin, 16);
694
695         gcry_md_open (&md_handle, GCRY_MD_MD5, /* flags = */ 0);
696         gcry_md_write (md_handle, data_buffer, sizeof (data_buffer));
697         memcpy (response_bin, gcry_md_read (md_handle, GCRY_MD_MD5), 16);
698         gcry_md_close (md_handle);
699
700         hash_binary_to_hex (response_hex, response_bin);
701 } /* }}} void make_password_hash */
702
703 static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
704                 void *user_data)
705 {
706         const char *ret;
707         char challenge_hex[33];
708         char response_hex[33];
709         mt_login_data_t *login_data;
710
711         const char *params[2];
712         char param_name[1024];
713         char param_response[64];
714
715         if (r == NULL)
716                 return (EINVAL);
717
718         /* The expected result looks like this:
719          * -- 8< --
720          *  !done 
721          *  =ret=ebddd18303a54111e2dea05a92ab46b4
722          * -- >8 --
723          */
724
725         printf ("login_handler has been called.\n");
726         reply_dump (r);
727
728         if (strcmp (r->status, "done") != 0)
729         {
730                 mt_debug ("login_handler: Unexpected status: %s.\n", r->status);
731                 return (EPROTO);
732         }
733
734         login_data = user_data;
735         if (login_data == NULL)
736                 return (EINVAL);
737
738         ret = mt_reply_param_val_by_key (r, "ret");
739         if (ret == NULL)
740         {
741                 mt_debug ("login_handler: Reply does not have parameter \"ret\".\n");
742                 return (EPROTO);
743         }
744         mt_debug ("login_handler: ret = %s;\n", ret);
745
746         if (strlen (ret) != 32)
747         {
748                 mt_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
749                 return (EPROTO);
750         }
751         strcpy (challenge_hex, ret);
752
753         make_password_hash (response_hex, 
754                         login_data->password, strlen (login_data->password),
755                         challenge_hex);
756
757         snprintf (param_name, sizeof (param_name), "=name=%s", login_data->username);
758         snprintf (param_response, sizeof (param_response),
759                         "=response=00%s", response_hex);
760         params[0] = param_name;
761         params[1] = param_response;
762
763         return (mt_query (c, "/login", 2, params, login2_handler,
764                                 /* user data = */ NULL));
765 } /* }}} int login_handler */
766
767 /*
768  * Public functions
769  */
770 mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
771                 const char *username, const char *password)
772 {
773         int fd;
774         mt_connection_t *c;
775         int status;
776         mt_login_data_t user_data;
777
778         if ((node == NULL) || (username == NULL) || (password == NULL))
779                 return (NULL);
780
781         fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
782         if (fd < 0)
783                 return (NULL);
784
785         c = malloc (sizeof (*c));
786         if (c == NULL)
787         {
788                 close (fd);
789                 return (NULL);
790         }
791         memset (c, 0, sizeof (*c));
792
793         c->fd = fd;
794
795         user_data.username = username;
796         user_data.password = password;
797         status = mt_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
798                         login_handler, &user_data);
799
800         return (c);
801 } /* }}} mt_connection_t *mt_connect */
802
803 int mt_disconnect (mt_connection_t *c) /* {{{ */
804 {
805         if (c == NULL)
806                 return (EINVAL);
807
808         if (c->fd >= 0)
809         {
810                 close (c->fd);
811                 c->fd = -1;
812         }
813
814         free (c);
815
816         return (0);
817 } /* }}} int mt_disconnect */
818
819 int mt_query (mt_connection_t *c, /* {{{ */
820                 const char *command,
821                 size_t args_num, const char * const *args,
822                 mt_reply_handler_t handler, void *user_data)
823 {
824         int status;
825         mt_reply_t *r;
826
827         status = send_command (c, command, args_num, args);
828         if (status != 0)
829                 return (status);
830
831         r = receive_reply (c);
832         if (r == NULL)
833                 return (EPROTO);
834
835         /* Call the callback function with the data we received. */
836         status = (*handler) (c, r, user_data);
837
838         /* Free the allocated memory ... */
839         reply_free (r);
840
841         /* ... and return. */
842         return (status);
843 } /* }}} int mt_query */
844
845 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
846 {
847         if (r == NULL)
848                 return (NULL);
849
850         return (r->next);
851 } /* }}} mt_reply_t *mt_reply_next */
852
853 int mt_reply_num (const mt_reply_t *r) /* {{{ */
854 {
855         int ret;
856         const mt_reply_t *ptr;
857
858         ret = 0;
859         for (ptr = r; ptr != NULL; ptr = ptr->next)
860                 ret++;
861
862         return (ret);
863 } /* }}} int mt_reply_num */
864
865 const char *mt_reply_status (const mt_reply_t *r) /* {{{ */
866 {
867         if (r == NULL)
868                 return (NULL);
869         return (r->status);
870 } /* }}} char *mt_reply_status */
871
872 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
873                 unsigned int index)
874 {
875         if (r == NULL)
876                 return (NULL);
877
878         if (index >= r->params_num)
879                 return (NULL);
880
881         return (r->keys[index]);
882 } /* }}} char *mt_reply_param_key_by_index */
883
884 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
885                 unsigned int index)
886 {
887         if (r == NULL)
888                 return (NULL);
889
890         if (index >= r->params_num)
891                 return (NULL);
892
893         return (r->values[index]);
894 } /* }}} char *mt_reply_param_key_by_index */
895
896 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
897                 const char *key)
898 {
899         unsigned int i;
900
901         if ((r == NULL) || (key == NULL))
902                 return (NULL);
903
904         for (i = 0; i < r->params_num; i++)
905                 if (strcmp (r->keys[i], key) == 0)
906                         return (r->values[i]);
907
908         return (NULL);
909 } /* }}} char *mt_reply_param_val_by_key */
910
911 /* vim: set ts=2 sw=2 noet fdm=marker : */