| 1 | /* |
| 2 | * Timestamp mechanism for udp tunnel |
| 3 | * |
| 4 | * mechanism: timestamp |
| 5 | * arguments: <max-skew> <max-age> |
| 6 | * |
| 7 | * restrictions: none |
| 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 | */ |
| 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 | */ |
| 32 | |
| 33 | #include <stdint.h> |
| 34 | #include <netinet/in.h> |
| 35 | |
| 36 | #include "forwarder.h" |
| 37 | |
| 38 | #define WARN_EVERY 30 |
| 39 | |
| 40 | struct mechdata { |
| 41 | time_t max_skew, max_age; |
| 42 | time_t next_warn; |
| 43 | }; |
| 44 | |
| 45 | static 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(); |
| 52 | md->next_warn= now(); |
| 53 | *md_r= md; |
| 54 | } |
| 55 | |
| 56 | static void mes_timestamp(struct mechdata **md_r, int *maxprefix_io, int *maxsuffix_io) { |
| 57 | mds_timestamp(md_r); |
| 58 | *maxprefix_io += 4; |
| 59 | } |
| 60 | |
| 61 | static void menc_timestamp(struct mechdata *md, struct buffer *buf) { |
| 62 | *(uint32_t*)buf_prepend(buf,4)= htonl(now()); |
| 63 | } |
| 64 | |
| 65 | static const char *mdec_timestamp(struct mechdata *md, struct buffer *buf) { |
| 66 | static char cbuf[40]; |
| 67 | |
| 68 | uint32_t *tp, timestamp; |
| 69 | time_t tnow; |
| 70 | long age; |
| 71 | |
| 72 | BUF_UNPREPEND(tp,buf,4); |
| 73 | timestamp= ntohl(*tp); |
| 74 | |
| 75 | tnow= now(); |
| 76 | age= timestamp - (uint32_t)tnow; |
| 77 | if (age > 0) { |
| 78 | if (!md->max_age || age <= md->max_age) return 0; |
| 79 | sprintf(cbuf,"packet too old (%lds)",age); |
| 80 | } else if (age < 0) { |
| 81 | if (!md->max_skew || age >= -md->max_skew) return 0; |
| 82 | sprintf(cbuf,"too much skew (%lds)",-age); |
| 83 | } else { |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | if (tnow < md->next_warn) return ""; |
| 88 | |
| 89 | md->next_warn= tnow+WARN_EVERY; |
| 90 | return cbuf; |
| 91 | } |
| 92 | |
| 93 | STANDARD_MECHANISMLIST("timestamp",timestamp); |