File: route.c

package info (click to toggle)
ipip 1.0-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 204 kB
  • ctags: 139
  • sloc: ansic: 1,397; sh: 116; makefile: 57
file content (211 lines) | stat: -rw-r--r-- 5,022 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* route.c		Route file reader
 *
 * $Id: route.c,v 1.3 1995/03/19 17:21:06 bdale Exp $
 *
 * Copyright 1991, Michael Westerhof, Sun Microsystems, Inc.
 * This software may be freely used, distributed, or modified, providing
 * this header is not removed.
 *
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <memory.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <syslog.h>
#include <stdlib.h>

#include "ipip.h"

static void rerr();		/* General error printer */
static void init_route();
static void r_route();
static void print_routes();

static int rlineno;
static int rerrflag;

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * open and read the route file
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */
int
read_routes(f)
char *f;
{
	FILE *cf;
	char buf[256], *p;

	init_route();

/* Open the route file */
	if((cf = fopen(f,"r"))==NULL){
		(void)fprintf(stderr,"%sRoute file %s not found or could not be opened\n",progname,f);
		return -1;
	}

	while(fgets(buf, 255, cf)!=NULL){
		rlineno++;
		if((p = strtok(buf, " \t\n\r"))==NULL)continue;
		if(*p=='#' || *p==';')continue;

		if(strcmp(p,"route")==0)r_route();
/*		else if(strcmp(p,"whatever")==0)r_dowhatever(); */
		else rerr("Unrecognized command: %s",p);
	}

	if(rts_top==0)rerr("No routes defined","");

	if(debugd)print_routes();

	return rerrflag;
}
/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Initialize defaults and null route entries
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */
static void
init_route()
{
	int i;

	for(i=0;i<MAX_ROUTES;i++){
		rts[i].ipaddr = 0;
		rts[i].mask = 0xffffffff;
		rts[i].destif = NULL;
		rts[i].destaddr = 0;
		rts[i].destport = 0;
		rts[i].hits = 0;
	}
	rts_top = 0;

	rlineno = 0;
	rerrflag = 0;
}

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Print out the route table (DEBUG)
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */
static void
print_routes()
{
	int i, port, type;
	unsigned char *p, *q;
	char *id;
	unsigned int m;

	if(rerrflag==0)(void)syslog(LOG_DEBUG,"%d routes:", rts_top);
	else (void)syslog(LOG_DEBUG,"%d routes (there are errors):", rts_top);
	for(i=0;i<rts_top;i++){
		p = (unsigned char *)&rts[i].ipaddr;		
		q = (unsigned char *)&rts[i].destaddr;		
		if(rts[i].destif != NULL){
			id = rts[i].destif->id;
			type = rts[i].destif->type;
		} else {
			id = "unknown";
			type = IF_TYPE_NONE;
		}
		port = ntohs(rts[i].destport);
		m = ntohl(rts[i].mask);

		if((type == IF_TYPE_SLIP)||
		   (type == IF_TYPE_TUN)){
			(void)syslog(LOG_DEBUG,"ip %d.%d.%d.%d mask 0x%08x interface %s\n",
				p[0],p[1],p[2],p[3],m,id);
		} else if(type == IF_TYPE_IPIP){
			(void)syslog(LOG_DEBUG,"ip %d.%d.%d.%d mask 0x%08x interface %s ip %d.%d.%d.%d\n",
				p[0],p[1],p[2],p[3],m,id,
				q[0],q[1],q[2],q[3]);
		} else {
			(void)syslog(LOG_DEBUG,"ip %d.%d.%d.%d mask 0x%08x interface %s ip %d.%d.%d.%d port %d\n",
				p[0],p[1],p[2],p[3],m,id,
				q[0],q[1],q[2],q[3],port);
		}
	}
}

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Print a general route file error
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */
static void
rerr(s,a)
char *s;
char *a;
{
	(void)syslog(LOG_ERR,"Route file error at line %d:\n",rlineno);
	(void)syslog(LOG_ERR,s,a);
	rerrflag--;
}

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Handle the "route" command
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */
static void
r_route()
{
	int i, syntax;
	char *q;
	struct hostent *he;

	if(rts_top>=MAX_ROUTES){
		rerr("Too many routes defined","");
		return;
	}

	syntax = 0;

	if((q = strtok((char *)NULL, " \t\n\r"))){
		if((he = gethostbyname(q))){
			(void)memcpy((char *)&rts[rts_top].ipaddr,
					he->h_addr_list[0], 4);
		} else {
			if((rts[rts_top].ipaddr = inet_addr(q)) == 0xffffffff)
				rerr("Bad IP address: %s",q);
		}
	} else syntax++;

	if((q = strtok((char *)NULL, " \t\n\r"))){
		rts[rts_top].mask = htonl(strtoul(q, NULL, 0));
	} else syntax++;

	if((q = strtok((char *)NULL, " \t\n\r"))){
		for(i=0;i<ifs_top;i++){
			if(strcmp(q,ifs[i].id)==0)rts[rts_top].destif = &ifs[i];
		}
		if(rts[rts_top].destif == NULL)
			rerr("Interface %s not defined",q);
	} else syntax++;

	if((q = strtok((char *)NULL, " \t\n\r"))){
		if((he = gethostbyname(q))){
			(void)memcpy((char *)&rts[rts_top].destaddr,
					he->h_addr_list[0], 4);
		} else {
			if((rts[rts_top].destaddr = inet_addr(q)) == (unsigned long)-1)
				rerr("Bad destination IP address: %s",q);
		}
	}

	if((q = strtok((char *)NULL, " \t\n\r"))){
		rts[rts_top].destport = htons((unsigned short)atoi(q));
	}

	if(syntax)rerr("Syntax error (route <ipaddr> <mask> <iface> [<destipaddr> [<destport>]]","");

	rts_top++;
}