113039489e782d9ebb55e67ad8866640ec3fd297
[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         status = buffer_add (&buffer_ptr, &buffer_size, command);
352         if (status != 0)
353                 return (status);
354
355         for (i = 0; i < args_num; i++)
356         {
357                 if (args[i] == NULL)
358                         return (EINVAL);
359
360                 status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
361                 if (status != 0)
362                         return (status);
363         }
364
365         status = buffer_end (&buffer_ptr, &buffer_size);
366         if (status != 0)
367                 return (status);
368
369         buffer_ptr = buffer;
370         buffer_size = sizeof (buffer) - buffer_size;
371         while (buffer_size > 0)
372         {
373                 ssize_t bytes_written;
374
375                 errno = 0;
376                 bytes_written = write (c->fd, buffer_ptr, buffer_size);
377                 if (bytes_written < 0)
378                 {
379                         if (errno == EAGAIN)
380                                 continue;
381                         else
382                                 return (errno);
383                 }
384                 assert (bytes_written <= buffer_size);
385
386                 buffer_ptr += bytes_written;
387                 buffer_size -= bytes_written;
388         } /* while (buffer_size > 0) */
389
390         return (0);
391 } /* }}} int send_command */
392
393 static int read_word (mt_connection_t *c, /* {{{ */
394                 char *buffer, size_t *buffer_size)
395 {
396         size_t req_size;
397         uint8_t word_length[5];
398         int status;
399
400         if ((buffer == NULL) || (*buffer_size < 1))
401                 return (EINVAL);
402
403         /* read one byte from the socket */
404         status = read_exact (c->fd, word_length, 1);
405         if (status != 0)
406                 return (status);
407
408         /* Calculate `req_size' */
409         if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
410         {
411                 status = read_exact (c->fd, &word_length[1], 4);
412                 if (status != 0)
413                         return (status);
414
415                 req_size = (buffer[1] << 24)
416                         | (buffer[2] << 16)
417                         | (buffer[3] << 8)
418                         | buffer[4];
419         }
420         else if ((buffer[0] & 0xE0) == 0xE0)
421         {
422                 status = read_exact (c->fd, &word_length[1], 3);
423                 if (status != 0)
424                         return (status);
425
426                 req_size = ((buffer[0] & 0x1F) << 24)
427                         | (buffer[1] << 16)
428                         | (buffer[2] << 8)
429                         | buffer[3];
430         }
431         else if ((buffer[0] & 0xC0) == 0xC0)
432         {
433                 status = read_exact (c->fd, &word_length[1], 2);
434                 if (status != 0)
435                         return (status);
436
437                 req_size = ((buffer[0] & 0x3F) << 16)
438                         | (buffer[1] << 8)
439                         | buffer[2];
440         }
441         else if ((buffer[0] & 0x80) == 0x80)
442         {
443                 status = read_exact (c->fd, &word_length[1], 1);
444                 if (status != 0)
445                         return (status);
446
447                 req_size = ((buffer[0] & 0x7F) << 8)
448                         | buffer[1];
449         }
450         else if ((buffer[0] & 0x80) == 0)
451         {
452                 req_size = (size_t) word_length[0];
453         }
454         else
455         {
456                 /* First nibble is `F' but second nibble is not `0'. */
457                 return (EPROTO);
458         } /* }}} */
459
460         if (*buffer_size < req_size)
461                 return (ENOMEM);
462
463         /* Empty word. This ends a `sentence' and must therefore be valid. */
464         if (req_size == 0)
465         {
466                 buffer[0] = 0;
467                 *buffer_size = 0;
468                 return (0);
469         }
470
471         status = read_exact (c->fd, buffer, req_size);
472         if (status != 0)
473                 return (status);
474         *buffer_size = req_size;
475
476         return (0);
477 } /* }}} int buffer_decode_next */
478
479 static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
480 {
481         char buffer[4096];
482         size_t buffer_size;
483         int status;
484
485         mt_reply_t *head;
486         mt_reply_t *tail;
487
488         head = NULL;
489         tail = NULL;
490
491         while (42)
492         {
493                 buffer_size = sizeof (buffer) - 1;
494                 status = read_word (c, buffer, &buffer_size);
495                 if (status != 0)
496                         break;
497                 assert (buffer_size < sizeof (buffer));
498                 buffer[buffer_size] = 0;
499
500                 /* Empty word means end of reply */
501                 if (buffer_size == 0)
502                         break;
503
504                 if (buffer[0] == '!') /* {{{ */
505                 {
506                         mt_reply_t *tmp;
507
508                         tmp = reply_alloc ();
509                         if (tmp == NULL)
510                         {
511                                 status = ENOMEM;
512                                 break;
513                         }
514
515                         tmp->status = strdup (&buffer[1]);
516                         if (tmp->status == NULL)
517                         {
518                                 reply_free (tmp);
519                                 status = ENOMEM;
520                                 break;
521                         }
522
523                         if (tail == NULL)
524                         {
525                                 head = tmp;
526                                 tail = tmp;
527                         }
528                         else
529                         {
530                                 tail->next = tmp;
531                                 tail = tmp;
532                         }
533                 } /* }}} if (buffer[0] == '!') */
534                 else if (buffer[0] == '=') /* {{{ */
535                 {
536                         char *key = &buffer[1];
537                         char *val;
538
539                         key = &buffer[1];
540                         val = strchr (key, '=');
541                         if (val == NULL)
542                         {
543                                 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
544                                 continue;
545                         }
546                         *val = 0;
547                         val++;
548
549                         reply_add_keyval (tail, key, val);
550                 } /* }}} if (buffer[0] == '=') */
551                 else
552                 {
553                         printf ("Ignoring unknown word: %s\n", buffer);
554                 }
555         } /* while (42) */
556         
557         if (status != 0)
558         {
559                 reply_free (head);
560                 return (NULL);
561         }
562
563         return (head);
564 } /* }}} mt_reply_t *receive_reply */
565
566 static int create_socket (const char *node, const char *service) /* {{{ */
567 {
568         struct addrinfo  ai_hint;
569         struct addrinfo *ai_list;
570         struct addrinfo *ai_ptr;
571         int status;
572
573         mt_debug ("create_socket (node = %s, service = %s);\n",
574                         node, service);
575
576         memset (&ai_hint, 0, sizeof (ai_hint));
577 #ifdef AI_ADDRCONFIG
578         ai_hint.ai_flags |= AI_ADDRCONFIG;
579 #endif
580         ai_hint.ai_family = AF_UNSPEC;
581         ai_hint.ai_socktype = SOCK_STREAM;
582
583         ai_list = NULL;
584         status = getaddrinfo (node, service, &ai_hint, &ai_list);
585         if (status != 0)
586                 return (-1);
587         assert (ai_list != NULL);
588
589         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
590         {
591                 int fd;
592                 int status;
593
594                 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
595                                 ai_ptr->ai_protocol);
596                 if (fd < 0)
597                 {
598                         mt_debug ("create_socket: socket(2) failed.\n");
599                         continue;
600                 }
601
602                 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
603                 if (status != 0)
604                 {
605                         mt_debug ("create_socket: connect(2) failed.\n");
606                         close (fd);
607                         continue;
608                 }
609
610                 return (fd);
611         }
612
613         freeaddrinfo (ai_list);
614
615         return (-1);
616 } /* }}} int create_socket */
617
618 static int login2_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
619                 void *user_data)
620 {
621         if (r == NULL)
622                 return (EINVAL);
623
624         printf ("login2_handler has been called.\n");
625         reply_dump (r);
626
627         return (0);
628 } /* }}} int login2_handler */
629
630 static void hash_binary_to_hex (char hex[33], uint8_t binary[16]) /* {{{ */
631 {
632         int i;
633
634         for (i = 0; i < 16; i++)
635         {
636                 char tmp[3];
637                 snprintf (tmp, 3, "%02"PRIx8, binary[i]);
638                 tmp[2] = 0;
639                 hex[2*i] = tmp[0];
640                 hex[2*i+1] = tmp[1];
641         }
642         hex[32] = 0;
643 } /* }}} void hash_binary_to_hex */
644
645 static void hash_hex_to_binary (uint8_t binary[16], char hex[33]) /* {{{ */
646 {
647         int i;
648
649         for (i = 0; i < 16; i++)
650         {
651                 char tmp[3];
652
653                 tmp[0] = hex[2*i];
654                 tmp[1] = hex[2*i + 1];
655                 tmp[2] = 0;
656
657                 binary[i] = (uint8_t) strtoul (tmp, /* endptr = */ NULL, /* base = */ 16);
658         }
659 } /* }}} void hash_hex_to_binary */
660
661 static void make_password_hash (char response_hex[33], /* {{{ */
662                 const char *password, size_t password_length, char challenge_hex[33])
663 {
664         uint8_t challenge_bin[16];
665         uint8_t response_bin[16];
666         char data_buffer[password_length+17];
667         gcry_md_hd_t md_handle;
668
669         hash_hex_to_binary (challenge_bin, challenge_hex);
670
671         data_buffer[0] = 0;
672         memcpy (&data_buffer[1], password, password_length);
673         memcpy (&data_buffer[1+password_length], challenge_bin, 16);
674
675         gcry_md_open (&md_handle, GCRY_MD_MD5, /* flags = */ 0);
676         gcry_md_write (md_handle, data_buffer, sizeof (data_buffer));
677         memcpy (response_bin, gcry_md_read (md_handle, GCRY_MD_MD5), 16);
678         gcry_md_close (md_handle);
679
680         hash_binary_to_hex (response_hex, response_bin);
681 } /* }}} void make_password_hash */
682
683 static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
684                 void *user_data)
685 {
686         const char *ret;
687         char challenge_hex[33];
688         char response_hex[33];
689         mt_login_data_t *login_data;
690
691         const char *params[2];
692         char param_name[1024];
693         char param_response[64];
694
695         if (r == NULL)
696                 return (EINVAL);
697
698         printf ("login_handler has been called.\n");
699         reply_dump (r);
700
701         login_data = user_data;
702         if (login_data == NULL)
703                 return (EINVAL);
704
705         ret = mt_reply_param_val_by_key (r, "ret");
706         if (ret == NULL)
707         {
708                 mt_debug ("login_handler: Reply does not have parameter \"ret\".\n");
709                 return (EPROTO);
710         }
711         mt_debug ("login_handler: ret = %s;\n", ret);
712
713         if (strlen (ret) != 32)
714         {
715                 mt_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
716                 return (EPROTO);
717         }
718         strcpy (challenge_hex, ret);
719
720         make_password_hash (response_hex, 
721                         login_data->password, strlen (login_data->password),
722                         challenge_hex);
723
724         snprintf (param_name, sizeof (param_name), "=name=%s", login_data->username);
725         snprintf (param_response, sizeof (param_response),
726                         "=response=00%s", response_hex);
727         params[0] = param_name;
728         params[1] = param_response;
729
730         return (mt_query (c, "/login", 2, params, login2_handler,
731                                 /* user data = */ NULL));
732 } /* }}} int login_handler */
733
734 /*
735  * Public functions
736  */
737 mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
738                 const char *username, const char *password)
739 {
740         int fd;
741         mt_connection_t *c;
742         int status;
743         mt_login_data_t user_data;
744
745         if ((node == NULL) || (username == NULL) || (password == NULL))
746                 return (NULL);
747
748         fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
749         if (fd < 0)
750                 return (NULL);
751
752         c = malloc (sizeof (*c));
753         if (c == NULL)
754         {
755                 close (fd);
756                 return (NULL);
757         }
758         memset (c, 0, sizeof (*c));
759
760         c->fd = fd;
761
762         user_data.username = username;
763         user_data.password = password;
764         status = mt_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
765                         login_handler, &user_data);
766
767         return (c);
768 } /* }}} mt_connection_t *mt_connect */
769
770 int mt_disconnect (mt_connection_t *c) /* {{{ */
771 {
772         if (c == NULL)
773                 return (EINVAL);
774
775         if (c->fd >= 0)
776         {
777                 close (c->fd);
778                 c->fd = -1;
779         }
780
781         free (c);
782
783         return (0);
784 } /* }}} int mt_disconnect */
785
786 int mt_query (mt_connection_t *c, /* {{{ */
787                 const char *command,
788                 size_t args_num, const char * const *args,
789                 mt_reply_handler_t handler, void *user_data)
790 {
791         int status;
792         mt_reply_t *r;
793
794         status = send_command (c, command, args_num, args);
795         if (status != 0)
796                 return (status);
797
798         r = receive_reply (c);
799         if (r == NULL)
800                 return (EPROTO);
801
802         /* Call the callback function with the data we received. */
803         status = (*handler) (c, r, user_data);
804
805         /* Free the allocated memory ... */
806         reply_free (r);
807
808         /* ... and return. */
809         return (status);
810 } /* }}} int mt_query */
811
812 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
813 {
814         if (r == NULL)
815                 return (NULL);
816
817         return (r->next);
818 } /* }}} mt_reply_t *mt_reply_next */
819
820 int mt_reply_num (const mt_reply_t *r) /* {{{ */
821 {
822         int ret;
823         const mt_reply_t *ptr;
824
825         ret = 0;
826         for (ptr = r; ptr != NULL; ptr = ptr->next)
827                 ret++;
828
829         return (ret);
830 } /* }}} int mt_reply_num */
831
832 const char *mt_reply_status (const mt_reply_t *r) /* {{{ */
833 {
834         if (r == NULL)
835                 return (NULL);
836         return (r->status);
837 } /* }}} char *mt_reply_status */
838
839 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
840                 unsigned int index)
841 {
842         if (r == NULL)
843                 return (NULL);
844
845         if (index >= r->params_num)
846                 return (NULL);
847
848         return (r->keys[index]);
849 } /* }}} char *mt_reply_param_key_by_index */
850
851 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
852                 unsigned int index)
853 {
854         if (r == NULL)
855                 return (NULL);
856
857         if (index >= r->params_num)
858                 return (NULL);
859
860         return (r->values[index]);
861 } /* }}} char *mt_reply_param_key_by_index */
862
863 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
864                 const char *key)
865 {
866         unsigned int i;
867
868         if ((r == NULL) || (key == NULL))
869                 return (NULL);
870
871         for (i = 0; i < r->params_num; i++)
872                 if (strcmp (r->keys[i], key) == 0)
873                         return (r->values[i]);
874
875         return (NULL);
876 } /* }}} char *mt_reply_param_val_by_key */
877
878 /* vim: set ts=2 sw=2 noet fdm=marker : */