Debian Codegraph @

ax25ipd/routing.c

raw
1 /* routing.c Routing table manipulation routines
2 *
3 * Copyright 1991, Michael Westerhof, Sun Microsystems, Inc.
4 * This software may be freely used, distributed, or modified, providing
5 * this header is not removed.
6 *
7 */
8
9 #include <memory.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <syslog.h>
13 #include <arpa/inet.h>
14 #include <netinet/in.h>
15 #include <sys/types.h>
16
17 #include "ax25ipd.h"
18
19 #ifndef FALSE
20 #define FALSE 0
21 #endif
22 #ifndef TRUE
23 #define TRUE 1
24 #endif
25
26 /* The routing table structure is not visible outside this module. */
27
28 struct route_table_entry {
29 unsigned char callsign[7]; /* the callsign and ssid */
30 unsigned char padcall; /* always set to zero */
31 union {
32 unsigned char ip_addr[4]; /* the IP address */
33 struct in_addr ip_addr_in;
34 };
35 unsigned short udp_port; /* the port number if udp */
36 unsigned char pad1;
37 unsigned char pad2;
38 unsigned int flags; /* route flags */
39 struct route_table_entry *next;
40 };
41
42 static struct route_table_entry *route_tbl;
43 static struct route_table_entry *default_route;
44
45 /* The Broadcast address structure is not visible outside this module either */
46
47 struct bcast_table_entry {
48 unsigned char callsign[7]; /* The broadcast address */
49 struct bcast_table_entry *next;
50 };
51
52 static struct bcast_table_entry *bcast_tbl;
53
54 /* Initialize the routing module */
55 void route_init(void)
56 {
57 route_tbl = NULL;
58 default_route = NULL;
59 bcast_tbl = NULL;
60 }
61
62 /* Add a new route entry */
63 void route_add(unsigned char *ip, unsigned char *call, int udpport,
64 unsigned int flags)
65 {
66 struct route_table_entry *rl, *rn;
67 int i;
68
69 /* Check we have an IP address */
70 if (ip == NULL)
71 return;
72
73 /* Check we have a callsign */
74 if (call == NULL)
75 return;
76
77 /* Find the last entry in the list */
78 rl = route_tbl;
79 if (route_tbl)
80 while (rl->next)
81 rl = rl->next;
82
83 rn = (struct route_table_entry *)
84 malloc(sizeof(struct route_table_entry));
85
86 /* Build this entry ... */
87 for (i = 0; i < 6; i++)
88 rn->callsign[i] = call[i] & 0xfe;
89 rn->callsign[6] = (call[6] & 0x1e) | 0x60;
90 rn->padcall = 0;
91 memcpy(rn->ip_addr, ip, 4);
92 rn->udp_port = htons(udpport);
93 rn->pad1 = 0;
94 rn->pad2 = 0;
95 rn->flags = flags;
96 rn->next = NULL;
97
98 /* Update the default_route pointer if this is a default route */
99 if (flags & AXRT_DEFAULT)
100 default_route = rn;
101
102 if (rl) /* ... the list is already started add the new route */
103 rl->next = rn;
104 else /* ... start the list off */
105 route_tbl = rn;
106
107 /* Log this entry ... */
108 LOGL4("added route: %s %s %s %d %d\n",
109 call_to_a(rn->callsign),
110 inet_ntoa(rn->ip_addr_in),
111 rn->udp_port ? "udp" : "ip", ntohs(rn->udp_port), flags);
112 }
113
114 /* Add a new broadcast address entry */
115 void bcast_add(unsigned char *call)
116 {
117 struct bcast_table_entry *bl, *bn;
118 int i;
119
120 /* Check we have a callsign */
121 if (call == NULL)
122 return;
123
124 /* Find the last entry in the list */
125 bl = bcast_tbl;
126 if (bcast_tbl)
127 while (bl->next)
128 bl = bl->next;
129
130 bn = (struct bcast_table_entry *)
131 malloc(sizeof(struct bcast_table_entry));
132
133 /* Build this entry ... */
134 for (i = 0; i < 6; i++)
135 bn->callsign[i] = call[i] & 0xfe;
136 bn->callsign[6] = (call[6] & 0x1e) | 0x60;
137
138 bn->next = NULL;
139
140 if (bl) /* ... the list is already started add the new route */
141 bl->next = bn;
142 else /* ... start the list off */
143 bcast_tbl = bn;
144
145 /* Log this entry ... */
146 LOGL4("added broadcast address: %s\n", call_to_a(bn->callsign));
147 }
148
149 /*
150 * Return an IP address and port number given a callsign.
151 * We return a pointer to the address; the port number can be found
152 * immediately following the IP address. (UGLY coding; to be fixed later!)
153 */
154
155 unsigned char *call_to_ip(unsigned char *call)
156 {
157 struct route_table_entry *rp;
158 unsigned char mycall[7];
159 int i;
160
161 if (call == NULL)
162 return NULL;
163
164 for (i = 0; i < 6; i++)
165 mycall[i] = call[i] & 0xfe;
166
167 mycall[6] = (call[6] & 0x1e) | 0x60;
168
169 LOGL4("lookup call %s ", call_to_a(mycall));
170
171 rp = route_tbl;
172 while (rp) {
173 if (addrmatch(mycall, rp->callsign)) {
174 LOGL4("found ip addr %s\n",
175 inet_ntoa(rp->ip_addr_in));
176 return rp->ip_addr;
177 }
178 rp = rp->next;
179 }
180
181 /*
182 * No match found in the routing table, use the default route if
183 * we have one defined.
184 */
185 if (default_route) {
186 LOGL4("failed, using default ip addr %s\n",
187 inet_ntoa(default_route->ip_addr_in));
188 return default_route->ip_addr;
189 }
190
191 LOGL4("failed.\n");
192 return NULL;
193 }
194
195 /*
196 * Accept a callsign and return true if it is a broadcast address, or false
197 * if it is not found on the list
198 */
199 int is_call_bcast(unsigned char *call)
200 {
201 struct bcast_table_entry *bp;
202 unsigned char bccall[7];
203 int i;
204
205 if (call == NULL)
206 return FALSE;
207
208 for (i = 0; i < 6; i++)
209 bccall[i] = call[i] & 0xfe;
210
211 bccall[6] = (call[6] & 0x1e) | 0x60;
212
213 LOGL4("lookup broadcast %s ", call_to_a(bccall));
214
215 bp = bcast_tbl;
216 while (bp) {
217 if (addrmatch(bccall, bp->callsign)) {
218 LOGL4("found broadcast %s\n",
219 call_to_a(bp->callsign));
220 return TRUE;
221 }
222 bp = bp->next;
223 }
224 return FALSE;
225 }
226
227 /* Traverse the routing table, transmitting the packet to each bcast route */
228 void send_broadcast(unsigned char *buf, int l)
229 {
230 struct route_table_entry *rp;
231
232 rp = route_tbl;
233 while (rp) {
234 if (rp->flags & AXRT_BCAST) {
235 send_ip(buf, l, rp->ip_addr);
236 }
237 rp = rp->next;
238 }
239 }
240
241 /* print out the list of routes */
242 void dump_routes(void)
243 {
244 struct route_table_entry *rp;
245 int i;
246
247 for (rp = route_tbl, i = 0; rp; rp = rp->next)
248 i++;
249
250 LOGL1("\n%d active routes.\n", i);
251
252 rp = route_tbl;
253 while (rp) {
254 LOGL1(" %s\t%s\t%s\t%d\t%d\n",
255 call_to_a(rp->callsign),
256 inet_ntoa(rp->ip_addr_in),
257 rp->udp_port ? "udp" : "ip",
258 ntohs(rp->udp_port), rp->flags);
259 rp = rp->next;
260 }
261 fflush(stdout);
262 }