Commit 0b778cc6 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Use streams for iterating over xroutes.

parent ec7519a4
......@@ -1033,9 +1033,8 @@ dump_route(FILE *out, struct babel_route *route)
}
static void
dump_xroute_callback(struct xroute *xroute, void *closure)
dump_xroute(FILE *out, struct xroute *xroute)
{
FILE *out = (FILE*)closure;
fprintf(out, "%s metric %d (exported)\n",
format_prefix(xroute->prefix, xroute->plen),
xroute->metric);
......@@ -1045,6 +1044,7 @@ static void
dump_tables(FILE *out)
{
struct neighbour *neigh;
struct xroute_stream *xroutes;
struct route_stream *routes;
fprintf(out, "\n");
......@@ -1064,7 +1064,17 @@ dump_tables(FILE *out)
neigh->ifp->channel,
if_up(neigh->ifp) ? "" : " (down)");
}
for_all_xroutes(dump_xroute_callback, out);
xroutes = xroute_stream();
if(xroutes) {
while(1) {
struct xroute *xroute = xroute_stream_next(xroutes);
if(xroute == NULL) break;
dump_xroute(out, xroute);
}
xroute_stream_done(xroutes);
}
routes = route_stream(0);
if(routes) {
while(1) {
......@@ -1074,6 +1084,7 @@ dump_tables(FILE *out)
}
route_stream_done(routes);
}
fflush(out);
}
......
......@@ -253,18 +253,13 @@ local_notify_route(struct babel_route *route, int kind)
local_notify_route_1(local_sockets[i], route, kind);
}
static void
local_notify_xroute_callback(struct xroute *xroute, void *closure)
{
local_notify_xroute_1(*(int*)closure, xroute, LOCAL_ADD);
}
void
local_notify_all_1(int s)
{
int rc;
struct neighbour *neigh;
const char *header = "BABEL 0.0\n";
struct xroute_stream *xroutes;
struct route_stream *routes;
rc = write_timeout(s, header, strlen(header));
......@@ -275,7 +270,18 @@ local_notify_all_1(int s)
FOR_ALL_NEIGHBOURS(neigh) {
local_notify_neighbour_1(s, neigh, LOCAL_ADD);
}
for_all_xroutes(local_notify_xroute_callback, &s);
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(0);
if(routes) {
while(1) {
......@@ -286,6 +292,7 @@ local_notify_all_1(int s)
}
route_stream_done(routes);
}
rc = write_timeout(s, "done\n", 5);
if(rc < 0)
goto fail;
......
......@@ -1370,16 +1370,10 @@ update_myseqno()
seqno_time = now;
}
static void
send_xroute_update_callback(struct xroute *xroute, void *closure)
{
struct interface *ifp = (struct interface*)closure;
send_update(ifp, 0, xroute->prefix, xroute->plen);
}
void
send_self_update(struct interface *ifp)
{
struct xroute_stream *xroutes;
if(ifp == NULL) {
struct interface *ifp_aux;
FOR_ALL_INTERFACES(ifp_aux) {
......@@ -1391,7 +1385,17 @@ send_self_update(struct interface *ifp)
}
debugf("Sending self update to %s.\n", ifp->name);
for_all_xroutes(send_xroute_update_callback, ifp);
xroutes = xroute_stream();
if(xroutes) {
while(1) {
struct xroute *xroute = xroute_stream_next(xroutes);
if(xroute == NULL) break;
send_update(ifp, 0, xroute->prefix, xroute->plen);
}
xroute_stream_done(xroutes);
} else {
fprintf(stderr, "Couldn't allocate xroute stream.\n");
}
}
void
......
......@@ -126,13 +126,36 @@ xroutes_estimate()
return numxroutes;
}
void
for_all_xroutes(void (*f)(struct xroute*, void*), void *closure)
struct xroute_stream {
int index;
};
struct
xroute_stream *
xroute_stream()
{
struct xroute_stream *stream = malloc(sizeof(struct xroute_stream));
if(stream == NULL)
return NULL;
stream->index = 0;
return stream;
}
struct xroute *
xroute_stream_next(struct xroute_stream *stream)
{
int i, n = numxroutes;
if(stream->index < numxroutes)
return &xroutes[stream->index++];
else
return NULL;
}
for(i = 0; i < n; i++)
(*f)(&xroutes[i], closure);
void
xroute_stream_done(struct xroute_stream *stream)
{
free(stream);
}
int
......
......@@ -28,10 +28,14 @@ struct xroute {
int proto;
};
struct xroute_stream;
struct xroute *find_xroute(const unsigned char *prefix, unsigned char plen);
void flush_xroute(struct xroute *xroute);
int add_xroute(unsigned char prefix[16], unsigned char plen,
unsigned short metric, unsigned int ifindex, int proto);
int xroutes_estimate(void);
void for_all_xroutes(void (*f)(struct xroute*, void*), void *closure);
struct xroute_stream *xroute_stream();
struct xroute *xroute_stream_next(struct xroute_stream *stream);
void xroute_stream_done(struct xroute_stream *stream);
int check_xroutes(int send_updates);
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