extern int errno;
#endif
-\f
+/* Define sockaddr_storage where unavailable (presumably on IPv4-only
+ hosts). */
+
+#ifndef ENABLE_IPV6
+# ifndef HAVE_STRUCT_SOCKADDR_STORAGE
+# define sockaddr_storage sockaddr_in
+# endif
+#endif /* ENABLE_IPV6 */
+
/* Fill SA as per the data in IP and PORT. SA shoult point to struct
sockaddr_storage if ENABLE_IPV6 is defined, to struct sockaddr_in
otherwise. */
case IPV4_ADDRESS:
{
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
+ xzero (*sin);
sin->sin_family = AF_INET;
sin->sin_port = htons (port);
sin->sin_addr = ADDRESS_IPV4_IN_ADDR (ip);
case IPV6_ADDRESS:
{
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
+ xzero (*sin6);
sin6->sin6_family = AF_INET6;
sin6->sin6_port = htons (port);
sin6->sin6_addr = ADDRESS_IPV6_IN6_ADDR (ip);
{
struct sockaddr_storage ss;
struct sockaddr *sa = (struct sockaddr *)&ss;
- int sock = -1;
+ int sock;
/* If PRINT is non-NULL, print the "Connecting to..." line, with
PRINT being the host name we're connecting to. */
{
const char *txt_addr = pretty_print_address (ip);
if (print && 0 != strcmp (print, txt_addr))
- logprintf (LOG_VERBOSE,
- _("Connecting to %s|%s|:%d... "), print, txt_addr, port);
+ logprintf (LOG_VERBOSE, _("Connecting to %s|%s|:%d... "),
+ escnonprint (print), txt_addr, port);
else
logprintf (LOG_VERBOSE, _("Connecting to %s:%d... "), txt_addr, port);
}
setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, setopt_ptr, setopt_size);
#endif
-#ifdef ENABLE_IPV6
-# ifdef HAVE_IPV6_V6ONLY
- if (family == AF_INET6)
- setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, setopt_ptr, setopt_size);
-# endif
-#endif
-
xzero (ss);
sockaddr_set_data (sa, bind_address, *port);
if (bind (sock, sa, sockaddr_size (sa)) < 0)
/* If *PORT is 0, find out which port we've bound to. */
if (*port == 0)
{
- socklen_t sa_len = sockaddr_size (sa);
- if (getsockname (sock, sa, &sa_len) < 0)
+ socklen_t addrlen = sockaddr_size (sa);
+ if (getsockname (sock, sa, &addrlen) < 0)
{
/* If we can't find out the socket's local address ("name"),
something is seriously wrong with the socket, and it's
return select_fd (fd, timeout, wait_for);
}
+static int
+sock_peek (int fd, char *buf, int bufsize)
+{
+ int res;
+ do
+ res = recv (fd, buf, bufsize, MSG_PEEK);
+ while (res == -1 && errno == EINTR);
+ return res;
+}
+
static void
sock_close (int fd)
{
fd_reader_t reader;
fd_writer_t writer;
fd_poller_t poller;
+ fd_peeker_t peeker;
fd_closer_t closer;
void *ctx;
};
void
fd_register_transport (int fd, fd_reader_t reader, fd_writer_t writer,
- fd_poller_t poller, fd_closer_t closer, void *ctx)
+ fd_poller_t poller, fd_peeker_t peeker,
+ fd_closer_t closer, void *ctx)
{
struct transport_info *info;
info->reader = reader;
info->writer = writer;
info->poller = poller;
+ info->peeker = peeker;
info->closer = closer;
info->ctx = ctx;
if (!transport_map)
} \
} while (0)
-/* Read no more than BUFSIZE bytes of data from FD, storing them to
- BUF. If TIMEOUT is non-zero, the operation aborts if no data is
- received after that many seconds. If TIMEOUT is -1, the value of
- opt.timeout is used for TIMEOUT. */
-
-int
-fd_read (int fd, char *buf, int bufsize, double timeout)
+static int
+poll_internal (int fd, struct transport_info *info, int wf, double timeout)
{
- struct transport_info *info;
- LAZY_RETRIEVE_INFO (info);
if (timeout == -1)
timeout = opt.read_timeout;
if (timeout)
{
int test;
if (info && info->poller)
- test = info->poller (fd, timeout, WAIT_FOR_READ, info->ctx);
+ test = info->poller (fd, timeout, wf, info->ctx);
else
- test = sock_poll (fd, timeout, WAIT_FOR_READ);
+ test = sock_poll (fd, timeout, wf);
if (test == 0)
errno = ETIMEDOUT;
if (test <= 0)
- return -1;
+ return 0;
}
+ return 1;
+}
+
+/* Read no more than BUFSIZE bytes of data from FD, storing them to
+ BUF. If TIMEOUT is non-zero, the operation aborts if no data is
+ received after that many seconds. If TIMEOUT is -1, the value of
+ opt.timeout is used for TIMEOUT. */
+
+int
+fd_read (int fd, char *buf, int bufsize, double timeout)
+{
+ struct transport_info *info;
+ LAZY_RETRIEVE_INFO (info);
+ if (!poll_internal (fd, info, WAIT_FOR_READ, timeout))
+ return -1;
if (info && info->reader)
return info->reader (fd, buf, bufsize, info->ctx);
else
return sock_read (fd, buf, bufsize);
}
+/* The same as fd_read, but don't actually read the data, just find out
+ what's the number of bytes available for reading. */
+
+int
+fd_peek (int fd, char *buf, int bufsize, double timeout)
+{
+ struct transport_info *info;
+ LAZY_RETRIEVE_INFO (info);
+ if (!poll_internal (fd, info, WAIT_FOR_READ, timeout))
+ return -1;
+ if (info && info->peeker)
+ return info->peeker (fd, buf, bufsize, info->ctx);
+ else
+ return sock_peek (fd, buf, bufsize);
+}
+
/* Write the entire contents of BUF to FD. If TIMEOUT is non-zero,
the operation aborts if no data is received after that many
seconds. If TIMEOUT is -1, the value of opt.timeout is used for
int res;
struct transport_info *info;
LAZY_RETRIEVE_INFO (info);
- if (timeout == -1)
- timeout = opt.read_timeout;
/* `write' may write less than LEN bytes, thus the loop keeps trying
it until all was written, or an error occurred. */
res = 0;
while (bufsize > 0)
{
- if (timeout)
- {
- int test;
- if (info && info->poller)
- test = info->poller (fd, timeout, WAIT_FOR_WRITE, info->ctx);
- else
- test = sock_poll (fd, timeout, WAIT_FOR_WRITE);
- if (test == 0)
- errno = ETIMEDOUT;
- if (test <= 0)
- return -1;
- }
+ if (!poll_internal (fd, info, WAIT_FOR_WRITE, timeout))
+ return -1;
if (info && info->writer)
res = info->writer (fd, buf, bufsize, info->ctx);
else