Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / bsd / krt-scan.c @ b1a1faba

History | View | Annotate | Download (2.42 KB)

1
/*
2
 *        BIRD -- *BSD Table Scanning
3
 *
4
 *        (c) 2004 Ondrej Filip <feela@network.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include <stdio.h>
10
#include <ctype.h>
11
#include <fcntl.h>
12
#include <unistd.h>
13
#include <sys/param.h>
14
#include <sys/types.h>
15
#include <sys/sysctl.h>
16
#include <sys/socket.h>
17
#include <net/route.h>
18

    
19
#undef LOCAL_DEBUG
20
#define LOCAL_DEBUG
21

    
22
#include "nest/bird.h"
23
#include "nest/route.h"
24
#include "nest/protocol.h"
25
#include "nest/iface.h"
26
#include "lib/timer.h"
27
#include "lib/unix.h"
28
#include "lib/krt.h"
29
#include "lib/string.h"
30

    
31
static int krt_scan_fd = -1;
32

    
33
struct iface *
34
krt_temp_iface(struct krt_proto *p, char *name)
35
{
36
  struct iface *i;
37

    
38
  WALK_LIST(i, p->scan.temp_ifs)
39
    if (!strcmp(i->name, name))
40
      return i;
41
  i = mb_allocz(p->p.pool, sizeof(struct iface));
42
  strcpy(i->name, name);
43
  add_tail(&p->scan.temp_ifs, &i->n);
44
  return i;
45
}
46

    
47

    
48
void
49
krt_scan_construct(struct krt_config *c)
50
{
51
}
52

    
53
void
54
krt_scan_preconfig(struct config *c)
55
{
56
}
57

    
58
void
59
krt_scan_postconfig(struct krt_config *c)
60
{
61
}
62

    
63
void
64
krt_scan_start(struct krt_proto *x, int first)
65
{
66
  init_list(&x->scan.temp_ifs);
67
}
68

    
69
void
70
krt_scan_shutdown(struct krt_proto *x, int last)
71
{
72
}
73

    
74
void
75
krt_sysctl_scan(struct proto *p, pool *pool, byte **buf, int *bl, int cmd)
76
{
77
  byte *next;
78
  int obl, needed, mib[6], on;
79
  struct ks_msg *m;
80

    
81
  mib[0] = CTL_NET;
82
  mib[1] = PF_ROUTE;
83
  mib[2] = 0;
84
  mib[3] = BIRD_PF;
85
  mib[4] = cmd;
86
  mib[5] = 0;
87

    
88
  if( sysctl(mib, 6 , NULL , &needed, NULL, 0) < 0)
89
  {
90
    die("RT scan...");
91
  }
92

    
93
  obl = *bl;
94

    
95
  while(needed > *bl) *bl *= 2;
96
  while(needed < (*bl/2)) *bl /= 2;
97

    
98
  if( (obl!=*bl) || !*buf)
99
  {
100
    if(*buf) mb_free(*buf);
101
    if( (*buf = mb_alloc(pool, *bl)) == NULL ) die("RT scan buf alloc");
102
  }
103

    
104
  on = needed;
105

    
106
  if( sysctl(mib, 6 , *buf, &needed, NULL, 0) < 0)
107
  {
108
    if(on != needed) return;         /* The buffer size changed since last sysctl */
109
    die("RT scan 2");
110
  }
111

    
112
  for (next = *buf; next < (*buf + needed); next += m->rtm.rtm_msglen)
113
  {
114
    m = (struct ks_msg *)next;
115
    krt_read_msg(p, m, 1);
116
  }
117
}
118

    
119
void
120
krt_scan_fire(struct krt_proto *p)
121
{
122
  static byte *buf = NULL;
123
  static int bl = 32768;
124
  krt_sysctl_scan((struct proto *)p , p->krt_pool, &buf, &bl, NET_RT_DUMP);
125
}
126

    
127
void
128
krt_if_scan(struct kif_proto *p)
129
{
130
  static byte *buf = NULL;
131
  static int bl = 4096;
132
  struct proto *P = (struct proto *)p;
133
  if_start_update();
134
  krt_sysctl_scan(P, P->pool, &buf, &bl, NET_RT_IFLIST);
135
  if_end_update();
136
}
137