#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include "ntp.h"
#include "ntp_io.h"
#include "ntp_net.h"
#include "ntp_debug.h"
#ifdef SYS_WINNT
#define ioctl(fd, opt, val) ioctlsocket(fd, opt, (u_long *)(val))
#endif
SOCKET
move_fd(
SOCKET fd
)
{
#if !defined(SYS_WINNT) && defined(F_DUPFD)
#ifndef FD_CHUNK
#define FD_CHUNK 10
#endif
#ifndef FOPEN_MAX
#define FOPEN_MAX 20
#endif
#ifndef FD_PREFERRED_SOCKBOUNDARY
#define FD_PREFERRED_SOCKBOUNDARY 48
#endif
static SOCKET socket_boundary = -1;
SOCKET newfd;
REQUIRE((int)fd >= 0);
if (socket_boundary == -1) {
socket_boundary = max(0, min(GETDTABLESIZE() - FD_CHUNK,
min(FOPEN_MAX, FD_PREFERRED_SOCKBOUNDARY)));
TRACE(1, ("move_fd: estimated max descriptors: %d, "
"initial socket boundary: %d\n",
GETDTABLESIZE(), socket_boundary));
}
do {
if (fd >= 0 && fd < socket_boundary) {
newfd = fcntl(fd, F_DUPFD, socket_boundary);
if (newfd != -1) {
close(fd);
return newfd;
}
} else {
return fd;
}
socket_boundary = max(0, socket_boundary - FD_CHUNK);
TRACE(1, ("move_fd: selecting new socket boundary: %d\n",
socket_boundary));
} while (socket_boundary > 0);
#else
ENSURE((int)fd >= 0);
#endif
return fd;
}
void
make_socket_nonblocking(
SOCKET fd
)
{
#ifdef USE_FIONBIO
#undef O_NONBLOCK
#undef FNDELAY
#undef O_NDELAY
#endif
#if defined(O_NONBLOCK)
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
msyslog(LOG_ERR,
"fcntl(O_NONBLOCK) fails on fd #%d: %m", fd);
exit(1);
}
#elif defined(FNDELAY)
if (fcntl(fd, F_SETFL, FNDELAY) < 0) {
msyslog(LOG_ERR, "fcntl(FNDELAY) fails on fd #%d: %m",
fd);
exit(1);
}
#elif defined(O_NDELAY)
if (fcntl(fd, F_SETFL, O_NDELAY) < 0) {
msyslog(LOG_ERR, "fcntl(O_NDELAY) fails on fd #%d: %m",
fd);
exit(1);
}
#elif defined(FIONBIO)
{
int on = 1;
if (ioctl(fd, FIONBIO, &on) < 0) {
msyslog(LOG_ERR,
"ioctl(FIONBIO) fails on fd #%d: %m",
fd);
exit(1);
}
}
#elif defined(FIOSNBIO)
if (ioctl(fd, FIOSNBIO, &on) < 0) {
msyslog(LOG_ERR,
"ioctl(FIOSNBIO) fails on fd #%d: %m", fd);
exit(1);
}
#else
# include "Bletch: Need non-blocking I/O!"
#endif
}
#if 0
static SOCKET
open_socket(
sockaddr_u * addr,
int bcast,
int turn_off_reuse,
endpt * interf
)
void
sendpkt(
sockaddr_u * dest,
struct interface * ep,
int ttl,
struct pkt * pkt,
int len
)
static inline int
read_refclock_packet(SOCKET fd, struct refclockio *rp, l_fp ts)
static inline int
read_network_packet(
SOCKET fd,
struct interface * itf,
l_fp ts
)
void
kill_asyncio(int startfd)
#endif