95
96
* Printf formats for special types
97
#if SIZEOF_UNSIGNED_LONG == 4
98
#define packet_id_format "%lu"
99
typedef unsigned long packet_id_print_type;
100
#elif SIZEOF_UNSIGNED_INT == 4
101
98
#define packet_id_format "%u"
102
99
typedef unsigned int packet_id_print_type;
104
#error "cannot figure proper format to print uint32_t"
108
102
* Maximum allowed backtrack in
225
218
/* change our current state to reflect an accepted packet id */
226
219
void packet_id_add (struct packet_id_rec *p,
227
const struct packet_id_net *pin,
220
const struct packet_id_net *pin);
230
222
/* expire TIME_BACKTRACK sequence numbers */
231
void packet_id_reap (struct packet_id_rec *p,
223
void packet_id_reap (struct packet_id_rec *p);
235
226
* packet ID persistence
251
242
void packet_id_persist_load_obj (const struct packet_id_persist *p, struct packet_id* pid);
253
244
/* return an ascii string representing a packet_id_persist object */
254
const char *packet_id_persist_print (const struct packet_id_persist *p);
245
const char *packet_id_persist_print (const struct packet_id_persist *p, struct gc_arena *gc);
248
* Read/write a packet ID to/from the buffer. Short form is sequence number
249
* only. Long form is sequence number and timestamp.
252
bool packet_id_read (struct packet_id_net *pin, struct buffer *buf, bool long_form);
253
bool packet_id_write (const struct packet_id_net *pin, struct buffer *buf, bool long_form, bool prepend);
257
256
* Inline functions.
278
/* flush the current packet_id to disk, once per n seconds */
280
packet_id_persist_flush (struct packet_id_persist *p, time_t current, int n)
282
if (packet_id_persist_enabled (p))
284
if (!p->last_flush || p->last_flush + n < current)
286
packet_id_persist_save (p);
287
p->last_flush = current;
292
const char* packet_id_net_print(const struct packet_id_net *pin, bool print_timestamp);
277
const char* packet_id_net_print(const struct packet_id_net *pin, bool print_timestamp, struct gc_arena *gc);
295
280
void packet_id_interactive_test();
316
301
packet_id_alloc_outgoing (struct packet_id_send *p, struct packet_id_net *pin, bool long_form)
319
p->time = time (NULL);
320
305
pin->id = ++p->id;
323
308
ASSERT (long_form);
324
p->time = time (NULL);
325
310
pin->id = p->id = 1;
327
312
pin->time = p->time;
331
* Read/write a packet ID to/from the buffer. Short form is sequence number
332
* only. Long form is sequence number and timestamp.
336
packet_id_read (struct packet_id_net *pin, struct buffer *buf, bool long_form)
338
packet_id_type net_id;
344
if (!buf_read (buf, &net_id, sizeof (net_id)))
346
pin->id = ntohpid (net_id);
349
if (!buf_read (buf, &net_time, sizeof (net_time)))
351
pin->time = ntohtime (net_time);
357
packet_id_write (const struct packet_id_net *pin, struct buffer *buf, bool long_form, bool prepend)
359
packet_id_type net_id = htonpid (pin->id);
360
net_time_t net_time = htontime (pin->time);
366
if (!buf_write_prepend (buf, &net_time, sizeof (net_time)))
369
if (!buf_write_prepend (buf, &net_id, sizeof (net_id)))
374
if (!buf_write (buf, &net_id, sizeof (net_id)))
378
if (!buf_write (buf, &net_time, sizeof (net_time)))
386
check_timestamp_delta (time_t current, time_t remote, unsigned int max_delta)
316
check_timestamp_delta (time_t remote, unsigned int max_delta)
388
318
unsigned int abs;
389
if (current >= remote)
390
abs = current - remote;
319
const time_t local_now = now;
321
if (local_now >= remote)
322
abs = local_now - remote;
392
abs = remote - current;
324
abs = remote - local_now;
393
325
return abs <= max_delta;
396
328
static inline void
397
packet_id_reap_test (struct packet_id_rec *p, time_t current)
329
packet_id_reap_test (struct packet_id_rec *p)
399
if (p->last_reap + SEQ_REAP_INTERVAL <= current)
400
packet_id_reap (p, current);
331
if (p->last_reap + SEQ_REAP_INTERVAL <= now)
403
335
#endif /* PACKET_ID_H */