Commit 336de763 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Implement dump, monitor, unmonitor.

parent 25ec057f
......@@ -881,6 +881,21 @@ parse_config_line(int c, gnc_t gnc, void *closure, int *action_return)
if(c < -1 || !action_return)
goto fail;
*action_return = CONFIG_QUIT;
} else if(strcmp(token, "dump") == 0) {
c = skip_eol(c, gnc, closure);
if(c < -1 || !action_return)
goto fail;
*action_return = CONFIG_DUMP;
} else if(strcmp(token, "monitor") == 0) {
c = skip_eol(c, gnc, closure);
if(c < -1 || !action_return)
goto fail;
*action_return = CONFIG_MONITOR;
} else if(strcmp(token, "unmonitor") == 0) {
c = skip_eol(c, gnc, closure);
if(c < -1 || !action_return)
goto fail;
*action_return = CONFIG_UNMONITOR;
} else {
c = parse_option(c, gnc, closure, token);
if(c < -1)
......
......@@ -24,6 +24,9 @@ THE SOFTWARE.
#define CONFIG_DONE 0
#define CONFIG_QUIT 1
#define CONFIG_DUMP 2
#define CONFIG_MONITOR 3
#define CONFIG_UNMONITOR 4
struct filter_result {
unsigned int add_metric; /* allow = 0, deny = INF, metric = <0..INF> */
......
......@@ -81,62 +81,6 @@ write_timeout(int fd, const void *buf, int len)
}
}
int
local_read(struct local_socket *s)
{
int rc;
char *eol;
if(s->buf == NULL)
s->buf = malloc(LOCAL_BUFSIZE);
if(s->buf == NULL)
return -1;
if(s->n >= LOCAL_BUFSIZE) {
errno = ENOSPC;
goto fail;
}
rc = read(s->fd, s->buf + s->n, LOCAL_BUFSIZE - s->n);
if(rc <= 0)
return rc;
s->n += rc;
eol = memchr(s->buf, '\n', s->n);
if(eol == NULL)
return 1;
rc = parse_config_from_string(s->buf, eol + 1 - s->buf);
switch(rc) {
case CONFIG_DONE:
break;
case CONFIG_QUIT:
shutdown(s->fd, 1);
break;
default: {
char *buf = "error\n";
rc = write_timeout(s->fd, buf, 6);
if(rc < 0)
goto fail;
}
}
if(s->n > eol + 1 - s->buf) {
memmove(s->buf, eol + 1, s->n - (eol + 1 - s->buf));
s->n -= (eol + 1 - s->buf);
} else {
s->n = 0;
free(s->buf);
s->buf = NULL;
}
return 1;
fail:
shutdown(s->fd, 1);
return -1;
}
static const char *
local_kind(int kind)
{
......@@ -195,8 +139,10 @@ void
local_notify_neighbour(struct neighbour *neigh, int kind)
{
int i;
for(i = 0; i < num_local_sockets; i++)
local_notify_neighbour_1(&local_sockets[i], neigh, kind);
for(i = 0; i < num_local_sockets; i++) {
if(local_sockets[i].monitor)
local_notify_neighbour_1(&local_sockets[i], neigh, kind);
}
}
static void
......@@ -230,8 +176,10 @@ void
local_notify_xroute(struct xroute *xroute, int kind)
{
int i;
for(i = 0; i < num_local_sockets; i++)
local_notify_xroute_1(&local_sockets[i], xroute, kind);
for(i = 0; i < num_local_sockets; i++) {
if(local_sockets[i].monitor)
local_notify_xroute_1(&local_sockets[i], xroute, kind);
}
}
static void
......@@ -273,8 +221,120 @@ void
local_notify_route(struct babel_route *route, int kind)
{
int i;
for(i = 0; i < num_local_sockets; i++)
local_notify_route_1(&local_sockets[i], route, kind);
for(i = 0; i < num_local_sockets; i++) {
if(local_sockets[i].monitor)
local_notify_route_1(&local_sockets[i], route, kind);
}
}
static void
local_notify_all_1(struct local_socket *s)
{
int rc;
struct neighbour *neigh;
struct xroute_stream *xroutes;
struct route_stream *routes;
FOR_ALL_NEIGHBOURS(neigh) {
local_notify_neighbour_1(s, neigh, LOCAL_ADD);
}
xroutes = xroute_stream();
if(xroutes) {
while(1) {
struct xroute *xroute = xroute_stream_next(xroutes);
if(xroute == NULL)
break;
local_notify_xroute_1(s, xroute, LOCAL_ADD);
}
xroute_stream_done(xroutes);
}
routes = route_stream(ROUTE_ALL);
if(routes) {
while(1) {
struct babel_route *route = route_stream_next(routes);
if(route == NULL)
break;
local_notify_route_1(s, route, LOCAL_ADD);
}
route_stream_done(routes);
}
rc = write_timeout(s->fd, "done\n", 5);
if(rc < 0)
goto fail;
return;
fail:
shutdown(s->fd, 1);
return;
}
int
local_read(struct local_socket *s)
{
int rc;
char *eol;
if(s->buf == NULL)
s->buf = malloc(LOCAL_BUFSIZE);
if(s->buf == NULL)
return -1;
if(s->n >= LOCAL_BUFSIZE) {
errno = ENOSPC;
goto fail;
}
rc = read(s->fd, s->buf + s->n, LOCAL_BUFSIZE - s->n);
if(rc <= 0)
return rc;
s->n += rc;
eol = memchr(s->buf, '\n', s->n);
if(eol == NULL)
return 1;
rc = parse_config_from_string(s->buf, eol + 1 - s->buf);
switch(rc) {
case CONFIG_DONE:
break;
case CONFIG_QUIT:
shutdown(s->fd, 1);
break;
case CONFIG_DUMP:
local_notify_all_1(s);
break;
case CONFIG_MONITOR:
local_notify_all_1(s);
s->monitor = 1;
break;
case CONFIG_UNMONITOR:
s->monitor = 0;
break;
default: {
char *buf = "error\n";
rc = write_timeout(s->fd, buf, 6);
if(rc < 0)
goto fail;
}
}
if(s->n > eol + 1 - s->buf) {
memmove(s->buf, eol + 1, s->n - (eol + 1 - s->buf));
s->n -= (eol + 1 - s->buf);
} else {
s->n = 0;
free(s->buf);
s->buf = NULL;
}
return 1;
fail:
shutdown(s->fd, 1);
return -1;
}
int
......@@ -302,7 +362,6 @@ local_header(struct local_socket *s)
return -1;
}
struct local_socket *
local_socket_create(int fd)
{
......
......@@ -40,6 +40,7 @@ struct local_socket {
int fd;
char *buf;
int n;
int monitor;
};
extern int local_server_socket;
......@@ -47,10 +48,10 @@ extern struct local_socket local_sockets[MAX_LOCAL_SOCKETS];
extern int num_local_sockets;
extern int local_server_port;
int local_read(struct local_socket *s);
void local_notify_neighbour(struct neighbour *neigh, int kind);
void local_notify_xroute(struct xroute *xroute, int kind);
void local_notify_route(struct babel_route *route, int kind);
int local_read(struct local_socket *s);
int local_header(struct local_socket *s);
struct local_socket *local_socket_create(int fd);
void local_socket_destroy(int i);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment