1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-08 21:17:47 +03:00

libsystemd-network: introduce sd_xxx_{set,get}_ifname()

This commit is contained in:
Yu Watanabe 2021-02-21 23:19:26 +09:00
parent dbdcd51f78
commit 61a9fa8f0c
23 changed files with 204 additions and 22 deletions

View File

@ -48,6 +48,7 @@ struct sd_dhcp_server {
int fd_raw;
int ifindex;
char *ifname;
be32_t address;
be32_t netmask;
be32_t subnet;

View File

@ -12,6 +12,7 @@ struct sd_lldp {
unsigned n_ref;
int ifindex;
char *ifname;
int fd;
sd_event *event;

View File

@ -16,6 +16,8 @@ sources = files('''
sd-ipv4acd.c
arp-util.h
arp-util.c
network-common.c
network-common.h
network-internal.c
network-internal.h
sd-ndisc.c

View File

@ -5,11 +5,11 @@
Copyright © 2014 Intel Corporation. All rights reserved.
***/
#include "sd-ndisc.h"
#include "log.h"
#include "time-util.h"
#include "sd-ndisc.h"
#define NDISC_ROUTER_SOLICITATION_INTERVAL (4U * USEC_PER_SEC)
#define NDISC_MAX_ROUTER_SOLICITATION_INTERVAL (3600U * USEC_PER_SEC)
#define NDISC_MAX_ROUTER_SOLICITATIONS 3U
@ -18,6 +18,7 @@ struct sd_ndisc {
unsigned n_ref;
int ifindex;
char *ifname;
int fd;
sd_event *event;

View File

@ -0,0 +1,24 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "format-util.h"
#include "network-common.h"
#include "string-util.h"
const char *get_ifname(int ifindex, char **ifname) {
char buf[IF_NAMESIZE + 1];
assert(ifname);
/* This sets ifname only when it is not set yet. */
if (*ifname)
return *ifname;
if (ifindex <= 0)
return NULL;
if (!format_ifname(ifindex, buf))
return NULL;
return *ifname = strdup(buf);
}

View File

@ -0,0 +1,4 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
const char *get_ifname(int ifindex, char **ifname);

View File

@ -41,6 +41,7 @@ struct sd_radv {
RAdvState state;
int ifindex;
char *ifname;
sd_event *event;
int event_priority;

View File

@ -24,6 +24,7 @@
#include "hostname-util.h"
#include "io-util.h"
#include "memory-util.h"
#include "network-common.h"
#include "random-util.h"
#include "set.h"
#include "sort-util.h"
@ -76,6 +77,7 @@ struct sd_dhcp_client {
int event_priority;
sd_event_source *timeout_resend;
int ifindex;
char *ifname;
int fd;
uint16_t port;
union sockaddr_union link;
@ -282,6 +284,23 @@ int sd_dhcp_client_set_ifindex(sd_dhcp_client *client, int ifindex) {
return 0;
}
int sd_dhcp_client_set_ifname(sd_dhcp_client *client, const char *ifname) {
assert_return(client, -EINVAL);
assert_return(ifname, -EINVAL);
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
return -EINVAL;
return free_and_strdup(&client->ifname, ifname);
}
const char *sd_dhcp_client_get_ifname(sd_dhcp_client *client) {
if (!client)
return NULL;
return get_ifname(client->ifindex, &client->ifname);
}
int sd_dhcp_client_set_mac(
sd_dhcp_client *client,
const uint8_t *addr,
@ -2205,6 +2224,7 @@ static sd_dhcp_client *dhcp_client_free(sd_dhcp_client *client) {
client->user_class = strv_free(client->user_class);
ordered_hashmap_free(client->extra_options);
ordered_hashmap_free(client->vendor_options);
free(client->ifname);
return mfree(client);
}

View File

@ -15,6 +15,7 @@
#include "fd-util.h"
#include "in-addr-util.h"
#include "io-util.h"
#include "network-common.h"
#include "ordered-set.h"
#include "siphash24.h"
#include "string-util.h"
@ -158,6 +159,8 @@ static sd_dhcp_server *dhcp_server_free(sd_dhcp_server *server) {
ordered_set_free(server->vendor_options);
free(server->bound_leases);
free(server->ifname);
return mfree(server);
}
@ -169,29 +172,47 @@ int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) {
assert_return(ret, -EINVAL);
assert_return(ifindex > 0, -EINVAL);
server = new0(sd_dhcp_server, 1);
server = new(sd_dhcp_server, 1);
if (!server)
return -ENOMEM;
server->n_ref = 1;
server->fd_raw = -1;
server->fd = -1;
server->address = htobe32(INADDR_ANY);
server->netmask = htobe32(INADDR_ANY);
server->ifindex = ifindex;
*server = (sd_dhcp_server) {
.n_ref = 1,
.fd_raw = -1,
.fd = -1,
.address = htobe32(INADDR_ANY),
.netmask = htobe32(INADDR_ANY),
.ifindex = ifindex,
.default_lease_time = DIV_ROUND_UP(DHCP_DEFAULT_LEASE_TIME_USEC, USEC_PER_SEC),
.max_lease_time = DIV_ROUND_UP(DHCP_MAX_LEASE_TIME_USEC, USEC_PER_SEC),
};
server->leases_by_client_id = hashmap_new(&dhcp_lease_hash_ops);
if (!server->leases_by_client_id)
return -ENOMEM;
server->default_lease_time = DIV_ROUND_UP(DHCP_DEFAULT_LEASE_TIME_USEC, USEC_PER_SEC);
server->max_lease_time = DIV_ROUND_UP(DHCP_MAX_LEASE_TIME_USEC, USEC_PER_SEC);
*ret = TAKE_PTR(server);
return 0;
}
int sd_dhcp_server_set_ifname(sd_dhcp_server *server, const char *ifname) {
assert_return(server, -EINVAL);
assert_return(ifname, -EINVAL);
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
return -EINVAL;
return free_and_strdup(&server->ifname, ifname);
}
const char *sd_dhcp_server_get_ifname(sd_dhcp_server *server) {
if (!server)
return NULL;
return get_ifname(server->ifindex, &server->ifname);
}
int sd_dhcp_server_attach_event(sd_dhcp_server *server, sd_event *event, int64_t priority) {
int r;

View File

@ -21,6 +21,7 @@
#include "hexdecoct.h"
#include "hostname-util.h"
#include "in-addr-util.h"
#include "network-common.h"
#include "random-util.h"
#include "socket-util.h"
#include "string-table.h"
@ -47,6 +48,7 @@ struct sd_dhcp6_client {
sd_event *event;
int event_priority;
int ifindex;
char *ifname;
DHCP6Address hint_pd_prefix;
struct in6_addr local_address;
uint8_t mac_addr[MAX_MAC_ADDR_LEN];
@ -165,6 +167,23 @@ int sd_dhcp6_client_set_ifindex(sd_dhcp6_client *client, int ifindex) {
return 0;
}
int sd_dhcp6_client_set_ifname(sd_dhcp6_client *client, const char *ifname) {
assert_return(client, -EINVAL);
assert_return(ifname, -EINVAL);
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
return -EINVAL;
return free_and_strdup(&client->ifname, ifname);
}
const char *sd_dhcp6_client_get_ifname(sd_dhcp6_client *client) {
if (!client)
return NULL;
return get_ifname(client->ifindex, &client->ifname);
}
int sd_dhcp6_client_set_local_address(
sd_dhcp6_client *client,
const struct in6_addr *local_address) {
@ -1787,6 +1806,7 @@ static sd_dhcp6_client *dhcp6_client_free(sd_dhcp6_client *client) {
ordered_hashmap_free(client->extra_options);
strv_free(client->user_class);
strv_free(client->vendor_class);
free(client->ifname);
return mfree(client);
}

View File

@ -16,9 +16,9 @@
#include "ether-addr-util.h"
#include "event-util.h"
#include "fd-util.h"
#include "format-util.h"
#include "in-addr-util.h"
#include "log-link.h"
#include "network-common.h"
#include "random-util.h"
#include "siphash24.h"
#include "string-table.h"
@ -56,7 +56,7 @@ struct sd_ipv4acd {
int ifindex;
int fd;
char ifname[IF_NAMESIZE + 1];
char *ifname;
unsigned n_iteration;
unsigned n_conflict;
@ -125,7 +125,7 @@ static sd_ipv4acd *ipv4acd_free(sd_ipv4acd *acd) {
ipv4acd_reset(acd);
sd_ipv4acd_detach_event(acd);
free(acd->ifname);
return mfree(acd);
}
@ -398,16 +398,10 @@ fail:
}
int sd_ipv4acd_set_ifindex(sd_ipv4acd *acd, int ifindex) {
char ifname[IF_NAMESIZE + 1];
assert_return(acd, -EINVAL);
assert_return(ifindex > 0, -EINVAL);
assert_return(acd->state == IPV4ACD_STATE_INIT, -EBUSY);
if (!format_ifname(ifindex, ifname))
return -ENODEV;
strcpy(acd->ifname, ifname);
acd->ifindex = ifindex;
return 0;
@ -420,11 +414,21 @@ int sd_ipv4acd_get_ifindex(sd_ipv4acd *acd) {
return acd->ifindex;
}
int sd_ipv4acd_set_ifname(sd_ipv4acd *acd, const char *ifname) {
assert_return(acd, -EINVAL);
assert_return(ifname, -EINVAL);
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
return -EINVAL;
return free_and_strdup(&acd->ifname, ifname);
}
const char *sd_ipv4acd_get_ifname(sd_ipv4acd *acd) {
if (!acd)
return NULL;
return empty_to_null(acd->ifname);
return get_ifname(acd->ifindex, &acd->ifname);
}
int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr) {

View File

@ -112,6 +112,13 @@ int sd_ipv4ll_get_ifindex(sd_ipv4ll *ll) {
return sd_ipv4acd_get_ifindex(ll->acd);
}
int sd_ipv4ll_set_ifname(sd_ipv4ll *ll, const char *ifname) {
assert_return(ll, -EINVAL);
assert_return(ifname, -EINVAL);
return sd_ipv4acd_set_ifname(ll->acd, ifname);
}
const char *sd_ipv4ll_get_ifname(sd_ipv4ll *ll) {
if (!ll)
return NULL;

View File

@ -14,6 +14,7 @@
#include "lldp-neighbor.h"
#include "lldp-network.h"
#include "memory-util.h"
#include "network-common.h"
#include "socket-util.h"
#include "sort-util.h"
#include "string-table.h"
@ -343,6 +344,23 @@ _public_ int sd_lldp_set_ifindex(sd_lldp *lldp, int ifindex) {
return 0;
}
int sd_lldp_set_ifname(sd_lldp *lldp, const char *ifname) {
assert_return(lldp, -EINVAL);
assert_return(ifname, -EINVAL);
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
return -EINVAL;
return free_and_strdup(&lldp->ifname, ifname);
}
const char *sd_lldp_get_ifname(sd_lldp *lldp) {
if (!lldp)
return NULL;
return get_ifname(lldp->ifindex, &lldp->ifname);
}
static sd_lldp* lldp_free(sd_lldp *lldp) {
assert(lldp);
@ -354,6 +372,7 @@ static sd_lldp* lldp_free(sd_lldp *lldp) {
hashmap_free(lldp->neighbor_by_id);
prioq_free(lldp->neighbor_by_expiry);
free(lldp->ifname);
return mfree(lldp);
}

View File

@ -16,6 +16,7 @@
#include "memory-util.h"
#include "ndisc-internal.h"
#include "ndisc-router.h"
#include "network-common.h"
#include "random-util.h"
#include "socket-util.h"
#include "string-table.h"
@ -65,6 +66,23 @@ _public_ int sd_ndisc_set_ifindex(sd_ndisc *nd, int ifindex) {
return 0;
}
int sd_ndisc_set_ifname(sd_ndisc *nd, const char *ifname) {
assert_return(nd, -EINVAL);
assert_return(ifname, -EINVAL);
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
return -EINVAL;
return free_and_strdup(&nd->ifname, ifname);
}
const char *sd_ndisc_get_ifname(sd_ndisc *nd) {
if (!nd)
return NULL;
return get_ifname(nd->ifindex, &nd->ifname);
}
_public_ int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr) {
assert_return(nd, -EINVAL);
@ -129,6 +147,7 @@ static sd_ndisc *ndisc_free(sd_ndisc *nd) {
ndisc_reset(nd);
sd_ndisc_detach_event(nd);
free(nd->ifname);
return mfree(nd);
}

View File

@ -19,6 +19,7 @@
#include "io-util.h"
#include "macro.h"
#include "memory-util.h"
#include "network-common.h"
#include "radv-internal.h"
#include "random-util.h"
#include "socket-util.h"
@ -122,6 +123,7 @@ static sd_radv *radv_free(sd_radv *ra) {
sd_radv_detach_event(ra);
ra->fd = safe_close(ra->fd);
free(ra->ifname);
return mfree(ra);
}
@ -432,6 +434,23 @@ _public_ int sd_radv_set_ifindex(sd_radv *ra, int ifindex) {
return 0;
}
int sd_radv_set_ifname(sd_radv *ra, const char *ifname) {
assert_return(ra, -EINVAL);
assert_return(ifname, -EINVAL);
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
return -EINVAL;
return free_and_strdup(&ra->ifname, ifname);
}
const char *sd_radv_get_ifname(sd_radv *ra) {
if (!ra)
return NULL;
return get_ifname(ra->ifindex, &ra->ifname);
}
_public_ int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr) {
assert_return(ra, -EINVAL);

View File

@ -125,6 +125,10 @@ int sd_dhcp_client_set_request_broadcast(
int sd_dhcp_client_set_ifindex(
sd_dhcp_client *client,
int interface_index);
int sd_dhcp_client_set_ifname(
sd_dhcp_client *client,
const char *interface_name);
const char *sd_dhcp_client_get_ifname(sd_dhcp_client *client);
int sd_dhcp_client_set_mac(
sd_dhcp_client *client,
const uint8_t *addr,

View File

@ -37,6 +37,9 @@ enum {
int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex);
int sd_dhcp_server_set_ifname(sd_dhcp_server *server, const char *ifname);
const char *sd_dhcp_server_get_ifname(sd_dhcp_server *server);
sd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server);
sd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server);

View File

@ -91,6 +91,10 @@ int sd_dhcp6_client_set_callback(
int sd_dhcp6_client_set_ifindex(
sd_dhcp6_client *client,
int interface_index);
int sd_dhcp6_client_set_ifname(
sd_dhcp6_client *client,
const char *interface_name);
const char * sd_dhcp6_client_get_ifname(sd_dhcp6_client *client);
int sd_dhcp6_client_set_local_address(
sd_dhcp6_client *client,
const struct in6_addr *local_address);

View File

@ -44,6 +44,7 @@ int sd_ipv4acd_set_callback(sd_ipv4acd *acd, sd_ipv4acd_callback_t cb, void *use
int sd_ipv4acd_set_mac(sd_ipv4acd *acd, const struct ether_addr *addr);
int sd_ipv4acd_set_ifindex(sd_ipv4acd *acd, int interface_index);
int sd_ipv4acd_get_ifindex(sd_ipv4acd *acd);
int sd_ipv4acd_set_ifname(sd_ipv4acd *acd, const char *interface_name);
const char *sd_ipv4acd_get_ifname(sd_ipv4acd *acd);
int sd_ipv4acd_set_address(sd_ipv4acd *acd, const struct in_addr *address);
int sd_ipv4acd_is_running(sd_ipv4acd *acd);

View File

@ -44,6 +44,7 @@ int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_callback_t cb, void *userdat
int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr);
int sd_ipv4ll_set_ifindex(sd_ipv4ll *ll, int interface_index);
int sd_ipv4ll_get_ifindex(sd_ipv4ll *ll);
int sd_ipv4ll_set_ifname(sd_ipv4ll *ll, const char *interface_name);
const char *sd_ipv4ll_get_ifname(sd_ipv4ll *ll);
int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address);
int sd_ipv4ll_set_address_seed(sd_ipv4ll *ll, uint64_t seed);

View File

@ -147,6 +147,8 @@ sd_event *sd_lldp_get_event(sd_lldp *lldp);
int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_callback_t cb, void *userdata);
int sd_lldp_set_ifindex(sd_lldp *lldp, int ifindex);
int sd_lldp_set_ifname(sd_lldp *lldp, const char *ifname);
const char *sd_lldp_get_ifname(sd_lldp *lldp);
/* Controls how much and what to store in the neighbors database */
int sd_lldp_set_neighbors_max(sd_lldp *lldp, uint64_t n);

View File

@ -78,6 +78,8 @@ sd_event *sd_ndisc_get_event(sd_ndisc *nd);
int sd_ndisc_set_callback(sd_ndisc *nd, sd_ndisc_callback_t cb, void *userdata);
int sd_ndisc_set_ifindex(sd_ndisc *nd, int interface_index);
int sd_ndisc_set_ifname(sd_ndisc *nd, const char *interface_name);
const char *sd_ndisc_get_ifname(sd_ndisc *nd);
int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr);
int sd_ndisc_get_mtu(sd_ndisc *nd, uint32_t *ret);

View File

@ -53,6 +53,8 @@ int sd_radv_stop(sd_radv *ra);
int sd_radv_is_running(sd_radv *ra);
int sd_radv_set_ifindex(sd_radv *ra, int interface_index);
int sd_radv_set_ifname(sd_radv *ra, const char *interface_name);
const char *sd_radv_get_ifname(sd_radv *ra);
int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr);
int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu);
int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit);