2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
16 #include <sys/types.h>
17 #include <libbridge.h>
20 #include <utils/linked_list.h>
24 typedef struct private_bridge_t private_bridge_t
;
26 struct private_bridge_t
{
27 /** public interface */
31 /** list of attached interfaces */
32 linked_list_t
*ifaces
;
36 * Implementation of bridge_t.get_name.
38 static char* get_name(private_bridge_t
*this)
44 * Implementation of bridge_t.create_iface_iterator.
46 static iterator_t
* create_iface_iterator(private_bridge_t
*this)
48 return this->ifaces
->create_iterator(this->ifaces
, TRUE
);
52 * Implementation of bridge_t.disconnect_iface.
54 static bool disconnect_iface(private_bridge_t
*this, iface_t
*iface
)
60 iterator
= this->ifaces
->create_iterator(this->ifaces
, TRUE
);
61 while (iterator
->iterate(iterator
, (void**)¤t
))
65 if (br_del_interface(this->name
, iface
->get_hostif(iface
)) != 0)
67 DBG1("removing iface '%s' from bridge '%s' in kernel failed: %m",
68 iface
->get_hostif(iface
), this->name
);
72 iface
->set_bridge(iface
, NULL
);
80 DBG1("iface '%s' not found on bridge '%s'", iface
->get_hostif(iface
),
83 iterator
->destroy(iterator
);
88 * Implementation of bridge_t.connect_iface.
90 static bool connect_iface(private_bridge_t
*this, iface_t
*iface
)
92 if (br_add_interface(this->name
, iface
->get_hostif(iface
)) != 0)
94 DBG1("adding iface '%s' to bridge '%s' failed: %m",
95 iface
->get_hostif(iface
), this->name
);
98 iface
->set_bridge(iface
, &this->public);
99 this->ifaces
->insert_last(this->ifaces
, iface
);
104 * instance counter to (de-)initialize libbridge
106 static int instances
= 0;
109 * unregister an interface from bridge
111 static void unregister(iface_t
*iface
)
113 iface
->set_bridge(iface
, NULL
);
117 * Implementation of bridge_t.destroy.
119 static void destroy(private_bridge_t
*this)
121 this->ifaces
->invoke_function(this->ifaces
, (void(*)(void*))unregister
);
122 this->ifaces
->destroy(this->ifaces
);
123 if (br_del_bridge(this->name
) != 0)
125 DBG1("deleting bridge '%s' from kernel failed: %m", this->name
);
129 if (--instances
== 0)
136 * create the bridge instance
138 bridge_t
*bridge_create(char *name
)
140 private_bridge_t
*this;
146 DBG1("libbridge initialization failed: %m");
151 this = malloc_thing(private_bridge_t
);
152 this->public.get_name
= (char*(*)(bridge_t
*))get_name
;
153 this->public.create_iface_iterator
= (iterator_t
*(*)(bridge_t
*))create_iface_iterator
;
154 this->public.disconnect_iface
= (bool(*)(bridge_t
*, iface_t
*iface
))disconnect_iface
;
155 this->public.connect_iface
= (bool(*)(bridge_t
*, iface_t
*iface
))connect_iface
;
156 this->public.destroy
= (void*)destroy
;
158 if (br_add_bridge(name
) != 0)
160 DBG1("creating bridge '%s' failed: %m", name
);
165 this->name
= strdup(name
);
166 this->ifaces
= linked_list_create();
169 return &this->public;