2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A/*
2N/A * Portions of this source code were derived from Berkeley
2N/A * 4.3 BSD under license from the Regents of the University of
2N/A * California.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * get time from remote machine
2N/A *
2N/A * gets time, obtaining value from host
2N/A * on the (udp, tcp)/time tli connection. Since timeserver returns
2N/A * with time of day in seconds since Jan 1, 1900, must
2N/A * subtract seconds before Jan 1, 1970 to get
2N/A * what unix uses.
2N/A */
2N/A#include "mt.h"
2N/A#include <rpc/rpc.h>
2N/A#include <errno.h>
2N/A#include <sys/poll.h>
2N/A#include <rpc/nettype.h>
2N/A#include <netdir.h>
2N/A#include <stdio.h>
2N/A
2N/Aextern int __rpc_timeval_to_msec();
2N/A
2N/A#ifdef DEBUG
2N/A#define debug(msg) t_error(msg)
2N/A#else
2N/A#define debug(msg)
2N/A#endif
2N/A
2N/A#define NYEARS (1970 - 1900)
2N/A#define TOFFSET ((uint_t)60*60*24*(365*NYEARS + (NYEARS/4)))
2N/A
2N/A/*
2N/A * This is based upon the internet time server, but it contacts it by
2N/A * using TLI instead of socket.
2N/A */
2N/Aint
2N/Artime_tli(char *host, struct timeval *timep, struct timeval *timeout)
2N/A{
2N/A uint32_t thetime;
2N/A int flag;
2N/A struct nd_addrlist *nlist = NULL;
2N/A struct nd_hostserv rpcbind_hs;
2N/A struct netconfig *nconf = NULL;
2N/A int foundit = 0;
2N/A int fd = -1;
2N/A
2N/A nconf = __rpc_getconfip(timeout == NULL ? "tcp" : "udp");
2N/A if (nconf == NULL)
2N/A goto error;
2N/A
2N/A if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
2N/A debug("open");
2N/A goto error;
2N/A }
2N/A if (t_bind(fd, NULL, NULL) < 0) {
2N/A debug("bind");
2N/A goto error;
2N/A }
2N/A
2N/A /* Get the address of the rpcbind */
2N/A rpcbind_hs.h_host = host;
2N/A rpcbind_hs.h_serv = "time";
2N/A /* Basically get the address of the remote machine on IP */
2N/A if (netdir_getbyname(nconf, &rpcbind_hs, &nlist))
2N/A goto error;
2N/A
2N/A if (nconf->nc_semantics == NC_TPI_CLTS) {
2N/A struct t_unitdata tu_data;
2N/A struct pollfd pfd;
2N/A int res;
2N/A int msec;
2N/A
2N/A tu_data.addr = *nlist->n_addrs;
2N/A tu_data.udata.buf = (char *)&thetime;
2N/A tu_data.udata.len = (uint_t)sizeof (thetime);
2N/A tu_data.udata.maxlen = tu_data.udata.len;
2N/A tu_data.opt.len = 0;
2N/A tu_data.opt.maxlen = 0;
2N/A if (t_sndudata(fd, &tu_data) == -1) {
2N/A debug("udp");
2N/A goto error;
2N/A }
2N/A pfd.fd = fd;
2N/A pfd.events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
2N/A
2N/A msec = __rpc_timeval_to_msec(timeout);
2N/A do {
2N/A res = poll(&pfd, 1, msec);
2N/A } while (res < 0);
2N/A if ((res <= 0) || (pfd.revents & POLLNVAL))
2N/A goto error;
2N/A if (t_rcvudata(fd, &tu_data, &flag) < 0) {
2N/A debug("udp");
2N/A goto error;
2N/A }
2N/A foundit = 1;
2N/A } else {
2N/A struct t_call sndcall;
2N/A
2N/A sndcall.addr = *nlist->n_addrs;
2N/A sndcall.opt.len = sndcall.opt.maxlen = 0;
2N/A sndcall.udata.len = sndcall.udata.maxlen = 0;
2N/A
2N/A if (t_connect(fd, &sndcall, NULL) == -1) {
2N/A debug("tcp");
2N/A goto error;
2N/A }
2N/A if (t_rcv(fd, (char *)&thetime, (uint_t)sizeof (thetime), &flag)
2N/A != (uint_t)sizeof (thetime)) {
2N/A debug("tcp");
2N/A goto error;
2N/A }
2N/A foundit = 1;
2N/A }
2N/A
2N/A thetime = ntohl(thetime);
2N/A timep->tv_sec = thetime - TOFFSET;
2N/A timep->tv_usec = 0;
2N/A
2N/Aerror:
2N/A if (nconf) {
2N/A (void) freenetconfigent(nconf);
2N/A if (fd != -1) {
2N/A (void) t_close(fd);
2N/A if (nlist)
2N/A netdir_free((char *)nlist, ND_ADDRLIST);
2N/A }
2N/A }
2N/A return (foundit);
2N/A}