projects
/
routeros-api.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
src/routeros_api.h: Protect against multiple inclusions.
[routeros-api.git]
/
src
/
main.c
diff --git
a/src/main.c
b/src/main.c
index
1c9fefe
..
5f77a5d
100644
(file)
--- a/
src/main.c
+++ b/
src/main.c
@@
-45,9
+45,9
@@
#include "routeros_api.h"
#if 1
#include "routeros_api.h"
#if 1
-# define
mt
_debug(...) fprintf (stdout, __VA_ARGS__)
+# define
ros
_debug(...) fprintf (stdout, __VA_ARGS__)
#else
#else
-# define
mt
_debug(...) /**/
+# define
ros
_debug(...) /**/
#endif
/* FIXME */
#endif
/* FIXME */
@@
-56,27
+56,27
@@
char *strdup (const char *);
/*
* Private structures
*/
/*
* Private structures
*/
-struct
mt
_connection_s
+struct
ros
_connection_s
{
int fd;
};
{
int fd;
};
-struct
mt
_reply_s
+struct
ros
_reply_s
{
unsigned int params_num;
char *status;
char **keys;
char **values;
{
unsigned int params_num;
char *status;
char **keys;
char **values;
-
mt
_reply_t *next;
+
ros
_reply_t *next;
};
};
-struct
mt
_login_data_s
+struct
ros
_login_data_s
{
const char *username;
const char *password;
};
{
const char *username;
const char *password;
};
-typedef struct
mt_login_data_s mt
_login_data_t;
+typedef struct
ros_login_data_s ros
_login_data_t;
/*
* Private functions
/*
* Private functions
@@
-118,9
+118,9
@@
static int read_exact (int fd, void *buffer, size_t buffer_size) /* {{{ */
return (0);
} /* }}} int read_exact */
return (0);
} /* }}} int read_exact */
-static
mt
_reply_t *reply_alloc (void) /* {{{ */
+static
ros
_reply_t *reply_alloc (void) /* {{{ */
{
{
-
mt
_reply_t *r;
+
ros
_reply_t *r;
r = malloc (sizeof (*r));
if (r == NULL)
r = malloc (sizeof (*r));
if (r == NULL)
@@
-132,9
+132,9
@@
static mt_reply_t *reply_alloc (void) /* {{{ */
r->next = NULL;
return (r);
r->next = NULL;
return (r);
-} /* }}}
mt
_reply_s *reply_alloc */
+} /* }}}
ros
_reply_s *reply_alloc */
-static int reply_add_keyval (
mt
_reply_t *r, const char *key, /* {{{ */
+static int reply_add_keyval (
ros
_reply_t *r, const char *key, /* {{{ */
const char *val)
{
char **tmp;
const char *val)
{
char **tmp;
@@
-165,7
+165,7
@@
static int reply_add_keyval (mt_reply_t *r, const char *key, /* {{{ */
return (0);
} /* }}} int reply_add_keyval */
return (0);
} /* }}} int reply_add_keyval */
-static void reply_dump (const
mt
_reply_t *r) /* {{{ */
+static void reply_dump (const
ros
_reply_t *r) /* {{{ */
{
if (r == NULL)
return;
{
if (r == NULL)
return;
@@
-189,9
+189,9
@@
static void reply_dump (const mt_reply_t *r) /* {{{ */
reply_dump (r->next);
} /* }}} void reply_dump */
reply_dump (r->next);
} /* }}} void reply_dump */
-static void reply_free (
mt
_reply_t *r) /* {{{ */
+static void reply_free (
ros
_reply_t *r) /* {{{ */
{
{
-
mt
_reply_t *next;
+
ros
_reply_t *next;
unsigned int i;
if (r == NULL)
unsigned int i;
if (r == NULL)
@@
-327,7
+327,7
@@
static int buffer_end (char **ret_buffer, size_t *ret_buffer_size) /* {{{ */
return (0);
} /* }}} int buffer_end */
return (0);
} /* }}} int buffer_end */
-static int send_command (
mt
_connection_t *c, /* {{{ */
+static int send_command (
ros
_connection_t *c, /* {{{ */
const char *command,
size_t args_num, const char * const *args)
{
const char *command,
size_t args_num, const char * const *args)
{
@@
-348,7
+348,7
@@
static int send_command (mt_connection_t *c, /* {{{ */
if (status != 0)
return (status);
if (status != 0)
return (status);
-
mt
_debug ("send_command: command = %s;\n", command);
+
ros
_debug ("send_command: command = %s;\n", command);
status = buffer_add (&buffer_ptr, &buffer_size, command);
if (status != 0)
return (status);
status = buffer_add (&buffer_ptr, &buffer_size, command);
if (status != 0)
return (status);
@@
-358,7
+358,7
@@
static int send_command (mt_connection_t *c, /* {{{ */
if (args[i] == NULL)
return (EINVAL);
if (args[i] == NULL)
return (EINVAL);
-
mt
_debug ("send_command: arg[%zu] = %s;\n", i, args[i]);
+
ros
_debug ("send_command: arg[%zu] = %s;\n", i, args[i]);
status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
if (status != 0)
return (status);
status = buffer_add (&buffer_ptr, &buffer_size, args[i]);
if (status != 0)
return (status);
@@
-392,7
+392,7
@@
static int send_command (mt_connection_t *c, /* {{{ */
return (0);
} /* }}} int send_command */
return (0);
} /* }}} int send_command */
-static int read_word (
mt
_connection_t *c, /* {{{ */
+static int read_word (
ros
_connection_t *c, /* {{{ */
char *buffer, size_t *buffer_size)
{
size_t req_size;
char *buffer, size_t *buffer_size)
{
size_t req_size;
@@
-408,48
+408,48
@@
static int read_word (mt_connection_t *c, /* {{{ */
return (status);
/* Calculate `req_size' */
return (status);
/* Calculate `req_size' */
- if (((unsigned char)
buffer
[0]) == 0xF0) /* {{{ */
+ if (((unsigned char)
word_length
[0]) == 0xF0) /* {{{ */
{
status = read_exact (c->fd, &word_length[1], 4);
if (status != 0)
return (status);
{
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];
+ req_size = (
word_length
[1] << 24)
+ | (
word_length
[2] << 16)
+ | (
word_length
[3] << 8)
+ |
word_length
[4];
}
}
- else if ((
buffer
[0] & 0xE0) == 0xE0)
+ else if ((
word_length
[0] & 0xE0) == 0xE0)
{
status = read_exact (c->fd, &word_length[1], 3);
if (status != 0)
return (status);
{
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];
+ req_size = ((
word_length
[0] & 0x1F) << 24)
+ | (
word_length
[1] << 16)
+ | (
word_length
[2] << 8)
+ |
word_length
[3];
}
}
- else if ((
buffer
[0] & 0xC0) == 0xC0)
+ else if ((
word_length
[0] & 0xC0) == 0xC0)
{
status = read_exact (c->fd, &word_length[1], 2);
if (status != 0)
return (status);
{
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];
+ req_size = ((
word_length
[0] & 0x3F) << 16)
+ | (
word_length
[1] << 8)
+ |
word_length
[2];
}
}
- else if ((
buffer
[0] & 0x80) == 0x80)
+ else if ((
word_length
[0] & 0x80) == 0x80)
{
status = read_exact (c->fd, &word_length[1], 1);
if (status != 0)
return (status);
{
status = read_exact (c->fd, &word_length[1], 1);
if (status != 0)
return (status);
- req_size = ((
buffer
[0] & 0x7F) << 8)
- |
buffer
[1];
+ req_size = ((
word_length
[0] & 0x7F) << 8)
+ |
word_length
[1];
}
}
- else if ((
buffer
[0] & 0x80) == 0)
+ else if ((
word_length
[0] & 0x80) == 0)
{
req_size = (size_t) word_length[0];
}
{
req_size = (size_t) word_length[0];
}
@@
-478,13
+478,13
@@
static int read_word (mt_connection_t *c, /* {{{ */
return (0);
} /* }}} int buffer_decode_next */
return (0);
} /* }}} int buffer_decode_next */
-static
mt_reply_t *receive_sentence (mt
_connection_t *c) /* {{{ */
+static
ros_reply_t *receive_sentence (ros
_connection_t *c) /* {{{ */
{
char buffer[4096];
size_t buffer_size;
int status;
{
char buffer[4096];
size_t buffer_size;
int status;
-
mt
_reply_t *r;
+
ros
_reply_t *r;
r = reply_alloc ();
if (r == NULL)
r = reply_alloc ();
if (r == NULL)
@@
-530,7
+530,7
@@
static mt_reply_t *receive_sentence (mt_connection_t *c) /* {{{ */
} /* }}} if (buffer[0] == '=') */
else
{
} /* }}} if (buffer[0] == '=') */
else
{
-
mt
_debug ("receive_sentence: Ignoring unknown word: %s\n", buffer);
+
ros
_debug ("receive_sentence: Ignoring unknown word: %s\n", buffer);
}
} /* while (42) */
}
} /* while (42) */
@@
-541,19
+541,19
@@
static mt_reply_t *receive_sentence (mt_connection_t *c) /* {{{ */
}
return (r);
}
return (r);
-} /* }}}
mt
_reply_t *receive_sentence */
+} /* }}}
ros
_reply_t *receive_sentence */
-static
mt_reply_t *receive_reply (mt
_connection_t *c) /* {{{ */
+static
ros_reply_t *receive_reply (ros
_connection_t *c) /* {{{ */
{
{
-
mt
_reply_t *head;
-
mt
_reply_t *tail;
+
ros
_reply_t *head;
+
ros
_reply_t *tail;
head = NULL;
tail = NULL;
while (42)
{
head = NULL;
tail = NULL;
while (42)
{
-
mt
_reply_t *tmp;
+
ros
_reply_t *tmp;
tmp = receive_sentence (c);
if (tmp == NULL)
tmp = receive_sentence (c);
if (tmp == NULL)
@@
-575,7
+575,7
@@
static mt_reply_t *receive_reply (mt_connection_t *c) /* {{{ */
} /* while (42) */
return (head);
} /* while (42) */
return (head);
-} /* }}}
mt
_reply_t *receive_reply */
+} /* }}}
ros
_reply_t *receive_reply */
static int create_socket (const char *node, const char *service) /* {{{ */
{
static int create_socket (const char *node, const char *service) /* {{{ */
{
@@
-584,7
+584,7
@@
static int create_socket (const char *node, const char *service) /* {{{ */
struct addrinfo *ai_ptr;
int status;
struct addrinfo *ai_ptr;
int status;
-
mt
_debug ("create_socket (node = %s, service = %s);\n",
+
ros
_debug ("create_socket (node = %s, service = %s);\n",
node, service);
memset (&ai_hint, 0, sizeof (ai_hint));
node, service);
memset (&ai_hint, 0, sizeof (ai_hint));
@@
-609,14
+609,14
@@
static int create_socket (const char *node, const char *service) /* {{{ */
ai_ptr->ai_protocol);
if (fd < 0)
{
ai_ptr->ai_protocol);
if (fd < 0)
{
-
mt
_debug ("create_socket: socket(2) failed.\n");
+
ros
_debug ("create_socket: socket(2) failed.\n");
continue;
}
status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
if (status != 0)
{
continue;
}
status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
if (status != 0)
{
-
mt
_debug ("create_socket: connect(2) failed.\n");
+
ros
_debug ("create_socket: connect(2) failed.\n");
close (fd);
continue;
}
close (fd);
continue;
}
@@
-629,7
+629,7
@@
static int create_socket (const char *node, const char *service) /* {{{ */
return (-1);
} /* }}} int create_socket */
return (-1);
} /* }}} int create_socket */
-static int login2_handler (
mt_connection_t *c, const mt
_reply_t *r, /* {{{ */
+static int login2_handler (
ros_connection_t *c, const ros
_reply_t *r, /* {{{ */
void *user_data)
{
if (r == NULL)
void *user_data)
{
if (r == NULL)
@@
-640,7
+640,7
@@
static int login2_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
if (strcmp (r->status, "done") != 0)
{
if (strcmp (r->status, "done") != 0)
{
-
mt
_debug ("login2_handler: Unexpected status: %s.\n", r->status);
+
ros
_debug ("login2_handler: Unexpected status: %s.\n", r->status);
return (EPROTO);
}
return (EPROTO);
}
@@
-700,13
+700,13
@@
static void make_password_hash (char response_hex[33], /* {{{ */
hash_binary_to_hex (response_hex, response_bin);
} /* }}} void make_password_hash */
hash_binary_to_hex (response_hex, response_bin);
} /* }}} void make_password_hash */
-static int login_handler (
mt_connection_t *c, const mt
_reply_t *r, /* {{{ */
+static int login_handler (
ros_connection_t *c, const ros
_reply_t *r, /* {{{ */
void *user_data)
{
const char *ret;
char challenge_hex[33];
char response_hex[33];
void *user_data)
{
const char *ret;
char challenge_hex[33];
char response_hex[33];
-
mt
_login_data_t *login_data;
+
ros
_login_data_t *login_data;
const char *params[2];
char param_name[1024];
const char *params[2];
char param_name[1024];
@@
-727,7
+727,7
@@
static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
if (strcmp (r->status, "done") != 0)
{
if (strcmp (r->status, "done") != 0)
{
-
mt
_debug ("login_handler: Unexpected status: %s.\n", r->status);
+
ros
_debug ("login_handler: Unexpected status: %s.\n", r->status);
return (EPROTO);
}
return (EPROTO);
}
@@
-735,17
+735,17
@@
static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
if (login_data == NULL)
return (EINVAL);
if (login_data == NULL)
return (EINVAL);
- ret =
mt
_reply_param_val_by_key (r, "ret");
+ ret =
ros
_reply_param_val_by_key (r, "ret");
if (ret == NULL)
{
if (ret == NULL)
{
-
mt
_debug ("login_handler: Reply does not have parameter \"ret\".\n");
+
ros
_debug ("login_handler: Reply does not have parameter \"ret\".\n");
return (EPROTO);
}
return (EPROTO);
}
-
mt
_debug ("login_handler: ret = %s;\n", ret);
+
ros
_debug ("login_handler: ret = %s;\n", ret);
if (strlen (ret) != 32)
{
if (strlen (ret) != 32)
{
-
mt
_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
+
ros
_debug ("login_handler: Unexpected length of the \"ret\" argument.\n");
return (EPROTO);
}
strcpy (challenge_hex, ret);
return (EPROTO);
}
strcpy (challenge_hex, ret);
@@
-760,20
+760,20
@@
static int login_handler (mt_connection_t *c, const mt_reply_t *r, /* {{{ */
params[0] = param_name;
params[1] = param_response;
params[0] = param_name;
params[1] = param_response;
- return (
mt
_query (c, "/login", 2, params, login2_handler,
+ return (
ros
_query (c, "/login", 2, params, login2_handler,
/* user data = */ NULL));
} /* }}} int login_handler */
/*
* Public functions
*/
/* user data = */ NULL));
} /* }}} int login_handler */
/*
* Public functions
*/
-
mt_connection_t *mt
_connect (const char *node, const char *service, /* {{{ */
+
ros_connection_t *ros
_connect (const char *node, const char *service, /* {{{ */
const char *username, const char *password)
{
int fd;
const char *username, const char *password)
{
int fd;
-
mt
_connection_t *c;
+
ros
_connection_t *c;
int status;
int status;
-
mt
_login_data_t user_data;
+
ros
_login_data_t user_data;
if ((node == NULL) || (username == NULL) || (password == NULL))
return (NULL);
if ((node == NULL) || (username == NULL) || (password == NULL))
return (NULL);
@@
-794,13
+794,13
@@
mt_connection_t *mt_connect (const char *node, const char *service, /* {{{ */
user_data.username = username;
user_data.password = password;
user_data.username = username;
user_data.password = password;
- status =
mt
_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
+ status =
ros
_query (c, "/login", /* args num = */ 0, /* args = */ NULL,
login_handler, &user_data);
return (c);
login_handler, &user_data);
return (c);
-} /* }}}
mt_connection_t *mt
_connect */
+} /* }}}
ros_connection_t *ros
_connect */
-int
mt_disconnect (mt
_connection_t *c) /* {{{ */
+int
ros_disconnect (ros
_connection_t *c) /* {{{ */
{
if (c == NULL)
return (EINVAL);
{
if (c == NULL)
return (EINVAL);
@@
-814,15
+814,15
@@
int mt_disconnect (mt_connection_t *c) /* {{{ */
free (c);
return (0);
free (c);
return (0);
-} /* }}} int
mt
_disconnect */
+} /* }}} int
ros
_disconnect */
-int
mt_query (mt
_connection_t *c, /* {{{ */
+int
ros_query (ros
_connection_t *c, /* {{{ */
const char *command,
size_t args_num, const char * const *args,
const char *command,
size_t args_num, const char * const *args,
-
mt
_reply_handler_t handler, void *user_data)
+
ros
_reply_handler_t handler, void *user_data)
{
int status;
{
int status;
-
mt
_reply_t *r;
+
ros
_reply_t *r;
status = send_command (c, command, args_num, args);
if (status != 0)
status = send_command (c, command, args_num, args);
if (status != 0)
@@
-840,36
+840,36
@@
int mt_query (mt_connection_t *c, /* {{{ */
/* ... and return. */
return (status);
/* ... and return. */
return (status);
-} /* }}} int
mt
_query */
+} /* }}} int
ros
_query */
-const
mt_reply_t *mt_reply_next (const mt
_reply_t *r) /* {{{ */
+const
ros_reply_t *ros_reply_next (const ros
_reply_t *r) /* {{{ */
{
if (r == NULL)
return (NULL);
return (r->next);
{
if (r == NULL)
return (NULL);
return (r->next);
-} /* }}}
mt_reply_t *mt
_reply_next */
+} /* }}}
ros_reply_t *ros
_reply_next */
-int
mt_reply_num (const mt
_reply_t *r) /* {{{ */
+int
ros_reply_num (const ros
_reply_t *r) /* {{{ */
{
int ret;
{
int ret;
- const
mt
_reply_t *ptr;
+ const
ros
_reply_t *ptr;
ret = 0;
for (ptr = r; ptr != NULL; ptr = ptr->next)
ret++;
return (ret);
ret = 0;
for (ptr = r; ptr != NULL; ptr = ptr->next)
ret++;
return (ret);
-} /* }}} int
mt
_reply_num */
+} /* }}} int
ros
_reply_num */
-const char *
mt_reply_status (const mt
_reply_t *r) /* {{{ */
+const char *
ros_reply_status (const ros
_reply_t *r) /* {{{ */
{
if (r == NULL)
return (NULL);
return (r->status);
{
if (r == NULL)
return (NULL);
return (r->status);
-} /* }}} char *
mt
_reply_status */
+} /* }}} char *
ros
_reply_status */
-const char *
mt_reply_param_key_by_index (const mt
_reply_t *r, /* {{{ */
+const char *
ros_reply_param_key_by_index (const ros
_reply_t *r, /* {{{ */
unsigned int index)
{
if (r == NULL)
unsigned int index)
{
if (r == NULL)
@@
-879,9
+879,9
@@
const char *mt_reply_param_key_by_index (const mt_reply_t *r, /* {{{ */
return (NULL);
return (r->keys[index]);
return (NULL);
return (r->keys[index]);
-} /* }}} char *
mt
_reply_param_key_by_index */
+} /* }}} char *
ros
_reply_param_key_by_index */
-const char *
mt_reply_param_val_by_index (const mt
_reply_t *r, /* {{{ */
+const char *
ros_reply_param_val_by_index (const ros
_reply_t *r, /* {{{ */
unsigned int index)
{
if (r == NULL)
unsigned int index)
{
if (r == NULL)
@@
-891,9
+891,9
@@
const char *mt_reply_param_val_by_index (const mt_reply_t *r, /* {{{ */
return (NULL);
return (r->values[index]);
return (NULL);
return (r->values[index]);
-} /* }}} char *
mt
_reply_param_key_by_index */
+} /* }}} char *
ros
_reply_param_key_by_index */
-const char *
mt_reply_param_val_by_key (const mt
_reply_t *r, /* {{{ */
+const char *
ros_reply_param_val_by_key (const ros
_reply_t *r, /* {{{ */
const char *key)
{
unsigned int i;
const char *key)
{
unsigned int i;
@@
-906,6
+906,6
@@
const char *mt_reply_param_val_by_key (const mt_reply_t *r, /* {{{ */
return (r->values[i]);
return (NULL);
return (r->values[i]);
return (NULL);
-} /* }}} char *
mt
_reply_param_val_by_key */
+} /* }}} char *
ros
_reply_param_val_by_key */
/* vim: set ts=2 sw=2 noet fdm=marker : */
/* vim: set ts=2 sw=2 noet fdm=marker : */