Add initial bits and pieces for Tethering support
[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 #define BRIDGE_NAME "tether"
34
35 static connman_bool_t tethering_status = FALSE;
36
37 connman_bool_t __connman_tethering_get_status(void)
38 {
39         return tethering_status;
40 }
41
42 static int create_bridge(const char *name)
43 {
44         int sk, err;
45
46         DBG("name %s", name);
47
48         sk = socket(AF_INET, SOCK_STREAM, 0);
49         if (sk < 0)
50                 return -EOPNOTSUPP;
51
52         err = ioctl(sk, SIOCBRADDBR, name);
53
54         close(sk);
55
56         if (err < 0)
57                 return -EOPNOTSUPP;
58
59         return 0;
60 }
61
62 static int remove_bridge(const char *name)
63 {
64         int sk, err;
65
66         DBG("name %s", name);
67
68         sk = socket(AF_INET, SOCK_STREAM, 0);
69         if (sk < 0)
70                 return -EOPNOTSUPP;
71
72         err = ioctl(sk, SIOCBRDELBR, name);
73
74         close(sk);
75
76         if (err < 0)
77                 return -EOPNOTSUPP;
78
79         return 0;
80 }
81
82 int __connman_tethering_set_status(connman_bool_t status)
83 {
84         if (status == tethering_status)
85                 return -EALREADY;
86
87         if (status == TRUE)
88                 create_bridge(BRIDGE_NAME);
89         else
90                 remove_bridge(BRIDGE_NAME);
91
92         tethering_status = status;
93
94         return 0;
95 }
96
97 void __connman_tethering_update_interface(const char *interface)
98 {
99         DBG("interface %s", interface);
100 }
101
102 int __connman_tethering_init(void)
103 {
104         DBG("");
105
106         return 0;
107 }
108
109 void __connman_tethering_cleanup(void)
110 {
111         DBG("");
112
113         if (tethering_status == TRUE)
114                 remove_bridge(BRIDGE_NAME);
115 }