ReactOS  0.4.13-dev-563-g0561610
rtime.c File Reference
#include <wintirpc.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
Include dependency graph for rtime.c:

Go to the source code of this file.

Macros

#define NYEARS   (unsigned long)(1970 - 1900)
 
#define TOFFSET   (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4)))
 

Functions

int _rpc_dtablesize (void)
 
static void do_close (SOCKET)
 
int rtime (struct sockaddr_in *addrp, struct timeval *timep, struct timeval *timeout)
 
static void do_close (int s)
 

Macro Definition Documentation

◆ NYEARS

#define NYEARS   (unsigned long)(1970 - 1900)

Definition at line 59 of file rtime.c.

◆ TOFFSET

#define TOFFSET   (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4)))

Definition at line 60 of file rtime.c.

Function Documentation

◆ _rpc_dtablesize()

int _rpc_dtablesize ( void  )

Definition at line 48 of file rpc_dtablesize.c.

49 {
50 #ifdef _WIN32
51  return FD_SETSIZE;
52 #else
53  static int size;
54 
55  if (size == 0) {
56  size = getdtablesize();
57  if (size > FD_SETSIZE)
58  size = FD_SETSIZE;
59  }
60  return (size);
61 #endif
62 }
#define FD_SETSIZE
Definition: winsock.h:50
GLsizeiptr size
Definition: glext.h:5919

Referenced by rtime().

◆ do_close() [1/2]

static void do_close ( SOCKET  )
static

◆ do_close() [2/2]

static void do_close ( int  s)
static

Definition at line 150 of file rtime.c.

152 {
153  int save;
154 
155  save = errno;
156  (void)closesocket(s);
157  errno = save;
158 }
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
int errno
#define closesocket
Definition: main.c:39
GLdouble s
Definition: gl.h:2039

◆ rtime()

int rtime ( struct sockaddr_in addrp,
struct timeval timep,
struct timeval timeout 
)

Definition at line 69 of file rtime.c.

73 {
74  SOCKET s;
75  fd_set readfds;
76  int res;
77  unsigned long thetime;
78  struct sockaddr_in from;
79  int fromlen;
80  int type;
81  struct servent *serv;
82 
83  if (timeout == NULL) {
84  type = SOCK_STREAM;
85  } else {
86  type = SOCK_DGRAM;
87  }
88  s = socket(AF_INET, type, 0);
89  if (s == INVALID_SOCKET) {
90  return(-1);
91  }
92  addrp->sin_family = AF_INET;
93 
94  /* TCP and UDP port are the same in this case */
95  if ((serv = getservbyname("time", "tcp")) == NULL) {
96  return(-1);
97  }
98 
99  addrp->sin_port = serv->s_port;
100 
101  if (type == SOCK_DGRAM) {
102  res = sendto(s, (char *)&thetime, sizeof(thetime), 0,
103  (struct sockaddr *)addrp, sizeof(*addrp));
104  if (res == SOCKET_ERROR) {
105  do_close(s);
106  return(-1);
107  }
108  do {
109  FD_ZERO(&readfds);
110  FD_SET(s, &readfds);
111  res = select(_rpc_dtablesize(), &readfds,
112  (fd_set *)NULL, (fd_set *)NULL, timeout);
113  } while (res == SOCKET_ERROR && WSAGetLastError() == WSAEINTR);
114  if (res == 0 || res == SOCKET_ERROR) {
115  if (res == 0) {
117  }
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 == SOCKET_ERROR) {
126  return(-1);
127  }
128  } else {
129  if (connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) == SOCKET_ERROR) {
130  do_close(s);
131  return(-1);
132  }
133  res = recv(s, (char *)&thetime, sizeof(thetime), 0);
134  do_close(s);
135  if (res == SOCKET_ERROR) {
136  return(-1);
137  }
138  }
139  if (res != sizeof(thetime)) {
140  errno = EIO;
141  return(-1);
142  }
143  thetime = ntohl(thetime);
144  timep->tv_sec = thetime - TOFFSET;
145  timep->tv_usec = 0;
146  return(0);
147 }
Definition: winsock.h:66
#define SOCKET_ERROR
Definition: winsock.h:333
INT WSAAPI recvfrom(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags, OUT LPSOCKADDR from, IN OUT INT FAR *fromlen)
Definition: recv.c:87
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
u_short sin_port
Definition: winsock.h:511
unsigned long tv_sec
Definition: linux.h:1738
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
Definition: dhcpd.h:245
int errno
#define FD_ZERO(set)
Definition: winsock.h:96
#define FD_SET(fd, set)
Definition: winsock.h:89
INT WSAAPI connect(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: sockctrl.c:23
#define TOFFSET
Definition: rtime.c:60
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
static void do_close(SOCKET)
smooth NULL
Definition: ftsmooth.c:416
unsigned long tv_usec
Definition: linux.h:1739
#define WSAETIMEDOUT
Definition: winerror.h:1973
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
PSERVENT WSAAPI getservbyname(IN const char FAR *name, IN const char FAR *proto)
Definition: getxbyxx.c:500
#define INVALID_SOCKET
Definition: winsock.h:332
GLdouble s
Definition: gl.h:2039
short s_port
Definition: winsock.h:165
Definition: arc.h:40
INT WSAAPI sendto(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags, IN CONST struct sockaddr *to, IN INT tolen)
Definition: send.c:82
GLuint res
Definition: glext.h:9613
#define AF_INET
Definition: tcpip.h:117
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCK_STREAM
Definition: tcpip.h:118
CardRegion * from
Definition: spigame.cpp:19
#define SOCK_DGRAM
Definition: winsock.h:336
int _rpc_dtablesize(void)
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23
short sin_family
Definition: winsock.h:510
#define WSAEINTR
Definition: winerror.h:1942
#define ntohl(x)
Definition: module.h:203