OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [inet/] [rpc/] [rtime.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*
2
 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3
 * unrestricted use provided that this legend is included on all tape
4
 * media and as a part of the software program in whole or part.  Users
5
 * may copy or modify Sun RPC without charge, but are not authorized
6
 * to license or distribute it to anyone else except as part of a product or
7
 * program developed by the user.
8
 *
9
 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10
 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12
 *
13
 * Sun RPC is provided with no support and without any obligation on the
14
 * part of Sun Microsystems, Inc. to assist in its use, correction,
15
 * modification or enhancement.
16
 *
17
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19
 * OR ANY PART THEREOF.
20
 *
21
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22
 * or profits or other special, indirect and consequential damages, even if
23
 * Sun has been advised of the possibility of such damages.
24
 *
25
 * Sun Microsystems, Inc.
26
 * 2550 Garcia Avenue
27
 * Mountain View, California  94043
28
 */
29
#if 0
30
static char sccsid[] = "@(#)rtime.c     2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI";
31
#endif
32
 
33
/*
34
 * Copyright (c) 1988 by Sun Microsystems, Inc.
35
 */
36
/*
37
 * rtime - get time from remote machine
38
 *
39
 * gets time, obtaining value from host
40
 * on the udp/time socket.  Since timeserver returns
41
 * with time of day in seconds since Jan 1, 1900,  must
42
 * subtract seconds before Jan 1, 1970 to get
43
 * what unix uses.
44
 */
45
#define __FORCE_GLIBC
46
#include <features.h>
47
 
48
#include <stdio.h>
49
#include <unistd.h>
50
#include <rpc/rpc.h>
51
#include <rpc/clnt.h>
52
#include <sys/types.h>
53
#include <sys/poll.h>
54
#include <sys/socket.h>
55
#include <sys/time.h>
56
#include <rpc/auth_des.h>
57
#include <errno.h>
58
#include <netinet/in.h>
59
 
60
#define NYEARS  (u_long)(1970 - 1900)
61
#define TOFFSET (u_long)(60*60*24*(365*NYEARS + (NYEARS/4)))
62
 
63
static void do_close (int);
64
 
65
static void
66
do_close (int s)
67
{
68
  int save;
69
 
70
  save = errno;
71
  close (s);
72
  __set_errno (save);
73
}
74
 
75
int
76
rtime (struct sockaddr_in *addrp, struct rpc_timeval *timep,
77
       struct rpc_timeval *timeout)
78
{
79
  int s;
80
  struct pollfd fd;
81
  int milliseconds;
82
  int res;
83
  unsigned long thetime;
84
  struct sockaddr_in from;
85
  int fromlen;
86
  int type;
87
 
88
  if (timeout == NULL)
89
    type = SOCK_STREAM;
90
  else
91
    type = SOCK_DGRAM;
92
 
93
  s = socket (AF_INET, type, 0);
94
  if (s < 0)
95
    return (-1);
96
 
97
  addrp->sin_family = AF_INET;
98
  addrp->sin_port = htons (IPPORT_TIMESERVER);
99
  if (type == SOCK_DGRAM)
100
    {
101
      res = sendto (s, (char *) &thetime, sizeof (thetime), 0,
102
                    (struct sockaddr *) addrp, sizeof (*addrp));
103
      if (res < 0)
104
        {
105
          do_close (s);
106
          return -1;
107
        }
108
      milliseconds = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
109
      fd.fd = s;
110
      fd.events = POLLIN;
111
      do
112
        res = poll (&fd, 1, milliseconds);
113
      while (res < 0 && errno == EINTR);
114
      if (res <= 0)
115
        {
116
          if (res == 0)
117
            __set_errno (ETIMEDOUT);
118
          do_close (s);
119
          return (-1);
120
        }
121
      fromlen = sizeof (from);
122
      res = recvfrom (s, (char *) &thetime, sizeof (thetime), 0,
123
                      (struct sockaddr *) &from, &fromlen);
124
      do_close (s);
125
      if (res < 0)
126
        return -1;
127
    }
128
  else
129
    {
130
      if (connect (s, (struct sockaddr *) addrp, sizeof (*addrp)) < 0)
131
        {
132
          do_close (s);
133
          return -1;
134
        }
135
      res = read (s, (char *) &thetime, sizeof (thetime));
136
      do_close (s);
137
      if (res < 0)
138
        return (-1);
139
    }
140
  if (res != sizeof (thetime))
141
    {
142
      __set_errno (EIO);
143
      return -1;
144
    }
145
  thetime = ntohl (thetime);
146
  timep->tv_sec = thetime - TOFFSET;
147
  timep->tv_usec = 0;
148
  return 0;
149
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.