chiark / gitweb /
Improve adduser calls:
[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  * This file is part of ipif, part of userv-utils
17  *
18  * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
19  * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
20  * Copyright 1999,2003
21  *    Chancellor Masters and Scholars of the University of Cambridge
22  * Copyright 2010 Tony Finch <fanf@dotat.at>
23  *
24  * This is free software; you can redistribute it and/or modify it
25  * under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful, but
30  * WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32  * General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with userv-utils; if not, see http://www.gnu.org/licenses/.
36  */
37
38 #include <stdint.h>
39 #include <netinet/in.h>
40
41 #include "forwarder.h"
42
43 #define WARN_EVERY 30
44
45 struct mechdata {
46   time_t max_skew, max_age;
47   time_t next_warn;
48 };
49
50 static void mds_timestamp(struct mechdata **md_r) {
51   struct mechdata *md;
52
53   md= xmalloc(sizeof(md));
54   
55   md->max_skew= getarg_ulong();
56   md->max_age= getarg_ulong();
57   md->next_warn= now();
58   *md_r= md;
59 }
60
61 static void mes_timestamp(struct mechdata **md_r, int *maxprefix_io, int *maxsuffix_io) {
62   mds_timestamp(md_r);
63   *maxprefix_io += 4;
64 }
65
66 static void menc_timestamp(struct mechdata *md, struct buffer *buf) {
67   *(uint32_t*)buf_prepend(buf,4)= htonl(now());
68 }
69   
70 static const char *mdec_timestamp(struct mechdata *md, struct buffer *buf) {
71   static char cbuf[40];
72   
73   uint32_t *tp, timestamp;
74   time_t tnow;
75   long age;
76
77   BUF_UNPREPEND(tp,buf,4);
78   timestamp= ntohl(*tp);
79
80   tnow= now();
81   age= timestamp - (uint32_t)tnow;
82   if (age > 0) {
83     if (!md->max_age || age <= md->max_age) return 0;
84     sprintf(cbuf,"packet too old (%lds)",age);
85   } else if (age < 0) {
86     if (!md->max_skew || age >= -md->max_skew) return 0;
87     sprintf(cbuf,"too much skew (%lds)",-age);
88   } else {
89     return 0;
90   }
91
92   if (tnow < md->next_warn) return "";
93
94   md->next_warn= tnow+WARN_EVERY;
95   return cbuf;
96 }
97
98 STANDARD_MECHANISMLIST("timestamp",timestamp);