2 * libmikrotik - src/main.c
3 * Copyright (C) 2009 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
39 #include <sys/types.h>
40 #include <sys/socket.h>
43 #include "routeros_api.h"
46 # define mt_debug(...) fprintf (stdout, __VA_ARGS__)
48 # define mt_debug(...) /**/
52 char *strdup (const char *);
57 struct mt_connection_s
64 unsigned int params_num;
75 static int read_exact (int fd, void *buffer, size_t buffer_size) /* {{{ */
80 if ((fd < 0) || (buffer == NULL))
88 while (have_bytes < buffer_size)
93 want_bytes = buffer_size - have_bytes;
95 status = read (fd, buffer_ptr, want_bytes);
104 assert (status <= want_bytes);
105 have_bytes += status;
106 buffer_ptr += status;
110 } /* }}} int read_exact */
112 static mt_reply_t *reply_alloc (void) /* {{{ */
116 r = malloc (sizeof (*r));
120 memset (r, 0, sizeof (*r));
126 } /* }}} mt_reply_s *reply_alloc */
128 static int reply_add_keyval (mt_reply_t *r, const char *key, /* {{{ */
133 tmp = realloc (r->keys, (r->params_num + 1) * sizeof (*tmp));
138 tmp = realloc (r->values, (r->params_num + 1) * sizeof (*tmp));
143 r->keys[r->params_num] = strdup (key);
144 if (r->keys[r->params_num] == NULL)
147 r->values[r->params_num] = strdup (val);
148 if (r->values[r->params_num] == NULL)
150 free (r->keys[r->params_num]);
151 r->keys[r->params_num] = NULL;
157 } /* }}} int reply_add_keyval */
159 static void reply_free (mt_reply_t *r) /* {{{ */
169 for (i = 0; i < r->params_num; i++)
181 } /* }}} void reply_free */
183 static int buffer_init (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
185 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
188 if (*ret_buffer_size < 1)
192 } /* }}} int buffer_init */
194 static int buffer_add (char **ret_buffer, size_t *ret_buffer_size, /* {{{ */
202 if ((ret_buffer == NULL) || (ret_buffer_size == NULL) || (string == NULL))
205 buffer = *ret_buffer;
206 buffer_size = *ret_buffer_size;
208 string_size = strlen (string);
209 if (string_size == 0)
212 if (string_size >= 0x10000000)
213 req_size = 5 + string_size;
214 else if (string_size >= 0x200000)
215 req_size = 4 + string_size;
216 else if (string_size >= 0x4000)
217 req_size = 3 + string_size;
218 else if (string_size >= 0x80)
219 req_size = 2 + string_size;
221 req_size = 1 + string_size;
223 if (*ret_buffer_size < req_size)
226 if (string_size >= 0x10000000)
229 buffer[1] = (string_size >> 24) & 0xff;
230 buffer[2] = (string_size >> 16) & 0xff;
231 buffer[3] = (string_size >> 8) & 0xff;
232 buffer[4] = (string_size ) & 0xff;
236 else if (string_size >= 0x200000)
238 buffer[0] = (string_size >> 24) & 0x1f;
240 buffer[1] = (string_size >> 16) & 0xff;
241 buffer[2] = (string_size >> 8) & 0xff;
242 buffer[3] = (string_size ) & 0xff;
246 else if (string_size >= 0x4000)
248 buffer[0] = (string_size >> 16) & 0x3f;
250 buffer[1] = (string_size >> 8) & 0xff;
251 buffer[2] = (string_size ) & 0xff;
255 else if (string_size >= 0x80)
257 buffer[0] = (string_size >> 8) & 0x7f;
259 buffer[1] = (string_size ) & 0xff;
263 else /* if (string_size <= 0x7f) */
265 buffer[0] = (char) string_size;
270 assert (buffer_size >= string_size);
271 memcpy (buffer, string, string_size);
272 buffer += string_size;
273 buffer_size -= string_size;
275 *ret_buffer = buffer;
276 *ret_buffer_size = buffer_size;
279 } /* }}} int buffer_add */
281 static int buffer_end (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
283 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
286 if (*ret_buffer_size < 1)
289 /* Add empty word. */
290 (*ret_buffer)[0] = 0;
292 (*ret_buffer_size)--;
295 } /* }}} int buffer_end */
297 static int send_command (mt_connection_t *c, /* {{{ */
299 size_t args_num, const char * const *args)
308 /* FIXME: For debugging only */
309 memset (buffer, 0, sizeof (buffer));
312 buffer_size = sizeof (buffer);
314 status = buffer_init (&buffer_ptr, &buffer_size);
318 status = buffer_add (&buffer_ptr, &buffer_size, command);
322 for (i = 0; i < args_num; i++)
327 status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
332 status = buffer_end (&buffer_ptr, &buffer_size);
337 buffer_size = sizeof (buffer) - buffer_size;
338 while (buffer_size > 0)
340 ssize_t bytes_written;
343 bytes_written = write (c->fd, buffer_ptr, buffer_size);
344 if (bytes_written < 0)
351 assert (bytes_written <= buffer_size);
353 buffer_ptr += bytes_written;
354 buffer_size -= bytes_written;
355 } /* while (buffer_size > 0) */
358 } /* }}} int send_command */
360 static int read_word (mt_connection_t *c, /* {{{ */
361 char *buffer, size_t *buffer_size)
364 uint8_t word_length[5];
367 if ((buffer == NULL) || (*buffer_size < 1))
370 /* read one byte from the socket */
371 status = read_exact (c->fd, word_length, 1);
375 /* Calculate `req_size' */
376 if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
378 status = read_exact (c->fd, &word_length[1], 4);
382 req_size = (buffer[1] << 24)
387 else if ((buffer[0] & 0xE0) == 0xE0)
389 status = read_exact (c->fd, &word_length[1], 3);
393 req_size = ((buffer[0] & 0x1F) << 24)
398 else if ((buffer[0] & 0xC0) == 0xC0)
400 status = read_exact (c->fd, &word_length[1], 2);
404 req_size = ((buffer[0] & 0x3F) << 16)
408 else if ((buffer[0] & 0x80) == 0x80)
410 status = read_exact (c->fd, &word_length[1], 1);
414 req_size = ((buffer[0] & 0x7F) << 8)
417 else if ((buffer[0] & 0x80) == 0)
419 req_size = (size_t) word_length[0];
423 /* First nibble is `F' but second nibble is not `0'. */
427 if (*buffer_size < req_size)
430 /* Empty word. This ends a `sentence' and must therefore be valid. */
438 status = read_exact (c->fd, buffer, req_size);
441 *buffer_size = req_size;
444 } /* }}} int buffer_decode_next */
446 static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
460 buffer_size = sizeof (buffer) - 1;
461 status = read_word (c, buffer, &buffer_size);
464 assert (buffer_size < sizeof (buffer));
465 buffer[buffer_size] = 0;
467 /* Empty word means end of reply */
468 if (buffer_size == 0)
471 if (buffer[0] == '!') /* {{{ */
475 tmp = reply_alloc ();
482 tmp->status = strdup (&buffer[1]);
483 if (tmp->status == NULL)
500 } /* }}} if (buffer[0] == '!') */
501 else if (buffer[0] == '=') /* {{{ */
503 char *key = &buffer[1];
507 val = strchr (key, '=');
510 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
516 reply_add_keyval (tail, key, val);
517 } /* }}} if (buffer[0] == '=') */
520 printf ("Ignoring unknown word: %s\n", buffer);
531 } /* }}} mt_reply_t *receive_reply */
533 static int create_socket (const char *node, const char *service) /* {{{ */
535 struct addrinfo ai_hint;
536 struct addrinfo *ai_list;
537 struct addrinfo *ai_ptr;
540 mt_debug ("create_socket (node = %s, service = %s);\n",
543 memset (&ai_hint, 0, sizeof (ai_hint));
545 ai_hint.ai_flags |= AI_ADDRCONFIG;
547 ai_hint.ai_family = AF_UNSPEC;
548 ai_hint.ai_socktype = SOCK_STREAM;
551 status = getaddrinfo (node, service, &ai_hint, &ai_list);
554 assert (ai_list != NULL);
556 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
561 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
562 ai_ptr->ai_protocol);
565 mt_debug ("create_socket: socket(2) failed.\n");
569 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
572 mt_debug ("create_socket: connect(2) failed.\n");
580 freeaddrinfo (ai_list);
583 } /* }}} int create_socket */
588 mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
589 const char *username, const char *password)
594 if ((node == NULL) || (username == NULL) || (password == NULL))
597 fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
601 c = malloc (sizeof (*c));
607 memset (c, 0, sizeof (*c));
612 } /* }}} mt_connection_t *mt_connect */
614 int mt_disconnect (mt_connection_t *c) /* {{{ */
628 } /* }}} int mt_disconnect */
630 int mt_query (mt_connection_t *c, /* {{{ */
632 size_t args_num, const char * const *args,
633 mt_reply_handler_t handler, void *user_data)
638 status = send_command (c, command, args_num, args);
642 r = receive_reply (c);
646 /* Call the callback function with the data we received. */
647 status = (*handler) (c, r, user_data);
649 /* Free the allocated memory ... */
652 /* ... and return. */
654 } /* }}} int mt_query */
656 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
662 } /* }}} mt_reply_t *mt_reply_next */
664 int mt_reply_num (const mt_reply_t *r) /* {{{ */
667 const mt_reply_t *ptr;
670 for (ptr = r; ptr != NULL; ptr = ptr->next)
674 } /* }}} int mt_reply_num */
676 const char *mt_reply_status (const mt_reply_t *r) /* {{{ */
681 } /* }}} char *mt_reply_status */
683 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
689 if (index >= r->params_num)
692 return (r->keys[index]);
693 } /* }}} char *mt_reply_param_key_by_index */
695 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
701 if (index >= r->params_num)
704 return (r->values[index]);
705 } /* }}} char *mt_reply_param_key_by_index */
707 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
712 if ((r == NULL) || (key == NULL))
715 for (i = 0; i < r->params_num; i++)
716 if (strcmp (r->keys[i], key) == 0)
717 return (r->values[i]);
720 } /* }}} char *mt_reply_param_val_by_key */
722 /* vim: set ts=2 sw=2 noet fdm=marker : */