-
Notifications
You must be signed in to change notification settings - Fork 3
/
rip.c
89 lines (79 loc) · 2.1 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
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "dat.h"
#include "fns.h"
int
parserippkt(const octet *restrict data, size_t len, RIPPacket *restrict packet)
{
assert(data != NULL);
assert(packet != NULL);
if (len < MIN_RIP_PACKET_SIZE)
return -1;
packet->command = data[0];
packet->version = data[1];
packet->nbz = readnet16(data + 2);
packet->datalen = len - MIN_RIP_PACKET_SIZE;
packet->data = NULL;
if (packet->datalen != 0)
packet->data = data + 4;
if ((packet->datalen % RIP_RESPONSE_SIZE) != 0)
return -1;
packet->nresponse = packet->datalen / RIP_RESPONSE_SIZE;
return 0;
}
int
verifyripauth(RIPPacket *restrict packet, const char *restrict password)
{
char packetpass[16 + 1];
assert(packet != NULL);
assert(password != NULL);
if (packet->datalen < RIP_RESPONSE_SIZE)
return -1;
if (readnet16(packet->data + 0) != 0xFFFF)
return -1;
if (readnet16(packet->data + 2) != 2)
return -1;
memmove(packetpass, (char *)packet->data + 4, 16);
packetpass[16] = '\0';
if (strcmp(packetpass, password) != 0)
return -1;
--packet->nresponse;
packet->data += RIP_RESPONSE_SIZE;
packet->datalen -= RIP_RESPONSE_SIZE;
return 0;
}
static int
parseriprespocts(const octet *data, size_t len, RIPResponse *restrict response)
{
assert(data != NULL);
assert(response != NULL);
if (len < RIP_RESPONSE_SIZE)
return -1;
response->addrfamily = readnet16(data + 0);
response->routetag = readnet16(data + 2);
response->ipaddr = readnet32(data + 4);
response->subnetmask = readnet32(data + 8);
response->nexthop = readnet32(data + 12);
response->metric = readnet32(data + 16);
if (!isvalidnetmask(response->subnetmask))
return -1;
return 0;
}
int
parseripresponse(const RIPPacket *restrict packet, int k,
RIPResponse *restrict response)
{
size_t offset;
assert(packet != NULL);
assert(response != NULL);
offset = k*RIP_RESPONSE_SIZE;
if (packet->datalen < offset)
return -1;
return parseriprespocts(packet->data + offset,
packet->datalen - offset, response);
}