chiark / gitweb /
ipif: move #include of automech.h down so that it has definition of struct mechanism
[userv-utils.git] / ipif / mech-timestamp.c
1 /*
2  * Timestamp mechanism for udp tunnel
3  *
4  * mechanism: timestamp
5  * arguments: <max-skew> <max-age>
6  *
7  * restrictions: none
8  * encoding: prepend 4 bytes of UNIX time in network byte order
9  *
10  * <max-age> is maximum age in seconds we will accept a packet (or 0
11  * for any age); <max-skew> is maximum future age in seconds we will
12  * accept a packet (or 0 for any future age).
13  *
14  */
15 /*
16  * Copyright (C) 2000,2003 Ian Jackson
17  * This file is part of ipif, part of userv-utils
18  *
19  * This is free software; you can redistribute it and/or modify it
20  * under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful, but
25  * WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27  * General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with userv-utils; if not, write to the Free Software
31  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32  */
33
34 #include <stdint.h>
35 #include <netinet/in.h>
36
37 #include "forwarder.h"
38
39 #define WARN_EVERY 30
40
41 struct mechdata {
42   time_t max_skew, max_age;
43   time_t next_warn;
44 };
45
46 static void mds_timestamp(struct mechdata **md_r) {
47   struct mechdata *md;
48
49   md= xmalloc(sizeof(md));
50   
51   md->max_skew= getarg_ulong();
52   md->max_age= getarg_ulong();
53   md->next_warn= now();
54   *md_r= md;
55 }
56
57 static void mes_timestamp(struct mechdata **md_r, int *maxprefix_io, int *maxsuffix_io) {
58   mds_timestamp(md_r);
59   *maxprefix_io += 4;
60 }
61
62 static void menc_timestamp(struct mechdata *md, struct buffer *buf) {
63   *(uint32_t*)buf_prepend(buf,4)= htonl(now());
64 }
65   
66 static const char *mdec_timestamp(struct mechdata *md, struct buffer *buf) {
67   static char cbuf[40];
68   
69   uint32_t *tp, timestamp;
70   time_t tnow;
71   long age;
72
73   BUF_UNPREPEND(tp,buf,4);
74   timestamp= ntohl(*tp);
75
76   tnow= now();
77   age= timestamp - (uint32_t)tnow;
78   if (age > 0) {
79     if (!md->max_age || age <= md->max_age) return 0;
80     sprintf(cbuf,"packet too old (%lds)",age);
81   } else if (age < 0) {
82     if (!md->max_skew || age >= -md->max_skew) return 0;
83     sprintf(cbuf,"too much skew (%lds)",-age);
84   } else {
85     return 0;
86   }
87
88   if (tnow < md->next_warn) return "";
89
90   md->next_warn= tnow+WARN_EVERY;
91   return cbuf;
92 }
93
94 STANDARD_MECHANISMLIST("timestamp",timestamp);