44 #include "contiki-lib.h"
45 #include "contiki-net.h"
55 struct servreg_hack_registration {
56 struct servreg_hack_registration *next;
65 #define MAX_REGISTRATIONS 16
67 LIST(others_services);
70 MEMB(registrations,
struct servreg_hack_registration, MAX_REGISTRATIONS);
72 PROCESS(servreg_hack_process,
"Service regstry hack");
74 #define PERIOD_TIME 120 * CLOCK_SECOND
76 #define NEW_REG_TIME 10 * CLOCK_SECOND
78 #define MAX_BUFSIZE 2 + 80
80 #define UDP_PORT 61616
82 #define LIFETIME 10 * 60 * CLOCK_SECOND
84 #define SEQNO_LT(a, b) ((signed char)((a) - (b)) < 0)
86 static struct etimer sendtimer;
88 static uint8_t started = 0;
94 purge_registrations(
void)
96 struct servreg_hack_registration *t;
134 servreg_hack_item_t *t;
135 struct servreg_hack_registration *r;
153 printf(
"servreg_hack_register: error, could not allocate memory, should reclaim another registration but this has not been implemented yet.\n");
169 servreg_hack_item_t *
172 purge_registrations();
179 return ((
struct servreg_hack_registration *)item)->id;
185 return &((
struct servreg_hack_registration *)item)->addr;
191 servreg_hack_item_t *t;
195 purge_registrations();
206 handle_incoming_reg(
const uip_ipaddr_t *owner, servreg_hack_id_t
id, uint8_t seqno)
208 servreg_hack_item_t *t;
209 struct servreg_hack_registration *r;
229 if(SEQNO_LT(r->seqno, seqno)) {
244 printf(
"servreg_hack_register: error, could not allocate memory, should reclaim another registration but this has not been implemented yet.\n");
268 #define MSG_NUMREGS_OFFSET 0
269 #define MSG_FLAGS_OFFSET 1
270 #define MSG_ADDRS_OFFSET 2
272 #define MSG_IPADDR_SUBOFFSET 0
273 #define MSG_REGS_SUBOFFSET 16
274 #define MSG_SEQNO_SUBOFFSET 19
276 #define MSG_ADDRS_LEN 20
283 uint8_t buf[MAX_BUFSIZE];
285 servreg_hack_item_t *t;
287 buf[MSG_FLAGS_OFFSET] = 0;
290 bufptr = MSG_ADDRS_OFFSET;
293 (bufptr + MSG_ADDRS_LEN <= MAX_BUFSIZE) && t !=
NULL;
298 buf[bufptr + MSG_REGS_SUBOFFSET] =
300 buf[bufptr + MSG_REGS_SUBOFFSET + 1] =
301 buf[bufptr + MSG_REGS_SUBOFFSET + 2] = 0;
302 buf[bufptr + MSG_SEQNO_SUBOFFSET] = ((
struct servreg_hack_registration *)t)->seqno;
304 bufptr += MSG_ADDRS_LEN;
309 (bufptr + MSG_ADDRS_LEN <= MAX_BUFSIZE) && t !=
NULL;
313 buf[bufptr + MSG_REGS_SUBOFFSET] =
315 buf[bufptr + MSG_REGS_SUBOFFSET + 1] =
316 buf[bufptr + MSG_REGS_SUBOFFSET + 2] = 0;
317 buf[bufptr + MSG_SEQNO_SUBOFFSET] = ((
struct servreg_hack_registration *)t)->seqno;
319 bufptr += MSG_ADDRS_LEN;
323 buf[MSG_NUMREGS_OFFSET] = numregs;
327 uip_udp_packet_send(conn, buf, bufptr);
332 parse_incoming_packet(
const uint8_t *buf,
int len)
339 numregs = buf[MSG_NUMREGS_OFFSET];
340 flags = buf[MSG_FLAGS_OFFSET];
344 bufptr = MSG_ADDRS_OFFSET;
345 for(i = 0; i < numregs; ++i) {
346 handle_incoming_reg((uip_ipaddr_t *)&buf[bufptr + MSG_IPADDR_SUBOFFSET],
347 buf[bufptr + MSG_REGS_SUBOFFSET],
348 buf[bufptr + MSG_SEQNO_SUBOFFSET]);
354 static struct etimer periodic;
370 if(ev == PROCESS_EVENT_TIMER && data == &periodic) {
373 }
else if(ev == PROCESS_EVENT_TIMER && data == &sendtimer) {
374 send_udp_packet(outconn);
uip_ipaddr_t * servreg_hack_lookup(servreg_hack_id_t id)
Get the IP address of a node offering a service.
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
void list_push(list_t list, void *item)
Add an item to the start of the list.
void servreg_hack_init(void)
Initialize and start the servreg-hack application.
#define PROCESS_BEGIN()
Define the beginning of a process.
Header file for the uIP TCP/IP stack.
CCIF struct uip_udp_conn * udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Create a new UDP connection.
Network interface and stateless autoconfiguration (RFC 4862)
void * list_item_next(void *item)
Get the next item following this item.
servreg_hack_id_t servreg_hack_item_id(servreg_hack_item_t *item)
Get the service ID for a list item.
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
#define NULL
The null pointer.
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
#define PROCESS_END()
Define the end of a process.
struct uip_udp_conn * udp_broadcast_new(uint16_t port, void *appstate)
Create a new UDP broadcast connection.
void list_init(list_t list)
Initialize a list.
servreg_hack_item_t * servreg_hack_list_head(void)
Obtain the list of services provided by neighbors.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
void * list_head(list_t list)
Get a pointer to the first element of a list.
#define MEMB(name, structure, num)
Declare a memory block.
void list_add(list_t list, void *item)
Add an item at the end of a list.
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
#define PROCESS(name, strname)
Declare a process.
#define LIST(name)
Declare a linked list.
uip_ipaddr_t * servreg_hack_item_address(servreg_hack_item_t *item)
Get the IP address for a list item.
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
#define uip_datalen()
The length of any incoming data that is currently available (if available) in the uip_appdata buffer...
process_event_t tcpip_event
The uIP event.
void process_start(struct process *p, process_data_t data)
Start a process.
Header file for the servreg-hack application
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
#define udp_bind(conn, port)
Bind a UDP connection to a local port.
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
Representation of a uIP UDP connection.
int timer_expired(struct timer *t)
Check if a timer has expired.
void servreg_hack_register(servreg_hack_id_t id, const uip_ipaddr_t *addr)
Register that this node provides a service.
unsigned short random_rand(void)
Generate the next state and return the upper part of it.
uip_appdata
Pointer to the application data in the packet buffer.
#define PROCESS_CONTEXT_END(p)
End a context switch.