chiark / gitweb /
networkctl: show interface names next to IP addresses if we dump adresses from all...
[elogind.git] / src / network / networkd-wait-online-link.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Lennart Poettering
7   Copyright 2014 Tom Gundersen
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <net/if.h>
24
25 #include "sd-network.h"
26 #include "strv.h"
27
28 #include "networkd-wait-online-link.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         free(l);
84         return NULL;
85  }
86
87 int link_update_rtnl(Link *l, sd_rtnl_message *m) {
88         const char *ifname;
89         int r;
90
91         assert(l);
92         assert(l->manager);
93         assert(m);
94
95         r = sd_rtnl_message_link_get_flags(m, &l->flags);
96         if (r < 0)
97                 return r;
98
99         r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname);
100         if (r < 0)
101                 return r;
102
103         if (!streq(l->ifname, ifname)) {
104                 char *new_ifname;
105
106                 new_ifname = strdup(ifname);
107                 if (!new_ifname)
108                         return -ENOMEM;
109
110                 hashmap_remove(l->manager->links_by_name, l->ifname);
111                 free(l->ifname);
112                 l->ifname = new_ifname;
113
114                 r = hashmap_put(l->manager->links_by_name, l->ifname, l);
115                 if (r < 0)
116                         return r;
117         }
118
119         return 0;
120 }
121
122 int link_update_monitor(Link *l) {
123         assert(l);
124
125         free(l->operational_state);
126         l->operational_state = NULL;
127
128         sd_network_link_get_operational_state(l->ifindex, &l->operational_state);
129
130         free(l->state);
131         l->state = NULL;
132
133         sd_network_link_get_setup_state(l->ifindex, &l->state);
134
135         return 0;
136 }
137
138 bool link_relevant(Link *l) {
139         assert(l);
140
141         if (l->flags & IFF_LOOPBACK)
142                 return false;
143
144         return true;
145 }