File: dnroute.c

package info (click to toggle)
dnprogs 2.18-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,896 kB
  • ctags: 3,051
  • sloc: ansic: 18,586; cpp: 9,436; makefile: 669; sh: 502; awk: 13
file content (189 lines) | stat: -rw-r--r-- 4,120 bytes parent folder | download
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
/*
 * dnroute.c    DECnet routing daemon (eventually...)
 *
 *		This program 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 of the License, or (at your option) any later version.
 *
 * Authors:     Patrick Caulfield <patrick@ChyGwyn.com>
 *              based on rtmon.c by Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <net/if.h>
#include <netinet/in.h>
#include <string.h>
#include <linux/netfilter_decnet.h>
#include <netdnet/dnetdb.h>

#include "libnetlink.h"
#include "dnrtlink.h"
#include "utils.h"
#include "csum.h"

int routing_multicast_timer = 15;

extern void  send_route_msg(int);


char *if_index_to_name(int ifindex)
{
    struct ifreq ifr;
    static char buf[64];

    int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    ifr.ifr_ifindex = ifindex;

    if (ioctl(sock, SIOCGIFNAME, &ifr) == 0)
    {
	strcpy(buf, ifr.ifr_name);
    }
    else
    {
	sprintf(buf, "if%d", ifindex);
    }
    
    close(sock);
    return buf;
}

/* Dump a routing message to stdout */
static int dump_msg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
{
    struct nf_dn_rtmsg *rtm;
    unsigned short *ptr2;
    unsigned char  *ptr1;
    int            len, i;
    unsigned int   sum=1;
	
    rtm = (struct nf_dn_rtmsg *)NLMSG_DATA(n);
    ptr2 = (unsigned short *)NFDN_RTMSG(rtm);
    ptr1 = (unsigned char *)NFDN_RTMSG(rtm);
    len = n->nlmsg_len - sizeof(*n) - sizeof(*rtm);   

#ifdef DUMP_PACKET
    for (i=0; i<len/2; i++)
    {
	if (!(i&0xf)) fprintf(stderr, "\n");
	fprintf(stderr, "%04x ", *(ptr2+i));
    }
    fprintf(stderr, "\n");
#endif

    /* Level 1 Routing Message */
    if ( (ptr1[0] & 0xE)>>1 == 3)
    {
	struct dn_naddr add;
	char   node[32];
	int    num;
	int    num_ids;
	int    start_id;
	int    entry;

	i = 4; /* Start of segments */
	
	add.a_len = 2;
	add.a_addr[0] = ptr1[1];
	add.a_addr[1] = ptr1[2];
	dnet_ntop(AF_DECnet, &add, node, sizeof(node));
       
	printf("Level 1 routing message from %s on %s, len = %d\n",
	       node, if_index_to_name(rtm->nfdn_ifindex), len);	

	while (i < len-4)
	{
	    num_ids = ptr1[i] | ptr1[i+1]<<8;
	    i+=2;
	    start_id = ptr1[i] | ptr1[i+1]<<8;
	    i+=2; /* Start of entries */
	    
	    for (num = 0; num<num_ids; num++)
	    {	    
		entry = ptr1[i] | ptr1[i+1]<<8;
		if (entry != 0x7fff)
		{
		    printf("  Node %d reachable. Hops %d, cost %d\n",
			   num+start_id, (entry&0x7E00)>>9, entry&0x1FF);
		}
		i+=2;
	    }
	}
    }
    
    /* Level 2 Routing Message */
    if ( (ptr1[0] & 0xE)>>1 == 4)
    {
	struct dn_naddr add;
	char   node[32];
	int    num;
	int    num_ids;
	int    start_id;
	int    entry;

	i = 4; /* Start of segments */
	
	add.a_len = 2;
	add.a_addr[0] = ptr1[1];
	add.a_addr[1] = ptr1[2];
	dnet_ntop(AF_DECnet, &add, node, sizeof(node));
	
	printf("Level 2 routing message from %s on %s, len = %d\n",
	       node, if_index_to_name(rtm->nfdn_ifindex), len);	
	while (i < len-4)
	{
	    num_ids = ptr1[i] | ptr1[i+1]<<8;
	    i+=2;
	    start_id = ptr1[i] | ptr1[i+1]<<8;
	    i+=2; /* Start of entries */
		
	    for (num = 0; num<num_ids; num++)
	    {	    
		entry = ptr1[i] | ptr1[i+1]<<8;
		if (entry != 0x7fff)
		{
		    printf("  Area %d reachable. Hops %d, cost %d\n",
			   num+start_id, (entry&0x7E00)>>9, entry&0x1FF);
		}
		i+=2;		
	    }
	}
	
    }

    /* Check the checksum */
    sum = route_csum(ptr1, 4, i);
    
    printf("Calc sum=%x, got sum: %x\n", sum, *(unsigned short *)(ptr1+i));

    return 0;
}

int
main(int argc, char **argv)
{
    struct rtnl_handle rth;
    unsigned groups = DNRMG_L1_GROUP | DNRMG_L2_GROUP;
    
    signal(SIGALRM, send_route_msg);
    
    alarm(routing_multicast_timer);
    
    if (dnrt_open(&rth, groups) < 0)
	exit(1);
    
    if (dnrt_listen(&rth, dump_msg, (void*)0) < 0)
	exit(2);
    
    exit(0);
}