local.c 5.95 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 28
#include <unistd.h>
#include <errno.h>

29
#include "babeld.h"
30
#include "interface.h"
31 32 33 34 35 36 37
#include "source.h"
#include "neighbour.h"
#include "xroute.h"
#include "route.h"
#include "util.h"
#include "local.h"

38 39 40 41 42 43
#ifdef NO_LOCAL_INTERFACE

int dummy;

#else

44 45 46 47 48 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 81 82 83 84 85 86 87 88 89 90 91 92
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()
{
93
    char buf[512];
94 95 96 97 98
    int rc;
    
    if(local_socket < 0)
        return;

99
    rc = snprintf(buf, 512, "add self alamakota id %s\n",
100
                  format_eui64(myid));
101 102 103 104 105 106 107 108 109 110 111 112 113 114

    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;
}

115 116 117 118 119 120 121 122 123 124 125
static char *
local_kind(int kind)
{
    switch(kind) {
    case LOCAL_FLUSH: return "flush";
    case LOCAL_CHANGE: return "change";
    case LOCAL_ADD: return "add";
    default: return "???";
    }
}

126
void
127
local_notify_neighbour(struct neighbour *neigh, int kind)
128 129 130 131 132 133 134 135
{
    char buf[512];
    int rc;
    
    if(local_socket < 0)
        return;

    rc = snprintf(buf, 512,
136
                  "%s neighbour %lx address %s "
137
                  "if %s reach %04x rxcost %d txcost %d cost %d\n",
138
                  local_kind(kind),
139
                  /* Neighbours never move around in memory , so we can use the
140 141
                     address as a unique identifier. */
                  (unsigned long int)neigh,
142
                  format_address(neigh->address),
143
                  neigh->ifp->name,
144 145
                  neigh->reach,
                  neighbour_rxcost(neigh),
146
                  neighbour_txcost(neigh),
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                  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
163
local_notify_xroute(struct xroute *xroute, int kind)
164 165 166 167 168 169 170
{
    char buf[512];
    int rc;

    if(local_socket < 0)
        return;

171
    rc = snprintf(buf, 512, "%s xroute %s prefix %s metric %d\n",
172
                  local_kind(kind),
173
                  format_prefix(xroute->prefix, xroute->plen),
174
                  format_prefix(xroute->prefix, xroute->plen),
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
                  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
191
local_notify_route(struct route *route, int kind)
192 193 194 195 196 197 198 199
{
    char buf[512];
    int rc;

    if(local_socket < 0)
        return;

    rc = snprintf(buf, 512,
200
                  "%s route %s-%lx prefix %s installed %s "
201
                  "id %s metric %d refmetric %d via %s if %s\n",
202
                  local_kind(kind),
203
                  format_prefix(route->src->prefix, route->src->plen),
204 205
                  (unsigned long)route->neigh,
                  format_prefix(route->src->prefix, route->src->plen),
206
                  route->installed ? "yes" : "no",
207
                  format_eui64(route->src->id),
208
                  route_metric(route), route->refmetric,
209
                  format_address(route->neigh->address),
210
                  route->neigh->ifp->name);
211 212 213 214 215 216 217 218 219 220 221 222 223 224
    
    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;
}

225 226 227 228 229 230
static void
local_notify_xroute_callback(struct xroute *xroute, void *closure)
{
    local_notify_xroute(xroute, LOCAL_ADD);
}

231 232 233 234 235 236
static void
local_notify_route_callback(struct route *route, void *closure)
{
    local_notify_route(route, LOCAL_ADD);
}

237
void
238
local_notify_all()
239
{
240
    int rc;
241
    struct neighbour *neigh;
242
    const char *header = "BABEL 0.0\n";
243 244 245 246

    if(local_socket < 0)
        return;

247 248 249 250
    rc = write_timeout(local_socket, header, strlen(header));
    if(rc < 0)
        goto fail;

251
    local_notify_self();
252 253
    FOR_ALL_NEIGHBOURS(neigh) {
        local_notify_neighbour(neigh, LOCAL_ADD);
254
    }
255
    for_all_xroutes(local_notify_xroute_callback, NULL);
256
    for_all_routes(local_notify_route_callback, NULL);
257 258 259 260 261
    return;

 fail:
    shutdown(local_socket, 1);
    return;
262
}
263 264

#endif