-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathservreg-hack.c
382 lines (324 loc) · 11.5 KB
/
servreg-hack.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/** \addtogroup servreghack
* @{ */
/*
* Copyright (c) 2010, Swedish Institute of Computer Science.
* 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.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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.
*
* This file is part of the Contiki operating system.
*
* $Id: servreg-hack.c,v 1.3 2010/10/19 18:29:03 adamdunkels Exp $
*/
/**
* \file
* Implementation of the servreg-hack application
* \author
* Adam Dunkels <[email protected]>
*/
#include "contiki.h"
#include "contiki-lib.h"
#include "contiki-net.h"
#include "net/uip.h"
#include "net/uip-ds6.h"
#include "servreg-hack.h"
#include <stdio.h>
struct servreg_hack_registration {
struct servreg_hack_registration *next;
struct timer timer;
uip_ipaddr_t addr;
servreg_hack_id_t id;
uint8_t seqno;
};
#define MAX_REGISTRATIONS 16
LIST(others_services);
LIST(own_services);
MEMB(registrations, struct servreg_hack_registration, MAX_REGISTRATIONS);
PROCESS(servreg_hack_process, "Service regstry hack");
#define PERIOD_TIME 120 * CLOCK_SECOND
#define NEW_REG_TIME 10 * CLOCK_SECOND
#define MAX_BUFSIZE 2 + 80
#define UDP_PORT 61616
#define LIFETIME 10 * 60 * CLOCK_SECOND
#define SEQNO_LT(a, b) ((signed char)((a) - (b)) < 0)
static struct etimer sendtimer;
static uint8_t started = 0;
/*---------------------------------------------------------------------------*/
/* Go through the list of registrations and remove those that are too
old. */
static void
purge_registrations(void)
{
struct servreg_hack_registration *t;
for(t = list_head(own_services);
t != NULL;
t = list_item_next(t)) {
if(timer_expired(&t->timer)) {
t->seqno++;
timer_set(&t->timer, LIFETIME / 2);
}
}
for(t = list_head(others_services);
t != NULL;
t = list_item_next(t)) {
if(timer_expired(&t->timer)) {
list_remove(others_services, t);
memb_free(®istrations, t);
t = list_head(others_services);
}
}
}
/*---------------------------------------------------------------------------*/
void
servreg_hack_init(void)
{
if(started == 0) {
list_init(others_services);
list_init(own_services);
memb_init(®istrations);
process_start(&servreg_hack_process, NULL);
started = 1;
}
}
/*---------------------------------------------------------------------------*/
void
servreg_hack_register(servreg_hack_id_t id, const uip_ipaddr_t *addr)
{
servreg_hack_item_t *t;
struct servreg_hack_registration *r;
/* Walk through list, see if we already have a service ID
registered. If not, allocate a new registration and put it on our
list. If we cannot allocate a service registration, we reuse one
from the service registrations made by others. */
servreg_hack_init();
for(t = list_head(own_services);
t != NULL;
t = list_item_next(t)) {
if(servreg_hack_item_id(t) == id) {
return;
}
}
r = memb_alloc(®istrations);
if(r == NULL) {
printf("servreg_hack_register: error, could not allocate memory, should reclaim another registration but this has not been implemented yet.\n");
return;
}
r->id = id;
r->seqno = 1;
uip_ipaddr_copy(&r->addr, addr);
timer_set(&r->timer, LIFETIME / 2);
list_push(own_services, r);
PROCESS_CONTEXT_BEGIN(&servreg_hack_process);
etimer_set(&sendtimer, random_rand() % (NEW_REG_TIME));
PROCESS_CONTEXT_END(&servreg_hack_process);
}
/*---------------------------------------------------------------------------*/
servreg_hack_item_t *
servreg_hack_list_head(void)
{
purge_registrations();
return list_head(others_services);
}
/*---------------------------------------------------------------------------*/
servreg_hack_id_t
servreg_hack_item_id(servreg_hack_item_t *item)
{
return ((struct servreg_hack_registration *)item)->id;
}
/*---------------------------------------------------------------------------*/
uip_ipaddr_t *
servreg_hack_item_address(servreg_hack_item_t *item)
{
return &((struct servreg_hack_registration *)item)->addr;
}
/*---------------------------------------------------------------------------*/
uip_ipaddr_t *
servreg_hack_lookup(servreg_hack_id_t id)
{
servreg_hack_item_t *t;
servreg_hack_init();
purge_registrations();
for(t = servreg_hack_list_head(); t != NULL; t = list_item_next(t)) {
if(servreg_hack_item_id(t) == id) {
return servreg_hack_item_address(t);
}
}
return NULL;
}
/*---------------------------------------------------------------------------*/
static void
handle_incoming_reg(const uip_ipaddr_t *owner, servreg_hack_id_t id, uint8_t seqno)
{
servreg_hack_item_t *t;
struct servreg_hack_registration *r;
/* Walk through list, see if we already have a service ID
registered. If so, we do different things depending on the seqno
of the update: if the seqno is older than what we have, we
discard the incoming registration. If the seqno is newer than
what we have, we reset the lifetime timer of the current
registration.
If we did not have the service registered already, we allocate a
new registration and put it on our list. If we cannot allocate a
service registration, we discard the incoming registration (for
now - we might later choose to discard the oldest registration
that we have). */
for(t = servreg_hack_list_head();
t != NULL;
t = list_item_next(t)) {
if(servreg_hack_item_id(t) == id) {
r = t;
if(SEQNO_LT(r->seqno, seqno)) {
r->seqno = seqno;
timer_set(&r->timer, LIFETIME);
/* Put item first on list, so that subsequent lookups will
find this one. */
list_remove(others_services, r);
list_push(others_services, r);
}
return;
}
}
r = memb_alloc(®istrations);
if(r == NULL) {
printf("servreg_hack_register: error, could not allocate memory, should reclaim another registration but this has not been implemented yet.\n");
return;
}
r->id = id;
r->seqno = 1;
uip_ipaddr_copy(&r->addr, owner);
timer_set(&r->timer, LIFETIME);
list_add(others_services, r);
}
/*---------------------------------------------------------------------------*/
/*
* The structure of UDP messages:
*
* +-------------------+-------------------+
* | Numregs (1 byte) | Flags (1 byte) |
* +-------------------+-------------------+-------------------+
* | IP addr (16 bytes)| 3 regs (3 bytes) | Seqno (1 byte) |
* +-------------------+-------------------+-------------------+
* | IP addr (16 bytes)| 3 regs (3 bytes) | Seqno (1 byte) |
* +-------------------+-------------------+-------------------+
* | ... | ... | ... |
* +-------------------+-------------------+-------------------+
*/
#define MSG_NUMREGS_OFFSET 0
#define MSG_FLAGS_OFFSET 1
#define MSG_ADDRS_OFFSET 2
#define MSG_IPADDR_SUBOFFSET 0
#define MSG_REGS_SUBOFFSET 16
#define MSG_SEQNO_SUBOFFSET 19
#define MSG_ADDRS_LEN 20
/*---------------------------------------------------------------------------*/
static void
send_udp_packet(struct uip_udp_conn *conn)
{
int numregs;
uint8_t buf[MAX_BUFSIZE];
int bufptr;
servreg_hack_item_t *t;
buf[MSG_FLAGS_OFFSET] = 0;
numregs = 0;
bufptr = MSG_ADDRS_OFFSET;
for(t = list_head(own_services);
(bufptr + MSG_ADDRS_LEN <= MAX_BUFSIZE) && t != NULL;
t = list_item_next(t)) {
uip_ipaddr_copy((uip_ipaddr_t *)&buf[bufptr + MSG_IPADDR_SUBOFFSET],
servreg_hack_item_address(t));
buf[bufptr + MSG_REGS_SUBOFFSET] =
servreg_hack_item_id(t);
buf[bufptr + MSG_REGS_SUBOFFSET + 1] =
buf[bufptr + MSG_REGS_SUBOFFSET + 2] = 0;
buf[bufptr + MSG_SEQNO_SUBOFFSET] = ((struct servreg_hack_registration *)t)->seqno;
bufptr += MSG_ADDRS_LEN;
++numregs;
}
for(t = servreg_hack_list_head();
(bufptr + MSG_ADDRS_LEN <= MAX_BUFSIZE) && t != NULL;
t = list_item_next(t)) {
uip_ipaddr_copy((uip_ipaddr_t *)&buf[bufptr + MSG_IPADDR_SUBOFFSET],
servreg_hack_item_address(t));
buf[bufptr + MSG_REGS_SUBOFFSET] =
servreg_hack_item_id(t);
buf[bufptr + MSG_REGS_SUBOFFSET + 1] =
buf[bufptr + MSG_REGS_SUBOFFSET + 2] = 0;
buf[bufptr + MSG_SEQNO_SUBOFFSET] = ((struct servreg_hack_registration *)t)->seqno;
bufptr += MSG_ADDRS_LEN;
++numregs;
}
/* printf("send_udp_packet numregs %d\n", numregs);*/
buf[MSG_NUMREGS_OFFSET] = numregs;
if(numregs > 0) {
/* printf("Sending buffer len %d\n", bufptr);*/
uip_udp_packet_send(conn, buf, bufptr);
}
}
/*---------------------------------------------------------------------------*/
static void
parse_incoming_packet(const uint8_t *buf, int len)
{
int numregs;
int flags;
int i;
int bufptr;
numregs = buf[MSG_NUMREGS_OFFSET];
flags = buf[MSG_FLAGS_OFFSET];
/* printf("parse_incoming_packet Numregs %d flags %d\n", numregs, flags);*/
bufptr = MSG_ADDRS_OFFSET;
for(i = 0; i < numregs; ++i) {
handle_incoming_reg((uip_ipaddr_t *)&buf[bufptr + MSG_IPADDR_SUBOFFSET],
buf[bufptr + MSG_REGS_SUBOFFSET],
buf[bufptr + MSG_SEQNO_SUBOFFSET]);
}
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(servreg_hack_process, ev, data)
{
static struct etimer periodic;
static struct uip_udp_conn *outconn, *inconn;
PROCESS_BEGIN();
/* Create outbound UDP connection. */
outconn = udp_broadcast_new(UIP_HTONS(UDP_PORT), NULL);
udp_bind(outconn, UIP_HTONS(UDP_PORT));
/* Create inbound UDP connection. */
inconn = udp_new(NULL, UIP_HTONS(UDP_PORT), NULL);
udp_bind(inconn, UIP_HTONS(UDP_PORT));
etimer_set(&periodic, PERIOD_TIME);
etimer_set(&sendtimer, random_rand() % (PERIOD_TIME));
while(1) {
PROCESS_WAIT_EVENT();
if(ev == PROCESS_EVENT_TIMER && data == &periodic) {
etimer_reset(&periodic);
etimer_set(&sendtimer, random_rand() % (PERIOD_TIME));
} else if(ev == PROCESS_EVENT_TIMER && data == &sendtimer) {
send_udp_packet(outconn);
} else if(ev == tcpip_event) {
parse_incoming_packet(uip_appdata, uip_datalen());
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/