2b59cbf40b8e031c7cfc5771db283d2c938c6b33
[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) buffer[0]) == 0xF0) /* {{{ */
412         {
413                 status = read_exact (c->fd, &word_length[1], 4);
414                 if (status != 0)
415                         return (status);
416
417                 req_size = (buffer[1] << 24)
418                         | (buffer[2] << 16)
419                         | (buffer[3] << 8)
420                         | buffer[4];
421         }
422         else if ((buffer[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 = ((buffer[0] & 0x1F) << 24)
429                         | (buffer[1] << 16)
430                         | (buffer[2] << 8)
431                         | buffer[3];
432         }
433         else if ((buffer[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 = ((buffer[0] & 0x3F) << 16)
440                         | (buffer[1] << 8)
441                         | buffer[2];
442         }
443         else if ((buffer[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 = ((buffer[0] & 0x7F) << 8)
450                         | buffer[1];
451         }
452         else if ((buffer[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_reply (mt_connection_t *c) /* {{{ */
482 {
483         char buffer[4096];
484         size_t buffer_size;
485         int status;
486
487         mt_reply_t *head;
488         mt_reply_t *tail;
489
490         head = NULL;
491         tail = 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                         mt_reply_t *tmp;
509
510                         tmp = reply_alloc ();
511                         if (tmp == NULL)
512                         {
513                                 status = ENOMEM;
514                                 break;
515                         }
516
517                         tmp->status = strdup (&buffer[1]);
518                         if (tmp->status == NULL)
519                         {
520                                 reply_free (tmp);
521                                 status = ENOMEM;
522                                 break;
523                         }
524
525                         if (tail == NULL)
526                         {
527                                 head = tmp;
528                                 tail = tmp;
529                         }
530                         else
531                         {
532                                 tail->next = tmp;
533                                 tail = tmp;
534                         }
535                 } /* }}} if (buffer[0] == '!') */
536                 else if (buffer[0] == '=') /* {{{ */
537                 {
538                         char *key = &buffer[1];
539                         char *val;
540
541                         key = &buffer[1];
542                         val = strchr (key, '=');
543                         if (val == NULL)
544                         {
545                                 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
546                                 continue;
547                         }
548                         *val = 0;
549                         val++;
550
551                         reply_add_keyval (tail, key, val);
552                 } /* }}} if (buffer[0] == '=') */
553                 else
554                 {
555                         printf ("Ignoring unknown word: %s\n", buffer);
556                 }
557         } /* while (42) */
558         
559         if (status != 0)
560         {
561                 reply_free (head);
562                 return (NULL);
563         }
564
565         return (head);
566 } /* }}} mt_reply_t *receive_reply */
567
568 static int create_socket (const char *node, const char *service) /* {{{ */
569 {
570         struct addrinfo  ai_hint;
571         struct addrinfo *ai_list;
572         struct addrinfo *ai_ptr;
573         int status;
574
575         mt_debug ("create_socket (node = %s, service = %s);\n",
576                         node, service);
577
578         memset (&ai_hint, 0, sizeof (ai_hint));
579 #ifdef AI_ADDRCONFIG
580         ai_hint.ai_flags |= AI_ADDRCONFIG;
581 #endif
582         ai_hint.ai_family = AF_UNSPEC;
583         ai_hint.ai_socktype = SOCK_STREAM;
584
585         ai_list = NULL;
586         status = getaddrinfo (node, service, &ai_hint, &ai_list);
587         if (status != 0)
588                 return (-1);
589         assert (ai_list != NULL);
590
591         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
592         {
593                 int fd;
594                 int status;
595
596                 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
597                                 ai_ptr->ai_protocol);
598                 if (fd < 0)
599                 {
600                         mt_debug ("create_socket: socket(2) failed.\n");
601                         continue;
602                 }
603
604                 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
605                 if (status != 0)
606                 {
607                         mt_debug ("create_socket: connect(2) failed.\n");
608                         close (fd);
609                         continue;
610                 }
611
612                 return (fd);
613         }
614
615         freeaddrinfo (ai_list);
616
617         return (-1);
618 } /* }}} int create_socket */
619
620 static int login2_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
621                 void *user_data)
622 {
623         if (r == NULL)
624                 return (EINVAL);
625
626         printf ("login2_handler has been called.\n");
627         reply_dump (r);
628
629         return (0);
630 } /* }}} int login2_handler */
631
632 static void hash_binary_to_hex (char hex[33], uint8_t binary[16]) /* {{{ */
633 {
634         int i;
635
636         for (i = 0; i < 16; i++)
637         {
638                 char tmp[3];
639                 snprintf (tmp, 3, "%02"PRIx8, binary[i]);
640                 tmp[2] = 0;
641                 hex[2*i] = tmp[0];
642                 hex[2*i+1] = tmp[1];
643         }
644         hex[32] = 0;
645 } /* }}} void hash_binary_to_hex */
646
647 static void hash_hex_to_binary (uint8_t binary[16], char hex[33]) /* {{{ */
648 {
649         int i;
650
651         for (i = 0; i < 16; i++)
652         {
653                 char tmp[3];
654
655                 tmp[0] = hex[2*i];
656                 tmp[1] = hex[2*i + 1];
657                 tmp[2] = 0;
658
659                 binary[i] = (uint8_t) strtoul (tmp, /* endptr = */ NULL, /* base = */ 16);
660         }
661 } /* }}} void hash_hex_to_binary */
662
663 static void make_password_hash (char response_hex[33], /* {{{ */
664                 const char *password, size_t password_length, char challenge_hex[33])
665 {
666         uint8_t challenge_bin[16];
667         uint8_t response_bin[16];
668         char data_buffer[password_length+17];
669         gcry_md_hd_t md_handle;
670
671         hash_hex_to_binary (challenge_bin, challenge_hex);
672
673         data_buffer[0] = 0;
674         memcpy (&data_buffer[1], password, password_length);
675         memcpy (&data_buffer[1+password_length], challenge_bin, 16);
676
677         gcry_md_open (&md_handle, GCRY_MD_MD5, /* flags = */ 0);
678         gcry_md_write (md_handle, data_buffer, sizeof (data_buffer));
679         memcpy (response_bin, gcry_md_read (md_handle, GCRY_MD_MD5), 16);
680         gcry_md_close (md_handle);
681
682         hash_binary_to_hex (response_hex, response_bin);
683 } /* }}} void make_password_hash */
684
685 static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
686                 void *user_data)
687 {
688         const char *ret;
689         char challenge_hex[33];
690         char response_hex[33];
691         mt_login_data_t *login_data;
692
693         const char *params[2];
694         char param_name[1024];
695         char param_response[64];
696
697         if (r == NULL)
698                 return (EINVAL);
699
700         printf ("login_handler has been called.\n");
701         reply_dump (r);
702
703         login_data = user_data;
704         if (login_data == NULL)
705                 return (EINVAL);
706
707         ret = mt_reply_param_val_by_key (r, "ret");
708         if (ret == NULL)
709         {
710                 mt_debug ("login_handler: Reply does not have parameter \"ret\".\n");
711                 return (EPROTO);
712         }
713         mt_debug ("login_handler: ret = %s;\n", ret);
714
715         if (strlen (ret) != 32)
716         {
717                 mt_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
718                 return (EPROTO);
719         }
720         strcpy (challenge_hex, ret);
721
722         make_password_hash (response_hex, 
723                         login_data->password, strlen (login_data->password),
724                         challenge_hex);
725
726         snprintf (param_name, sizeof (param_name), "=name=%s", login_data->username);
727         snprintf (param_response, sizeof (param_response),
728                         "=response=00%s", response_hex);
729         params[0] = param_name;
730         params[1] = param_response;
731
732         return (mt_query (c, "/login", 2, params, login2_handler,
733                                 /* user data = */ NULL));
734 } /* }}} int login_handler */
735
736 /*
737  * Public functions
738  */
739 mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
740                 const char *username, const char *password)
741 {
742         int fd;
743         mt_connection_t *c;
744         int status;
745         mt_login_data_t user_data;
746
747         if ((node == NULL) || (username == NULL) || (password == NULL))
748                 return (NULL);
749
750         fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
751         if (fd < 0)
752                 return (NULL);
753
754         c = malloc (sizeof (*c));
755         if (c == NULL)
756         {
757                 close (fd);
758                 return (NULL);
759         }
760         memset (c, 0, sizeof (*c));
761
762         c->fd = fd;
763
764         user_data.username = username;
765         user_data.password = password;
766         status = mt_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
767                         login_handler, &user_data);
768
769         return (c);
770 } /* }}} mt_connection_t *mt_connect */
771
772 int mt_disconnect (mt_connection_t *c) /* {{{ */
773 {
774         if (c == NULL)
775                 return (EINVAL);
776
777         if (c->fd >= 0)
778         {
779                 close (c->fd);
780                 c->fd = -1;
781         }
782
783         free (c);
784
785         return (0);
786 } /* }}} int mt_disconnect */
787
788 int mt_query (mt_connection_t *c, /* {{{ */
789                 const char *command,
790                 size_t args_num, const char * const *args,
791                 mt_reply_handler_t handler, void *user_data)
792 {
793         int status;
794         mt_reply_t *r;
795
796         status = send_command (c, command, args_num, args);
797         if (status != 0)
798                 return (status);
799
800         r = receive_reply (c);
801         if (r == NULL)
802                 return (EPROTO);
803
804         /* Call the callback function with the data we received. */
805         status = (*handler) (c, r, user_data);
806
807         /* Free the allocated memory ... */
808         reply_free (r);
809
810         /* ... and return. */
811         return (status);
812 } /* }}} int mt_query */
813
814 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
815 {
816         if (r == NULL)
817                 return (NULL);
818
819         return (r->next);
820 } /* }}} mt_reply_t *mt_reply_next */
821
822 int mt_reply_num (const mt_reply_t *r) /* {{{ */
823 {
824         int ret;
825         const mt_reply_t *ptr;
826
827         ret = 0;
828         for (ptr = r; ptr != NULL; ptr = ptr->next)
829                 ret++;
830
831         return (ret);
832 } /* }}} int mt_reply_num */
833
834 const char *mt_reply_status (const mt_reply_t *r) /* {{{ */
835 {
836         if (r == NULL)
837                 return (NULL);
838         return (r->status);
839 } /* }}} char *mt_reply_status */
840
841 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
842                 unsigned int index)
843 {
844         if (r == NULL)
845                 return (NULL);
846
847         if (index >= r->params_num)
848                 return (NULL);
849
850         return (r->keys[index]);
851 } /* }}} char *mt_reply_param_key_by_index */
852
853 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
854                 unsigned int index)
855 {
856         if (r == NULL)
857                 return (NULL);
858
859         if (index >= r->params_num)
860                 return (NULL);
861
862         return (r->values[index]);
863 } /* }}} char *mt_reply_param_key_by_index */
864
865 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
866                 const char *key)
867 {
868         unsigned int i;
869
870         if ((r == NULL) || (key == NULL))
871                 return (NULL);
872
873         for (i = 0; i < r->params_num; i++)
874                 if (strcmp (r->keys[i], key) == 0)
875                         return (r->values[i]);
876
877         return (NULL);
878 } /* }}} char *mt_reply_param_val_by_key */
879
880 /* vim: set ts=2 sw=2 noet fdm=marker : */