-
Notifications
You must be signed in to change notification settings - Fork 2
/
execute_wsh_dispatch.h
181 lines (148 loc) · 5.93 KB
/
execute_wsh_dispatch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <windows.h>
#include "net_adapter.h"
#include "util_win.h"
typedef struct script_dispatch_s {
IDispatch dispatch;
ULONG ref_count;
} script_dispatch_s;
#define SCRIPT_DISPATCH_DNS_RESOLVE_ID 1
#define SCRIPT_DISPATCH_DNS_RESOLVE_EX_ID 2
#define SCRIPT_DISPATCH_MY_IP_ADDRESS_ID 3
#define SCRIPT_DISPATCH_MY_IP_ADDRESS_EX_ID 4
#define cast_from_dispatch_interface(this) \
(script_dispatch_s *)((uint8_t *)this - offsetof(script_dispatch_s, dispatch))
static ULONG STDMETHODCALLTYPE script_dispatch_add_ref(IDispatch *dispatch) {
script_dispatch_s *this = cast_from_dispatch_interface(dispatch);
return ++this->ref_count;
}
static ULONG STDMETHODCALLTYPE script_dispatch_release(IDispatch *dispatch) {
script_dispatch_s *this = cast_from_dispatch_interface(dispatch);
if (--this->ref_count == 0)
return 0;
return this->ref_count;
}
static HRESULT STDMETHODCALLTYPE script_dispatch_query_interface(IDispatch *dispatch, REFIID riid, void **ppv) {
if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDispatch)) {
script_dispatch_add_ref(dispatch);
*ppv = dispatch;
return NOERROR;
}
*ppv = 0;
return E_NOINTERFACE;
}
static HRESULT STDMETHODCALLTYPE script_dispatch_get_type_info_count(IDispatch *dispatch, unsigned int *pctinfo) {
UNUSED(dispatch);
if (!pctinfo)
return E_INVALIDARG;
*pctinfo = 0;
return NOERROR;
}
static HRESULT STDMETHODCALLTYPE script_dispatch_get_type_info(IDispatch *dispatch, unsigned int type, LCID lcid,
ITypeInfo **type_info) {
UNUSED(dispatch);
UNUSED(type);
UNUSED(lcid);
UNUSED(type_info);
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE script_dispatch_get_ids_of_names(IDispatch *dispatch, REFIID riid, OLECHAR **names,
unsigned names_count, LCID lcid,
DISPID *ret_disp_id) {
HRESULT result = S_OK;
UNUSED(dispatch);
UNUSED(riid);
UNUSED(lcid);
// Return the ID of the function
for (unsigned i = 0; i < names_count; i++) {
if (wcscmp(names[i], OLESTR("dnsResolve")) == 0) {
ret_disp_id[i] = SCRIPT_DISPATCH_DNS_RESOLVE_ID;
} else if (wcscmp(names[i], OLESTR("dnsResolveEx")) == 0) {
ret_disp_id[i] = SCRIPT_DISPATCH_DNS_RESOLVE_EX_ID;
} else if (wcscmp(names[i], OLESTR("myIpAddress")) == 0) {
ret_disp_id[i] = SCRIPT_DISPATCH_MY_IP_ADDRESS_ID;
} else if (wcscmp(names[i], OLESTR("myIpAddressEx")) == 0) {
ret_disp_id[i] = SCRIPT_DISPATCH_MY_IP_ADDRESS_EX_ID;
} else {
ret_disp_id[i] = DISPID_UNKNOWN;
result = DISP_E_UNKNOWNNAME;
}
}
return result;
}
static HRESULT STDMETHODCALLTYPE script_dispatch_invoke(IDispatch *dispatch, DISPID disp_id, REFIID riid, LCID lcid,
WORD flags, DISPPARAMS *disp_params, VARIANT *result_ptr,
EXCEPINFO *excep_info, UINT *arg_err) {
UNUSED(dispatch);
UNUSED(riid);
UNUSED(lcid);
UNUSED(flags);
UNUSED(excep_info);
UNUSED(arg_err);
// Check ID of the function and return the result
if (disp_id == SCRIPT_DISPATCH_DNS_RESOLVE_ID || disp_id == SCRIPT_DISPATCH_DNS_RESOLVE_EX_ID) {
if (disp_params->cArgs != 1)
return DISP_E_BADPARAMCOUNT;
if (disp_params->cNamedArgs != 0)
return DISP_E_NONAMEDARGS;
if (disp_params->rgvarg[0].vt != VT_BSTR) {
arg_err = 0;
return DISP_E_TYPEMISMATCH;
}
char *host_utf8 = wchar_dup_to_utf8(disp_params->rgvarg[0].bstrVal);
if (!host_utf8)
return E_OUTOFMEMORY;
// Resolve hostname to IP addresses string
char *ip = NULL;
if (disp_id == SCRIPT_DISPATCH_DNS_RESOLVE_EX_ID)
ip = dns_resolve_ex(host_utf8, NULL);
else
ip = dns_resolve(host_utf8, NULL);
free(host_utf8);
if (!ip)
return E_FAIL;
wchar_t *ip_wchar = utf8_dup_to_wchar(ip);
free(ip);
if (!ip_wchar)
return E_OUTOFMEMORY;
VariantInit(result_ptr);
// Return string as BSTR
result_ptr->vt = VT_BSTR;
result_ptr->bstrVal = SysAllocString(ip_wchar);
free(ip_wchar);
return S_OK;
} else if (disp_id == SCRIPT_DISPATCH_MY_IP_ADDRESS_ID || disp_id == SCRIPT_DISPATCH_MY_IP_ADDRESS_EX_ID) {
if (disp_params->cArgs != 0)
return DISP_E_BADPARAMCOUNT;
if (disp_params->cNamedArgs != 0)
return DISP_E_NONAMEDARGS;
// Resolve local hostname to IP addresses string
char *ip = NULL;
if (disp_id == SCRIPT_DISPATCH_MY_IP_ADDRESS_EX_ID)
ip = my_ip_address_ex();
else
ip = my_ip_address();
if (!ip)
return E_FAIL;
wchar_t *ip_wchar = utf8_dup_to_wchar(ip);
free(ip);
if (!ip_wchar)
return E_OUTOFMEMORY;
VariantInit(result_ptr);
// Return string as BSTR
result_ptr->vt = VT_BSTR;
result_ptr->bstrVal = SysAllocString(ip_wchar);
free(ip_wchar);
return S_OK;
}
return DISP_E_MEMBERNOTFOUND;
}
static const IDispatchVtbl script_dispatch_vtbl = {script_dispatch_query_interface, script_dispatch_add_ref,
script_dispatch_release, script_dispatch_get_type_info_count,
script_dispatch_get_type_info, script_dispatch_get_ids_of_names,
script_dispatch_invoke};
IDispatchVtbl *script_dispatch_get_vtbl(void) {
return (IDispatchVtbl *)&script_dispatch_vtbl;
}