-
Notifications
You must be signed in to change notification settings - Fork 0
/
rip.c
153 lines (140 loc) · 3.68 KB
/
rip.c
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
/** rip.c - RIP-1 and -2 code for sendip
* Taken from code by Richard Polton <Richard.Polton@msdw.com>
*/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include "sendip_module.h"
#include "common.h"
#include "rip.h"
/* Character that identifies our options */
const char opt_char = 'r';
sendip_data *initialize(void) {
sendip_data *ret = malloc(sizeof(sendip_data));
rip_header *rip = malloc(sizeof(rip_header));
memset(rip, 0, sizeof(rip_header));
ret->alloc_len = sizeof(rip_header);
ret->data = rip;
ret->modified = 0;
return ret;
}
#define NEXT_FIELD \
p = (c == '|') ? ++q : q; \
while (*q != '|' && *q != '\0') { q++; } \
len = q - p; \
c = *q; \
*q = '\0';
bool do_opt(const char *opt, const char *arg, sendip_data *pack) {
rip_header *rippack = (rip_header *) pack->data;
rip_options *ripopt;
char *s, *p, *q;
char c = '\0';
int len = 0;
switch (opt[1]) {
case 'v': /* version */
rippack->version = opt2intn(arg, NULL, 1);
pack->modified |= RIP_MOD_VERSION;
break;
case 'c': /* command */
rippack->command = opt2intn(arg, NULL, 1);
pack->modified |= RIP_MOD_COMMAND;
break;
case 'a': /* authenticate */
if (RIP_NUM_ENTRIES(pack) != 0) {
WARN("real RIP-2 packets use authentication as the first entry")
}
RIP_ADD_ENTRY(pack);
ripopt = RIP_OPTION(pack);
memset(ripopt, 0, sizeof(rip_options));
ripopt->addressFamily = 0xFFFF;
s = q = strdup(arg == NULL ? "" : arg);
NEXT_FIELD
ripopt->routeTagOrAuthenticationType =
opt2intn(c == '|' ? p : "2", NULL, 2);
if (c == '|') {
NEXT_FIELD
}
if (len > 16) {
WARN("Cutting down passord to 16 characters")
len = 16;
}
if (len < 1) {
WARN("no password supplied")
} else {
strncpy((char *) &(ripopt->address), p, len);
}
free(s);
break;
case 'e': /* rip entry */
if (RIP_NUM_ENTRIES(pack) == 25) {
WARN("real RIP packets have not more than 25 entries")
}
RIP_ADD_ENTRY(pack);
ripopt = RIP_OPTION(pack);
s = q = strdup(arg == NULL ? "" : arg);
NEXT_FIELD
ripopt->addressFamily = opt2intn(len == 0 ? "2" : p, NULL, 2);
NEXT_FIELD
ripopt->routeTagOrAuthenticationType =
opt2intn(len == 0 ? "2" : p, NULL, 2);
NEXT_FIELD
ripopt->address = opt2v4(len == 0 ? "0.0.0.0" : p, 4);
NEXT_FIELD
ripopt->subnetMask = opt2v4(len == 0 ? "255.255.255.0" : p, 4);
NEXT_FIELD
ripopt->nextHop = opt2v4(len == 0 ? "0.0.0.0" : p, 4);
NEXT_FIELD
ripopt->metric = opt2intn(len == 0 ? p : "16", NULL, 4);
free(s);
break;
case 'd': /* default request */
if (RIP_NUM_ENTRIES(pack) != 0) {
WARN("real RIP-1/-2 packet have no entries in a default request")
}
RIP_ADD_ENTRY(pack);
ripopt = RIP_OPTION(pack);
rippack->command = (u_int8_t) 1;
ripopt->addressFamily = (u_int16_t) 0;
ripopt->routeTagOrAuthenticationType = (u_int16_t) 0;
ripopt->address = inet_addr("0.0.0.0");
ripopt->subnetMask = inet_addr("0.0.0.0");
ripopt->nextHop = inet_addr("0.0.0.0");
ripopt->metric = htons((u_int16_t) 16);
break;
case 'r': /* set reserved field */
rippack->res = opt2intn(arg, NULL, 2);
pack->modified |= RIP_MOD_RESERVED;
break;
default:
DERROR("rip - nrecognized option '%s'", opt)
return FALSE;
}
return TRUE;
}
bool
finalize(char *hdrs, __attribute__((unused)) sendip_data *headers[], int index,
__attribute__((unused)) sendip_data *data,
__attribute__((unused)) sendip_data *pack)
{
if (hdrs[index - 1] != 'u') {
WARN("RIP should be contained in a UDP packet")
}
return TRUE;
}
int
num_opts() {
return sizeof(rip_opts) / sizeof(sendip_option);
}
sendip_option *
get_opts() {
return rip_opts;
}
char
get_optchar() {
return opt_char;
}
/* vim: ts=4 sw=4 filetype=c
*/