]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Store Clients DB at stack, also fix incorrect line width
authorpcarana <pc.moreno2099@gmail.com>
Wed, 20 Mar 2019 22:59:51 +0000 (16:59 -0600)
committerpcarana <pc.moreno2099@gmail.com>
Wed, 20 Mar 2019 22:59:51 +0000 (16:59 -0600)
src/clients.c

index e09bb4e5f55d6d71eb5561f1153a4871d0292487..0d496f8a55b26cf3e594768cac1f099bba26b671 100644 (file)
@@ -7,20 +7,14 @@
 
 ARRAY_LIST(clientsdb, struct client)
 
-/* TODO (review) Does not need to live in the heap */
-struct clientsdb *clients_db;
+struct clientsdb clients_db;
 
 int
 clients_db_init(void)
 {
        int error;
 
-       clients_db = malloc(sizeof(struct clientsdb));
-       if (clients_db == NULL) {
-               err(-ENOMEM, "Clients DB couldn't be allocated");
-               return -ENOMEM;
-       }
-       error = clientsdb_init(clients_db);
+       error = clientsdb_init(&clients_db);
        if (error)
                err(error, "Clients DB couldn't be initialized");
 
@@ -30,7 +24,7 @@ clients_db_init(void)
 static int
 clients_db_add_client(struct client *client)
 {
-       return clientsdb_add(clients_db, client);
+       return clientsdb_add(&clients_db, client);
 }
 
 static struct client *
@@ -38,15 +32,19 @@ get_client(struct sockaddr_storage *addr)
 {
        struct client *ptr;
 
-       ARRAYLIST_FOREACH(clients_db, ptr)
+       ARRAYLIST_FOREACH(&clients_db, ptr)
                if (ptr->sin_family == addr->ss_family) {
                        if (ptr->sin_family == AF_INET) {
-                               if (ptr->sin_addr.s_addr == SADDR_IN(addr)->sin_addr.s_addr &&
+                               if (ptr->sin_addr.s_addr ==
+                                   SADDR_IN(addr)->sin_addr.s_addr &&
                                    ptr->sin_port == SADDR_IN(addr)->sin_port)
                                        return ptr;
                        } else if (ptr->sin_family == AF_INET6)
-                               if (IN6_ARE_ADDR_EQUAL(ptr->sin6_addr.s6_addr32, SADDR_IN6(addr)->sin6_addr.s6_addr32) &&
-                                   ptr->sin_port == SADDR_IN6(addr)->sin6_port)
+                               if (IN6_ARE_ADDR_EQUAL(
+                                   ptr->sin6_addr.s6_addr32,
+                                   SADDR_IN6(addr)->sin6_addr.s6_addr32) &&
+                                   ptr->sin_port ==
+                                   SADDR_IN6(addr)->sin6_port)
                                        return ptr;
                }
 
@@ -56,27 +54,20 @@ get_client(struct sockaddr_storage *addr)
 static int
 create_client(int fd, struct sockaddr_storage *addr, u_int8_t rtr_version)
 {
-       struct client *client;
+       struct client client;
 
-       /* TODO (review) `client` should be in the stack. */
-       client = malloc(sizeof(struct client));
-       if (client == NULL) {
-               err(-ENOMEM, "Couldn't allocate client");
-               return -ENOMEM;
-       }
-
-       client->fd = fd;
-       client->sin_family = addr->ss_family;
+       client.fd = fd;
+       client.sin_family = addr->ss_family;
        if (addr->ss_family == AF_INET) {
-               client->sin_addr = SADDR_IN(addr)->sin_addr;
-               client->sin_port = SADDR_IN(addr)->sin_port;
+               client.sin_addr = SADDR_IN(addr)->sin_addr;
+               client.sin_port = SADDR_IN(addr)->sin_port;
        } else if (addr->ss_family == AF_INET6) {
-               client->sin6_addr = SADDR_IN6(addr)->sin6_addr;
-               client->sin_port = SADDR_IN6(addr)->sin6_port;
+               client.sin6_addr = SADDR_IN6(addr)->sin6_addr;
+               client.sin_port = SADDR_IN6(addr)->sin6_port;
        }
-       client->rtr_version = rtr_version;
+       client.rtr_version = rtr_version;
 
-       return clients_db_add_client(client);
+       return clients_db_add_client(&client);
 }
 
 /*
@@ -98,18 +89,8 @@ update_client(int fd, struct sockaddr_storage *addr, u_int8_t rtr_version)
                return create_client(fd, addr, rtr_version);
 
        /*
-        * TODO (review) Your margin seems to be misconfigured; you're violating
-        * the line width often.
-        *
-        * Line width is 80 characters and tabs are 8.
-        *
-        * `Window > Preferences > C/C++ > Code Style > Formatter > New... > Ok`
-        *      `Indentation > Tab size`
-        *      `Line Wrapping > Maximum line width`
-        */
-       /*
-        * Isn't ready to handle distinct version on clients reconnection, but for
-        * now there's no problem since only 1 RTR version is supported (RVR v0).
+        * Isn't ready to handle distinct version on clients reconnection, but
+        * for now there's no problem since only RTR v0 is supported.
         */
        if (client->rtr_version != rtr_version)
                return -EINVAL;
@@ -121,19 +102,12 @@ update_client(int fd, struct sockaddr_storage *addr, u_int8_t rtr_version)
 size_t
 client_list(struct client **clients)
 {
-       *clients = clients_db->array;
-       return clients_db->len;
-}
-
-static void
-client_destroy(struct client *client)
-{
-       free(client);
+       *clients = clients_db.array;
+       return clients_db.len;
 }
 
 void
 clients_db_destroy(void)
 {
-       clientsdb_cleanup(clients_db, client_destroy);
-       free(clients_db);
+       clientsdb_cleanup(&clients_db, NULL);
 }