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