diff options
Diffstat (limited to 'src/gns/w32nsp-resolve.c')
-rw-r--r-- | src/gns/w32nsp-resolve.c | 636 |
1 files changed, 327 insertions, 309 deletions
diff --git a/src/gns/w32nsp-resolve.c b/src/gns/w32nsp-resolve.c index 841f5b9e6..ad0fd3586 100644 --- a/src/gns/w32nsp-resolve.c +++ b/src/gns/w32nsp-resolve.c | |||
@@ -11,19 +11,19 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | /** | 20 | /** |
21 | * @file gns/w32nsp-resolve.c | 21 | * @file gns/w32nsp-resolve.c |
22 | * @brief W32 integration for GNS | 22 | * @brief W32 integration for GNS |
23 | * @author LRN | 23 | * @author LRN |
24 | */ | 24 | */ |
25 | /* Instead of including gnunet_common.h */ | 25 | /* Instead of including gnunet_common.h */ |
26 | #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) | 26 | #define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0) |
27 | 27 | ||
28 | #include <ws2tcpip.h> | 28 | #include <ws2tcpip.h> |
29 | #include <windows.h> | 29 | #include <windows.h> |
@@ -34,15 +34,15 @@ | |||
34 | #include "gnunet_w32nsp_lib.h" | 34 | #include "gnunet_w32nsp_lib.h" |
35 | #include <stdio.h> | 35 | #include <stdio.h> |
36 | 36 | ||
37 | typedef int (WSPAPI *LPNSPSTARTUP) (LPGUID lpProviderId, LPNSP_ROUTINE lpnspRoutines); | 37 | typedef int (WSPAPI *LPNSPSTARTUP)(LPGUID lpProviderId, LPNSP_ROUTINE lpnspRoutines); |
38 | 38 | ||
39 | GUID host = {0x0002a800,0,0,{ 0xC0,0,0,0,0,0,0,0x46 }}; | 39 | GUID host = { 0x0002a800, 0, 0, { 0xC0, 0, 0, 0, 0, 0, 0, 0x46 } }; |
40 | GUID ip4 = {0x00090035,0,1,{ 0xc0,0,0,0,0,0,0,0x046}}; | 40 | GUID ip4 = { 0x00090035, 0, 1, { 0xc0, 0, 0, 0, 0, 0, 0, 0x046 } }; |
41 | GUID ip6 = {0x00090035,0,0x001c, { 0xc0,0,0,0,0,0,0,0x046}}; | 41 | GUID ip6 = { 0x00090035, 0, 0x001c, { 0xc0, 0, 0, 0, 0, 0, 0, 0x046 } }; |
42 | 42 | ||
43 | DEFINE_GUID(W32_DNS, 0x22059D40, 0x7E9E, 0x11CF, 0xAE, 0x5A, 0x00, 0xAA, 0x00, 0xA7, 0x11, 0x2B); | 43 | DEFINE_GUID(W32_DNS, 0x22059D40, 0x7E9E, 0x11CF, 0xAE, 0x5A, 0x00, 0xAA, 0x00, 0xA7, 0x11, 0x2B); |
44 | 44 | ||
45 | #define DEFINE_DNS_GUID(a,x) DEFINE_GUID(a, 0x00090035, 0x0000, x, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46) | 45 | #define DEFINE_DNS_GUID(a, x) DEFINE_GUID(a, 0x00090035, 0x0000, x, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46) |
46 | DEFINE_DNS_GUID(SVCID_DNS_TYPE_A, 0x0001); | 46 | DEFINE_DNS_GUID(SVCID_DNS_TYPE_A, 0x0001); |
47 | DEFINE_DNS_GUID(SVCID_DNS_TYPE_NS, 0x0002); | 47 | DEFINE_DNS_GUID(SVCID_DNS_TYPE_NS, 0x0002); |
48 | DEFINE_DNS_GUID(SVCID_DNS_TYPE_CNAME, 0x0005); | 48 | DEFINE_DNS_GUID(SVCID_DNS_TYPE_CNAME, 0x0005); |
@@ -63,15 +63,15 @@ DEFINE_GUID(SVCID_INET_HOSTADDRBYNAME, 0x0002a803, 0x0000, 0x0000, 0xc0, 0x00, 0 | |||
63 | VOID | 63 | VOID |
64 | FixList(PCHAR ** List, PCHAR Base) | 64 | FixList(PCHAR ** List, PCHAR Base) |
65 | { | 65 | { |
66 | if(*List) | 66 | if (*List) |
67 | { | 67 | { |
68 | PCHAR * Addr; | 68 | PCHAR * Addr; |
69 | 69 | ||
70 | Addr = *List = (PCHAR *)( ((DWORD)*List + Base) ); | 70 | Addr = *List = (PCHAR *)(((DWORD)*List + Base)); |
71 | while(*Addr) | 71 | while (*Addr) |
72 | { | 72 | { |
73 | *Addr = (PCHAR)(((DWORD)*Addr + Base)); | 73 | *Addr = (PCHAR)(((DWORD)*Addr + Base)); |
74 | Addr++; | 74 | Addr++; |
75 | } | 75 | } |
76 | } | 76 | } |
77 | } | 77 | } |
@@ -84,82 +84,92 @@ FixList(PCHAR ** List, PCHAR Base) | |||
84 | VOID | 84 | VOID |
85 | UnpackHostEnt(struct hostent * hostent) | 85 | UnpackHostEnt(struct hostent * hostent) |
86 | { | 86 | { |
87 | PCHAR pch; | 87 | PCHAR pch; |
88 | 88 | ||
89 | pch = (PCHAR)hostent; | 89 | pch = (PCHAR)hostent; |
90 | 90 | ||
91 | if(hostent->h_name) | 91 | if (hostent->h_name) |
92 | { | 92 | { |
93 | hostent->h_name = (PCHAR)((DWORD)hostent->h_name + pch); | 93 | hostent->h_name = (PCHAR)((DWORD)hostent->h_name + pch); |
94 | } | 94 | } |
95 | FixList(&hostent->h_aliases, pch); | 95 | FixList(&hostent->h_aliases, pch); |
96 | FixList(&hostent->h_addr_list, pch); | 96 | FixList(&hostent->h_addr_list, pch); |
97 | } | 97 | } |
98 | 98 | ||
99 | 99 | ||
100 | static void | 100 | static void |
101 | print_hostent (struct hostent *he) | 101 | print_hostent(struct hostent *he) |
102 | { | 102 | { |
103 | int i; | 103 | int i; |
104 | char **pAlias; | 104 | char **pAlias; |
105 | 105 | ||
106 | printf("\tOfficial name: %s\n", he->h_name); | 106 | printf("\tOfficial name: %s\n", he->h_name); |
107 | for (i=0, pAlias = he->h_aliases; *pAlias != 0; pAlias++) { | 107 | for (i = 0, pAlias = he->h_aliases; *pAlias != 0; pAlias++) |
108 | { | ||
108 | printf("\tAlternate name #%d: %s\n", ++i, *pAlias); | 109 | printf("\tAlternate name #%d: %s\n", ++i, *pAlias); |
109 | } | 110 | } |
110 | printf("\tAddress type: "); | 111 | printf("\tAddress type: "); |
111 | switch (he->h_addrtype) { | 112 | switch (he->h_addrtype) |
112 | case AF_INET: | 113 | { |
114 | case AF_INET: | ||
113 | printf("AF_INET\n"); | 115 | printf("AF_INET\n"); |
114 | break; | 116 | break; |
115 | case AF_INET6: | 117 | |
118 | case AF_INET6: | ||
116 | printf("AF_INET6\n"); | 119 | printf("AF_INET6\n"); |
117 | break; | 120 | break; |
118 | case AF_NETBIOS: | 121 | |
122 | case AF_NETBIOS: | ||
119 | printf("AF_NETBIOS\n"); | 123 | printf("AF_NETBIOS\n"); |
120 | break; | 124 | break; |
121 | default: | 125 | |
126 | default: | ||
122 | printf(" %d\n", he->h_addrtype); | 127 | printf(" %d\n", he->h_addrtype); |
123 | break; | 128 | break; |
124 | } | 129 | } |
125 | printf("\tAddress length: %d\n", he->h_length); | 130 | printf("\tAddress length: %d\n", he->h_length); |
126 | 131 | ||
127 | if (he->h_addrtype == AF_INET) { | 132 | if (he->h_addrtype == AF_INET) |
128 | struct sockaddr_in addr; | 133 | { |
129 | memset (&addr, 0, sizeof (addr)); | 134 | struct sockaddr_in addr; |
130 | addr.sin_family = AF_INET; | 135 | memset(&addr, 0, sizeof(addr)); |
131 | addr.sin_port = 0; | 136 | addr.sin_family = AF_INET; |
132 | i = 0; | 137 | addr.sin_port = 0; |
133 | while (he->h_addr_list[i] != 0) { | 138 | i = 0; |
134 | char buf[1024]; | 139 | while (he->h_addr_list[i] != 0) |
135 | DWORD buflen = 1024; | 140 | { |
136 | addr.sin_addr = *(struct in_addr *) he->h_addr_list[i++]; | 141 | char buf[1024]; |
137 | if (NO_ERROR == WSAAddressToStringA ((LPSOCKADDR) &addr, sizeof (addr), NULL, buf, &buflen)) | 142 | DWORD buflen = 1024; |
138 | printf("\tIPv4 Address #%d: %s\n", i, buf); | 143 | addr.sin_addr = *(struct in_addr *)he->h_addr_list[i++]; |
139 | else | 144 | if (NO_ERROR == WSAAddressToStringA((LPSOCKADDR)&addr, sizeof(addr), NULL, buf, &buflen)) |
140 | printf("\tIPv4 Address #%d: Can't convert: %lu\n", i, GetLastError ()); | 145 | printf("\tIPv4 Address #%d: %s\n", i, buf); |
146 | else | ||
147 | printf("\tIPv4 Address #%d: Can't convert: %lu\n", i, GetLastError()); | ||
148 | } | ||
141 | } | 149 | } |
142 | } else if (he->h_addrtype == AF_INET6) { | 150 | else if (he->h_addrtype == AF_INET6) |
143 | struct sockaddr_in6 addr; | 151 | { |
144 | memset (&addr, 0, sizeof (addr)); | 152 | struct sockaddr_in6 addr; |
145 | addr.sin6_family = AF_INET6; | 153 | memset(&addr, 0, sizeof(addr)); |
146 | addr.sin6_port = 0; | 154 | addr.sin6_family = AF_INET6; |
147 | i = 0; | 155 | addr.sin6_port = 0; |
148 | while (he->h_addr_list[i] != 0) { | 156 | i = 0; |
149 | char buf[1024]; | 157 | while (he->h_addr_list[i] != 0) |
150 | DWORD buflen = 1024; | 158 | { |
151 | addr.sin6_addr = *(struct in6_addr *) he->h_addr_list[i++]; | 159 | char buf[1024]; |
152 | if (NO_ERROR == WSAAddressToStringA ((LPSOCKADDR) &addr, sizeof (addr), NULL, buf, &buflen)) | 160 | DWORD buflen = 1024; |
153 | printf("\tIPv6 Address #%d: %s\n", i, buf); | 161 | addr.sin6_addr = *(struct in6_addr *)he->h_addr_list[i++]; |
154 | else | 162 | if (NO_ERROR == WSAAddressToStringA((LPSOCKADDR)&addr, sizeof(addr), NULL, buf, &buflen)) |
155 | printf("\tIPv6 Address #%d: Can't convert: %lu\n", i, GetLastError ()); | 163 | printf("\tIPv6 Address #%d: %s\n", i, buf); |
164 | else | ||
165 | printf("\tIPv6 Address #%d: Can't convert: %lu\n", i, GetLastError()); | ||
166 | } | ||
156 | } | 167 | } |
157 | } | ||
158 | } | 168 | } |
159 | 169 | ||
160 | 170 | ||
161 | int | 171 | int |
162 | main (int argc, char **argv) | 172 | main(int argc, char **argv) |
163 | { | 173 | { |
164 | int ret; | 174 | int ret; |
165 | int r = 1; | 175 | int r = 1; |
@@ -170,277 +180,285 @@ main (int argc, char **argv) | |||
170 | int wargc; | 180 | int wargc; |
171 | wchar_t **wargv; | 181 | wchar_t **wargv; |
172 | 182 | ||
173 | if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) | 183 | if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0) |
174 | { | 184 | { |
175 | fprintf (stderr, "WSAStartup() failed: %lu\n", GetLastError()); | 185 | fprintf(stderr, "WSAStartup() failed: %lu\n", GetLastError()); |
176 | return 5; | 186 | return 5; |
177 | } | 187 | } |
178 | 188 | ||
179 | cmdl = GetCommandLineW (); | 189 | cmdl = GetCommandLineW(); |
180 | if (cmdl == NULL) | 190 | if (cmdl == NULL) |
181 | { | 191 | { |
182 | WSACleanup(); | 192 | WSACleanup(); |
183 | return 2; | 193 | return 2; |
184 | } | 194 | } |
185 | wargv = CommandLineToArgvW (cmdl, &wargc); | 195 | wargv = CommandLineToArgvW(cmdl, &wargc); |
186 | if (wargv == NULL) | 196 | if (wargv == NULL) |
187 | { | 197 | { |
188 | WSACleanup(); | 198 | WSACleanup(); |
189 | return 3; | 199 | return 3; |
190 | } | 200 | } |
191 | r = 4; | 201 | r = 4; |
192 | 202 | ||
193 | if (wargc == 5) | 203 | if (wargc == 5) |
194 | { | 204 | { |
195 | if (wcscmp (wargv[1], L"A") == 0) | 205 | if (wcscmp(wargv[1], L"A") == 0) |
196 | sc = SVCID_DNS_TYPE_A; | 206 | sc = SVCID_DNS_TYPE_A; |
197 | else if (wcscmp (wargv[1], L"AAAA") == 0) | 207 | else if (wcscmp(wargv[1], L"AAAA") == 0) |
198 | sc = SVCID_DNS_TYPE_AAAA; | 208 | sc = SVCID_DNS_TYPE_AAAA; |
199 | else if (wcscmp (wargv[1], L"name") == 0) | 209 | else if (wcscmp(wargv[1], L"name") == 0) |
200 | sc = SVCID_HOSTNAME; | 210 | sc = SVCID_HOSTNAME; |
201 | else if (wcscmp (wargv[1], L"addr") == 0) | 211 | else if (wcscmp(wargv[1], L"addr") == 0) |
202 | sc = SVCID_INET_HOSTADDRBYNAME; | 212 | sc = SVCID_INET_HOSTADDRBYNAME; |
203 | else | 213 | else |
204 | wargc -= 1; | 214 | wargc -= 1; |
205 | if (wcscmp (wargv[4], L"mswdns") == 0) | 215 | if (wcscmp(wargv[4], L"mswdns") == 0) |
206 | prov = W32_DNS; | 216 | prov = W32_DNS; |
207 | else if (wcscmp (wargv[4], L"gnunetdns") == 0) | 217 | else if (wcscmp(wargv[4], L"gnunetdns") == 0) |
208 | prov = GNUNET_NAMESPACE_PROVIDER_DNS; | 218 | prov = GNUNET_NAMESPACE_PROVIDER_DNS; |
209 | else | 219 | else |
210 | wargc -= 1; | 220 | wargc -= 1; |
211 | } | 221 | } |
212 | else if (wargc == 3) | 222 | else if (wargc == 3) |
213 | { | 223 | { |
214 | } | 224 | } |
215 | else | 225 | else |
216 | { | ||
217 | fprintf (stderr, "Usage: %S <record type> <service name> <NSP library path> <NSP id>\n" | ||
218 | "record type - one of the following: A | AAAA | name | addr\n" | ||
219 | "service name - a string to resolve; \" \" (a space) means 'blank'\n" | ||
220 | "NSP library path - path to libw32nsp\n" | ||
221 | "NSP id - one of the following: mswdns | gnunetdns\n", | ||
222 | wargv[0]); | ||
223 | } | ||
224 | |||
225 | if (wargc == 5) | ||
226 | { | ||
227 | HMODULE nsp; | ||
228 | |||
229 | nsp = LoadLibraryW (wargv[3]); | ||
230 | if (nsp == NULL) | ||
231 | { | 226 | { |
232 | fprintf (stderr, "Failed to load library `%S'\n", wargv[3]); | 227 | fprintf(stderr, "Usage: %S <record type> <service name> <NSP library path> <NSP id>\n" |
228 | "record type - one of the following: A | AAAA | name | addr\n" | ||
229 | "service name - a string to resolve; \" \" (a space) means 'blank'\n" | ||
230 | "NSP library path - path to libw32nsp\n" | ||
231 | "NSP id - one of the following: mswdns | gnunetdns\n", | ||
232 | wargv[0]); | ||
233 | } | 233 | } |
234 | else | 234 | |
235 | if (wargc == 5) | ||
235 | { | 236 | { |
236 | LPNSPSTARTUP startup = (LPNSPSTARTUP) GetProcAddress (nsp, "NSPStartup"); | 237 | HMODULE nsp; |
237 | if (startup == NULL) | 238 | |
238 | startup = (LPNSPSTARTUP) GetProcAddress (nsp, "NSPStartup@8"); | 239 | nsp = LoadLibraryW(wargv[3]); |
239 | if (startup != NULL) | 240 | if (nsp == NULL) |
240 | { | ||
241 | NSP_ROUTINE api; | ||
242 | api.cbSize = sizeof (api); | ||
243 | ret = startup (&prov, &api); | ||
244 | if (NO_ERROR != ret) | ||
245 | fprintf (stderr, "startup failed: %lu\n", GetLastError ()); | ||
246 | else | ||
247 | { | 241 | { |
248 | HANDLE lookup; | 242 | fprintf(stderr, "Failed to load library `%S'\n", wargv[3]); |
249 | WSAQUERYSETW search; | 243 | } |
250 | char buf[4096]; | 244 | else |
251 | WSAQUERYSETW *result = (WSAQUERYSETW *) buf; | 245 | { |
252 | DWORD resultsize; | 246 | LPNSPSTARTUP startup = (LPNSPSTARTUP)GetProcAddress(nsp, "NSPStartup"); |
253 | DWORD err; | 247 | if (startup == NULL) |
254 | memset (&search, 0, sizeof (search)); | 248 | startup = (LPNSPSTARTUP)GetProcAddress(nsp, "NSPStartup@8"); |
255 | search.dwSize = sizeof (search); | 249 | if (startup != NULL) |
256 | search.lpszServiceInstanceName = (wcscmp (wargv[2], L" ") == 0) ? NULL : wargv[2]; | ||
257 | search.lpServiceClassId = ≻ | ||
258 | search.lpNSProviderId = &prov; | ||
259 | search.dwNameSpace = NS_ALL; | ||
260 | ret = api.NSPLookupServiceBegin (&prov, &search, NULL, LUP_RETURN_ALL, &lookup); | ||
261 | if (ret != NO_ERROR) | ||
262 | { | ||
263 | fprintf (stderr, "lookup start failed\n"); | ||
264 | } | ||
265 | else | ||
266 | { | ||
267 | resultsize = 4096; | ||
268 | ret = api.NSPLookupServiceNext (lookup, LUP_RETURN_ALL, &resultsize, result); | ||
269 | err = GetLastError (); | ||
270 | if (ret != NO_ERROR) | ||
271 | { | ||
272 | fprintf (stderr, "lookup next failed: %lu\n", err); | ||
273 | } | ||
274 | else | ||
275 | { | 250 | { |
276 | int i; | 251 | NSP_ROUTINE api; |
277 | printf ("Got result:\n"); | 252 | api.cbSize = sizeof(api); |
278 | printf (" lpszServiceInstanceName: %S\n", result->lpszServiceInstanceName ? result->lpszServiceInstanceName : L"NULL"); | 253 | ret = startup(&prov, &api); |
279 | if (result->lpServiceClassId) | 254 | if (NO_ERROR != ret) |
280 | printf (" lpServiceClassId: { 0x%08lX,0x%04X,0x%04X, { 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X } }\n", | 255 | fprintf(stderr, "startup failed: %lu\n", GetLastError()); |
281 | result->lpServiceClassId->Data1, result->lpServiceClassId->Data2, result->lpServiceClassId->Data3, result->lpServiceClassId->Data4[0], | ||
282 | result->lpServiceClassId->Data4[1], result->lpServiceClassId->Data4[2], result->lpServiceClassId->Data4[3], result->lpServiceClassId->Data4[4], | ||
283 | result->lpServiceClassId->Data4[5], result->lpServiceClassId->Data4[6], result->lpServiceClassId->Data4[7]); | ||
284 | else | ||
285 | printf (" lpServiceClassId: NULL\n"); | ||
286 | if (result->lpVersion) | ||
287 | printf (" lpVersion: 0x%08lX, %d\n", result->lpVersion->dwVersion, result->lpVersion->ecHow); | ||
288 | else | 256 | else |
289 | printf (" lpVersion: NULL\n"); | ||
290 | printf (" lpszComment: %S\n", result->lpszComment ? result->lpszComment : L"NULL"); | ||
291 | printf (" dwNameSpace: %lu\n", result->dwNameSpace); | ||
292 | if (result->lpNSProviderId) | ||
293 | printf (" lpNSProviderId: { 0x%08lX,0x%04X,0x%04X, { 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X } }\n", | ||
294 | result->lpNSProviderId->Data1, result->lpNSProviderId->Data2, result->lpNSProviderId->Data3, result->lpNSProviderId->Data4[0], | ||
295 | result->lpNSProviderId->Data4[1], result->lpNSProviderId->Data4[2], result->lpNSProviderId->Data4[3], result->lpNSProviderId->Data4[4], | ||
296 | result->lpNSProviderId->Data4[5], result->lpNSProviderId->Data4[6], result->lpNSProviderId->Data4[7]); | ||
297 | else | ||
298 | printf (" lpNSProviderId: NULL\n"); | ||
299 | printf (" lpszContext: %S\n", result->lpszContext ? result->lpszContext : L"NULL"); | ||
300 | printf (" dwNumberOfProtocols: %lu\n", result->dwNumberOfProtocols); | ||
301 | printf (" lpszQueryString: %S\n", result->lpszQueryString ? result->lpszQueryString : L"NULL"); | ||
302 | printf (" dwNumberOfCsAddrs: %lu\n", result->dwNumberOfCsAddrs); | ||
303 | for (i = 0; i < result->dwNumberOfCsAddrs; i++) | ||
304 | { | ||
305 | switch (result->lpcsaBuffer[i].iSocketType) | ||
306 | { | 257 | { |
307 | case SOCK_STREAM: | 258 | HANDLE lookup; |
308 | printf (" %d: iSocketType = SOCK_STREAM\n", i); | 259 | WSAQUERYSETW search; |
309 | break; | 260 | char buf[4096]; |
310 | case SOCK_DGRAM: | 261 | WSAQUERYSETW *result = (WSAQUERYSETW *)buf; |
311 | printf (" %d: iSocketType = SOCK_DGRAM\n", i); | 262 | DWORD resultsize; |
312 | break; | 263 | DWORD err; |
313 | default: | 264 | memset(&search, 0, sizeof(search)); |
314 | printf (" %d: iSocketType = %d\n", i, result->lpcsaBuffer[i].iSocketType); | 265 | search.dwSize = sizeof(search); |
315 | } | 266 | search.lpszServiceInstanceName = (wcscmp(wargv[2], L" ") == 0) ? NULL : wargv[2]; |
316 | switch (result->lpcsaBuffer[i].iProtocol) | 267 | search.lpServiceClassId = ≻ |
317 | { | 268 | search.lpNSProviderId = &prov; |
318 | case IPPROTO_TCP: | 269 | search.dwNameSpace = NS_ALL; |
319 | printf (" %d: iProtocol = IPPROTO_TCP\n", i); | 270 | ret = api.NSPLookupServiceBegin(&prov, &search, NULL, LUP_RETURN_ALL, &lookup); |
320 | break; | 271 | if (ret != NO_ERROR) |
321 | case IPPROTO_UDP: | 272 | { |
322 | printf (" %d: iProtocol = IPPROTO_UDP\n", i); | 273 | fprintf(stderr, "lookup start failed\n"); |
323 | break; | 274 | } |
324 | default: | 275 | else |
325 | printf (" %d: iProtocol = %d\n", i, result->lpcsaBuffer[i].iProtocol); | 276 | { |
326 | } | 277 | resultsize = 4096; |
327 | switch (result->lpcsaBuffer[i].LocalAddr.lpSockaddr->sa_family) | 278 | ret = api.NSPLookupServiceNext(lookup, LUP_RETURN_ALL, &resultsize, result); |
328 | { | 279 | err = GetLastError(); |
329 | case AF_INET: | 280 | if (ret != NO_ERROR) |
330 | printf (" %d: loc family = AF_INET\n", i); | 281 | { |
331 | break; | 282 | fprintf(stderr, "lookup next failed: %lu\n", err); |
332 | case AF_INET6: | 283 | } |
333 | printf (" %d: loc family = AF_INET6\n", i); | 284 | else |
334 | break; | 285 | { |
335 | default: | 286 | int i; |
336 | printf (" %d: loc family = %hu\n", i, result->lpcsaBuffer[i].LocalAddr.lpSockaddr->sa_family); | 287 | printf("Got result:\n"); |
337 | } | 288 | printf(" lpszServiceInstanceName: %S\n", result->lpszServiceInstanceName ? result->lpszServiceInstanceName : L"NULL"); |
338 | switch (result->lpcsaBuffer[i].RemoteAddr.lpSockaddr->sa_family) | 289 | if (result->lpServiceClassId) |
339 | { | 290 | printf(" lpServiceClassId: { 0x%08lX,0x%04X,0x%04X, { 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X } }\n", |
340 | case AF_INET: | 291 | result->lpServiceClassId->Data1, result->lpServiceClassId->Data2, result->lpServiceClassId->Data3, result->lpServiceClassId->Data4[0], |
341 | printf (" %d: rem family = AF_INET\n", i); | 292 | result->lpServiceClassId->Data4[1], result->lpServiceClassId->Data4[2], result->lpServiceClassId->Data4[3], result->lpServiceClassId->Data4[4], |
342 | break; | 293 | result->lpServiceClassId->Data4[5], result->lpServiceClassId->Data4[6], result->lpServiceClassId->Data4[7]); |
343 | case AF_INET6: | 294 | else |
344 | printf (" %d: rem family = AF_INET6\n", i); | 295 | printf(" lpServiceClassId: NULL\n"); |
345 | break; | 296 | if (result->lpVersion) |
346 | default: | 297 | printf(" lpVersion: 0x%08lX, %d\n", result->lpVersion->dwVersion, result->lpVersion->ecHow); |
347 | printf (" %d: rem family = %hu\n", i, result->lpcsaBuffer[i].RemoteAddr.lpSockaddr->sa_family); | 298 | else |
348 | } | 299 | printf(" lpVersion: NULL\n"); |
349 | char buf[1024]; | 300 | printf(" lpszComment: %S\n", result->lpszComment ? result->lpszComment : L"NULL"); |
350 | DWORD buflen = 1024; | 301 | printf(" dwNameSpace: %lu\n", result->dwNameSpace); |
351 | if (NO_ERROR == WSAAddressToStringA (result->lpcsaBuffer[i].LocalAddr.lpSockaddr, result->lpcsaBuffer[i].LocalAddr.iSockaddrLength, NULL, buf, &buflen)) | 302 | if (result->lpNSProviderId) |
352 | printf("\tLocal Address #%d: %s\n", i, buf); | 303 | printf(" lpNSProviderId: { 0x%08lX,0x%04X,0x%04X, { 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X } }\n", |
353 | else | 304 | result->lpNSProviderId->Data1, result->lpNSProviderId->Data2, result->lpNSProviderId->Data3, result->lpNSProviderId->Data4[0], |
354 | printf("\tLocal Address #%d: Can't convert: %lu\n", i, GetLastError ()); | 305 | result->lpNSProviderId->Data4[1], result->lpNSProviderId->Data4[2], result->lpNSProviderId->Data4[3], result->lpNSProviderId->Data4[4], |
355 | buflen = 1024; | 306 | result->lpNSProviderId->Data4[5], result->lpNSProviderId->Data4[6], result->lpNSProviderId->Data4[7]); |
356 | if (NO_ERROR == WSAAddressToStringA (result->lpcsaBuffer[i].RemoteAddr.lpSockaddr, result->lpcsaBuffer[i].RemoteAddr.iSockaddrLength, NULL, buf, &buflen)) | 307 | else |
357 | printf("\tRemote Address #%d: %s\n", i, buf); | 308 | printf(" lpNSProviderId: NULL\n"); |
358 | else | 309 | printf(" lpszContext: %S\n", result->lpszContext ? result->lpszContext : L"NULL"); |
359 | printf("\tRemote Address #%d: Can't convert: %lu\n", i, GetLastError ()); | 310 | printf(" dwNumberOfProtocols: %lu\n", result->dwNumberOfProtocols); |
360 | } | 311 | printf(" lpszQueryString: %S\n", result->lpszQueryString ? result->lpszQueryString : L"NULL"); |
361 | printf (" dwOutputFlags: 0x%08lX\n", result->dwOutputFlags); | 312 | printf(" dwNumberOfCsAddrs: %lu\n", result->dwNumberOfCsAddrs); |
362 | printf (" lpBlob: 0x%p\n", result->lpBlob); | 313 | for (i = 0; i < result->dwNumberOfCsAddrs; i++) |
363 | if (result->lpBlob) | 314 | { |
364 | { | 315 | switch (result->lpcsaBuffer[i].iSocketType) |
365 | struct hostent *he = malloc (result->lpBlob->cbSize); | 316 | { |
366 | if (he != NULL) | 317 | case SOCK_STREAM: |
367 | { | 318 | printf(" %d: iSocketType = SOCK_STREAM\n", i); |
368 | GNUNET_memcpy (he, result->lpBlob->pBlobData, result->lpBlob->cbSize); | 319 | break; |
369 | UnpackHostEnt (he); | 320 | |
370 | print_hostent (he); | 321 | case SOCK_DGRAM: |
371 | free (he); | 322 | printf(" %d: iSocketType = SOCK_DGRAM\n", i); |
323 | break; | ||
324 | |||
325 | default: | ||
326 | printf(" %d: iSocketType = %d\n", i, result->lpcsaBuffer[i].iSocketType); | ||
327 | } | ||
328 | switch (result->lpcsaBuffer[i].iProtocol) | ||
329 | { | ||
330 | case IPPROTO_TCP: | ||
331 | printf(" %d: iProtocol = IPPROTO_TCP\n", i); | ||
332 | break; | ||
333 | |||
334 | case IPPROTO_UDP: | ||
335 | printf(" %d: iProtocol = IPPROTO_UDP\n", i); | ||
336 | break; | ||
337 | |||
338 | default: | ||
339 | printf(" %d: iProtocol = %d\n", i, result->lpcsaBuffer[i].iProtocol); | ||
340 | } | ||
341 | switch (result->lpcsaBuffer[i].LocalAddr.lpSockaddr->sa_family) | ||
342 | { | ||
343 | case AF_INET: | ||
344 | printf(" %d: loc family = AF_INET\n", i); | ||
345 | break; | ||
346 | |||
347 | case AF_INET6: | ||
348 | printf(" %d: loc family = AF_INET6\n", i); | ||
349 | break; | ||
350 | |||
351 | default: | ||
352 | printf(" %d: loc family = %hu\n", i, result->lpcsaBuffer[i].LocalAddr.lpSockaddr->sa_family); | ||
353 | } | ||
354 | switch (result->lpcsaBuffer[i].RemoteAddr.lpSockaddr->sa_family) | ||
355 | { | ||
356 | case AF_INET: | ||
357 | printf(" %d: rem family = AF_INET\n", i); | ||
358 | break; | ||
359 | |||
360 | case AF_INET6: | ||
361 | printf(" %d: rem family = AF_INET6\n", i); | ||
362 | break; | ||
363 | |||
364 | default: | ||
365 | printf(" %d: rem family = %hu\n", i, result->lpcsaBuffer[i].RemoteAddr.lpSockaddr->sa_family); | ||
366 | } | ||
367 | char buf[1024]; | ||
368 | DWORD buflen = 1024; | ||
369 | if (NO_ERROR == WSAAddressToStringA(result->lpcsaBuffer[i].LocalAddr.lpSockaddr, result->lpcsaBuffer[i].LocalAddr.iSockaddrLength, NULL, buf, &buflen)) | ||
370 | printf("\tLocal Address #%d: %s\n", i, buf); | ||
371 | else | ||
372 | printf("\tLocal Address #%d: Can't convert: %lu\n", i, GetLastError()); | ||
373 | buflen = 1024; | ||
374 | if (NO_ERROR == WSAAddressToStringA(result->lpcsaBuffer[i].RemoteAddr.lpSockaddr, result->lpcsaBuffer[i].RemoteAddr.iSockaddrLength, NULL, buf, &buflen)) | ||
375 | printf("\tRemote Address #%d: %s\n", i, buf); | ||
376 | else | ||
377 | printf("\tRemote Address #%d: Can't convert: %lu\n", i, GetLastError()); | ||
378 | } | ||
379 | printf(" dwOutputFlags: 0x%08lX\n", result->dwOutputFlags); | ||
380 | printf(" lpBlob: 0x%p\n", result->lpBlob); | ||
381 | if (result->lpBlob) | ||
382 | { | ||
383 | struct hostent *he = malloc(result->lpBlob->cbSize); | ||
384 | if (he != NULL) | ||
385 | { | ||
386 | GNUNET_memcpy(he, result->lpBlob->pBlobData, result->lpBlob->cbSize); | ||
387 | UnpackHostEnt(he); | ||
388 | print_hostent(he); | ||
389 | free(he); | ||
390 | } | ||
391 | } | ||
392 | } | ||
393 | ret = api.NSPLookupServiceEnd(lookup); | ||
394 | if (ret != NO_ERROR) | ||
395 | printf("NSPLookupServiceEnd() failed: %lu\n", GetLastError()); | ||
396 | } | ||
397 | api.NSPCleanup(&prov); | ||
372 | } | 398 | } |
373 | } | ||
374 | } | 399 | } |
375 | ret = api.NSPLookupServiceEnd (lookup); | 400 | FreeLibrary(nsp); |
376 | if (ret != NO_ERROR) | ||
377 | printf ("NSPLookupServiceEnd() failed: %lu\n", GetLastError ()); | ||
378 | } | ||
379 | api.NSPCleanup (&prov); | ||
380 | } | 401 | } |
381 | } | ||
382 | FreeLibrary (nsp); | ||
383 | } | 402 | } |
384 | } | ||
385 | else if (wargc == 3) | 403 | else if (wargc == 3) |
386 | { | 404 | { |
387 | int s; | 405 | int s; |
388 | ADDRINFOW hints; | 406 | ADDRINFOW hints; |
389 | ADDRINFOW *result; | 407 | ADDRINFOW *result; |
390 | ADDRINFOW *pos; | 408 | ADDRINFOW *pos; |
391 | 409 | ||
392 | memset (&hints, 0, sizeof (struct addrinfo)); | 410 | memset(&hints, 0, sizeof(struct addrinfo)); |
393 | hints.ai_family = AF_UNSPEC; | 411 | hints.ai_family = AF_UNSPEC; |
394 | hints.ai_socktype = SOCK_STREAM; | 412 | hints.ai_socktype = SOCK_STREAM; |
395 | 413 | ||
396 | if (0 != (s = GetAddrInfoW (wargv[2], NULL, &hints, &result))) | 414 | if (0 != (s = GetAddrInfoW(wargv[2], NULL, &hints, &result))) |
397 | { | 415 | { |
398 | fprintf (stderr, "Cound not resolve `%S' using GetAddrInfoW: %lu\n", | 416 | fprintf(stderr, "Cound not resolve `%S' using GetAddrInfoW: %lu\n", |
399 | wargv[2], GetLastError ()); | 417 | wargv[2], GetLastError()); |
400 | } | 418 | } |
401 | else | 419 | else |
402 | { | 420 | { |
403 | for (pos = result; pos != NULL; pos = pos->ai_next) | 421 | for (pos = result; pos != NULL; pos = pos->ai_next) |
404 | { | 422 | { |
405 | wchar_t tmpbuf[1024]; | 423 | wchar_t tmpbuf[1024]; |
406 | DWORD buflen = 1024; | 424 | DWORD buflen = 1024; |
407 | if (0 == WSAAddressToStringW (pos->ai_addr, pos->ai_addrlen, NULL, tmpbuf, &buflen)) | 425 | if (0 == WSAAddressToStringW(pos->ai_addr, pos->ai_addrlen, NULL, tmpbuf, &buflen)) |
408 | fprintf (stderr, "Result:\n" | 426 | fprintf(stderr, "Result:\n" |
409 | " flags: 0x%X\n" | 427 | " flags: 0x%X\n" |
410 | " family: 0x%X\n" | 428 | " family: 0x%X\n" |
411 | " socktype: 0x%X\n" | 429 | " socktype: 0x%X\n" |
412 | " protocol: 0x%X\n" | 430 | " protocol: 0x%X\n" |
413 | " addrlen: %u\n" | 431 | " addrlen: %u\n" |
414 | " addr: %S\n" | 432 | " addr: %S\n" |
415 | " canonname: %S\n", | 433 | " canonname: %S\n", |
416 | pos->ai_flags, | 434 | pos->ai_flags, |
417 | pos->ai_family, | 435 | pos->ai_family, |
418 | pos->ai_socktype, | 436 | pos->ai_socktype, |
419 | pos->ai_protocol, | 437 | pos->ai_protocol, |
420 | pos->ai_addrlen, | 438 | pos->ai_addrlen, |
421 | tmpbuf, | 439 | tmpbuf, |
422 | pos->ai_canonname); | 440 | pos->ai_canonname); |
423 | else | 441 | else |
424 | fprintf (stderr, "Result:\n" | 442 | fprintf(stderr, "Result:\n" |
425 | " flags: 0x%X\n" | 443 | " flags: 0x%X\n" |
426 | " family: 0x%X\n" | 444 | " family: 0x%X\n" |
427 | " socktype: 0x%X\n" | 445 | " socktype: 0x%X\n" |
428 | " protocol: 0x%X\n" | 446 | " protocol: 0x%X\n" |
429 | " addrlen: %u\n" | 447 | " addrlen: %u\n" |
430 | " addr: %S\n" | 448 | " addr: %S\n" |
431 | " canonname: %S\n", | 449 | " canonname: %S\n", |
432 | pos->ai_flags, | 450 | pos->ai_flags, |
433 | pos->ai_family, | 451 | pos->ai_family, |
434 | pos->ai_socktype, | 452 | pos->ai_socktype, |
435 | pos->ai_protocol, | 453 | pos->ai_protocol, |
436 | pos->ai_addrlen, | 454 | pos->ai_addrlen, |
437 | L"<can't stringify>", | 455 | L"<can't stringify>", |
438 | pos->ai_canonname); | 456 | pos->ai_canonname); |
439 | } | 457 | } |
440 | if (NULL != result) | 458 | if (NULL != result) |
441 | FreeAddrInfoW (result); | 459 | FreeAddrInfoW(result); |
460 | } | ||
442 | } | 461 | } |
443 | } | ||
444 | WSACleanup(); | 462 | WSACleanup(); |
445 | return r; | 463 | return r; |
446 | } | 464 | } |