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 buffer_size = sizeof (buffer) - buffer_size;
330 while (buffer_size > 0)
332 ssize_t bytes_written;
335 bytes_written = write (c->fd, buffer_ptr, buffer_size);
336 if (bytes_written < 0)
343 assert (bytes_written <= buffer_size);
345 buffer_ptr += bytes_written;
346 buffer_size -= bytes_written;
347 } /* while (buffer_size > 0) */
350 } /* }}} int send_command */
352 static int read_word (mt_connection_t *c, /* {{{ */
353 char *buffer, size_t *buffer_size)
356 uint8_t word_length[5];
359 if ((buffer == NULL) || (*buffer_size < 1))
362 /* read one byte from the socket */
363 status = read_exact (c->fd, word_length, 1);
367 /* Calculate `req_size' */
368 if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
370 status = read_exact (c->fd, &word_length[1], 4);
374 req_size = (buffer[1] << 24)
379 else if ((buffer[0] & 0xE0) == 0xE0)
381 status = read_exact (c->fd, &word_length[1], 3);
385 req_size = ((buffer[0] & 0x1F) << 24)
390 else if ((buffer[0] & 0xC0) == 0xC0)
392 status = read_exact (c->fd, &word_length[1], 2);
396 req_size = ((buffer[0] & 0x3F) << 16)
400 else if ((buffer[0] & 0x80) == 0x80)
402 status = read_exact (c->fd, &word_length[1], 1);
406 req_size = ((buffer[0] & 0x7F) << 8)
409 else if ((buffer[0] & 0x80) == 0)
411 req_size = (size_t) word_length[0];
415 /* First nibble is `F' but second nibble is not `0'. */
419 if (*buffer_size < req_size)
422 /* Empty word. This ends a `sentence' and must therefore be valid. */
430 status = read_exact (c->fd, buffer, req_size);
433 *buffer_size = req_size;
436 } /* }}} int buffer_decode_next */
438 static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
452 buffer_size = sizeof (buffer) - 1;
453 status = read_word (c, buffer, &buffer_size);
456 assert (buffer_size < sizeof (buffer));
457 buffer[buffer_size] = 0;
459 /* Empty word means end of reply */
460 if (buffer_size == 0)
463 if (buffer[0] == '!') /* {{{ */
467 tmp = reply_alloc ();
474 tmp->status = strdup (&buffer[1]);
475 if (tmp->status == NULL)
492 } /* }}} if (buffer[0] == '!') */
493 else if (buffer[0] == '=') /* {{{ */
495 char *key = &buffer[1];
499 val = strchr (key, '=');
502 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
508 reply_add_keyval (tail, key, val);
509 } /* }}} if (buffer[0] == '=') */
512 printf ("Ignoring unknown word: %s\n", buffer);
523 } /* }}} mt_reply_t *receive_reply */
525 static int create_socket (const char *node, const char *service) /* {{{ */
527 struct addrinfo ai_hint;
528 struct addrinfo *ai_list;
529 struct addrinfo *ai_ptr;
532 memset (&ai_hint, 0, sizeof (ai_hint));
534 ai_hint.ai_flags |= AI_ADDRCONFIG;
536 ai_hint.ai_family = AF_UNSPEC;
537 ai_hint.ai_socktype = SOCK_STREAM;
540 status = getaddrinfo (node, service, &ai_hint, &ai_list);
543 assert (ai_list != NULL);
545 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
550 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
551 ai_ptr->ai_protocol);
555 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
565 freeaddrinfo (ai_list);
568 } /* }}} int create_socket */
573 mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
574 const char *username, const char *password)
579 if ((node == NULL) || (username == NULL) || (password == NULL))
582 fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
586 c = malloc (sizeof (*c));
592 memset (c, 0, sizeof (*c));
597 } /* }}} mt_connection_t *mt_connect */
599 int mt_disconnect (mt_connection_t *c) /* {{{ */
613 } /* }}} int mt_disconnect */
615 int mt_query (mt_connection_t *c, /* {{{ */
617 size_t args_num, const char * const *args,
618 mt_reply_handler_t handler, void *user_data)
623 status = send_command (c, command, args_num, args);
627 r = receive_reply (c);
631 /* Call the callback function with the data we received. */
632 status = (*handler) (c, r, user_data);
634 /* Free the allocated memory ... */
637 /* ... and return. */
639 } /* }}} int mt_query */
641 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
647 } /* }}} mt_reply_t *mt_reply_next */
649 int mt_reply_num (const mt_reply_t *r) /* {{{ */
652 const mt_reply_t *ptr;
655 for (ptr = r; ptr != NULL; ptr = ptr->next)
659 } /* }}} int mt_reply_num */
661 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
667 if (index >= r->params_num)
670 return (r->keys[index]);
671 } /* }}} char *mt_reply_param_key_by_index */
673 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
679 if (index >= r->params_num)
682 return (r->values[index]);
683 } /* }}} char *mt_reply_param_key_by_index */
685 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
690 if ((r == NULL) || (key == NULL))
693 for (i = 0; i < r->params_num; i++)
694 if (strcmp (r->keys[i], key) == 0)
695 return (r->values[i]);
698 } /* }}} char *mt_reply_param_val_by_key */
700 /* vim: set ts=2 sw=2 noet fdm=marker : */