84f87e82 |
1 | /* |
935da5a4 |
2 | * Timestamp mechanism for udp tunnel |
84f87e82 |
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 | */ |
935da5a4 |
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 | */ |
84f87e82 |
30 | |
31 | #include <stdint.h> |
32 | #include <netinet/in.h> |
33 | |
aaa9ab3a |
34 | #include "forwarder.h" |
84f87e82 |
35 | |
935da5a4 |
36 | #define WARN_EVERY 30 |
37 | |
84f87e82 |
38 | struct mechdata { |
8bb9d875 |
39 | time_t max_skew, max_age; |
935da5a4 |
40 | time_t next_warn; |
84f87e82 |
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(); |
935da5a4 |
50 | md->next_warn= now(); |
84f87e82 |
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 | |
935da5a4 |
66 | uint32_t *tp, timestamp; |
67 | time_t tnow; |
84f87e82 |
68 | long age; |
69 | |
70 | BUF_UNPREPEND(tp,buf,4); |
71 | timestamp= ntohl(*tp); |
72 | |
73 | tnow= now(); |
935da5a4 |
74 | age= timestamp - (uint32_t)tnow; |
84f87e82 |
75 | if (age > 0) { |
935da5a4 |
76 | if (!md->max_age || age <= md->max_age) return 0; |
77 | sprintf(cbuf,"packet too old (%lds)",age); |
84f87e82 |
78 | } else if (age < 0) { |
935da5a4 |
79 | if (!md->max_skew || age >= -md->max_skew) return 0; |
80 | sprintf(cbuf,"too much skew (%lds)",-age); |
4fa63592 |
81 | } else { |
82 | return 0; |
84f87e82 |
83 | } |
84 | |
935da5a4 |
85 | if (tnow < md->next_warn) return ""; |
86 | |
87 | md->next_warn= tnow+WARN_EVERY; |
88 | return cbuf; |
84f87e82 |
89 | } |
90 | |
91 | STANDARD_MECHANISMLIST("timestamp",timestamp); |