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 | * Copyright (C) 2000,2003 Ian Jackson |
17 | * This file is part of ipif, part of userv-utils |
f0e54a99 |
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 | */ |
1fb3cba0 |
33 | |
34 | #include <stdint.h> |
35 | #include <netinet/in.h> |
36 | |
f9e59051 |
37 | #include "forwarder.h" |
1fb3cba0 |
38 | |
f0e54a99 |
39 | #define WARN_EVERY 30 |
40 | |
1fb3cba0 |
41 | struct mechdata { |
ed509ebd |
42 | time_t max_skew, max_age; |
f0e54a99 |
43 | time_t next_warn; |
1fb3cba0 |
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(); |
f0e54a99 |
53 | md->next_warn= now(); |
1fb3cba0 |
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 | |
f0e54a99 |
69 | uint32_t *tp, timestamp; |
70 | time_t tnow; |
1fb3cba0 |
71 | long age; |
72 | |
73 | BUF_UNPREPEND(tp,buf,4); |
74 | timestamp= ntohl(*tp); |
75 | |
76 | tnow= now(); |
f0e54a99 |
77 | age= timestamp - (uint32_t)tnow; |
1fb3cba0 |
78 | if (age > 0) { |
f0e54a99 |
79 | if (!md->max_age || age <= md->max_age) return 0; |
80 | sprintf(cbuf,"packet too old (%lds)",age); |
1fb3cba0 |
81 | } else if (age < 0) { |
f0e54a99 |
82 | if (!md->max_skew || age >= -md->max_skew) return 0; |
83 | sprintf(cbuf,"too much skew (%lds)",-age); |
31dcbf98 |
84 | } else { |
85 | return 0; |
1fb3cba0 |
86 | } |
87 | |
f0e54a99 |
88 | if (tnow < md->next_warn) return ""; |
89 | |
90 | md->next_warn= tnow+WARN_EVERY; |
91 | return cbuf; |
1fb3cba0 |
92 | } |
93 | |
94 | STANDARD_MECHANISMLIST("timestamp",timestamp); |