From 74d8b443aba528dc65af874a81c53be26aaace9e Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 15 Sep 2007 19:42:29 +0200 Subject: [PATCH] replace para_connect() by PARA_CONNECT. PARA_CONNECT is a macro which works for all three socket address types used in paraslash (sockaddr_in, sockaddr, sockaddr_un) and which does not need a length parameter. Change all instances of connect() and para_connect() to use PARA_CONNECT(). --- afs.c | 4 ++-- audioc.c | 6 +++--- audiod.c | 3 +-- client_common.c | 4 ++-- dccp_recv.c | 2 +- dccp_send.c | 2 +- http_recv.c | 6 ++---- net.c | 22 +--------------------- net.h | 22 +++++++++++++++++++++- 9 files changed, 34 insertions(+), 37 deletions(-) diff --git a/afs.c b/afs.c index 00aab24b..0efb6991 100644 --- a/afs.c +++ b/afs.c @@ -174,8 +174,8 @@ int send_callback_request(callback_function *f, struct osl_object *query, ret = init_unix_addr(&unix_addr, conf.afs_socket_arg); if (ret < 0) goto out; - ret = -E_CONNECT; - if (connect(fd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) < 0) /* FIXME: Use para_connect() */ + ret = PARA_CONNECT(fd, &unix_addr); + if (ret < 0) goto out; ret = send_bin_buffer(fd, buf, sizeof(buf)); if (ret < 0) diff --git a/audioc.c b/audioc.c index d98ab460..15faebf1 100644 --- a/audioc.c +++ b/audioc.c @@ -8,10 +8,10 @@ #include "audioc.cmdline.h" #include "para.h" +#include "error.h" #include "net.h" #include "string.h" #include "fd.h" -#include "error.h" INIT_AUDIOC_ERRLISTS; @@ -112,8 +112,8 @@ int main(int argc, char *argv[]) ret = -E_INIT_SOCK_ADDR; if (init_unix_addr(&unix_addr, socket_name) < 0) goto out; - ret = -E_AUDIOC_CONNECT; - if (connect(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX) < 0) /* FIXME: Use para_connect() */ + ret = PARA_CONNECT(fd, &unix_addr); + if (ret < 0) goto out; ret = send_cred_buffer(fd, args); if (ret < 0) diff --git a/audiod.c b/audiod.c index ccb23318..5e0ff8d4 100644 --- a/audiod.c +++ b/audiod.c @@ -7,7 +7,7 @@ /** \file audiod.c the paraslash's audio daemon */ #include "para.h" - +#include "error.h" #include "audiod.cmdline.h" #include "list.h" #include "sched.h" @@ -24,7 +24,6 @@ #include "fd.h" #include "write.h" #include "write_common.h" -#include "error.h" #include "signal.h" /** define the array of error lists needed by para_audiod */ diff --git a/client_common.c b/client_common.c index 5a555469..d950d2d7 100644 --- a/client_common.c +++ b/client_common.c @@ -7,6 +7,7 @@ /** \file client_common.c common functions of para_client and para_audiod */ #include "para.h" +#include "error.h" #include "list.h" #include "sched.h" #include "client.cmdline.h" @@ -17,7 +18,6 @@ #include "string.h" #include "client.cmdline.h" #include "client.h" -#include "error.h" /* * rc4 encrypt data before sending @@ -90,7 +90,7 @@ static int client_connect(struct private_client_data *pcd) pcd->fd = ret; /* init their_addr */ init_sockaddr(&their_addr, pcd->conf.server_port_arg, he); - ret = para_connect(pcd->fd, &their_addr); + ret = PARA_CONNECT(pcd->fd, &their_addr); if (ret < 0) goto err_out; pcd->status = CL_CONNECTED; diff --git a/dccp_recv.c b/dccp_recv.c index f1139683..2f946fc6 100644 --- a/dccp_recv.c +++ b/dccp_recv.c @@ -83,7 +83,7 @@ static int dccp_recv_open(struct receiver_node *rn) if (ret < 0) goto err_out; PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg, conf->port_arg); - ret = connect(pdd->fd, ai->ai_addr, ai->ai_addrlen); + ret = PARA_CONNECT(pdd->fd, ai->ai_addr); freeaddrinfo(ai); if (ret < 0) { ret = -E_DCCP_CONNECT; diff --git a/dccp_send.c b/dccp_send.c index 4394b33b..3509d5d8 100644 --- a/dccp_send.c +++ b/dccp_send.c @@ -12,6 +12,7 @@ */ #include "para.h" +#include "error.h" #include "afh.h" #include "server.h" #include "net.h" @@ -19,7 +20,6 @@ #include "vss.h" #include "send.h" #include "dccp.h" -#include "error.h" #include "string.h" #include "fd.h" #include "close_on_fork.h" diff --git a/http_recv.c b/http_recv.c index 05f452e6..f82924d9 100644 --- a/http_recv.c +++ b/http_recv.c @@ -7,13 +7,12 @@ /** \file http_recv.c paraslash's http receiver */ #include "para.h" - +#include "error.h" #include "http.h" #include "list.h" #include "sched.h" #include "recv.h" #include "http_recv.cmdline.h" -#include "error.h" #include "net.h" #include "string.h" #include "fd.h" @@ -181,10 +180,9 @@ static int http_recv_open(struct receiver_node *rn) phd->fd = ret; /* init their_addr */ init_sockaddr(&their_addr, conf->port_arg, he); - /* connect */ PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg, conf->port_arg); - ret = para_connect(phd->fd, &their_addr); + ret = PARA_CONNECT(phd->fd, &their_addr); if (ret < 0) { close(phd->fd); goto err_out; diff --git a/net.c b/net.c index 3dcd9c0b..221ad890 100644 --- a/net.c +++ b/net.c @@ -6,10 +6,10 @@ /** \file net.c networking-related helper functions */ +#include "error.h" #include "para.h" #include "net.h" #include "string.h" -#include "error.h" /** Information about one encrypted connection. */ @@ -302,26 +302,6 @@ int get_stream_socket(int domain) return socket_fd; } -/** - * a wrapper around connect(2) - * - * \param fd the file descriptor - * \param their_addr the address to connect - * - * \return \p -E_CONNECT on errors, 1 on success - * - * \sa connect(2) - */ -int para_connect(int fd, struct sockaddr_in *their_addr) -{ - int ret; - - if ((ret = connect(fd, (struct sockaddr *)their_addr, - sizeof(struct sockaddr))) == -1) - return -E_CONNECT; - return 1; -} - /** * paraslash's wrapper around the accept system call * diff --git a/net.h b/net.h index ddc25095..8fdaa3ee 100644 --- a/net.h +++ b/net.h @@ -24,7 +24,6 @@ typedef void crypt_function(unsigned long len, int get_host_info(char *host, struct hostent **ret); int get_stream_socket(int domain); void init_sockaddr(struct sockaddr_in*, int, const struct hostent*); -int para_connect(int fd, struct sockaddr_in *their_addr); int send_buffer(int, const char *); int send_bin_buffer(int, const char *, size_t); __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...); @@ -42,3 +41,24 @@ void enable_crypt(int fd, crypt_function *recv_f, crypt_function *send_f, void *private_data); void disable_crypt(int fd); +/** + * A wrapper around connect(2). + * + * \param fd The file descriptor. + * \param their_addr The address to connect. + * + * This should not be called directly. Always use the PARA_CONNECT macro. + * + * \return \p -E_CONNECT on errors, 1 on success. + * + * \sa connect(2), PARA_CONNECT. + */ +_static_inline_ int _para_connect(int fd, void *addr, socklen_t len) +{ + if (connect(fd, (struct sockaddr *)addr, len) == -1) + return -E_CONNECT; + return 1; +} + +/** A macro for connect() which does not need a \a len parameter. */ +#define PARA_CONNECT(fd, addr) _para_connect(fd, addr, sizeof(*(addr))) -- 2.39.5