* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
static char sccsid
[] = "@(#)cmds.c 2.2 (Berkeley) 4/21/86";
#include <netinet/in_systm.h>
#include <netinet/ip_icmp.h>
#include <protocols/timed.h>
char hostname
[MAXHOSTNAMELEN
];
struct hostent
*hp
, *gethostbyname();
struct sockaddr_in server
;
extern int measure_delta
;
int bytenetorder(), bytehostorder();
* Clockdiff computes the difference between the time of the machine on
* which it is called and the time of the machines given as argument.
* The time differences measured by clockdiff are obtained using a sequence
* of ICMP TSTAMP messages which are returned to the sender by the IP module
* In order to compare clocks of machines in different time zones, the time
* is transmitted (as a 32-bit value) in milliseconds since midnight UT.
* If a hosts uses a different time format, it should set the high order
* bit of the 32-bit quantity it transmits.
* However, VMS apparently transmits the time in milliseconds since midnight
* local time (rather than GMT) without setting the high order bit.
* Furthermore, it does not understand daylight-saving time. This makes
* clockdiff behaving inconsistently with hosts running VMS.
* In order to reduce the sensitivity to the variance of message transmission
* time, clockdiff sends a sequence of messages. Yet, measures between
* two `distant' hosts can be affected by a small error. The error can, however,
* be reduced by increasing the number of messages sent in each measurement.
printf("Usage: clockdiff host ... \n");
(void)gethostname(hostname
,sizeof(hostname
));
hp
= gethostbyname(*argv
);
printf("%s: unknown host\n", *argv
);
server
.sin_family
= hp
->h_addrtype
;
bcopy(hp
->h_addr
, &(server
.sin_addr
.s_addr
), hp
->h_length
);
if ((measure_status
= measure(&ack
, &server
)) < 0) {
switch (measure_status
) {
printf("%s is down\n", hp
->h_name
);
printf("%s time transmitted in a non-standard format\n", hp
->h_name
);
printf("%s is unreachable\n", hp
->h_name
);
printf("time on %s is %d ms. ahead of time on %s\n",
hp
->h_name
, measure_delta
,
printf("%s and %s have the same time\n",
printf("time on %s is %d ms. behind time on %s\n",
hp
->h_name
, -measure_delta
, hostname
);
* finds location of master timedaemon
printf("Usage: msite\n");
srvp
= getservbyname("timed", "udp");
fprintf(stderr
, "udp/timed: unknown service\n");
dest
.sin_port
= srvp
->s_port
;
dest
.sin_family
= AF_INET
;
(void)gethostname(hostname
,sizeof(hostname
));
hp
= gethostbyname(hostname
);
bcopy(hp
->h_addr
, &dest
.sin_addr
.s_addr
, hp
->h_length
);
(void)strcpy(msg
.tsp_name
, hostname
);
msg
.tsp_type
= TSP_MSITE
;
msg
.tsp_vers
= TSPVERSION
;
length
= sizeof(struct sockaddr_in
);
if (sendto(sock
, (char *)&msg
, sizeof(struct tsp
), 0,
if (select(FD_SETSIZE
, &ready
, (fd_set
*)0, (fd_set
*)0, &tout
)) {
length
= sizeof(struct sockaddr_in
);
cc
= recvfrom(sock
, (char *)&msg
, sizeof(struct tsp
), 0,
if (msg
.tsp_type
== TSP_ACK
)
printf("master timedaemon runs on %s\n", msg
.tsp_name
);
printf("received wrong ack: %s\n",
printf("communication error\n");
#define MAXH 4 /* max no. of hosts where election can occur */
* Causes the election timer to expire on the selected hosts
* It sends just one udp message per machine, relying on
* reliability of communication channel.
struct sockaddr_in sin
[MAXH
];
printf("Usage: testing host ...\n");
srvp
= getservbyname("timed", "udp");
fprintf(stderr
, "udp/timed: unknown service\n");
hp
= gethostbyname(*argv
);
printf("%s: unknown host %s\n", *argv
);
sin
[nhosts
].sin_port
= srvp
->s_port
;
sin
[nhosts
].sin_family
= hp
->h_addrtype
;
bcopy(hp
->h_addr
, &(sin
[nhosts
].sin_addr
.s_addr
), hp
->h_length
);
msg
.tsp_vers
= TSPVERSION
;
(void)gethostname(hostname
, sizeof(hostname
));
(void)strcpy(msg
.tsp_name
, hostname
);
bytenetorder(&msg
); /* it is not really necessary here */
length
= sizeof(struct sockaddr_in
);
if (sendto(sock
, (char *)&msg
, sizeof(struct tsp
), 0,
&sin
[nhosts
], length
) < 0) {
* Enables or disables tracing on local timedaemon
printf("Usage: tracing { on | off }\n");
srvp
= getservbyname("timed", "udp");
fprintf(stderr
, "udp/timed: unknown service\n");
dest
.sin_port
= srvp
->s_port
;
dest
.sin_family
= AF_INET
;
(void)gethostname(hostname
,sizeof(hostname
));
hp
= gethostbyname(hostname
);
bcopy(hp
->h_addr
, &dest
.sin_addr
.s_addr
, hp
->h_length
);
if (strcmp(argv
[1], "on") == 0) {
msg
.tsp_type
= TSP_TRACEON
;
msg
.tsp_type
= TSP_TRACEOFF
;
(void)strcpy(msg
.tsp_name
, hostname
);
msg
.tsp_vers
= TSPVERSION
;
length
= sizeof(struct sockaddr_in
);
if (sendto(sock
, (char *)&msg
, sizeof(struct tsp
), 0,
if (select(FD_SETSIZE
, &ready
, (fd_set
*)0, (fd_set
*)0, &tout
)) {
length
= sizeof(struct sockaddr_in
);
cc
= recvfrom(sock
, (char *)&msg
, sizeof(struct tsp
), 0,
if (msg
.tsp_type
== TSP_ACK
)
printf("timed tracing enabled\n");
printf("timed tracing disabled\n");
printf("wrong ack received: %s\n",
printf("communication error\n");
sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
perror("opening socket");
sin
.sin_family
= AF_INET
;
for (port
= IPPORT_RESERVED
- 1; port
> IPPORT_RESERVED
/ 2; port
--) {
sin
.sin_port
= htons((u_short
)port
);
if (bind(sock
, (struct sockaddr
*)&sin
, sizeof (sin
)) >= 0)
if (errno
!= EADDRINUSE
&& errno
!= EADDRNOTAVAIL
) {
if (port
== IPPORT_RESERVED
/ 2) {
fprintf(stderr
, "all reserved ports in use\n");
sock_raw
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
perror("opening raw socket");