split alias support into a separate source file for better readability
[project/netifd.git] / alias.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include "netifd.h"
19 #include "device.h"
20
21 static struct avl_tree aliases;
22
23 struct alias_device {
24 struct avl_node avl;
25 struct device dev;
26 struct device_user dep;
27 bool cleanup;
28 char name[];
29 };
30
31 static const struct device_type alias_device_type;
32
33 static int
34 alias_device_set_state(struct device *dev, bool state)
35 {
36 struct alias_device *alias;
37
38 alias = container_of(dev, struct alias_device, dev);
39 if (!alias->dep.dev)
40 return -1;
41
42 if (state)
43 return device_claim(&alias->dep);
44
45 device_release(&alias->dep);
46 if (alias->cleanup)
47 device_remove_user(&alias->dep);
48 return 0;
49 }
50
51 static struct device *
52 alias_device_create(const char *name, struct blob_attr *attr)
53 {
54 struct alias_device *alias;
55
56 alias = calloc(1, sizeof(*alias) + strlen(name) + 1);
57 strcpy(alias->name, name);
58 alias->dev.set_state = alias_device_set_state;
59 alias->dev.hidden = true;
60 device_init_virtual(&alias->dev, &alias_device_type, NULL);
61 alias->avl.key = alias->name;
62 avl_insert(&aliases, &alias->avl);
63
64 return &alias->dev;
65 }
66
67 static void alias_device_free(struct device *dev)
68 {
69 struct alias_device *alias;
70
71 alias = container_of(dev, struct alias_device, dev);
72 avl_delete(&aliases, &alias->avl);
73 free(alias);
74 }
75
76 static const struct device_type alias_device_type = {
77 .name = "Network alias",
78 .create = alias_device_create,
79 .free = alias_device_free,
80 };
81
82 void
83 alias_notify_device(const char *name, struct device *dev)
84 {
85 struct alias_device *alias;
86
87 device_lock();
88
89 alias = avl_find_element(&aliases, name, alias, avl);
90 if (!alias)
91 return;
92
93 alias->cleanup = !dev;
94 if (dev) {
95 if (dev != alias->dep.dev) {
96 device_remove_user(&alias->dep);
97 strcpy(alias->dev.ifname, dev->ifname);
98 device_add_user(&alias->dep, dev);
99 alias->dev.hidden = false;
100 device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME);
101 }
102 }
103
104 device_set_present(&alias->dev, !!dev);
105
106 if (!dev && alias->dep.dev && !alias->dep.dev->active) {
107 device_remove_user(&alias->dep);
108 alias->dev.hidden = true;
109 alias->dev.ifname[0] = 0;
110 device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME);
111 }
112
113 device_unlock();
114 }
115
116 struct device *
117 device_alias_get(const char *name)
118 {
119 struct alias_device *alias;
120
121 alias = avl_find_element(&aliases, name, alias, avl);
122 if (alias)
123 return &alias->dev;
124
125 return alias_device_create(name, NULL);
126 }
127
128 static void __init alias_init(void)
129 {
130 avl_init(&aliases, avl_strcmp, false, NULL);
131 }