#include #include #include #include #include #include #include #include main(argc,argv) int argc; char *argv[]; { /* client process to connect to the time service via port 37. The client process uses the time message received to check (and optionally to set) the time of the local clock. the comparison assumes that the local clock keeps time in seconds from 1/1/70 which is the UNIX standard, and it computes the time difference by converting the received time from seconds since 1900.0 to seconds since 1/1/70. If the local machine keeps time in some other way, then the comparison method will have to change, but the rest should be okay. */ long int address; /* holds ip address */ int pserv = 37; /* time port number on server */ char *cp; /* server address in . notation */ struct sockaddr_in sin; /* socket address structure */ int s; /* socket number */ int length; /* size of message */ unsigned long int xxy; /* holds received time*/ struct timeval tvv; /* holds local time */ long int diff; /* time difference, local - PSB */ char cc; int stat; int isw(); /*parses command-line switches*/ char let; /*command-line letter*/ long int val; /*command line value*/ /* the following variables define what this program should do and what output should be produced. The values below are the defaults which may be changed by characters on the command line as described below. The effect of each variable is as follows: Command-Line Switch effect -m0 msg = 0 Do not produce any messages; only time difference is written to standard output. -m1 or -M = 1 Produce messages. -s0 set = 0 Do not adjust local time. -s1 or -S = 1 Adjust local time without asking -s2 = 2 Ask operator first. */ int msg =1; /*default is short messages */ int set =2; /*default is ask before setting clock */ /* parse command line */ while( sw(argc,argv,&let,&val) != 0) /*switch is present*/ { switch(let) { case 'm': msg=val; break; case 's': set=val; break; case 'S': set=1; break; case 'M': msg=1; break; default: fprintf(stderr,"\nSwitch %c not recognized.",let); break; } argc--; /*decrement argument counter */ argv++; /*and increment pointer */ } cp="203.117.180.35"; /* convert address to internal format */ if( (address=inet_addr(cp) ) == -1) { fprintf(stderr,"\n Internet address error."); exit(1); } bzero( (char *)&sin,sizeof(sin)); sin.sin_addr.s_addr=address; sin.sin_family=AF_INET; sin.sin_port=htons(pserv); /* create the socket and then connect to the server note that this is a stream socket and that record boundaries are not preserved. */ if( (s=socket(AF_INET,SOCK_STREAM,0) ) < 0) { fprintf(stderr,"\n Socket creation error."); exit(1); } if(connect(s, (struct sockaddr *) &sin, sizeof(sin) ) < 0) { perror(" time server Connect failed --"); exit(1); } length=read(s,&xxy,4); /*read 32 bit time value */ gettimeofday(&tvv,0); /*get time as soon as read completes*/ if(length <= 0) { perror(" No response from server "); close(s); exit(1); } xxy= ntohl(xxy); /*convert to local byte order*/ xxy -= 2208988800l; /*subtract 1970.0 - 1900.0*/ if(msg != 0) printf("\n Time value received = %lu",xxy); diff= tvv.tv_sec - xxy; if(tvv.tv_usec > 500000l) diff++; /*round up difference*/ if(msg != 0) printf("\n Local clock - SST(PSB)= %ld seconds.",diff); else printf("%ld",diff); close(s); if(diff == 0) { if(msg != 0) printf("\n No adjustment is needed.\n"); exit(); } if(set == 0) exit(); /*never adjust the time*/ if(set == 2) /*ask before doing anything*/ { printf("\n Do you want to adjust the local clock ? [y/n] "); cc=getchar(); } if( (set == 1) || (cc == 'y') || (cc == 'Y') ) { if(diff > 2100l) { if(msg != 0) printf("\n Adjustment limited to -2100 s."); diff= 2100l; } if(diff < -2100l) { if(msg != 0) printf("\n Adjustment limited to +2100 s."); diff= -2100l; } tvv.tv_sec= -diff; tvv.tv_usec = 0; stat=adjtime(&tvv,(struct timeval *) 0); if(stat == 0) printf("\n Adjustment was performed.\n"); else perror("Adjustment failed. "); } exit(); }