File: ripng_peer.c

package info (click to toggle)
quagga 0.99.23.1-1
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 14,748 kB
  • sloc: ansic: 215,042; sh: 12,693; perl: 805; makefile: 644; exp: 184; lisp: 62; awk: 30
file content (216 lines) | stat: -rw-r--r-- 5,002 bytes parent folder | download | duplicates (4)
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
/* RIPng peer support
 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.  
 */

/* RIPng support added by Vincent Jardin <vincent.jardin@6wind.com>
 * Copyright (C) 2002 6WIND
 */

#include <zebra.h>

#include "if.h"
#include "prefix.h"
#include "command.h"
#include "linklist.h"
#include "thread.h"
#include "memory.h"

#include "ripngd/ripngd.h"
#include "ripngd/ripng_nexthop.h"


/* Linked list of RIPng peer. */
struct list *peer_list;

static struct ripng_peer *
ripng_peer_new (void)
{
  return XCALLOC (MTYPE_RIPNG_PEER, sizeof (struct ripng_peer));
}

static void
ripng_peer_free (struct ripng_peer *peer)
{
  XFREE (MTYPE_RIPNG_PEER, peer);
}

struct ripng_peer *
ripng_peer_lookup (struct in6_addr *addr)
{
  struct ripng_peer *peer;
  struct listnode *node, *nnode;

  for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
    {
      if (IPV6_ADDR_SAME (&peer->addr, addr))
	return peer;
    }
  return NULL;
}

struct ripng_peer *
ripng_peer_lookup_next (struct in6_addr *addr)
{
  struct ripng_peer *peer;
  struct listnode *node, *nnode;

  for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
    {
      if (addr6_cmp(&peer->addr, addr) > 0) 
	return peer;
    }
  return NULL;
}

/* RIPng peer is timeout.
 * Garbage collector.
 **/
static int
ripng_peer_timeout (struct thread *t)
{
  struct ripng_peer *peer;

  peer = THREAD_ARG (t);
  listnode_delete (peer_list, peer);
  ripng_peer_free (peer);

  return 0;
}

/* Get RIPng peer.  At the same time update timeout thread. */
static struct ripng_peer *
ripng_peer_get (struct in6_addr *addr)
{
  struct ripng_peer *peer;

  peer = ripng_peer_lookup (addr);

  if (peer)
    {
      if (peer->t_timeout)
	thread_cancel (peer->t_timeout);
    }
  else
    {
      peer = ripng_peer_new ();
      peer->addr = *addr; /* XXX */
      listnode_add_sort (peer_list, peer);
    }

  /* Update timeout thread. */
  peer->t_timeout = thread_add_timer (master, ripng_peer_timeout, peer,
				      RIPNG_PEER_TIMER_DEFAULT);

  /* Last update time set. */
  time (&peer->uptime);
  
  return peer;
}

void
ripng_peer_update (struct sockaddr_in6 *from, u_char version)
{
  struct ripng_peer *peer;
  peer = ripng_peer_get (&from->sin6_addr);
  peer->version = version;
}

void
ripng_peer_bad_route (struct sockaddr_in6 *from)
{
  struct ripng_peer *peer;
  peer = ripng_peer_get (&from->sin6_addr);
  peer->recv_badroutes++;
}

void
ripng_peer_bad_packet (struct sockaddr_in6 *from)
{
  struct ripng_peer *peer;
  peer = ripng_peer_get (&from->sin6_addr);
  peer->recv_badpackets++;
}

/* Display peer uptime. */
static char *
ripng_peer_uptime (struct ripng_peer *peer, char *buf, size_t len)
{
  time_t uptime;
  struct tm *tm;

  /* If there is no connection has been done before print `never'. */
  if (peer->uptime == 0)
    {
      snprintf (buf, len, "never   ");
      return buf;
    }

  /* Get current time. */
  uptime = time (NULL);
  uptime -= peer->uptime;
  tm = gmtime (&uptime);

  /* Making formatted timer strings. */
#define ONE_DAY_SECOND 60*60*24
#define ONE_WEEK_SECOND 60*60*24*7

  if (uptime < ONE_DAY_SECOND)
    snprintf (buf, len, "%02d:%02d:%02d", 
	      tm->tm_hour, tm->tm_min, tm->tm_sec);
  else if (uptime < ONE_WEEK_SECOND)
    snprintf (buf, len, "%dd%02dh%02dm", 
	      tm->tm_yday, tm->tm_hour, tm->tm_min);
  else
    snprintf (buf, len, "%02dw%dd%02dh", 
	      tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
  return buf;
}

void
ripng_peer_display (struct vty *vty)
{
  struct ripng_peer *peer;
  struct listnode *node, *nnode;
#define RIPNG_UPTIME_LEN 25
  char timebuf[RIPNG_UPTIME_LEN];

  for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
    {
      vty_out (vty, "    %s %s%14s %10d %10d %10d      %s%s", inet6_ntoa (peer->addr),
               VTY_NEWLINE, " ",
	       peer->recv_badpackets, peer->recv_badroutes,
	       ZEBRA_RIPNG_DISTANCE_DEFAULT,
	       ripng_peer_uptime (peer, timebuf, RIPNG_UPTIME_LEN),
	       VTY_NEWLINE);
    }
}

static int
ripng_peer_list_cmp (struct ripng_peer *p1, struct ripng_peer *p2)
{
  return addr6_cmp(&p1->addr, &p2->addr) > 0;
}

void
ripng_peer_init ()
{
  peer_list = list_new ();
  peer_list->cmp = (int (*)(void *, void *)) ripng_peer_list_cmp;
}