From: Alberto Leiva Popper Date: Tue, 30 Apr 2019 16:23:48 +0000 (-0500) Subject: Merge branch 'cleanup' into validator-rtr-fusion X-Git-Tag: v0.0.2~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=51ef03fbf7dc9bfe56d299988a65b7bfa80244fa;p=thirdparty%2FFORT-validator.git Merge branch 'cleanup' into validator-rtr-fusion --- 51ef03fbf7dc9bfe56d299988a65b7bfa80244fa diff --cc src/main.c index cc673259,77ce3aed..f3813e35 --- a/src/main.c +++ b/src/main.c @@@ -17,24 -16,18 +17,24 @@@ start_rtr_server(void error = vrps_init(); if (error) - goto end1; - - return error; ++ goto just_quit; error = clients_db_init(); if (error) - goto end2; - + goto revert_vrps; + error = slurm_load(); + if (error) - goto end3; ++ goto revert_clients; error = rtr_listen(); rtr_cleanup(); /* TODO shouldn't this only happen on !error? */ - end3: - clients_db_destroy(); + slurm_cleanup(); - end2: vrps_destroy(); - end1: return error; ++revert_clients: + clients_db_destroy(); + revert_vrps: + vrps_destroy(); ++just_quit: + return error; } int diff --cc src/notify.c index f7f8bea2,5db92afa..a57da60c --- a/src/notify.c +++ b/src/notify.c @@@ -6,32 -6,35 +6,35 @@@ #include "rtr/db/vrps.h" static int - send_notify(int fd, uint8_t rtr_version) + send_notify(struct client const *client, void *arg) { struct sender_common common; - uint32_t serial; + uint32_t *serial = arg; uint16_t session_id; + int error; + + /* Send Serial Notify PDU */ + session_id = get_current_session_id(client->rtr_version); + init_sender_common(&common, client->fd, client->rtr_version, + &session_id, serial, NULL); + error = send_serial_notify_pdu(&common); - serial = get_last_serial_number(); - session_id = get_current_session_id(rtr_version); - init_sender_common(&common, fd, rtr_version, &session_id, &serial, - NULL); - return send_serial_notify_pdu(&common); + /* Error? Log it */ + if (error) - warnx("Error sending notify PDU to client"); ++ pr_warn("Error sending notify PDU to client"); + + return 0; /* Do not interrupt notify to other clients */ } - void + int notify_clients(void) { - struct client *clients, *ptr; - size_t clients_len; + uint32_t serial; int error; - clients_len = client_list(&clients); - for (ptr = clients; (ptr - clients) < clients_len; ptr++) { - /* Send Serial Notify PDU */ - error = send_notify(ptr->fd, ptr->rtr_version); - /* Error? Log it */ - if (error) - pr_warn("Error sending notify PDU to client"); - } + error = get_last_serial_number(&serial); + if (error) + return error; + + return clients_foreach(send_notify, &serial); } diff --cc src/rtr/db/roa.c index 187dcb39,63940f33..15aefcc0 --- a/src/rtr/db/roa.c +++ b/src/rtr/db/roa.c @@@ -3,23 -3,10 +3,10 @@@ DEFINE_ARRAY_LIST_FUNCTIONS(v4_addresses, struct v4_address) DEFINE_ARRAY_LIST_FUNCTIONS(v6_addresses, struct v6_address) - static void - v4_address_destroy(struct v4_address *addr) - { - free(addr); - } - - static void - v6_address_destroy(struct v6_address *addr) - { - free(addr); - } - int -roa_create(u_int32_t as, struct roa **_result) +roa_create(uint32_t as, struct roa **_result) { struct roa *result; - int error; result = malloc(sizeof(struct roa)); if (result == NULL) diff --cc src/rtr/db/roa_tree.c index f51c12e5,7bd0a7b6..d29c01c7 --- a/src/rtr/db/roa_tree.c +++ b/src/rtr/db/roa_tree.c @@@ -26,17 -26,6 +26,17 @@@ struct roa_tree DEFINE_ARRAY_LIST_FUNCTIONS(nodes, struct node) +static void +node_init(struct node *node, struct rfc5280_name *subject_name, + struct node *parent) +{ + node->subject_name = subject_name; + x509_name_get(subject_name); + node->parent = parent; - node->children.array = NULL; ++ nodes_init(&node->children); + node->roa = NULL; +} + static struct node * node_create(struct rfc5280_name *subject_name, struct node *parent) { @@@ -51,35 -44,28 +51,26 @@@ } static void -node_destroy(struct node *node) +node_cleanup(struct node *node) { - x509_name_put(node->subject_name); - nodes_cleanup(&node->children, node_destroy); + if (node->subject_name != NULL) + x509_name_put(node->subject_name); - - if (node->children.array != NULL) - nodes_cleanup(&node->children, node_cleanup); - ++ nodes_cleanup(&node->children, node_cleanup); if (node->roa != NULL) roa_destroy(node->roa); - free(node); } static int node_add_child(struct node *parent, struct rfc5280_name *subject_name) { - struct node *child; + struct node child; int error; - if (parent->children.array == NULL) { - error = nodes_init(&parent->children); - if (error) - return error; - } - - child = node_create(subject_name, parent); - if (child == NULL) - return pr_enomem(); + node_init(&child, subject_name, parent); - error = nodes_add(&parent->children, child); + error = nodes_add(&parent->children, &child); if (error) - node_destroy(child); + node_cleanup(&child); return error; } @@@ -189,18 -174,17 +180,17 @@@ __foreach(struct node *node, vrp_foreac struct node *child; int error; - if (node->children.array != NULL) - ARRAYLIST_FOREACH(&node->children, child) { - error = __foreach(child, cb, arg); - if (error) - return error; - } + ARRAYLIST_FOREACH(&node->children, child) { + error = __foreach(child, cb, arg); + if (error) + return error; + } - if (child->roa != NULL) { - error = __foreach_v4(child->roa, cb, arg); + if (node->roa != NULL) { + error = __foreach_v4(node->roa, cb, arg); if (error) return error; - error = __foreach_v6(child->roa, cb, arg); + error = __foreach_v6(node->roa, cb, arg); if (error) return error; } diff --cc src/rtr/pdu_handler.c index f0c0320e,675ca163..6053a1a2 --- a/src/rtr/pdu_handler.c +++ b/src/rtr/pdu_handler.c @@@ -100,8 -109,12 +108,12 @@@ handle_serial_query_pdu(int fd, void *p return send_end_of_data_pdu(&common); } - warnx("Reached 'unreachable' code"); + pr_warn("Reached 'unreachable' code"); return -EINVAL; + + critical: + return err_pdu_send(fd, version, ERR_PDU_INTERNAL_ERROR, + &received->header, NULL); } int @@@ -134,8 -150,12 +149,12 @@@ handle_reset_query_pdu(int fd, void *pd break; } - warnx("Reached 'unreachable' code"); + pr_warn("Reached 'unreachable' code"); return -EINVAL; + + critical: + return err_pdu_send(fd, version, ERR_PDU_INTERNAL_ERROR, + &received->header, NULL); } int diff --cc src/rtr/rtr.c index 4a2f46e6,2839f872..e8077c93 --- a/src/rtr/rtr.c +++ b/src/rtr/rtr.c @@@ -48,10 -48,12 +48,12 @@@ init_addrinfo(struct addrinfo **result service = config_get_server_port(); error = getaddrinfo(hostname, service, &hints, result); - if (error) - return pr_err("Could not infer a bindable address out of address '%s' and port '%s': %s", + if (error) { - warnx("Could not infer a bindable address out of address '%s' and port '%s': %s", ++ pr_err("Could not infer a bindable address out of address '%s' and port '%s': %s", (hostname != NULL) ? hostname : "any", service, gai_strerror(error)); + return error; + } return 0; } @@@ -69,6 -71,6 +71,8 @@@ create_server_socket(int *result int fd; /* "file descriptor" */ int error; ++ *result = 0; /* Shuts up gcc */ ++ error = init_addrinfo(&addrs); if (error) return error; @@@ -258,16 -262,29 +263,29 @@@ handle_client_connections(int server_fd continue; } - arg.client_fd = client_fd; - arg.client_addr = client_addr; + arg = malloc(sizeof(struct thread_param)); + if (arg == NULL) { - warnx("Couldn't create thread_param struct"); ++ pr_err("Couldn't create thread_param struct"); + free(new_thread); + close(client_fd); + continue; + } + arg->client_fd = client_fd; + arg->client_addr = client_addr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); errno = pthread_create(&new_thread->tid, &attr, - client_thread_cb, &arg); + client_thread_cb, arg); pthread_attr_destroy(&attr); if (errno) { - warn("Could not spawn the client's thread"); + pr_errno(errno, "Could not spawn the client's thread"); + /* + * It is not clear to me whether @arg should be freed + * here. We're supposed to have transferred its + * ownership to the thread. + * Maybe we should store it in @new_thread instead. + */ free(new_thread); close(client_fd); continue;