Blame sunrpc/rtime.c

Packit 6c4009
/*
Packit 6c4009
 * Copyright (c) 2010, Oracle America, Inc.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions are
Packit 6c4009
 * met:
Packit 6c4009
 *
Packit 6c4009
 *     * Redistributions of source code must retain the above copyright
Packit 6c4009
 *       notice, this list of conditions and the following disclaimer.
Packit 6c4009
 *     * Redistributions in binary form must reproduce the above
Packit 6c4009
 *       copyright notice, this list of conditions and the following
Packit 6c4009
 *       disclaimer in the documentation and/or other materials
Packit 6c4009
 *       provided with the distribution.
Packit 6c4009
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit 6c4009
 *       contributors may be used to endorse or promote products derived
Packit 6c4009
 *       from this software without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 6c4009
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 6c4009
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit 6c4009
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit 6c4009
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit 6c4009
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit 6c4009
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 6c4009
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit 6c4009
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 6c4009
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 6c4009
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
/*
Packit 6c4009
 * rtime - get time from remote machine
Packit 6c4009
 *
Packit 6c4009
 * gets time, obtaining value from host
Packit 6c4009
 * on the udp/time socket.  Since timeserver returns
Packit 6c4009
 * with time of day in seconds since Jan 1, 1900,  must
Packit 6c4009
 * subtract seconds before Jan 1, 1970 to get
Packit 6c4009
 * what unix uses.
Packit 6c4009
 */
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <rpc/rpc.h>
Packit 6c4009
#include <rpc/clnt.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/poll.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
#include <rpc/auth_des.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
#define NYEARS	(u_long)(1970 - 1900)
Packit 6c4009
#define TOFFSET (u_long)(60*60*24*(365*NYEARS + (NYEARS/4)))
Packit 6c4009
Packit 6c4009
static void do_close (int);
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_close (int s)
Packit 6c4009
{
Packit 6c4009
  int save;
Packit 6c4009
Packit 6c4009
  save = errno;
Packit 6c4009
  __close (s);
Packit 6c4009
  __set_errno (save);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
rtime (struct sockaddr_in *addrp, struct rpc_timeval *timep,
Packit 6c4009
       struct rpc_timeval *timeout)
Packit 6c4009
{
Packit 6c4009
  int s;
Packit 6c4009
  struct pollfd fd;
Packit 6c4009
  int milliseconds;
Packit 6c4009
  int res;
Packit 6c4009
  /* RFC 868 says the time is transmitted as a 32-bit value.  */
Packit 6c4009
  uint32_t thetime;
Packit 6c4009
  struct sockaddr_in from;
Packit 6c4009
  socklen_t fromlen;
Packit 6c4009
  int type;
Packit 6c4009
Packit 6c4009
  if (timeout == NULL)
Packit 6c4009
    type = SOCK_STREAM;
Packit 6c4009
  else
Packit 6c4009
    type = SOCK_DGRAM;
Packit 6c4009
Packit 6c4009
  s = __socket (AF_INET, type, 0);
Packit 6c4009
  if (s < 0)
Packit 6c4009
    return (-1);
Packit 6c4009
Packit 6c4009
  addrp->sin_family = AF_INET;
Packit 6c4009
  addrp->sin_port = htons (IPPORT_TIMESERVER);
Packit 6c4009
  if (type == SOCK_DGRAM)
Packit 6c4009
    {
Packit 6c4009
      res = __sendto (s, (char *) &thetime, sizeof (thetime), 0,
Packit 6c4009
		      (struct sockaddr *) addrp, sizeof (*addrp));
Packit 6c4009
      if (res < 0)
Packit 6c4009
	{
Packit 6c4009
	  do_close (s);
Packit 6c4009
	  return -1;
Packit 6c4009
	}
Packit 6c4009
      milliseconds = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
Packit 6c4009
      fd.fd = s;
Packit 6c4009
      fd.events = POLLIN;
Packit 6c4009
      do
Packit 6c4009
	res = __poll (&fd, 1, milliseconds);
Packit 6c4009
      while (res < 0 && errno == EINTR);
Packit 6c4009
      if (res <= 0)
Packit 6c4009
	{
Packit 6c4009
	  if (res == 0)
Packit 6c4009
	    __set_errno (ETIMEDOUT);
Packit 6c4009
	  do_close (s);
Packit 6c4009
	  return (-1);
Packit 6c4009
	}
Packit 6c4009
      fromlen = sizeof (from);
Packit 6c4009
      res = __recvfrom (s, (char *) &thetime, sizeof (thetime), 0,
Packit 6c4009
			(struct sockaddr *) &from, &fromlen);
Packit 6c4009
      do_close (s);
Packit 6c4009
      if (res < 0)
Packit 6c4009
	return -1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (__connect (s, (struct sockaddr *) addrp, sizeof (*addrp)) < 0)
Packit 6c4009
	{
Packit 6c4009
	  do_close (s);
Packit 6c4009
	  return -1;
Packit 6c4009
	}
Packit 6c4009
      res = __read (s, (char *) &thetime, sizeof (thetime));
Packit 6c4009
      do_close (s);
Packit 6c4009
      if (res < 0)
Packit 6c4009
	return (-1);
Packit 6c4009
    }
Packit 6c4009
  if (res != sizeof (thetime))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EIO);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
  thetime = ntohl (thetime);
Packit 6c4009
  timep->tv_sec = thetime - TOFFSET;
Packit 6c4009
  timep->tv_usec = 0;
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
libc_hidden_nolink_sunrpc (rtime, GLIBC_2_1)