summaryrefslogtreecommitdiffstats
path: root/examples/private-network.c
blob: 5d5705dc89e3a9f11173df058620ee1c9fbee2d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2011  Nokia Corporation and/or its subsidiary(-ies).
 *
 *  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 <string.h>
#include <glib.h>

#include <fcntl.h>
#include <errno.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdio.h>

#define OFONO_API_SUBJECT_TO_CHANGE

#include <ofono/modem.h>
#include <ofono/types.h>
#include <ofono/plugin.h>
#include <ofono/log.h>
#include <ofono/private-network.h>

#define SERVER_ADDRESS		"192.168.1.1"
#define DNS_SERVER_1		"10.10.10.10"
#define DNS_SERVER_2		"10.10.10.11"
#define PEER_ADDRESS_PREFIX	"192.168.1."

static int next_peer = 2;

struct req_data {
	ofono_private_network_cb_t cb;
	void *userdata;
};

static gboolean request_cb(gpointer data)
{
	struct req_data *rd = data;
	struct ofono_private_network_settings pns;
	struct ifreq ifr;
	int fd, err;
	char ip[16];

	fd = open("/dev/net/tun", O_RDWR);
	if (fd < 0)
		goto error;

	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
	strcpy(ifr.ifr_name, "ppp%d");

	err = ioctl(fd, TUNSETIFF, (void *) &ifr);
	if (err < 0)
		goto error;

	sprintf(ip, "%s%d", PEER_ADDRESS_PREFIX, next_peer++);

	pns.fd = fd;
	pns.server_ip = SERVER_ADDRESS;
	pns.peer_ip = ip;
	pns.primary_dns = DNS_SERVER_1;
	pns.secondary_dns = DNS_SERVER_2;

	rd->cb(&pns, rd->userdata);

	return FALSE;

error:
	if (fd >= 0)
		close(fd);

	rd->cb(NULL, rd->userdata);

	return FALSE;
}

static int example_request(ofono_private_network_cb_t cb, void *data)
{
	struct req_data *rd = g_new0(struct req_data, 1);

	DBG("");

	rd->cb = cb;
	rd->userdata = data;

	return g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, 2, request_cb,
						rd, (GDestroyNotify) g_free);
}

static void example_release(int id)
{
	DBG("");

	g_source_remove(id);
}

static struct ofono_private_network_driver example_driver = {
	.name		= "Example Private Network Driver",
	.request	= example_request,
	.release	= example_release,
};

static int example_private_network_init(void)
{
	return ofono_private_network_driver_register(&example_driver);
}

static void example_private_network_exit(void)
{
	ofono_private_network_driver_unregister(&example_driver);
}

OFONO_PLUGIN_DEFINE(example_private_network, "Example Private Network Plugin",
			VERSION, OFONO_PLUGIN_PRIORITY_LOW,
			example_private_network_init,
			example_private_network_exit)