]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7de12ae7 TG |
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 | ||
7de12ae7 | 22 | #include "sd-network.h" |
7de12ae7 | 23 | |
b5efdb8a | 24 | #include "alloc-util.h" |
c5fcf6e4 TG |
25 | #include "hashmap.h" |
26 | #include "link.h" | |
27 | #include "manager.h" | |
07630cea | 28 | #include "string-util.h" |
7de12ae7 TG |
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 | ||
d5099efc | 37 | r = hashmap_ensure_allocated(&m->links, NULL); |
7de12ae7 TG |
38 | if (r < 0) |
39 | return r; | |
40 | ||
d5099efc | 41 | r = hashmap_ensure_allocated(&m->links_by_name, &string_hash_ops); |
7de12ae7 TG |
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); | |
6b430fdb | 83 | return mfree(l); |
7de12ae7 TG |
84 | } |
85 | ||
1c4baffc | 86 | int link_update_rtnl(Link *l, sd_netlink_message *m) { |
d2437732 | 87 | const char *ifname; |
7de12ae7 TG |
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 | ||
1c4baffc | 98 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
7de12ae7 TG |
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 | ||
a1e58e8e | 124 | l->operational_state = mfree(l->operational_state); |
7de12ae7 | 125 | |
d6731e4c | 126 | sd_network_link_get_operational_state(l->ifindex, &l->operational_state); |
7de12ae7 | 127 | |
a1e58e8e | 128 | l->state = mfree(l->state); |
7de12ae7 | 129 | |
438ca2bb | 130 | sd_network_link_get_setup_state(l->ifindex, &l->state); |
7de12ae7 TG |
131 | |
132 | return 0; | |
133 | } |