Moved data structures to new collections subfolder
[strongswan.git] / src / dumm / bridge.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <sys/types.h>
17 #include <libbridge.h>
18
19 #include <debug.h>
20 #include <collections/linked_list.h>
21
22 #include "bridge.h"
23
24 typedef struct private_bridge_t private_bridge_t;
25
26 struct private_bridge_t {
27 /** public interface */
28 bridge_t public;
29 /** device name */
30 char *name;
31 /** list of attached interfaces */
32 linked_list_t *ifaces;
33 };
34
35 /**
36 * defined in iface.c
37 */
38 bool iface_control(char *name, bool up);
39
40 METHOD(bridge_t, get_name, char*,
41 private_bridge_t *this)
42 {
43 return this->name;
44 }
45
46 METHOD(bridge_t, create_iface_enumerator, enumerator_t*,
47 private_bridge_t *this)
48 {
49 return this->ifaces->create_enumerator(this->ifaces);
50 }
51
52 METHOD(bridge_t, disconnect_iface, bool,
53 private_bridge_t *this, iface_t *iface)
54 {
55 enumerator_t *enumerator;
56 iface_t *current = NULL;
57 bool good = FALSE;
58
59 enumerator = this->ifaces->create_enumerator(this->ifaces);
60 while (enumerator->enumerate(enumerator, (void**)&current))
61 {
62 if (current == iface)
63 {
64 if (br_del_interface(this->name, iface->get_hostif(iface)) != 0)
65 {
66 DBG1(DBG_LIB, "removing iface '%s' from bridge '%s' in kernel"
67 " failed: %m", iface->get_hostif(iface), this->name);
68 }
69 else
70 {
71 iface->set_bridge(iface, NULL);
72 this->ifaces->remove_at(this->ifaces, enumerator);
73 good = TRUE;
74 }
75 break;
76 }
77 }
78 if (iface != current)
79 {
80 DBG1(DBG_LIB, "iface '%s' not found on bridge '%s'",
81 iface->get_hostif(iface), this->name);
82 }
83 enumerator->destroy(enumerator);
84 return good;
85 }
86
87 METHOD(bridge_t, connect_iface, bool,
88 private_bridge_t *this, iface_t *iface)
89 {
90 if (br_add_interface(this->name, iface->get_hostif(iface)) != 0)
91 {
92 DBG1(DBG_LIB, "adding iface '%s' to bridge '%s' failed: %m",
93 iface->get_hostif(iface), this->name);
94 return FALSE;
95 }
96 iface->set_bridge(iface, &this->public);
97 this->ifaces->insert_last(this->ifaces, iface);
98 return TRUE;
99 }
100
101 /**
102 * instance counter to (de-)initialize libbridge
103 */
104 static int instances = 0;
105
106 METHOD(bridge_t, destroy, void,
107 private_bridge_t *this)
108 {
109 enumerator_t *enumerator;
110 iface_t *iface;
111
112 enumerator = this->ifaces->create_enumerator(this->ifaces);
113 while (enumerator->enumerate(enumerator, (void**)&iface))
114 {
115 if (br_del_interface(this->name, iface->get_hostif(iface)) != 0)
116 {
117 DBG1(DBG_LIB, "disconnecting iface '%s' failed: %m",
118 iface->get_hostif(iface));
119 }
120 iface->set_bridge(iface, NULL);
121 }
122 enumerator->destroy(enumerator);
123 this->ifaces->destroy(this->ifaces);
124 iface_control(this->name, FALSE);
125 if (br_del_bridge(this->name) != 0)
126 {
127 DBG1(DBG_LIB, "deleting bridge '%s' from kernel failed: %m",
128 this->name);
129 }
130 free(this->name);
131 free(this);
132 if (--instances == 0)
133 {
134 br_shutdown();
135 }
136 }
137
138 /**
139 * create the bridge instance
140 */
141 bridge_t *bridge_create(char *name)
142 {
143 private_bridge_t *this;
144
145 if (instances == 0)
146 {
147 if (br_init() != 0)
148 {
149 DBG1(DBG_LIB, "libbridge initialization failed: %m");
150 return NULL;
151 }
152 }
153
154 INIT(this,
155 .public = {
156 .get_name = _get_name,
157 .create_iface_enumerator = _create_iface_enumerator,
158 .disconnect_iface = _disconnect_iface,
159 .connect_iface = _connect_iface,
160 .destroy = _destroy,
161 }
162 );
163
164 if (br_add_bridge(name) != 0)
165 {
166 DBG1(DBG_LIB, "creating bridge '%s' failed: %m", name);
167 free(this);
168 return NULL;
169 }
170 if (!iface_control(name, TRUE))
171 {
172 DBG1(DBG_LIB, "bringing bridge '%s' up failed: %m", name);
173 }
174
175 this->name = strdup(name);
176 this->ifaces = linked_list_create();
177
178 instances++;
179 return &this->public;
180 }
181