back to topotato report
topotato coverage report
Current view: top level - ripngd - ripng_peer.c (source / functions) Hit Total Coverage
Test: test_demo.py::AllStartupTest Lines: 7 73 9.6 %
Date: 2023-02-24 18:37:51 Functions: 3 14 21.4 %

          Line data    Source code
       1             : /* RIPng peer support
       2             :  * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
       3             :  *
       4             :  * This file is part of GNU Zebra.
       5             :  *
       6             :  * GNU Zebra is free software; you can redistribute it and/or modify it
       7             :  * under the terms of the GNU General Public License as published by the
       8             :  * Free Software Foundation; either version 2, or (at your option) any
       9             :  * later version.
      10             :  *
      11             :  * GNU Zebra is distributed in the hope that it will be useful, but
      12             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License along
      17             :  * with this program; see the file COPYING; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : /* RIPng support added by Vincent Jardin <vincent.jardin@6wind.com>
      22             :  * Copyright (C) 2002 6WIND
      23             :  */
      24             : 
      25             : #include <zebra.h>
      26             : 
      27             : #include "if.h"
      28             : #include "prefix.h"
      29             : #include "command.h"
      30             : #include "linklist.h"
      31             : #include "thread.h"
      32             : #include "memory.h"
      33             : 
      34             : #include "ripngd/ripngd.h"
      35             : #include "ripngd/ripng_nexthop.h"
      36             : 
      37           3 : DEFINE_MTYPE_STATIC(RIPNGD, RIPNG_PEER, "RIPng peer");
      38             : 
      39           0 : static struct ripng_peer *ripng_peer_new(void)
      40             : {
      41           0 :         return XCALLOC(MTYPE_RIPNG_PEER, sizeof(struct ripng_peer));
      42             : }
      43             : 
      44           0 : static void ripng_peer_free(struct ripng_peer *peer)
      45             : {
      46           0 :         THREAD_OFF(peer->t_timeout);
      47           0 :         XFREE(MTYPE_RIPNG_PEER, peer);
      48           0 : }
      49             : 
      50           0 : struct ripng_peer *ripng_peer_lookup(struct ripng *ripng, struct in6_addr *addr)
      51             : {
      52           0 :         struct ripng_peer *peer;
      53           0 :         struct listnode *node, *nnode;
      54             : 
      55           0 :         for (ALL_LIST_ELEMENTS(ripng->peer_list, node, nnode, peer)) {
      56           0 :                 if (IPV6_ADDR_SAME(&peer->addr, addr))
      57           0 :                         return peer;
      58             :         }
      59             :         return NULL;
      60             : }
      61             : 
      62           0 : struct ripng_peer *ripng_peer_lookup_next(struct ripng *ripng,
      63             :                                           struct in6_addr *addr)
      64             : {
      65           0 :         struct ripng_peer *peer;
      66           0 :         struct listnode *node, *nnode;
      67             : 
      68           0 :         for (ALL_LIST_ELEMENTS(ripng->peer_list, node, nnode, peer)) {
      69           0 :                 if (addr6_cmp(&peer->addr, addr) > 0)
      70           0 :                         return peer;
      71             :         }
      72             :         return NULL;
      73             : }
      74             : 
      75             : /* RIPng peer is timeout.
      76             :  * Garbage collector.
      77             :  **/
      78           0 : static void ripng_peer_timeout(struct thread *t)
      79             : {
      80           0 :         struct ripng_peer *peer;
      81             : 
      82           0 :         peer = THREAD_ARG(t);
      83           0 :         listnode_delete(peer->ripng->peer_list, peer);
      84           0 :         ripng_peer_free(peer);
      85           0 : }
      86             : 
      87             : /* Get RIPng peer.  At the same time update timeout thread. */
      88           0 : static struct ripng_peer *ripng_peer_get(struct ripng *ripng,
      89             :                                          struct in6_addr *addr)
      90             : {
      91           0 :         struct ripng_peer *peer;
      92             : 
      93           0 :         peer = ripng_peer_lookup(ripng, addr);
      94             : 
      95           0 :         if (peer) {
      96           0 :                 THREAD_OFF(peer->t_timeout);
      97             :         } else {
      98           0 :                 peer = ripng_peer_new();
      99           0 :                 peer->ripng = ripng;
     100           0 :                 peer->addr = *addr;
     101           0 :                 listnode_add_sort(ripng->peer_list, peer);
     102             :         }
     103             : 
     104             :         /* Update timeout thread. */
     105           0 :         thread_add_timer(master, ripng_peer_timeout, peer,
     106             :                          RIPNG_PEER_TIMER_DEFAULT, &peer->t_timeout);
     107             : 
     108             :         /* Last update time set. */
     109           0 :         time(&peer->uptime);
     110             : 
     111           0 :         return peer;
     112             : }
     113             : 
     114           0 : void ripng_peer_update(struct ripng *ripng, struct sockaddr_in6 *from,
     115             :                        uint8_t version)
     116             : {
     117           0 :         struct ripng_peer *peer;
     118           0 :         peer = ripng_peer_get(ripng, &from->sin6_addr);
     119           0 :         peer->version = version;
     120           0 : }
     121             : 
     122           0 : void ripng_peer_bad_route(struct ripng *ripng, struct sockaddr_in6 *from)
     123             : {
     124           0 :         struct ripng_peer *peer;
     125           0 :         peer = ripng_peer_get(ripng, &from->sin6_addr);
     126           0 :         peer->recv_badroutes++;
     127           0 : }
     128             : 
     129           0 : void ripng_peer_bad_packet(struct ripng *ripng, struct sockaddr_in6 *from)
     130             : {
     131           0 :         struct ripng_peer *peer;
     132           0 :         peer = ripng_peer_get(ripng, &from->sin6_addr);
     133           0 :         peer->recv_badpackets++;
     134           0 : }
     135             : 
     136             : /* Display peer uptime. */
     137           0 : static char *ripng_peer_uptime(struct ripng_peer *peer, char *buf, size_t len)
     138             : {
     139           0 :         time_t uptime;
     140             : 
     141             :         /* If there is no connection has been done before print `never'. */
     142           0 :         if (peer->uptime == 0) {
     143           0 :                 snprintf(buf, len, "never   ");
     144           0 :                 return buf;
     145             :         }
     146             : 
     147             :         /* Get current time. */
     148           0 :         uptime = time(NULL);
     149           0 :         uptime -= peer->uptime;
     150             : 
     151           0 :         frrtime_to_interval(uptime, buf, len);
     152             : 
     153           0 :         return buf;
     154             : }
     155             : 
     156           1 : void ripng_peer_display(struct vty *vty, struct ripng *ripng)
     157             : {
     158           1 :         struct ripng_peer *peer;
     159           1 :         struct listnode *node, *nnode;
     160             : #define RIPNG_UPTIME_LEN 25
     161           1 :         char timebuf[RIPNG_UPTIME_LEN];
     162             : 
     163           2 :         for (ALL_LIST_ELEMENTS(ripng->peer_list, node, nnode, peer)) {
     164           0 :                 vty_out(vty, "    %pI6 \n%14s %10d %10d %10d      %s\n",
     165             :                         &peer->addr, " ", peer->recv_badpackets,
     166             :                         peer->recv_badroutes, ZEBRA_RIPNG_DISTANCE_DEFAULT,
     167             :                         ripng_peer_uptime(peer, timebuf, RIPNG_UPTIME_LEN));
     168             :         }
     169           1 : }
     170             : 
     171           0 : int ripng_peer_list_cmp(struct ripng_peer *p1, struct ripng_peer *p2)
     172             : {
     173           0 :         return memcmp(&p1->addr, &p2->addr, sizeof(struct in6_addr));
     174             : }
     175             : 
     176           0 : void ripng_peer_list_del(void *arg)
     177             : {
     178           0 :         ripng_peer_free(arg);
     179           0 : }

Generated by: LCOV version v1.16-topotato