From 64f2b95f622a3c2b16e4dbdc8553d506ef870a8c Mon Sep 17 00:00:00 2001
From: Sander Vrijders <sander.vrijders@intec.ugent.be>
Date: Tue, 21 Feb 2017 14:45:04 +0100
Subject: ipcpd: normal: Change address authority policy

This changes the address authority to follow a similar approach to
that of the other policies. No function pointers are passed to its
user anymore.
---
 src/ipcpd/normal/addr_auth.c         | 44 ++++++++++++++----------------------
 src/ipcpd/normal/addr_auth.h         | 12 ++++------
 src/ipcpd/normal/main.c              | 20 ++++++++--------
 src/ipcpd/normal/pol-addr-auth-ops.h | 34 ++++++++++++++++++++++++++++
 src/ipcpd/normal/pol/flat.c          | 10 ++++++++
 src/ipcpd/normal/pol/flat.h          |  8 +++++++
 6 files changed, 83 insertions(+), 45 deletions(-)
 create mode 100644 src/ipcpd/normal/pol-addr-auth-ops.h

(limited to 'src/ipcpd')

diff --git a/src/ipcpd/normal/addr_auth.c b/src/ipcpd/normal/addr_auth.c
index 210744af..8469e95e 100644
--- a/src/ipcpd/normal/addr_auth.c
+++ b/src/ipcpd/normal/addr_auth.c
@@ -3,7 +3,8 @@
  *
  * Address authority
  *
- *    Sander Vrijders <sander.vrijders@intec.ugent.be>
+ *    Sander Vrijders   <sander.vrijders@intec.ugent.be>
+ *    Dimitri Staessens <dimitri.staessens@intec.ugent.be>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -25,47 +26,36 @@
 #include <ouroboros/logs.h>
 
 #include "addr_auth.h"
+#include "pol-addr-auth-ops.h"
 #include "pol/flat.h"
 
 #include <stdlib.h>
 #include <assert.h>
 
-struct addr_auth * addr_auth_create(enum pol_addr_auth type)
-{
-        struct addr_auth * tmp;
-
-        tmp = malloc(sizeof(*tmp));
-        if (tmp == NULL) {
-                log_err("Failed to malloc addr auth.");
-                return NULL;
-        }
+struct addr_auth {
+        struct pol_addr_auth_ops * ops;
+} addr_auth;
 
+int addr_auth_init(enum pol_addr_auth type)
+{
         switch (type) {
         case FLAT_RANDOM:
-                tmp->address = flat_address;
-                tmp->type = type;
+                addr_auth.ops = &flat_ops;
                 break;
         default:
                 log_err("Unknown address authority type.");
-                free(tmp);
-                return NULL;
+                return -1;
         }
 
-        return tmp;
+        return addr_auth.ops->init();
 }
 
-int addr_auth_destroy(struct addr_auth * instance)
+uint64_t addr_auth_address(void)
 {
-        assert(instance);
-
-        switch (instance->type) {
-        case FLAT_RANDOM:
-                break;
-        default:
-                log_err("Unknown address authority type.");
-        }
-
-        free(instance);
+        return addr_auth.ops->address();
+}
 
-        return 0;
+int addr_auth_fini(void)
+{
+        return addr_auth.ops->fini();
 }
diff --git a/src/ipcpd/normal/addr_auth.h b/src/ipcpd/normal/addr_auth.h
index 8d67bc66..b389fa90 100644
--- a/src/ipcpd/normal/addr_auth.h
+++ b/src/ipcpd/normal/addr_auth.h
@@ -3,7 +3,8 @@
  *
  * Address authority
  *
- *    Sander Vrijders <sander.vrijders@intec.ugent.be>
+ *    Sander Vrijders   <sander.vrijders@intec.ugent.be>
+ *    Dimitri Staessens <dimitri.staessens@intec.ugent.be>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -26,13 +27,10 @@
 
 #include <stdint.h>
 
-struct addr_auth {
-        enum pol_addr_auth type;
-        uint64_t (* address)(void);
-};
+int      addr_auth_init(enum pol_addr_auth type);
 
-struct addr_auth * addr_auth_create(enum pol_addr_auth type);
+int      addr_auth_fini(void);
 
-int                addr_auth_destroy(struct addr_auth * instance);
+uint64_t addr_auth_address(void);
 
 #endif /* OUROBOROS_IPCPD_NORMAL_ADDR_AUTH_H */
diff --git a/src/ipcpd/normal/main.c b/src/ipcpd/normal/main.c
index 74a74c5b..9ce2383d 100644
--- a/src/ipcpd/normal/main.c
+++ b/src/ipcpd/normal/main.c
@@ -54,7 +54,6 @@
 
 struct {
         pthread_t acceptor;
-        struct addr_auth * auth;
 } normal;
 
 void ipcp_sig_handler(int         sig,
@@ -167,16 +166,15 @@ static int boot_components(void)
                 return -1;
         }
 
-        normal.auth = addr_auth_create(pa);
-        if (normal.auth == NULL) {
+        if (addr_auth_init(pa)) {
                 log_err("Failed to init address authority.");
                 return -1;
         }
 
-        ipcpi.address = normal.auth->address();
+        ipcpi.address = addr_auth_address();
         if (ipcpi.address == 0) {
                 log_err("Failed to get a valid address.");
-                addr_auth_destroy(normal.auth);
+                addr_auth_fini();
                 return -1;
         }
 
@@ -186,14 +184,14 @@ static int boot_components(void)
 
         if (ribmgr_init()) {
                 log_err("Failed to initialize RIB manager.");
-                addr_auth_destroy(normal.auth);
+                addr_auth_fini();
                 return -1;
         }
 
         if (dir_init()) {
                 log_err("Failed to initialize directory.");
                 ribmgr_fini();
-                addr_auth_destroy(normal.auth);
+                addr_auth_fini();
                 return -1;
         }
 
@@ -202,7 +200,7 @@ static int boot_components(void)
         if (fmgr_init()) {
                 dir_fini();
                 ribmgr_fini();
-                addr_auth_destroy(normal.auth);
+                addr_auth_fini();
                 log_err("Failed to start flow manager.");
                 return -1;
         }
@@ -211,7 +209,7 @@ static int boot_components(void)
                 fmgr_fini();
                 dir_fini();
                 ribmgr_fini();
-                addr_auth_destroy(normal.auth);
+                addr_auth_fini();
                 log_err("Failed to initialize FRCT.");
                 return -1;
         }
@@ -223,7 +221,7 @@ static int boot_components(void)
                 fmgr_fini();
                 dir_fini();
                 ribmgr_fini();
-                addr_auth_destroy(normal.auth);
+                addr_auth_fini();
                 log_err("Failed to create acceptor thread.");
                 return -1;
         }
@@ -244,7 +242,7 @@ void shutdown_components(void)
 
         ribmgr_fini();
 
-        addr_auth_destroy(normal.auth);
+        addr_auth_fini();
 }
 
 static int normal_ipcp_enroll(char * dst_name)
diff --git a/src/ipcpd/normal/pol-addr-auth-ops.h b/src/ipcpd/normal/pol-addr-auth-ops.h
new file mode 100644
index 00000000..25952636
--- /dev/null
+++ b/src/ipcpd/normal/pol-addr-auth-ops.h
@@ -0,0 +1,34 @@
+/*
+ * Ouroboros - Copyright (C) 2016 - 2017
+ *
+ * Address authority policy ops
+ *
+ *    Dimitri Staessens <dimitri.staessens@intec.ugent.be>
+ *    Sander Vrijders   <sander.vrijders@intec.ugent.be>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef OUROBOROS_IPCPD_NORMAL_POL_ADDR_AUTH_OPS_H
+#define OUROBOROS_IPCPD_NORMAL_POL_ADDR_AUTH_OPS_H
+
+struct pol_addr_auth_ops {
+        int      (* init)(void);
+
+        int      (* fini)(void);
+
+        uint64_t (* address)(void);
+};
+
+#endif /* OUROBOROS_IPCPD_NORMAL_POL_ADDR_AUTH_OPS_H */
diff --git a/src/ipcpd/normal/pol/flat.c b/src/ipcpd/normal/pol/flat.c
index d982f5ac..aa0f6c7c 100644
--- a/src/ipcpd/normal/pol/flat.c
+++ b/src/ipcpd/normal/pol/flat.c
@@ -80,6 +80,16 @@ static int addr_taken(char *  name,
 
 #define INVALID_ADDRESS 0
 
+int flat_init(void)
+{
+        return 0;
+}
+
+int flat_fini(void)
+{
+        return 0;
+}
+
 uint64_t flat_address(void)
 {
         struct timespec t;
diff --git a/src/ipcpd/normal/pol/flat.h b/src/ipcpd/normal/pol/flat.h
index 73d7de8b..85fe9281 100644
--- a/src/ipcpd/normal/pol/flat.h
+++ b/src/ipcpd/normal/pol/flat.h
@@ -22,8 +22,16 @@
 #ifndef OUROBOROS_IPCPD_NORMAL_FLAT_H
 #define OUROBOROS_IPCPD_NORMAL_FLAT_H
 
+#include "pol-addr-auth-ops.h"
+
 int      flat_init(void);
 int      flat_fini(void);
 uint64_t flat_address(void);
 
+struct pol_addr_auth_ops flat_ops = {
+        .init    = flat_init,
+        .fini    = flat_fini,
+        .address = flat_address
+};
+
 #endif /* OUROBOROS_IPCPD_NORMAL_FLAT_H */
-- 
cgit v1.2.3