# #
#############################################################################*/
+#include <limits.h>
#include <net/if.h>
#include <stdlib.h>
#include "config.h"
+#include "logging.h"
#include "string.h"
#include "port.h"
int nrefs;
char name[IF_NAMESIZE];
+ nw_port_type_t type;
// Configuration
struct nw_config *config;
};
+static const struct nw_port_type_map {
+ nw_port_type_t type;
+ const char* name;
+} nw_port_type_map[] = {
+ { NW_PORT_DUMMY, "dummy" },
+ { NW_PORT_UNKNOWN, NULL },
+};
+
+static nw_port_type_t nw_port_type_from_string(const char* s) {
+ const struct nw_port_type_map* map = NULL;
+
+ for (map = nw_port_type_map; *map->name; map++) {
+ if (strcmp(map->name, s) == 0)
+ return map->type;
+ }
+
+ return NW_PORT_UNKNOWN;
+}
+
static void nw_port_free(struct nw_port* port) {
if (port->config)
nw_config_unref(port->config);
free(port);
}
+static int nw_port_setup_common(struct nw_port* port) {
+ return 0; // XXX TODO
+}
+
+static nw_port_type_t nw_port_setup_type(struct nw_port* port) {
+ const char* type = nw_config_get(port->config, "TYPE");
+ if (!type)
+ return NW_PORT_UNKNOWN;
+
+ return nw_port_type_from_string(type);
+}
+
+static int nw_port_setup(struct nw_port* port) {
+ char path[PATH_MAX];
+ int r;
+
+ // Compose the path to the main configuration file
+ r = nw_path_join(path, PORT_CONFIG_DIR, port->name);
+ if (r)
+ return r;
+
+ // Initialize the configuration
+ r = nw_config_create(&port->config, path);
+ if (r)
+ return r;
+
+ // Determine type
+ port->type = nw_port_setup_type(port);
+ if (!port->type) {
+ ERROR("Could not determine type of port %s\n", port->name);
+ return 0;
+ }
+
+ // Perform some common initialization
+ r = nw_port_setup_common(port);
+ if (r)
+ return r;
+
+ // Call any custom initialization
+ switch (port->type) {
+ // These do not need any special initialization
+ case NW_PORT_DUMMY:
+ case NW_PORT_UNKNOWN:
+ break;
+ }
+
+ return 0;
+}
+
int nw_port_create(struct nw_port** port, const char* name) {
int r;
if (r)
goto ERROR;
+ // Setup the port
+ r = nw_port_setup(p);
+ if (r)
+ goto ERROR;
+
*port = p;
return 0;