summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorRuss Allbery <rra@stanford.edu>2009-10-12 16:30:18 -0700
committerRuss Allbery <rra@stanford.edu>2009-10-12 17:14:48 -0700
commitc482fa125958252effcfe1c6623d53e76baaca41 (patch)
tree4782e4923916e271d5009438ad8a25fd30f0ff24 /util
parent9f5acab7df461621ce1b5191a3668e154aed2d41 (diff)
Cleanups for Windows portability and build improvements
Use a socket_type typedef rather than a #define of SOCKET directly to preserve my coding style for defined types. Add documentation to portable/socket.h for proper use of the typedef. Remove unnecessary prototype changes for xwrite* and fdflag_*. These functions are not used on Windows. Minor code readability improvements in network_set_reuseaddr. Update the test suite to use socket_type and INVALID_SOCKET, and add the same wrapper around xwrite to the token test case. The test suite still doesn't run on Windows due to extensive use of fork, but may as well keep a consistent coding style. Add NEWS updates and copyright date changes.
Diffstat (limited to 'util')
-rw-r--r--util/gss-tokens.c9
-rw-r--r--util/messages.c4
-rw-r--r--util/network.c65
-rw-r--r--util/tokens.c8
-rw-r--r--util/util.h37
5 files changed, 64 insertions, 59 deletions
diff --git a/util/gss-tokens.c b/util/gss-tokens.c
index b707277..525314e 100644
--- a/util/gss-tokens.c
+++ b/util/gss-tokens.c
@@ -9,7 +9,7 @@
*
* Originally written by Anton Ushakov
* Extensive modifications by Russ Allbery <rra@stanford.edu>
- * Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008
+ * Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
* Board of Trustees, Leland Stanford Jr. University
*
* See README for licensing terms.
@@ -46,7 +46,7 @@ enum token_status token_recv(int, int *, gss_buffer_t, size_t);
* side to reply with a MIC, which we then verify.
*/
enum token_status
-token_send_priv(SOCKET fd, gss_ctx_id_t ctx, int flags, gss_buffer_t tok,
+token_send_priv(socket_type fd, gss_ctx_id_t ctx, int flags, gss_buffer_t tok,
OM_uint32 *major, OM_uint32 *minor)
{
gss_buffer_desc out, mic;
@@ -95,8 +95,9 @@ token_send_priv(SOCKET fd, gss_ctx_id_t ctx, int flags, gss_buffer_t tok,
* and send it back.
*/
enum token_status
-token_recv_priv(SOCKET fd, gss_ctx_id_t ctx, int *flags, gss_buffer_t tok,
- size_t max, OM_uint32 *major, OM_uint32 *minor)
+token_recv_priv(socket_type fd, gss_ctx_id_t ctx, int *flags,
+ gss_buffer_t tok, size_t max, OM_uint32 *major,
+ OM_uint32 *minor)
{
gss_buffer_desc in, mic;
int state;
diff --git a/util/messages.c b/util/messages.c
index 5dc4c5a..6f728a7 100644
--- a/util/messages.c
+++ b/util/messages.c
@@ -39,7 +39,7 @@
* the format and arguments), a format, an argument list as a va_list, and the
* applicable errno value (if any).
*
- * Copyright 2008 Board of Trustees, Leland Stanford Jr. University
+ * Copyright 2008, 2009 Board of Trustees, Leland Stanford Jr. University
* Copyright (c) 2004, 2005, 2006
* by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
@@ -182,7 +182,7 @@ message_log_syslog(int pri, int len, const char *fmt, va_list args, int err)
eventlog = RegisterEventSource(NULL, message_program_name);
if (eventlog != NULL) {
- ReportEvent(eventlog, (WORD)pri, 0, 0, NULL, 1, 0, &buffer, NULL);
+ ReportEvent(eventlog, (WORD) pri, 0, 0, NULL, 1, 0, &buffer, NULL);
CloseEventLog(eventlog);
}
}
diff --git a/util/network.c b/util/network.c
index 5c79be7..fd49b3a 100644
--- a/util/network.c
+++ b/util/network.c
@@ -10,6 +10,7 @@
* implementations for functions that aren't found on some pre-IPv6 systems.
* No other part of remctl should have to care about IPv4 vs. IPv6.
*
+ * Copyright 2009 Board of Trustees, Leland Stanford Jr. University
* Copyright (c) 2004, 2005, 2006, 2007, 2008
* by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
@@ -47,11 +48,12 @@
*/
#ifdef SO_REUSEADDR
static void
-network_set_reuseaddr(SOCKET fd)
+network_set_reuseaddr(socket_type fd)
{
int flag = 1;
+ const void *flagaddr = &flag;
- if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&flag, sizeof(flag)) < 0)
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, flagaddr, sizeof(flag)) < 0)
syswarn("cannot mark bind address reusable");
}
#endif
@@ -59,12 +61,12 @@ network_set_reuseaddr(SOCKET fd)
/*
* Create an IPv4 socket and bind it, returning the resulting file descriptor
- * (or -1 on a failure).
+ * (or INVALID_SOCKET on a failure).
*/
-SOCKET
+socket_type
network_bind_ipv4(const char *address, unsigned short port)
{
- SOCKET fd;
+ socket_type fd;
struct sockaddr_in server;
struct in_addr addr;
@@ -99,16 +101,16 @@ network_bind_ipv4(const char *address, unsigned short port)
/*
* Create an IPv6 socket and bind it, returning the resulting file descriptor
- * (or -1 on a failure). Note that we don't warn (but still return failure)
- * if the reason for the socket creation failure is that IPv6 isn't supported;
- * this is to handle systems like many Linux hosts where IPv6 is available in
- * userland but the kernel doesn't support it.
+ * (or INVALID_SOCKET on a failure). Note that we don't warn (but still
+ * return failure) if the reason for the socket creation failure is that IPv6
+ * isn't supported; this is to handle systems like many Linux hosts where IPv6
+ * is available in userland but the kernel doesn't support it.
*/
#if HAVE_INET6
-SOCKET
+socket_type
network_bind_ipv6(const char *address, unsigned short port)
{
- SOCKET fd;
+ socket_type fd;
struct sockaddr_in6 server;
struct in6_addr addr;
@@ -143,7 +145,7 @@ network_bind_ipv6(const char *address, unsigned short port)
return fd;
}
#else /* HAVE_INET6 */
-SOCKET
+socket_type
network_bind_ipv6(const char *address, unsigned short port)
{
warn("cannot bind %s,%hu: not built with IPv6 support", address, port);
@@ -165,7 +167,7 @@ network_bind_all(unsigned short port, int **fds, int *count)
{
struct addrinfo hints, *addrs, *addr;
int error, size;
- SOCKET fd;
+ socket_type fd;
char service[16], name[INET6_ADDRSTRLEN];
*count = 0;
@@ -199,7 +201,7 @@ network_bind_all(unsigned short port, int **fds, int *count)
if (fd != INVALID_SOCKET) {
if (*count >= size) {
size += 2;
- *fds = xrealloc(*fds, size * sizeof(SOCKET));
+ *fds = xrealloc(*fds, size * sizeof(socket_type));
}
(*fds)[*count] = fd;
(*count)++;
@@ -209,13 +211,13 @@ network_bind_all(unsigned short port, int **fds, int *count)
}
#else /* HAVE_INET6 */
void
-network_bind_all(unsigned short port, SOCKET **fds, int *count)
+network_bind_all(unsigned short port, socket_type **fds, int *count)
{
- SOCKET fd;
+ socket_type fd;
fd = network_bind_ipv4("0.0.0.0", port);
if (fd >= 0) {
- *fds = xmalloc(sizeof(SOCKET));
+ *fds = xmalloc(sizeof(socket_type));
*fds[0] = fd;
*count = 1;
} else {
@@ -232,7 +234,7 @@ network_bind_all(unsigned short port, SOCKET **fds, int *count)
* success and false on failure.
*/
static int
-network_source(SOCKET fd, int family, const char *source)
+network_source(socket_type fd, int family, const char *source)
{
if (source == NULL)
return 1;
@@ -269,13 +271,14 @@ network_source(SOCKET fd, int family, const char *source)
* Given a linked list of addrinfo structs representing the remote service,
* try to create a local socket and connect to that service. Takes an
* optional source address. Try each address in turn until one of them
- * connects. Returns the file descriptor of the open socket on success, or -1
- * on failure. Tries to leave the reason for the failure in errno.
+ * connects. Returns the file descriptor of the open socket on success, or
+ * INVALID_SOCKET on failure. Tries to leave the reason for the failure in
+ * errno.
*/
-SOCKET
+socket_type
network_connect(struct addrinfo *ai, const char *source)
{
- SOCKET fd = INVALID_SOCKET;
+ socket_type fd = INVALID_SOCKET;
int oerrno;
int success;
@@ -308,16 +311,16 @@ network_connect(struct addrinfo *ai, const char *source)
/*
* Like network_connect, but takes a host and a port instead of an addrinfo
* struct list. Returns the file descriptor of the open socket on success, or
- * -1 on failure. If getaddrinfo fails, errno may not be set to anything
- * useful.
+ * INVALID_SOCKET on failure. If getaddrinfo fails, errno may not be set to
+ * anything useful.
*/
-SOCKET
+socket_type
network_connect_host(const char *host, unsigned short port,
const char *source)
{
struct addrinfo hints, *ai;
char portbuf[16];
- SOCKET fd;
+ socket_type fd;
int oerrno;
memset(&hints, 0, sizeof(hints));
@@ -337,14 +340,14 @@ network_connect_host(const char *host, unsigned short port,
/*
* Create a new socket of the specified domain and type and do the binding as
* if we were a regular client socket, but then return before connecting.
- * Returns the file descriptor of the open socket on success, or -1 on
- * failure. Intended primarily for the use of clients that will then go on to
- * do a non-blocking connect.
+ * Returns the file descriptor of the open socket on success, or
+ * INVALID_SOCKET on failure. Intended primarily for the use of clients that
+ * will then go on to do a non-blocking connect.
*/
-SOCKET
+socket_type
network_client_create(int domain, int type, const char *source)
{
- SOCKET fd;
+ socket_type fd;
int oerrno;
fd = socket(domain, type, 0);
diff --git a/util/tokens.c b/util/tokens.c
index 2d10030..f7e2f35 100644
--- a/util/tokens.c
+++ b/util/tokens.c
@@ -7,7 +7,7 @@
*
* Originally written by Anton Ushakov
* Extensive modifications by Russ Allbery <rra@stanford.edu>
- * Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008
+ * Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
* Board of Trustees, Leland Stanford Jr. University
*
* See LICENSE for licensing terms.
@@ -38,7 +38,7 @@
* length, using multiple reads if needed and handling EINTR and EAGAIN.
*/
static ssize_t
-xread(SOCKET fd, void *buffer, size_t size)
+xread(socket_type fd, void *buffer, size_t size)
{
size_t total;
ssize_t status;
@@ -69,7 +69,7 @@ xread(SOCKET fd, void *buffer, size_t size)
* writes).
*/
enum token_status
-token_send(SOCKET fd, int flags, gss_buffer_t tok)
+token_send(socket_type fd, int flags, gss_buffer_t tok)
{
ssize_t status;
size_t buflen;
@@ -117,7 +117,7 @@ token_send(SOCKET fd, int flags, gss_buffer_t tok)
* free().
*/
enum token_status
-token_recv(SOCKET fd, int *flags, gss_buffer_t tok, size_t max)
+token_recv(socket_type fd, int *flags, gss_buffer_t tok, size_t max)
{
ssize_t status;
OM_uint32 len;
diff --git a/util/util.h b/util/util.h
index ff8a57b..807b78c 100644
--- a/util/util.h
+++ b/util/util.h
@@ -6,7 +6,7 @@
*
* Written by Russ Allbery <rra@stanford.edu>
* Based on prior work by Anton Ushakov
- * Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008
+ * Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
* Board of Trustees, Leland Stanford Jr. University
* Copyright (c) 2004, 2005, 2006, 2007
* by Internet Systems Consortium, Inc. ("ISC")
@@ -22,8 +22,8 @@
#include <config.h>
#include <portable/gssapi.h>
#include <portable/macros.h>
-#include <portable/stdbool.h>
#include <portable/socket.h>
+#include <portable/stdbool.h>
#include <stdarg.h>
#include <sys/types.h>
@@ -99,17 +99,18 @@ enum error_codes {
* token returned by token_recv; this will cause crashes on Windows. Call
* free on the value member instead.
*/
-enum token_status token_send(SOCKET fd, int flags, gss_buffer_t);
-enum token_status token_recv(SOCKET fd, int *flags, gss_buffer_t, size_t max);
+enum token_status token_send(socket_type, int flags, gss_buffer_t);
+enum token_status token_recv(socket_type, int *flags, gss_buffer_t,
+ size_t max);
/*
* The same, but with a GSS-API protection layer applied. On a GSS-API
* failure, the major and minor status are returned in the final two
* arguments.
*/
-enum token_status token_send_priv(SOCKET fd, gss_ctx_id_t, int flags,
+enum token_status token_send_priv(socket_type, gss_ctx_id_t, int flags,
gss_buffer_t, OM_uint32 *, OM_uint32 *);
-enum token_status token_recv_priv(SOCKET fd, gss_ctx_id_t, int *flags,
+enum token_status token_recv_priv(socket_type, gss_ctx_id_t, int *flags,
gss_buffer_t, size_t max, OM_uint32 *,
OM_uint32 *);
@@ -134,9 +135,9 @@ char *concatpath(const char *base, const char *name);
* the write is not making progress or there's a real error. Handle partial
* writes and EINTR/EAGAIN errors.
*/
-ssize_t xpwrite(SOCKET fd, const void *buffer, size_t size, off_t offset);
-ssize_t xwrite(SOCKET fd, const void *buffer, size_t size);
-ssize_t xwritev(SOCKET fd, const struct iovec *iov, int iovcnt);
+ssize_t xpwrite(int fd, const void *buffer, size_t size, off_t offset);
+ssize_t xwrite(int fd, const void *buffer, size_t size);
+ssize_t xwritev(int fd, const struct iovec *iov, int iovcnt);
/*
* Create a socket and bind it to the specified address and port (either IPv4
@@ -144,8 +145,8 @@ ssize_t xwritev(SOCKET fd, const struct iovec *iov, int iovcnt);
* are reported using warn/syswarn. To bind to all interfaces, use "any" or
* "all" for address.
*/
-SOCKET network_bind_ipv4(const char *address, unsigned short port);
-SOCKET network_bind_ipv6(const char *address, unsigned short port);
+socket_type network_bind_ipv4(const char *address, unsigned short port);
+socket_type network_bind_ipv6(const char *address, unsigned short port);
/*
* Create and bind sockets for every local address (normally two, one for IPv4
@@ -154,7 +155,7 @@ SOCKET network_bind_ipv6(const char *address, unsigned short port);
* fds will be set to an array containing the resulting file descriptors, with
* count holding the count returned.
*/
-void network_bind_all(unsigned short port, SOCKET **fds, int *count);
+void network_bind_all(unsigned short port, socket_type **fds, int *count);
/*
* Create a socket and connect it to the remote service given by the linked
@@ -162,14 +163,14 @@ void network_bind_all(unsigned short port, SOCKET **fds, int *count);
* -1 on failure, with the error left in errno. Takes an optional source
* address.
*/
-SOCKET network_connect(struct addrinfo *, const char *source);
+socket_type network_connect(struct addrinfo *, const char *source);
/*
* Like network_connect but takes a host and port instead. If host lookup
* fails, errno may not be set to anything useful.
*/
-SOCKET network_connect_host(const char *host, unsigned short port,
- const char *source);
+socket_type network_connect_host(const char *host, unsigned short port,
+ const char *source);
/*
* Creates a socket of the specified domain and type and binds it to the
@@ -181,7 +182,7 @@ SOCKET network_connect_host(const char *host, unsigned short port,
* This is a lower-level function intended primarily for the use of clients
* that will then go on to do a non-blocking connect.
*/
-SOCKET network_client_create(int domain, int type, const char *source);
+socket_type network_client_create(int domain, int type, const char *source);
/*
* Put an ASCII representation of the address in a sockaddr into the provided
@@ -206,8 +207,8 @@ unsigned short network_sockaddr_port(const struct sockaddr *);
bool network_addr_match(const char *, const char *, const char *mask);
/* Set a file descriptor close-on-exec or nonblocking. */
-bool fdflag_close_exec(SOCKET fd, bool flag);
-bool fdflag_nonblocking(SOCKET fd, bool flag);
+bool fdflag_close_exec(int fd, bool flag);
+bool fdflag_nonblocking(int fd, bool flag);
/*
* The reporting functions. The ones prefaced by "sys" add a colon, a space,