prototype of dumm GUI
[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 <utils/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 /**
41 * Implementation of bridge_t.get_name.
42 */
43 static char* get_name(private_bridge_t *this)
44 {
45 return this->name;
46 }
47
48 /**
49 * Implementation of bridge_t.create_iface_enumerator.
50 */
51 static enumerator_t* create_iface_enumerator(private_bridge_t *this)
52 {
53 return this->ifaces->create_enumerator(this->ifaces);
54 }
55
56 /**
57 * Implementation of bridge_t.disconnect_iface.
58 */
59 static bool disconnect_iface(private_bridge_t *this, iface_t *iface)
60 {
61 enumerator_t *enumerator;
62 iface_t *current;
63 bool good = FALSE;
64
65 enumerator = this->ifaces->create_enumerator(this->ifaces);
66 while (enumerator->enumerate(enumerator, (void**)&current))
67 {
68 if (current == iface)
69 {
70 if (br_del_interface(this->name, iface->get_hostif(iface)) != 0)
71 {
72 DBG1("removing iface '%s' from bridge '%s' in kernel failed: %m",
73 iface->get_hostif(iface), this->name);
74 }
75 else
76 {
77 iface->set_bridge(iface, NULL);
78 good = TRUE;
79 }
80 break;
81 }
82 }
83 if (iface != current)
84 {
85 DBG1("iface '%s' not found on bridge '%s'", iface->get_hostif(iface),
86 this->name);
87 }
88 enumerator->destroy(enumerator);
89 return good;
90 }
91
92 /**
93 * Implementation of bridge_t.connect_iface.
94 */
95 static bool connect_iface(private_bridge_t *this, iface_t *iface)
96 {
97 if (br_add_interface(this->name, iface->get_hostif(iface)) != 0)
98 {
99 DBG1("adding iface '%s' to bridge '%s' failed: %m",
100 iface->get_hostif(iface), this->name);
101 return FALSE;
102 }
103 iface->set_bridge(iface, &this->public);
104 this->ifaces->insert_last(this->ifaces, iface);
105 return TRUE;
106 }
107
108 /**
109 * instance counter to (de-)initialize libbridge
110 */
111 static int instances = 0;
112
113 /**
114 * unregister an interface from bridge
115 */
116 static void unregister(iface_t *iface)
117 {
118 iface->set_bridge(iface, NULL);
119 }
120
121 /**
122 * Implementation of bridge_t.destroy.
123 */
124 static void destroy(private_bridge_t *this)
125 {
126 this->ifaces->invoke_function(this->ifaces, (linked_list_invoke_t)unregister);
127 this->ifaces->destroy(this->ifaces);
128 iface_control(this->name, FALSE);
129 if (br_del_bridge(this->name) != 0)
130 {
131 DBG1("deleting bridge '%s' from kernel failed: %m", this->name);
132 }
133 free(this->name);
134 free(this);
135 if (--instances == 0)
136 {
137 br_shutdown();
138 }
139 }
140
141 /**
142 * create the bridge instance
143 */
144 bridge_t *bridge_create(char *name)
145 {
146 private_bridge_t *this;
147
148 if (instances == 0)
149 {
150 if (br_init() != 0)
151 {
152 DBG1("libbridge initialization failed: %m");
153 return NULL;
154 }
155 }
156
157 this = malloc_thing(private_bridge_t);
158 this->public.get_name = (char*(*)(bridge_t*))get_name;
159 this->public.create_iface_enumerator = (enumerator_t*(*)(bridge_t*))create_iface_enumerator;
160 this->public.disconnect_iface = (bool(*)(bridge_t*, iface_t *iface))disconnect_iface;
161 this->public.connect_iface = (bool(*)(bridge_t*, iface_t *iface))connect_iface;
162 this->public.destroy = (void*)destroy;
163
164 if (br_add_bridge(name) != 0)
165 {
166 DBG1("creating bridge '%s' failed: %m", name);
167 free(this);
168 return NULL;
169 }
170 if (!iface_control(name, TRUE))
171 {
172 DBG1("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