1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
|
/* $OpenBSD: mrt.c,v 1.55 2009/03/13 05:43:50 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "bgpd.h"
#include "rde.h"
#include "session.h"
#include "mrt.h"
static u_int16_t mrt_attr_length(struct rde_aspath *, int);
static int mrt_attr_dump(void *, u_int16_t, struct rde_aspath *,
struct bgpd_addr *);
static int mrt_dump_entry_mp(struct mrt *, struct prefix *,
u_int16_t, struct rde_peer*);
static int mrt_dump_entry(struct mrt *, struct prefix *,
u_int16_t, struct rde_peer*);
static int mrt_dump_header(struct buf *, u_int16_t, u_int16_t,
u_int32_t);
static int mrt_open(struct mrt *, time_t);
#define DUMP_BYTE(x, b) \
do { \
u_char t = (b); \
if (buf_add((x), &t, sizeof(t)) == -1) { \
log_warnx("mrt_dump1: buf_add error"); \
buf_free((x)); \
return (-1); \
} \
} while (0)
#define DUMP_SHORT(x, s) \
do { \
u_int16_t t; \
t = htons((s)); \
if (buf_add((x), &t, sizeof(t)) == -1) { \
log_warnx("mrt_dump2: buf_add error"); \
buf_free((x)); \
return (-1); \
} \
} while (0)
#define DUMP_LONG(x, l) \
do { \
u_int32_t t; \
t = htonl((l)); \
if (buf_add((x), &t, sizeof(t)) == -1) { \
log_warnx("mrt_dump3: buf_add error"); \
buf_free((x)); \
return (-1); \
} \
} while (0)
#define DUMP_NLONG(x, l) \
do { \
u_int32_t t = (l); \
if (buf_add((x), &t, sizeof(t)) == -1) { \
log_warnx("mrt_dump4: buf_add error"); \
buf_free((x)); \
return (-1); \
} \
} while (0)
int
mrt_dump_bgp_msg(struct mrt *mrt, void *pkg, u_int16_t pkglen,
struct peer *peer, struct bgpd_config *bgp)
{
struct buf *buf;
u_int16_t len;
int incoming = 0;
switch (peer->sa_local.ss_family) {
case AF_INET:
len = pkglen + MRT_BGP4MP_IPv4_HEADER_SIZE;
break;
case AF_INET6:
len = pkglen + MRT_BGP4MP_IPv6_HEADER_SIZE;
break;
default:
return (-1);
}
/* get the direction of the message to swap address and AS fields */
if (mrt->type == MRT_ALL_IN || mrt->type == MRT_UPDATE_IN)
incoming = 1;
if ((buf = buf_open(len + MRT_HEADER_SIZE)) == NULL) {
log_warnx("mrt_dump_bgp_msg: buf_open error");
return (-1);
}
if (mrt_dump_header(buf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE,
len) == -1) {
log_warnx("mrt_dump_bgp_msg: buf_add error");
return (-1);
}
if (!incoming)
DUMP_SHORT(buf, bgp->short_as);
DUMP_SHORT(buf, peer->short_as);
if (incoming)
DUMP_SHORT(buf, bgp->short_as);
DUMP_SHORT(buf, /* ifindex */ 0);
switch (peer->sa_local.ss_family) {
case AF_INET:
DUMP_SHORT(buf, AFI_IPv4);
if (!incoming)
DUMP_NLONG(buf, ((struct sockaddr_in *)
&peer->sa_local)->sin_addr.s_addr);
DUMP_NLONG(buf,
((struct sockaddr_in *)&peer->sa_remote)->sin_addr.s_addr);
if (incoming)
DUMP_NLONG(buf, ((struct sockaddr_in *)
&peer->sa_local)->sin_addr.s_addr);
break;
case AF_INET6:
DUMP_SHORT(buf, AFI_IPv6);
if (!incoming)
if (buf_add(buf, &((struct sockaddr_in6 *)
&peer->sa_local)->sin6_addr,
sizeof(struct in6_addr)) == -1) {
log_warnx("mrt_dump_bgp_msg: buf_add error");
buf_free(buf);
return (-1);
}
if (buf_add(buf,
&((struct sockaddr_in6 *)&peer->sa_remote)->sin6_addr,
sizeof(struct in6_addr)) == -1) {
log_warnx("mrt_dump_bgp_msg: buf_add error");
buf_free(buf);
return (-1);
}
if (incoming)
if (buf_add(buf, &((struct sockaddr_in6 *)
&peer->sa_local)->sin6_addr,
sizeof(struct in6_addr)) == -1) {
log_warnx("mrt_dump_bgp_msg: buf_add error");
buf_free(buf);
return (-1);
}
break;
}
if (buf_add(buf, pkg, pkglen) == -1) {
log_warnx("mrt_dump_bgp_msg: buf_add error");
buf_free(buf);
return (-1);
}
buf_close(&mrt->wbuf, buf);
return (len + MRT_HEADER_SIZE);
}
int
mrt_dump_state(struct mrt *mrt, u_int16_t old_state, u_int16_t new_state,
struct peer *peer, struct bgpd_config *bgp)
{
struct buf *buf;
u_int16_t len;
switch (peer->sa_local.ss_family) {
case AF_INET:
len = 4 + MRT_BGP4MP_IPv4_HEADER_SIZE;
break;
case AF_INET6:
len = 4 + MRT_BGP4MP_IPv6_HEADER_SIZE;
break;
default:
return (-1);
}
if ((buf = buf_open(len + MRT_HEADER_SIZE)) == NULL) {
log_warnx("mrt_dump_bgp_state: buf_open error");
return (-1);
}
if (mrt_dump_header(buf, MSG_PROTOCOL_BGP4MP, BGP4MP_STATE_CHANGE,
len) == -1) {
log_warnx("mrt_dump_bgp_state: buf_add error");
return (-1);
}
DUMP_SHORT(buf, bgp->short_as);
DUMP_SHORT(buf, peer->short_as);
DUMP_SHORT(buf, /* ifindex */ 0);
switch (peer->sa_local.ss_family) {
case AF_INET:
DUMP_SHORT(buf, AFI_IPv4);
DUMP_NLONG(buf,
((struct sockaddr_in *)&peer->sa_local)->sin_addr.s_addr);
DUMP_NLONG(buf,
((struct sockaddr_in *)&peer->sa_remote)->sin_addr.s_addr);
break;
case AF_INET6:
DUMP_SHORT(buf, AFI_IPv6);
if (buf_add(buf,
&((struct sockaddr_in6 *)&peer->sa_local)->sin6_addr,
sizeof(struct in6_addr)) == -1 ||
buf_add(buf,
&((struct sockaddr_in6 *)&peer->sa_remote)->sin6_addr,
sizeof(struct in6_addr)) == -1) {
log_warnx("mrt_dump_bgp_msg: buf_add error");
buf_free(buf);
return (-1);
}
break;
}
DUMP_SHORT(buf, old_state);
DUMP_SHORT(buf, new_state);
buf_close(&mrt->wbuf, buf);
return (len + MRT_HEADER_SIZE);
}
static u_int16_t
mrt_attr_length(struct rde_aspath *a, int oldform)
{
u_int16_t alen, plen;
u_int8_t l;
alen = 4 /* origin */ + 7 /* lpref */;
if (oldform)
alen += 7 /* nexthop */;
plen = aspath_length(a->aspath);
alen += 2 + plen + (plen > 255 ? 2 : 1);
if (a->med != 0)
alen += 7;
for (l = 0; l < a->others_len; l++)
if (a->others[l] != NULL)
alen += 2 + a->others[l]->len +
(a->others[l]->len > 255 ? 2 : 1);
else
break;
return alen;
}
static int
mrt_attr_dump(void *p, u_int16_t len, struct rde_aspath *a,
struct bgpd_addr *nexthop)
{
struct attr *oa;
u_char *buf = p;
u_int32_t tmp32;
int r;
u_int16_t aslen, wlen = 0;
u_int8_t l;
/* origin */
if ((r = attr_write(buf + wlen, len, ATTR_WELL_KNOWN, ATTR_ORIGIN,
&a->origin, 1)) == -1)
return (-1);
wlen += r; len -= r;
/* aspath */
aslen = aspath_length(a->aspath);
if ((r = attr_write(buf + wlen, len, ATTR_WELL_KNOWN, ATTR_ASPATH,
aspath_dump(a->aspath), aslen)) == -1)
return (-1);
wlen += r; len -= r;
if (nexthop) {
/* nexthop, already network byte order */
if ((r = attr_write(buf + wlen, len, ATTR_WELL_KNOWN,
ATTR_NEXTHOP, &nexthop->v4.s_addr, 4)) == -1)
return (-1);
wlen += r; len -= r;
}
/* MED, non transitive */
if (a->med != 0) {
tmp32 = htonl(a->med);
if ((r = attr_write(buf + wlen, len, ATTR_OPTIONAL, ATTR_MED,
&tmp32, 4)) == -1)
return (-1);
wlen += r; len -= r;
}
/* local preference, only valid for ibgp */
tmp32 = htonl(a->lpref);
if ((r = attr_write(buf + wlen, len, ATTR_WELL_KNOWN, ATTR_LOCALPREF,
&tmp32, 4)) == -1)
return (-1);
wlen += r; len -= r;
/* dump all other path attributes without modification */
for (l = 0; l < a->others_len; l++) {
if ((oa = a->others[l]) == NULL)
break;
if ((r = attr_write(buf + wlen, len, oa->flags, oa->type,
oa->data, oa->len)) == -1)
return (-1);
wlen += r; len -= r;
}
return (wlen);
}
static int
mrt_dump_entry_mp(struct mrt *mrt, struct prefix *p, u_int16_t snum,
struct rde_peer *peer)
{
struct buf *buf;
void *bptr;
struct bgpd_addr addr, nexthop, *nh;
u_int16_t len, attr_len;
u_int8_t p_len;
sa_family_t af;
attr_len = mrt_attr_length(p->aspath, 0);
p_len = PREFIX_SIZE(p->prefix->prefixlen);
pt_getaddr(p->prefix, &addr);
af = peer->remote_addr.af == 0 ? addr.af : peer->remote_addr.af;
switch (af) {
case AF_INET:
len = MRT_BGP4MP_IPv4_HEADER_SIZE;
break;
case AF_INET6:
len = MRT_BGP4MP_IPv6_HEADER_SIZE;
break;
default:
return (-1);
}
switch (addr.af) {
case AF_INET:
len += MRT_BGP4MP_IPv4_ENTRY_SIZE + p_len + attr_len;
break;
case AF_INET6:
len += MRT_BGP4MP_IPv6_ENTRY_SIZE + p_len + attr_len;
break;
default:
return (-1);
}
if ((buf = buf_open(len + MRT_HEADER_SIZE)) == NULL) {
log_warnx("mrt_dump_entry_mp: buf_open error");
return (-1);
}
if (mrt_dump_header(buf, MSG_PROTOCOL_BGP4MP, BGP4MP_ENTRY,
len) == -1) {
log_warnx("mrt_dump_entry_mp: buf_add error");
return (-1);
}
DUMP_SHORT(buf, rde_local_as());
DUMP_SHORT(buf, peer->short_as);
DUMP_SHORT(buf, /* ifindex */ 0);
switch (af) {
case AF_INET:
DUMP_SHORT(buf, AFI_IPv4);
DUMP_NLONG(buf, peer->local_v4_addr.v4.s_addr);
DUMP_NLONG(buf, peer->remote_addr.v4.s_addr);
break;
case AF_INET6:
DUMP_SHORT(buf, AFI_IPv6);
if (buf_add(buf, &peer->local_v6_addr.v6,
sizeof(struct in6_addr)) == -1 ||
buf_add(buf, &peer->remote_addr.v6,
sizeof(struct in6_addr)) == -1) {
log_warnx("mrt_dump_entry_mp: buf_add error");
buf_free(buf);
return (-1);
}
break;
}
DUMP_SHORT(buf, 0); /* view */
DUMP_SHORT(buf, 1); /* status */
DUMP_LONG(buf, p->lastchange); /* originated */
if (p->aspath->nexthop == NULL) {
bzero(&nexthop, sizeof(struct bgpd_addr));
nexthop.af = addr.af;
nh = &nexthop;
} else
nh = &p->aspath->nexthop->exit_nexthop;
switch (addr.af) {
case AF_INET:
DUMP_SHORT(buf, AFI_IPv4); /* afi */
DUMP_BYTE(buf, SAFI_UNICAST); /* safi */
DUMP_BYTE(buf, 4); /* nhlen */
DUMP_NLONG(buf, nh->v4.s_addr); /* nexthop */
break;
case AF_INET6:
DUMP_SHORT(buf, AFI_IPv6); /* afi */
DUMP_BYTE(buf, SAFI_UNICAST); /* safi */
DUMP_BYTE(buf, 16); /* nhlen */
if (buf_add(buf, &nh->v6, sizeof(struct in6_addr)) == -1) {
log_warnx("mrt_dump_entry_mp: buf_add error");
buf_free(buf);
return (-1);
}
break;
}
if ((bptr = buf_reserve(buf, p_len)) == NULL) {
log_warnx("mrt_dump_entry_mp: buf_reserve error");
buf_free(buf);
return (-1);
}
if (prefix_write(bptr, p_len, &addr, p->prefix->prefixlen) == -1) {
log_warnx("mrt_dump_entry_mp: prefix_write error");
buf_free(buf);
return (-1);
}
DUMP_SHORT(buf, attr_len);
if ((bptr = buf_reserve(buf, attr_len)) == NULL) {
log_warnx("mrt_dump_entry_mp: buf_reserve error");
buf_free(buf);
return (-1);
}
if (mrt_attr_dump(bptr, attr_len, p->aspath, NULL) == -1) {
log_warnx("mrt_dump_entry_mp: mrt_attr_dump error");
buf_free(buf);
return (-1);
}
buf_close(&mrt->wbuf, buf);
return (len + MRT_HEADER_SIZE);
}
static int
mrt_dump_entry(struct mrt *mrt, struct prefix *p, u_int16_t snum,
struct rde_peer *peer)
{
struct buf *buf;
void *bptr;
struct bgpd_addr addr, *nh;
u_int16_t len, attr_len;
if (p->prefix->af != AF_INET && peer->remote_addr.af == AF_INET)
/* only for true IPv4 */
return (0);
attr_len = mrt_attr_length(p->aspath, 1);
len = MRT_DUMP_HEADER_SIZE + attr_len;
pt_getaddr(p->prefix, &addr);
if ((buf = buf_open(len + MRT_HEADER_SIZE)) == NULL) {
log_warnx("mrt_dump_entry: buf_open error");
return (-1);
}
if (mrt_dump_header(buf, MSG_TABLE_DUMP, AFI_IPv4, len) == -1) {
log_warnx("mrt_dump_entry: buf_add error");
return (-1);
}
DUMP_SHORT(buf, 0);
DUMP_SHORT(buf, snum);
DUMP_NLONG(buf, addr.v4.s_addr);
DUMP_BYTE(buf, p->prefix->prefixlen);
DUMP_BYTE(buf, 1); /* state */
DUMP_LONG(buf, p->lastchange); /* originated */
DUMP_NLONG(buf, peer->remote_addr.v4.s_addr);
DUMP_SHORT(buf, peer->short_as);
DUMP_SHORT(buf, attr_len);
if ((bptr = buf_reserve(buf, attr_len)) == NULL) {
log_warnx("mrt_dump_entry: buf_reserve error");
buf_free(buf);
return (-1);
}
if (p->aspath->nexthop == NULL) {
bzero(&addr, sizeof(struct bgpd_addr));
addr.af = AF_INET;
nh = &addr;
} else
nh = &p->aspath->nexthop->exit_nexthop;
if (mrt_attr_dump(bptr, attr_len, p->aspath, nh) == -1) {
log_warnx("mrt_dump_entry: mrt_attr_dump error");
buf_free(buf);
return (-1);
}
buf_close(&mrt->wbuf, buf);
return (len + MRT_HEADER_SIZE);
}
static u_int16_t sequencenum = 0;
void
mrt_clear_seq(void)
{
sequencenum = 0;
}
void
mrt_dump_upcall(struct pt_entry *pt, void *ptr)
{
struct mrt *mrtbuf = ptr;
struct prefix *p;
/*
* dump all prefixes even the inactive ones. That is the way zebra
* dumps the table so we do the same. If only the active route should
* be dumped p should be set to p = pt->active.
*/
LIST_FOREACH(p, &pt->prefix_h, prefix_l) {
/* for now dump only stuff from the local-RIB */
if (!(p->flags & F_LOCAL))
continue;
if (mrtbuf->type == MRT_TABLE_DUMP)
mrt_dump_entry(mrtbuf, p, sequencenum++,
p->aspath->peer);
else
mrt_dump_entry_mp(mrtbuf, p, sequencenum++,
p->aspath->peer);
}
}
static int
mrt_dump_header(struct buf *buf, u_int16_t type, u_int16_t subtype,
u_int32_t len)
{
time_t now;
now = time(NULL);
DUMP_LONG(buf, now);
DUMP_SHORT(buf, type);
DUMP_SHORT(buf, subtype);
DUMP_LONG(buf, len);
return (0);
}
int
mrt_write(struct mrt *mrt)
{
struct buf *b;
int r = 0;
while ((b = TAILQ_FIRST(&mrt->wbuf.bufs)) &&
(r = buf_write(mrt->wbuf.fd, b)) == 1) {
TAILQ_REMOVE(&mrt->wbuf.bufs, b, entry);
mrt->wbuf.queued--;
buf_free(b);
}
if (r <= -1) {
log_warn("mrt dump write");
mrt_clean(mrt);
return (-1);
}
return (0);
}
void
mrt_clean(struct mrt *mrt)
{
struct buf *b;
close(mrt->wbuf.fd);
while ((b = TAILQ_FIRST(&mrt->wbuf.bufs))) {
TAILQ_REMOVE(&mrt->wbuf.bufs, b, entry);
buf_free(b);
}
mrt->wbuf.queued = 0;
}
static struct imsgbuf *mrt_imsgbuf[2];
void
mrt_init(struct imsgbuf *rde, struct imsgbuf *se)
{
mrt_imsgbuf[0] = rde;
mrt_imsgbuf[1] = se;
}
int
mrt_open(struct mrt *mrt, time_t now)
{
enum imsg_type type;
int i, fd;
if (strftime(MRT2MC(mrt)->file, sizeof(MRT2MC(mrt)->file),
MRT2MC(mrt)->name, localtime(&now)) == 0) {
log_warnx("mrt_open: strftime conversion failed");
return (-1);
}
fd = open(MRT2MC(mrt)->file,
O_WRONLY|O_NONBLOCK|O_CREAT|O_TRUNC, 0644);
if (fd == -1) {
log_warn("mrt_open %s", MRT2MC(mrt)->file);
return (1);
}
if (MRT2MC(mrt)->state == MRT_STATE_OPEN)
type = IMSG_MRT_OPEN;
else
type = IMSG_MRT_REOPEN;
i = mrt->type == MRT_TABLE_DUMP ? 0 : 1;
if (imsg_compose(mrt_imsgbuf[i], type, 0, 0, fd,
mrt, sizeof(struct mrt)) == -1)
log_warn("mrt_open");
return (1);
}
int
mrt_timeout(struct mrt_head *mrt)
{
struct mrt *m;
time_t now;
int timeout = MRT_MAX_TIMEOUT;
now = time(NULL);
LIST_FOREACH(m, mrt, entry) {
if (MRT2MC(m)->state == MRT_STATE_RUNNING &&
MRT2MC(m)->ReopenTimerInterval != 0) {
if (MRT2MC(m)->ReopenTimer <= now) {
mrt_open(m, now);
MRT2MC(m)->ReopenTimer =
now + MRT2MC(m)->ReopenTimerInterval;
}
if (MRT2MC(m)->ReopenTimer - now < timeout)
timeout = MRT2MC(m)->ReopenTimer - now;
}
}
return (timeout > 0 ? timeout : 0);
}
void
mrt_reconfigure(struct mrt_head *mrt)
{
struct mrt *m, *xm;
time_t now;
now = time(NULL);
for (m = LIST_FIRST(mrt); m != NULL; m = xm) {
xm = LIST_NEXT(m, entry);
if (MRT2MC(m)->state == MRT_STATE_OPEN ||
MRT2MC(m)->state == MRT_STATE_REOPEN) {
if (mrt_open(m, now) == -1)
continue;
if (MRT2MC(m)->ReopenTimerInterval != 0)
MRT2MC(m)->ReopenTimer =
now + MRT2MC(m)->ReopenTimerInterval;
MRT2MC(m)->state = MRT_STATE_RUNNING;
}
if (MRT2MC(m)->state == MRT_STATE_REMOVE) {
LIST_REMOVE(m, entry);
free(m);
continue;
}
}
}
void
mrt_handler(struct mrt_head *mrt)
{
struct mrt *m;
time_t now;
now = time(NULL);
LIST_FOREACH(m, mrt, entry) {
if (MRT2MC(m)->state == MRT_STATE_RUNNING &&
(MRT2MC(m)->ReopenTimerInterval != 0 ||
m->type == MRT_TABLE_DUMP)) {
if (mrt_open(m, now) == -1)
continue;
MRT2MC(m)->ReopenTimer =
now + MRT2MC(m)->ReopenTimerInterval;
}
}
}
struct mrt *
mrt_get(struct mrt_head *c, struct mrt *m)
{
struct mrt *t;
LIST_FOREACH(t, c, entry) {
if (t->type != m->type)
continue;
if (t->type == MRT_TABLE_DUMP)
return (t);
if (t->peer_id == m->peer_id &&
t->group_id == m->group_id)
return (t);
}
return (NULL);
}
int
mrt_mergeconfig(struct mrt_head *xconf, struct mrt_head *nconf)
{
struct mrt *m, *xm;
LIST_FOREACH(m, nconf, entry) {
if ((xm = mrt_get(xconf, m)) == NULL) {
/* NEW */
if ((xm = calloc(1, sizeof(struct mrt_config))) == NULL)
fatal("mrt_mergeconfig");
memcpy(xm, m, sizeof(struct mrt_config));
MRT2MC(xm)->state = MRT_STATE_OPEN;
LIST_INSERT_HEAD(xconf, xm, entry);
} else {
/* MERGE */
if (strlcpy(MRT2MC(xm)->name, MRT2MC(m)->name,
sizeof(MRT2MC(xm)->name)) >=
sizeof(MRT2MC(xm)->name))
fatalx("mrt_mergeconfig: strlcpy");
MRT2MC(xm)->ReopenTimerInterval =
MRT2MC(m)->ReopenTimerInterval;
MRT2MC(xm)->state = MRT_STATE_REOPEN;
}
}
LIST_FOREACH(xm, xconf, entry)
if (mrt_get(nconf, xm) == NULL)
/* REMOVE */
MRT2MC(xm)->state = MRT_STATE_REMOVE;
/* free config */
while ((m = LIST_FIRST(nconf)) != NULL) {
LIST_REMOVE(m, entry);
free(m);
}
return (0);
}
|