X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=ipif%2Fmech-timestamp.c;h=129eee96b12ceed693409535930950ce04653cf2;hb=97f318e04ed0024b2bf5903215a75249401d3fd7;hp=f679b104e625c889308a1274f07b3cb7466fdf4a;hpb=1fb3cba0b41ae774f83c11d2a9c23b12b2c87d1a;p=userv-utils.git diff --git a/ipif/mech-timestamp.c b/ipif/mech-timestamp.c index f679b10..129eee9 100644 --- a/ipif/mech-timestamp.c +++ b/ipif/mech-timestamp.c @@ -1,8 +1,10 @@ /* - * Timestamp mechanism + * Timestamp mechanism for udp tunnel * + * mechanism: timestamp * arguments: * + * restrictions: none * encoding: prepend 4 bytes of UNIX time in network byte order * * is maximum age in seconds we will accept a packet (or 0 @@ -10,14 +12,39 @@ * accept a packet (or 0 for any future age). * */ +/* + * This file is part of ipif, part of userv-utils + * + * Copyright 1996-2013 Ian Jackson + * Copyright 1998 David Damerell + * Copyright 1999,2003 + * Chancellor Masters and Scholars of the University of Cambridge + * Copyright 2010 Tony Finch + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with userv-utils; if not, see http://www.gnu.org/licenses/. + */ #include #include -#include "mech.h" +#include "forwarder.h" + +#define WARN_EVERY 30 struct mechdata { - uint32_t max_skew, max_age; + time_t max_skew, max_age; + time_t next_warn; }; static void mds_timestamp(struct mechdata **md_r) { @@ -27,6 +54,7 @@ static void mds_timestamp(struct mechdata **md_r) { md->max_skew= getarg_ulong(); md->max_age= getarg_ulong(); + md->next_warn= now(); *md_r= md; } @@ -42,27 +70,29 @@ static void menc_timestamp(struct mechdata *md, struct buffer *buf) { static const char *mdec_timestamp(struct mechdata *md, struct buffer *buf) { static char cbuf[40]; - uint32_t *tp, timestamp, tnow; + uint32_t *tp, timestamp; + time_t tnow; long age; BUF_UNPREPEND(tp,buf,4); timestamp= ntohl(*tp); tnow= now(); - age= timestamp - tnow; + age= timestamp - (uint32_t)tnow; if (age > 0) { - if (md->max_age && age > md->max_age) { - sprintf(cbuf,"packet too old (%lds)",age); - return cbuf; - } + if (!md->max_age || age <= md->max_age) return 0; + sprintf(cbuf,"packet too old (%lds)",age); } else if (age < 0) { - if (md->max_skew && age > md->max_skew) { - sprintf(cbuf,"too much skew (%lds)",-age); - return cbuf; - } + if (!md->max_skew || age >= -md->max_skew) return 0; + sprintf(cbuf,"too much skew (%lds)",-age); + } else { + return 0; } - return 0; + if (tnow < md->next_warn) return ""; + + md->next_warn= tnow+WARN_EVERY; + return cbuf; } STANDARD_MECHANISMLIST("timestamp",timestamp);