Commit | Line | Data |
---|---|---|
1fb3cba0 | 1 | /* |
f0e54a99 | 2 | * Timestamp mechanism for udp tunnel |
1fb3cba0 | 3 | * |
5124214b | 4 | * mechanism: timestamp |
1fb3cba0 | 5 | * arguments: <max-skew> <max-age> |
6 | * | |
5124214b | 7 | * restrictions: none |
1fb3cba0 | 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 | */ | |
f0e54a99 | 15 | /* |
c07be359 | 16 | * This file is part of ipif, part of userv-utils |
f0e54a99 | 17 | * |
9028e234 IJ |
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 | * | |
f0e54a99 | 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 | |
9028e234 | 26 | * the Free Software Foundation; either version 3 of the License, or |
f0e54a99 | 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 | |
9028e234 | 35 | * along with userv-utils; if not, see http://www.gnu.org/licenses/. |
f0e54a99 | 36 | */ |
1fb3cba0 | 37 | |
38 | #include <stdint.h> | |
39 | #include <netinet/in.h> | |
40 | ||
f9e59051 | 41 | #include "forwarder.h" |
1fb3cba0 | 42 | |
f0e54a99 | 43 | #define WARN_EVERY 30 |
44 | ||
1fb3cba0 | 45 | struct mechdata { |
ed509ebd | 46 | time_t max_skew, max_age; |
f0e54a99 | 47 | time_t next_warn; |
1fb3cba0 | 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(); | |
f0e54a99 | 57 | md->next_warn= now(); |
1fb3cba0 | 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 | ||
f0e54a99 | 73 | uint32_t *tp, timestamp; |
74 | time_t tnow; | |
1fb3cba0 | 75 | long age; |
76 | ||
77 | BUF_UNPREPEND(tp,buf,4); | |
78 | timestamp= ntohl(*tp); | |
79 | ||
80 | tnow= now(); | |
f0e54a99 | 81 | age= timestamp - (uint32_t)tnow; |
1fb3cba0 | 82 | if (age > 0) { |
f0e54a99 | 83 | if (!md->max_age || age <= md->max_age) return 0; |
84 | sprintf(cbuf,"packet too old (%lds)",age); | |
1fb3cba0 | 85 | } else if (age < 0) { |
f0e54a99 | 86 | if (!md->max_skew || age >= -md->max_skew) return 0; |
87 | sprintf(cbuf,"too much skew (%lds)",-age); | |
31dcbf98 | 88 | } else { |
89 | return 0; | |
1fb3cba0 | 90 | } |
91 | ||
f0e54a99 | 92 | if (tnow < md->next_warn) return ""; |
93 | ||
94 | md->next_warn= tnow+WARN_EVERY; | |
95 | return cbuf; | |
1fb3cba0 | 96 | } |
97 | ||
98 | STANDARD_MECHANISMLIST("timestamp",timestamp); |