Debian Codegraph @

libicmp/icmp_timestamp.c

raw
1 /*
2 Copyright (C) 2001-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 #include <config.h>
20
21 #include <stddef.h>
22
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/time.h>
26
27 #include <netinet/in_systm.h>
28 #include <netinet/in.h>
29 #include <netinet/ip.h>
30 /*#include <netinet/ip_icmp.h> -- deliberately not including this */
31 #include <arpa/inet.h>
32 #include <icmp.h>
33
34 int
35 icmp_timestamp_encode (unsigned char *buffer, size_t bufsize, int ident,
36 int seqno)
37 {
38 icmphdr_t *icmp;
39 struct timeval tv;
40 unsigned long v;
41
42 if (bufsize < ICMP_TSLEN)
43 return -1;
44
45 gettimeofday (&tv, NULL);
46 v = htonl ((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
47
48 icmp = (icmphdr_t *) buffer;
49 icmp->icmp_otime = v;
50 icmp->icmp_rtime = v;
51 icmp->icmp_ttime = v;
52 icmp_generic_encode (buffer, bufsize, ICMP_TIMESTAMP, ident, seqno);
53 return 0;
54 }