local.c 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
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>
25
#include <string.h>
26 27
#include <unistd.h>
#include <errno.h>
28
#include <sys/time.h>
29
#include <arpa/inet.h>
30

31
#include "babeld.h"
32
#include "interface.h"
33 34
#include "source.h"
#include "neighbour.h"
35
#include "kernel.h"
36 37 38
#include "xroute.h"
#include "route.h"
#include "util.h"
39
#include "configuration.h"
40
#include "local.h"
41
#include "version.h"
42

43 44
int local_server_socket = -1;
struct local_socket local_sockets[MAX_LOCAL_SOCKETS];
45 46
int num_local_sockets = 0;
int local_server_port = -1;
47
char *local_server_path;
48
int local_server_write = 0;
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
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;
    }
}

81
static const char *
82 83 84 85 86 87 88 89 90 91
local_kind(int kind)
{
    switch(kind) {
    case LOCAL_FLUSH: return "flush";
    case LOCAL_CHANGE: return "change";
    case LOCAL_ADD: return "add";
    default: return "???";
    }
}

92 93 94 95
static void
local_notify_interface_1(struct local_socket *s,
                         struct interface *ifp, int kind)
{
96
    char buf[512], v4[INET_ADDRSTRLEN];
97
    int rc;
98 99 100 101 102 103 104 105 106 107 108
    int up;

    up = if_up(ifp);
    if(up && ifp->ipv4)
        inet_ntop(AF_INET, ifp->ipv4, v4, INET_ADDRSTRLEN);
    else
        v4[0] = '\0';
    if(up)
        rc = snprintf(buf, 512,
                      "%s interface %s up true%s%s%s%s\n",
                      local_kind(kind), ifp->name,
109 110
                      ifp->numll > 0 ? " ipv6 " : "",
                      ifp->numll > 0 ? format_address(ifp->ll[0]) : "",
111 112
                      v4[0] ? " ipv4 " : "", v4);
    else
Matthieu Boutier's avatar
Matthieu Boutier committed
113 114
        rc = snprintf(buf, 512, "%s interface %s up false\n",
                      local_kind(kind), ifp->name);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

    if(rc < 0 || rc >= 512)
        goto fail;

    rc = write_timeout(s->fd, buf, rc);
    if(rc < 0)
        goto fail;
    return;

 fail:
    shutdown(s->fd, 1);
    return;
}

void
local_notify_interface(struct interface *ifp, int kind)
{
    int i;
    for(i = 0; i < num_local_sockets; i++) {
        if(local_sockets[i].monitor)
            local_notify_interface_1(&local_sockets[i], ifp, kind);
    }
}

139
static void
140 141
local_notify_neighbour_1(struct local_socket *s,
                         struct neighbour *neigh, int kind)
142
{
143
    char buf[512], rttbuf[64];
144 145
    int rc;

146 147
    rttbuf[0] = '\0';
    if(valid_rtt(neigh)) {
148
        rc = snprintf(rttbuf, 64, " rtt %s rttcost %u",
149
                      format_thousands(neigh->rtt), neighbour_rttcost(neigh));
150
        if(rc < 0 || rc >= 64)
151 152 153
            rttbuf[0] = '\0';
    }

154
    rc = snprintf(buf, 512,
155
                  "%s neighbour %lx address %s "
156
                  "if %s reach %04x ureach %04x "
157
                  "rxcost %u txcost %u%s cost %u\n",
158
                  local_kind(kind),
159
                  /* Neighbours never move around in memory , so we can use the
160 161
                     address as a unique identifier. */
                  (unsigned long int)neigh,
162
                  format_address(neigh->address),
163
                  neigh->ifp->name,
164
                  neigh->hello.reach,
165
                  neigh->uhello.reach,
166
                  neighbour_rxcost(neigh),
167
                  neighbour_txcost(neigh),
168
                  rttbuf,
169 170 171 172 173
                  neighbour_cost(neigh));

    if(rc < 0 || rc >= 512)
        goto fail;

174
    rc = write_timeout(s->fd, buf, rc);
175 176 177 178 179
    if(rc < 0)
        goto fail;
    return;

 fail:
180
    shutdown(s->fd, 1);
181 182 183 184
    return;
}

void
185 186 187
local_notify_neighbour(struct neighbour *neigh, int kind)
{
    int i;
188 189 190 191
    for(i = 0; i < num_local_sockets; i++) {
        if(local_sockets[i].monitor)
            local_notify_neighbour_1(&local_sockets[i], neigh, kind);
    }
192 193 194
}

static void
195
local_notify_xroute_1(struct local_socket *s, struct xroute *xroute, int kind)
196 197 198
{
    char buf[512];
    int rc;
199 200 201 202
    const char *dst_prefix = format_prefix(xroute->prefix,
                                           xroute->plen);
    const char *src_prefix = format_prefix(xroute->src_prefix,
                                           xroute->src_plen);
203

204 205 206
    rc = snprintf(buf, 512, "%s xroute %s-%s prefix %s from %s metric %d\n",
                  local_kind(kind), dst_prefix, src_prefix,
                  dst_prefix, src_prefix, xroute->metric);
207

208 209 210
    if(rc < 0 || rc >= 512)
        goto fail;

211
    rc = write_timeout(s->fd, buf, rc);
212 213 214 215 216
    if(rc < 0)
        goto fail;
    return;

 fail:
217
    shutdown(s->fd, 1);
218 219 220 221
    return;
}

void
222 223 224
local_notify_xroute(struct xroute *xroute, int kind)
{
    int i;
225 226 227 228
    for(i = 0; i < num_local_sockets; i++) {
        if(local_sockets[i].monitor)
            local_notify_xroute_1(&local_sockets[i], xroute, kind);
    }
229 230 231
}

static void
232
local_notify_route_1(struct local_socket *s, struct babel_route *route, int kind)
233 234 235
{
    char buf[512];
    int rc;
236 237 238 239
    const char *dst_prefix = format_prefix(route->src->prefix,
                                           route->src->plen);
    const char *src_prefix = format_prefix(route->src->src_prefix,
                                           route->src->src_plen);
240 241

    rc = snprintf(buf, 512,
242
                  "%s route %lx prefix %s from %s installed %s "
243
                  "id %s metric %d refmetric %d via %s if %s\n",
244
                  local_kind(kind),
245
                  (unsigned long)route,
246
                  dst_prefix, src_prefix,
247
                  route->installed ? "yes" : "no",
248
                  format_eui64(route->src->id),
249
                  route_metric(route), route->refmetric,
250
                  format_address(route->neigh->address),
251
                  route->neigh->ifp->name);
252

253 254 255
    if(rc < 0 || rc >= 512)
        goto fail;

256
    rc = write_timeout(s->fd, buf, rc);
257 258 259 260 261
    if(rc < 0)
        goto fail;
    return;

 fail:
262
    shutdown(s->fd, 1);
263 264 265
    return;
}

266 267 268 269
void
local_notify_route(struct babel_route *route, int kind)
{
    int i;
270 271 272 273 274 275 276 277 278
    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)
{
279
    struct interface *ifp;
280 281 282 283
    struct neighbour *neigh;
    struct xroute_stream *xroutes;
    struct route_stream *routes;

284
    FOR_ALL_INTERFACES(ifp) {
285
        local_notify_interface_1(s, ifp, LOCAL_ADD);
286 287
    }

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    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);
    }
    return;
}

int
local_read(struct local_socket *s)
{
319
    int rc, n;
320
    char *eol;
321 322
    char reply[100] = "ok\n";
    const char *message = NULL;
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

    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;

339 340 341 342 343
    while(s->n > 0) {
        eol = memchr(s->buf, '\n', s->n);
        if(eol == NULL)
            break;
        n = eol + 1 - s->buf;
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        rc = parse_config_from_string(s->buf, n, &message);
        switch(rc) {
        case CONFIG_ACTION_DONE:
            break;
        case CONFIG_ACTION_QUIT:
            shutdown(s->fd, 1);
            reply[0] = '\0';
            break;
        case CONFIG_ACTION_DUMP:
            local_notify_all_1(s);
            break;
        case CONFIG_ACTION_MONITOR:
            local_notify_all_1(s);
            s->monitor = 1;
            break;
        case CONFIG_ACTION_UNMONITOR:
            s->monitor = 0;
            break;
        case CONFIG_ACTION_NO:
            snprintf(reply, sizeof(reply), "no%s%s\n",
                     message ? " " : "", message ? message : "");
            break;
        default:
            snprintf(reply, sizeof(reply), "bad\n");
        }
370

371 372 373 374 375 376 377 378 379
        if(reply[0] != '\0') {
            rc = write_timeout(s->fd, reply, strlen(reply));
            if(rc < 0) {
                goto fail;
            }
        }
        if(s->n > n)
            memmove(s->buf, s->buf + n, s->n - n);
        s->n -= n;
380 381
    }

382
    if(s->n == 0) {
383 384 385 386 387 388 389 390 391
        free(s->buf);
        s->buf = NULL;
    }

    return 1;

 fail:
    shutdown(s->fd, 1);
    return -1;
392 393
}

394 395
int
local_header(struct local_socket *s)
396
{
397
    char buf[512], host[64];
398
    int rc;
399

400
    rc = gethostname(host, 64);
401
    if(rc < 0)
402
        strncpy(host, "alamakota", 64);
403

404
    rc = snprintf(buf, 512, "BABEL 1.0\nversion %s\nhost %s\nmy-id %s\nok\n",
405
                  BABELD_VERSION, host, format_eui64(myid));
406 407
    if(rc < 0 || rc >= 512)
        goto fail;
408
    rc = write_timeout(s->fd, buf, rc);
409 410 411
    if(rc < 0)
        goto fail;

412
    return 1;
413 414

 fail:
415
    shutdown(s->fd, 1);
416
    return -1;
417
}
418

419 420 421 422 423 424
struct local_socket *
local_socket_create(int fd)
{
    if(num_local_sockets >= MAX_LOCAL_SOCKETS)
        return NULL;

425
    memset(&local_sockets[num_local_sockets], 0, sizeof(struct local_socket));
426 427 428 429 430 431 432 433 434 435 436 437 438 439
    local_sockets[num_local_sockets].fd = fd;
    num_local_sockets++;

    return &local_sockets[num_local_sockets - 1];
}

void
local_socket_destroy(int i)
{
    if(i < 0 || i >= num_local_sockets) {
        fprintf(stderr, "Internal error: closing unknown local socket.\n");
        return;
    }

440
    free(local_sockets[i].buf);
441 442
    close(local_sockets[i].fd);
    local_sockets[i] = local_sockets[--num_local_sockets];
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
443 444
    VALGRIND_MAKE_MEM_UNDEFINED(local_sockets + num_local_sockets,
                                sizeof(struct local_socket));
445
}