diff options
author | David Woodhouse <David.Woodhouse@intel.com> | 2016-08-16 17:01:35 +0100 |
---|---|---|
committer | Patrik Flykt <patrik.flykt@linux.intel.com> | 2016-08-19 10:10:29 +0300 |
commit | db61076cb02e0f6ced02cd89f0191f2e3d46222e (patch) | |
tree | b4283522fd43ec9bc9372499760ab8a1ff5758f2 | |
parent | d37b07a40813cdc8541fe6449739679bf3818b97 (diff) | |
download | pacrunner-db61076cb02e0f6ced02cd89f0191f2e3d46222e.tar.gz |
js: add __pacrunner_js_resolve() function
Remove the duplicate definitions from mozjs and v8 plugins
-rw-r--r-- | plugins/mozjs.c | 34 | ||||
-rw-r--r-- | plugins/v8.cc | 33 | ||||
-rw-r--r-- | src/js.h | 2 | ||||
-rw-r--r-- | src/js_funcs.c | 33 |
4 files changed, 40 insertions, 62 deletions
diff --git a/plugins/mozjs.c b/plugins/mozjs.c index 2720cac..a923203 100644 --- a/plugins/mozjs.c +++ b/plugins/mozjs.c @@ -45,25 +45,6 @@ struct pacrunner_mozjs { JSObject *jsobj; }; -static int resolve(const char *node, char *host, size_t hostlen) -{ - struct addrinfo *info; - int err; - - if (getaddrinfo(node, NULL, NULL, &info) < 0) - return -EIO; - - err = getnameinfo(info->ai_addr, info->ai_addrlen, - host, hostlen, NULL, 0, NI_NUMERICHOST); - - freeaddrinfo(info); - - if (err < 0) - return -EIO; - - return 0; -} - static JSBool myipaddress(JSContext *jsctx, uintN argc, jsval *vp) { struct pacrunner_mozjs *ctx = JS_GetContextPrivate(jsctx); @@ -89,26 +70,19 @@ static JSBool myipaddress(JSContext *jsctx, uintN argc, jsval *vp) static JSBool dnsresolve(JSContext *jsctx, uintN argc, jsval *vp) { + struct pacrunner_mozjs *ctx = JS_GetContextPrivate(jsctx); char address[NI_MAXHOST]; jsval *argv = JS_ARGV(jsctx, vp); char *host = JS_EncodeString(jsctx, JS_ValueToString(jsctx, argv[0])); - char **split_res; DBG("host %s", host); JS_SET_RVAL(jsctx, vp, JSVAL_NULL); - /* Q&D test on host to know if it is a proper hostname */ - split_res = g_strsplit_set(host, ":%?!,;@\\'*|<>{}[]()+=$&~# \"", -1); - if (split_res) { - int length = g_strv_length(split_res); - g_strfreev(split_res); - - if (length > 1) - goto out; - } + if (!ctx) + goto out; - if (resolve(host, address, sizeof(address)) < 0) + if (__pacrunner_js_resolve(ctx->proxy, host, address, sizeof(address)) < 0) goto out; DBG("address %s", address); diff --git a/plugins/v8.cc b/plugins/v8.cc index 954239d..00aef82 100644 --- a/plugins/v8.cc +++ b/plugins/v8.cc @@ -48,25 +48,6 @@ static gboolean v8_gc(gpointer user_data) return !v8::V8::IdleNotification(); } -static int resolve(const char *node, char *host, size_t hostlen) -{ - struct addrinfo *info; - int err; - - if (getaddrinfo(node, NULL, NULL, &info) < 0) - return -EIO; - - err = getnameinfo(info->ai_addr, info->ai_addrlen, - host, hostlen, NULL, 0, NI_NUMERICHOST); - - freeaddrinfo(info); - - if (err < 0) - return -EIO; - - return 0; -} - static v8::Handle<v8::Value> myipaddress(const v8::Arguments& args) { const char *interface; @@ -96,19 +77,7 @@ static v8::Handle<v8::Value> dnsresolve(const v8::Arguments& args) DBG("host %s", *host); - /* Q&D test on host to know if it is a proper hostname */ - split_res = g_strsplit_set(*host, ":%?!,;@\\'*|<>{}[]()+=$&~# \"", -1); - if (split_res) { - int length = g_strv_length(split_res); - g_strfreev(split_res); - - if (length > 1) { - v8::ThrowException( - v8::String::New("Failed to resolve")); - } - } - - if (resolve(*host, address, sizeof(address)) < 0) + if (__pacrunner_js_resolve(current_proxy, *host, address, sizeof(address)) < 0) return v8::ThrowException(v8::String::New("Failed to resolve")); DBG("address %s", address); @@ -38,4 +38,6 @@ void pacrunner_js_driver_unregister(struct pacrunner_js_driver *driver); /* Common functions for JS plugins */ int __pacrunner_js_getipaddr(struct pacrunner_proxy *proxy, char *host, size_t hostlen); +int __pacrunner_js_resolve(struct pacrunner_proxy *proxy, const char *node, + char *host, size_t hostlen); extern const char __pacrunner_js_routines[]; diff --git a/src/js_funcs.c b/src/js_funcs.c index d8204e5..eff73f7 100644 --- a/src/js_funcs.c +++ b/src/js_funcs.c @@ -29,6 +29,7 @@ #include <sys/ioctl.h> #include <stdio.h> +#include <netdb.h> #include <arpa/inet.h> #include <linux/if_arp.h> @@ -67,6 +68,38 @@ int __pacrunner_js_getipaddr(struct pacrunner_proxy *proxy, char *host, return 0; } +int __pacrunner_js_resolve(struct pacrunner_proxy *proxy, const char *node, + char *host, size_t hostlen) +{ + struct addrinfo *info; + char **split_res; + int err; + + /* Q&D test on host to know if it is a proper hostname */ + split_res = g_strsplit_set(node, ":%?!,;@\\'*|<>{}[]()+=$&~# \"", -1); + if (split_res) { + int length = g_strv_length(split_res); + g_strfreev(split_res); + + if (length > 1) + return -EINVAL; + } + + + if (getaddrinfo(node, NULL, NULL, &info) < 0) + return -EIO; + + err = getnameinfo(info->ai_addr, info->ai_addrlen, + host, hostlen, NULL, 0, NI_NUMERICHOST); + + freeaddrinfo(info); + + if (err < 0) + return -EIO; + + return 0; +} + const char __pacrunner_js_routines[] = "function dnsDomainIs(host, domain) {\n" \ " return (host.length >= domain.length &&\n" \ |