tethering: Add async tethering_enabled callback
[platform/upstream/connman.git] / src / tethering.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <sys/ioctl.h>
28 #include <net/if.h>
29 #include <linux/sockios.h>
30
31 #include "connman.h"
32
33 #include <connman/tethering.h>
34
35 #define BRIDGE_NAME "tether"
36
37 static connman_bool_t tethering_status = FALSE;
38 gint tethering_enabled;
39
40 connman_bool_t __connman_tethering_get_status(void)
41 {
42         return tethering_status;
43 }
44
45 static int create_bridge(const char *name)
46 {
47         int sk, err;
48
49         DBG("name %s", name);
50
51         sk = socket(AF_INET, SOCK_STREAM, 0);
52         if (sk < 0)
53                 return -EOPNOTSUPP;
54
55         err = ioctl(sk, SIOCBRADDBR, name);
56
57         close(sk);
58
59         if (err < 0)
60                 return -EOPNOTSUPP;
61
62         return 0;
63 }
64
65 static int remove_bridge(const char *name)
66 {
67         int sk, err;
68
69         DBG("name %s", name);
70
71         sk = socket(AF_INET, SOCK_STREAM, 0);
72         if (sk < 0)
73                 return -EOPNOTSUPP;
74
75         err = ioctl(sk, SIOCBRDELBR, name);
76
77         close(sk);
78
79         if (err < 0)
80                 return -EOPNOTSUPP;
81
82         return 0;
83 }
84
85 void connman_tethering_enabled(void)
86 {
87         if (tethering_status == FALSE)
88                 return;
89
90         DBG("enabled %d", tethering_enabled + 1);
91
92         if (g_atomic_int_exchange_and_add(&tethering_enabled, 1) == 0) {
93                 /* TODO Start DHCP server and DNS proxy on the bridge */
94                 DBG("tethering started");
95         }
96 }
97
98 void connman_tethering_disabled(void)
99 {
100         if (tethering_status == FALSE)
101                 return;
102
103         DBG("enabled %d", tethering_enabled - 1);
104
105         if (g_atomic_int_dec_and_test(&tethering_enabled) == 0) {
106                 /* TODO Stop DHCP server and DNS proxy on the bridge */
107                 DBG("tethering stopped");
108         }
109 }
110
111 int __connman_tethering_set_status(connman_bool_t status)
112 {
113         if (status == tethering_status)
114                 return -EALREADY;
115
116         if (status == TRUE) {
117                 create_bridge(BRIDGE_NAME);
118                 __connman_technology_enable_tethering(BRIDGE_NAME);
119         } else {
120                 __connman_technology_disable_tethering(BRIDGE_NAME);
121                 remove_bridge(BRIDGE_NAME);
122         }
123
124         tethering_status = status;
125
126         return 0;
127 }
128
129 void __connman_tethering_update_interface(const char *interface)
130 {
131         DBG("interface %s", interface);
132 }
133
134 int __connman_tethering_init(void)
135 {
136         DBG("");
137
138         tethering_enabled = 0;
139
140         return 0;
141 }
142
143 void __connman_tethering_cleanup(void)
144 {
145         DBG("");
146
147         if (tethering_status == TRUE)
148                 remove_bridge(BRIDGE_NAME);
149 }