1998-07-06 10:56:06 +04:00
|
|
|
/* $NetBSD: cmds.c,v 1.9 1998/07/06 07:06:14 mrg Exp $ */
|
1997-10-17 12:49:29 +04:00
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1985, 1993 The Regents of the University of California.
|
1993-03-21 12:45:37 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-17 17:59:34 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-17 17:59:34 +04:00
|
|
|
#if 0
|
1997-10-17 12:49:29 +04:00
|
|
|
static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 3/26/95";
|
1997-10-17 17:59:34 +04:00
|
|
|
#else
|
1998-07-06 10:56:06 +04:00
|
|
|
__RCSID("$NetBSD: cmds.c,v 1.9 1998/07/06 07:06:14 mrg Exp $");
|
1997-10-17 17:59:34 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
#ifdef sgi
|
1998-07-06 10:56:06 +04:00
|
|
|
#ident "$Revision: 1.9 $"
|
1993-11-03 14:08:15 +03:00
|
|
|
#endif
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "timedc.h"
|
1993-11-03 14:08:15 +03:00
|
|
|
#include <sys/file.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
1993-11-03 14:08:15 +03:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
1997-10-18 11:05:31 +04:00
|
|
|
#include <string.h>
|
1993-11-03 14:08:15 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#define TSPTYPES
|
|
|
|
#include <protocols/timed.h>
|
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
#ifdef sgi
|
|
|
|
#include <bstring.h>
|
|
|
|
#include <sys/clock.h>
|
|
|
|
#else
|
|
|
|
#define SECHR (60*60)
|
|
|
|
#define SECDAY (24*SECHR)
|
|
|
|
#endif /* sgi */
|
|
|
|
|
|
|
|
# define DATE_PROTO "udp"
|
|
|
|
# define DATE_PORT "time"
|
|
|
|
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
int sock;
|
|
|
|
int sock_raw;
|
1998-07-06 10:56:06 +04:00
|
|
|
char myname[MAXHOSTNAMELEN + 1];
|
1993-11-03 14:08:15 +03:00
|
|
|
struct hostent *hp;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct sockaddr_in server;
|
1993-11-03 14:08:15 +03:00
|
|
|
struct sockaddr_in dayaddr;
|
1993-03-21 12:45:37 +03:00
|
|
|
extern int measure_delta;
|
1993-11-03 14:08:15 +03:00
|
|
|
|
|
|
|
void bytenetorder(struct tsp *);
|
|
|
|
void bytehostorder(struct tsp *);
|
|
|
|
|
|
|
|
|
1993-12-08 02:25:00 +03:00
|
|
|
#define BU ((unsigned long)2208988800U) /* seconds before UNIX epoch */
|
1993-11-03 14:08:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* compute the difference between our date and another machine
|
|
|
|
*/
|
|
|
|
static int /* difference in days from our time */
|
|
|
|
daydiff(char *hostname)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int trials;
|
|
|
|
struct timeval tout, now;
|
|
|
|
fd_set ready;
|
|
|
|
struct sockaddr from;
|
|
|
|
int fromlen;
|
|
|
|
unsigned long sec;
|
|
|
|
|
|
|
|
|
|
|
|
/* wait 2 seconds between 10 tries */
|
|
|
|
tout.tv_sec = 2;
|
|
|
|
tout.tv_usec = 0;
|
|
|
|
for (trials = 0; trials < 10; trials++) {
|
|
|
|
/* ask for the time */
|
|
|
|
sec = 0;
|
|
|
|
if (sendto(sock, &sec, sizeof(sec), 0,
|
|
|
|
(struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
|
|
|
|
perror("sendto(sock)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
FD_ZERO(&ready);
|
|
|
|
FD_SET(sock, &ready);
|
|
|
|
i = select(sock+1, &ready, (fd_set *)0,
|
|
|
|
(fd_set *)0, &tout);
|
|
|
|
if (i < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
perror("select(date read)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (0 == i)
|
|
|
|
break;
|
|
|
|
|
|
|
|
fromlen = sizeof(from);
|
|
|
|
if (recvfrom(sock,&sec,sizeof(sec),0,
|
|
|
|
&from,&fromlen) < 0) {
|
|
|
|
perror("recvfrom(date read)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sec = ntohl(sec);
|
|
|
|
if (sec < BU) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s says it is before 1970: %lu",
|
|
|
|
hostname, sec);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sec -= BU;
|
|
|
|
|
|
|
|
(void)gettimeofday(&now, (struct timezone*)0);
|
|
|
|
return (sec - now.tv_sec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we get here, we tried too many times */
|
|
|
|
fprintf(stderr,"%s will not tell us the date\n", hostname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
1993-11-03 14:08:15 +03:00
|
|
|
* Clockdiff computes the difference between the time of the machine on
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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 the remote machine.
|
1993-11-03 14:08:15 +03:00
|
|
|
* 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.
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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
|
1993-11-03 14:08:15 +03:00
|
|
|
* local time (rather than GMT) without setting the high order bit.
|
|
|
|
* Furthermore, it does not understand daylight-saving time. This makes
|
1993-03-21 12:45:37 +03:00
|
|
|
* clockdiff behaving inconsistently with hosts running VMS.
|
|
|
|
*
|
1993-11-03 14:08:15 +03:00
|
|
|
* 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.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1993-11-03 14:08:15 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
clockdiff(argc, argv)
|
1993-11-03 14:08:15 +03:00
|
|
|
int argc;
|
|
|
|
char *argv[];
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int measure_status;
|
1993-11-03 14:08:15 +03:00
|
|
|
extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
|
|
|
|
register int avg_cnt;
|
|
|
|
register long avg;
|
|
|
|
struct servent *sp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
if (argc < 2) {
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("Usage: clockdiff host ... \n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
(void)gethostname(myname,sizeof(myname));
|
1998-07-06 10:56:06 +04:00
|
|
|
myname[sizeof(myname) - 1] = '\0';
|
1993-11-03 14:08:15 +03:00
|
|
|
|
|
|
|
/* get the address for the date ready */
|
|
|
|
sp = getservbyname(DATE_PORT, DATE_PROTO);
|
|
|
|
if (!sp) {
|
|
|
|
(void)fprintf(stderr, "%s/%s is an unknown service\n",
|
|
|
|
DATE_PORT, DATE_PROTO);
|
|
|
|
dayaddr.sin_port = 0;
|
|
|
|
} else {
|
|
|
|
dayaddr.sin_port = sp->s_port;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
while (argc > 1) {
|
|
|
|
argc--; argv++;
|
|
|
|
hp = gethostbyname(*argv);
|
|
|
|
if (hp == NULL) {
|
1993-11-03 14:08:15 +03:00
|
|
|
fprintf(stderr, "timedc: %s: ", *argv);
|
|
|
|
herror(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
1993-11-03 14:08:15 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
server.sin_family = hp->h_addrtype;
|
1993-11-03 14:08:15 +03:00
|
|
|
bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
|
|
|
|
for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
|
|
|
|
measure_status = measure(10000,100, *argv, &server, 1);
|
|
|
|
if (measure_status != GOOD)
|
|
|
|
break;
|
|
|
|
avg += measure_delta;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1993-11-03 14:08:15 +03:00
|
|
|
if (measure_status == GOOD)
|
|
|
|
measure_delta = avg/avg_cnt;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
switch (measure_status) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case HOSTDOWN:
|
|
|
|
printf("%s is down\n", hp->h_name);
|
|
|
|
continue;
|
|
|
|
case NONSTDTIME:
|
1993-11-03 14:08:15 +03:00
|
|
|
printf("%s transmitts a non-standard time format\n",
|
|
|
|
hp->h_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
case UNREACHABLE:
|
|
|
|
printf("%s is unreachable\n", hp->h_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
/*
|
|
|
|
* Try to get the date only after using ICMP timestamps to
|
|
|
|
* get the time. This is because the date protocol
|
|
|
|
* is optional.
|
|
|
|
*/
|
|
|
|
if (dayaddr.sin_port != 0) {
|
|
|
|
dayaddr.sin_family = hp->h_addrtype;
|
|
|
|
bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
|
|
|
|
hp->h_length);
|
|
|
|
avg = daydiff(*argv);
|
|
|
|
if (avg > SECDAY) {
|
|
|
|
printf("time on %s is %ld days ahead %s\n",
|
|
|
|
hp->h_name, avg/SECDAY, myname);
|
|
|
|
continue;
|
|
|
|
} else if (avg < -SECDAY) {
|
|
|
|
printf("time on %s is %ld days behind %s\n",
|
|
|
|
hp->h_name, -avg/SECDAY, myname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (measure_delta > 0) {
|
|
|
|
printf("time on %s is %d ms. ahead of time on %s\n",
|
|
|
|
hp->h_name, measure_delta, myname);
|
|
|
|
} else if (measure_delta == 0) {
|
|
|
|
printf("%s and %s have the same time\n",
|
|
|
|
hp->h_name, myname);
|
|
|
|
} else {
|
|
|
|
printf("time on %s is %d ms. behind time on %s\n",
|
|
|
|
hp->h_name, -measure_delta, myname);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
1993-11-03 14:08:15 +03:00
|
|
|
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* finds location of master timedaemon
|
|
|
|
*/
|
1993-11-03 14:08:15 +03:00
|
|
|
void
|
|
|
|
msite(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int cc;
|
|
|
|
fd_set ready;
|
|
|
|
struct sockaddr_in dest;
|
1993-11-03 14:08:15 +03:00
|
|
|
int i, length;
|
|
|
|
struct sockaddr from;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct timeval tout;
|
|
|
|
struct tsp msg;
|
|
|
|
struct servent *srvp;
|
1993-11-03 14:08:15 +03:00
|
|
|
char *tgtname;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
if (argc < 1) {
|
|
|
|
printf("Usage: msite [hostname]\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
srvp = getservbyname("timed", "udp");
|
|
|
|
if (srvp == 0) {
|
|
|
|
fprintf(stderr, "udp/timed: unknown service\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dest.sin_port = srvp->s_port;
|
|
|
|
dest.sin_family = AF_INET;
|
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
(void)gethostname(myname, sizeof(myname));
|
|
|
|
i = 1;
|
|
|
|
do {
|
|
|
|
tgtname = (i >= argc) ? myname : argv[i];
|
|
|
|
hp = gethostbyname(tgtname);
|
|
|
|
if (hp == 0) {
|
|
|
|
fprintf(stderr, "timedc: %s: ", tgtname);
|
|
|
|
herror(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
|
|
|
|
|
1997-04-13 17:45:02 +04:00
|
|
|
(void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name) - 1);
|
1993-11-03 14:08:15 +03:00
|
|
|
msg.tsp_type = TSP_MSITE;
|
|
|
|
msg.tsp_vers = TSPVERSION;
|
|
|
|
bytenetorder(&msg);
|
|
|
|
if (sendto(sock, &msg, sizeof(struct tsp), 0,
|
|
|
|
(struct sockaddr*)&dest,
|
|
|
|
sizeof(struct sockaddr)) < 0) {
|
|
|
|
perror("sendto");
|
|
|
|
continue;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
tout.tv_sec = 15;
|
|
|
|
tout.tv_usec = 0;
|
|
|
|
FD_ZERO(&ready);
|
|
|
|
FD_SET(sock, &ready);
|
|
|
|
if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
|
|
|
|
&tout)) {
|
|
|
|
length = sizeof(struct sockaddr);
|
|
|
|
cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
|
|
|
|
&from, &length);
|
|
|
|
if (cc < 0) {
|
|
|
|
perror("recvfrom");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bytehostorder(&msg);
|
|
|
|
if (msg.tsp_type == TSP_ACK) {
|
|
|
|
printf("master timedaemon at %s is %s\n",
|
|
|
|
tgtname, msg.tsp_name);
|
|
|
|
} else {
|
|
|
|
printf("received wrong ack: %s\n",
|
|
|
|
tsptype[msg.tsp_type]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("communication error with %s\n", tgtname);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1993-11-03 14:08:15 +03:00
|
|
|
} while (++i < argc);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* quits timedc
|
|
|
|
*/
|
1993-11-03 14:08:15 +03:00
|
|
|
void
|
1997-10-17 17:59:34 +04:00
|
|
|
quit(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Causes the election timer to expire on the selected hosts
|
|
|
|
* It sends just one udp message per machine, relying on
|
|
|
|
* reliability of communication channel.
|
|
|
|
*/
|
1993-11-03 14:08:15 +03:00
|
|
|
void
|
|
|
|
testing(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct servent *srvp;
|
1993-11-03 14:08:15 +03:00
|
|
|
struct sockaddr_in sin;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct tsp msg;
|
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
if (argc < 2) {
|
|
|
|
printf("Usage: election host1 [host2 ...]\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
srvp = getservbyname("timed", "udp");
|
|
|
|
if (srvp == 0) {
|
|
|
|
fprintf(stderr, "udp/timed: unknown service\n");
|
|
|
|
return;
|
1993-11-03 14:08:15 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
while (argc > 1) {
|
|
|
|
argc--; argv++;
|
|
|
|
hp = gethostbyname(*argv);
|
|
|
|
if (hp == NULL) {
|
1993-11-03 14:08:15 +03:00
|
|
|
fprintf(stderr, "timedc: %s: ", *argv);
|
|
|
|
herror(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
argc--; argv++;
|
|
|
|
continue;
|
|
|
|
}
|
1993-11-03 14:08:15 +03:00
|
|
|
sin.sin_port = srvp->s_port;
|
|
|
|
sin.sin_family = hp->h_addrtype;
|
|
|
|
bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
|
|
|
|
|
|
|
|
msg.tsp_type = TSP_TEST;
|
|
|
|
msg.tsp_vers = TSPVERSION;
|
|
|
|
(void)gethostname(myname, sizeof(myname));
|
|
|
|
(void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
|
|
|
|
bytenetorder(&msg);
|
|
|
|
if (sendto(sock, &msg, sizeof(struct tsp), 0,
|
|
|
|
(struct sockaddr*)&sin,
|
|
|
|
sizeof(struct sockaddr)) < 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
perror("sendto");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Enables or disables tracing on local timedaemon
|
|
|
|
*/
|
1993-11-03 14:08:15 +03:00
|
|
|
void
|
|
|
|
tracing(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int onflag;
|
|
|
|
int length;
|
|
|
|
int cc;
|
|
|
|
fd_set ready;
|
|
|
|
struct sockaddr_in dest;
|
1993-11-03 14:08:15 +03:00
|
|
|
struct sockaddr from;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct timeval tout;
|
|
|
|
struct tsp msg;
|
|
|
|
struct servent *srvp;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
printf("Usage: tracing { on | off }\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
srvp = getservbyname("timed", "udp");
|
|
|
|
if (srvp == 0) {
|
|
|
|
fprintf(stderr, "udp/timed: unknown service\n");
|
|
|
|
return;
|
1993-11-03 14:08:15 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
dest.sin_port = srvp->s_port;
|
|
|
|
dest.sin_family = AF_INET;
|
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
(void)gethostname(myname,sizeof(myname));
|
|
|
|
hp = gethostbyname(myname);
|
1993-03-21 12:45:37 +03:00
|
|
|
bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "on") == 0) {
|
|
|
|
msg.tsp_type = TSP_TRACEON;
|
|
|
|
onflag = ON;
|
|
|
|
} else {
|
|
|
|
msg.tsp_type = TSP_TRACEOFF;
|
|
|
|
onflag = OFF;
|
|
|
|
}
|
|
|
|
|
1997-04-13 17:45:02 +04:00
|
|
|
(void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name) - 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
msg.tsp_vers = TSPVERSION;
|
|
|
|
bytenetorder(&msg);
|
1993-11-03 14:08:15 +03:00
|
|
|
if (sendto(sock, &msg, sizeof(struct tsp), 0,
|
|
|
|
(struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
perror("sendto");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tout.tv_sec = 5;
|
|
|
|
tout.tv_usec = 0;
|
|
|
|
FD_ZERO(&ready);
|
|
|
|
FD_SET(sock, &ready);
|
|
|
|
if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) {
|
1993-11-03 14:08:15 +03:00
|
|
|
length = sizeof(struct sockaddr);
|
|
|
|
cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
|
|
|
|
&from, &length);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (cc < 0) {
|
|
|
|
perror("recvfrom");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bytehostorder(&msg);
|
|
|
|
if (msg.tsp_type == TSP_ACK)
|
|
|
|
if (onflag)
|
|
|
|
printf("timed tracing enabled\n");
|
|
|
|
else
|
|
|
|
printf("timed tracing disabled\n");
|
|
|
|
else
|
1993-11-03 14:08:15 +03:00
|
|
|
printf("wrong ack received: %s\n",
|
1993-03-21 12:45:37 +03:00
|
|
|
tsptype[msg.tsp_type]);
|
|
|
|
} else
|
|
|
|
printf("communication error\n");
|
|
|
|
}
|
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
priv_resources()
|
|
|
|
{
|
|
|
|
int port;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sock < 0) {
|
|
|
|
perror("opening socket");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = 0;
|
|
|
|
for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
|
|
|
|
sin.sin_port = htons((u_short)port);
|
1993-11-03 14:08:15 +03:00
|
|
|
if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
|
|
|
|
perror("bind");
|
|
|
|
(void) close(sock);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (port == IPPORT_RESERVED / 2) {
|
|
|
|
fprintf(stderr, "all reserved ports in use\n");
|
|
|
|
(void) close(sock);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
1993-11-03 14:08:15 +03:00
|
|
|
sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (sock_raw < 0) {
|
|
|
|
perror("opening raw socket");
|
1993-11-03 14:08:15 +03:00
|
|
|
(void) close(sock);
|
1993-03-21 12:45:37 +03:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
return(1);
|
|
|
|
}
|