Add basic implementation for internal proxy framework
[framework/connectivity/connman.git] / src / proxy.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 <glib.h>
27
28 #include "connman.h"
29
30 static unsigned int next_lookup_token = 1;
31
32 static GSList *driver_list = NULL;
33
34 struct proxy_lookup {
35         unsigned int token;
36         connman_proxy_lookup_cb cb;
37         void *user_data;
38         guint watch;
39 };
40
41 static gboolean lookup_callback(gpointer user_data)
42 {
43         struct proxy_lookup *lookup = user_data;
44
45         lookup->watch = 0;
46
47         if (lookup->cb)
48                 lookup->cb(NULL, lookup->user_data);
49
50         g_free(lookup);
51
52         return FALSE;
53 }
54
55 unsigned int connman_proxy_lookup(const char *interface, const char *url,
56                                 connman_proxy_lookup_cb cb, void *user_data)
57 {
58         struct proxy_lookup *lookup;
59
60         DBG("interface %s url %s", interface, url);
61
62         if (interface == NULL)
63                 return 0;
64
65         lookup = g_try_new0(struct proxy_lookup, 1);
66         if (lookup == NULL)
67                 return 0;
68
69         lookup->token = next_lookup_token++;
70
71         lookup->cb = cb;
72         lookup->user_data = user_data;
73
74         lookup->watch = g_timeout_add_seconds(0, lookup_callback, lookup);
75         if (lookup->watch == 0) {
76                 g_free(lookup);
77                 return 0;
78         }
79
80         DBG("token %u", lookup->token);
81
82         return lookup->token;
83 }
84
85 void connman_proxy_lookup_cancel(unsigned int token)
86 {
87         DBG("token %u", token);
88 }
89
90 void connman_proxy_driver_lookup_notify(struct connman_service *service,
91                                         const char *url, const char *result)
92 {
93         DBG("service %p url %s result %s", service, url, result);
94
95         if (service == NULL)
96                 return;
97 }
98
99 static gint compare_priority(gconstpointer a, gconstpointer b)
100 {
101         const struct connman_proxy_driver *driver1 = a;
102         const struct connman_proxy_driver *driver2 = b;
103
104         return driver2->priority - driver1->priority;
105 }
106
107 /**
108  * connman_proxy_driver_register:
109  * @driver: Proxy driver definition
110  *
111  * Register a new proxy driver
112  *
113  * Returns: %0 on success
114  */
115 int connman_proxy_driver_register(struct connman_proxy_driver *driver)
116 {
117         DBG("driver %p name %s", driver, driver->name);
118
119         driver_list = g_slist_insert_sorted(driver_list, driver,
120                                                         compare_priority);
121
122         return 0;
123 }
124
125 /**
126  * connman_proxy_driver_unregister:
127  * @driver: Proxy driver definition
128  *
129  * Remove a previously registered proxy driver
130  */
131 void connman_proxy_driver_unregister(struct connman_proxy_driver *driver)
132 {
133         DBG("driver %p name %s", driver, driver->name);
134
135         driver_list = g_slist_remove(driver_list, driver);
136 }
137
138 int __connman_proxy_init(void)
139 {
140         DBG("");
141
142         return 0;
143 }
144
145 void __connman_proxy_cleanup(void)
146 {
147         DBG("");
148 }