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"
47 char *strdup (const char *);
52 struct mt_connection_s
59 unsigned int params_num;
70 static int read_exact (int fd, void *buffer, size_t buffer_size) /* {{{ */
75 if ((fd < 0) || (buffer == NULL))
83 while (have_bytes < buffer_size)
88 want_bytes = buffer_size - have_bytes;
90 status = read (fd, buffer_ptr, want_bytes);
99 assert (status <= want_bytes);
100 have_bytes += status;
101 buffer_ptr += status;
105 } /* }}} int read_exact */
107 static mt_reply_t *reply_alloc (void) /* {{{ */
111 r = malloc (sizeof (*r));
115 memset (r, 0, sizeof (*r));
121 } /* }}} mt_reply_s *reply_alloc */
123 static int reply_add_keyval (mt_reply_t *r, const char *key, /* {{{ */
128 tmp = realloc (r->keys, (r->params_num + 1) * sizeof (*tmp));
133 tmp = realloc (r->values, (r->params_num + 1) * sizeof (*tmp));
138 r->keys[r->params_num] = strdup (key);
139 if (r->keys[r->params_num] == NULL)
142 r->values[r->params_num] = strdup (val);
143 if (r->values[r->params_num] == NULL)
145 free (r->keys[r->params_num]);
146 r->keys[r->params_num] = NULL;
152 } /* }}} int reply_add_keyval */
154 static void reply_free (mt_reply_t *r) /* {{{ */
164 for (i = 0; i < r->params_num; i++)
176 } /* }}} void reply_free */
178 static int buffer_init (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
180 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
183 if (*ret_buffer_size < 1)
187 } /* }}} int buffer_init */
189 static int buffer_add (char **ret_buffer, size_t *ret_buffer_size, /* {{{ */
197 if ((ret_buffer == NULL) || (ret_buffer_size == NULL) || (string == NULL))
200 buffer = *ret_buffer;
201 buffer_size = *ret_buffer_size;
203 string_size = strlen (string);
204 if (string_size == 0)
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;
216 req_size = 1 + string_size;
218 if (*ret_buffer_size < req_size)
221 if (string_size >= 0x10000000)
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;
231 else if (string_size >= 0x200000)
233 buffer[0] = (string_size >> 24) & 0x1f;
235 buffer[1] = (string_size >> 16) & 0xff;
236 buffer[2] = (string_size >> 8) & 0xff;
237 buffer[3] = (string_size ) & 0xff;
241 else if (string_size >= 0x4000)
243 buffer[0] = (string_size >> 16) & 0x3f;
245 buffer[1] = (string_size >> 8) & 0xff;
246 buffer[2] = (string_size ) & 0xff;
250 else if (string_size >= 0x80)
252 buffer[0] = (string_size >> 8) & 0x7f;
254 buffer[1] = (string_size ) & 0xff;
258 else /* if (string_size <= 0x7f) */
260 buffer[0] = (char) string_size;
265 assert (buffer_size >= string_size);
266 memcpy (buffer, string, string_size);
267 buffer += string_size;
268 buffer_size -= string_size;
270 *ret_buffer = buffer;
271 *ret_buffer_size = buffer_size;
274 } /* }}} int buffer_add */
276 static int buffer_end (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
278 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
281 if (*ret_buffer_size < 1)
284 /* Add empty word. */
285 (*ret_buffer)[0] = 0;
287 (*ret_buffer_size)--;
290 } /* }}} int buffer_end */
292 static int send_command (mt_connection_t *c, /* {{{ */
294 size_t args_num, const char * const *args)
304 buffer_size = sizeof (buffer);
306 status = buffer_init (&buffer_ptr, &buffer_size);
310 status = buffer_add (&buffer_ptr, &buffer_size, command);
314 for (i = 0; i < args_num; i++)
319 status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
324 status = buffer_end (&buffer_ptr, &buffer_size);
329 while (buffer_size > 0)
331 ssize_t bytes_written;
334 bytes_written = write (c->fd, buffer_ptr, buffer_size);
335 if (bytes_written < 0)
342 assert (bytes_written <= buffer_size);
344 buffer_ptr += bytes_written;
345 buffer_size -= bytes_written;
346 } /* while (buffer_size > 0) */
349 } /* }}} int send_command */
351 static int read_word (mt_connection_t *c, /* {{{ */
352 char *buffer, size_t *buffer_size)
355 uint8_t word_length[5];
358 if ((buffer == NULL) || (*buffer_size < 1))
361 /* read one byte from the socket */
362 status = read_exact (c->fd, word_length, 1);
366 /* Calculate `req_size' */
367 if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
369 status = read_exact (c->fd, &word_length[1], 4);
373 req_size = (buffer[1] << 24)
378 else if ((buffer[0] & 0xE0) == 0xE0)
380 status = read_exact (c->fd, &word_length[1], 3);
384 req_size = ((buffer[0] & 0x1F) << 24)
389 else if ((buffer[0] & 0xC0) == 0xC0)
391 status = read_exact (c->fd, &word_length[1], 2);
395 req_size = ((buffer[0] & 0x3F) << 16)
399 else if ((buffer[0] & 0x80) == 0x80)
401 status = read_exact (c->fd, &word_length[1], 1);
405 req_size = ((buffer[0] & 0x7F) << 8)
408 else if ((buffer[0] & 0x80) == 0)
410 req_size = (size_t) word_length[0];
414 /* First nibble is `F' but second nibble is not `0'. */
418 if (*buffer_size < req_size)
421 /* Empty word. This ends a `sentence' and must therefore be valid. */
429 status = read_exact (c->fd, buffer, req_size);
432 *buffer_size = req_size;
435 } /* }}} int buffer_decode_next */
437 static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
451 buffer_size = sizeof (buffer) - 1;
452 status = read_word (c, buffer, &buffer_size);
455 assert (buffer_size < sizeof (buffer));
456 buffer[buffer_size] = 0;
458 /* Empty word means end of reply */
459 if (buffer_size == 0)
462 if (buffer[0] == '!') /* {{{ */
466 tmp = reply_alloc ();
473 tmp->status = strdup (&buffer[1]);
474 if (tmp->status == NULL)
491 } /* }}} if (buffer[0] == '!') */
492 else if (buffer[0] == '=') /* {{{ */
494 char *key = &buffer[1];
498 val = strchr (key, '=');
501 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
507 reply_add_keyval (tail, key, val);
508 } /* }}} if (buffer[0] == '=') */
511 printf ("Ignoring unknown word: %s\n", buffer);
522 } /* }}} mt_reply_t *receive_reply */
524 static int create_socket (const char *node, const char *service) /* {{{ */
526 struct addrinfo ai_hint;
527 struct addrinfo *ai_list;
528 struct addrinfo *ai_ptr;
531 memset (&ai_hint, 0, sizeof (ai_hint));
533 ai_hint.ai_flags |= AI_ADDRCONFIG;
535 ai_hint.ai_family = AF_UNSPEC;
536 ai_hint.ai_socktype = SOCK_STREAM;
539 status = getaddrinfo (node, service, &ai_hint, &ai_list);
542 assert (ai_list != NULL);
544 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
549 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
550 ai_ptr->ai_protocol);
554 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
564 freeaddrinfo (ai_list);
567 } /* }}} int create_socket */
572 mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
573 const char *username, const char *password)
578 if ((node == NULL) || (username == NULL) || (password == NULL))
581 fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
585 c = malloc (sizeof (*c));
591 memset (c, 0, sizeof (*c));
596 } /* }}} mt_connection_t *mt_connect */
598 int mt_disconnect (mt_connection_t *c) /* {{{ */
612 } /* }}} int mt_disconnect */
614 int mt_query (mt_connection_t *c, /* {{{ */
616 size_t args_num, const char * const *args,
617 mt_reply_handler_t *handler, void *user_data)
622 status = send_command (c, command, args_num, args);
626 r = receive_reply (c);
630 /* Call the callback function with the data we received. */
631 status = (*handler) (c, r, user_data);
633 /* Free the allocated memory ... */
636 /* ... and return. */
638 } /* }}} int mt_query */
640 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
646 } /* }}} mt_reply_t *mt_reply_next */
648 int mt_reply_num (const mt_reply_t *r) /* {{{ */
651 const mt_reply_t *ptr;
654 for (ptr = r; ptr != NULL; ptr = ptr->next)
658 } /* }}} int mt_reply_num */
660 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
666 if (index >= r->params_num)
669 return (r->keys[index]);
670 } /* }}} char *mt_reply_param_key_by_index */
672 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
678 if (index >= r->params_num)
681 return (r->values[index]);
682 } /* }}} char *mt_reply_param_key_by_index */
684 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
689 if ((r == NULL) || (key == NULL))
692 for (i = 0; i < r->params_num; i++)
693 if (strcmp (r->keys[i], key) == 0)
694 return (r->values[i]);
697 } /* }}} char *mt_reply_param_val_by_key */
699 /* vim: set ts=2 sw=2 noet fdm=marker : */