]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/wait-online/link.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / network / wait-online / link.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Lennart Poettering
6 Copyright 2014 Tom Gundersen
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "sd-network.h"
23
24 #include "alloc-util.h"
25 #include "hashmap.h"
26 #include "link.h"
27 #include "manager.h"
28 #include "string-util.h"
29
30 int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
31 _cleanup_(link_freep) Link *l = NULL;
32 int r;
33
34 assert(m);
35 assert(ifindex > 0);
36
37 r = hashmap_ensure_allocated(&m->links, NULL);
38 if (r < 0)
39 return r;
40
41 r = hashmap_ensure_allocated(&m->links_by_name, &string_hash_ops);
42 if (r < 0)
43 return r;
44
45 l = new0(Link, 1);
46 if (!l)
47 return -ENOMEM;
48
49 l->manager = m;
50
51 l->ifname = strdup(ifname);
52 if (!l->ifname)
53 return -ENOMEM;
54
55 r = hashmap_put(m->links_by_name, l->ifname, l);
56 if (r < 0)
57 return r;
58
59 l->ifindex = ifindex;
60
61 r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
62 if (r < 0)
63 return r;
64
65 if (ret)
66 *ret = l;
67 l = NULL;
68
69 return 0;
70 }
71
72 Link *link_free(Link *l) {
73
74 if (!l)
75 return NULL;
76
77 if (l->manager) {
78 hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
79 hashmap_remove(l->manager->links_by_name, l->ifname);
80 }
81
82 free(l->ifname);
83 return mfree(l);
84 }
85
86 int link_update_rtnl(Link *l, sd_netlink_message *m) {
87 const char *ifname;
88 int r;
89
90 assert(l);
91 assert(l->manager);
92 assert(m);
93
94 r = sd_rtnl_message_link_get_flags(m, &l->flags);
95 if (r < 0)
96 return r;
97
98 r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname);
99 if (r < 0)
100 return r;
101
102 if (!streq(l->ifname, ifname)) {
103 char *new_ifname;
104
105 new_ifname = strdup(ifname);
106 if (!new_ifname)
107 return -ENOMEM;
108
109 hashmap_remove(l->manager->links_by_name, l->ifname);
110 free(l->ifname);
111 l->ifname = new_ifname;
112
113 r = hashmap_put(l->manager->links_by_name, l->ifname, l);
114 if (r < 0)
115 return r;
116 }
117
118 return 0;
119 }
120
121 int link_update_monitor(Link *l) {
122 assert(l);
123
124 l->operational_state = mfree(l->operational_state);
125
126 sd_network_link_get_operational_state(l->ifindex, &l->operational_state);
127
128 l->state = mfree(l->state);
129
130 sd_network_link_get_setup_state(l->ifindex, &l->state);
131
132 return 0;
133 }