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>
31 #include "routeros_api.h"
36 struct mt_connection_s
43 unsigned int params_num;
54 static mt_reply_t *reply_alloc (void) /* {{{ */
58 r = malloc (sizeof (*r));
62 memset (r, 0, sizeof (*r));
68 } /* }}} mt_reply_s *reply_alloc */
70 static void reply_free (mt_reply_t *r) /* {{{ */
84 } /* }}} void reply_free */
86 static int buffer_init (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
88 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
91 if (*ret_buffer_size < 1)
95 } /* }}} int buffer_init */
97 static int buffer_add (char **ret_buffer, size_t *ret_buffer_size, /* {{{ */
105 if ((ret_buffer == NULL) || (ret_buffer_size == NULL) || (string == NULL))
108 buffer = *ret_buffer;
109 buffer_size = *ret_buffer_size;
111 string_size = strlen (string);
112 if (string_size == 0)
115 if (string_size >= 0x10000000)
116 req_size = 5 + string_size;
117 else if (string_size >= 0x200000)
118 req_size = 4 + string_size;
119 else if (string_size >= 0x4000)
120 req_size = 3 + string_size;
121 else if (string_size >= 0x80)
122 req_size = 2 + string_size;
124 req_size = 1 + string_size;
126 if (*ret_buffer_size < req_size)
129 if (string_size >= 0x10000000)
132 buffer[1] = (string_size >> 24) & 0xff;
133 buffer[2] = (string_size >> 16) & 0xff;
134 buffer[3] = (string_size >> 8) & 0xff;
135 buffer[4] = (string_size ) & 0xff;
139 else if (string_size >= 0x200000)
141 buffer[0] = (string_size >> 24) & 0x1f;
143 buffer[1] = (string_size >> 16) & 0xff;
144 buffer[2] = (string_size >> 8) & 0xff;
145 buffer[3] = (string_size ) & 0xff;
149 else if (string_size >= 0x4000)
151 buffer[0] = (string_size >> 16) & 0x3f;
153 buffer[1] = (string_size >> 8) & 0xff;
154 buffer[2] = (string_size ) & 0xff;
158 else if (string_size >= 0x80)
160 buffer[0] = (string_size >> 8) & 0x7f;
162 buffer[1] = (string_size ) & 0xff;
166 else /* if (string_size <= 0x7f) */
168 buffer[0] = (char) string_size;
173 assert (buffer_size >= string_size);
174 memcpy (buffer, string, string_size);
175 buffer += string_size;
176 buffer_size -= string_size;
178 *ret_buffer = buffer;
179 *ret_buffer_size = buffer_size;
182 } /* }}} int buffer_add */
184 static int buffer_end (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
186 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
189 if (*ret_buffer_size < 1)
192 /* Add empty word. */
193 (*ret_buffer)[0] = 0;
195 (*ret_buffer_size)--;
198 } /* }}} int buffer_end */
200 static int buffer_decode_next (char **ret_buffer, size_t *ret_buffer_size, /* {{{ */
201 char *dst, size_t *dst_size)
207 if ((ret_buffer == NULL) || (ret_buffer_size == NULL) || (dst_size == NULL))
210 buffer = (uint8_t *) (*ret_buffer);
211 buffer_size = *ret_buffer_size;
216 /* Calculate `req_size' and update `buffer' and `buffer_size'. */
217 if (buffer[0] == 0xF0) /* {{{ */
219 if (buffer_size < (0x10000000 + 5))
221 req_size = (buffer[1] << 24)
228 else if ((buffer[0] & 0xE0) == 0xE0)
230 if (buffer_size < (0x200000 + 4))
232 req_size = ((buffer[0] & 0x1F) << 24)
239 else if ((buffer[0] & 0xC0) == 0xC0)
241 if (buffer_size < (0x4000 + 3))
243 req_size = ((buffer[0] & 0x3F) << 16)
249 else if ((buffer[0] & 0x80) == 0x80)
251 if (buffer_size < (0x80 + 2))
253 req_size = ((buffer[0] & 0x7F) << 8) | buffer[1];
257 else if ((buffer[0] & 0x80) == 0)
259 req_size = buffer[0];
265 /* First nibble is `F' but second nibble is not `0'. */
269 if (buffer_size < req_size)
274 *dst_size = (req_size + 1);
278 if (*dst_size <= req_size)
281 memcpy (dst, buffer, req_size);
283 *dst_size = (req_size + 1);
285 assert (buffer_size >= req_size);
287 buffer_size -= req_size;
289 *ret_buffer = buffer;
290 *ret_buffer_size = buffer_size;
293 } /* }}} int buffer_decode_next */
298 mt_connection_t *mt_connect (const char *node, const char *service,
299 const char *username, const char *password);
300 int mt_disconnect (mt_connection_t *con);
302 int mt_query (mt_connection_t *c,
304 size_t args_num, const char * const *args,
305 mt_reply_handler_t *handler, void *user_data)
315 buffer_size = sizeof (buffer);
317 status = buffer_init (&buffer_ptr, &buffer_size);
321 status = buffer_add (&buffer_ptr, &buffer_size, command);
325 for (i = 0; i < args_num; i++)
330 status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
335 status = buffer_end (&buffer_ptr, &buffer_size);
339 /* FIXME: Send out the buffer and read back results. */
342 const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
348 } /* }}} mt_reply_t *mt_reply_next */
350 int mt_reply_num (const mt_reply_t *r) /* {{{ */
353 const mt_reply_t *ptr;
356 for (ptr = r; ptr != NULL; ptr = ptr->next)
360 } /* }}} int mt_reply_num */
362 const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
368 if (index >= r->params_num)
371 return (r->keys[index]);
372 } /* }}} char *mt_reply_param_key_by_index */
374 const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
380 if (index >= r->params_num)
383 return (r->values[index]);
384 } /* }}} char *mt_reply_param_key_by_index */
386 const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
391 if ((r == NULL) || (key == NULL))
394 for (i = 0; i < r->params_num; i++)
395 if (strcmp (r->keys[i], key) == 0)
396 return (r->values[i]);
399 } /* }}} char *mt_reply_param_val_by_key */
401 /* vim: set ts=2 sw=2 noet fdm=marker : */