/*
 * time - obtain the time from the remote server
 *
 * Copyright (C) 1992/93/94 Stephen Hebditch <steveh@tqmcomms.co.uk>.
 * All rights reserved. TQM Communications, BCM Box 225, London, WC1N 3XX.
 *
 * See README for more information and disclaimers
 *
 * Obtain the current time from the remote server in standard unix time
 * format for use with the next NEWNEWS. If the client is unable to
 * connect to the time server or the read fails then the error is
 * reported and the program is exited.
 *
 * $Id: time.c,v 1.9 1995/01/10 13:20:25 root Exp $
 *
 * $Log: time.c,v $
 * Revision 1.9  1995/01/10  13:20:25  root
 * Moved includes from slurp.h.
 * Return more meaningful time at server when in debug mode.
 * Largely pointless tidying-up.
 *
 * Revision 1.7  1993/06/07  11:20:27  root
 * Removed unistd.h inclusion.
 * Display time since epoch at server as unsigned.
 *
 * Revision 1.5  1993/03/01  18:09:12  root
 * Made the epoch constant an unsigned long.
 *
 * Revision 1.4  1993/02/14  15:10:01  root
 * No changes.
 *
 * Revision 1.0  1992/08/92
 * Initial coding.
 *
 */

#include "conf.h"

/* POSIX headers */

#undef _POSIX_SOURCE
#include <sys/types.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef NOUNISTD
	#include <unistd.h>
#endif

/* IP headers */

#include <netinet/in.h>

/* Local headers */

#include "slurp.h"


	time_t
server_time (const char *hostname)
	{
	int ret;
	int server;
	struct tm *timeptr;
	time_t unixtime;
	u_long inettime;

	/* Read in the time from the remote server */
	if ((server = tcp_open (hostname, "time", 0)) < 0)
		return ((time_t) 0);

	ret = read (server, &inettime, 4);

	(void) close (server);

	/* Check we got 4 bytes */
	if (ret != 4)
		{
		log_ret ("server_time: Read error on time server socket");
		return ((time_t) 0);
		}

	/* Convert byte order if needed */
	inettime = ntohl (inettime);

	/* Convert the time from seconds since 1900 to seconds since 1970 */
	unixtime = (time_t) (inettime - 2208988800UL);

	if (debug_flag)
		{
		timeptr = gmtime (&unixtime);
		(void) fprintf (stderr, "time at server %s is currently %s", hostname,
						asctime (timeptr));
		}

	return (unixtime);
	}

/* END-OF-FILE */


syntax highlighted by Code2HTML, v. 0.9.1