]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Use warn(x) to log, don't start server without conf file
authorpcarana <pc.moreno2099@gmail.com>
Fri, 22 Mar 2019 18:07:04 +0000 (12:07 -0600)
committerpcarana <pc.moreno2099@gmail.com>
Fri, 22 Mar 2019 18:07:04 +0000 (12:07 -0600)
13 files changed:
src/address.c
src/array_list.h
src/clients.c
src/configuration.c
src/csv.c
src/line_file.c
src/notify.c
src/rtr/err_pdu.c
src/rtr/pdu_handler.c
src/rtr/pdu_sender.c
src/rtr/rtr.c
src/updates_daemon.c
src/vrps.c

index 63639d132e894dc5084c1fc7dadc55dc0c8e2769..de8b9fdff8e53cdf72d988f8f27a8e435ea0aa08 100644 (file)
@@ -103,16 +103,6 @@ prefix6_decode(const char *str, struct ipv6_prefix *result)
 {
        int error;
 
-       /*
-        * TODO (review) I see this pattern often: You call `err()`, and then
-        * throw a pretty exception.
-        *
-        * `err()` does not return, so any cleanup that follows it is pointless.
-        *
-        * More importantly, returnless functions are bad practice,
-        * because they destroy the recovery potential of calling code.
-        * Use `warn()`/`warnx()` instead (and continue throwing cleanly).
-        */
        if (str == NULL) {
                warnx("Null string received, can't decode IPv6 prefix");
                return -EINVAL;
@@ -132,15 +122,6 @@ prefix_length_decode (const char *text, unsigned int *dst, int max_value)
 {
        unsigned long len;
 
-       /*
-        * TODO (review) You probably meant to use `errx()`, not `err()`.
-        *
-        * `err()` depends on a thread variable called `errno`. It makes no
-        * sense to call `err` when you know `errno` has not been set properly
-        * by some system function.
-        *
-        * If you haven't done it, see `man 3 err` and `man 3 errno`.
-        */
        if (text == NULL) {
                warnx("Null string received, can't decode prefix length");
                return -EINVAL;
@@ -149,13 +130,12 @@ prefix_length_decode (const char *text, unsigned int *dst, int max_value)
        errno = 0;
        len = strtoul(text, NULL, 10);
        if (errno) {
-               warn("Invalid prefix length '%s': %s", text,
-                   strerror(errno));
+               warn("Invalid prefix length '%s'", text);
                return -EINVAL;
        }
        /* An underflow or overflow will be considered here */
        if (len < 0 || max_value < len) {
-               warnx("Prefix length (%ld) is out of bounds (0-%d).",
+               warnx("Prefix length (%ld) is out of range (0-%d).",
                    len, max_value);
                return -EINVAL;
        }
@@ -169,7 +149,7 @@ prefix4_validate (struct ipv4_prefix *prefix)
        char buffer[INET_ADDRSTRLEN];
 
        if ((prefix->addr.s_addr & be32_suffix_mask(prefix->len)) != 0) {
-               warn("IPv4 prefix %s/%u has enabled suffix bits.",
+               warnx("IPv4 prefix %s/%u has enabled suffix bits.",
                    addr2str4(&prefix->addr, buffer), prefix->len);
                return -EINVAL;
        }
@@ -188,7 +168,7 @@ prefix6_validate (struct ipv6_prefix *prefix)
            || (prefix->addr.s6_addr32[1] & suffix.s6_addr32[1])
            || (prefix->addr.s6_addr32[2] & suffix.s6_addr32[2])
            || (prefix->addr.s6_addr32[3] & suffix.s6_addr32[3])) {
-               warn("IPv6 prefix %s/%u has enabled suffix bits.",
+               warnx("IPv6 prefix %s/%u has enabled suffix bits.",
                    addr2str6(&prefix->addr, buffer), prefix->len);
                return -EINVAL;
        }
index 6232a6de546237595b0b4ae52cb0576d0b03009d..882fb52f6ae906e149e516e1b27b0a9af04da56a 100644 (file)
@@ -47,8 +47,8 @@
                        tmp = realloc(list->array, list->capacity       \
                            * sizeof(elem_type));                       \
                        if (tmp == NULL) {                              \
-                               err(-ENOMEM, "Out of memory");          \
-                               return -ENOMEM;                         \
+                               warn("Couldn't realloc array");         \
+                               return errno;                           \
                        }                                               \
                        list->array = tmp;                              \
                }                                                       \
index 28316e4fa8b2fe1e3f87d0f0ac37c6029c38f75f..c0fc6fce84734b8950bcd5f94a8e25fae27b5cde 100644 (file)
@@ -16,7 +16,7 @@ clients_db_init(void)
 
        error = clientsdb_init(&clients_db);
        if (error)
-               err(error, "Clients DB couldn't be initialized");
+               warnx( "Clients DB couldn't be initialized");
 
        return error;
 }
index 21748471338b887d728a62549ed74218b77ae232..aa82144225b38b41f47a85bf43c994d4282b5a6c 100644 (file)
@@ -74,12 +74,8 @@ config_init(char const *json_file_path)
        json_error_t json_error;
        int error;
 
-       /*
-        * TODO What's the point of a default start if there's
-        * no vrps input?
-        */
        if (json_file_path == NULL)
-               return init_addrinfo(DEFAULT_ADDR, DEFAULT_PORT);
+               return -EINVAL;
 
        json_root = json_load_file(json_file_path, JSON_REJECT_DUPLICATES,
            &json_error);
@@ -114,13 +110,13 @@ load_range(json_t *parent, char const *name, int default_value,
 
        error = json_get_int(parent, name, default_value, result);
        if (error) {
-               err(error, "Invalid value for '%s'", name);
+               warnx("Invalid value for '%s'", name);
                return error;
        }
 
        if (*result < min_value || max_value < *result) {
-               err(-EINVAL, "'%s' (%d) out of range, must be from %d to %d",
-                   name, *result, min_value, max_value);
+               warnx("'%s' (%d) out of range, must be from %d to %d", name,
+                   *result, min_value, max_value);
                return -EINVAL;
        }
 
@@ -194,8 +190,8 @@ handle_json(json_t *root)
 
                config.vrps_location = strdup(vrps_location);
                if (config.vrps_location == NULL) {
-                       err(errno, "'%s' couldn't be allocated.",
-                                       OPTNAME_VRPS_LOCATION);
+                       warn("'%s' couldn't be allocated.",
+                           OPTNAME_VRPS_LOCATION);
                        return errno;
                }
 
@@ -318,10 +314,9 @@ init_addrinfo(char const *hostname, char const *service)
                return error;
        }
 
-       /* TODO (review) check NULL */
        config.port = strdup(service);
        if (config.port == NULL) {
-               err(errno, "'%s' couldn't be allocated.", OPTNAME_LISTEN_PORT);
+               warn( "'%s' couldn't be allocated.", OPTNAME_LISTEN_PORT);
                return errno;
        }
 
index e464476bef2e238fdcd7d9c678bf3fc2de85aaac..ea89100eab4e6b1756e15f1ba5244c44715d9072 100644 (file)
--- a/src/csv.c
+++ b/src/csv.c
@@ -39,7 +39,7 @@ parse_asn(char *text, unsigned int *value)
        }
        /* An underflow or overflow will be considered here */
        if (asn < 0 || UINT32_MAX < asn) {
-               warnx("Prefix length (%lu) is out of bounds (0-%u).",
+               warnx("Prefix length (%lu) is out of range (0-%u).",
                    asn, UINT32_MAX);
                return -EINVAL;
        }
@@ -90,8 +90,8 @@ add_vrp(char *line, struct vrplist *vrplist)
 
        line_copy = malloc(strlen(line) + 1);
        if (line_copy == NULL) {
-               error = -ENOMEM;
-               err(error, "Out of memory allocating CSV line copy");
+               error = errno;
+               warn("Out of memory allocating CSV line copy");
                goto error;
        }
        strcpy(line_copy, line);
@@ -143,8 +143,7 @@ add_vrp(char *line, struct vrplist *vrplist)
 
        if (prefix_length > max_prefix_length) {
                error = -EINVAL;
-               err(error,
-                   "Prefix length is greater than max prefix length at line '%s'",
+               warnx("Prefix length is greater than max prefix length at line '%s'",
                    line);
                goto error;
        }
@@ -179,15 +178,15 @@ load_vrps(struct line_file *lfile, bool is_update)
 
        /* Init delta */
        error = vrplist_init(&localvrps);
-       if (error ) {
-               warn("Couldn't allocate new VRPs");
+       if (error) {
+               warnx("Couldn't allocate new VRPs");
                return error;
        }
        current_line = 1;
        do {
                error = lfile_read(lfile, &line);
                if (error) {
-                       warn("Error reading line %d, stop processing file.",
+                       warnx("Error reading line %d, stop processing file.",
                            current_line);
                        goto end;
                }
index a6f00d4c15635a988ba336b8bb9ffa9d19622e36..923fe0ab3e35a60b5d5888167e670f17300b9c99 100644 (file)
@@ -41,7 +41,7 @@ void
 lfile_close(struct line_file *lf)
 {
        if (fclose(lf->file) == -1)
-               err(errno, "fclose() failed: %s", strerror(errno));
+               warn("fclose() failed");
        free(lf);
 }
 
@@ -103,7 +103,7 @@ lfile_read(struct line_file *lfile, char **result)
                free(string);
                *result = NULL;
                if (ferror(lfile->file)) {
-                       err(error, "Error while reading file: %s\n",
+                       warnx("Error while reading file: %s",
                            strerror(error));
                        return error;
                }
@@ -111,8 +111,7 @@ lfile_read(struct line_file *lfile, char **result)
                        return 0;
 
                error = -EINVAL;
-               err(error,
-                   "Supposedly unreachable code reached. ferror:%d feof:%d\n",
+               warnx("Supposedly unreachable code reached. ferror:%d feof:%d",
                    ferror(lfile->file), feof(lfile->file));
                return error;
        }
@@ -127,8 +126,7 @@ lfile_read(struct line_file *lfile, char **result)
        for (i = 0; i < len; i++) {
                if (string[i] == '\0') {
                        error = -EINVAL;
-                       err(error,
-                           "File '%s' has an illegal null character in its body. Please remove it.\n",
+                       warnx("File '%s' has an illegal null character in its body. Please remove it.",
                            lfile_name(lfile));
                        free(string);
                        return error;
index 25456feb8b748ba75c8ac7444e0661df738dc1aa..9d22aeaf2fede95f4ee6cd651a83721e29860aeb 100644 (file)
@@ -32,6 +32,6 @@ notify_clients(void)
                error = send_notify(ptr->fd, ptr->rtr_version);
                /* Error? Log it */
                if (error)
-                       err(error, "Error sending notify PDU");
+                       warnx("Error sending notify PDU to client");
        }
 }
index 8a809a225a8fb0517e21605accbafe56b7319087..cbea8177e0599278353a9953c0d47ad9227cb0c3 100644 (file)
@@ -66,6 +66,6 @@ err_pdu_log(u_int16_t code, char *message)
                break;
        }
 
-       warnx("Error report info: '%s', message '%s'.",
+       warnx("Error report PDU info: '%s', message '%s'.",
            code_title, message == NULL ? "[empty]" : message);
 }
index 250d0be6fef6c4fa22e71074a46c562c2169d3fd..f60d08eea44139c34d72ca534eac8c4e9543575f 100644 (file)
@@ -91,9 +91,8 @@ handle_serial_query_pdu(int fd, void *pdu)
                        return error;
                return send_end_of_data_pdu(&common);
        default:
-               error = -EINVAL;
-               err(error, "Reached 'unreachable' code");
-               return error;
+               warnx("Reached 'unreachable' code");
+               return -EINVAL;
        }
 }
 
@@ -105,7 +104,7 @@ handle_reset_query_pdu(int fd, void *pdu)
        u_int32_t current_serial;
        u_int16_t session_id;
        u_int8_t version;
-       int error, updates;
+       int updates;
 
        version = received->header.protocol_version;
        session_id = get_current_session_id(version);
@@ -123,9 +122,8 @@ handle_reset_query_pdu(int fd, void *pdu)
                /* https://tools.ietf.org/html/rfc8210#section-8.1 */
                return send_commmon_exchange(&common);
        default:
-               error = -EINVAL;
-               err(error, "Reached 'unreachable' code");
-               return error;
+               warnx("Reached 'unreachable' code");
+               return -EINVAL;
        }
 }
 
index 3cf291ea22d1de35e2e1d473df9ab920f0676e93..34d28b4140c543142225a719ef920e5fdacc3f2f 100644 (file)
@@ -85,9 +85,8 @@ send_response(int fd, char *data, size_t data_len)
        init_buffer(&buffer);
        /* Check for buffer overflow */
        if (data_len > buffer.capacity) {
-               error = -EINVAL;
-               err(error, "Buffer out of capacity");
-               return error;
+               warnx("Response buffer out of capacity");
+               return -EINVAL;
        }
        memcpy(buffer.data, data, data_len);
        buffer.len = data_len;
@@ -95,7 +94,7 @@ send_response(int fd, char *data, size_t data_len)
        error = write(fd, buffer.data, buffer.len);
        free_buffer(&buffer);
        if (error < 0) {
-               err(errno, "Error sending response");
+               warnx("Error sending response");
                return error;
        }
 
index 99883ba7e73e9003d0eacafad5954afdfeb364e4..7a1da41034279a8d98eec4dc7e86e84752485481 100644 (file)
@@ -133,6 +133,7 @@ client_thread_cb(void *param_void)
                        err_pdu_send(param.client_fd, RTR_VERSION_SUPPORTED,
                            ERR_PDU_UNSUP_PROTO_VERSION,
                            (struct pdu_header *) pdu, NULL);
+                       meta->destructor(pdu);
                        return NULL;
                }
                /* RTR Version ready, now update client */
@@ -146,6 +147,7 @@ client_thread_cb(void *param_void)
                                    : ERR_PDU_UNEXPECTED_PROTO_VERSION),
                                    (struct pdu_header *) pdu, NULL);
                        }
+                       meta->destructor(pdu);
                        return NULL;
                }
 
@@ -194,8 +196,8 @@ handle_client_connections(int server_fd)
                 */
 
                arg = malloc(sizeof(struct thread_param));
-               if (!arg) {
-                       warnx("Thread parameter allocation failure");
+               if (arg == NULL) {
+                       warn("Thread parameter allocation failure");
                        continue;
                }
                arg->client_fd = client_fd;
index fd4efbd60015aa78ac07e006de6e3b74dd319dc6..f105c47d826c7c246a935da0fccd47b9180edeaa 100644 (file)
@@ -18,7 +18,7 @@ check_vrps_updates(void *param_void) {
                updated = false;
                error = csv_check_vrps_file(&updated);
                if (error) {
-                       err(error, "Error while searching CSV updates");
+                       warnx("Error while searching CSV updates, sleeping..");
                        goto sleep;
                }
                if (updated)
index 71889406778f681b0ba5888344ad50556f45e8fd..8c8eb521d2f14e269a21ccb3ebdb61e857e1430c 100644 (file)
@@ -230,7 +230,7 @@ copy_vrps(struct vrp **dst, struct vrp *src, unsigned int len)
        struct vrp *tmp;
        tmp = realloc(*dst, len * sizeof(struct vrp));
        if (tmp == NULL) {
-               warnx("Couldn't copy VRPs");
+               warn("Couldn't copy VRPs");
                return;
        }
        *dst = tmp;
@@ -245,7 +245,7 @@ deltas_db_create_delta(struct vrp *array, unsigned int len)
 
        error = delta_init(&new_delta);
        if (error) {
-               warn("New Delta couldn't be initialized");
+               warnx("New Delta couldn't be initialized");
                return error;
        }