2001-12-14 11:06:03 +00:00
|
|
|
/*
|
2001-12-20 16:16:25 +00:00
|
|
|
* Copyright (c) 2000-2001, Boris Popov
|
2001-12-14 11:06:03 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Boris Popov.
|
|
|
|
* 4. Neither the name of the author nor the names of any co-contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2001-12-20 16:16:25 +00:00
|
|
|
* $Id: nb_name.c,v 1.2 2001/08/22 03:31:36 bp Exp $
|
2001-12-14 11:06:03 +00:00
|
|
|
*/
|
Remove macros
htole{s,l,q}, letoh{s,l,q}, htobe{s,l,q}, betoh{s,l,q}
and replace it with more standard byteorder macros in our system.
2005-09-19 08:07:18 +00:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-12-14 11:06:03 +00:00
|
|
|
#include <sys/param.h>
|
Remove macros
htole{s,l,q}, letoh{s,l,q}, htobe{s,l,q}, betoh{s,l,q}
and replace it with more standard byteorder macros in our system.
2005-09-19 08:07:18 +00:00
|
|
|
#include <sys/endian.h>
|
2001-12-14 11:06:03 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <netsmb/netbios.h>
|
|
|
|
#include <netsmb/smb_lib.h>
|
|
|
|
#include <netsmb/nb_lib.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
nb_snballoc(int namelen, struct sockaddr_nb **dst)
|
|
|
|
{
|
|
|
|
struct sockaddr_nb *snb;
|
|
|
|
int slen;
|
|
|
|
|
|
|
|
slen = namelen + sizeof(*snb) - sizeof(snb->snb_name);
|
|
|
|
snb = malloc(slen);
|
|
|
|
if (snb == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
bzero(snb, slen);
|
|
|
|
snb->snb_family = AF_NETBIOS;
|
|
|
|
snb->snb_len = slen;
|
|
|
|
*dst = snb;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nb_snbfree(struct sockaddr *snb)
|
|
|
|
{
|
|
|
|
free(snb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a full NETBIOS address
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
nb_sockaddr(struct sockaddr *peer, struct nb_name *np,
|
|
|
|
struct sockaddr_nb **dst)
|
|
|
|
|
|
|
|
{
|
|
|
|
struct sockaddr_nb *snb;
|
|
|
|
int nmlen, error;
|
|
|
|
|
|
|
|
if (peer && (peer->sa_family != AF_INET && peer->sa_family != AF_IPX))
|
|
|
|
return EPROTONOSUPPORT;
|
|
|
|
nmlen = nb_name_len(np);
|
|
|
|
if (nmlen < NB_ENCNAMELEN)
|
|
|
|
return EINVAL;
|
|
|
|
error = nb_snballoc(nmlen, &snb);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
if (nmlen != nb_name_encode(np, snb->snb_name))
|
|
|
|
printf("a bug somewhere in the nb_name* code\n");
|
|
|
|
if (peer)
|
|
|
|
memcpy(&snb->snb_tran, peer, peer->sa_len);
|
|
|
|
*dst = snb;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nb_name_len(struct nb_name *np)
|
|
|
|
{
|
|
|
|
u_char *name;
|
|
|
|
int len, sclen;
|
|
|
|
|
|
|
|
len = 1 + NB_ENCNAMELEN;
|
|
|
|
if (np->nn_scope == NULL)
|
|
|
|
return len + 1;
|
|
|
|
sclen = 0;
|
|
|
|
for (name = np->nn_scope; *name; name++) {
|
|
|
|
if (*name == '.') {
|
|
|
|
sclen = 0;
|
|
|
|
} else {
|
|
|
|
if (sclen < NB_MAXLABLEN) {
|
|
|
|
sclen++;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nb_encname_len(const char *str)
|
|
|
|
{
|
|
|
|
const u_char *cp = (const u_char *)str;
|
|
|
|
int len, blen;
|
|
|
|
|
|
|
|
if ((cp[0] & 0xc0) == 0xc0)
|
|
|
|
return -1; /* first two bytes are offset to name */
|
|
|
|
|
|
|
|
len = 1;
|
|
|
|
for (;;) {
|
|
|
|
blen = *cp;
|
|
|
|
if (blen++ == 0)
|
|
|
|
break;
|
|
|
|
len += blen;
|
|
|
|
cp += blen;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-12-21 17:17:00 +00:00
|
|
|
static inline void
|
|
|
|
nb_char_encode(u_char **ptr, u_char c, int n)
|
2001-12-14 11:06:03 +00:00
|
|
|
{
|
2015-12-21 17:17:00 +00:00
|
|
|
|
2001-12-14 11:06:03 +00:00
|
|
|
while (n--) {
|
2015-12-21 17:17:00 +00:00
|
|
|
*(*ptr)++ = 0x41 + (c >> 4);
|
|
|
|
*(*ptr)++ = 0x41 + (c & 0x0f);
|
2001-12-14 11:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nb_name_encode(struct nb_name *np, u_char *dst)
|
|
|
|
{
|
|
|
|
u_char *name, *plen;
|
|
|
|
u_char *cp = dst;
|
|
|
|
int i, lblen;
|
|
|
|
|
|
|
|
*cp++ = NB_ENCNAMELEN;
|
|
|
|
name = np->nn_name;
|
|
|
|
if (name[0] == '*' && name[1] == 0) {
|
2015-12-21 17:17:00 +00:00
|
|
|
nb_char_encode(&cp, '*', 1);
|
|
|
|
nb_char_encode(&cp, ' ', NB_NAMELEN - 1);
|
2001-12-14 11:06:03 +00:00
|
|
|
} else {
|
2015-12-21 17:17:00 +00:00
|
|
|
for (i = 0; i < NB_NAMELEN - 1; i++)
|
|
|
|
if (*name != 0)
|
|
|
|
nb_char_encode(&cp, toupper(*name++), 1);
|
|
|
|
else
|
|
|
|
nb_char_encode(&cp, ' ', 1);
|
|
|
|
nb_char_encode(&cp, np->nn_type, 1);
|
2001-12-14 11:06:03 +00:00
|
|
|
}
|
|
|
|
*cp = 0;
|
|
|
|
if (np->nn_scope == NULL)
|
|
|
|
return nb_encname_len(dst);
|
|
|
|
plen = cp++;
|
|
|
|
lblen = 0;
|
|
|
|
for (name = np->nn_scope; ; name++) {
|
|
|
|
if (*name == '.' || *name == 0) {
|
|
|
|
*plen = lblen;
|
|
|
|
plen = cp++;
|
|
|
|
*plen = 0;
|
|
|
|
if (*name == 0)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (lblen < NB_MAXLABLEN) {
|
|
|
|
*cp++ = *name;
|
|
|
|
lblen++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nb_encname_len(dst);
|
|
|
|
}
|
|
|
|
|