#include "port.h"
struct nw_port {
- struct nw_daemon* daemon;
+ nw_daemon* daemon;
int nrefs;
+ // Link
+ nw_link* link;
+
char name[IF_NAMESIZE];
nw_port_type_t type;
// Configuration
- struct nw_config *config;
+ nw_config *config;
// Common attributes
nw_address_t address;
return NW_PORT_UNKNOWN;
}
-static void nw_port_free(struct nw_port* port) {
+static void nw_port_free(nw_port* port) {
+ if (port->link)
+ nw_link_unref(port->link);
if (port->config)
nw_config_unref(port->config);
if (port->daemon)
free(port);
}
-static int nw_port_setup_address(struct nw_port* port) {
+static int nw_port_setup_address(nw_port* port) {
int r;
// Read ADDRESS from configuration
return 0;
}
-static int nw_port_setup_common(struct nw_port* port) {
+static int nw_port_setup_common(nw_port* port) {
int r;
// Address
return 0;
}
-static nw_port_type_t nw_port_setup_type(struct nw_port* port) {
+static nw_port_type_t nw_port_setup_type(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) {
+static int nw_port_setup(nw_port* port) {
char path[PATH_MAX];
int r;
+ // Find the link
+ port->link = nw_daemon_get_link_by_name(port->daemon, port->name);
+ if (port->link) {
+ DEBUG("%s: Found matching link %d\n", port->name, nw_link_ifindex(port->link));
+ } else {
+ DEBUG("%s: Could not find matching link\n", port->name);
+ }
+
// Compose the path to the main configuration file
r = nw_path_join(path, PORT_CONFIG_DIR, port->name);
if (r)
return 0;
}
-int nw_port_create(struct nw_port** port, struct nw_daemon* daemon, const char* name) {
+int nw_port_create(nw_port** port, nw_daemon* daemon, const char* name) {
int r;
// Allocate a new object
- struct nw_port* p = calloc(1, sizeof(*p));
+ nw_port* p = calloc(1, sizeof(*p));
if (!p)
return 1;
return r;
}
-struct nw_port* nw_port_ref(struct nw_port* port) {
+nw_port* nw_port_ref(nw_port* port) {
port->nrefs++;
return port;
}
-struct nw_port* nw_port_unref(struct nw_port* port) {
+nw_port* nw_port_unref(nw_port* port) {
if (--port->nrefs > 0)
return port;
return NULL;
}
-const char* nw_port_name(struct nw_port* port) {
+int nw_port_save(nw_port* port) {
+ int r;
+
+ r = nw_config_write(port->config);
+ if (r)
+ return r;
+
+ return 0;
+}
+
+const char* nw_port_name(nw_port* port) {
return port->name;
}
-char* nw_port_bus_path(struct nw_port* port) {
+char* nw_port_bus_path(nw_port* port) {
char* p = NULL;
int r;
return p;
}
-static struct nw_link* nw_port_get_link(struct nw_port* port) {
- return nw_daemon_get_link_by_name(port->daemon, port->name);
+static nw_link* nw_port_get_link(nw_port* port) {
+ if (!port->link)
+ return NULL;
+
+ return nw_link_ref(port->link);
}
-const nw_address_t* nw_port_get_address(struct nw_port* port) {
+const nw_address_t* nw_port_get_address(nw_port* port) {
return &port->address;
}
+
+int nw_port_has_carrier(nw_port* port) {
+ if (!port->link)
+ return 0;
+
+ return nw_link_has_carrier(port->link);
+}