Improve error reporting.
[wine.git] / dlls / netapi32 / nbt.c
blob82941c4538f7f8c7ecf05cc39034aa8bff324986
1 /* Copyright (c) 2003 Juan Lang
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * I am heavily indebted to Chris Hertel's excellent Implementing CIFS,
18 * http://ubiqx.org/cifs/ , for whatever understanding I have of NBT.
19 * I also stole from Mike McCormack's smb.c and netapi32.c, although little of
20 * that code remains.
21 * Lack of understanding and bugs are my fault.
23 * FIXME:
24 * - Of the NetBIOS session functions, only client functions are supported, and
25 * it's likely they'll be the only functions supported. NBT requires session
26 * servers to listen on TCP/139. This requires root privilege, and Samba is
27 * likely to be listening here already. This further restricts NetBIOS
28 * applications, both explicit users and implicit ones: CreateNamedPipe
29 * won't actually create a listening pipe, for example, so applications can't
30 * act as RPC servers using a named pipe protocol binding, DCOM won't be able
31 * to support callbacks or servers over the named pipe protocol, etc.
33 * - Datagram support is omitted for the same reason. To send a NetBIOS
34 * datagram, you must include the NetBIOS name by which your application is
35 * known. This requires you to have registered the name previously, and be
36 * able to act as a NetBIOS datagram server (listening on UDP/138).
38 * - Name registration functions are omitted for the same reason--registering a
39 * name requires you to be able to defend it, and this means listening on
40 * UDP/137.
41 * Win98 requires you either use your computer's NetBIOS name (with the NULL
42 * suffix byte) as the calling name when creating a session, or to register
43 * a new name before creating one: it disallows '*' as the calling name.
44 * Win2K initially starts with an empty name table, and doesn't allow you to
45 * use the machine's NetBIOS name (with the NULL suffix byte) as the calling
46 * name. Although it allows sessions to be created with '*' as the calling
47 * name, doing so results in timeouts for all receives, because the
48 * application never gets them.
49 * So, a well-behaved Netbios application will typically want to register a
50 * name. I should probably support a do-nothing name list that allows
51 * NCBADDNAME to add to it, but doesn't actually register the name, or does
52 * attempt to register it without being able to defend it.
54 * - Name lookups may not behave quite as you'd expect/like if you have
55 * multiple LANAs. If a name is resolvable through DNS, or if you're using
56 * WINS, it'll resolve on _any_ LANA. So, a Call will succeed on any LANA as
57 * well.
58 * I'm not sure how Windows behaves in this case. I could try to force
59 * lookups to the correct adapter by using one of the GetPreferred*
60 * functions, but with the possibility of multiple adapters in the same
61 * same subnet, there's no guarantee that what IpHlpApi thinks is the
62 * preferred adapter will actually be a LANA. (It's highly probable because
63 * this is an unusual configuration, but not guaranteed.)
65 * See also other FIXMEs in the code.
68 #include "config.h"
70 #include <stdarg.h>
71 #include "windef.h"
72 #include "winbase.h"
73 #include "wine/debug.h"
74 #include "winreg.h"
75 #include "iphlpapi.h"
76 #include "winsock2.h"
77 #include "netbios.h"
78 #include "nbnamecache.h"
80 WINE_DEFAULT_DEBUG_CHANNEL(netbios);
82 #define PORT_NBNS 137
83 #define PORT_NBDG 138
84 #define PORT_NBSS 139
86 #define NBR_ADDWORD(p,word) (*(WORD *)(p)) = htons(word)
87 #define NBR_GETWORD(p) ntohs(*(WORD *)(p))
89 #define MIN_QUERIES 1
90 #define MAX_QUERIES 0xffff
91 #define MIN_QUERY_TIMEOUT 100
92 #define MAX_QUERY_TIMEOUT 0xffffffff
93 #define BCAST_QUERIES 3
94 #define BCAST_QUERY_TIMEOUT 750
95 #define WINS_QUERIES 3
96 #define WINS_QUERY_TIMEOUT 750
97 #define MAX_WINS_SERVERS 2
98 #define MIN_CACHE_TIMEOUT 60000
99 #define CACHE_TIMEOUT 360000
101 #define MAX_NBT_NAME_SZ (NCBNAMSZ * 2 + MAX_DOMAIN_NAME_LEN + 2)
102 #define SIMPLE_NAME_QUERY_PKT_SIZE 26 + MAX_NBT_NAME_SZ
104 #define DEFAULT_NBT_SESSIONS 16
106 #define NBNS_TYPE_NB 0x0020
107 #define NBNS_TYPE_NBSTAT 0x0021
108 #define NBNS_CLASS_INTERNET 0x00001
109 #define NBNS_HEADER_SIZE (sizeof(WORD) * 6)
110 #define NBNS_RESPONSE_AND_OPCODE 0xf800
111 #define NBNS_RESPONSE_AND_QUERY 0x8000
112 #define NBNS_REPLYCODE 0x0f
114 #define NBSS_HDRSIZE 4
116 #define NBSS_MSG 0x00
117 #define NBSS_REQ 0x81
118 #define NBSS_ACK 0x82
119 #define NBSS_NACK 0x83
120 #define NBSS_RETARGET 0x84
121 #define NBSS_KEEPALIVE 0x85
123 #define NBSS_ERR_NOT_LISTENING_ON_NAME 0x80
124 #define NBSS_ERR_NOT_LISTENING_FOR_CALLER 0x81
125 #define NBSS_ERR_BAD_NAME 0x82
126 #define NBSS_ERR_INSUFFICIENT_RESOURCES 0x83
128 #define NBSS_EXTENSION 0x01
130 typedef struct _NetBTSession
132 CRITICAL_SECTION cs;
133 SOCKET fd;
134 DWORD bytesPending;
135 } NetBTSession;
137 typedef struct _NetBTAdapter
139 MIB_IPADDRROW ipr;
140 WORD nameQueryXID;
141 struct NBNameCache *nameCache;
142 DWORD xmit_success;
143 DWORD recv_success;
144 } NetBTAdapter;
146 static ULONG gTransportID;
147 static BOOL gEnableDNS;
148 static DWORD gBCastQueries;
149 static DWORD gBCastQueryTimeout;
150 static DWORD gWINSQueries;
151 static DWORD gWINSQueryTimeout;
152 static DWORD gWINSServers[MAX_WINS_SERVERS];
153 static int gNumWINSServers;
154 static char gScopeID[MAX_DOMAIN_NAME_LEN];
155 static DWORD gCacheTimeout;
156 static struct NBNameCache *gNameCache;
158 /* Converts from a NetBIOS name into a Second Level Encoding-formatted name.
159 * Assumes p is not NULL and is either NULL terminated or has at most NCBNAMSZ
160 * bytes, and buffer has at least MAX_NBT_NAME_SZ bytes. Pads with space bytes
161 * if p is NULL-terminated. Returns the number of bytes stored in buffer.
163 static int NetBTNameEncode(const UCHAR *p, UCHAR *buffer)
165 int i,len=0;
167 if (!p) return 0;
168 if (!buffer) return 0;
170 buffer[len++] = NCBNAMSZ * 2;
171 for (i = 0; p[i] && i < NCBNAMSZ; i++)
173 buffer[len++] = ((p[i] & 0xf0) >> 4) + 'A';
174 buffer[len++] = (p[i] & 0x0f) + 'A';
176 while (len < NCBNAMSZ * 2)
178 buffer[len++] = 'C';
179 buffer[len++] = 'A';
181 if (*gScopeID)
183 int scopeIDLen = strlen(gScopeID);
185 memcpy(buffer + len, gScopeID, scopeIDLen);
186 len += scopeIDLen;
188 buffer[len++] = 0; /* add second terminator */
189 return len;
192 /* Creates a NBT name request packet for name in buffer. If broadcast is true,
193 * creates a broadcast request, otherwise creates a unicast request.
194 * Returns the number of bytes stored in buffer.
196 static DWORD NetBTNameReq(const UCHAR name[NCBNAMSZ], WORD xid, WORD qtype,
197 BOOL broadcast, UCHAR *buffer, int len)
199 int i = 0;
201 if (len < SIMPLE_NAME_QUERY_PKT_SIZE) return 0;
203 NBR_ADDWORD(&buffer[i],xid); i+=2; /* transaction */
204 if (broadcast)
206 NBR_ADDWORD(&buffer[i],0x0110); /* flags: r=req,op=query,rd=1,b=1 */
207 i+=2;
209 else
211 NBR_ADDWORD(&buffer[i],0x0100); /* flags: r=req,op=query,rd=1,b=0 */
212 i+=2;
214 NBR_ADDWORD(&buffer[i],0x0001); i+=2; /* one name query */
215 NBR_ADDWORD(&buffer[i],0x0000); i+=2; /* zero answers */
216 NBR_ADDWORD(&buffer[i],0x0000); i+=2; /* zero authorities */
217 NBR_ADDWORD(&buffer[i],0x0000); i+=2; /* zero additional */
219 i += NetBTNameEncode(name, &buffer[i]);
221 NBR_ADDWORD(&buffer[i],qtype); i+=2;
222 NBR_ADDWORD(&buffer[i],NBNS_CLASS_INTERNET); i+=2;
224 return i;
227 /* Sends a name query request for name on fd to destAddr. Sets SO_BROADCAST on
228 * fd if broadcast is TRUE. Assumes fd is not INVALID_SOCKET, and name is not
229 * NULL.
230 * Returns 0 on success, -1 on failure.
232 static int NetBTSendNameQuery(SOCKET fd, const UCHAR name[NCBNAMSZ], WORD xid,
233 WORD qtype, DWORD destAddr, BOOL broadcast)
235 int ret = 0, on = 1;
236 struct in_addr addr;
238 addr.s_addr = destAddr;
239 TRACE("name %s, dest addr %s\n", name, inet_ntoa(addr));
241 if (broadcast)
242 ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (PUCHAR)&on, sizeof(on));
243 if(ret == 0)
245 WSABUF wsaBuf;
246 UCHAR buf[SIMPLE_NAME_QUERY_PKT_SIZE];
247 struct sockaddr_in sin;
249 memset(&sin, 0, sizeof(sin));
250 sin.sin_addr.s_addr = destAddr;
251 sin.sin_family = AF_INET;
252 sin.sin_port = htons(PORT_NBNS);
254 wsaBuf.buf = buf;
255 wsaBuf.len = NetBTNameReq(name, xid, qtype, broadcast, buf,
256 sizeof(buf));
257 if (wsaBuf.len > 0)
259 DWORD bytesSent;
261 ret = WSASendTo(fd, &wsaBuf, 1, &bytesSent, 0,
262 (struct sockaddr*)&sin, sizeof(sin), NULL, NULL);
263 if (ret < 0 || bytesSent < wsaBuf.len)
264 ret = -1;
265 else
266 ret = 0;
268 else
269 ret = -1;
271 return ret;
274 typedef BOOL (*NetBTAnswerCallback)(void *data, WORD answerCount,
275 WORD answerIndex, PUCHAR rData, WORD rdLength);
277 /* Waits on fd until GetTickCount() returns a value greater than or equal to
278 * waitUntil for a name service response. If a name response matching xid
279 * is received, calls answerCallback once for each answer resource record in
280 * the response. (The callback's answerCount will be the total number of
281 * answers to expect, and answerIndex will be the 0-based index that's being
282 * sent this time.) Quits parsing if answerCallback returns FALSE.
283 * Returns NRC_GOODRET on timeout or a valid response received, something else
284 * on error.
286 static UCHAR NetBTWaitForNameResponse(NetBTAdapter *adapter, SOCKET fd,
287 DWORD waitUntil, NetBTAnswerCallback answerCallback, void *data)
289 BOOL found = FALSE;
290 DWORD now;
291 UCHAR ret = NRC_GOODRET;
293 if (!adapter) return NRC_BADDR;
294 if (fd == INVALID_SOCKET) return NRC_BADDR;
295 if (!answerCallback) return NRC_BADDR;
297 while (!found && ret == NRC_GOODRET && (now = GetTickCount()) < waitUntil)
299 DWORD msToWait = waitUntil - now;
300 FD_SET fds;
301 struct timeval timeout = { msToWait / 1000, msToWait % 1000 };
302 int r;
304 FD_ZERO(&fds);
305 FD_SET(fd, &fds);
306 r = select(fd + 1, &fds, NULL, NULL, &timeout);
307 if (r < 0)
308 ret = NRC_SYSTEM;
309 else if (r == 1)
311 /* FIXME: magic #, is this always enough? */
312 UCHAR buffer[256];
313 int fromsize;
314 struct sockaddr_in fromaddr;
315 WORD respXID, flags, queryCount, answerCount;
316 WSABUF wsaBuf = { sizeof(buffer), buffer };
317 DWORD bytesReceived, recvFlags = 0;
319 fromsize = sizeof(fromaddr);
320 r = WSARecvFrom(fd, &wsaBuf, 1, &bytesReceived, &recvFlags,
321 (struct sockaddr*)&fromaddr, &fromsize, NULL, NULL);
322 if(r < 0)
324 ret = NRC_SYSTEM;
325 break;
328 if (bytesReceived < NBNS_HEADER_SIZE)
329 continue;
331 respXID = NBR_GETWORD(buffer);
332 if (adapter->nameQueryXID != respXID)
333 continue;
335 flags = NBR_GETWORD(buffer + 2);
336 queryCount = NBR_GETWORD(buffer + 4);
337 answerCount = NBR_GETWORD(buffer + 6);
339 /* a reply shouldn't contain a query, ignore bad packet */
340 if (queryCount > 0)
341 continue;
343 if ((flags & NBNS_RESPONSE_AND_OPCODE) == NBNS_RESPONSE_AND_QUERY)
345 if ((flags & NBNS_REPLYCODE) != 0)
346 ret = NRC_NAMERR;
347 else if ((flags & NBNS_REPLYCODE) == 0 && answerCount > 0)
349 PUCHAR ptr = buffer + NBNS_HEADER_SIZE;
350 BOOL shouldContinue = TRUE;
351 WORD answerIndex = 0;
353 found = TRUE;
354 /* decode one answer at a time */
355 while (ret == NRC_GOODRET && answerIndex < answerCount &&
356 ptr - buffer < bytesReceived && shouldContinue)
358 WORD rLen;
360 /* scan past name */
361 for (; ptr[0] && ptr - buffer < bytesReceived; )
362 ptr += ptr[0] + 1;
363 ptr++;
364 ptr += 2; /* scan past type */
365 if (ptr - buffer < bytesReceived && ret == NRC_GOODRET
366 && NBR_GETWORD(ptr) == NBNS_CLASS_INTERNET)
367 ptr += sizeof(WORD);
368 else
369 ret = NRC_SYSTEM; /* parse error */
370 ptr += sizeof(DWORD); /* TTL */
371 rLen = NBR_GETWORD(ptr);
372 rLen = min(rLen, bytesReceived - (ptr - buffer));
373 ptr += sizeof(WORD);
374 shouldContinue = answerCallback(data, answerCount,
375 answerIndex, ptr, rLen);
376 ptr += rLen;
377 answerIndex++;
383 TRACE("returning 0x%02x\n", ret);
384 return ret;
387 typedef struct _NetBTNameQueryData {
388 NBNameCacheEntry *cacheEntry;
389 UCHAR ret;
390 } NetBTNameQueryData;
392 /* Name query callback function for NetBTWaitForNameResponse, creates a cache
393 * entry on the first answer, adds each address as it's called again (as long
394 * as there's space). If there's an error that should be propagated as the
395 * NetBIOS error, modifies queryData's ret member to the proper return code.
397 static BOOL NetBTFindNameAnswerCallback(void *pVoid, WORD answerCount,
398 WORD answerIndex, PUCHAR rData, WORD rLen)
400 NetBTNameQueryData *queryData = (NetBTNameQueryData *)pVoid;
401 BOOL ret;
403 if (queryData)
405 if (queryData->cacheEntry == NULL)
407 queryData->cacheEntry = (NBNameCacheEntry *)HeapAlloc(
408 GetProcessHeap(), 0, sizeof(NBNameCacheEntry) +
409 (answerCount - 1) * sizeof(DWORD));
410 if (queryData->cacheEntry)
411 queryData->cacheEntry->numAddresses = 0;
412 else
414 ret = FALSE;
415 queryData->ret = NRC_OSRESNOTAV;
418 if (rLen == 6 && queryData->cacheEntry &&
419 queryData->cacheEntry->numAddresses < answerCount)
421 queryData->cacheEntry->addresses[queryData->cacheEntry->
422 numAddresses++] = *(PDWORD)(rData + 2);
423 ret = queryData->cacheEntry->numAddresses < answerCount;
425 else
426 ret = FALSE;
428 else
429 ret = FALSE;
430 return ret;
433 /* Workhorse NetBT name lookup function. Sends a name lookup query for
434 * ncb->ncb_callname to sendTo, as a broadcast if broadcast is TRUE, using
435 * adapter->nameQueryXID as the transaction ID. Waits up to timeout
436 * milliseconds, and retries up to maxQueries times, waiting for a reply.
437 * If a valid response is received, stores the looked up addresses as a
438 * NBNameCacheEntry in *cacheEntry.
439 * Returns NRC_GOODRET on success, though this may not mean the name was
440 * resolved--check whether *cacheEntry is NULL.
442 static UCHAR NetBTNameWaitLoop(NetBTAdapter *adapter, SOCKET fd, PNCB ncb,
443 DWORD sendTo, BOOL broadcast, DWORD timeout, DWORD maxQueries,
444 NBNameCacheEntry **cacheEntry)
446 int queries;
447 NetBTNameQueryData queryData;
449 if (!adapter) return NRC_BADDR;
450 if (fd == INVALID_SOCKET) return NRC_BADDR;
451 if (!ncb) return NRC_BADDR;
452 if (!cacheEntry) return NRC_BADDR;
454 queryData.cacheEntry = NULL;
455 queryData.ret = NRC_GOODRET;
456 for (queries = 0; queryData.cacheEntry == NULL && queries < maxQueries;
457 queries++)
459 if (!NCB_CANCELLED(ncb))
461 int r = NetBTSendNameQuery(fd, ncb->ncb_callname,
462 adapter->nameQueryXID, NBNS_TYPE_NB, sendTo, broadcast);
464 if (r == 0)
465 queryData.ret = NetBTWaitForNameResponse(adapter, fd,
466 GetTickCount() + timeout, NetBTFindNameAnswerCallback,
467 &queryData);
468 else
469 queryData.ret = NRC_SYSTEM;
471 else
472 queryData.ret = NRC_CMDCAN;
474 if (queryData.cacheEntry)
476 memcpy(queryData.cacheEntry->name, ncb->ncb_callname, NCBNAMSZ);
477 memcpy(queryData.cacheEntry->nbname, ncb->ncb_callname, NCBNAMSZ);
479 *cacheEntry = queryData.cacheEntry;
480 return queryData.ret;
483 /* Attempts to add cacheEntry to the name cache in *nameCache; if *nameCache
484 * has not yet been created, creates it, using gCacheTimeout as the cache
485 * entry timeout. If memory allocation fails, or if NBNameCacheAddEntry fails,
486 * frees cacheEntry.
487 * Returns NRC_GOODRET on success, and something else on failure.
489 static UCHAR NetBTStoreCacheEntry(struct NBNameCache **nameCache,
490 NBNameCacheEntry *cacheEntry)
492 UCHAR ret;
494 if (!nameCache) return NRC_BADDR;
495 if (!cacheEntry) return NRC_BADDR;
497 if (!*nameCache)
498 *nameCache = NBNameCacheCreate(GetProcessHeap(), gCacheTimeout);
499 if (*nameCache)
500 ret = NBNameCacheAddEntry(*nameCache, cacheEntry)
501 ? NRC_GOODRET : NRC_OSRESNOTAV;
502 else
504 HeapFree(GetProcessHeap(), 0, cacheEntry);
505 ret = NRC_OSRESNOTAV;
507 return ret;
510 /* Attempts to resolve name using inet_addr(), then gethostbyname() if
511 * gEnableDNS is TRUE, if the suffix byte is either <00> or <20>. If the name
512 * can be looked up, returns 0 and stores the looked up addresses as a
513 * NBNameCacheEntry in *cacheEntry.
514 * Returns NRC_GOODRET on success, though this may not mean the name was
515 * resolved--check whether *cacheEntry is NULL. Returns something else on
516 * error.
518 static UCHAR NetBTinetResolve(const UCHAR name[NCBNAMSZ],
519 NBNameCacheEntry **cacheEntry)
521 UCHAR ret = NRC_GOODRET;
523 TRACE("name %s, cacheEntry %p\n", name, cacheEntry);
525 if (!name) return NRC_BADDR;
526 if (!cacheEntry) return NRC_BADDR;
528 if (isalnum(name[0]) && (name[NCBNAMSZ - 1] == 0 ||
529 name[NCBNAMSZ - 1] == 0x20))
531 UCHAR toLookup[NCBNAMSZ];
532 int i;
534 for (i = 0; i < NCBNAMSZ - 1 && name[i] && name[i] != ' '; i++)
535 toLookup[i] = name[i];
536 toLookup[i] = '\0';
538 if (isdigit(toLookup[0]))
540 unsigned long addr = inet_addr(toLookup);
542 if (addr != INADDR_NONE)
544 *cacheEntry = (NBNameCacheEntry *)HeapAlloc(GetProcessHeap(),
545 0, sizeof(NBNameCacheEntry));
546 if (*cacheEntry)
548 memcpy((*cacheEntry)->name, name, NCBNAMSZ);
549 memset((*cacheEntry)->nbname, 0, NCBNAMSZ);
550 (*cacheEntry)->nbname[0] = '*';
551 (*cacheEntry)->numAddresses = 1;
552 (*cacheEntry)->addresses[0] = addr;
554 else
555 ret = NRC_OSRESNOTAV;
558 if (gEnableDNS && ret == NRC_GOODRET && !*cacheEntry)
560 struct hostent *host;
562 if ((host = gethostbyname(toLookup)) != NULL)
564 for (i = 0; ret == NRC_GOODRET && host->h_addr_list &&
565 host->h_addr_list[i]; i++)
567 if (host->h_addr_list && host->h_addr_list[0])
569 *cacheEntry = (NBNameCacheEntry *)HeapAlloc(
570 GetProcessHeap(), 0, sizeof(NBNameCacheEntry) +
571 (i - 1) * sizeof(DWORD));
572 if (*cacheEntry)
574 memcpy((*cacheEntry)->name, name, NCBNAMSZ);
575 memset((*cacheEntry)->nbname, 0, NCBNAMSZ);
576 (*cacheEntry)->nbname[0] = '*';
577 (*cacheEntry)->numAddresses = i;
578 for (i = 0; i < (*cacheEntry)->numAddresses; i++)
579 (*cacheEntry)->addresses[i] =
580 (DWORD)host->h_addr_list[i];
582 else
583 ret = NRC_OSRESNOTAV;
589 TRACE("returning 0x%02x\n", ret);
590 return ret;
593 /* Looks up the name in ncb->ncb_callname, first in the name caches (global
594 * and this adapter's), then using gethostbyname(), next by WINS if configured,
595 * and finally using broadcast NetBT name resolution. In NBT parlance, this
596 * makes this an "H-node". Stores an entry in the appropriate name cache for a
597 * found node, and returns it as *cacheEntry.
598 * Assumes data, ncb, and cacheEntry are not NULL.
599 * Returns NRC_GOODRET on success--which doesn't mean the name was resolved,
600 * just that all name lookup operations completed successfully--and something
601 * else on failure. *cacheEntry will be NULL if the name was not found.
603 static UCHAR NetBTInternalFindName(NetBTAdapter *adapter, PNCB ncb,
604 const NBNameCacheEntry **cacheEntry)
606 UCHAR ret = NRC_GOODRET;
608 TRACE("adapter %p, ncb %p, cacheEntry %p\n", adapter, ncb, cacheEntry);
610 if (!cacheEntry) return NRC_BADDR;
611 *cacheEntry = NULL;
613 if (!adapter) return NRC_BADDR;
614 if (!ncb) return NRC_BADDR;
616 if (ncb->ncb_callname[0] == '*')
617 ret = NRC_NOWILD;
618 else
620 *cacheEntry = NBNameCacheFindEntry(gNameCache, ncb->ncb_callname);
621 if (!*cacheEntry)
622 *cacheEntry = NBNameCacheFindEntry(adapter->nameCache,
623 ncb->ncb_callname);
624 if (!*cacheEntry)
626 NBNameCacheEntry *newEntry = NULL;
628 ret = NetBTinetResolve(ncb->ncb_callname, &newEntry);
629 if (ret == NRC_GOODRET && newEntry)
631 ret = NetBTStoreCacheEntry(&gNameCache, newEntry);
632 if (ret != NRC_GOODRET)
633 newEntry = NULL;
635 else
637 SOCKET fd = WSASocketA(PF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL,
638 0, WSA_FLAG_OVERLAPPED);
640 if(fd == INVALID_SOCKET)
641 ret = NRC_OSRESNOTAV;
642 else
644 int winsNdx;
646 adapter->nameQueryXID++;
647 for (winsNdx = 0; ret == NRC_GOODRET && *cacheEntry == NULL
648 && winsNdx < gNumWINSServers; winsNdx++)
649 ret = NetBTNameWaitLoop(adapter, fd, ncb,
650 gWINSServers[winsNdx], FALSE, gWINSQueryTimeout,
651 gWINSQueries, &newEntry);
652 if (ret == NRC_GOODRET && newEntry)
654 ret = NetBTStoreCacheEntry(&gNameCache, newEntry);
655 if (ret != NRC_GOODRET)
656 newEntry = NULL;
658 if (ret == NRC_GOODRET && *cacheEntry == NULL)
660 ret = NetBTNameWaitLoop(adapter, fd, ncb,
661 adapter->ipr.dwBCastAddr, TRUE, gBCastQueryTimeout,
662 gBCastQueries, &newEntry);
663 if (ret == NRC_GOODRET && newEntry)
665 ret = NetBTStoreCacheEntry(&adapter->nameCache,
666 newEntry);
667 if (ret != NRC_GOODRET)
668 newEntry = NULL;
671 closesocket(fd);
674 *cacheEntry = newEntry;
677 TRACE("returning 0x%02x\n", ret);
678 return ret;
681 typedef struct _NetBTNodeQueryData
683 BOOL gotResponse;
684 PADAPTER_STATUS astat;
685 WORD astatLen;
686 } NetBTNodeQueryData;
688 /* Callback function for NetBTAstatRemote, parses the rData for the node
689 * status and name list of the remote node. Always returns FALSE, since
690 * there's never more than one answer we care about in a node status response.
692 static BOOL NetBTNodeStatusAnswerCallback(void *pVoid, WORD answerCount,
693 WORD answerIndex, PUCHAR rData, WORD rLen)
695 NetBTNodeQueryData *data = (NetBTNodeQueryData *)pVoid;
697 if (data && !data->gotResponse && rData && rLen >= 1)
699 /* num names is first byte; each name is NCBNAMSZ + 2 bytes */
700 if (rLen >= rData[0] * (NCBNAMSZ + 2))
702 WORD i;
703 PUCHAR src;
704 PNAME_BUFFER dst;
706 data->gotResponse = TRUE;
707 data->astat->name_count = rData[0];
708 for (i = 0, src = rData + 1,
709 dst = (PNAME_BUFFER)((PUCHAR)data->astat +
710 sizeof(ADAPTER_STATUS));
711 i < data->astat->name_count && src - rData < rLen &&
712 (PUCHAR)dst - (PUCHAR)data->astat < data->astatLen;
713 i++, dst++, src += NCBNAMSZ + 2)
715 UCHAR flags = *(src + NCBNAMSZ);
717 memcpy(dst->name, src, NCBNAMSZ);
718 /* we won't actually see a registering name in the returned
719 * response. It's useful to see if no other flags are set; if
720 * none are, then the name is registered. */
721 dst->name_flags = REGISTERING;
722 if (flags & 0x80)
723 dst->name_flags |= GROUP_NAME;
724 if (flags & 0x10)
725 dst->name_flags |= DEREGISTERED;
726 if (flags & 0x08)
727 dst->name_flags |= DUPLICATE;
728 if (dst->name_flags == REGISTERING)
729 dst->name_flags = REGISTERED;
731 /* arbitrarily set HW type to Ethernet */
732 data->astat->adapter_type = 0xfe;
733 if (src - rData < rLen)
734 memcpy(data->astat->adapter_address, src,
735 min(rLen - (src - rData), 6));
738 return FALSE;
741 /* This uses the WINS timeout and query values, as they're the
742 * UCAST_REQ_RETRY_TIMEOUT and UCAST_REQ_RETRY_COUNT according to the RFCs.
744 static UCHAR NetBTAstatRemote(NetBTAdapter *adapter, PNCB ncb)
746 UCHAR ret = NRC_GOODRET;
747 const NBNameCacheEntry *cacheEntry = NULL;
749 TRACE("adapter %p, NCB %p\n", adapter, ncb);
751 if (!adapter) return NRC_BADDR;
752 if (!ncb) return NRC_INVADDRESS;
754 ret = NetBTInternalFindName(adapter, ncb, &cacheEntry);
755 if (ret == NRC_GOODRET && cacheEntry)
757 if (cacheEntry->numAddresses > 0)
759 SOCKET fd = WSASocketA(PF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0,
760 WSA_FLAG_OVERLAPPED);
762 if(fd == INVALID_SOCKET)
763 ret = NRC_OSRESNOTAV;
764 else
766 NetBTNodeQueryData queryData;
767 DWORD queries;
768 PADAPTER_STATUS astat = (PADAPTER_STATUS)ncb->ncb_buffer;
770 adapter->nameQueryXID++;
771 astat->name_count = 0;
772 queryData.gotResponse = FALSE;
773 queryData.astat = astat;
774 queryData.astatLen = ncb->ncb_length;
775 for (queries = 0; !queryData.gotResponse &&
776 queries < gWINSQueries; queries++)
778 if (!NCB_CANCELLED(ncb))
780 int r = NetBTSendNameQuery(fd, ncb->ncb_callname,
781 adapter->nameQueryXID, NBNS_TYPE_NBSTAT,
782 cacheEntry->addresses[0], FALSE);
784 if (r == 0)
785 ret = NetBTWaitForNameResponse(adapter, fd,
786 GetTickCount() + gWINSQueryTimeout,
787 NetBTNodeStatusAnswerCallback, &queryData);
788 else
789 ret = NRC_SYSTEM;
791 else
792 ret = NRC_CMDCAN;
794 closesocket(fd);
797 else
798 ret = NRC_CMDTMO;
800 else if (ret == NRC_CMDCAN)
801 ; /* do nothing, we were cancelled */
802 else
803 ret = NRC_CMDTMO;
804 TRACE("returning 0x%02x\n", ret);
805 return ret;
808 static UCHAR NetBTAstat(void *adapt, PNCB ncb)
810 NetBTAdapter *adapter = (NetBTAdapter *)adapt;
811 UCHAR ret;
813 TRACE("adapt %p, NCB %p\n", adapt, ncb);
815 if (!adapter) return NRC_ENVNOTDEF;
816 if (!ncb) return NRC_INVADDRESS;
817 if (!ncb->ncb_buffer) return NRC_BADDR;
818 if (ncb->ncb_length < sizeof(ADAPTER_STATUS)) return NRC_BUFLEN;
820 if (ncb->ncb_callname[0] == '*')
822 DWORD physAddrLen;
823 MIB_IFROW ifRow;
824 PADAPTER_STATUS astat = (PADAPTER_STATUS)ncb->ncb_buffer;
826 memset(astat, 0, sizeof(ADAPTER_STATUS));
827 astat->rev_major = 3;
828 ifRow.dwIndex = adapter->ipr.dwIndex;
829 if (GetIfEntry(&ifRow) != NO_ERROR)
830 ret = NRC_BRIDGE;
831 else
833 physAddrLen = min(ifRow.dwPhysAddrLen, 6);
834 if (physAddrLen > 0)
835 memcpy(astat->adapter_address, ifRow.bPhysAddr, physAddrLen);
836 /* doubt anyone cares, but why not.. */
837 if (ifRow.dwType == MIB_IF_TYPE_TOKENRING)
838 astat->adapter_type = 0xff;
839 else
840 astat->adapter_type = 0xfe; /* for Ethernet */
841 astat->max_sess_pkt_size = 0xffff;
842 astat->xmit_success = adapter->xmit_success;
843 astat->recv_success = adapter->recv_success;
845 ret = NRC_GOODRET;
847 else
848 ret = NetBTAstatRemote(adapter, ncb);
849 TRACE("returning 0x%02x\n", ret);
850 return ret;
853 static UCHAR NetBTFindName(void *adapt, PNCB ncb)
855 NetBTAdapter *adapter = (NetBTAdapter *)adapt;
856 UCHAR ret;
857 const NBNameCacheEntry *cacheEntry = NULL;
858 PFIND_NAME_HEADER foundName;
860 TRACE("adapt %p, NCB %p\n", adapt, ncb);
862 if (!adapter) return NRC_ENVNOTDEF;
863 if (!ncb) return NRC_INVADDRESS;
864 if (!ncb->ncb_buffer) return NRC_BADDR;
865 if (ncb->ncb_length < sizeof(FIND_NAME_HEADER)) return NRC_BUFLEN;
867 foundName = (PFIND_NAME_HEADER)ncb->ncb_buffer;
868 memset(foundName, 0, sizeof(FIND_NAME_HEADER));
870 ret = NetBTInternalFindName(adapter, ncb, &cacheEntry);
871 if (ret == NRC_GOODRET)
873 if (cacheEntry)
875 DWORD spaceFor = min((ncb->ncb_length - sizeof(FIND_NAME_HEADER)) /
876 sizeof(FIND_NAME_BUFFER), cacheEntry->numAddresses);
877 DWORD ndx;
879 for (ndx = 0; ndx < spaceFor; ndx++)
881 PFIND_NAME_BUFFER findNameBuffer;
883 findNameBuffer =
884 (PFIND_NAME_BUFFER)((PUCHAR)foundName +
885 sizeof(FIND_NAME_HEADER) + foundName->node_count *
886 sizeof(FIND_NAME_BUFFER));
887 memset(findNameBuffer->destination_addr, 0, 2);
888 memcpy(findNameBuffer->destination_addr + 2,
889 &adapter->ipr.dwAddr, sizeof(DWORD));
890 memset(findNameBuffer->source_addr, 0, 2);
891 memcpy(findNameBuffer->source_addr + 2,
892 &cacheEntry->addresses[ndx], sizeof(DWORD));
893 foundName->node_count++;
895 if (spaceFor < cacheEntry->numAddresses)
896 ret = NRC_BUFLEN;
898 else
899 ret = NRC_CMDTMO;
901 TRACE("returning 0x%02x\n", ret);
902 return ret;
905 static UCHAR NetBTSessionReq(SOCKET fd, const UCHAR *calledName,
906 const UCHAR *callingName)
908 UCHAR buffer[NBSS_HDRSIZE + MAX_DOMAIN_NAME_LEN * 2], ret;
909 int len = 0, r;
910 DWORD bytesSent, bytesReceived, recvFlags = 0;
911 WSABUF wsaBuf;
913 buffer[0] = NBSS_REQ;
914 buffer[1] = 0;
916 len += NetBTNameEncode(calledName, &buffer[NBSS_HDRSIZE]);
917 len += NetBTNameEncode(callingName, &buffer[NBSS_HDRSIZE + len]);
919 NBR_ADDWORD(&buffer[2], len);
921 wsaBuf.len = len + NBSS_HDRSIZE;
922 wsaBuf.buf = buffer;
924 r = WSASend(fd, &wsaBuf, 1, &bytesSent, 0, NULL, NULL);
925 if(r < 0 || bytesSent < len + NBSS_HDRSIZE)
927 ERR("send failed\n");
928 return NRC_SABORT;
931 /* I've already set the recv timeout on this socket (if it supports it), so
932 * just block. Hopefully we'll always receive the session acknowledgement
933 * within one timeout.
935 wsaBuf.len = NBSS_HDRSIZE + 1;
936 r = WSARecv(fd, &wsaBuf, 1, &bytesReceived, &recvFlags, NULL, NULL);
937 if (r < 0 || bytesReceived < NBSS_HDRSIZE)
938 ret = NRC_SABORT;
939 else if (buffer[0] == NBSS_NACK)
941 if (r == NBSS_HDRSIZE + 1)
943 switch (buffer[NBSS_HDRSIZE])
945 case NBSS_ERR_INSUFFICIENT_RESOURCES:
946 ret = NRC_REMTFUL;
947 break;
948 default:
949 ret = NRC_NOCALL;
952 else
953 ret = NRC_NOCALL;
955 else if (buffer[0] == NBSS_RETARGET)
957 FIXME("Got a session retarget, can't deal\n");
958 ret = NRC_NOCALL;
960 else if (buffer[0] == NBSS_ACK)
961 ret = NRC_GOODRET;
962 else
963 ret = NRC_SYSTEM;
965 TRACE("returning 0x%02x\n", ret);
966 return ret;
969 static UCHAR NetBTCall(void *adapt, PNCB ncb, void **sess)
971 NetBTAdapter *adapter = (NetBTAdapter *)adapt;
972 UCHAR ret;
973 const NBNameCacheEntry *cacheEntry = NULL;
975 TRACE("adapt %p, ncb %p", adapt, ncb);
977 if (!adapter) return NRC_ENVNOTDEF;
978 if (!ncb) return NRC_INVADDRESS;
979 if (!sess) return NRC_BADDR;
981 ret = NetBTInternalFindName(adapter, ncb, &cacheEntry);
982 if (ret == NRC_GOODRET)
984 if (cacheEntry && cacheEntry->numAddresses > 0)
986 SOCKET fd;
988 fd = WSASocketA(PF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
989 WSA_FLAG_OVERLAPPED);
990 if (fd != INVALID_SOCKET)
992 DWORD timeout;
993 struct sockaddr_in sin;
995 if (ncb->ncb_rto > 0)
997 timeout = ncb->ncb_rto * 500;
998 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (PUCHAR)&timeout,
999 sizeof(timeout));
1001 if (ncb->ncb_rto > 0)
1003 timeout = ncb->ncb_sto * 500;
1004 setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (PUCHAR)&timeout,
1005 sizeof(timeout));
1008 memset(&sin, 0, sizeof(sin));
1009 memcpy(&sin.sin_addr, &cacheEntry->addresses[0],
1010 sizeof(sin.sin_addr));
1011 sin.sin_family = AF_INET;
1012 sin.sin_port = htons(PORT_NBSS);
1013 /* FIXME: use nonblocking mode for the socket, check the
1014 * cancel flag periodically
1016 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin))
1017 == SOCKET_ERROR)
1018 ret = NRC_CMDTMO;
1019 else
1021 static UCHAR fakedCalledName[] = "*SMBSERVER";
1022 const UCHAR *calledParty = cacheEntry->nbname[0] == '*'
1023 ? fakedCalledName : cacheEntry->nbname;
1025 ret = NetBTSessionReq(fd, calledParty, ncb->ncb_name);
1026 if (ret != NRC_GOODRET && calledParty[0] == '*')
1028 FIXME("NBT session to \"*SMBSERVER\" refused,\n");
1029 FIXME("should try finding name using ASTAT\n");
1032 if (ret != NRC_GOODRET)
1033 closesocket(fd);
1034 else
1036 NetBTSession *session = (NetBTSession *)HeapAlloc(
1037 GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(NetBTSession));
1039 if (session)
1041 session->fd = fd;
1042 InitializeCriticalSection(&session->cs);
1043 *sess = session;
1045 else
1047 ret = NRC_OSRESNOTAV;
1048 closesocket(fd);
1052 else
1053 ret = NRC_OSRESNOTAV;
1055 else
1056 ret = NRC_NAMERR;
1058 TRACE("returning 0x%02x\n", ret);
1059 return ret;
1062 /* Notice that I don't protect against multiple thread access to NetBTSend.
1063 * This is because I don't update any data in the adapter, and I only make a
1064 * single call to WSASend, which I assume to act atomically (not interleaving
1065 * data from other threads).
1066 * I don't lock, because I only depend on the fd being valid, and this won't be
1067 * true until a session setup is completed.
1069 static UCHAR NetBTSend(void *adapt, void *sess, PNCB ncb)
1071 NetBTAdapter *adapter = (NetBTAdapter *)adapt;
1072 NetBTSession *session = (NetBTSession *)sess;
1073 UCHAR buffer[NBSS_HDRSIZE], ret;
1074 int r;
1075 WSABUF wsaBufs[2];
1076 DWORD bytesSent;
1078 TRACE("adapt %p, session %p, NCB %p\n", adapt, session, ncb);
1080 if (!adapter) return NRC_ENVNOTDEF;
1081 if (!ncb) return NRC_INVADDRESS;
1082 if (!ncb->ncb_buffer) return NRC_BADDR;
1083 if (!session) return NRC_SNUMOUT;
1084 if (session->fd == INVALID_SOCKET) return NRC_SNUMOUT;
1086 buffer[0] = NBSS_MSG;
1087 buffer[1] = 0;
1088 NBR_ADDWORD(&buffer[2], ncb->ncb_length);
1090 wsaBufs[0].len = NBSS_HDRSIZE;
1091 wsaBufs[0].buf = buffer;
1092 wsaBufs[1].len = ncb->ncb_length;
1093 wsaBufs[1].buf = ncb->ncb_buffer;
1095 r = WSASend(session->fd, wsaBufs, sizeof(wsaBufs) / sizeof(wsaBufs[0]),
1096 &bytesSent, 0, NULL, NULL);
1097 if (r == SOCKET_ERROR)
1099 NetBIOSHangupSession(ncb);
1100 ret = NRC_SABORT;
1102 else if (bytesSent < NBSS_HDRSIZE + ncb->ncb_length)
1104 FIXME("Only sent %ld bytes (of %d), hanging up session\n", bytesSent,
1105 NBSS_HDRSIZE + ncb->ncb_length);
1106 NetBIOSHangupSession(ncb);
1107 ret = NRC_SABORT;
1109 else
1111 ret = NRC_GOODRET;
1112 adapter->xmit_success++;
1114 TRACE("returning 0x%02x\n", ret);
1115 return ret;
1118 static UCHAR NetBTRecv(void *adapt, void *sess, PNCB ncb)
1120 NetBTAdapter *adapter = (NetBTAdapter *)adapt;
1121 NetBTSession *session = (NetBTSession *)sess;
1122 UCHAR buffer[NBSS_HDRSIZE], ret;
1123 int r;
1124 WSABUF wsaBufs[2];
1125 DWORD bufferCount, bytesReceived, flags;
1127 TRACE("adapt %p, session %p, NCB %p\n", adapt, session, ncb);
1129 if (!adapter) return NRC_ENVNOTDEF;
1130 if (!ncb) return NRC_BADDR;
1131 if (!ncb->ncb_buffer) return NRC_BADDR;
1132 if (!session) return NRC_SNUMOUT;
1133 if (session->fd == INVALID_SOCKET) return NRC_SNUMOUT;
1135 EnterCriticalSection(&session->cs);
1136 bufferCount = 0;
1137 if (session->bytesPending == 0)
1139 bufferCount++;
1140 wsaBufs[0].len = NBSS_HDRSIZE;
1141 wsaBufs[0].buf = buffer;
1143 wsaBufs[bufferCount].len = ncb->ncb_length;
1144 wsaBufs[bufferCount].buf = ncb->ncb_buffer;
1145 bufferCount++;
1147 flags = 0;
1148 /* FIXME: should poll a bit so I can check the cancel flag */
1149 r = WSARecv(session->fd, wsaBufs, bufferCount, &bytesReceived, &flags,
1150 NULL, NULL);
1151 if (r == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
1153 LeaveCriticalSection(&session->cs);
1154 ERR("Receive error, WSAGetLastError() returns %d\n", WSAGetLastError());
1155 NetBIOSHangupSession(ncb);
1156 ret = NRC_SABORT;
1158 else if (NCB_CANCELLED(ncb))
1160 LeaveCriticalSection(&session->cs);
1161 ret = NRC_CMDCAN;
1163 else
1165 if (bufferCount == 2)
1167 if (buffer[0] == NBSS_KEEPALIVE)
1169 LeaveCriticalSection(&session->cs);
1170 FIXME("Oops, received a session keepalive and lost my place\n");
1171 /* need to read another session header until we get a session
1172 * message header. */
1173 NetBIOSHangupSession(ncb);
1174 ret = NRC_SABORT;
1176 else if (buffer[0] != NBSS_MSG)
1178 LeaveCriticalSection(&session->cs);
1179 FIXME("Received unexpected session msg type %d\n", buffer[0]);
1180 NetBIOSHangupSession(ncb);
1181 ret = NRC_SABORT;
1183 else
1185 if (buffer[1] & NBSS_EXTENSION)
1187 LeaveCriticalSection(&session->cs);
1188 FIXME("Received a message that's too long for my taste\n");
1189 NetBIOSHangupSession(ncb);
1190 ret = NRC_SABORT;
1192 else
1194 session->bytesPending = NBSS_HDRSIZE
1195 + NBR_GETWORD(&buffer[2]) - bytesReceived;
1196 ncb->ncb_length = bytesReceived - NBSS_HDRSIZE;
1197 LeaveCriticalSection(&session->cs);
1201 else
1203 if (bytesReceived < session->bytesPending)
1204 session->bytesPending -= bytesReceived;
1205 else
1206 session->bytesPending = 0;
1207 LeaveCriticalSection(&session->cs);
1208 ncb->ncb_length = bytesReceived;
1210 if (session->bytesPending > 0)
1211 ret = NRC_INCOMP;
1212 else
1214 ret = NRC_GOODRET;
1215 adapter->recv_success++;
1218 TRACE("returning 0x%02x\n", ret);
1219 return ret;
1222 static UCHAR NetBTHangup(void *adapt, void *sess)
1224 NetBTSession *session = (NetBTSession *)sess;
1226 TRACE("adapt %p, session %p\n", adapt, session);
1228 if (!session) return NRC_SNUMOUT;
1230 /* I don't lock the session, because NetBTRecv knows not to decrement
1231 * past 0, so if a receive completes after this it should still deal.
1233 closesocket(session->fd);
1234 session->fd = INVALID_SOCKET;
1235 session->bytesPending = 0;
1236 DeleteCriticalSection(&session->cs);
1237 HeapFree(GetProcessHeap(), 0, session);
1239 return NRC_GOODRET;
1242 static void NetBTCleanupAdapter(void *adapt)
1244 TRACE("adapt %p\n", adapt);
1245 if (adapt)
1247 NetBTAdapter *adapter = (NetBTAdapter *)adapt;
1249 if (adapter->nameCache)
1250 NBNameCacheDestroy(adapter->nameCache);
1251 HeapFree(GetProcessHeap(), 0, adapt);
1255 static void NetBTCleanup(void)
1257 TRACE("\n");
1258 if (gNameCache)
1260 NBNameCacheDestroy(gNameCache);
1261 gNameCache = NULL;
1265 static UCHAR NetBTRegisterAdapter(PMIB_IPADDRROW ipRow)
1267 UCHAR ret;
1268 NetBTAdapter *adapter;
1270 if (!ipRow) return NRC_BADDR;
1272 adapter = (NetBTAdapter *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1273 sizeof(NetBTAdapter));
1274 if (adapter)
1276 memcpy(&adapter->ipr, ipRow, sizeof(MIB_IPADDRROW));
1277 if (!NetBIOSRegisterAdapter(gTransportID, ipRow->dwIndex, adapter))
1279 NetBTCleanupAdapter(adapter);
1280 ret = NRC_SYSTEM;
1282 else
1283 ret = NRC_GOODRET;
1285 else
1286 ret = NRC_OSRESNOTAV;
1287 return ret;
1290 /* Callback for NetBIOS adapter enumeration. Assumes closure is a pointer to
1291 * a MIB_IPADDRTABLE containing all the IP adapters needed to be added to the
1292 * NetBIOS adapter table. For each callback, checks if the passed-in adapt
1293 * has an entry in the table; if so, this adapter was enumerated previously,
1294 * and it's enabled. As a flag, the table's dwAddr entry is changed to
1295 * INADDR_LOOPBACK, since this is an invalid address for a NetBT adapter.
1296 * The NetBTEnum function will add any remaining adapters from the
1297 * MIB_IPADDRTABLE to the NetBIOS adapter table.
1299 static BOOL NetBTEnumCallback(UCHAR totalLANAs, UCHAR lanaIndex,
1300 ULONG transport, const NetBIOSAdapterImpl *data, void *closure)
1302 BOOL ret;
1303 PMIB_IPADDRTABLE table = (PMIB_IPADDRTABLE)closure;
1305 if (table && data)
1307 DWORD ndx;
1309 ret = FALSE;
1310 for (ndx = 0; !ret && ndx < table->dwNumEntries; ndx++)
1312 const NetBTAdapter *adapter = (const NetBTAdapter *)data->data;
1314 if (table->table[ndx].dwIndex == adapter->ipr.dwIndex)
1316 NetBIOSEnableAdapter(data->lana);
1317 table->table[ndx].dwAddr = INADDR_LOOPBACK;
1318 ret = TRUE;
1322 else
1323 ret = FALSE;
1324 return ret;
1327 /* Enumerates adapters by:
1328 * - retrieving the IP address table for the local machine
1329 * - eliminating loopback addresses from the table
1330 * - eliminating redundant addresses, that is, multiple addresses on the same
1331 * subnet
1332 * Calls NetBIOSEnumAdapters, passing the resulting table as the callback
1333 * data. The callback reenables each adapter that's already in the NetBIOS
1334 * table. After NetBIOSEnumAdapters returns, this function adds any remaining
1335 * adapters to the NetBIOS table.
1337 static UCHAR NetBTEnum(void)
1339 UCHAR ret;
1340 DWORD size = 0;
1342 TRACE("\n");
1344 if (GetIpAddrTable(NULL, &size, FALSE) == ERROR_INSUFFICIENT_BUFFER)
1346 PMIB_IPADDRTABLE ipAddrs, coalesceTable = NULL;
1347 DWORD numIPAddrs = (size - sizeof(MIB_IPADDRTABLE)) /
1348 sizeof(MIB_IPADDRROW) + 1;
1350 ipAddrs = (PMIB_IPADDRTABLE)HeapAlloc(GetProcessHeap(),
1351 HEAP_ZERO_MEMORY, size);
1352 if (ipAddrs)
1353 coalesceTable = (PMIB_IPADDRTABLE)HeapAlloc(GetProcessHeap(),
1354 HEAP_ZERO_MEMORY, sizeof(MIB_IPADDRTABLE) +
1355 (min(numIPAddrs, MAX_LANA + 1) - 1) * sizeof(MIB_IPADDRROW));
1356 if (ipAddrs && coalesceTable)
1358 if (GetIpAddrTable(ipAddrs, &size, FALSE) == ERROR_SUCCESS)
1360 DWORD ndx;
1362 for (ndx = 0; ndx < ipAddrs->dwNumEntries; ndx++)
1364 if ((ipAddrs->table[ndx].dwAddr &
1365 ipAddrs->table[ndx].dwMask) !=
1366 htonl((INADDR_LOOPBACK & IN_CLASSA_NET)))
1368 BOOL newNetwork = TRUE;
1369 DWORD innerIndex;
1371 /* make sure we don't have more than one entry
1372 * for a subnet */
1373 for (innerIndex = 0; newNetwork &&
1374 innerIndex < coalesceTable->dwNumEntries; innerIndex++)
1375 if ((ipAddrs->table[ndx].dwAddr &
1376 ipAddrs->table[ndx].dwMask) ==
1377 (coalesceTable->table[innerIndex].dwAddr
1378 & coalesceTable->table[innerIndex].dwMask))
1379 newNetwork = FALSE;
1381 if (newNetwork)
1382 memcpy(&coalesceTable->table[
1383 coalesceTable->dwNumEntries++],
1384 &ipAddrs->table[ndx], sizeof(MIB_IPADDRROW));
1388 NetBIOSEnumAdapters(gTransportID, NetBTEnumCallback,
1389 coalesceTable);
1390 ret = NRC_GOODRET;
1391 for (ndx = 0; ret == NRC_GOODRET &&
1392 ndx < coalesceTable->dwNumEntries; ndx++)
1393 if (coalesceTable->table[ndx].dwAddr != INADDR_LOOPBACK)
1394 ret = NetBTRegisterAdapter(&coalesceTable->table[ndx]);
1396 else
1397 ret = NRC_SYSTEM;
1398 HeapFree(GetProcessHeap(), 0, ipAddrs);
1399 HeapFree(GetProcessHeap(), 0, coalesceTable);
1401 else
1402 ret = NRC_OSRESNOTAV;
1404 else
1405 ret = NRC_SYSTEM;
1406 TRACE("returning 0x%02x\n", ret);
1407 return ret;
1410 /* Initializes global variables and registers the NetBT transport */
1411 void NetBTInit(void)
1413 HKEY hKey;
1414 NetBIOSTransport transport;
1415 LONG ret;
1417 TRACE("\n");
1419 gEnableDNS = TRUE;
1420 gBCastQueries = BCAST_QUERIES;
1421 gBCastQueryTimeout = BCAST_QUERY_TIMEOUT;
1422 gWINSQueries = WINS_QUERIES;
1423 gWINSQueryTimeout = WINS_QUERY_TIMEOUT;
1424 gNumWINSServers = 0;
1425 memset(gWINSServers, 0, sizeof(gWINSServers));
1426 gScopeID[0] = '\0';
1427 gCacheTimeout = CACHE_TIMEOUT;
1429 /* Try to open the Win9x NetBT configuration key */
1430 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1431 "\\SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &hKey);
1432 /* If that fails, try the WinNT NetBT configuration key */
1433 if (ret != ERROR_SUCCESS)
1434 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1435 "\\SYSTEM\\CurrentControlSet\\Services\\NetBT\\Parameters", 0,
1436 KEY_READ, &hKey);
1437 if (ret == ERROR_SUCCESS)
1439 DWORD dword, size;
1441 size = sizeof(dword);
1442 if (RegQueryValueExA(hKey, "EnableDNS", NULL, NULL,
1443 (LPBYTE)&dword, &size) == ERROR_SUCCESS)
1444 gEnableDNS = dword;
1445 size = sizeof(dword);
1446 if (RegQueryValueExA(hKey, "BcastNameQueryCount", NULL, NULL,
1447 (LPBYTE)&dword, &size) == ERROR_SUCCESS && dword >= MIN_QUERIES
1448 && dword <= MAX_QUERIES)
1449 gBCastQueries = dword;
1450 size = sizeof(dword);
1451 if (RegQueryValueExA(hKey, "BcastNameQueryTimeout", NULL, NULL,
1452 (LPBYTE)&dword, &size) == ERROR_SUCCESS && dword >= MIN_QUERY_TIMEOUT
1453 && dword <= MAX_QUERY_TIMEOUT)
1454 gBCastQueryTimeout = dword;
1455 size = sizeof(dword);
1456 if (RegQueryValueExA(hKey, "NameSrvQueryCount", NULL, NULL,
1457 (LPBYTE)&dword, &size) == ERROR_SUCCESS && dword >= MIN_QUERIES
1458 && dword <= MAX_QUERIES)
1459 gWINSQueries = dword;
1460 size = sizeof(dword);
1461 if (RegQueryValueExA(hKey, "NameSrvQueryTimeout", NULL, NULL,
1462 (LPBYTE)&dword, &size) == ERROR_SUCCESS && dword >= MIN_QUERY_TIMEOUT
1463 && dword <= MAX_QUERY_TIMEOUT)
1464 gWINSQueryTimeout = dword;
1465 size = MAX_DOMAIN_NAME_LEN - 1;
1466 if (RegQueryValueExA(hKey, "ScopeID", NULL, NULL, gScopeID + 1, &size)
1467 == ERROR_SUCCESS)
1469 /* convert into L2-encoded version, suitable for use by
1470 NetBTNameEncode */
1471 char *ptr, *lenPtr;
1473 for (ptr = gScopeID + 1; *ptr &&
1474 ptr - gScopeID < MAX_DOMAIN_NAME_LEN; )
1476 for (lenPtr = ptr - 1, *lenPtr = 0; *ptr && *ptr != '.' &&
1477 ptr - gScopeID < MAX_DOMAIN_NAME_LEN; ptr++)
1478 *lenPtr += 1;
1479 ptr++;
1482 if (RegQueryValueExA(hKey, "CacheTimeout", NULL, NULL,
1483 (LPBYTE)&dword, &size) == ERROR_SUCCESS && dword >= MIN_CACHE_TIMEOUT)
1484 gCacheTimeout = dword;
1485 RegCloseKey(hKey);
1487 /* WINE-specific NetBT registry settings. Because our adapter naming is
1488 * different than MS', we can't do per-adapter WINS configuration in the
1489 * same place. Just do a global WINS configuration instead.
1491 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1492 "\\Software\\Wine\\Wine\\Config\\Network", 0, KEY_READ, &hKey)
1493 == ERROR_SUCCESS)
1495 static const char *nsValueNames[] = { "WinsServer", "BackupWinsServer" };
1496 char nsString[16];
1497 DWORD size, ndx;
1499 for (ndx = 0; ndx < sizeof(nsValueNames) / sizeof(nsValueNames[0]);
1500 ndx++)
1502 size = sizeof(nsString) / sizeof(char);
1503 if (RegQueryValueExA(hKey, nsValueNames[ndx], NULL, NULL,
1504 (LPBYTE)nsString, &size) == ERROR_SUCCESS)
1506 unsigned long addr = inet_addr(nsString);
1508 if (addr != INADDR_NONE && gNumWINSServers < MAX_WINS_SERVERS)
1509 gWINSServers[gNumWINSServers++] = addr;
1512 RegCloseKey(hKey);
1515 transport.enumerate = NetBTEnum;
1516 transport.astat = NetBTAstat;
1517 transport.findName = NetBTFindName;
1518 transport.call = NetBTCall;
1519 transport.send = NetBTSend;
1520 transport.recv = NetBTRecv;
1521 transport.hangup = NetBTHangup;
1522 transport.cleanupAdapter = NetBTCleanupAdapter;
1523 transport.cleanup = NetBTCleanup;
1524 memcpy(&gTransportID, TRANSPORT_NBT, sizeof(ULONG));
1525 NetBIOSRegisterTransport(gTransportID, &transport);