xref: /illumos-gate/usr/src/lib/smbsrv/libsmbns/common/smbns_netbios_cache.c (revision dc41c81f4872a0d4ce52a59cd88a576aeb49183a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <synch.h>
27 #include <stdio.h>
28 #include <syslog.h>
29 #include <stdlib.h>
30 #include <arpa/inet.h>
31 #include <netdb.h>
32 
33 #include <smbsrv/libsmbns.h>
34 #include <smbns_netbios.h>
35 
36 #define	NETBIOS_HTAB_SZ	128
37 #define	NETBIOS_HKEY_SZ	(NETBIOS_NAME_SZ + NETBIOS_DOMAIN_NAME_MAX)
38 
39 #define	NETBIOS_SAME_IP(addr1, addr2) \
40 	((addr1)->sin.sin_addr.s_addr == (addr2)->sin.sin_addr.s_addr)
41 
42 typedef char nb_key_t[NETBIOS_HKEY_SZ];
43 static HT_HANDLE *smb_netbios_cache = 0;
44 static rwlock_t nb_cache_lock;
45 
46 static void smb_strname(struct name_entry *name, char *buf, int bufsize);
47 static void hash_callback(HT_ITEM *item);
48 static int smb_netbios_match(const char *key1, const char *key2, size_t n);
49 static void smb_netbios_cache_key(char *key, unsigned char *name,
50 					unsigned char *scope);
51 
52 int
53 smb_netbios_cache_init(void)
54 {
55 	(void) rw_wrlock(&nb_cache_lock);
56 	if (smb_netbios_cache == NULL) {
57 		smb_netbios_cache = ht_create_table(NETBIOS_HTAB_SZ,
58 		    NETBIOS_HKEY_SZ, HTHF_FIXED_KEY);
59 		if (smb_netbios_cache == NULL) {
60 			syslog(LOG_ERR, "nbns: cannot create name cache");
61 			(void) rw_unlock(&nb_cache_lock);
62 			return (-1);
63 		}
64 		(void) ht_register_callback(smb_netbios_cache, hash_callback);
65 		ht_set_cmpfn(smb_netbios_cache, smb_netbios_match);
66 	}
67 	(void) rw_unlock(&nb_cache_lock);
68 
69 	return (0);
70 }
71 
72 void
73 smb_netbios_cache_fini(void)
74 {
75 	(void) rw_wrlock(&nb_cache_lock);
76 	ht_destroy_table(smb_netbios_cache);
77 	smb_netbios_cache = NULL;
78 	(void) rw_unlock(&nb_cache_lock);
79 }
80 
81 void
82 smb_netbios_cache_clean(void)
83 {
84 	(void) rw_wrlock(&nb_cache_lock);
85 	(void) ht_clean_table(smb_netbios_cache);
86 	(void) rw_unlock(&nb_cache_lock);
87 }
88 
89 int
90 smb_netbios_cache_getfirst(nbcache_iter_t *iter)
91 {
92 	HT_ITEM *item;
93 	struct name_entry *entry;
94 
95 	(void) rw_rdlock(&nb_cache_lock);
96 	item = ht_findfirst(smb_netbios_cache, &iter->nbc_hti);
97 	if (item == NULL || item->hi_data == NULL) {
98 		(void) rw_unlock(&nb_cache_lock);
99 		return (-1);
100 	}
101 
102 	entry = (struct name_entry *)item->hi_data;
103 	(void) mutex_lock(&entry->mtx);
104 	iter->nbc_entry = smb_netbios_name_dup(entry, 1);
105 	(void) mutex_unlock(&entry->mtx);
106 
107 	(void) rw_unlock(&nb_cache_lock);
108 
109 	return ((iter->nbc_entry) ? 0 : -1);
110 }
111 
112 int
113 smb_netbios_cache_getnext(nbcache_iter_t *iter)
114 {
115 	HT_ITEM *item;
116 	struct name_entry *entry;
117 
118 	(void) rw_rdlock(&nb_cache_lock);
119 	item = ht_findnext(&iter->nbc_hti);
120 	if (item == NULL || item->hi_data == NULL) {
121 		(void) rw_unlock(&nb_cache_lock);
122 		return (-1);
123 	}
124 
125 	entry = (struct name_entry *)item->hi_data;
126 	(void) mutex_lock(&entry->mtx);
127 	iter->nbc_entry = smb_netbios_name_dup(entry, 1);
128 	(void) mutex_unlock(&entry->mtx);
129 
130 	(void) rw_unlock(&nb_cache_lock);
131 
132 	return ((iter->nbc_entry) ? 0 : -1);
133 }
134 
135 /*
136  * smb_netbios_cache_lookup
137  *
138  * Searches the name cache for the given entry, if found
139  * the entry will be locked before returning to caller
140  * so caller MUST unlock the entry after it's done with it.
141  */
142 struct name_entry *
143 smb_netbios_cache_lookup(struct name_entry *name)
144 {
145 	HT_ITEM *item;
146 	nb_key_t key;
147 	struct name_entry *entry = NULL;
148 	unsigned char hostname[MAXHOSTNAMELEN];
149 
150 	if (NETBIOS_NAME_IS_STAR(name->name)) {
151 		/* Return our address */
152 		if (smb_getnetbiosname((char *)hostname, sizeof (hostname))
153 		    != 0)
154 			return (NULL);
155 
156 		smb_encode_netbios_name(hostname, 0x00, NULL, name);
157 	}
158 
159 	(void) rw_rdlock(&nb_cache_lock);
160 
161 	smb_netbios_cache_key(key, name->name, name->scope);
162 	item = ht_find_item(smb_netbios_cache, key);
163 	if (item) {
164 		entry = (struct name_entry *)item->hi_data;
165 		(void) mutex_lock(&entry->mtx);
166 		if ((entry->attributes & NAME_ATTR_CONFLICT) != 0) {
167 			(void) mutex_unlock(&entry->mtx);
168 			entry = NULL;
169 		}
170 	}
171 
172 	(void) rw_unlock(&nb_cache_lock);
173 	return (entry);
174 }
175 
176 void
177 smb_netbios_cache_unlock_entry(struct name_entry *name)
178 {
179 	if (name)
180 		(void) mutex_unlock(&name->mtx);
181 }
182 
183 /*
184  * smb_netbios_cache_lookup_addr
185  *
186  * lookup the given 'name' in the cache and then checks
187  * if the address also matches with the found entry.
188  * 'name' is supposed to contain only one address.
189  *
190  * The found entry will be locked before returning to caller
191  * so caller MUST unlock the entry after it's done with it.
192  */
193 struct name_entry *
194 smb_netbios_cache_lookup_addr(struct name_entry *name)
195 {
196 	struct name_entry *entry = 0;
197 	addr_entry_t *addr;
198 	addr_entry_t *name_addr;
199 	HT_ITEM *item;
200 	nb_key_t key;
201 
202 	(void) rw_rdlock(&nb_cache_lock);
203 	smb_netbios_cache_key(key, name->name, name->scope);
204 	item = ht_find_item(smb_netbios_cache, key);
205 
206 	if (item && item->hi_data) {
207 		name_addr = &name->addr_list;
208 		entry = (struct name_entry *)item->hi_data;
209 		(void) mutex_lock(&entry->mtx);
210 		addr = &entry->addr_list;
211 		do {
212 			if (NETBIOS_SAME_IP(addr, name_addr)) {
213 				/* note that entry lock isn't released here */
214 				(void) rw_unlock(&nb_cache_lock);
215 				return (entry);
216 			}
217 			addr = addr->forw;
218 		} while (addr != &entry->addr_list);
219 		(void) mutex_unlock(&entry->mtx);
220 	}
221 
222 	(void) rw_unlock(&nb_cache_lock);
223 	return (0);
224 }
225 
226 int
227 smb_netbios_cache_insert(struct name_entry *name)
228 {
229 	struct name_entry *entry;
230 	addr_entry_t *addr;
231 	addr_entry_t *name_addr;
232 	HT_ITEM *item;
233 	nb_key_t key;
234 	int rc;
235 
236 	/* No point in adding a name with IP address 255.255.255.255 */
237 	if (name->addr_list.sin.sin_addr.s_addr == 0xffffffff)
238 		return (0);
239 
240 	(void) rw_wrlock(&nb_cache_lock);
241 	smb_netbios_cache_key(key, name->name, name->scope);
242 	item = ht_find_item(smb_netbios_cache, key);
243 
244 	if (item && item->hi_data) {
245 		/* Name already exists */
246 		entry = (struct name_entry *)item->hi_data;
247 		(void) mutex_lock(&entry->mtx);
248 
249 		name_addr = &name->addr_list;
250 		addr = &entry->addr_list;
251 		if (NETBIOS_SAME_IP(addr, name_addr) &&
252 		    (addr->sin.sin_port == name_addr->sin.sin_port)) {
253 			entry->attributes |=
254 			    name_addr->attributes & NAME_ATTR_LOCAL;
255 			(void) mutex_unlock(&entry->mtx);
256 			(void) rw_unlock(&nb_cache_lock);
257 			return (0);
258 		}
259 
260 		/* Was not primary: looks for others */
261 		for (addr = entry->addr_list.forw;
262 		    addr != &entry->addr_list; addr = addr->forw) {
263 			if (NETBIOS_SAME_IP(addr, name_addr) &&
264 			    (addr->sin.sin_port == name_addr->sin.sin_port)) {
265 				(void) mutex_unlock(&entry->mtx);
266 				(void) rw_unlock(&nb_cache_lock);
267 				return (0);
268 			}
269 		}
270 
271 		if ((addr = malloc(sizeof (addr_entry_t))) != NULL) {
272 			*addr = name->addr_list;
273 			entry->attributes |= addr->attributes;
274 			QUEUE_INSERT_TAIL(&entry->addr_list, addr);
275 			rc = 0;
276 		} else {
277 			rc = -1;
278 		}
279 
280 		(void) mutex_unlock(&entry->mtx);
281 		(void) rw_unlock(&nb_cache_lock);
282 		return (rc);
283 	}
284 
285 	if ((entry = malloc(sizeof (struct name_entry))) == NULL) {
286 		(void) rw_unlock(&nb_cache_lock);
287 		return (-1);
288 	}
289 
290 	*entry = *name;
291 	entry->addr_list.forw = entry->addr_list.back = &entry->addr_list;
292 	entry->attributes |= entry->addr_list.attributes;
293 	(void) mutex_init(&entry->mtx, 0, 0);
294 	if (ht_replace_item(smb_netbios_cache, key, entry) == 0) {
295 		free(entry);
296 		(void) rw_unlock(&nb_cache_lock);
297 		return (-1);
298 	}
299 
300 	(void) rw_unlock(&nb_cache_lock);
301 	return (0);
302 }
303 
304 
305 void
306 smb_netbios_cache_delete(struct name_entry *name)
307 {
308 	nb_key_t key;
309 	HT_ITEM *item;
310 	struct name_entry *entry;
311 
312 	(void) rw_wrlock(&nb_cache_lock);
313 	smb_netbios_cache_key(key, name->name, name->scope);
314 	item = ht_find_item(smb_netbios_cache, key);
315 	if (item && item->hi_data) {
316 		entry = (struct name_entry *)item->hi_data;
317 		(void) mutex_lock(&entry->mtx);
318 		ht_mark_delete(smb_netbios_cache, item);
319 		(void) mutex_unlock(&entry->mtx);
320 	}
321 	(void) rw_unlock(&nb_cache_lock);
322 }
323 
324 /*
325  * smb_netbios_cache_insert_list
326  *
327  * Insert a name with multiple addresses
328  */
329 int
330 smb_netbios_cache_insert_list(struct name_entry *name)
331 {
332 	struct name_entry entry;
333 	addr_entry_t *addr;
334 
335 	addr = &name->addr_list;
336 	do {
337 		smb_init_name_struct(NETBIOS_EMPTY_NAME, 0, name->scope,
338 		    addr->sin.sin_addr.s_addr,
339 		    addr->sin.sin_port,
340 		    name->attributes,
341 		    addr->attributes,
342 		    &entry);
343 		(void) memcpy(entry.name, name->name, NETBIOS_NAME_SZ);
344 		entry.addr_list.refresh_ttl = entry.addr_list.ttl =
345 		    addr->refresh_ttl;
346 		(void) smb_netbios_cache_insert(&entry);
347 		addr = addr->forw;
348 	} while (addr != &name->addr_list);
349 
350 	return (0);
351 }
352 
353 void
354 smb_netbios_cache_update_entry(struct name_entry *entry,
355     struct name_entry *name)
356 {
357 	addr_entry_t *addr;
358 	addr_entry_t *name_addr;
359 
360 	addr = &entry->addr_list;
361 	name_addr = &name->addr_list;
362 
363 	if (IS_UNIQUE(entry->attributes)) {
364 		do {
365 			addr->ttl = name_addr->ttl;
366 			addr = addr->forw;
367 		} while (addr != &entry->addr_list);
368 
369 	} else {
370 		do {
371 			if (NETBIOS_SAME_IP(addr, name_addr) &&
372 			    (addr->sin.sin_port == name_addr->sin.sin_port)) {
373 				addr->ttl = name_addr->ttl;
374 				return;
375 			}
376 			addr = addr->forw;
377 		} while (addr != &entry->addr_list);
378 	}
379 }
380 
381 /*
382  * smb_netbios_cache_status
383  *
384  * Scan the name cache and gather status for
385  * Node Status response for names in the given scope
386  */
387 unsigned char *
388 smb_netbios_cache_status(unsigned char *buf, int bufsize, unsigned char *scope)
389 {
390 	HT_ITERATOR hti;
391 	HT_ITEM *item;
392 	struct name_entry *name;
393 	unsigned char *numnames;
394 	unsigned char *scan;
395 	unsigned char *scan_end;
396 
397 	scan = buf;
398 	scan_end = scan + bufsize;
399 
400 	numnames = scan++;
401 	*numnames = 0;
402 
403 	(void) rw_rdlock(&nb_cache_lock);
404 	item = ht_findfirst(smb_netbios_cache, &hti);
405 	do {
406 		if (item == 0)
407 			break;
408 
409 		if (item->hi_data == 0)
410 			continue;
411 
412 		if ((scan + NETBIOS_NAME_SZ + 2) >= scan_end)
413 			/* no room for adding next entry */
414 			break;
415 
416 		name = (struct name_entry *)item->hi_data;
417 		(void) mutex_lock(&name->mtx);
418 
419 		if (IS_LOCAL(name->attributes) &&
420 		    (strcasecmp((char *)scope, (char *)name->scope) == 0)) {
421 			bcopy(name->name, scan, NETBIOS_NAME_SZ);
422 			scan += NETBIOS_NAME_SZ;
423 			*scan++ = (PUBLIC_BITS(name->attributes) >> 8) & 0xff;
424 			*scan++ = PUBLIC_BITS(name->attributes) & 0xff;
425 			(*numnames)++;
426 		}
427 
428 		(void) mutex_unlock(&name->mtx);
429 	} while ((item = ht_findnext(&hti)) != 0);
430 	(void) rw_unlock(&nb_cache_lock);
431 
432 	return (scan);
433 }
434 
435 void
436 smb_netbios_cache_reset_ttl()
437 {
438 	addr_entry_t *addr;
439 	struct name_entry *name;
440 	HT_ITERATOR hti;
441 	HT_ITEM *item;
442 
443 	(void) rw_rdlock(&nb_cache_lock);
444 	item = ht_findfirst(smb_netbios_cache, &hti);
445 	do {
446 		if (item == 0)
447 			break;
448 
449 		if (item->hi_data == 0)
450 			continue;
451 
452 		name = (struct name_entry *)item->hi_data;
453 		(void) mutex_lock(&name->mtx);
454 
455 		addr = &name->addr_list;
456 		do {
457 			if (addr->ttl < 1) {
458 				if (addr->refresh_ttl)
459 					addr->ttl = addr->refresh_ttl;
460 				else
461 					addr->refresh_ttl = addr->ttl =
462 					    TO_SECONDS(DEFAULT_TTL);
463 			}
464 			addr = addr->forw;
465 		} while (addr != &name->addr_list);
466 
467 		(void) mutex_unlock(&name->mtx);
468 	} while ((item = ht_findnext(&hti)) != 0);
469 	(void) rw_unlock(&nb_cache_lock);
470 }
471 
472 /*
473  * Returns TRUE when given name is added to the refresh queue
474  * FALSE if not.
475  */
476 static boolean_t
477 smb_netbios_cache_insrefq(name_queue_t *refq, HT_ITEM *item)
478 {
479 	struct name_entry *name;
480 	struct name_entry *refent;
481 
482 	name = (struct name_entry *)item->hi_data;
483 
484 	if (IS_LOCAL(name->attributes)) {
485 		if (IS_UNIQUE(name->attributes)) {
486 			refent = smb_netbios_name_dup(name, 1);
487 			if (refent)
488 				QUEUE_INSERT_TAIL(&refq->head, refent)
489 
490 			/* next name */
491 			return (B_TRUE);
492 		}
493 	} else {
494 		ht_mark_delete(smb_netbios_cache, item);
495 		refent = smb_netbios_name_dup(name, 0);
496 		if (refent)
497 			QUEUE_INSERT_TAIL(&refq->head, refent)
498 
499 		/* next name */
500 		return (B_TRUE);
501 	}
502 
503 	return (B_FALSE);
504 }
505 
506 /*
507  * smb_netbios_cache_refresh
508  *
509  * Scans the name cache and add all local unique names
510  * and non-local names the passed refresh queue. Non-
511  * local names will also be marked as deleted.
512  *
513  * NOTE that the caller MUST protect the queue using
514  * its mutex
515  */
516 void
517 smb_netbios_cache_refresh(name_queue_t *refq)
518 {
519 	struct name_entry *name;
520 	addr_entry_t *addr;
521 	HT_ITERATOR hti;
522 	HT_ITEM *item;
523 
524 	bzero(&refq->head, sizeof (refq->head));
525 	refq->head.forw = refq->head.back = &refq->head;
526 
527 	(void) rw_rdlock(&nb_cache_lock);
528 	item = ht_findfirst(smb_netbios_cache, &hti);
529 	do { /* name loop */
530 		if (item == 0)
531 			break;
532 
533 		if (item->hi_data == 0)
534 			continue;
535 
536 		name = (struct name_entry *)item->hi_data;
537 		(void) mutex_lock(&name->mtx);
538 
539 		addr = &name->addr_list;
540 		do { /* address loop */
541 			if (addr->ttl > 0) {
542 				addr->ttl--;
543 				if (addr->ttl == 0) {
544 					if (smb_netbios_cache_insrefq(refq,
545 					    item))
546 						break;
547 				}
548 			}
549 			addr = addr->forw;
550 		} while (addr != &name->addr_list);
551 
552 		(void) mutex_unlock(&name->mtx);
553 	} while ((item = ht_findnext(&hti)) != 0);
554 	(void) rw_unlock(&nb_cache_lock);
555 }
556 
557 /*
558  * smb_netbios_cache_delete_locals
559  *
560  * Scans the name cache and add all local names to
561  * the passed delete queue.
562  *
563  * NOTE that the caller MUST protect the queue using
564  * its mutex
565  */
566 void
567 smb_netbios_cache_delete_locals(name_queue_t *delq)
568 {
569 	struct name_entry *entry;
570 	struct name_entry *delent;
571 	HT_ITERATOR hti;
572 	HT_ITEM *item;
573 
574 	bzero(&delq->head, sizeof (delq->head));
575 	delq->head.forw = delq->head.back = &delq->head;
576 
577 	(void) rw_wrlock(&nb_cache_lock);
578 	item = ht_findfirst(smb_netbios_cache, &hti);
579 	do {
580 		if (item == 0)
581 			break;
582 
583 		if (item->hi_data == 0)
584 			continue;
585 
586 		entry = (struct name_entry *)item->hi_data;
587 		(void) mutex_lock(&entry->mtx);
588 
589 		if (IS_LOCAL(entry->attributes)) {
590 			ht_mark_delete(smb_netbios_cache, item);
591 			delent = smb_netbios_name_dup(entry, 1);
592 			if (delent)
593 				QUEUE_INSERT_TAIL(&delq->head, delent)
594 		}
595 
596 		(void) mutex_unlock(&entry->mtx);
597 	} while ((item = ht_findnext(&hti)) != 0);
598 	(void) rw_unlock(&nb_cache_lock);
599 }
600 
601 void
602 smb_netbios_name_freeaddrs(struct name_entry *entry)
603 {
604 	addr_entry_t *addr;
605 
606 	if (entry == 0)
607 		return;
608 
609 	while ((addr = entry->addr_list.forw) != &entry->addr_list) {
610 		QUEUE_CLIP(addr);
611 		free(addr);
612 	}
613 }
614 
615 /*
616  * smb_netbios_cache_count
617  *
618  * Returns the number of names in the cache
619  */
620 int
621 smb_netbios_cache_count()
622 {
623 	int cnt;
624 
625 	(void) rw_rdlock(&nb_cache_lock);
626 	cnt = ht_get_total_items(smb_netbios_cache);
627 	(void) rw_unlock(&nb_cache_lock);
628 
629 	return (cnt);
630 }
631 
632 void
633 smb_netbios_cache_dump(FILE *fp)
634 {
635 	struct name_entry *name;
636 	HT_ITERATOR hti;
637 	HT_ITEM *item;
638 
639 	(void) rw_rdlock(&nb_cache_lock);
640 
641 	if (ht_get_total_items(smb_netbios_cache) != 0) {
642 		(void) fprintf(fp, "\n%-22s %-16s %-16s  %s\n",
643 		    "Name", "Type", "Address", "TTL");
644 		(void) fprintf(fp, "%s%s\n",
645 		    "-------------------------------",
646 		    "------------------------------");
647 	}
648 
649 	item = ht_findfirst(smb_netbios_cache, &hti);
650 	while (item) {
651 		if (item->hi_data) {
652 			name = (struct name_entry *)item->hi_data;
653 			(void) mutex_lock(&name->mtx);
654 			smb_netbios_name_dump(fp, name);
655 			(void) mutex_unlock(&name->mtx);
656 		}
657 		item = ht_findnext(&hti);
658 	}
659 	(void) rw_unlock(&nb_cache_lock);
660 }
661 
662 void
663 smb_netbios_name_dump(FILE *fp, struct name_entry *entry)
664 {
665 	char		buf[MAXHOSTNAMELEN];
666 	addr_entry_t	*addr;
667 	char		*type;
668 	int		count = 0;
669 
670 	smb_strname(entry, buf, sizeof (buf));
671 	type = (IS_UNIQUE(entry->attributes)) ? "UNIQUE" : "GROUP";
672 
673 	(void) fprintf(fp, "%s %-6s (0x%04x)  ", buf, type, entry->attributes);
674 
675 	addr = &entry->addr_list;
676 	do {
677 		if (count == 0)
678 			(void) fprintf(fp, "%-16s  %d\n",
679 			    inet_ntoa(addr->sin.sin_addr), addr->ttl);
680 		else
681 			(void) fprintf(fp, "%-28s  (0x%04x)  %-16s  %d\n",
682 			    " ", addr->attributes,
683 			    inet_ntoa(addr->sin.sin_addr), addr->ttl);
684 		++count;
685 		addr = addr->forw;
686 	} while (addr != &entry->addr_list);
687 }
688 
689 void
690 smb_netbios_name_logf(struct name_entry *entry)
691 {
692 	char		namebuf[MAXHOSTNAMELEN];
693 	addr_entry_t	*addr;
694 
695 	smb_strname(entry, namebuf, sizeof (namebuf));
696 	syslog(LOG_DEBUG, "%s flags=0x%x\n", namebuf, entry->attributes);
697 	addr = &entry->addr_list;
698 	do {
699 		syslog(LOG_DEBUG, "  %s ttl=%d flags=0x%x port=%d",
700 		    inet_ntoa(addr->sin.sin_addr),
701 		    addr->ttl, addr->attributes,
702 		    addr->sin.sin_port);
703 		addr = addr->forw;
704 	} while (addr && (addr != &entry->addr_list));
705 }
706 
707 /*
708  * smb_netbios_name_dup
709  *
710  * Duplicate the given name entry. If 'alladdr' is 0 only
711  * copy the primary address otherwise duplicate all the
712  * addresses. NOTE that the duplicate structure is not
713  * like a regular cache entry i.e. it's a contiguous block
714  * of memory and each addr structure doesn't have it's own
715  * allocated memory. So, the returned structure can be freed
716  * by one free call.
717  */
718 struct name_entry *
719 smb_netbios_name_dup(struct name_entry *entry, int alladdr)
720 {
721 	addr_entry_t *addr;
722 	addr_entry_t *dup_addr;
723 	struct name_entry *dup;
724 	int addr_cnt = 0;
725 	int size = 0;
726 
727 	if (alladdr) {
728 		addr = entry->addr_list.forw;
729 		while (addr && (addr != &entry->addr_list)) {
730 			addr_cnt++;
731 			addr = addr->forw;
732 		}
733 	}
734 
735 	size = sizeof (struct name_entry) +
736 	    (addr_cnt * sizeof (addr_entry_t));
737 	dup = (struct name_entry *)malloc(size);
738 	if (dup == 0)
739 		return (0);
740 
741 	bzero(dup, size);
742 
743 	dup->forw = dup->back = dup;
744 	dup->attributes = entry->attributes;
745 	(void) memcpy(dup->name, entry->name, NETBIOS_NAME_SZ);
746 	(void) strlcpy((char *)dup->scope, (char *)entry->scope,
747 	    NETBIOS_DOMAIN_NAME_MAX);
748 	dup->addr_list = entry->addr_list;
749 	dup->addr_list.forw = dup->addr_list.back = &dup->addr_list;
750 
751 	if (alladdr == 0)
752 		return (dup);
753 
754 	/* LINTED - E_BAD_PTR_CAST_ALIGN */
755 	dup_addr = (addr_entry_t *)((unsigned char *)dup +
756 	    sizeof (struct name_entry));
757 
758 	addr = entry->addr_list.forw;
759 	while (addr && (addr != &entry->addr_list)) {
760 		*dup_addr = *addr;
761 		QUEUE_INSERT_TAIL(&dup->addr_list, dup_addr);
762 		addr = addr->forw;
763 		dup_addr++;
764 	}
765 
766 	return (dup);
767 }
768 
769 static void
770 smb_strname(struct name_entry *entry, char *buf, int bufsize)
771 {
772 	char	tmp[MAXHOSTNAMELEN];
773 	char	*p;
774 
775 	(void) snprintf(tmp, MAXHOSTNAMELEN, "%15.15s", entry->name);
776 	if ((p = strchr(tmp, ' ')) != NULL)
777 		*p = '\0';
778 
779 	if (entry->scope[0] != '\0') {
780 		(void) strlcat(tmp, ".", MAXHOSTNAMELEN);
781 		(void) strlcat(tmp, (char *)entry->scope, MAXHOSTNAMELEN);
782 	}
783 
784 	(void) snprintf(buf, bufsize, "%-16s  <%02X>", tmp, entry->name[15]);
785 }
786 
787 static void
788 hash_callback(HT_ITEM *item)
789 {
790 	struct name_entry *entry;
791 
792 	if (item && item->hi_data) {
793 		entry = (struct name_entry *)item->hi_data;
794 		smb_netbios_name_freeaddrs(entry);
795 		free(entry);
796 	}
797 }
798 
799 
800 /*ARGSUSED*/
801 static int
802 smb_netbios_match(const char *key1, const char *key2, size_t n)
803 {
804 	int res;
805 
806 	res = bcmp(key1, key2, NETBIOS_NAME_SZ);
807 	if (res == 0) {
808 		/* Names are the same, compare scopes */
809 		res = strcmp(key1 + NETBIOS_NAME_SZ, key2 + NETBIOS_NAME_SZ);
810 	}
811 
812 	return (res);
813 }
814 
815 static void
816 smb_netbios_cache_key(char *key, unsigned char *name, unsigned char *scope)
817 {
818 	bzero(key, NETBIOS_HKEY_SZ);
819 	(void) memcpy(key, name, NETBIOS_NAME_SZ);
820 	(void) memcpy(key + NETBIOS_NAME_SZ, scope,
821 	    strlen((const char *)scope));
822 }
823