~ubuntu-branches/ubuntu/saucy/openvpn/saucy

« back to all changes in this revision

Viewing changes to packet_id.h

  • Committer: Bazaar Package Importer
  • Author(s): Alberto Gonzalez Iniesta
  • Date: 2005-01-05 19:03:11 UTC
  • mto: (1.4.1) (10.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050105190311-d0uioiqtor5xbzre
Tags: upstream-1.99+2.rc6
ImportĀ upstreamĀ versionĀ 1.99+2.rc6

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include "circ_list.h"
38
38
#include "buffer.h"
39
39
#include "error.h"
 
40
#include "otime.h"
40
41
 
41
42
/*
42
43
 * Enables OpenVPN to be compiled in special packet_id test mode.
94
95
/*
95
96
 * Printf formats for special types
96
97
 */
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;
103
 
#else
104
 
#error "cannot figure proper format to print uint32_t"
105
 
#endif
106
100
 
107
101
/*
108
102
 * Maximum allowed backtrack in
161
155
  packet_id_type id;       /* sequence number */
162
156
  time_t time_last_written;
163
157
  packet_id_type id_last_written;
164
 
  time_t last_flush;
165
158
};
166
159
 
167
160
struct packet_id_persist_file_image
224
217
 
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,
228
 
                    time_t current);
 
220
                    const struct packet_id_net *pin);
229
221
 
230
222
/* expire TIME_BACKTRACK sequence numbers */ 
231
 
void packet_id_reap (struct packet_id_rec *p,
232
 
                     time_t current);
 
223
void packet_id_reap (struct packet_id_rec *p);
233
224
 
234
225
/*
235
226
 * packet ID persistence
251
242
void packet_id_persist_load_obj (const struct packet_id_persist *p, struct packet_id* pid);
252
243
 
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);
 
246
 
 
247
/*
 
248
 * Read/write a packet ID to/from the buffer.  Short form is sequence number
 
249
 * only.  Long form is sequence number and timestamp.
 
250
 */
 
251
 
 
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);
255
254
 
256
255
/*
257
256
 * Inline functions.
275
274
    }
276
275
}
277
276
 
278
 
/* flush the current packet_id to disk, once per n seconds */
279
 
static inline void
280
 
packet_id_persist_flush (struct packet_id_persist *p, time_t current, int n)
281
 
{
282
 
  if (packet_id_persist_enabled (p))
283
 
    {
284
 
      if (!p->last_flush || p->last_flush + n < current)
285
 
        {
286
 
          packet_id_persist_save (p);
287
 
          p->last_flush = current;
288
 
        }
289
 
    }
290
 
}
291
 
 
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);
293
278
 
294
279
#ifdef PID_TEST
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)
317
302
{
318
303
  if (!p->time)
319
 
    p->time = time (NULL);
 
304
    p->time = now;
320
305
  pin->id = ++p->id;
321
306
  if (!pin->id)
322
307
    {
323
308
      ASSERT (long_form);
324
 
      p->time = time (NULL);
 
309
      p->time = now;
325
310
      pin->id = p->id = 1;
326
311
    }
327
312
  pin->time = p->time;
328
313
}
329
314
 
330
 
/*
331
 
 * Read/write a packet ID to/from the buffer.  Short form is sequence number
332
 
 * only.  Long form is sequence number and timestamp.
333
 
 */
334
 
 
335
 
static inline bool
336
 
packet_id_read (struct packet_id_net *pin, struct buffer *buf, bool long_form)
337
 
{
338
 
  packet_id_type net_id;
339
 
  net_time_t net_time;
340
 
 
341
 
  pin->id = 0;
342
 
  pin->time = 0;
343
 
 
344
 
  if (!buf_read (buf, &net_id, sizeof (net_id)))
345
 
    return false;
346
 
  pin->id = ntohpid (net_id);
347
 
  if (long_form)
348
 
    {
349
 
      if (!buf_read (buf, &net_time, sizeof (net_time)))
350
 
        return false;
351
 
      pin->time = ntohtime (net_time);
352
 
    }
353
 
  return true;
354
 
}
355
 
 
356
 
static inline bool
357
 
packet_id_write (const struct packet_id_net *pin, struct buffer *buf, bool long_form, bool prepend)
358
 
{
359
 
  packet_id_type net_id = htonpid (pin->id);
360
 
  net_time_t net_time = htontime (pin->time);
361
 
 
362
 
  if (prepend)
363
 
    {
364
 
      if (long_form)
365
 
        {
366
 
          if (!buf_write_prepend (buf, &net_time, sizeof (net_time)))
367
 
            return false;
368
 
        }
369
 
      if (!buf_write_prepend (buf, &net_id, sizeof (net_id)))
370
 
        return false;
371
 
    }
372
 
  else
373
 
    {
374
 
      if (!buf_write (buf, &net_id, sizeof (net_id)))
375
 
        return false;
376
 
      if (long_form)
377
 
        {
378
 
          if (!buf_write (buf, &net_time, sizeof (net_time)))
379
 
            return false;
380
 
        }
381
 
    }
382
 
  return true;
383
 
}
384
 
 
385
 
static inline bool
386
 
check_timestamp_delta (time_t current, time_t remote, unsigned int max_delta)
 
315
static inline bool
 
316
check_timestamp_delta (time_t remote, unsigned int max_delta)
387
317
{
388
318
  unsigned int abs;
389
 
  if (current >= remote)
390
 
    abs = current - remote;
 
319
  const time_t local_now = now;
 
320
 
 
321
  if (local_now >= remote)
 
322
    abs = local_now - remote;
391
323
  else
392
 
    abs = remote - current;
 
324
    abs = remote - local_now;
393
325
  return abs <= max_delta;
394
326
}
395
327
 
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)
398
330
{
399
 
  if (p->last_reap + SEQ_REAP_INTERVAL <= current)
400
 
    packet_id_reap (p, current);
 
331
  if (p->last_reap + SEQ_REAP_INTERVAL <= now)
 
332
    packet_id_reap (p);
401
333
}
402
334
 
403
335
#endif /* PACKET_ID_H */