forked from buytenh/dvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dgp_reader.c
130 lines (106 loc) · 3.05 KB
/
dgp_reader.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
/*
* dvpn, a multipoint vpn implementation
* Copyright (C) 2016 Lennert Buytenhek
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <iv.h>
#include <string.h>
#include "dgp_reader.h"
#include "lsa_deserialise.h"
#include "util.h"
#define KEEPALIVE_TIMEOUT 15
static void dgp_reader_keepalive_timeout(void *_dr)
{
struct dgp_reader *dr = _dr;
dr->io_error(dr->cookie);
}
void dgp_reader_register(struct dgp_reader *dr)
{
dr->bytes = 0;
if (dr->remoteid != NULL) {
dr->adj_rib_in.myid = dr->myid;
dr->adj_rib_in.remoteid = dr->remoteid;
adj_rib_in_init(&dr->adj_rib_in);
dr->to_loc.dest = dr->rib;
rib_listener_to_loc_init(&dr->to_loc);
adj_rib_in_listener_register(&dr->adj_rib_in, &dr->to_loc.rl);
}
IV_TIMER_INIT(&dr->keepalive_timeout);
iv_validate_now();
dr->keepalive_timeout.expires = iv_now;
timespec_add_ms(&dr->keepalive_timeout.expires,
1000 * KEEPALIVE_TIMEOUT, 1000 * KEEPALIVE_TIMEOUT);
dr->keepalive_timeout.cookie = dr;
dr->keepalive_timeout.handler = dgp_reader_keepalive_timeout;
iv_timer_register(&dr->keepalive_timeout);
}
int dgp_reader_read(struct dgp_reader *dr, int fd)
{
int ret;
int off;
do {
ret = read(fd, dr->buf + dr->bytes,
sizeof(dr->buf) - dr->bytes);
} while (ret < 0 && errno == EINTR);
if (ret <= 0) {
if (ret < 0) {
if (errno == EAGAIN)
return 0;
perror("dgp_reader_read");
}
return -1;
}
dr->bytes += ret;
iv_timer_unregister(&dr->keepalive_timeout);
iv_validate_now();
dr->keepalive_timeout.expires = iv_now;
timespec_add_ms(&dr->keepalive_timeout.expires,
1000 * KEEPALIVE_TIMEOUT, 1000 * KEEPALIVE_TIMEOUT);
iv_timer_register(&dr->keepalive_timeout);
off = 0;
while (off < dr->bytes) {
int len;
struct lsa *lsa;
len = lsa_deserialise(&lsa, dr->buf + off, dr->bytes - off);
if (len < 0)
return -1;
if (len == 0) {
if (off == 0 && dr->bytes == sizeof(dr->buf))
return -1;
break;
}
if (lsa != NULL) {
if (dr->remoteid != NULL)
adj_rib_in_add_lsa(&dr->adj_rib_in, lsa);
lsa_put(lsa);
}
off += len;
}
dr->bytes -= off;
memmove(dr->buf, dr->buf + off, dr->bytes);
return 0;
}
void dgp_reader_unregister(struct dgp_reader *dr)
{
if (dr->remoteid != NULL) {
adj_rib_in_truncate(&dr->adj_rib_in);
rib_listener_to_loc_deinit(&dr->to_loc);
}
if (iv_timer_registered(&dr->keepalive_timeout))
iv_timer_unregister(&dr->keepalive_timeout);
}