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