tizen 2.4 release
[external/pacrunner.git] / plugins / mozjs.c
1 /*
2  *
3  *  PACrunner - Proxy configuration daemon
4  *
5  *  Copyright (C) 2010-2011  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 <errno.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30
31 #include <netdb.h>
32 #include <arpa/inet.h>
33 #include <linux/if_arp.h>
34
35 #pragma GCC diagnostic ignored "-Wredundant-decls"
36 #include <jsapi.h>
37 #pragma GCC diagnostic error "-Wredundant-decls"
38
39 #include "javascript.h"
40
41 #include "pacrunner.h"
42 #include "js.h"
43
44 static GStaticMutex mozjs_mutex = G_STATIC_MUTEX_INIT;
45
46 static struct pacrunner_proxy *current_proxy = NULL;
47
48 static int getaddr(const char *node, char *host, size_t hostlen)
49 {
50         struct sockaddr_in addr;
51         struct ifreq ifr;
52         int sk, err;
53
54         sk = socket(PF_INET, SOCK_DGRAM, 0);
55         if (sk < 0)
56                 return -EIO;
57
58         memset(&ifr, 0, sizeof(ifr));
59         strncpy(ifr.ifr_name, node, sizeof(ifr.ifr_name));
60
61         err = ioctl(sk, SIOCGIFADDR, &ifr);
62
63         close(sk);
64
65         if (err < 0)
66                 return -EIO;
67
68         memcpy(&addr, &ifr.ifr_addr, sizeof(addr));
69         snprintf(host, hostlen, "%s", inet_ntoa(addr.sin_addr));
70
71         return 0;
72 }
73
74 static int resolve(const char *node, char *host, size_t hostlen)
75 {
76         struct addrinfo *info;
77         int err;
78
79         if (getaddrinfo(node, NULL, NULL, &info) < 0)
80                 return -EIO;
81
82         err = getnameinfo(info->ai_addr, info->ai_addrlen,
83                                 host, hostlen, NULL, 0, NI_NUMERICHOST);
84
85         freeaddrinfo(info);
86
87         if (err < 0)
88                 return -EIO;
89
90         return 0;
91 }
92
93 static JSBool myipaddress(JSContext *ctx, uintN argc, jsval *vp)
94 {
95         const char *interface;
96         char address[NI_MAXHOST];
97
98         DBG("");
99
100         JS_SET_RVAL(ctx, vp, JSVAL_NULL);
101
102         if (current_proxy == NULL)
103                 return JS_TRUE;
104
105         interface = pacrunner_proxy_get_interface(current_proxy);
106         if (interface == NULL)
107                 return JS_TRUE;
108
109         if (getaddr(interface, address, sizeof(address)) < 0)
110                 return JS_TRUE;
111
112         DBG("address %s", address);
113
114         JS_SET_RVAL(ctx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(ctx, address)));
115
116         return JS_TRUE;
117 }
118
119 static JSBool dnsresolve(JSContext *ctx, uintN argc, jsval *vp)
120 {
121         char address[NI_MAXHOST];
122         jsval *argv = JS_ARGV(ctx, vp);
123         char *host = JS_EncodeString(ctx, JS_ValueToString(ctx, argv[0]));
124
125         DBG("host %s", host);
126
127         JS_SET_RVAL(ctx, vp, JSVAL_NULL);
128
129         if (resolve(host, address, sizeof(address)) < 0)
130                 goto out;
131
132         DBG("address %s", address);
133
134         JS_SET_RVAL(ctx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(ctx, address)));
135
136  out:
137         JS_free(ctx, host);
138         return JS_TRUE;
139
140 }
141
142 static JSClass jscls = {
143         "global", JSCLASS_GLOBAL_FLAGS,
144         JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
145         JS_StrictPropertyStub,
146         JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
147         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
148 };
149
150 static JSRuntime *jsrun;
151 static JSContext *jsctx = NULL;
152 static JSObject *jsobj = NULL;
153
154 static void create_object(void)
155 {
156         const char *script;
157         jsval rval;
158
159         if (current_proxy == NULL)
160                 return;
161
162         script = pacrunner_proxy_get_script(current_proxy);
163         if (script == NULL)
164                 return;
165
166         jsctx = JS_NewContext(jsrun, 8 * 1024);
167
168 #if JS_VERSION >= 185
169         jsobj = JS_NewCompartmentAndGlobalObject(jsctx, &jscls, NULL);
170 #else
171         jsobj = JS_NewObject(jsctx, &jscls, NULL, NULL);
172 #endif
173
174         if (!JS_InitStandardClasses(jsctx, jsobj))
175                 pacrunner_error("Failed to init JS standard classes");
176
177         JS_DefineFunction(jsctx, jsobj, "myIpAddress", myipaddress, 0, 0);
178         JS_DefineFunction(jsctx, jsobj, "dnsResolve", dnsresolve, 1, 0);
179
180         JS_EvaluateScript(jsctx, jsobj, JAVASCRIPT_ROUTINES,
181                                 strlen(JAVASCRIPT_ROUTINES), NULL, 0, &rval);
182
183         JS_EvaluateScript(jsctx, jsobj, script, strlen(script),
184                                                 "wpad.dat", 0, &rval);
185 }
186
187 static void destroy_object(void)
188 {
189         if (jsctx == NULL)
190                 return;
191
192         JS_DestroyContext(jsctx);
193         jsctx = NULL;
194
195         jsobj = NULL;
196 }
197
198 static int mozjs_set_proxy(struct pacrunner_proxy *proxy)
199 {
200         DBG("proxy %p", proxy);
201
202         if (current_proxy != NULL)
203                 destroy_object();
204
205         current_proxy = proxy;
206
207         if (current_proxy != NULL)
208                 create_object();
209
210         return 0;
211 }
212
213 static char * mozjs_execute(const char *url, const char *host)
214 {
215         JSBool result;
216         jsval rval, args[2];
217         char *answer, *g_answer;
218
219         DBG("url %s host %s", url, host);
220
221         if (jsctx == NULL)
222                 return NULL;
223
224         g_static_mutex_lock(&mozjs_mutex);
225
226         JS_BeginRequest(jsctx);
227
228         args[0] = STRING_TO_JSVAL(JS_NewStringCopyZ(jsctx, url));
229         args[1] = STRING_TO_JSVAL(JS_NewStringCopyZ(jsctx, host));
230
231         result = JS_CallFunctionName(jsctx, jsobj, "FindProxyForURL",
232                                                         2, args, &rval);
233
234         JS_EndRequest(jsctx);
235
236         JS_MaybeGC(jsctx);
237
238         g_static_mutex_unlock(&mozjs_mutex);
239
240         if (result) {
241                 answer = JS_EncodeString(jsctx, JS_ValueToString(jsctx, rval));
242                 g_answer = g_strdup(answer);
243                 JS_free(jsctx, answer);
244                 return g_answer;
245         }
246
247         return NULL;
248 }
249
250 static struct pacrunner_js_driver mozjs_driver = {
251         .name           = "mozjs",
252         .priority       = PACRUNNER_JS_PRIORITY_DEFAULT,
253         .set_proxy      = mozjs_set_proxy,
254         .execute        = mozjs_execute,
255 };
256
257 static int mozjs_init(void)
258 {
259         DBG("");
260
261         jsrun = JS_NewRuntime(8 * 1024 * 1024);
262
263         return pacrunner_js_driver_register(&mozjs_driver);
264 }
265
266 static void mozjs_exit(void)
267 {
268         DBG("");
269
270         pacrunner_js_driver_unregister(&mozjs_driver);
271
272         mozjs_set_proxy(NULL);
273
274         JS_DestroyRuntime(jsrun);
275 }
276
277 PACRUNNER_PLUGIN_DEFINE(mozjs, mozjs_init, mozjs_exit)