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