1294328Sdes/*
298675Sdes * Copyright (c) 2000-2001, Boris Popov
398675Sdes * All rights reserved.
498675Sdes *
598675Sdes * Redistribution and use in source and binary forms, with or without
698675Sdes * modification, are permitted provided that the following conditions
798675Sdes * are met:
898675Sdes * 1. Redistributions of source code must retain the above copyright
998675Sdes *    notice, this list of conditions and the following disclaimer.
1098675Sdes * 2. Redistributions in binary form must reproduce the above copyright
1198675Sdes *    notice, this list of conditions and the following disclaimer in the
1298675Sdes *    documentation and/or other materials provided with the distribution.
1398675Sdes * 3. All advertising materials mentioning features or use of this software
1498675Sdes *    must display the following acknowledgement:
1598675Sdes *    This product includes software developed by Boris Popov.
1698675Sdes * 4. Neither the name of the author nor the names of any co-contributors
1798675Sdes *    may be used to endorse or promote products derived from this software
1898675Sdes *    without specific prior written permission.
1998675Sdes *
2098675Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2198675Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2298675Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2398675Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2498675Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2598675Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2698675Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2798675Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28162852Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29162852Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30162852Sdes * SUCH DAMAGE.
31162852Sdes *
32162852Sdes * $Id: nb_name.c,v 1.2 2001/08/22 03:31:36 bp Exp $
33181111Sdes */
34162852Sdes
35294328Sdes#include <sys/cdefs.h>
36294328Sdes__FBSDID("$FreeBSD$");
37162852Sdes
38181111Sdes#include <sys/param.h>
39162852Sdes#include <sys/endian.h>
40162852Sdes#include <sys/socket.h>
41162852Sdes
4298675Sdes#include <ctype.h>
4398675Sdes#include <err.h>
4498675Sdes#include <errno.h>
45162852Sdes#include <stdio.h>
46294328Sdes#include <stdlib.h>
4798675Sdes#include <string.h>
4898675Sdes
4998675Sdes#include <netsmb/netbios.h>
50162852Sdes#include <netsmb/smb_lib.h>
51162852Sdes#include <netsmb/nb_lib.h>
52162852Sdes
5398675Sdesint
5498675Sdesnb_snballoc(int namelen, struct sockaddr_nb **dst)
5598675Sdes{
5698675Sdes	struct sockaddr_nb *snb;
5798675Sdes	int slen;
5898675Sdes
5998675Sdes	slen = namelen + sizeof(*snb) - sizeof(snb->snb_name);
6098675Sdes	snb = malloc(slen);
6198675Sdes	if (snb == NULL)
6298675Sdes		return ENOMEM;
6398675Sdes	bzero(snb, slen);
6498675Sdes	snb->snb_family = AF_NETBIOS;
6598675Sdes	snb->snb_len = slen;
66215116Sdes	*dst = snb;
67124208Sdes	return 0;
68124208Sdes}
6998675Sdes
7098675Sdesvoid
7198675Sdesnb_snbfree(struct sockaddr *snb)
7298675Sdes{
7398675Sdes	free(snb);
7498675Sdes}
7598675Sdes
76/*
77 * Create a full NETBIOS address
78 */
79int
80nb_sockaddr(struct sockaddr *peer, struct nb_name *np,
81	struct sockaddr_nb **dst)
82
83{
84	struct sockaddr_nb *snb;
85	int nmlen, error;
86
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)
91		return EINVAL;
92	error = nb_snballoc(nmlen, &snb);
93	if (error)
94		return error;
95	if (nmlen != nb_name_encode(np, snb->snb_name))
96		printf("a bug somewhere in the nb_name* code\n");
97	if (peer)
98		memcpy(&snb->snb_tran, peer, peer->sa_len);
99	*dst = snb;
100	return 0;
101}
102
103int
104nb_name_len(struct nb_name *np)
105{
106	u_char *name;
107	int len, sclen;
108
109	len = 1 + NB_ENCNAMELEN;
110	if (np->nn_scope == NULL)
111		return len + 1;
112	sclen = 0;
113	for (name = np->nn_scope; *name; name++) {
114		if (*name == '.') {
115			sclen = 0;
116		} else {
117			if (sclen < NB_MAXLABLEN) {
118				sclen++;
119				len++;
120			}
121		}
122	}
123	return len + 1;
124}
125
126int
127nb_encname_len(const char *str)
128{
129	const u_char *cp = (const u_char *)str;
130	int len, blen;
131
132	if ((cp[0] & 0xc0) == 0xc0)
133		return -1;	/* first two bytes are offset to name */
134
135	len = 1;
136	for (;;) {
137		blen = *cp;
138		if (blen++ == 0)
139			break;
140		len += blen;
141		cp += blen;
142	}
143	return len;
144}
145
146static inline void
147nb_char_encode(u_char **ptr, u_char c, int n)
148{
149
150	while (n--) {
151		*(*ptr)++ = 0x41 + (c >> 4);
152		*(*ptr)++ = 0x41 + (c & 0x0f);
153	}
154}
155
156int
157nb_name_encode(struct nb_name *np, u_char *dst)
158{
159	u_char *name, *plen;
160	u_char *cp = dst;
161	int i, lblen;
162
163	*cp++ = NB_ENCNAMELEN;
164	name = np->nn_name;
165	if (name[0] == '*' && name[1] == 0) {
166		nb_char_encode(&cp, '*', 1);
167		nb_char_encode(&cp, ' ', NB_NAMELEN - 1);
168	} else {
169		for (i = 0; i < NB_NAMELEN - 1; i++)
170			if (*name != 0)
171				nb_char_encode(&cp, toupper(*name++), 1);
172			else
173				nb_char_encode(&cp, ' ', 1);
174		nb_char_encode(&cp, np->nn_type, 1);
175	}
176	*cp = 0;
177	if (np->nn_scope == NULL)
178		return nb_encname_len(dst);
179	plen = cp++;
180	lblen = 0;
181	for (name = np->nn_scope; ; name++) {
182		if (*name == '.' || *name == 0) {
183			*plen = lblen;
184			plen = cp++;
185			*plen = 0;
186			if (*name == 0)
187				break;
188		} else {
189			if (lblen < NB_MAXLABLEN) {
190				*cp++ = *name;
191				lblen++;
192			}
193		}
194	}
195	return nb_encname_len(dst);
196}
197
198