2 * Copyright (c) 2000-2001, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: nb_name.c,v 1.2 2001/08/22 03:31:36 bp Exp $
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/endian.h>
40 #include <sys/socket.h>
49 #include <netsmb/netbios.h>
50 #include <netsmb/smb_lib.h>
51 #include <netsmb/nb_lib.h>
54 nb_snballoc(int namelen, struct sockaddr_nb **dst)
56 struct sockaddr_nb *snb;
59 slen = namelen + sizeof(*snb) - sizeof(snb->snb_name);
64 snb->snb_family = AF_NETBIOS;
71 nb_snbfree(struct sockaddr *snb)
77 * Create a full NETBIOS address
80 nb_sockaddr(struct sockaddr *peer, struct nb_name *np,
81 struct sockaddr_nb **dst)
84 struct sockaddr_nb *snb;
87 if (peer && (peer->sa_family != AF_INET && peer->sa_family != AF_IPX))
88 return EPROTONOSUPPORT;
89 nmlen = nb_name_len(np);
90 if (nmlen < NB_ENCNAMELEN)
92 error = nb_snballoc(nmlen, &snb);
95 if (nmlen != nb_name_encode(np, snb->snb_name))
96 printf("a bug somewhere in the nb_name* code\n");
98 memcpy(&snb->snb_tran, peer, peer->sa_len);
104 nb_name_len(struct nb_name *np)
109 len = 1 + NB_ENCNAMELEN;
110 if (np->nn_scope == NULL)
113 for (name = np->nn_scope; *name; name++) {
117 if (sclen < NB_MAXLABLEN) {
127 nb_encname_len(const char *str)
129 const u_char *cp = (const u_char *)str;
132 if ((cp[0] & 0xc0) == 0xc0)
133 return -1; /* first two bytes are offset to name */
147 nb_char_encode(u_char **ptr, u_char c, int n)
151 *(*ptr)++ = 0x41 + (c >> 4);
152 *(*ptr)++ = 0x41 + (c & 0x0f);
157 nb_name_encode(struct nb_name *np, u_char *dst)
163 *cp++ = NB_ENCNAMELEN;
165 if (name[0] == '*' && name[1] == 0) {
166 nb_char_encode(&cp, '*', 1);
167 nb_char_encode(&cp, ' ', NB_NAMELEN - 1);
169 for (i = 0; i < NB_NAMELEN - 1; i++)
171 nb_char_encode(&cp, toupper(*name++), 1);
173 nb_char_encode(&cp, ' ', 1);
174 nb_char_encode(&cp, np->nn_type, 1);
177 if (np->nn_scope == NULL)
178 return nb_encname_len(dst);
181 for (name = np->nn_scope; ; name++) {
182 if (*name == '.' || *name == 0) {
189 if (lblen < NB_MAXLABLEN) {
195 return nb_encname_len(dst);