c1a7aa3ede1f13c971f962c88413c3767a4d8dc0
[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         buffer_size = sizeof (buffer) - buffer_size;
330         while (buffer_size > 0)
331         {
332                 ssize_t bytes_written;
333
334                 errno = 0;
335                 bytes_written = write (c->fd, buffer_ptr, buffer_size);
336                 if (bytes_written < 0)
337                 {
338                         if (errno == EAGAIN)
339                                 continue;
340                         else
341                                 return (errno);
342                 }
343                 assert (bytes_written <= buffer_size);
344
345                 buffer_ptr += bytes_written;
346                 buffer_size -= bytes_written;
347         } /* while (buffer_size > 0) */
348
349         return (0);
350 } /* }}} int send_command */
351
352 static int read_word (mt_connection_t *c, /* {{{ */
353                 char *buffer, size_t *buffer_size)
354 {
355         size_t req_size;
356         uint8_t word_length[5];
357         int status;
358
359         if ((buffer == NULL) || (*buffer_size < 1))
360                 return (EINVAL);
361
362         /* read one byte from the socket */
363         status = read_exact (c->fd, word_length, 1);
364         if (status != 0)
365                 return (status);
366
367         /* Calculate `req_size' */
368         if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
369         {
370                 status = read_exact (c->fd, &word_length[1], 4);
371                 if (status != 0)
372                         return (status);
373
374                 req_size = (buffer[1] << 24)
375                         | (buffer[2] << 16)
376                         | (buffer[3] << 8)
377                         | buffer[4];
378         }
379         else if ((buffer[0] & 0xE0) == 0xE0)
380         {
381                 status = read_exact (c->fd, &word_length[1], 3);
382                 if (status != 0)
383                         return (status);
384
385                 req_size = ((buffer[0] & 0x1F) << 24)
386                         | (buffer[1] << 16)
387                         | (buffer[2] << 8)
388                         | buffer[3];
389         }
390         else if ((buffer[0] & 0xC0) == 0xC0)
391         {
392                 status = read_exact (c->fd, &word_length[1], 2);
393                 if (status != 0)
394                         return (status);
395
396                 req_size = ((buffer[0] & 0x3F) << 16)
397                         | (buffer[1] << 8)
398                         | buffer[2];
399         }
400         else if ((buffer[0] & 0x80) == 0x80)
401         {
402                 status = read_exact (c->fd, &word_length[1], 1);
403                 if (status != 0)
404                         return (status);
405
406                 req_size = ((buffer[0] & 0x7F) << 8)
407                         | buffer[1];
408         }
409         else if ((buffer[0] & 0x80) == 0)
410         {
411                 req_size = (size_t) word_length[0];
412         }
413         else
414         {
415                 /* First nibble is `F' but second nibble is not `0'. */
416                 return (EPROTO);
417         } /* }}} */
418
419         if (*buffer_size < req_size)
420                 return (ENOMEM);
421
422         /* Empty word. This ends a `sentence' and must therefore be valid. */
423         if (req_size == 0)
424         {
425                 buffer[0] = 0;
426                 *buffer_size = 0;
427                 return (0);
428         }
429
430         status = read_exact (c->fd, buffer, req_size);
431         if (status != 0)
432                 return (status);
433         *buffer_size = req_size;
434
435         return (0);
436 } /* }}} int buffer_decode_next */
437
438 static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
439 {
440         char buffer[4096];
441         size_t buffer_size;
442         int status;
443
444         mt_reply_t *head;
445         mt_reply_t *tail;
446
447         head = NULL;
448         tail = NULL;
449
450         while (42)
451         {
452                 buffer_size = sizeof (buffer) - 1;
453                 status = read_word (c, buffer, &buffer_size);
454                 if (status != 0)
455                         break;
456                 assert (buffer_size < sizeof (buffer));
457                 buffer[buffer_size] = 0;
458
459                 /* Empty word means end of reply */
460                 if (buffer_size == 0)
461                         break;
462
463                 if (buffer[0] == '!') /* {{{ */
464                 {
465                         mt_reply_t *tmp;
466
467                         tmp = reply_alloc ();
468                         if (tmp == NULL)
469                         {
470                                 status = ENOMEM;
471                                 break;
472                         }
473
474                         tmp->status = strdup (&buffer[1]);
475                         if (tmp->status == NULL)
476                         {
477                                 reply_free (tmp);
478                                 status = ENOMEM;
479                                 break;
480                         }
481
482                         if (tail == NULL)
483                         {
484                                 head = tmp;
485                                 tail = tmp;
486                         }
487                         else
488                         {
489                                 tail->next = tmp;
490                                 tail = tmp;
491                         }
492                 } /* }}} if (buffer[0] == '!') */
493                 else if (buffer[0] == '=') /* {{{ */
494                 {
495                         char *key = &buffer[1];
496                         char *val;
497
498                         key = &buffer[1];
499                         val = strchr (key, '=');
500                         if (val == NULL)
501                         {
502                                 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
503                                 continue;
504                         }
505                         *val = 0;
506                         val++;
507
508                         reply_add_keyval (tail, key, val);
509                 } /* }}} if (buffer[0] == '=') */
510                 else
511                 {
512                         printf ("Ignoring unknown word: %s\n", buffer);
513                 }
514         } /* while (42) */
515         
516         if (status != 0)
517         {
518                 reply_free (head);
519                 return (NULL);
520         }
521
522         return (head);
523 } /* }}} mt_reply_t *receive_reply */
524
525 static int create_socket (const char *node, const char *service) /* {{{ */
526 {
527         struct addrinfo  ai_hint;
528         struct addrinfo *ai_list;
529         struct addrinfo *ai_ptr;
530         int status;
531
532         memset (&ai_hint, 0, sizeof (ai_hint));
533 #ifdef AI_ADDRCONFIG
534         ai_hint.ai_flags |= AI_ADDRCONFIG;
535 #endif
536         ai_hint.ai_family = AF_UNSPEC;
537         ai_hint.ai_socktype = SOCK_STREAM;
538
539         ai_list = NULL;
540         status = getaddrinfo (node, service, &ai_hint, &ai_list);
541         if (status != 0)
542                 return (-1);
543         assert (ai_list != NULL);
544
545         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
546         {
547                 int fd;
548                 int status;
549
550                 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
551                                 ai_ptr->ai_protocol);
552                 if (fd < 0)
553                         continue;
554
555                 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
556                 if (status != 0)
557                 {
558                         close (fd);
559                         continue;
560                 }
561
562                 return (fd);
563         }
564
565         freeaddrinfo (ai_list);
566
567         return (-1);
568 } /* }}} int create_socket */
569
570 /*
571  * Public functions
572  */
573 mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
574                 const char *username, const char *password)
575 {
576         int fd;
577         mt_connection_t *c;
578
579         if ((node == NULL) || (username == NULL) || (password == NULL))
580                 return (NULL);
581
582         fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
583         if (fd < 0)
584                 return (NULL);
585
586         c = malloc (sizeof (*c));
587         if (c == NULL)
588         {
589                 close (fd);
590                 return (NULL);
591         }
592         memset (c, 0, sizeof (*c));
593
594         c->fd = fd;
595
596         return (c);
597 } /* }}} mt_connection_t *mt_connect */
598
599 int mt_disconnect (mt_connection_t *c) /* {{{ */
600 {
601         if (c == NULL)
602                 return (EINVAL);
603
604         if (c->fd >= 0)
605         {
606                 close (c->fd);
607                 c->fd = -1;
608         }
609
610         free (c);
611
612         return (0);
613 } /* }}} int mt_disconnect */
614
615 int mt_query (mt_connection_t *c, /* {{{ */
616                 const char *command,
617                 size_t args_num, const char * const *args,
618                 mt_reply_handler_t handler, void *user_data)
619 {
620         int status;
621         mt_reply_t *r;
622
623         status = send_command (c, command, args_num, args);
624         if (status != 0)
625                 return (status);
626
627         r = receive_reply (c);
628         if (r == NULL)
629                 return (EPROTO);
630
631         /* Call the callback function with the data we received. */
632         status = (*handler) (c, r, user_data);
633
634         /* Free the allocated memory ... */
635         reply_free (r);
636
637         /* ... and return. */
638         return (status);
639 } /* }}} int mt_query */
640
641 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
642 {
643         if (r == NULL)
644                 return (NULL);
645
646         return (r->next);
647 } /* }}} mt_reply_t *mt_reply_next */
648
649 int mt_reply_num (const mt_reply_t *r) /* {{{ */
650 {
651         int ret;
652         const mt_reply_t *ptr;
653
654         ret = 0;
655         for (ptr = r; ptr != NULL; ptr = ptr->next)
656                 ret++;
657
658         return (ret);
659 } /* }}} int mt_reply_num */
660
661 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
662                 unsigned int index)
663 {
664         if (r == NULL)
665                 return (NULL);
666
667         if (index >= r->params_num)
668                 return (NULL);
669
670         return (r->keys[index]);
671 } /* }}} char *mt_reply_param_key_by_index */
672
673 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
674                 unsigned int index)
675 {
676         if (r == NULL)
677                 return (NULL);
678
679         if (index >= r->params_num)
680                 return (NULL);
681
682         return (r->values[index]);
683 } /* }}} char *mt_reply_param_key_by_index */
684
685 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
686                 const char *key)
687 {
688         unsigned int i;
689
690         if ((r == NULL) || (key == NULL))
691                 return (NULL);
692
693         for (i = 0; i < r->params_num; i++)
694                 if (strcmp (r->keys[i], key) == 0)
695                         return (r->values[i]);
696
697         return (NULL);
698 } /* }}} char *mt_reply_param_val_by_key */
699
700 /* vim: set ts=2 sw=2 noet fdm=marker : */