bridge: Move bridge code into a seperate file
authorDaniel Wagner <daniel.wagner@bmw-carit.de>
Wed, 1 Feb 2012 17:54:48 +0000 (18:54 +0100)
committerSamuel Ortiz <sameo@linux.intel.com>
Mon, 13 Feb 2012 10:33:17 +0000 (11:33 +0100)
The bridge handling code will reused by session.c.

Makefile.am
src/bridge.c [new file with mode: 0644]
src/connman.h
src/tethering.c

index 03776ad..5a70f0c 100644 (file)
@@ -80,7 +80,7 @@ src_connmand_SOURCES = $(gdbus_sources) $(gdhcp_sources) \
                        src/technology.c src/counter.c src/ntp.c \
                        src/session.c src/tethering.c src/wpad.c src/wispr.c \
                        src/stats.c src/iptables.c src/dnsproxy.c src/6to4.c \
-                       src/ippool.c
+                       src/ippool.c src/bridge.c
 
 src_connmand_LDADD = $(builtin_libadd) @GLIB_LIBS@ @DBUS_LIBS@ \
                                @CAPNG_LIBS@ @XTABLES_LIBS@ -lresolv -ldl
diff --git a/src/bridge.c b/src/bridge.c
new file mode 100644 (file)
index 0000000..da1622f
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2012  BMW Car IT GmbH. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <linux/sockios.h>
+#include <string.h>
+#include <fcntl.h>
+#include <linux/if_tun.h>
+
+#include "connman.h"
+
+static int set_forward_delay(const char *name, unsigned int delay)
+{
+       FILE *f;
+       char *forward_delay_path;
+
+       forward_delay_path =
+               g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
+
+       if (forward_delay_path == NULL)
+               return -ENOMEM;
+
+       f = fopen(forward_delay_path, "r+");
+
+       g_free(forward_delay_path);
+
+       if (f == NULL)
+               return -errno;
+
+       fprintf(f, "%d", delay);
+
+       fclose(f);
+
+       return 0;
+}
+
+int __connman_bridge_create(const char *name)
+{
+       int sk, err;
+
+       DBG("name %s", name);
+
+       sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
+       if (sk < 0)
+               return -EOPNOTSUPP;
+
+       if (ioctl(sk, SIOCBRADDBR, name) == -1) {
+               err = -errno;
+               if (err != -EEXIST)
+                       return -EOPNOTSUPP;
+       }
+
+       err = set_forward_delay(name, 0);
+
+       if (err < 0)
+               ioctl(sk, SIOCBRDELBR, name);
+
+       close(sk);
+
+       return err;
+}
+
+int __connman_bridge_remove(const char *name)
+{
+       int sk, err;
+
+       DBG("name %s", name);
+
+       sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
+       if (sk < 0)
+               return -EOPNOTSUPP;
+
+       err = ioctl(sk, SIOCBRDELBR, name);
+
+       close(sk);
+
+       if (err < 0)
+               return -EOPNOTSUPP;
+
+       return 0;
+}
+
+int __connman_bridge_enable(const char *name, const char *broadcast,
+                               const char *gateway)
+{
+       int err, index;
+
+       index = connman_inet_ifindex(name);
+       if (index < 0)
+               return index;
+
+       err = __connman_inet_modify_address(RTM_NEWADDR,
+                       NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
+                                       gateway, NULL, 24, broadcast);
+       if (err < 0)
+               return err;
+
+       return connman_inet_ifup(index);
+}
+
+int __connman_bridge_disable(const char *name)
+{
+       int index;
+
+       index = connman_inet_ifindex(name);
+       if (index < 0)
+               return index;
+
+       return connman_inet_ifdown(index);
+}
index dcde0f3..ec35afc 100644 (file)
@@ -744,3 +744,9 @@ const char *__connman_ippool_get_end_ip(struct connman_ippool *pool);
 
 void __connman_ippool_newaddr(int index, const char *address);
 void __connman_ippool_deladdr(int index, const char *address);
+
+int __connman_bridge_create(const char *name);
+int __connman_bridge_remove(const char *name);
+int __connman_bridge_enable(const char *name, const char *broadcast,
+                               const char *gateway);
+int __connman_bridge_disable(const char *name);
index b135b45..c240302 100644 (file)
@@ -167,106 +167,6 @@ static void dhcp_server_stop(GDHCPServer *server)
        g_dhcp_server_unref(server);
 }
 
-static int set_forward_delay(const char *name, unsigned int delay)
-{
-       FILE *f;
-       char *forward_delay_path;
-
-       forward_delay_path =
-               g_strdup_printf("/sys/class/net/%s/bridge/forward_delay", name);
-
-       if (forward_delay_path == NULL)
-               return -ENOMEM;
-
-       f = fopen(forward_delay_path, "r+");
-
-       g_free(forward_delay_path);
-
-       if (f == NULL)
-               return -errno;
-
-       fprintf(f, "%d", delay);
-
-       fclose(f);
-
-       return 0;
-}
-
-static int create_bridge(const char *name)
-{
-       int sk, err;
-
-       DBG("name %s", name);
-
-       sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
-       if (sk < 0)
-               return -EOPNOTSUPP;
-
-       if (ioctl(sk, SIOCBRADDBR, name) == -1) {
-               err = -errno;
-               if (err != -EEXIST)
-                       return -EOPNOTSUPP;
-       }
-
-       err = set_forward_delay(name, 0);
-
-       if (err < 0)
-               ioctl(sk, SIOCBRDELBR, name);
-
-       close(sk);
-
-       return err;
-}
-
-static int remove_bridge(const char *name)
-{
-       int sk, err;
-
-       DBG("name %s", name);
-
-       sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
-       if (sk < 0)
-               return -EOPNOTSUPP;
-
-       err = ioctl(sk, SIOCBRDELBR, name);
-
-       close(sk);
-
-       if (err < 0)
-               return -EOPNOTSUPP;
-
-       return 0;
-}
-
-static int enable_bridge(const char *name, const char *broadcast,
-                               const char *gateway)
-{
-       int err, index;
-
-       index = connman_inet_ifindex(name);
-       if (index < 0)
-               return index;
-
-       err = __connman_inet_modify_address(RTM_NEWADDR,
-                       NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
-                                       gateway, NULL, 24, broadcast);
-       if (err < 0)
-               return err;
-
-       return connman_inet_ifup(index);
-}
-
-static int disable_bridge(const char *name)
-{
-       int index;
-
-       index = connman_inet_ifindex(name);
-       if (index < 0)
-               return index;
-
-       return connman_inet_ifdown(index);
-}
-
 static int enable_ip_forward(connman_bool_t enable)
 {
 
@@ -349,7 +249,7 @@ void __connman_tethering_set_enabled(void)
        if (__sync_fetch_and_add(&tethering_enabled, 1) != 0)
                return;
 
-       err = create_bridge(BRIDGE_NAME);
+       err = __connman_bridge_create(BRIDGE_NAME);
        if (err < 0)
                return;
 
@@ -367,9 +267,9 @@ void __connman_tethering_set_enabled(void)
        start_ip = __connman_ippool_get_start_ip(dhcp_ippool);
        end_ip = __connman_ippool_get_end_ip(dhcp_ippool);
 
-       err = enable_bridge(BRIDGE_NAME, gateway, broadcast);
+       err = __connman_bridge_enable(BRIDGE_NAME, gateway, broadcast);
        if (err < 0 && err != -EALREADY) {
-               remove_bridge(BRIDGE_NAME);
+               __connman_bridge_remove(BRIDGE_NAME);
                return;
        }
 
@@ -385,8 +285,8 @@ void __connman_tethering_set_enabled(void)
                                                start_ip, end_ip,
                                                24 * 3600, dns);
        if (tethering_dhcp_server == NULL) {
-               disable_bridge(BRIDGE_NAME);
-               remove_bridge(BRIDGE_NAME);
+               __connman_bridge_disable(BRIDGE_NAME);
+               __connman_bridge_remove(BRIDGE_NAME);
                return;
        }
 
@@ -410,11 +310,11 @@ void __connman_tethering_set_disabled(void)
 
        tethering_dhcp_server = NULL;
 
-       disable_bridge(BRIDGE_NAME);
+       __connman_bridge_disable(BRIDGE_NAME);
 
        __connman_ippool_unref(dhcp_ippool);
 
-       remove_bridge(BRIDGE_NAME);
+       __connman_bridge_remove(BRIDGE_NAME);
 
        DBG("tethering stopped");
 }
@@ -661,8 +561,8 @@ void __connman_tethering_cleanup(void)
        if (tethering_enabled == 0) {
                if (tethering_dhcp_server)
                        dhcp_server_stop(tethering_dhcp_server);
-               disable_bridge(BRIDGE_NAME);
-               remove_bridge(BRIDGE_NAME);
+               __connman_bridge_disable(BRIDGE_NAME);
+               __connman_bridge_remove(BRIDGE_NAME);
        }
 
        if (connection == NULL)