Commit e5bbae14 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Implement local interface.

parent 80194503
......@@ -9,10 +9,10 @@ CFLAGS = $(CDEBUGFLAGS) $(DEFINES) $(EXTRA_DEFINES)
LDLIBS = -lrt
SRCS = babel.c net.c kernel.c util.c network.c source.c neighbour.c \
route.c xroute.c message.c resend.c filter.c
route.c xroute.c message.c request.c filter.c
OBJS = babel.o net.o kernel.o util.o network.o source.o neighbour.o \
route.o xroute.o message.o resend.o filter.o
route.o xroute.o message.o request.o filter.o
babel: $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o babel $(OBJS) $(LDLIBS)
......
......@@ -50,6 +50,7 @@ THE SOFTWARE.
#include "message.h"
#include "resend.h"
#include "filter.h"
#include "local.h"
struct timeval now;
......@@ -90,6 +91,8 @@ struct timeval check_neighbours_timeout;
static volatile sig_atomic_t exiting = 0, dumping = 0, changed = 0;
int local_server_socket = -1, local_socket = -1;
static int kernel_routes_callback(int changed, void *closure);
static void init_signals(void);
static void dump_tables(FILE *out);
......@@ -502,6 +505,12 @@ main(int argc, char **argv)
SHIFT();
}
local_server_socket = tcp_server_socket(33123, 1);
if(local_server_socket < 0) {
perror("local_server_socket");
goto fail;
}
init_signals();
resize_receive_buffer(1500);
check_networks();
......@@ -560,13 +569,23 @@ main(int argc, char **argv)
timeval_min(&tv, &unicast_flush_timeout);
FD_ZERO(&readfds);
if(timeval_compare(&tv, &now) > 0) {
int maxfd = 0;
timeval_minus(&tv, &tv, &now);
FD_SET(protocol_socket, &readfds);
maxfd = MAX(maxfd, protocol_socket);
if(kernel_socket < 0) kernel_setup_socket(1);
if(kernel_socket >= 0)
if(kernel_socket >= 0) {
FD_SET(kernel_socket, &readfds);
rc = select(MAX(protocol_socket, kernel_socket) + 1,
&readfds, NULL, NULL, &tv);
maxfd = MAX(maxfd, kernel_socket);
}
if(local_socket >= 0) {
FD_SET(local_socket, &readfds);
maxfd = MAX(maxfd, local_socket);
} else if(local_server_socket >= 0) {
FD_SET(local_server_socket, &readfds);
maxfd = MAX(maxfd, local_server_socket);
}
rc = select(maxfd + 1, &readfds, NULL, NULL, &tv);
if(rc < 0) {
if(errno != EINTR) {
perror("select");
......@@ -609,6 +628,31 @@ main(int argc, char **argv)
}
}
if(local_server_socket >= 0 &&
FD_ISSET(local_server_socket, &readfds)) {
if(local_socket >= 0) {
close(local_socket);
local_socket = -1;
}
local_socket = accept(local_server_socket, NULL, NULL);
if(local_socket < 0) {
if(errno != EINTR && errno != EAGAIN)
perror("accept(local_server_socket)");
} else {
local_dump();
}
}
if(local_socket >= 0 && FD_ISSET(local_socket, &readfds)) {
rc = local_read(local_socket);
if(rc <= 0) {
if(rc < 0)
perror("read(local_socket)");
close(local_socket);
local_socket = -1;
}
}
if(changed) {
kernel_dump_time = now.tv_sec;
check_neighbours_timeout = now;
......
......@@ -63,6 +63,7 @@ extern int wireless_hello_interval, wired_hello_interval, idle_hello_interval;
extern int idle_time;
extern int link_detect;
extern int all_wireless;
extern int local_socket;
extern unsigned char myid[16];
......
/*
Copyright (c) 2008 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "babel.h"
#include "network.h"
#include "source.h"
#include "neighbour.h"
#include "xroute.h"
#include "route.h"
#include "util.h"
#include "local.h"
int
local_read(int s)
{
int rc;
char buf[500];
/* Ignore anything that comes in, except for EOF */
rc = read(s, buf, 500);
if(rc <= 0)
return rc;
return 1;
}
static int
write_timeout(int fd, const void *buf, int len)
{
int n = 0, rc = 0;
const char *b = buf;
while(n < len) {
rc = write(fd, b + n, len - n);
if(rc < 0) {
if(errno == EAGAIN || errno == EINTR) {
rc = wait_for_fd(1, fd, 100);
if(rc > 0) {
rc = write(fd, b + n, len - n);
}
}
}
if(rc > 0)
n += rc;
else
break;
}
if(n >= len)
return 1;
else {
if(rc >= 0)
errno = EAGAIN;
return -1;
}
}
void
local_notify_self()
{
char buf[512];
int rc;
if(local_socket < 0)
return;
rc = snprintf(buf, 512, "self %s", format_address(myid));
if(rc < 0 || rc >= 512)
goto fail;
rc = write_timeout(local_socket, buf, rc);
if(rc < 0)
goto fail;
return;
fail:
shutdown(local_socket, 1);
return;
}
void
local_notify_neighbour(struct neighbour *neigh, int flush)
{
char buf[512];
int rc;
if(local_socket < 0)
return;
rc = snprintf(buf, 512,
"neighbour %s%s at %s "
"dev %s reach %04x rxcost %d txcost %d cost %d\n",
format_address(neigh->id),
flush ? " flush" : "",
format_address(neigh->address),
neigh->network->ifname,
neigh->reach,
neighbour_rxcost(neigh),
neigh->txcost,
neighbour_cost(neigh));
if(rc < 0 || rc >= 512)
goto fail;
rc = write_timeout(local_socket, buf, rc);
if(rc < 0)
goto fail;
return;
fail:
shutdown(local_socket, 1);
return;
}
void
local_notify_xroute(struct xroute *xroute, int flush)
{
char buf[512];
int rc;
if(local_socket < 0)
return;
rc = snprintf(buf, 512, "xroute %s%s metric %d\n",
flush ? "flush " : "",
format_prefix(xroute->prefix, xroute->plen),
xroute->metric);
if(rc < 0 || rc >= 512)
goto fail;
rc = write_timeout(local_socket, buf, rc);
if(rc < 0)
goto fail;
return;
fail:
shutdown(local_socket, 1);
return;
}
void
local_notify_route(struct route *route, int flush)
{
char buf[512];
int rc;
if(local_socket < 0)
return;
rc = snprintf(buf, 512,
"route %s%s installed %s "
"id %s metric %d refmetric %d via %s if %s neigh %s\n",
flush ? "flush " : "",
format_prefix(route->src->prefix, route->src->plen),
route->installed ? "yes" : "no",
format_address(route->src->id),
route->metric, route->refmetric,
format_address(route->neigh->address),
route->neigh->network->ifname,
format_address(route->neigh->id));
if(rc < 0 || rc >= 512)
goto fail;
rc = write_timeout(local_socket, buf, rc);
if(rc < 0)
goto fail;
return;
fail:
shutdown(local_socket, 1);
return;
}
void
local_dump()
{
int i;
if(local_socket < 0)
return;
local_notify_self();
for(i = 0; i < numneighs; i++)
local_notify_neighbour(&neighs[i], 0);
for(i = 0; i < numxroutes; i++)
local_notify_xroute(&xroutes[i], 0);
for(i = 0; i < numroutes; i++)
local_notify_route(&routes[i], 0);
}
/*
Copyright (c) 2008 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
struct neighbour;
struct route;
struct xroute;
int local_read(int s);
void local_notify_self(void);
void local_notify_neighbour(struct neighbour *neigh, int flush);
void local_notify_xroute(struct xroute *xroute, int flush);
void local_notify_route(struct route *route, int flush);
void local_dump(void);
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