aboutsummaryrefslogtreecommitdiff
path: root/src/gns/w32nsp.c
blob: 082319ffef24b88b60030fc8438cf6cd77d69025 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*
     This file is part of GNUnet.
     Copyright (C) 2012 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.
    
     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @file gns/w32nsp.c
 * @brief W32 integration for GNS
 * @author LRN
 */
/* This code is partially based upon samples from the book
 * "Network Programming For Microsoft Windows, 2Nd Edition".
 */

#define VERBOSE 0
#if !VERBOSE
#  define DEBUGLOG(s, ...)
#endif
#if VERBOSE
#  define __printf__ printf
#  define DEBUGLOG(s, ...) printf (s, ##__VA_ARGS__)
#endif

#include <stdint.h>
#include <ws2tcpip.h>
#include <ws2spi.h>
#include <windows.h>
#include <nspapi.h>

#define WINDOWS 1
#define MINGW 1
#ifndef __BYTE_ORDER
#ifdef _BYTE_ORDER
#define __BYTE_ORDER _BYTE_ORDER
#else
#ifdef BYTE_ORDER
#define __BYTE_ORDER BYTE_ORDER
#endif
#endif
#endif
#ifndef __BIG_ENDIAN
#ifdef _BIG_ENDIAN
#define __BIG_ENDIAN _BIG_ENDIAN
#else
#ifdef BIG_ENDIAN
#define __BIG_ENDIAN BIG_ENDIAN
#endif
#endif
#endif
#ifndef __LITTLE_ENDIAN
#ifdef _LITTLE_ENDIAN
#define __LITTLE_ENDIAN _LITTLE_ENDIAN
#else
#ifdef LITTLE_ENDIAN
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#endif
#endif
#endif
#include "w32resolver.h"
#include <initguid.h>
#include "gnunet_w32nsp_lib.h"
#undef INITGUID

#define NSPAPI_VERSION_MAJOR 4
#define NSPAPI_VERSION_MINOR 4

static CRITICAL_SECTION records_cs;

struct record
{
  SOCKET s;
  DWORD flags;
  uint8_t state;
  char *buf;
  wchar_t *name;
};

static struct record *records = NULL;
static size_t records_len = 0;
static size_t records_size = 0;

static int
resize_records ()
{
  size_t new_size = records_len > 0 ? records_len * 2 : 5;
  struct record *new_records = malloc (new_size * sizeof (struct record));
  if (new_records == NULL)
  {
    SetLastError (WSA_NOT_ENOUGH_MEMORY);
    return 0;
  }
  GNUNET_memcpy (new_records, records, records_len * sizeof (struct record));
  memset (&new_records[records_len], 0, sizeof (struct record) * (new_size - records_len));
  records_size = new_size;
  free (records);
  records = new_records;
  return 1;
}

static int
add_record (SOCKET s, const wchar_t *name, DWORD flags)
{
  int res = 1;
  int i;
  int empty = -1;
  //EnterCriticalSection (&records_cs);
  for (i = 0; i < records_len; i++)
    if (records[i].state == 0)
      break;
  empty = i;
  if (i == records_len)
  {
    res = resize_records ();
    if (res)
      empty = records_len++;
  }
  if (res)
  {
    struct record r;
    r.s = s;
    r.flags = flags;
    r.name = (wchar_t *) name;
    r.state = 1;
    r.buf = NULL;
    if (name)
      r.name = wcsdup (name);
    records[empty] = r;
  }
  //LeaveCriticalSection (&records_cs);
  return res;
}

/* These are not defined by mingw.org headers at the moment*/
typedef INT (WSPAPI *LPNSPIOCTL) (HANDLE,DWORD,LPVOID,DWORD,LPVOID,DWORD,LPDWORD,LPWSACOMPLETION,LPWSATHREADID);
typedef struct _NSP_ROUTINE_XP {
  DWORD cbSize;
  DWORD dwMajorVersion;
  DWORD dwMinorVersion;
  LPNSPCLEANUP NSPCleanup;
  LPNSPLOOKUPSERVICEBEGIN NSPLookupServiceBegin;
  LPNSPLOOKUPSERVICENEXT NSPLookupServiceNext;
  LPNSPLOOKUPSERVICEEND NSPLookupServiceEnd;
  LPNSPSETSERVICE NSPSetService;
  LPNSPINSTALLSERVICECLASS NSPInstallServiceClass;
  LPNSPREMOVESERVICECLASS NSPRemoveServiceClass;
  LPNSPGETSERVICECLASSINFO NSPGetServiceClassInfo;
  LPNSPIOCTL NSPIoctl;
} NSP_ROUTINE_XP;

static SOCKET
connect_to_dns_resolver ()
{
  struct sockaddr_in addr;
  SOCKET r;
  int ret;

  r = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (INVALID_SOCKET == r)
  {
    SetLastError (16004);
    return r;
  }

  addr.sin_family = AF_INET;
  addr.sin_port = htons (5353); /* TCP 5353 is not registered; UDP 5353 is */
  addr.sin_addr.s_addr = inet_addr ("127.0.0.1");

  ret = connect (r, (struct sockaddr *) &addr, sizeof (addr));
  if (SOCKET_ERROR == ret)
  {
    DWORD err = GetLastError ();
    closesocket (r);
    SetLastError (err);
    SetLastError (16005);
    r = INVALID_SOCKET;
  }
  return r;
}

static int
send_name_to_ip_request (LPWSAQUERYSETW lpqsRestrictions,
    LPWSASERVICECLASSINFOW lpServiceClassInfo, DWORD dwControlFlags,
    SOCKET *resolver)
{
  struct GNUNET_W32RESOLVER_GetMessage *msg;
  int af4 = 0;
  int af6 = 0;
  char *buf;
  int ret = 1;
  int i;
  size_t size = sizeof (struct GNUNET_W32RESOLVER_GetMessage);
  size_t namelen = 0;
  if (lpqsRestrictions->lpszServiceInstanceName)
    namelen = sizeof (wchar_t) * (wcslen (lpqsRestrictions->lpszServiceInstanceName) + 1);
  size += namelen;
  buf = malloc (size);
  msg = (struct GNUNET_W32RESOLVER_GetMessage *) buf;
  msg->header.size = htons (size);
  msg->header.type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_REQUEST);
  if (lpqsRestrictions->dwNumberOfProtocols > 0)
  {
    int i;
    for (i = 0; i < lpqsRestrictions->dwNumberOfProtocols; i++)
    {
      if (lpqsRestrictions->lpafpProtocols[0].iAddressFamily == AF_INET)
        af4 = 1;
      if (lpqsRestrictions->lpafpProtocols[0].iAddressFamily == AF_INET6)
        af6 = 1;
    }
  }
  if (af4 && !af6)
    msg->af = htonl (AF_INET);
  else if (af6 && !af4)
    msg->af = htonl (AF_INET6);
  else
    msg->af = htonl (AF_UNSPEC);
  if (lpqsRestrictions->lpszServiceInstanceName)
    GNUNET_memcpy (&msg[1], lpqsRestrictions->lpszServiceInstanceName, namelen);
  msg->sc_data1 = htonl (lpqsRestrictions->lpServiceClassId->Data1);
  msg->sc_data2 = htons (lpqsRestrictions->lpServiceClassId->Data2);
  msg->sc_data3 = htons (lpqsRestrictions->lpServiceClassId->Data3);
  for (i = 0; i < 8; i++)
    msg->sc_data4[i] = lpqsRestrictions->lpServiceClassId->Data4[i];
  *resolver = connect_to_dns_resolver ();
  if (*resolver != INVALID_SOCKET)
  {
    if (size != send (*resolver, buf, size, 0))
    {
#if VERBOSE
      DWORD err = GetLastError ();
#endif
      closesocket (*resolver);
      *resolver = INVALID_SOCKET;
      DEBUGLOG ("GNUNET_W32NSP_LookupServiceBegin: failed to send request: %lu\n", err);
      SetLastError (WSATRY_AGAIN);
      ret = 0;
    }
  }
  else
    ret = 0;
  free (buf);
  return ret;
}

static int WSPAPI
NSPCleanup (LPGUID lpProviderId)
{
  DEBUGLOG ("NSPCleanup\n");
  if (IsEqualGUID (lpProviderId, &GNUNET_NAMESPACE_PROVIDER_DNS))
  {
    return NO_ERROR;
  }
  SetLastError (WSAEINVALIDPROVIDER);
  return SOCKET_ERROR;
}

BOOL WINAPI
DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
  switch (fdwReason)
  {
    case DLL_PROCESS_ATTACH:
      if (!InitializeCriticalSectionAndSpinCount (&records_cs, 0x00000400))
      {
        return FALSE;
      }
      break;
    case DLL_THREAD_ATTACH:
      break;
    case DLL_THREAD_DETACH:
      break;
    case DLL_PROCESS_DETACH:
      DeleteCriticalSection (&records_cs);
      break;
  }
  return TRUE;
}




static int WSPAPI
GNUNET_W32NSP_LookupServiceBegin (LPGUID lpProviderId, LPWSAQUERYSETW lpqsRestrictions,
    LPWSASERVICECLASSINFOW lpServiceClassInfo, DWORD dwControlFlags,
    LPHANDLE lphLookup)
{
  DEBUGLOG ("GNUNET_W32NSP_LookupServiceBegin\n");
  if (IsEqualGUID (lpProviderId, &GNUNET_NAMESPACE_PROVIDER_DNS))
  {
    SOCKET s;
    if (lpqsRestrictions->dwNameSpace != NS_DNS && lpqsRestrictions->dwNameSpace != NS_ALL)
    {
      DEBUGLOG ("GNUNET_W32NSP_LookupServiceBegin: wrong namespace\n");
      SetLastError (WSAEINVAL);
      return SOCKET_ERROR;
    }
    if (lpqsRestrictions->lpszServiceInstanceName != NULL)
    {
      wchar_t *s = lpqsRestrictions->lpszServiceInstanceName;
      size_t len = wcslen (s);
      if (len >= 5 && wcscmp (&s[len - 5], L".zkey") == 0)
      {
      }
      else if (len >= 4 && wcscmp (&s[len - 4], L".gnu") == 0)
      {
      }
      else
      {
        DEBUGLOG ("GNUNET_W32NSP_LookupServiceBegin: unsupported TLD\n");
        SetLastError (WSAEINVAL);
        return SOCKET_ERROR;
      }
    }

    if (send_name_to_ip_request (lpqsRestrictions,
        lpServiceClassInfo, dwControlFlags, &s))
    {
      if (!(add_record (s, lpqsRestrictions->lpszServiceInstanceName, dwControlFlags)))
      {
        DWORD err = GetLastError ();
        DEBUGLOG ("GNUNET_W32NSP_LookupServiceBegin: failed to add a record\n");
        closesocket (s);
        SetLastError (err);
        return SOCKET_ERROR;
      }
      *lphLookup = (HANDLE) s;
      DEBUGLOG ("GNUNET_W32NSP_LookupServiceBegin: OK (%lu)\n", GetLastError ());
      return NO_ERROR;
    }
    return SOCKET_ERROR;
  }
  DEBUGLOG ("GNUNET_W32NSP_LookupServiceBegin: wrong provider\n");
  SetLastError (WSAEINVALIDPROVIDER);
  return SOCKET_ERROR;
}

#define UnmarshallPtr(ptr, ptrtype, base) \
  if (ptr) \
    ptr = (ptrtype *) (base + (uintptr_t) ptr)

static void
UnmarshallWSAQUERYSETW (LPWSAQUERYSETW req)
{
  int i;
  char *base = (char *) req;
  UnmarshallPtr (req->lpszServiceInstanceName, wchar_t, base);
  UnmarshallPtr (req->lpServiceClassId, GUID, base);
  UnmarshallPtr (req->lpVersion, WSAVERSION, base);
  UnmarshallPtr (req->lpszComment, wchar_t, base);
  UnmarshallPtr (req->lpNSProviderId, GUID, base);
  UnmarshallPtr (req->lpszContext, wchar_t, base);
  UnmarshallPtr (req->lpafpProtocols, AFPROTOCOLS, base);
  UnmarshallPtr (req->lpszQueryString, wchar_t, base);
  UnmarshallPtr (req->lpcsaBuffer, CSADDR_INFO, base);
  for (i = 0; i < req->dwNumberOfCsAddrs; i++)
  {
    UnmarshallPtr (req->lpcsaBuffer[i].LocalAddr.lpSockaddr, SOCKADDR, base);
    UnmarshallPtr (req->lpcsaBuffer[i].RemoteAddr.lpSockaddr, SOCKADDR, base);
  }
  UnmarshallPtr (req->lpBlob, BLOB, base);
  if (req->lpBlob)
    UnmarshallPtr (req->lpBlob->pBlobData, BYTE, base);
}

static int WSAAPI
GNUNET_W32NSP_LookupServiceNext (HANDLE hLookup, DWORD dwControlFlags,
    LPDWORD lpdwBufferLength, LPWSAQUERYSETW lpqsResults)
{
  /*DWORD effective_flags;*/
  int i;
  struct GNUNET_MessageHeader header = {0, 0};
  int rec = -1;
  int rc;
  int to_receive;
  int t;
  char *buf;

  DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext\n");
  //EnterCriticalSection (&records_cs);
  for (i = 0; i < records_len; i++)
  {
    if (records[i].s == (SOCKET) hLookup)
    {
      rec = i;
      break;
    }
  }
  if (rec == -1)
  {
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: invalid handle\n");
    SetLastError (WSA_INVALID_HANDLE);
    //LeaveCriticalSection (&records_cs);
    return SOCKET_ERROR;
  }
  if (records[rec].state & 4)
  {
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: session is closed\n");
    SetLastError (WSA_E_NO_MORE);
    //LeaveCriticalSection (&records_cs);
    return SOCKET_ERROR;
  }
  /*effective_flags = dwControlFlags & records[rec].flags;*/
  if (records[rec].buf)
  {
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: checking buffer\n");
    header = *((struct GNUNET_MessageHeader *) records[rec].buf);
    if (*lpdwBufferLength < header.size - sizeof (struct GNUNET_W32RESOLVER_GetMessage))
    {
      DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: client buffer is too small\n");
      SetLastError (WSAEFAULT);
      //LeaveCriticalSection (&records_cs);
      return SOCKET_ERROR;
    }
    GNUNET_memcpy (lpqsResults, &((struct GNUNET_W32RESOLVER_GetMessage *)records[rec].buf)[1], header.size - sizeof (struct GNUNET_W32RESOLVER_GetMessage));
    free (records[rec].buf);
    records[rec].buf = NULL;
    //LeaveCriticalSection (&records_cs);
    UnmarshallWSAQUERYSETW ((LPWSAQUERYSETW) lpqsResults);
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: OK (from buffer)\n");
    return NO_ERROR;
  }
  records[rec].state |= 8;
  //LeaveCriticalSection (&records_cs);
  to_receive = sizeof (header);
  rc = 0;
#if VERBOSE
  {
    unsigned long have;
    int ior = ioctlsocket ((SOCKET) hLookup, FIONREAD, &have);
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: reading %d bytes as a header from %p, %lu bytes available\n", to_receive, hLookup, have);
  }
#endif
  while (to_receive > 0)
  {
    t = recv ((SOCKET) hLookup, &((char *) &header)[rc], to_receive, 0);
    if (t > 0)
    {
      rc += t;
      to_receive -= t;
    }
    else
      break;
  }
#if VERBOSE
  {
    unsigned long have;
    int ior = ioctlsocket ((SOCKET) hLookup, FIONREAD, &have);
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: read %d bytes as a header from %p, %lu bytes available\n", rc, hLookup, have);
  }
#endif
  //EnterCriticalSection (&records_cs);
  records[rec].state &= ~8;
  if (rc != sizeof (header))
  {
    if (records[rec].state & 2)
    {
      DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: call cancelled\n");
      SetLastError (WSA_E_CANCELLED);
    }
    else
    {
      DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: failed to receive enough data for a header (rc %d != %u, state is 0x%0X)\n", rc, sizeof (header), records[rec].state);
      SetLastError (WSA_E_NO_MORE);
    }
    records[rec].state |= 4;
    //LeaveCriticalSection (&records_cs);
    return SOCKET_ERROR;
  }
  records[rec].state &= ~8;
  header.type = ntohs (header.type);
  header.size = ntohs (header.size);
  DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: header type %d, header size %u\n", header.type, header.size);
  if (header.type != GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE ||
      (header.type == GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE &&
      header.size == sizeof (header)))
  {
    records[rec].state |= 4;
    if (header.type != GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE)
      DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: header type is wrong\n");
    else
      DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: empty header - no data\n");
    //LeaveCriticalSection (&records_cs);
    SetLastError (WSA_E_NO_MORE);
    return SOCKET_ERROR;
  }
  buf = malloc (header.size);
  if (buf == NULL)
  {
    records[rec].state |= 4;
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: malloc() failed\n");
    //LeaveCriticalSection (&records_cs);
    SetLastError (WSA_E_NO_MORE);
    return SOCKET_ERROR;
  }
  records[rec].state |= 8;
  //LeaveCriticalSection (&records_cs);
  GNUNET_memcpy (buf, &header, sizeof (header));
  to_receive = header.size - sizeof (header);
  rc = 0;
#if VERBOSE
  {
    unsigned long have;
    int ior = ioctlsocket ((SOCKET) hLookup, FIONREAD, &have);
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: reading %d bytes as a body from %p, %lu bytes available\n", to_receive, hLookup, have);
  }
#endif
  while (to_receive > 0)
  {
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: recv (%d)\n", to_receive);
    t = recv ((SOCKET) hLookup, &((char *) &((struct GNUNET_MessageHeader *) buf)[1])[rc], to_receive, 0);
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: recv returned %d\n", t);
    if (t > 0)
    {
      rc += t;
      to_receive -= t;
    }
    else
      break;
  }
#if VERBOSE
  {
    unsigned long have;
    int ior = ioctlsocket ((SOCKET) hLookup, FIONREAD, &have);
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: read %d bytes as a body from %p, %lu bytes available\n", rc, hLookup, have);
  }
#endif
  //EnterCriticalSection (&records_cs);
  records[rec].state &= ~8;
  if (rc != header.size - sizeof (header))
  {
    free (buf);
    if (records[rec].state & 2)
    {
      DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: call cancelled\n");
      SetLastError (WSA_E_CANCELLED);
    }
    else
    {
      DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: failed to receive enough data for the rest (rc %d != %d, state is 0x%0X)\n", rc, header.size - sizeof (header), records[rec].state);
      SetLastError (WSA_E_NO_MORE);
    }
    records[rec].state |= 4;
    //LeaveCriticalSection (&records_cs);
    return SOCKET_ERROR;
  }
  if (*lpdwBufferLength < header.size - sizeof (struct GNUNET_W32RESOLVER_GetMessage))
  {
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: client buffer is too small\n");
    SetLastError (WSAEFAULT);
    records[rec].buf = buf;
    //LeaveCriticalSection (&records_cs);
    return SOCKET_ERROR;
  }
  //LeaveCriticalSection (&records_cs);
  DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: writing %d bytes into result buffer\n", header.size - sizeof (struct GNUNET_W32RESOLVER_GetMessage));
  GNUNET_memcpy (lpqsResults, &((struct GNUNET_W32RESOLVER_GetMessage *)buf)[1], header.size - sizeof (struct GNUNET_W32RESOLVER_GetMessage));
  free (buf);
  DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: OK\n");
  UnmarshallWSAQUERYSETW ((LPWSAQUERYSETW) lpqsResults);
  DEBUGLOG ("GNUNET_W32NSP_LookupServiceNext: returning (%lu)\n", GetLastError ());
  return NO_ERROR;
}

static int WSPAPI
GNUNET_W32NSP_LookupServiceEnd (HANDLE hLookup)
{
  int i;
  int rec = -1;

  DEBUGLOG ("GNUNET_W32NSP_LookupServiceEnd\n");
  //EnterCriticalSection (&records_cs);
  for (i = 0; i < records_len; i++)
  {
    if (records[i].s == (SOCKET) hLookup)
    {
      rec = i;
      break;
    }
  }
  if (rec == -1)
  {
    SetLastError (WSA_INVALID_HANDLE);
    //LeaveCriticalSection (&records_cs);
    DEBUGLOG ("GNUNET_W32NSP_LookupServiceEnd: invalid handle\n");
    return SOCKET_ERROR;
  }
  records[rec].state |= 2;
  closesocket (records[rec].s);
  while (records[rec].state & 8)
  {
    //LeaveCriticalSection (&records_cs);
    Sleep (10);
    //EnterCriticalSection (&records_cs);
  }
  if (records[rec].buf)
    free (records[rec].buf);
  records[rec].buf = NULL;
  records[rec].state = 0;
  if (records[rec].name)
    free (records[rec].name);
  //LeaveCriticalSection (&records_cs);
  DEBUGLOG ("GNUNET_W32NSP_LookupServiceEnd: OK\n");
  return NO_ERROR;
}

static int WSAAPI
GNUNET_W32NSP_SetService (LPGUID lpProviderId,
    LPWSASERVICECLASSINFOW lpServiceClassInfo, LPWSAQUERYSETW lpqsRegInfo,
    WSAESETSERVICEOP essOperation, DWORD dwControlFlags)
{
  DEBUGLOG ("GNUNET_W32NSP_SetService\n");
  SetLastError (WSAEOPNOTSUPP);
  return SOCKET_ERROR;
}

static int WSAAPI
GNUNET_W32NSP_InstallServiceClass (LPGUID lpProviderId,
    LPWSASERVICECLASSINFOW lpServiceClassInfo)
{
  DEBUGLOG ("GNUNET_W32NSP_InstallServiceClass\n");
  SetLastError (WSAEOPNOTSUPP);
  return SOCKET_ERROR;
}


static int WSAAPI
GNUNET_W32NSP_RemoveServiceClass (LPGUID lpProviderId, LPGUID lpServiceClassId)
{
  DEBUGLOG ("GNUNET_W32NSP_RemoveServiceClass\n");
  SetLastError (WSAEOPNOTSUPP);
  return SOCKET_ERROR;
}

static int WSAAPI
GNUNET_W32NSP_GetServiceClassInfo (LPGUID lpProviderId, LPDWORD lpdwBufSize,
  LPWSASERVICECLASSINFOW lpServiceClassInfo)
{
  DEBUGLOG ("GNUNET_W32NSP_GetServiceClassInfo\n");
  SetLastError (WSAEOPNOTSUPP);
  return SOCKET_ERROR;
}

static int WSAAPI
GNUNET_W32NSP_Ioctl (HANDLE hLookup, DWORD dwControlCode, LPVOID lpvInBuffer,
    DWORD cbInBuffer, LPVOID lpvOutBuffer, DWORD cbOutBuffer,
    LPDWORD lpcbBytesReturned, LPWSACOMPLETION lpCompletion,
    LPWSATHREADID lpThreadId)
{
  DEBUGLOG ("GNUNET_W32NSP_Ioctl\n");
  SetLastError (WSAEOPNOTSUPP);
  return SOCKET_ERROR;
}

/**
 * This function is called by Winsock to hook up our provider.
 * It is the only function that [should be/is] exported by the
 * provider. All other routines are passed as pointers in lpnspRoutines.
 */
int WSAAPI
GNUNET_W32NSP_NSPStartup (LPGUID lpProviderId, LPNSP_ROUTINE lpnspRoutines)
{
  if (IsEqualGUID (lpProviderId, &GNUNET_NAMESPACE_PROVIDER_DNS))
  {
    if (!connect_to_dns_resolver ())
    {
      return SOCKET_ERROR;
    }
    /* This assumes that NSP_ROUTINE struct doesn't have a NSPIoctl member.
     * If it does, you need to use FIELD_OFFSET() macro to get offset of NSPIoctl
     * and use that offset as cbSize.
     */
    lpnspRoutines->cbSize = sizeof(NSP_ROUTINE);

    lpnspRoutines->dwMajorVersion = NSPAPI_VERSION_MAJOR;
    lpnspRoutines->dwMinorVersion = NSPAPI_VERSION_MINOR;
    lpnspRoutines->NSPCleanup = NSPCleanup;
    lpnspRoutines->NSPLookupServiceBegin = GNUNET_W32NSP_LookupServiceBegin;
    lpnspRoutines->NSPLookupServiceNext = GNUNET_W32NSP_LookupServiceNext;
    lpnspRoutines->NSPLookupServiceEnd = GNUNET_W32NSP_LookupServiceEnd;
    lpnspRoutines->NSPSetService = GNUNET_W32NSP_SetService;
    lpnspRoutines->NSPInstallServiceClass = GNUNET_W32NSP_InstallServiceClass;
    lpnspRoutines->NSPRemoveServiceClass = GNUNET_W32NSP_RemoveServiceClass;
    lpnspRoutines->NSPGetServiceClassInfo = GNUNET_W32NSP_GetServiceClassInfo;
    /*((NSP_ROUTINE_XP *) lpnspRoutines)->NSPIoctl = GNUNET_W32NSP_Ioctl;*/
    lpnspRoutines->NSPIoctl = GNUNET_W32NSP_Ioctl;
    return NO_ERROR;
  }
  SetLastError (WSAEINVALIDPROVIDER);
  return SOCKET_ERROR;
}