The connection struct has grown a large range of fields that are
stored per-connection but are not associated with the
server/connections.c code. The protocol code freely accesses these
fields (because it was in the same file) but for other fields accessed
in other parts of nbdkit there were awkward accessor functions.
In this commit the structure is simply moved as a whole to
server/internal.h, thus allowing other parts of nbdkit free access.
In future it may be worth splitting this structure.
---
server/internal.h | 51 +++++++++++++++++++----------
server/connections.c | 77 +-------------------------------------------
server/crypto.c | 16 ++++-----
server/locks.c | 4 +--
4 files changed, 45 insertions(+), 103 deletions(-)
diff --git a/server/internal.h b/server/internal.h
index 506882b..8427401 100644
--- a/server/internal.h
+++ b/server/internal.h
@@ -1,5 +1,5 @@
/* nbdkit
- * Copyright (C) 2013-2018 Red Hat Inc.
+ * Copyright (C) 2013-2019 Red Hat Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -147,6 +147,7 @@ extern void cleanup_unlock (pthread_mutex_t **ptr);
/* connections.c */
struct connection;
+
typedef int (*connection_recv_function) (struct connection *,
void *buf, size_t len)
__attribute__((__nonnull__ (1, 2)));
@@ -155,28 +156,44 @@ typedef int (*connection_send_function) (struct connection *,
__attribute__((__nonnull__ (1, 2)));
typedef void (*connection_close_function) (struct connection *)
__attribute__((__nonnull__ (1)));
+
+struct connection {
+ pthread_mutex_t request_lock;
+ pthread_mutex_t read_lock;
+ pthread_mutex_t write_lock;
+ pthread_mutex_t status_lock;
+ int status; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
+ void *crypto_session;
+ int nworkers;
+
+ void **handles;
+ size_t nr_handles;
+
+ uint32_t cflags;
+ uint64_t exportsize;
+ uint16_t eflags;
+ bool readonly;
+ bool can_flush;
+ bool is_rotational;
+ bool can_trim;
+ bool can_zero;
+ bool can_fua;
+ bool can_multi_conn;
+ bool using_tls;
+ bool structured_replies;
+
+ int sockin, sockout;
+ connection_recv_function recv;
+ connection_send_function send;
+ connection_close_function close;
+};
+
extern int handle_single_connection (int sockin, int sockout);
extern int connection_set_handle (struct connection *conn,
size_t i, void *handle)
__attribute__((__nonnull__ (1 /* not 3 */)));
extern void *connection_get_handle (struct connection *conn, size_t i)
__attribute__((__nonnull__ (1)));
-extern pthread_mutex_t *connection_get_request_lock (struct connection *conn)
- __attribute__((__nonnull__ (1)));
-extern void connection_set_crypto_session (struct connection *conn,
- void *session)
- __attribute__((__nonnull__ (1 /* not 2 */)));
-extern void *connection_get_crypto_session (struct connection *conn)
- __attribute__((__nonnull__ (1)));
-extern void connection_set_recv (struct connection *,
- connection_recv_function)
- __attribute__((__nonnull__ (1, 2)));
-extern void connection_set_send (struct connection *,
- connection_send_function)
- __attribute__((__nonnull__ (1, 2)));
-extern void connection_set_close (struct connection *,
- connection_close_function)
- __attribute__((__nonnull__ (1, 2)));
/* crypto.c */
#define root_tls_certificates_dir sysconfdir "/pki/" PACKAGE_NAME
diff --git a/server/connections.c b/server/connections.c
index 7864602..7be282f 100644
--- a/server/connections.c
+++ b/server/connections.c
@@ -1,5 +1,5 @@
/* nbdkit
- * Copyright (C) 2013-2018 Red Hat Inc.
+ * Copyright (C) 2013-2019 Red Hat Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -62,38 +62,6 @@
/* Default number of parallel requests. */
#define DEFAULT_PARALLEL_REQUESTS 16
-/* Connection structure. */
-struct connection {
- pthread_mutex_t request_lock;
- pthread_mutex_t read_lock;
- pthread_mutex_t write_lock;
- pthread_mutex_t status_lock;
- int status; /* 1 for more I/O with client, 0 for shutdown, -1 on error */
- void *crypto_session;
- int nworkers;
-
- void **handles;
- size_t nr_handles;
-
- uint32_t cflags;
- uint64_t exportsize;
- uint16_t eflags;
- bool readonly;
- bool can_flush;
- bool is_rotational;
- bool can_trim;
- bool can_zero;
- bool can_fua;
- bool can_multi_conn;
- bool using_tls;
- bool structured_replies;
-
- int sockin, sockout;
- connection_recv_function recv;
- connection_send_function send;
- connection_close_function close;
-};
-
static struct connection *new_connection (int sockin, int sockout,
int nworkers);
static void free_connection (struct connection *conn);
@@ -105,9 +73,6 @@ static int raw_recv (struct connection *, void *buf, size_t len);
static int raw_send (struct connection *, const void *buf, size_t len);
static void raw_close (struct connection *);
-/* Accessors for public fields in the connection structure.
- * Everything else is private to this file.
- */
int
connection_set_handle (struct connection *conn, size_t i, void *handle)
{
@@ -141,46 +106,6 @@ connection_get_handle (struct connection *conn, size_t i)
return NULL;
}
-pthread_mutex_t *
-connection_get_request_lock (struct connection *conn)
-{
- return &conn->request_lock;
-}
-
-void
-connection_set_crypto_session (struct connection *conn, void *session)
-{
- conn->crypto_session = session;
-}
-
-void *
-connection_get_crypto_session (struct connection *conn)
-{
- return conn->crypto_session;
-}
-
-/* The code in crypto.c uses these three functions to replace the
- * recv, send and close callbacks when a connection is upgraded to
- * TLS.
- */
-void
-connection_set_recv (struct connection *conn, connection_recv_function recv)
-{
- conn->recv = recv;
-}
-
-void
-connection_set_send (struct connection *conn, connection_send_function send)
-{
- conn->send = send;
-}
-
-void
-connection_set_close (struct connection *conn, connection_close_function close)
-{
- conn->close = close;
-}
-
static int
get_status (struct connection *conn)
{
diff --git a/server/crypto.c b/server/crypto.c
index 4638a69..625b290 100644
--- a/server/crypto.c
+++ b/server/crypto.c
@@ -315,7 +315,7 @@ crypto_free (void)
static int
crypto_recv (struct connection *conn, void *vbuf, size_t len)
{
- gnutls_session_t *session = connection_get_crypto_session (conn);
+ gnutls_session_t *session = conn->crypto_session;
char *buf = vbuf;
ssize_t r;
bool first_read = true;
@@ -352,7 +352,7 @@ crypto_recv (struct connection *conn, void *vbuf, size_t len)
static int
crypto_send (struct connection *conn, const void *vbuf, size_t len)
{
- gnutls_session_t *session = connection_get_crypto_session (conn);
+ gnutls_session_t *session = conn->crypto_session;
const char *buf = vbuf;
ssize_t r;
@@ -378,7 +378,7 @@ crypto_send (struct connection *conn, const void *vbuf, size_t len)
static void
crypto_close (struct connection *conn)
{
- gnutls_session_t *session = connection_get_crypto_session (conn);
+ gnutls_session_t *session = conn->crypto_session;
int sockin, sockout;
assert (session != NULL);
@@ -394,7 +394,7 @@ crypto_close (struct connection *conn)
gnutls_deinit (*session);
free (session);
- connection_set_crypto_session (conn, NULL);
+ conn->crypto_session = NULL;
}
/* Upgrade an existing connection to TLS. Also this should do access
@@ -501,10 +501,10 @@ crypto_negotiate_tls (struct connection *conn, int sockin, int
sockout)
/* Set up the connection recv/send/close functions so they call
* GnuTLS wrappers instead.
*/
- connection_set_crypto_session (conn, session);
- connection_set_recv (conn, crypto_recv);
- connection_set_send (conn, crypto_send);
- connection_set_close (conn, crypto_close);
+ conn->crypto_session = session;
+ conn->recv = crypto_recv;
+ conn->send = crypto_send;
+ conn->close = crypto_close;
return 0;
error:
diff --git a/server/locks.c b/server/locks.c
index 1724b5a..347c13a 100644
--- a/server/locks.c
+++ b/server/locks.c
@@ -74,7 +74,7 @@ lock_request (struct connection *conn)
pthread_mutex_lock (&all_requests_lock);
if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS)
- pthread_mutex_lock (connection_get_request_lock (conn));
+ pthread_mutex_lock (&conn->request_lock);
pthread_rwlock_rdlock (&unload_prevention_lock);
}
@@ -85,7 +85,7 @@ unlock_request (struct connection *conn)
pthread_rwlock_unlock (&unload_prevention_lock);
if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS)
- pthread_mutex_unlock (connection_get_request_lock (conn));
+ pthread_mutex_unlock (&conn->request_lock);
if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS)
pthread_mutex_unlock (&all_requests_lock);
--
2.20.1