Skip to content

Commit

Permalink
Merge pull request #349 from Tarsnap/queue
Browse files Browse the repository at this point in the history
Use queue.h
  • Loading branch information
cperciva authored Apr 17, 2022
2 parents 779b4fa + 1a56c21 commit 88dcd94
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 63 deletions.
4 changes: 2 additions & 2 deletions spiped/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
PROG=spiped
MAN1=spiped.1
SRCS=main.c dispatch.c
IDIRS=-I../libcperciva/crypto -I../libcperciva/events -I../libcperciva/network -I../libcperciva/util -I../lib/dnsthread -I../lib/proto -I../lib/util
IDIRS=-I../libcperciva/crypto -I../libcperciva/events -I../libcperciva/external/queue -I../libcperciva/network -I../libcperciva/util -I../lib/dnsthread -I../lib/proto -I../lib/util
LDADD_REQ=-lcrypto -lpthread
SUBDIR_DEPTH=..
RELATIVE_DIR=spiped
Expand Down Expand Up @@ -41,5 +41,5 @@ ${PROG}:${SRCS:.c=.o} ${LIBALL}

main.o: main.c ../libcperciva/util/asprintf.h ../libcperciva/util/daemonize.h ../libcperciva/events/events.h ../libcperciva/util/getopt.h ../lib/util/graceful_shutdown.h ../libcperciva/util/parsenum.h ../libcperciva/util/setuidgid.h ../libcperciva/util/sock.h ../libcperciva/util/warnp.h dispatch.h ../lib/proto/proto_crypt.h ../libcperciva/crypto/crypto_dh.h
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -I.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c main.c -o main.o
dispatch.o: dispatch.c ../lib/dnsthread/dnsthread.h ../libcperciva/events/events.h ../libcperciva/network/network.h ../libcperciva/util/sock.h ../libcperciva/util/sock_util.h ../libcperciva/util/warnp.h ../lib/proto/proto_conn.h dispatch.h
dispatch.o: dispatch.c ../lib/dnsthread/dnsthread.h ../libcperciva/events/events.h ../libcperciva/network/network.h ../libcperciva/external/queue/queue.h ../libcperciva/util/sock.h ../libcperciva/util/sock_util.h ../libcperciva/util/warnp.h ../lib/proto/proto_conn.h dispatch.h
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -I.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c dispatch.c -o dispatch.o
1 change: 1 addition & 0 deletions spiped/Makefile.BSD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ SRCS += dispatch.c
# libcperciva includes
IDIRS += -I${LIBCPERCIVA_DIR}/crypto
IDIRS += -I${LIBCPERCIVA_DIR}/events
IDIRS += -I${LIBCPERCIVA_DIR}/external/queue
IDIRS += -I${LIBCPERCIVA_DIR}/network
IDIRS += -I${LIBCPERCIVA_DIR}/util

Expand Down
39 changes: 10 additions & 29 deletions spiped/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "dnsthread.h"
#include "events.h"
#include "network.h"
#include "queue.h"
#include "sock.h"
#include "sock_util.h"
#include "warnp.h"
Expand All @@ -30,15 +31,14 @@ struct accept_state {
double timeo;
void * accept_cookie;
void * dnstimer_cookie;
struct conn_list_node * conn_cookies;
LIST_HEAD(conn_head, conn_list_node) conn_cookies;
DNSTHREAD T;
};

/* Doubly linked list. */
struct conn_list_node {
void * conn_cookie;
struct conn_list_node * prev;
struct conn_list_node * next;
LIST_ENTRY(conn_list_node) entries;
struct accept_state * A;
};

Expand Down Expand Up @@ -120,24 +120,13 @@ callback_conndied(void * cookie, int reason)
(void)reason; /* UNUSED */

/* We should always have a non-empty list of conn_cookies. */
assert(A->conn_cookies != NULL);
assert(!LIST_EMPTY(&A->conn_cookies));

/* We've lost a connection. */
A->nconn -= 1;

/* Remove the closed connection from the list of conn_cookies. */
if (node_ptr == A->conn_cookies) {
/* Closed conn_cookie is first in the list. */
A->conn_cookies = node_ptr->next;
if (node_ptr->next != NULL)
node_ptr->next->prev = NULL;
} else {
/* Closed conn_cookie is in the middle of list. */
assert(node_ptr->prev != NULL);
node_ptr->prev->next = node_ptr->next;
if (node_ptr->next != NULL)
node_ptr->next->prev = node_ptr->prev;
}
LIST_REMOVE(node_ptr, entries);

/* Clean up the now-unused node. */
free(node_ptr);
Expand Down Expand Up @@ -177,8 +166,6 @@ callback_gotconn(void * cookie, int s)
/* Create new conn_list_node. */
if ((node_new = malloc(sizeof(struct conn_list_node))) == NULL)
goto err2;
node_new->prev = NULL;
node_new->next = NULL;
node_new->A = A;

/* Create a new connection. */
Expand All @@ -189,14 +176,8 @@ callback_gotconn(void * cookie, int s)
goto err3;
}

/* Link node_new to the beginning of the conn_cookies list. */
if (A->conn_cookies != NULL) {
node_new->next = A->conn_cookies;
node_new->next->prev = node_new;
}

/* Insert node_new to the beginning of the conn_cookies list. */
A->conn_cookies = node_new;
LIST_INSERT_HEAD(&A->conn_cookies, node_new, entries);

/* Accept another connection if we can. */
if (doaccept(A))
Expand Down Expand Up @@ -262,7 +243,7 @@ dispatch_accept(int s, const char * tgt, double rtime, struct sock_addr ** sas,
A->T = NULL;
A->accept_cookie = NULL;
A->dnstimer_cookie = NULL;
A->conn_cookies = NULL;
LIST_INIT(&A->conn_cookies);

/* If address re-resolution is enabled... */
if (rtime > 0.0) {
Expand Down Expand Up @@ -304,15 +285,15 @@ void
dispatch_shutdown(void * dispatch_cookie)
{
struct accept_state * A = dispatch_cookie;
struct conn_list_node * C;

/*
* Shutdown any open connections. proto_conn_drop() will call
* callback_conndied(), which removes the relevant conn_list_node from
* the list of conn_cookies.
*/
while (A->conn_cookies != NULL)
proto_conn_drop(A->conn_cookies->conn_cookie,
PROTO_CONN_CANCELLED);
while ((C = LIST_FIRST(&A->conn_cookies)) != NULL)
proto_conn_drop(C, PROTO_CONN_CANCELLED);

if (A->accept_cookie != NULL)
network_accept_cancel(A->accept_cookie);
Expand Down
4 changes: 2 additions & 2 deletions tests/nc-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# AUTOGENERATED FILE, DO NOT EDIT
PROG=nc-server
SRCS=main.c simple_server.c
IDIRS=-I../../libcperciva/events -I../../libcperciva/network -I../../libcperciva/util
IDIRS=-I../../libcperciva/events -I../../libcperciva/external/queue -I../../libcperciva/network -I../../libcperciva/util
SUBDIR_DEPTH=../..
RELATIVE_DIR=tests/nc-server
LIBALL=../../liball/liball.a
Expand All @@ -24,5 +24,5 @@ ${PROG}:${SRCS:.c=.o} ${LIBALL}

main.o: main.c ../../libcperciva/util/monoclock.h ../../libcperciva/util/parsenum.h ../../libcperciva/util/warnp.h simple_server.h
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -I../.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c main.c -o main.o
simple_server.o: simple_server.c ../../libcperciva/events/events.h ../../libcperciva/network/network.h ../../libcperciva/util/sock.h ../../libcperciva/util/warnp.h simple_server.h
simple_server.o: simple_server.c ../../libcperciva/events/events.h ../../libcperciva/network/network.h ../../libcperciva/external/queue/queue.h ../../libcperciva/util/sock.h ../../libcperciva/util/warnp.h simple_server.h
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -I../.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c simple_server.c -o simple_server.o
1 change: 1 addition & 0 deletions tests/nc-server/Makefile.BSD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ SRCS += simple_server.c

# libcperciva includes
IDIRS += -I${LIBCPERCIVA_DIR}/events
IDIRS += -I${LIBCPERCIVA_DIR}/external/queue
IDIRS += -I${LIBCPERCIVA_DIR}/network
IDIRS += -I${LIBCPERCIVA_DIR}/util

Expand Down
40 changes: 10 additions & 30 deletions tests/nc-server/simple_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "events.h"
#include "network.h"
#include "queue.h"
#include "sock.h"
#include "warnp.h"

Expand All @@ -20,16 +21,15 @@ struct accept_state {
size_t nconn;
size_t nconn_max;
void * accept_cookie;
struct conn_list_node * conn_cookies;
LIST_HEAD(conn_head, conn_list_node) conn_cookies;
void * caller_cookie;
int (* callback_nc_message)(void *, uint8_t *, size_t, int);
};

/* Doubly linked list. */
struct conn_list_node {
/* General "dispatch"-level info. */
struct conn_list_node * prev;
struct conn_list_node * next;
LIST_ENTRY(conn_list_node) entries;
struct accept_state * A;

/* Reading a network message. */
Expand Down Expand Up @@ -73,7 +73,7 @@ conndied(struct conn_list_node * node_ptr)
struct accept_state * A = node_ptr->A;

/* We should always have a non-empty list of conn_cookies. */
assert(A->conn_cookies != NULL);
assert(!LIST_EMPTY(&A->conn_cookies));

/* We've lost a connection. */
A->nconn -= 1;
Expand All @@ -86,18 +86,7 @@ conndied(struct conn_list_node * node_ptr)
}

/* Remove the closed connection from the list of conn_cookies. */
if (node_ptr == A->conn_cookies) {
/* Closed conn_cookie is first in the list. */
A->conn_cookies = node_ptr->next;
if (node_ptr->next != NULL)
node_ptr->next->prev = NULL;
} else {
/* Closed conn_cookie is in the middle of list. */
assert(node_ptr->prev != NULL);
node_ptr->prev->next = node_ptr->next;
if (node_ptr->next != NULL)
node_ptr->next->prev = node_ptr->prev;
}
LIST_REMOVE(node_ptr, entries);

/* Clean up the now-unused node. */
free(node_ptr);
Expand Down Expand Up @@ -134,8 +123,6 @@ callback_gotconn(void * cookie, int s)
warn0("Out of memory");
goto err1;
}
node_new->prev = NULL;
node_new->next = NULL;
node_new->A = A;
node_new->sock_read = s;

Expand All @@ -146,14 +133,8 @@ callback_gotconn(void * cookie, int s)
goto err2;
}

/* Link node_new to the beginning of the conn_cookies list. */
if (A->conn_cookies != NULL) {
node_new->next = A->conn_cookies;
node_new->next->prev = node_new;
}

/* Insert node_new to the beginning of the conn_cookies list. */
A->conn_cookies = node_new;
LIST_INSERT_HEAD(&A->conn_cookies, node_new, entries);

/* Accept another connection if we can. */
if (doaccept(A)) {
Expand Down Expand Up @@ -261,17 +242,16 @@ simple_server_shutdown(void * cookie)
* conndied(), which removes the relevant conn_list_node
* from the list of conn_cookies.
*/
while (A->conn_cookies != NULL) {
while ((node_ptr = LIST_FIRST(&A->conn_cookies)) != NULL) {
/* Remove nodes from the list. */
node_ptr = A->conn_cookies;
if (drop(A->conn_cookies))
if (drop(LIST_FIRST(&A->conn_cookies)))
warn0("drop");

/*
* Force the clang static analyzer to realize that
* the A->conn_cookies pointer changed.
*/
assert(node_ptr != A->conn_cookies);
assert(node_ptr != LIST_FIRST(&A->conn_cookies));
}

/* Close socket and free memory. */
Expand Down Expand Up @@ -321,9 +301,9 @@ simple_server(const char * addr, size_t nconn_max, size_t shutdown_after,
A->nconn = 0;
A->nconn_max = nconn_max;
A->accept_cookie = NULL;
A->conn_cookies = NULL;
A->callback_nc_message = callback_nc_message;
A->caller_cookie = caller_cookie;
LIST_INIT(&A->conn_cookies);

/* Accept a connection. */
if (doaccept(A)) {
Expand Down

0 comments on commit 88dcd94

Please sign in to comment.