source: rtems-libbsd/services/librpc/src/rpc/rtime.c @ 20ec9e6

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 20ec9e6 was 20ec9e6, checked in by Joel Sherrill <joel.sherrill@…>, on 08/03/12 at 19:19:49

librpc: Initial addition

This does not currently compile. There is an include file
issue and int32_t is not defined even though stdint.h is included
before its use.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
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
30/*
31 * Copyright (c) 1988 by Sun Microsystems, Inc.
32
33 */
34
35/*
36 * rtime - get time from remote machine
37 *
38 * gets time, obtaining value from host
39 * on the udp/time socket.  Since timeserver returns
40 * with time of day in seconds since Jan 1, 1900,  must
41 * subtract seconds before Jan 1, 1970 to get
42 * what unix uses.
43 */
44
45#ifdef HAVE_CONFIG_H
46#include "config.h"
47#endif
48
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52#include <errno.h>
53#include <sys/types.h>
54#include <sys/socket.h>
55#include <sys/time.h>
56#include <netinet/in.h>
57#include <stdio.h>
58#include <netdb.h>
59#include <sys/select.h>
60#include <inttypes.h>
61
62#if defined(LIBC_SCCS) && !defined(lint)
63/* from: static char sccsid[] =         "@(#)rtime.c    2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI"; */
64static const char rcsid[] = "$FreeBSD: src/lib/libc/rpc/rtime.c,v 1.5 2000/01/27 23:06:41 jasone Exp $";
65#endif
66
67extern int _rpc_dtablesize( void );
68
69#define NYEARS  (UINT32_C(1970) - UINT32_C(1900))
70#define TOFFSET (UINT32_C(60)*UINT32_C(60)*UINT32_C(24)*(UINT32_C(365)*NYEARS + (NYEARS/UINT32_C(4))))
71
72static void do_close( int );
73
74int
75rtime(
76        struct sockaddr_in *addrp,
77        struct timeval *timep,
78        struct timeval *timeout )
79{
80        int s;
81        fd_set readfds;
82        int res;
83        uint32_t thetime;
84        struct sockaddr_in from;
85        socklen_t fromlen;
86        int type;
87        struct servent *serv;
88
89        if (timeout == NULL) {
90                type = SOCK_STREAM;
91        } else {
92                type = SOCK_DGRAM;
93        }
94        s = socket(AF_INET, type, 0);
95        if (s < 0) {
96                return(-1);
97        }
98        addrp->sin_family = AF_INET;
99
100        /* TCP and UDP port are the same in this case */
101        if ((serv = getservbyname("time", "tcp")) == NULL) {
102                return(-1);
103        }
104
105        addrp->sin_port = serv->s_port;
106
107        if (type == SOCK_DGRAM) {
108                res = sendto(s, (char *)&thetime, sizeof(thetime), 0,
109                             (struct sockaddr *)addrp, sizeof(*addrp));
110                if (res < 0) {
111                        do_close(s);
112                        return(-1);     
113                }
114                do {
115                        FD_ZERO(&readfds);
116                        FD_SET(s, &readfds);
117                        res = select(_rpc_dtablesize(), &readfds,
118                                     (fd_set *)NULL, (fd_set *)NULL, timeout);
119                } while (res < 0 && errno == EINTR);
120                if (res <= 0) {
121                        if (res == 0) {
122                                errno = ETIMEDOUT;
123                        }
124                        do_close(s);
125                        return(-1);     
126                }
127                fromlen = sizeof(from);
128                res = recvfrom(s, (char *)&thetime, sizeof(thetime), 0,
129                               (struct sockaddr *)&from, &fromlen);
130                do_close(s);
131                if (res < 0) {
132                        return(-1);     
133                }
134        } else {
135                if (connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) < 0) {
136                        do_close(s);
137                        return(-1);
138                }
139                res = _RPC_read(s, (char *)&thetime, sizeof(thetime));
140                do_close(s);
141                if (res < 0) {
142                        return(-1);
143                }
144        }
145        if (res != sizeof(thetime)) {
146                errno = EIO;
147                return(-1);     
148        }
149        thetime = ntohl(thetime);
150        timep->tv_sec = thetime - TOFFSET;
151        timep->tv_usec = 0;
152        return(0);
153}
154
155static void
156do_close(int s)
157{
158        int save;
159
160        save = errno;
161        (void)_RPC_close(s);
162        errno = save;
163}
Note: See TracBrowser for help on using the repository browser.