26b44234f93f9e135a2176e122fce7406f851017
[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 "routeros_api.h"
44
45
46 /* FIXME */
47 char *strdup (const char *);
48
49 /*
50  * Private structures
51  */
52 struct mt_connection_s
53 {
54         int fd;
55 };
56
57 struct mt_reply_s
58 {
59         unsigned int params_num;
60         char *status;
61         char **keys;
62         char **values;
63
64         mt_reply_t *next;
65 };
66
67 /*
68  * Private functions
69  */
70 static int read_exact (int fd, void *buffer, size_t buffer_size) /* {{{ */
71 {
72         char *buffer_ptr;
73         size_t have_bytes;
74
75         if ((fd < 0) || (buffer == NULL))
76                 return (EINVAL);
77
78         if (buffer_size == 0)
79                 return (0);
80
81         buffer_ptr = buffer;
82         have_bytes = 0;
83         while (have_bytes < buffer_size)
84         {
85                 size_t want_bytes;
86                 ssize_t status;
87
88                 want_bytes = buffer_size - have_bytes;
89                 errno = 0;
90                 status = read (fd, buffer_ptr, want_bytes);
91                 if (status < 0)
92                 {
93                         if (errno == EAGAIN)
94                                 continue;
95                         else
96                                 return (status);
97                 }
98
99                 assert (status <= want_bytes);
100                 have_bytes += status;
101                 buffer_ptr += status;
102         }
103
104         return (0);
105 } /* }}} int read_exact */
106
107 static mt_reply_t *reply_alloc (void) /* {{{ */
108 {
109         mt_reply_t *r;
110
111         r = malloc (sizeof (*r));
112         if (r == NULL)
113                 return (NULL);
114
115         memset (r, 0, sizeof (*r));
116         r->keys = NULL;
117         r->values = NULL;
118         r->next = NULL;
119
120         return (r);
121 } /* }}} mt_reply_s *reply_alloc */
122
123 static int reply_add_keyval (mt_reply_t *r, const char *key, /* {{{ */
124                 const char *val)
125 {
126         char **tmp;
127
128         tmp = realloc (r->keys, (r->params_num + 1) * sizeof (*tmp));
129         if (tmp == NULL)
130                 return (ENOMEM);
131         r->keys = tmp;
132
133         tmp = realloc (r->values, (r->params_num + 1) * sizeof (*tmp));
134         if (tmp == NULL)
135                 return (ENOMEM);
136         r->values = tmp;
137
138         r->keys[r->params_num] = strdup (key);
139         if (r->keys[r->params_num] == NULL)
140                 return (ENOMEM);
141
142         r->values[r->params_num] = strdup (val);
143         if (r->values[r->params_num] == NULL)
144         {
145                 free (r->keys[r->params_num]);
146                 r->keys[r->params_num] = NULL;
147                 return (ENOMEM);
148         }
149
150         r->params_num++;
151         return (0);
152 } /* }}} int reply_add_keyval */
153
154 static void reply_free (mt_reply_t *r) /* {{{ */
155 {
156         mt_reply_t *next;
157         unsigned int i;
158
159         if (r == NULL)
160                 return;
161
162         next = r->next;
163
164         for (i = 0; i < r->params_num; i++)
165         {
166                 free (r->keys[i]);
167                 free (r->values[i]);
168         }
169
170         free (r->keys);
171         free (r->values);
172
173         free (r);
174
175         reply_free (next);
176 } /* }}} void reply_free */
177
178 static int buffer_init (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
179 {
180         if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
181                 return (EINVAL);
182
183         if (*ret_buffer_size < 1)
184                 return (EINVAL);
185
186         return (0);
187 } /* }}} int buffer_init */
188
189 static int buffer_add (char **ret_buffer, size_t *ret_buffer_size, /* {{{ */
190                 const char *string)
191 {
192         char *buffer;
193         size_t buffer_size;
194         size_t string_size;
195         size_t req_size;
196
197         if ((ret_buffer == NULL) || (ret_buffer_size == NULL) || (string == NULL))
198                 return (EINVAL);
199
200         buffer = *ret_buffer;
201         buffer_size = *ret_buffer_size;
202
203         string_size = strlen (string);
204         if (string_size == 0)
205                 return (EINVAL);
206
207         if (string_size >= 0x10000000)
208                 req_size = 5 + string_size;
209         else if (string_size >= 0x200000)
210                 req_size = 4 + string_size;
211         else if (string_size >= 0x4000)
212                 req_size = 3 + string_size;
213         else if (string_size >= 0x80)
214                 req_size = 2 + string_size;
215         else
216                 req_size = 1 + string_size;
217
218         if (*ret_buffer_size < req_size)
219                 return (ENOMEM);
220
221         if (string_size >= 0x10000000)
222         {
223                 buffer[0] = 0xF0;
224                 buffer[1] = (string_size >> 24) & 0xff;
225                 buffer[2] = (string_size >> 16) & 0xff;
226                 buffer[3] = (string_size >>  8) & 0xff;
227                 buffer[4] = (string_size      ) & 0xff;
228                 buffer += 5;
229                 buffer_size -= 5;
230         }
231         else if (string_size >= 0x200000)
232         {
233                 buffer[0] = (string_size >> 24) & 0x1f;
234                 buffer[0] |= 0xE0;
235                 buffer[1] = (string_size >> 16) & 0xff;
236                 buffer[2] = (string_size >>  8) & 0xff;
237                 buffer[3] = (string_size      ) & 0xff;
238                 buffer += 4;
239                 buffer_size -= 4;
240         }
241         else if (string_size >= 0x4000)
242         {
243                 buffer[0] = (string_size >> 16) & 0x3f;
244                 buffer[0] |= 0xC0;
245                 buffer[1] = (string_size >>  8) & 0xff;
246                 buffer[2] = (string_size      ) & 0xff;
247                 buffer += 3;
248                 buffer_size -= 3;
249         }
250         else if (string_size >= 0x80)
251         {
252                 buffer[0] = (string_size >>  8) & 0x7f;
253                 buffer[0] |= 0x80;
254                 buffer[1] = (string_size      ) & 0xff;
255                 buffer += 2;
256                 buffer_size -= 2;
257         }
258         else /* if (string_size <= 0x7f) */
259         {
260                 buffer[0] = (char) string_size;
261                 buffer += 1;
262                 buffer_size -= 1;
263         }
264
265         assert (buffer_size >= string_size);
266         memcpy (buffer, string, string_size);
267         buffer += string_size;
268         buffer_size -= string_size;
269
270         *ret_buffer = buffer;
271         *ret_buffer_size = buffer_size;
272
273         return (0);
274 } /* }}} int buffer_add */
275
276 static int buffer_end (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
277 {
278         if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
279                 return (EINVAL);
280
281         if (*ret_buffer_size < 1)
282                 return (EINVAL);
283
284         /* Add empty word. */
285         (*ret_buffer)[0] = 0;
286         (*ret_buffer)++;
287         (*ret_buffer_size)--;
288
289         return (0);
290 } /* }}} int buffer_end */
291
292 static int send_command (mt_connection_t *c, /* {{{ */
293                 const char *command,
294                 size_t args_num, const char * const *args)
295 {
296         char buffer[4096];
297         char *buffer_ptr;
298         size_t buffer_size;
299
300         size_t i;
301         int status;
302
303         buffer_ptr = buffer;
304         buffer_size = sizeof (buffer);
305
306         status = buffer_init (&buffer_ptr, &buffer_size);
307         if (status != 0)
308                 return (status);
309
310         status = buffer_add (&buffer_ptr, &buffer_size, command);
311         if (status != 0)
312                 return (status);
313
314         for (i = 0; i < args_num; i++)
315         {
316                 if (args[i] == NULL)
317                         return (EINVAL);
318
319                 status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
320                 if (status != 0)
321                         return (status);
322         }
323
324         status = buffer_end (&buffer_ptr, &buffer_size);
325         if (status != 0)
326                 return (status);
327
328         buffer_ptr = buffer;
329         while (buffer_size > 0)
330         {
331                 ssize_t bytes_written;
332
333                 errno = 0;
334                 bytes_written = write (c->fd, buffer_ptr, buffer_size);
335                 if (bytes_written < 0)
336                 {
337                         if (errno == EAGAIN)
338                                 continue;
339                         else
340                                 return (errno);
341                 }
342                 assert (bytes_written <= buffer_size);
343
344                 buffer_ptr += bytes_written;
345                 buffer_size -= bytes_written;
346         } /* while (buffer_size > 0) */
347
348         return (0);
349 } /* }}} int send_command */
350
351 static int read_word (mt_connection_t *c, /* {{{ */
352                 char *buffer, size_t *buffer_size)
353 {
354         size_t req_size;
355         uint8_t word_length[5];
356         int status;
357
358         if ((buffer == NULL) || (*buffer_size < 1))
359                 return (EINVAL);
360
361         /* read one byte from the socket */
362         status = read_exact (c->fd, word_length, 1);
363         if (status != 0)
364                 return (status);
365
366         /* Calculate `req_size' */
367         if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
368         {
369                 status = read_exact (c->fd, &word_length[1], 4);
370                 if (status != 0)
371                         return (status);
372
373                 req_size = (buffer[1] << 24)
374                         | (buffer[2] << 16)
375                         | (buffer[3] << 8)
376                         | buffer[4];
377         }
378         else if ((buffer[0] & 0xE0) == 0xE0)
379         {
380                 status = read_exact (c->fd, &word_length[1], 3);
381                 if (status != 0)
382                         return (status);
383
384                 req_size = ((buffer[0] & 0x1F) << 24)
385                         | (buffer[1] << 16)
386                         | (buffer[2] << 8)
387                         | buffer[3];
388         }
389         else if ((buffer[0] & 0xC0) == 0xC0)
390         {
391                 status = read_exact (c->fd, &word_length[1], 2);
392                 if (status != 0)
393                         return (status);
394
395                 req_size = ((buffer[0] & 0x3F) << 16)
396                         | (buffer[1] << 8)
397                         | buffer[2];
398         }
399         else if ((buffer[0] & 0x80) == 0x80)
400         {
401                 status = read_exact (c->fd, &word_length[1], 1);
402                 if (status != 0)
403                         return (status);
404
405                 req_size = ((buffer[0] & 0x7F) << 8)
406                         | buffer[1];
407         }
408         else if ((buffer[0] & 0x80) == 0)
409         {
410                 req_size = (size_t) word_length[0];
411         }
412         else
413         {
414                 /* First nibble is `F' but second nibble is not `0'. */
415                 return (EPROTO);
416         } /* }}} */
417
418         if (*buffer_size < req_size)
419                 return (ENOMEM);
420
421         /* Empty word. This ends a `sentence' and must therefore be valid. */
422         if (req_size == 0)
423         {
424                 buffer[0] = 0;
425                 *buffer_size = 0;
426                 return (0);
427         }
428
429         status = read_exact (c->fd, buffer, req_size);
430         if (status != 0)
431                 return (status);
432         *buffer_size = req_size;
433
434         return (0);
435 } /* }}} int buffer_decode_next */
436
437 static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
438 {
439         char buffer[4096];
440         size_t buffer_size;
441         int status;
442
443         mt_reply_t *head;
444         mt_reply_t *tail;
445
446         head = NULL;
447         tail = NULL;
448
449         while (42)
450         {
451                 buffer_size = sizeof (buffer) - 1;
452                 status = read_word (c, buffer, &buffer_size);
453                 if (status != 0)
454                         break;
455                 assert (buffer_size < sizeof (buffer));
456                 buffer[buffer_size] = 0;
457
458                 /* Empty word means end of reply */
459                 if (buffer_size == 0)
460                         break;
461
462                 if (buffer[0] == '!') /* {{{ */
463                 {
464                         mt_reply_t *tmp;
465
466                         tmp = reply_alloc ();
467                         if (tmp == NULL)
468                         {
469                                 status = ENOMEM;
470                                 break;
471                         }
472
473                         tmp->status = strdup (&buffer[1]);
474                         if (tmp->status == NULL)
475                         {
476                                 reply_free (tmp);
477                                 status = ENOMEM;
478                                 break;
479                         }
480
481                         if (tail == NULL)
482                         {
483                                 head = tmp;
484                                 tail = tmp;
485                         }
486                         else
487                         {
488                                 tail->next = tmp;
489                                 tail = tmp;
490                         }
491                 } /* }}} if (buffer[0] == '!') */
492                 else if (buffer[0] == '=') /* {{{ */
493                 {
494                         char *key = &buffer[1];
495                         char *val;
496
497                         key = &buffer[1];
498                         val = strchr (key, '=');
499                         if (val == NULL)
500                         {
501                                 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
502                                 continue;
503                         }
504                         *val = 0;
505                         val++;
506
507                         reply_add_keyval (tail, key, val);
508                 } /* }}} if (buffer[0] == '=') */
509                 else
510                 {
511                         printf ("Ignoring unknown word: %s\n", buffer);
512                 }
513         } /* while (42) */
514         
515         if (status != 0)
516         {
517                 reply_free (head);
518                 return (NULL);
519         }
520
521         return (head);
522 } /* }}} mt_reply_t *receive_reply */
523
524 static int create_socket (const char *node, const char *service) /* {{{ */
525 {
526         struct addrinfo  ai_hint;
527         struct addrinfo *ai_list;
528         struct addrinfo *ai_ptr;
529         int status;
530
531         memset (&ai_hint, 0, sizeof (ai_hint));
532 #ifdef AI_ADDRCONFIG
533         ai_hint.ai_flags |= AI_ADDRCONFIG;
534 #endif
535         ai_hint.ai_family = AF_UNSPEC;
536         ai_hint.ai_socktype = SOCK_STREAM;
537
538         ai_list = NULL;
539         status = getaddrinfo (node, service, &ai_hint, &ai_list);
540         if (status != 0)
541                 return (-1);
542         assert (ai_list != NULL);
543
544         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
545         {
546                 int fd;
547                 int status;
548
549                 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
550                                 ai_ptr->ai_protocol);
551                 if (fd < 0)
552                         continue;
553
554                 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
555                 if (status != 0)
556                 {
557                         close (fd);
558                         continue;
559                 }
560
561                 return (fd);
562         }
563
564         freeaddrinfo (ai_list);
565
566         return (-1);
567 } /* }}} int create_socket */
568
569 /*
570  * Public functions
571  */
572 mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
573                 const char *username, const char *password)
574 {
575         int fd;
576         mt_connection_t *c;
577
578         if ((node == NULL) || (username == NULL) || (password == NULL))
579                 return (NULL);
580
581         fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
582         if (fd < 0)
583                 return (NULL);
584
585         c = malloc (sizeof (*c));
586         if (c == NULL)
587         {
588                 close (fd);
589                 return (NULL);
590         }
591         memset (c, 0, sizeof (*c));
592
593         c->fd = fd;
594
595         return (c);
596 } /* }}} mt_connection_t *mt_connect */
597
598 int mt_disconnect (mt_connection_t *c) /* {{{ */
599 {
600         if (c == NULL)
601                 return (EINVAL);
602
603         if (c->fd >= 0)
604         {
605                 close (c->fd);
606                 c->fd = -1;
607         }
608
609         free (c);
610
611         return (0);
612 } /* }}} int mt_disconnect */
613
614 int mt_query (mt_connection_t *c, /* {{{ */
615                 const char *command,
616                 size_t args_num, const char * const *args,
617                 mt_reply_handler_t *handler, void *user_data)
618 {
619         int status;
620         mt_reply_t *r;
621
622         status = send_command (c, command, args_num, args);
623         if (status != 0)
624                 return (status);
625
626         r = receive_reply (c);
627         if (r == NULL)
628                 return (EPROTO);
629
630         /* Call the callback function with the data we received. */
631         status = (*handler) (c, r, user_data);
632
633         /* Free the allocated memory ... */
634         reply_free (r);
635
636         /* ... and return. */
637         return (status);
638 } /* }}} int mt_query */
639
640 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
641 {
642         if (r == NULL)
643                 return (NULL);
644
645         return (r->next);
646 } /* }}} mt_reply_t *mt_reply_next */
647
648 int mt_reply_num (const mt_reply_t *r) /* {{{ */
649 {
650         int ret;
651         const mt_reply_t *ptr;
652
653         ret = 0;
654         for (ptr = r; ptr != NULL; ptr = ptr->next)
655                 ret++;
656
657         return (ret);
658 } /* }}} int mt_reply_num */
659
660 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
661                 unsigned int index)
662 {
663         if (r == NULL)
664                 return (NULL);
665
666         if (index >= r->params_num)
667                 return (NULL);
668
669         return (r->keys[index]);
670 } /* }}} char *mt_reply_param_key_by_index */
671
672 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
673                 unsigned int index)
674 {
675         if (r == NULL)
676                 return (NULL);
677
678         if (index >= r->params_num)
679                 return (NULL);
680
681         return (r->values[index]);
682 } /* }}} char *mt_reply_param_key_by_index */
683
684 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
685                 const char *key)
686 {
687         unsigned int i;
688
689         if ((r == NULL) || (key == NULL))
690                 return (NULL);
691
692         for (i = 0; i < r->params_num; i++)
693                 if (strcmp (r->keys[i], key) == 0)
694                         return (r->values[i]);
695
696         return (NULL);
697 } /* }}} char *mt_reply_param_val_by_key */
698
699 /* vim: set ts=2 sw=2 noet fdm=marker : */