home *** CD-ROM | disk | FTP | other *** search
- /* (C) Silvano Maffeis, maffeis@ifi.unizh.ch
- * University of Zurich, Switzerland 1992
- *
- * getrtime.c: read time from remote host using udp
- */
-
- #include <stdio.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/errno.h>
- #include <netinet/in.h>
-
- extern errno;
-
- void sclose(s)
- int s;
- {
- int save;
-
- save = errno;
- (void) close(s);
- errno = save;
- }
-
- getrtime(addrp, timep)
- struct sockaddr_in *addrp;
- struct timeval *timep;
- {
- int s;
- int res;
- char thetime[2*sizeof(long)];
- struct timeval *rtime = (struct timeval *)thetime;
- struct sockaddr_in from;
- int fromlen;
-
- s = socket(AF_INET, SOCK_DGRAM, 0);
- if (s < 0) {
- return(-1);
- }
- addrp->sin_family = AF_INET;
- res = sendto(s, (char *)thetime, 2*sizeof(long), 0,
- (struct sockaddr *)addrp, sizeof(*addrp));
- if (res < 0) {
- sclose(s);
- return(-1);
- }
- fromlen = sizeof(from);
- res = recvfrom(s, (char *)thetime, 2*sizeof(long), 0,
- (struct sockaddr *)&from, &fromlen);
- sclose(s);
- if (res < 0) {
- return(-1);
- }
- if (res != sizeof(thetime)) {
- errno = EIO;
- return(-1);
- }
- timep->tv_sec = ntohl(rtime->tv_sec);
- timep->tv_usec = ntohl(rtime->tv_usec);
- return(0);
- }
-
-