Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / sources / int / net / ip / timediff.c
1 #include "sock.h"
2
3 char m[100];
4 int sock;
5
6 char *host;
7
8
9 #ifndef NO_PROTOTYPES
10 int main(int argc,char **argv);
11 #endif
12
13 static int bytes_received=0;
14 static char title[100];
15
16
17 static void usage(s) char *s;{
18    printf("usage: %s host\n",s);
19    exit(0);
20 }
21
22
23 static int host_time( host )
24    char *host;
25 {
26    int size=0;
27    int time;
28    int sock = sock_open( SOCK_STREAM, "tcp", host, "time", 0, AS_CLIENT );
29    if( sock>=0 ){
30       size=sock_cli_recv(sock,&time,sizeof(int));
31       close( sock );
32    }
33    if( size == sizeof(int) ) return ntohl(time);
34    else return 0;
35 }
36
37
38 int main(argc,argv) int argc; char** argv; {
39
40    int uid=getuid();
41
42    if( argc != 2 )  usage(argv[0]);
43    host = argv[1];
44
45    printf("STARTED\n");
46
47    for(;;sleep(60*15)){
48
49       int local,remote,i;
50       long diff;
51       struct timeval delta,olddelta;
52       struct timeval time_before,time_after;
53
54       errno=0;
55
56       for( i=0; i<3; i++ ){
57
58          if( gettimeofday(&time_before,NULL) ){
59             perror("gettimeofday:");
60             if( uid!=0 )  return 1;
61             continue;
62          }
63
64          local = host_time( "localhost" );
65          remote= host_time( host        );
66
67          if( gettimeofday(&time_after ,NULL) ){
68             perror("gettimeofday:");
69             if( uid!=0 )  return 1;
70             continue;
71          }
72
73          diff  = (time_after.tv_sec - time_before.tv_sec)*1000;
74          diff -= time_before.tv_usec/1000;
75          diff += time_after .tv_usec/1000;
76          if( diff >=0  &&  diff < 300 )  break;
77
78       }
79
80       if( i==3 ){
81          printf("transaction too long ( %dms ) or errors in time\n",diff);
82          fflush(stdout);
83          fflush(stderr);
84          if( uid==0 )
85             continue;
86       }
87
88       if( remote==0 ){
89          printf("error in connect to %s\n",host);
90          fflush(stdout);
91          fflush(stderr);
92          if( uid!=0 )  return 1;
93          continue;
94       }
95       if( local==0 ){
96          printf("error in connect to %s\n","localhost");
97          fflush(stdout);
98          fflush(stderr);
99          if( uid!=0 )  return 1;
100          continue;
101       }
102
103       delta.tv_sec=remote-local;
104       delta.tv_usec=0;
105
106       if( delta.tv_sec!=0 ){
107          if( delta.tv_sec >  1 ) delta.tv_sec =  1;
108          else
109          if( delta.tv_sec < -1 ) delta.tv_sec = -1;
110          else{
111             if( delta.tv_sec == 1 ) delta.tv_usec =  500000;
112             else                    delta.tv_usec = -500000;
113             delta.tv_sec = 0;
114          }
115          if( uid==0 )
116          if( adjtime(&delta,&olddelta)<0 ) perror("adjtime error:");
117       }
118
119       {
120          extern char *ctime();
121          extern time_t time();
122          time_t t=time(NULL);
123          char *c=ctime(&t);
124          if( c[strlen(c)-1]=='\n' )  c[strlen(c)-1]='\0';
125          if( remote > local )
126             printf("at %s(%dms) time on %s is %u s forward\n",
127                    c,diff,host,remote-local);
128          if( remote < local )
129             printf("at %s(%dms) time on %s is %u s late\n",
130                    c,diff,host,local-remote);
131          if( uid != 0  &&  remote == local )
132             printf("at %s(%dms) time on %s is equal\n",c,diff,host);
133       }
134
135       fflush(stdout);
136       fflush(stderr);
137
138       if( uid!=0 )  return 0;
139
140    }
141
142    return 0;
143 }
144