chiark / gitweb /
New encrypting tunnel seems to work !
[userv-utils.git] / ipif / mech-sequence.c
1 /*
2  * Sequence number / nonce mechanism
3  *
4  * arguments: none
5  *
6  * encoding: prepend 4 bytes of sequence arithmetic serial number
7  * decoding: check increasingness, or ignore
8  */
9
10 #include <netinet/in.h>
11
12 #include "forwarder.h"
13
14 struct mechdata {
15   uint32_t number;
16   int anyseen; /* decode only */
17 };
18
19 static void mes_sequence(struct mechdata **md_r, int *maxprefix_io, int *maxsuffix_io) {
20   struct mechdata *md;
21
22   XMALLOC(md);
23   get_random(&md->number,sizeof(md->number));
24   *maxprefix_io += 4;
25   *md_r= md;
26 }
27
28 static void mds_sequence(struct mechdata **md_r) {
29   struct mechdata *md;
30   XMALLOC(md);
31   md->anyseen= 0;
32   *md_r= md;
33 }
34
35 static void menc_sequence(struct mechdata *md, struct buffer *buf) {
36   md->number++;
37   *(uint32_t*)buf_prepend(buf,4)= htonl(md->number);
38 }
39   
40 static const char *mdec_check(struct mechdata *md, struct buffer *buf) {
41   uint32_t *sp, sequence;
42
43   BUF_UNPREPEND(sp,buf,4);
44   sequence= ntohl(*sp);
45
46   if (md->anyseen)
47     if (sequence - md->number >= 0x800000UL) return "out of order packet";
48
49   md->number= sequence;
50   md->anyseen= 1;
51
52   return 0;
53 }
54   
55 static const char *mdec_skip(struct mechdata *md, struct buffer *buf) {
56   uint32_t *sp;
57   BUF_UNPREPEND(sp,buf,4);
58   return 0;
59 }
60
61 const struct mechanism mechlist_sequence[]= {
62  { "nonce",         mes_sequence, mds_sequence,  menc_sequence, mdec_skip    },
63  { "sequence",      mes_sequence, mds_sequence,  menc_sequence, mdec_check   },
64  { 0 }
65 };