atutil: Add destroy notify to sim_state_query
[platform/upstream/ofono.git] / examples / private-network.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2011  Nokia Corporation and/or its subsidiary(-ies).
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 <string.h>
27 #include <glib.h>
28
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <net/if.h>
32 #include <linux/if_tun.h>
33 #include <unistd.h>
34 #include <sys/ioctl.h>
35 #include <stdio.h>
36
37 #define OFONO_API_SUBJECT_TO_CHANGE
38
39 #include <ofono/modem.h>
40 #include <ofono/types.h>
41 #include <ofono/plugin.h>
42 #include <ofono/log.h>
43 #include <ofono/private-network.h>
44
45 #define SERVER_ADDRESS          "192.168.1.1"
46 #define DNS_SERVER_1            "10.10.10.10"
47 #define DNS_SERVER_2            "10.10.10.11"
48 #define PEER_ADDRESS_PREFIX     "192.168.1."
49
50 static int next_peer = 2;
51
52 struct req_data {
53         ofono_private_network_cb_t cb;
54         void *userdata;
55 };
56
57 static gboolean request_cb(gpointer data)
58 {
59         struct req_data *rd = data;
60         struct ofono_private_network_settings pns;
61         struct ifreq ifr;
62         int fd, err;
63         char ip[16];
64
65         fd = open("/dev/net/tun", O_RDWR);
66         if (fd < 0)
67                 goto error;
68
69         memset(&ifr, 0, sizeof(ifr));
70         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
71         strcpy(ifr.ifr_name, "ppp%d");
72
73         err = ioctl(fd, TUNSETIFF, (void *) &ifr);
74         if (err < 0)
75                 goto error;
76
77         sprintf(ip, "%s%d", PEER_ADDRESS_PREFIX, next_peer++);
78
79         pns.fd = fd;
80         pns.server_ip = SERVER_ADDRESS;
81         pns.peer_ip = ip;
82         pns.primary_dns = DNS_SERVER_1;
83         pns.secondary_dns = DNS_SERVER_2;
84
85         rd->cb(&pns, rd->userdata);
86
87         return FALSE;
88
89 error:
90         if (fd >= 0)
91                 close(fd);
92
93         rd->cb(NULL, rd->userdata);
94
95         return FALSE;
96 }
97
98 static int example_request(ofono_private_network_cb_t cb, void *data)
99 {
100         struct req_data *rd = g_new0(struct req_data, 1);
101
102         DBG("");
103
104         rd->cb = cb;
105         rd->userdata = data;
106
107         return g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, 2, request_cb,
108                                                 rd, (GDestroyNotify) g_free);
109 }
110
111 static void example_release(int id)
112 {
113         DBG("");
114
115         g_source_remove(id);
116 }
117
118 static struct ofono_private_network_driver example_driver = {
119         .name           = "Example Private Network Driver",
120         .request        = example_request,
121         .release        = example_release,
122 };
123
124 static int example_private_network_init(void)
125 {
126         return ofono_private_network_driver_register(&example_driver);
127 }
128
129 static void example_private_network_exit(void)
130 {
131         ofono_private_network_driver_unregister(&example_driver);
132 }
133
134 OFONO_PLUGIN_DEFINE(example_private_network, "Example Private Network Plugin",
135                         VERSION, OFONO_PLUGIN_PRIORITY_LOW,
136                         example_private_network_init,
137                         example_private_network_exit)