2 * librouteros - 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
41 #include <sys/types.h>
42 #include <sys/socket.h>
47 #include "routeros_api.h"
50 # define ros_debug(...) fprintf (stdout, __VA_ARGS__)
52 # define ros_debug(...) /**/
56 # define __attribute__(x) /**/
60 char *strdup (const char *);
65 struct ros_connection_s
72 unsigned int params_num;
80 struct ros_login_data_s
85 typedef struct ros_login_data_s ros_login_data_t;
90 static int read_exact (int fd, void *buffer, size_t buffer_size) /* {{{ */
95 if ((fd < 0) || (buffer == NULL))
103 while (have_bytes < buffer_size)
108 want_bytes = buffer_size - have_bytes;
110 status = read (fd, buffer_ptr, want_bytes);
119 assert (((size_t) status) <= want_bytes);
120 have_bytes += status;
121 buffer_ptr += status;
125 } /* }}} int read_exact */
127 static ros_reply_t *reply_alloc (void) /* {{{ */
131 r = malloc (sizeof (*r));
135 memset (r, 0, sizeof (*r));
141 } /* }}} ros_reply_s *reply_alloc */
143 static int reply_add_keyval (ros_reply_t *r, const char *key, /* {{{ */
148 tmp = realloc (r->keys, (r->params_num + 1) * sizeof (*tmp));
153 tmp = realloc (r->values, (r->params_num + 1) * sizeof (*tmp));
158 r->keys[r->params_num] = strdup (key);
159 if (r->keys[r->params_num] == NULL)
162 r->values[r->params_num] = strdup (val);
163 if (r->values[r->params_num] == NULL)
165 free (r->keys[r->params_num]);
166 r->keys[r->params_num] = NULL;
172 } /* }}} int reply_add_keyval */
175 static void reply_dump (const ros_reply_t *r) /* {{{ */
180 printf ("=== BEGIN REPLY ===\n"
183 (void *) r, r->status);
184 if (r->params_num > 0)
188 printf ("Arguments:\n");
189 for (i = 0; i < r->params_num; i++)
190 printf (" %3u: %s = %s\n", i, r->keys[i], r->values[i]);
193 printf ("Next: %p\n", (void *) r->next);
194 printf ("=== END REPLY ===\n");
196 reply_dump (r->next);
197 } /* }}} void reply_dump */
199 # define reply_dump(foo) /**/
202 static void reply_free (ros_reply_t *r) /* {{{ */
212 for (i = 0; i < r->params_num; i++)
224 } /* }}} void reply_free */
226 static int buffer_init (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
228 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
231 if (*ret_buffer_size < 1)
235 } /* }}} int buffer_init */
237 static int buffer_add (char **ret_buffer, size_t *ret_buffer_size, /* {{{ */
245 if ((ret_buffer == NULL) || (ret_buffer_size == NULL) || (string == NULL))
248 buffer = *ret_buffer;
249 buffer_size = *ret_buffer_size;
251 string_size = strlen (string);
252 if (string_size == 0)
255 if (string_size >= 0x10000000)
256 req_size = 5 + string_size;
257 else if (string_size >= 0x200000)
258 req_size = 4 + string_size;
259 else if (string_size >= 0x4000)
260 req_size = 3 + string_size;
261 else if (string_size >= 0x80)
262 req_size = 2 + string_size;
264 req_size = 1 + string_size;
266 if (*ret_buffer_size < req_size)
269 if (string_size >= 0x10000000)
272 buffer[1] = (string_size >> 24) & 0xff;
273 buffer[2] = (string_size >> 16) & 0xff;
274 buffer[3] = (string_size >> 8) & 0xff;
275 buffer[4] = (string_size ) & 0xff;
279 else if (string_size >= 0x200000)
281 buffer[0] = (string_size >> 24) & 0x1f;
283 buffer[1] = (string_size >> 16) & 0xff;
284 buffer[2] = (string_size >> 8) & 0xff;
285 buffer[3] = (string_size ) & 0xff;
289 else if (string_size >= 0x4000)
291 buffer[0] = (string_size >> 16) & 0x3f;
293 buffer[1] = (string_size >> 8) & 0xff;
294 buffer[2] = (string_size ) & 0xff;
298 else if (string_size >= 0x80)
300 buffer[0] = (string_size >> 8) & 0x7f;
302 buffer[1] = (string_size ) & 0xff;
306 else /* if (string_size <= 0x7f) */
308 buffer[0] = (char) string_size;
313 assert (buffer_size >= string_size);
314 memcpy (buffer, string, string_size);
315 buffer += string_size;
316 buffer_size -= string_size;
318 *ret_buffer = buffer;
319 *ret_buffer_size = buffer_size;
322 } /* }}} int buffer_add */
324 static int buffer_end (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
326 if ((ret_buffer == NULL) || (ret_buffer_size == NULL))
329 if (*ret_buffer_size < 1)
332 /* Add empty word. */
333 (*ret_buffer)[0] = 0;
335 (*ret_buffer_size)--;
338 } /* }}} int buffer_end */
340 static int send_command (ros_connection_t *c, /* {{{ */
342 size_t args_num, const char * const *args)
352 assert (command != NULL);
354 if ((args == NULL) && (args_num > 0))
357 /* FIXME: For debugging only */
358 memset (buffer, 0, sizeof (buffer));
361 buffer_size = sizeof (buffer);
363 status = buffer_init (&buffer_ptr, &buffer_size);
367 ros_debug ("send_command: command = %s;\n", command);
368 status = buffer_add (&buffer_ptr, &buffer_size, command);
372 for (i = 0; i < args_num; i++)
377 ros_debug ("send_command: arg[%zu] = %s;\n", i, args[i]);
378 status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
383 status = buffer_end (&buffer_ptr, &buffer_size);
388 buffer_size = sizeof (buffer) - buffer_size;
389 while (buffer_size > 0)
391 ssize_t bytes_written;
394 bytes_written = write (c->fd, buffer_ptr, buffer_size);
395 if (bytes_written < 0)
402 assert (((size_t) bytes_written) <= buffer_size);
404 buffer_ptr += bytes_written;
405 buffer_size -= bytes_written;
406 } /* while (buffer_size > 0) */
409 } /* }}} int send_command */
411 static int read_word (ros_connection_t *c, /* {{{ */
412 char *buffer, size_t *buffer_size)
415 uint8_t word_length[5];
418 if ((buffer == NULL) || (*buffer_size < 1))
423 /* read one byte from the socket */
424 status = read_exact (c->fd, word_length, 1);
428 /* Calculate `req_size' */
429 if (((unsigned char) word_length[0]) == 0xF0) /* {{{ */
431 status = read_exact (c->fd, &word_length[1], 4);
435 req_size = (word_length[1] << 24)
436 | (word_length[2] << 16)
437 | (word_length[3] << 8)
440 else if ((word_length[0] & 0xE0) == 0xE0)
442 status = read_exact (c->fd, &word_length[1], 3);
446 req_size = ((word_length[0] & 0x1F) << 24)
447 | (word_length[1] << 16)
448 | (word_length[2] << 8)
451 else if ((word_length[0] & 0xC0) == 0xC0)
453 status = read_exact (c->fd, &word_length[1], 2);
457 req_size = ((word_length[0] & 0x3F) << 16)
458 | (word_length[1] << 8)
461 else if ((word_length[0] & 0x80) == 0x80)
463 status = read_exact (c->fd, &word_length[1], 1);
467 req_size = ((word_length[0] & 0x7F) << 8)
470 else if ((word_length[0] & 0x80) == 0)
472 req_size = (size_t) word_length[0];
476 /* First nibble is `F' but second nibble is not `0'. */
480 if (*buffer_size < req_size)
483 /* Empty word. This ends a `sentence' and must therefore be valid. */
491 status = read_exact (c->fd, buffer, req_size);
494 *buffer_size = req_size;
497 } /* }}} int buffer_decode_next */
499 static ros_reply_t *receive_sentence (ros_connection_t *c) /* {{{ */
513 buffer_size = sizeof (buffer) - 1;
514 status = read_word (c, buffer, &buffer_size);
517 assert (buffer_size < sizeof (buffer));
518 buffer[buffer_size] = 0;
520 /* Empty word means end of reply */
521 if (buffer_size == 0)
524 if (buffer[0] == '!') /* {{{ */
526 if (r->status != NULL)
528 r->status = strdup (&buffer[1]);
529 if (r->status == NULL)
531 } /* }}} if (buffer[0] == '!') */
532 else if (buffer[0] == '=') /* {{{ */
534 char *key = &buffer[1];
538 val = strchr (key, '=');
541 fprintf (stderr, "Ignoring misformed word: %s\n", buffer);
547 reply_add_keyval (r, key, val);
548 } /* }}} if (buffer[0] == '=') */
551 ros_debug ("receive_sentence: Ignoring unknown word: %s\n", buffer);
555 if (r->status == NULL)
562 } /* }}} ros_reply_t *receive_sentence */
564 static ros_reply_t *receive_reply (ros_connection_t *c) /* {{{ */
579 tmp = receive_sentence (c);
594 if (strcmp ("done", tmp->status) == 0)
599 } /* }}} ros_reply_t *receive_reply */
601 static int create_socket (const char *node, const char *service) /* {{{ */
603 struct addrinfo ai_hint;
604 struct addrinfo *ai_list;
605 struct addrinfo *ai_ptr;
608 ros_debug ("create_socket (node = %s, service = %s);\n",
611 memset (&ai_hint, 0, sizeof (ai_hint));
613 ai_hint.ai_flags |= AI_ADDRCONFIG;
615 ai_hint.ai_family = AF_UNSPEC;
616 ai_hint.ai_socktype = SOCK_STREAM;
619 status = getaddrinfo (node, service, &ai_hint, &ai_list);
622 assert (ai_list != NULL);
624 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
629 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
630 ai_ptr->ai_protocol);
633 ros_debug ("create_socket: socket(2) failed.\n");
637 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
640 ros_debug ("create_socket: connect(2) failed.\n");
648 freeaddrinfo (ai_list);
651 } /* }}} int create_socket */
653 static int login2_handler (__attribute__((unused)) ros_connection_t *c, /* {{{ */
654 const ros_reply_t *r,
655 __attribute__((unused)) void *user_data)
662 if (strcmp (r->status, "trap") == 0)
664 ros_debug ("login2_handler: Logging in failed: %s.\n",
665 ros_reply_param_val_by_key (r, "message"));
668 else if (strcmp (r->status, "done") != 0)
670 ros_debug ("login2_handler: Unexpected status: %s.\n", r->status);
675 } /* }}} int login2_handler */
677 static void hash_binary_to_hex (char hex[33], uint8_t binary[16]) /* {{{ */
681 for (i = 0; i < 16; i++)
684 snprintf (tmp, 3, "%02"PRIx8, binary[i]);
690 } /* }}} void hash_binary_to_hex */
692 static void hash_hex_to_binary (uint8_t binary[16], char hex[33]) /* {{{ */
696 for (i = 0; i < 16; i++)
701 tmp[1] = hex[2*i + 1];
704 binary[i] = (uint8_t) strtoul (tmp, /* endptr = */ NULL, /* base = */ 16);
706 } /* }}} void hash_hex_to_binary */
708 static void make_password_hash (char response_hex[33], /* {{{ */
709 const char *password, size_t password_length, char challenge_hex[33])
711 uint8_t challenge_bin[16];
712 uint8_t response_bin[16];
713 char data_buffer[password_length+17];
714 gcry_md_hd_t md_handle;
716 hash_hex_to_binary (challenge_bin, challenge_hex);
719 memcpy (&data_buffer[1], password, password_length);
720 memcpy (&data_buffer[1+password_length], challenge_bin, 16);
722 gcry_md_open (&md_handle, GCRY_MD_MD5, /* flags = */ 0);
723 gcry_md_write (md_handle, data_buffer, sizeof (data_buffer));
724 memcpy (response_bin, gcry_md_read (md_handle, GCRY_MD_MD5), 16);
725 gcry_md_close (md_handle);
727 hash_binary_to_hex (response_hex, response_bin);
728 } /* }}} void make_password_hash */
730 static int login_handler (ros_connection_t *c, const ros_reply_t *r, /* {{{ */
734 char challenge_hex[33];
735 char response_hex[33];
736 ros_login_data_t *login_data;
738 const char *params[2];
739 char param_name[1024];
740 char param_response[64];
745 /* The expected result looks like this:
748 * =ret=ebddd18303a54111e2dea05a92ab46b4
753 if (strcmp (r->status, "done") != 0)
755 ros_debug ("login_handler: Unexpected status: %s.\n", r->status);
759 login_data = user_data;
760 if (login_data == NULL)
763 ret = ros_reply_param_val_by_key (r, "ret");
766 ros_debug ("login_handler: Reply does not have parameter \"ret\".\n");
769 ros_debug ("login_handler: ret = %s;\n", ret);
771 if (strlen (ret) != 32)
773 ros_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
776 strcpy (challenge_hex, ret);
778 make_password_hash (response_hex,
779 login_data->password, strlen (login_data->password),
782 snprintf (param_name, sizeof (param_name), "=name=%s", login_data->username);
783 snprintf (param_response, sizeof (param_response),
784 "=response=00%s", response_hex);
785 params[0] = param_name;
786 params[1] = param_response;
788 return (ros_query (c, "/login", 2, params, login2_handler,
789 /* user data = */ NULL));
790 } /* }}} int login_handler */
795 ros_connection_t *ros_connect (const char *node, const char *service, /* {{{ */
796 const char *username, const char *password)
801 ros_login_data_t user_data;
803 if ((node == NULL) || (username == NULL) || (password == NULL))
806 fd = create_socket (node, (service != NULL) ? service : ROUTEROS_API_PORT);
810 c = malloc (sizeof (*c));
816 memset (c, 0, sizeof (*c));
820 user_data.username = username;
821 user_data.password = password;
822 status = ros_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
823 login_handler, &user_data);
833 } /* }}} ros_connection_t *ros_connect */
835 int ros_disconnect (ros_connection_t *c) /* {{{ */
849 } /* }}} int ros_disconnect */
851 int ros_query (ros_connection_t *c, /* {{{ */
853 size_t args_num, const char * const *args,
854 ros_reply_handler_t handler, void *user_data)
859 if ((c == NULL) || (command == NULL) || (handler == NULL))
862 status = send_command (c, command, args_num, args);
866 r = receive_reply (c);
870 /* Call the callback function with the data we received. */
871 status = (*handler) (c, r, user_data);
873 /* Free the allocated memory ... */
876 /* ... and return. */
878 } /* }}} int ros_query */
880 const ros_reply_t *ros_reply_next (const ros_reply_t *r) /* {{{ */
886 } /* }}} ros_reply_t *ros_reply_next */
888 int ros_reply_num (const ros_reply_t *r) /* {{{ */
891 const ros_reply_t *ptr;
894 for (ptr = r; ptr != NULL; ptr = ptr->next)
898 } /* }}} int ros_reply_num */
900 const char *ros_reply_status (const ros_reply_t *r) /* {{{ */
905 } /* }}} char *ros_reply_status */
907 const char *ros_reply_param_key_by_index (const ros_reply_t *r, /* {{{ */
913 if (index >= r->params_num)
916 return (r->keys[index]);
917 } /* }}} char *ros_reply_param_key_by_index */
919 const char *ros_reply_param_val_by_index (const ros_reply_t *r, /* {{{ */
925 if (index >= r->params_num)
928 return (r->values[index]);
929 } /* }}} char *ros_reply_param_key_by_index */
931 const char *ros_reply_param_val_by_key (const ros_reply_t *r, /* {{{ */
936 if ((r == NULL) || (key == NULL))
939 for (i = 0; i < r->params_num; i++)
940 if (strcmp (r->keys[i], key) == 0)
941 return (r->values[i]);
944 } /* }}} char *ros_reply_param_val_by_key */
946 int ros_version (void) /* {{{ */
948 return (ROS_VERSION);
949 } /* }}} int ros_version */
951 const char *ros_version_string (void) /* {{{ */
953 return (ROS_VERSION_STRING);
954 } /* }}} char *ros_version_string */
956 /* vim: set ts=2 sw=2 noet fdm=marker : */