From db61076cb02e0f6ced02cd89f0191f2e3d46222e Mon Sep 17 00:00:00 2001 From: David Woodhouse Date: Tue, 16 Aug 2016 17:01:35 +0100 Subject: js: add __pacrunner_js_resolve() function Remove the duplicate definitions from mozjs and v8 plugins --- plugins/mozjs.c | 34 ++++------------------------------ plugins/v8.cc | 33 +-------------------------------- src/js.h | 2 ++ 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 myipaddress(const v8::Arguments& args) { const char *interface; @@ -96,19 +77,7 @@ static v8::Handle 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); diff --git a/src/js.h b/src/js.h index 9941a0b..499e58e 100644 --- a/src/js.h +++ b/src/js.h @@ -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 #include +#include #include #include @@ -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" \ -- cgit 1.2.3-korg