]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7de12ae7 | 2 | /*** |
96b2fb93 | 3 | Copyright © 2014 Tom Gundersen |
7de12ae7 TG |
4 | ***/ |
5 | ||
7de12ae7 | 6 | #include "sd-network.h" |
7de12ae7 | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
c5fcf6e4 TG |
9 | #include "hashmap.h" |
10 | #include "link.h" | |
11 | #include "manager.h" | |
07630cea | 12 | #include "string-util.h" |
7de12ae7 TG |
13 | |
14 | int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) { | |
15 | _cleanup_(link_freep) Link *l = NULL; | |
16 | int r; | |
17 | ||
18 | assert(m); | |
19 | assert(ifindex > 0); | |
20 | ||
d5099efc | 21 | r = hashmap_ensure_allocated(&m->links, NULL); |
7de12ae7 TG |
22 | if (r < 0) |
23 | return r; | |
24 | ||
d5099efc | 25 | r = hashmap_ensure_allocated(&m->links_by_name, &string_hash_ops); |
7de12ae7 TG |
26 | if (r < 0) |
27 | return r; | |
28 | ||
29 | l = new0(Link, 1); | |
30 | if (!l) | |
31 | return -ENOMEM; | |
32 | ||
33 | l->manager = m; | |
34 | ||
35 | l->ifname = strdup(ifname); | |
36 | if (!l->ifname) | |
37 | return -ENOMEM; | |
38 | ||
39 | r = hashmap_put(m->links_by_name, l->ifname, l); | |
40 | if (r < 0) | |
41 | return r; | |
42 | ||
43 | l->ifindex = ifindex; | |
44 | ||
45 | r = hashmap_put(m->links, INT_TO_PTR(ifindex), l); | |
46 | if (r < 0) | |
47 | return r; | |
48 | ||
49 | if (ret) | |
50 | *ret = l; | |
51 | l = NULL; | |
52 | ||
53 | return 0; | |
54 | } | |
55 | ||
56 | Link *link_free(Link *l) { | |
57 | ||
58 | if (!l) | |
59 | return NULL; | |
60 | ||
61 | if (l->manager) { | |
62 | hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex)); | |
63 | hashmap_remove(l->manager->links_by_name, l->ifname); | |
64 | } | |
65 | ||
66 | free(l->ifname); | |
6b430fdb | 67 | return mfree(l); |
7de12ae7 TG |
68 | } |
69 | ||
1c4baffc | 70 | int link_update_rtnl(Link *l, sd_netlink_message *m) { |
d2437732 | 71 | const char *ifname; |
7de12ae7 TG |
72 | int r; |
73 | ||
74 | assert(l); | |
75 | assert(l->manager); | |
76 | assert(m); | |
77 | ||
78 | r = sd_rtnl_message_link_get_flags(m, &l->flags); | |
79 | if (r < 0) | |
80 | return r; | |
81 | ||
1c4baffc | 82 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
7de12ae7 TG |
83 | if (r < 0) |
84 | return r; | |
85 | ||
86 | if (!streq(l->ifname, ifname)) { | |
87 | char *new_ifname; | |
88 | ||
89 | new_ifname = strdup(ifname); | |
90 | if (!new_ifname) | |
91 | return -ENOMEM; | |
92 | ||
93 | hashmap_remove(l->manager->links_by_name, l->ifname); | |
94 | free(l->ifname); | |
95 | l->ifname = new_ifname; | |
96 | ||
97 | r = hashmap_put(l->manager->links_by_name, l->ifname, l); | |
98 | if (r < 0) | |
99 | return r; | |
100 | } | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
105 | int link_update_monitor(Link *l) { | |
106 | assert(l); | |
107 | ||
c1a38904 MTL |
108 | l->required_for_online = sd_network_link_get_required_for_online(l->ifindex) != 0; |
109 | ||
a1e58e8e | 110 | l->operational_state = mfree(l->operational_state); |
7de12ae7 | 111 | |
d6731e4c | 112 | sd_network_link_get_operational_state(l->ifindex, &l->operational_state); |
7de12ae7 | 113 | |
a1e58e8e | 114 | l->state = mfree(l->state); |
7de12ae7 | 115 | |
438ca2bb | 116 | sd_network_link_get_setup_state(l->ifindex, &l->state); |
7de12ae7 TG |
117 | |
118 | return 0; | |
119 | } |