84f87e82 |
1 | /* |
2 | * Timestamp mechanism |
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 | */ |
13 | |
14 | #include <stdint.h> |
15 | #include <netinet/in.h> |
16 | |
aaa9ab3a |
17 | #include "forwarder.h" |
84f87e82 |
18 | |
19 | struct mechdata { |
8bb9d875 |
20 | time_t max_skew, max_age; |
84f87e82 |
21 | }; |
22 | |
23 | static void mds_timestamp(struct mechdata **md_r) { |
24 | struct mechdata *md; |
25 | |
26 | md= xmalloc(sizeof(md)); |
27 | |
28 | md->max_skew= getarg_ulong(); |
29 | md->max_age= getarg_ulong(); |
30 | *md_r= md; |
31 | } |
32 | |
33 | static void mes_timestamp(struct mechdata **md_r, int *maxprefix_io, int *maxsuffix_io) { |
34 | mds_timestamp(md_r); |
35 | *maxprefix_io += 4; |
36 | } |
37 | |
38 | static void menc_timestamp(struct mechdata *md, struct buffer *buf) { |
39 | *(uint32_t*)buf_prepend(buf,4)= htonl(now()); |
40 | } |
41 | |
42 | static const char *mdec_timestamp(struct mechdata *md, struct buffer *buf) { |
43 | static char cbuf[40]; |
44 | |
45 | uint32_t *tp, timestamp, tnow; |
46 | long age; |
47 | |
48 | BUF_UNPREPEND(tp,buf,4); |
49 | timestamp= ntohl(*tp); |
50 | |
51 | tnow= now(); |
52 | age= timestamp - tnow; |
53 | if (age > 0) { |
54 | if (md->max_age && age > md->max_age) { |
55 | sprintf(cbuf,"packet too old (%lds)",age); |
56 | return cbuf; |
57 | } |
58 | } else if (age < 0) { |
8bb9d875 |
59 | if (md->max_skew && age < -md->max_skew) { |
84f87e82 |
60 | sprintf(cbuf,"too much skew (%lds)",-age); |
61 | return cbuf; |
62 | } |
63 | } |
64 | |
65 | return 0; |
66 | } |
67 | |
68 | STANDARD_MECHANISMLIST("timestamp",timestamp); |