* Florian octo Forster <octo at verplant.org>
**/
+#ifndef _ISOC99_SOURCE
+# define _ISOC99_SOURCE
+#endif
+
+#ifndef _POSIX_C_SOURCE
+# define _POSIX_C_SOURCE 200112L
+#endif
+
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+
#include "routeros_api.h"
+
+/* FIXME */
+char *strdup (const char *);
+
/*
* Private structures
*/
/*
* Private functions
*/
+static int read_exact (int fd, void *buffer, size_t buffer_size) /* {{{ */
+{
+ char *buffer_ptr;
+ size_t have_bytes;
+
+ if ((fd < 0) || (buffer == NULL))
+ return (EINVAL);
+
+ if (buffer_size == 0)
+ return (0);
+
+ buffer_ptr = buffer;
+ have_bytes = 0;
+ while (have_bytes < buffer_size)
+ {
+ size_t want_bytes;
+ ssize_t status;
+
+ want_bytes = buffer_size - have_bytes;
+ errno = 0;
+ status = read (fd, buffer_ptr, want_bytes);
+ if (status < 0)
+ {
+ if (errno == EAGAIN)
+ continue;
+ else
+ return (status);
+ }
+
+ assert (status <= want_bytes);
+ have_bytes += status;
+ buffer_ptr += status;
+ }
+
+ return (0);
+} /* }}} int read_exact */
+
static mt_reply_t *reply_alloc (void) /* {{{ */
{
mt_reply_t *r;
return (r);
} /* }}} mt_reply_s *reply_alloc */
+static int reply_add_keyval (mt_reply_t *r, const char *key, /* {{{ */
+ const char *val)
+{
+ char **tmp;
+
+ tmp = realloc (r->keys, (r->params_num + 1) * sizeof (*tmp));
+ if (tmp == NULL)
+ return (ENOMEM);
+ r->keys = tmp;
+
+ tmp = realloc (r->values, (r->params_num + 1) * sizeof (*tmp));
+ if (tmp == NULL)
+ return (ENOMEM);
+ r->values = tmp;
+
+ r->keys[r->params_num] = strdup (key);
+ if (r->keys[r->params_num] == NULL)
+ return (ENOMEM);
+
+ r->values[r->params_num] = strdup (val);
+ if (r->values[r->params_num] == NULL)
+ {
+ free (r->keys[r->params_num]);
+ r->keys[r->params_num] = NULL;
+ return (ENOMEM);
+ }
+
+ r->params_num++;
+ return (0);
+} /* }}} int reply_add_keyval */
+
static void reply_free (mt_reply_t *r) /* {{{ */
{
mt_reply_t *next;
+ unsigned int i;
if (r == NULL)
return;
next = r->next;
+ for (i = 0; i < r->params_num; i++)
+ {
+ free (r->keys[i]);
+ free (r->values[i]);
+ }
+
free (r->keys);
free (r->values);
+
free (r);
reply_free (next);
return (0);
} /* }}} int buffer_end */
-static int buffer_decode_next (char **ret_buffer, size_t *ret_buffer_size, /* {{{ */
- char *dst, size_t *dst_size)
+static int send_command (mt_connection_t *c, /* {{{ */
+ const char *command,
+ size_t args_num, const char * const *args)
{
- uint8_t *buffer;
+ char buffer[4096];
+ char *buffer_ptr;
size_t buffer_size;
- size_t req_size;
- if ((ret_buffer == NULL) || (ret_buffer_size == NULL) || (dst_size == NULL))
- return (EINVAL);
+ size_t i;
+ int status;
- buffer = (uint8_t *) (*ret_buffer);
- buffer_size = *ret_buffer_size;
+ buffer_ptr = buffer;
+ buffer_size = sizeof (buffer);
- if (buffer_size < 1)
+ status = buffer_init (&buffer_ptr, &buffer_size);
+ if (status != 0)
+ return (status);
+
+ status = buffer_add (&buffer_ptr, &buffer_size, command);
+ if (status != 0)
+ return (status);
+
+ for (i = 0; i < args_num; i++)
+ {
+ if (args[i] == NULL)
+ return (EINVAL);
+
+ status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
+ if (status != 0)
+ return (status);
+ }
+
+ status = buffer_end (&buffer_ptr, &buffer_size);
+ if (status != 0)
+ return (status);
+
+ buffer_ptr = buffer;
+ while (buffer_size > 0)
+ {
+ ssize_t bytes_written;
+
+ errno = 0;
+ bytes_written = write (c->fd, buffer_ptr, buffer_size);
+ if (bytes_written < 0)
+ {
+ if (errno == EAGAIN)
+ continue;
+ else
+ return (errno);
+ }
+ assert (bytes_written <= buffer_size);
+
+ buffer_ptr += bytes_written;
+ buffer_size -= bytes_written;
+ } /* while (buffer_size > 0) */
+
+ return (0);
+} /* }}} int send_command */
+
+static int read_word (mt_connection_t *c, /* {{{ */
+ char *buffer, size_t *buffer_size)
+{
+ size_t req_size;
+ uint8_t word_length[5];
+ int status;
+
+ if ((buffer == NULL) || (*buffer_size < 1))
return (EINVAL);
- /* Calculate `req_size' and update `buffer' and `buffer_size'. */
- if (buffer[0] == 0xF0) /* {{{ */
+ /* read one byte from the socket */
+ status = read_exact (c->fd, word_length, 1);
+ if (status != 0)
+ return (status);
+
+ /* Calculate `req_size' */
+ if (((unsigned char) buffer[0]) == 0xF0) /* {{{ */
{
- if (buffer_size < (0x10000000 + 5))
- return (EPROTO);
+ status = read_exact (c->fd, &word_length[1], 4);
+ if (status != 0)
+ return (status);
+
req_size = (buffer[1] << 24)
| (buffer[2] << 16)
| (buffer[3] << 8)
| buffer[4];
- buffer += 5;
- buffer_size -= 5;
}
else if ((buffer[0] & 0xE0) == 0xE0)
{
- if (buffer_size < (0x200000 + 4))
- return (EPROTO);
+ status = read_exact (c->fd, &word_length[1], 3);
+ if (status != 0)
+ return (status);
+
req_size = ((buffer[0] & 0x1F) << 24)
| (buffer[1] << 16)
| (buffer[2] << 8)
| buffer[3];
- buffer += 4;
- buffer_size -= 4;
}
else if ((buffer[0] & 0xC0) == 0xC0)
{
- if (buffer_size < (0x4000 + 3))
- return (EPROTO);
+ status = read_exact (c->fd, &word_length[1], 2);
+ if (status != 0)
+ return (status);
+
req_size = ((buffer[0] & 0x3F) << 16)
| (buffer[1] << 8)
| buffer[2];
- buffer += 3;
- buffer_size -= 3;
}
else if ((buffer[0] & 0x80) == 0x80)
{
- if (buffer_size < (0x80 + 2))
- return (EPROTO);
- req_size = ((buffer[0] & 0x7F) << 8) | buffer[1];
- buffer += 2;
- buffer_size -= 2;
+ status = read_exact (c->fd, &word_length[1], 1);
+ if (status != 0)
+ return (status);
+
+ req_size = ((buffer[0] & 0x7F) << 8)
+ | buffer[1];
}
else if ((buffer[0] & 0x80) == 0)
{
- req_size = buffer[0];
- buffer += 1;
- buffer_size -= 1;
+ req_size = (size_t) word_length[0];
}
else
{
return (EPROTO);
} /* }}} */
- if (buffer_size < req_size)
- return (EPROTO);
+ if (*buffer_size < req_size)
+ return (ENOMEM);
- if (dst == NULL)
+ /* Empty word. This ends a `sentence' and must therefore be valid. */
+ if (req_size == 0)
{
- *dst_size = (req_size + 1);
+ buffer[0] = 0;
+ *buffer_size = 0;
return (0);
}
- if (*dst_size <= req_size)
- return (ENOMEM);
-
- memcpy (dst, buffer, req_size);
- dst[req_size] = 0;
- *dst_size = (req_size + 1);
-
- assert (buffer_size >= req_size);
- buffer += req_size;
- buffer_size -= req_size;
+ status = read_exact (c->fd, buffer, req_size);
+ if (status != 0)
+ return (status);
+ *buffer_size = req_size;
- *ret_buffer = buffer;
- *ret_buffer_size = buffer_size;
-
return (0);
} /* }}} int buffer_decode_next */
-/*
- * Public functions
- */
-mt_connection_t *mt_connect (const char *node, const char *service,
- const char *username, const char *password);
-int mt_disconnect (mt_connection_t *con);
-
-int mt_query (mt_connection_t *c,
- const char *command,
- size_t args_num, const char * const *args,
- mt_reply_handler_t *handler, void *user_data)
+static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
{
char buffer[4096];
- char *buffer_ptr;
size_t buffer_size;
-
- size_t i;
int status;
- buffer_ptr = buffer;
- buffer_size = sizeof (buffer);
+ mt_reply_t *head;
+ mt_reply_t *tail;
- status = buffer_init (&buffer_ptr, &buffer_size);
+ head = NULL;
+ tail = NULL;
+
+ while (42)
+ {
+ buffer_size = sizeof (buffer) - 1;
+ status = read_word (c, buffer, &buffer_size);
+ if (status != 0)
+ break;
+ assert (buffer_size < sizeof (buffer));
+ buffer[buffer_size] = 0;
+
+ /* Empty word means end of reply */
+ if (buffer_size == 0)
+ break;
+
+ if (buffer[0] == '!') /* {{{ */
+ {
+ mt_reply_t *tmp;
+
+ tmp = reply_alloc ();
+ if (tmp == NULL)
+ {
+ status = ENOMEM;
+ break;
+ }
+
+ tmp->status = strdup (&buffer[1]);
+ if (tmp->status == NULL)
+ {
+ reply_free (tmp);
+ status = ENOMEM;
+ break;
+ }
+
+ if (tail == NULL)
+ {
+ head = tmp;
+ tail = tmp;
+ }
+ else
+ {
+ tail->next = tmp;
+ tail = tmp;
+ }
+ } /* }}} if (buffer[0] == '!') */
+ else if (buffer[0] == '=') /* {{{ */
+ {
+ char *key = &buffer[1];
+ char *val;
+
+ key = &buffer[1];
+ val = strchr (key, '=');
+ if (val == NULL)
+ {
+ fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
+ continue;
+ }
+ *val = 0;
+ val++;
+
+ reply_add_keyval (tail, key, val);
+ } /* }}} if (buffer[0] == '=') */
+ else
+ {
+ printf ("Ignoring unknown word: %s\n", buffer);
+ }
+ } /* while (42) */
+
if (status != 0)
- return (status);
+ {
+ reply_free (head);
+ return (NULL);
+ }
- status = buffer_add (&buffer_ptr, &buffer_size, command);
+ return (head);
+} /* }}} mt_reply_t *receive_reply */
+
+static int create_socket (const char *node, const char *service) /* {{{ */
+{
+ struct addrinfo ai_hint;
+ struct addrinfo *ai_list;
+ struct addrinfo *ai_ptr;
+ int status;
+
+ memset (&ai_hint, 0, sizeof (ai_hint));
+#ifdef AI_ADDRCONFIG
+ ai_hint.ai_flags |= AI_ADDRCONFIG;
+#endif
+ ai_hint.ai_family = AF_UNSPEC;
+ ai_hint.ai_socktype = SOCK_STREAM;
+
+ ai_list = NULL;
+ status = getaddrinfo (node, service, &ai_hint, &ai_list);
if (status != 0)
- return (status);
+ return (-1);
+ assert (ai_list != NULL);
- for (i = 0; i < args_num; i++)
+ for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
{
- if (args[i] == NULL)
- return (EINVAL);
+ int fd;
+ int status;
- status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
+ fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
+ ai_ptr->ai_protocol);
+ if (fd < 0)
+ continue;
+
+ status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
if (status != 0)
- return (status);
+ {
+ close (fd);
+ continue;
+ }
+
+ return (fd);
}
- status = buffer_end (&buffer_ptr, &buffer_size);
+ freeaddrinfo (ai_list);
+
+ return (-1);
+} /* }}} int create_socket */
+
+/*
+ * Public functions
+ */
+mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
+ const char *username, const char *password)
+{
+ int fd;
+ mt_connection_t *c;
+
+ if ((node == NULL) || (username == NULL) || (password == NULL))
+ return (NULL);
+
+ fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
+ if (fd < 0)
+ return (NULL);
+
+ c = malloc (sizeof (*c));
+ if (c == NULL)
+ {
+ close (fd);
+ return (NULL);
+ }
+ memset (c, 0, sizeof (*c));
+
+ c->fd = fd;
+
+ return (c);
+} /* }}} mt_connection_t *mt_connect */
+
+int mt_disconnect (mt_connection_t *c) /* {{{ */
+{
+ if (c == NULL)
+ return (EINVAL);
+
+ if (c->fd >= 0)
+ {
+ close (c->fd);
+ c->fd = -1;
+ }
+
+ free (c);
+
+ return (0);
+} /* }}} int mt_disconnect */
+
+int mt_query (mt_connection_t *c, /* {{{ */
+ const char *command,
+ size_t args_num, const char * const *args,
+ mt_reply_handler_t *handler, void *user_data)
+{
+ int status;
+ mt_reply_t *r;
+
+ status = send_command (c, command, args_num, args);
if (status != 0)
return (status);
- /* FIXME: Send out the buffer and read back results. */
-}
+ r = receive_reply (c);
+ if (r == NULL)
+ return (EPROTO);
+
+ /* Call the callback function with the data we received. */
+ status = (*handler) (c, r, user_data);
+
+ /* Free the allocated memory ... */
+ reply_free (r);
+
+ /* ... and return. */
+ return (status);
+} /* }}} int mt_query */
const mt_reply_t *mt_reply_next (const mt_reply_t *r) /* {{{ */
{