summaryrefslogtreecommitdiffstats
path: root/src/send.c
blob: f5734b52385c3100b896587a8851b82ec3c3e944 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
  Copyright (c) 2013-2014, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "babel.h"
#include "neigh.h"
#include "packet.h"
#include "tlv.h"
#include "tlv_types.h"

#include <assert.h>


static inline bool send_neigh(gmrf_context_t *ctx, const gp_babel_neigh_t *neigh, const gp_babel_packet_t *packet) {
	if (!neigh->iface)
		return false;

	if (!gmrf_iface_send(ctx->gmrf, neigh->iface->gmrf_iface, packet, gp_babel_packet_size(packet), &neigh->addr)) {
		gmrf_logf(ctx->gmrf, LOG_WARNING, "gmrf_iface_send: %m");
		return false;
	}

	return true;
}

static inline bool send_iface(gmrf_context_t *ctx, const gmrf_iface_state_t *iface, const gp_babel_packet_t *packet) {
	if (!gmrf_iface_send_bc(ctx->gmrf, iface->gmrf_iface, packet, gp_babel_packet_size(packet))) {
		gmrf_logf(ctx->gmrf, LOG_WARNING, "gmrf_iface_send_bc: %m");
		return false;
	}

	return true;
}

static inline void send_bc(gmrf_context_t *ctx, const gp_babel_packet_t *packet) {
	gmrf_iface_state_t *iface;
	for (iface = ctx->interfaces; iface; iface = iface->next)
		send_iface(ctx, iface, packet);
}

void gp_babel_send_ack(gmrf_context_t *ctx, gp_babel_neigh_t *neigh, uint16_t nonce) {
	gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX);

	gp_babel_tlv_ack_t *ack = gp_babel_tlv_add(buf, TLV_ACK, sizeof(gp_babel_tlv_ack_t));
	if (!ack)
		return;

	ack->nonce = htons(nonce);

	send_neigh(ctx, neigh, &buf->packet);
}

static void add_ihus(gmrf_context_t *ctx, gp_babel_packet_buf_t *buf, const gmrf_iface_state_t *iface) {
	const gp_babel_neigh_t *neigh;

	for (neigh = iface->neighbours; neigh; neigh = neigh->next) {
		gp_babel_tlv_ihu_t *ihu = gp_babel_tlv_add(buf, TLV_IHU, sizeof(gp_babel_tlv_ihu_t)+sizeof(gmrf_addr_t));
		if (!ihu)
			return;

		ihu->ae = ADDR_ENC_GMRF;
		ihu->reserved = 0;
		ihu->rxcost = htons(gp_babel_neigh_get_rxcost(ctx, neigh));
		ihu->interval = htons(GP_BABEL_IHU_INTERVAL);
		memcpy(ihu->address, &neigh->addr, sizeof(gmrf_addr_t));
	}
}

void gp_babel_send_hellos(gmrf_context_t *ctx) {
	gmrf_logf(ctx->gmrf, LOG_DEBUG, "sending hellos...");

	gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX);

	gp_babel_tlv_hello_t *hello = gp_babel_tlv_add(buf, TLV_HELLO, sizeof(gp_babel_tlv_hello_t));
	if (!hello)
		return;

	hello->reserved = 0;
	hello->interval = htons(GP_BABEL_HELLO_INTERVAL);

	uint16_t len = buf->packet.len;

	gmrf_iface_state_t *iface;
	for (iface = ctx->interfaces; iface; iface = iface->next) {
		hello->seqno = htons(ctx->hello_seqno);

		buf->packet.len = len;
		add_ihus(ctx, buf, iface);

		send_iface(ctx, iface, &buf->packet);
	}

	ctx->hello_seqno++;
}

/*static inline bool add_node_id_tlv(gp_babel_packet_buf_t *buf, const gp_babel_node_id_t *node_id) {
	gp_babel_tlv_node_id_t *tlv = gp_babel_tlv_add(buf, TLV_NODE_ID, sizeof(gp_babel_tlv_node_id_t));
	if (!tlv)
		return false;

	tlv->id = *node_id;

	return true;
	}*/

static gp_babel_tlv_update_t* add_update_tlv(gp_babel_packet_buf_t *buf, const gp_babel_node_id_t *node, gp_babel_metric_seqno_t ms) {
	gp_babel_tlv_update_t *update = gp_babel_tlv_add(buf, TLV_UPDATE, sizeof(gp_babel_tlv_update_t));

	if (!update)
		return NULL;

	update->flags = 0;
	update->reserved = 0;
	update->interval = htons(GP_BABEL_UPDATE_INTERVAL);
	update->seqno = (ms.metric != GP_BABEL_INFINITY) ? htons(ms.seqno) : 0;
	update->metric = htons(ms.metric);
	update->node = *node;

	return update;
}

static gp_babel_tlv_update_t* add_retract_tlv(gp_babel_packet_buf_t *buf, const gp_babel_node_id_t *node) {
	return add_update_tlv(buf, node, (gp_babel_metric_seqno_t){GP_BABEL_INFINITY, 0});
}

static bool add_update(gp_babel_packet_buf_t *buf, gp_babel_route_t *route, bool targeted) {
	gp_babel_tlv_update_t *update = add_update_tlv(buf, &route->node, route->metric);
	if (!update)
		return false;

	if (gp_babel_is_metric_better(route->metric, route->feasibility_distance))
		route->feasibility_distance = route->metric;

	if (!targeted)
		route->last_metric = route->metric.metric;

	return true;
}

void gp_babel_send_update(gmrf_context_t *ctx, gmrf_iface_state_t *iface, gp_babel_neigh_t *neigh) {
	gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX);
	bool targeted = neigh;

	gp_babel_route_t *route;
	for (route = ctx->routes; route; route = route->next) {
		if ((route->metric.metric == GP_BABEL_INFINITY) && targeted)
			/* when targeted == true we are responding to a wildcard route request */
			continue;

		if (!add_update(buf, route, targeted)) {
			if (neigh)
				send_neigh(ctx, neigh, &buf->packet);
			else
				send_iface(ctx, iface, &buf->packet);

			buf->packet.len = 0;

			assert(add_update(buf, route, targeted));
		}
	}

	if (buf->packet.len) {
		if (neigh)
			send_neigh(ctx, neigh, &buf->packet);
		else
			send_iface(ctx, iface, &buf->packet);
	}
}

void gp_babel_send_update_for_route(gmrf_context_t *ctx, gmrf_iface_state_t *iface, gp_babel_neigh_t *neigh, gp_babel_route_t *route) {
	gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX);

	assert(add_update(buf, route, neigh));

	if (neigh)
		send_neigh(ctx, neigh, &buf->packet);
	else
		send_iface(ctx, iface, &buf->packet);
}

void gp_babel_send_retract(gmrf_context_t *ctx, gmrf_iface_state_t *iface, gp_babel_neigh_t *neigh, const gp_babel_node_id_t *node) {
	gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX);

	assert(add_retract_tlv(buf, node));

	if (neigh)
		send_neigh(ctx, neigh, &buf->packet);
	else
		send_iface(ctx, iface, &buf->packet);
}

void gp_babel_send_route_request(gmrf_context_t *ctx, gmrf_iface_state_t *iface, gp_babel_neigh_t *neigh, const gp_babel_node_id_t *node) {
	gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX);

	gp_babel_tlv_route_req_t *req = gp_babel_tlv_add(buf, TLV_ROUTE_REQ, sizeof(gp_babel_tlv_route_req_t));
	if (!req)
		return;

	req->node = node ? (*node) : (gp_babel_node_id_t){};

	req->flags = 0;
	req->reserved = 0;

	if (neigh)
		send_neigh(ctx, neigh, &buf->packet);
	else
		send_iface(ctx, iface, &buf->packet);
}

void gp_babel_send_seqno_request(gmrf_context_t *ctx, gp_babel_neigh_t *neigh, gp_babel_route_t *route, uint16_t seqno, uint8_t hop_count) {
	gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX);

	gp_babel_tlv_seqno_req_t *req = gp_babel_tlv_add(buf, TLV_SEQNO_REQ, sizeof(gp_babel_tlv_seqno_req_t));
	if (!req)
		return;

	req->seqno = htons(seqno);
	req->hop_count = hop_count;
	req->reserved = 0;
	req->node = route->node;

	if (neigh)
		send_neigh(ctx, neigh, &buf->packet);
	else
		send_bc(ctx, &buf->packet);
}