return nw_links_get_by_ifindex(daemon->links, ifindex);
}
+struct nw_link* nw_daemon_get_link_by_name(struct nw_daemon* daemon, const char* name) {
+ if (!daemon->links)
+ return NULL;
+
+ return nw_links_get_by_name(daemon->links, name);
+}
+
/*
Ports
*/
struct nw_links* nw_daemon_links(struct nw_daemon* daemon);
void nw_daemon_drop_link(struct nw_daemon* daemon, struct nw_link* link);
struct nw_link* nw_daemon_get_link_by_ifindex(struct nw_daemon* daemon, int ifindex);
+struct nw_link* nw_daemon_get_link_by_name(struct nw_daemon* daemon, const char* name);
/*
Ports
#ifndef NETWORKD_LINK_H
#define NETWORKD_LINK_H
-#include "daemon.h"
-
struct nw_link;
+#include "daemon.h"
+
int nw_link_create(struct nw_link** link, struct nw_daemon* daemon, int ifindex);
struct nw_link* nw_link_ref(struct nw_link* link);
struct nw_link* nw_link_unref(struct nw_link* link);
int nw_link_ifindex(struct nw_link* link);
+const char* nw_link_name(struct nw_link* link);
int nw_link_process(sd_netlink* rtnl, sd_netlink_message* message, void* data);
#include <stddef.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/queue.h>
#include "daemon.h"
return nw_link_ref(entry->link);
}
+
+struct nw_link* nw_links_get_by_name(struct nw_links* links, const char* name) {
+ struct nw_links_entry* entry = NULL;
+
+ STAILQ_FOREACH(entry, &links->entries, nodes) {
+ const char* n = nw_link_name(entry->link);
+
+ if (strcmp(name, n) == 0)
+ return nw_link_ref(entry->link);
+ }
+
+ // No match found
+ return NULL;
+}
int nw_links_enumerate(struct nw_links* links);
struct nw_link* nw_links_get_by_ifindex(struct nw_links* links, int ifindex);
+struct nw_link* nw_links_get_by_name(struct nw_links* links, const char* name);
#endif /* NETWORKD_LINKS_H */
#include "address.h"
#include "config.h"
+#include "link.h"
#include "logging.h"
#include "string.h"
#include "port.h"
struct nw_port {
+ struct nw_daemon* daemon;
int nrefs;
char name[IF_NAMESIZE];
static void nw_port_free(struct nw_port* port) {
if (port->config)
nw_config_unref(port->config);
+ if (port->daemon)
+ nw_daemon_unref(port->daemon);
free(port);
}
return 0;
}
-int nw_port_create(struct nw_port** port, const char* name) {
+int nw_port_create(struct nw_port** port, struct nw_daemon* daemon, const char* name) {
int r;
// Allocate a new object
if (!p)
return 1;
+ // Store a reference to the daemon
+ p->daemon = nw_daemon_ref(daemon);
+
// Initialize reference counter
p->nrefs = 1;
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);
+}
+
const nw_address_t* nw_port_get_address(struct nw_port* port) {
return &port->address;
}
struct nw_port;
#include "address.h"
+#include "daemon.h"
-int nw_port_create(struct nw_port** port, const char* name);
+int nw_port_create(struct nw_port** port, struct nw_daemon* daemon, const char* name);
struct nw_port* nw_port_ref(struct nw_port* port);
struct nw_port* nw_port_unref(struct nw_port* port);
return 0;
// Create a new port
- r = nw_port_create(&port, name);
+ r = nw_port_create(&port, ports->daemon, name);
if (r)
goto ERROR;