nb_name.c revision 87866
1178476Sjb/* 2178476Sjb * Copyright (c) 2000, Boris Popov 3178476Sjb * All rights reserved. 4178476Sjb * 5178476Sjb * Redistribution and use in source and binary forms, with or without 6178476Sjb * modification, are permitted provided that the following conditions 7178476Sjb * are met: 8178476Sjb * 1. Redistributions of source code must retain the above copyright 9178476Sjb * notice, this list of conditions and the following disclaimer. 10178476Sjb * 2. Redistributions in binary form must reproduce the above copyright 11178476Sjb * notice, this list of conditions and the following disclaimer in the 12178476Sjb * documentation and/or other materials provided with the distribution. 13178476Sjb * 3. All advertising materials mentioning features or use of this software 14178476Sjb * must display the following acknowledgement: 15178476Sjb * This product includes software developed by Boris Popov. 16178476Sjb * 4. Neither the name of the author nor the names of any co-contributors 17178476Sjb * may be used to endorse or promote products derived from this software 18178476Sjb * without specific prior written permission. 19178476Sjb * 20178476Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21178476Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22178476Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23178476Sjb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24178476Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25178476Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26178476Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27178476Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28178476Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29178476Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30178476Sjb * SUCH DAMAGE. 31178476Sjb * 32178476Sjb * $Id: nb_name.c,v 1.1 2000/07/16 01:52:07 bp Exp $ 33178476Sjb */ 34178476Sjb#include <sys/param.h> 35178476Sjb#include <sys/socket.h> 36178476Sjb 37178476Sjb#include <ctype.h> 38178476Sjb#include <err.h> 39178476Sjb#include <errno.h> 40178476Sjb#include <stdio.h> 41178476Sjb#include <stdlib.h> 42178476Sjb#include <string.h> 43178476Sjb 44178476Sjb#include <netsmb/netbios.h> 45178476Sjb#include <netsmb/smb_lib.h> 46178476Sjb#include <netsmb/nb_lib.h> 47178476Sjb 48178476Sjbint 49178476Sjbnb_snballoc(int namelen, struct sockaddr_nb **dst) 50178476Sjb{ 51178476Sjb struct sockaddr_nb *snb; 52178476Sjb int slen; 53178476Sjb 54178476Sjb slen = namelen + sizeof(*snb) - sizeof(snb->snb_name); 55178476Sjb snb = malloc(slen); 56178476Sjb if (snb == NULL) 57178476Sjb return ENOMEM; 58178476Sjb bzero(snb, slen); 59178476Sjb snb->snb_family = AF_NETBIOS; 60178476Sjb snb->snb_len = slen; 61178476Sjb *dst = snb; 62178476Sjb return 0; 63178476Sjb} 64178476Sjb 65178476Sjbvoid 66178476Sjbnb_snbfree(struct sockaddr *snb) 67178476Sjb{ 68178476Sjb free(snb); 69178476Sjb} 70178476Sjb 71178476Sjb/* 72178476Sjb * Create a full NETBIOS address 73178476Sjb */ 74178476Sjbint 75178476Sjbnb_sockaddr(struct sockaddr *peer, struct nb_name *np, 76178476Sjb struct sockaddr_nb **dst) 77178476Sjb 78178476Sjb{ 79178476Sjb struct sockaddr_nb *snb; 80178476Sjb int nmlen, error; 81178476Sjb 82178476Sjb if (peer && (peer->sa_family != AF_INET && peer->sa_family != AF_IPX)) 83178476Sjb return EPROTONOSUPPORT; 84178476Sjb nmlen = nb_name_len(np); 85178476Sjb if (nmlen < NB_ENCNAMELEN) 86178476Sjb return EINVAL; 87178476Sjb error = nb_snballoc(nmlen, &snb); 88178476Sjb if (error) 89178476Sjb return error; 90178476Sjb if (nmlen != nb_name_encode(np, snb->snb_name)) 91178476Sjb printf("a bug somewhere in the nb_name* code\n"); 92178476Sjb if (peer) 93 memcpy(&snb->snb_tran, peer, peer->sa_len); 94 *dst = snb; 95 return 0; 96} 97 98int 99nb_name_len(struct nb_name *np) 100{ 101 u_char *name; 102 int len, sclen; 103 104 len = 1 + NB_ENCNAMELEN; 105 if (np->nn_scope == NULL) 106 return len + 1; 107 sclen = 0; 108 for (name = np->nn_scope; *name; name++) { 109 if (*name == '.') { 110 sclen = 0; 111 } else { 112 if (sclen < NB_MAXLABLEN) { 113 sclen++; 114 len++; 115 } 116 } 117 } 118 return len + 1; 119} 120 121int 122nb_encname_len(const char *str) 123{ 124 const u_char *cp = (const u_char *)str; 125 int len, blen; 126 127 if ((cp[0] & 0xc0) == 0xc0) 128 return -1; /* first two bytes are offset to name */ 129 130 len = 1; 131 for (;;) { 132 blen = *cp; 133 if (blen++ == 0) 134 break; 135 len += blen; 136 cp += blen; 137 } 138 return len; 139} 140 141#define NBENCODE(c) ((u_short)(((u_char)(c) >> 4) | \ 142 (((u_char)(c) & 0xf) << 8)) + 0x4141) 143 144static void 145memsetw(char *dst, int n, u_short word) 146{ 147 while (n--) { 148 *(u_short*)dst = word; 149 dst += 2; 150 } 151} 152 153int 154nb_name_encode(struct nb_name *np, u_char *dst) 155{ 156 u_char *name, *plen; 157 u_char *cp = dst; 158 int i, lblen; 159 160 *cp++ = NB_ENCNAMELEN; 161 name = np->nn_name; 162 if (name[0] == '*' && name[1] == 0) { 163 *(u_short*)cp = NBENCODE('*'); 164 memsetw(cp + 2, NB_NAMELEN - 1, NBENCODE(' ')); 165 cp += NB_ENCNAMELEN; 166 } else { 167 for (i = 0; *name && i < NB_NAMELEN; i++, cp += 2, name++) 168 *(u_short*)cp = NBENCODE(toupper(*name)); 169 i = NB_NAMELEN - i - 1; 170 if (i > 0) { 171 memsetw(cp, i, NBENCODE(' ')); 172 cp += i * 2; 173 } 174 *(u_short*)cp = NBENCODE(np->nn_type); 175 cp += 2; 176 } 177 *cp = 0; 178 if (np->nn_scope == NULL) 179 return nb_encname_len(dst); 180 plen = cp++; 181 lblen = 0; 182 for (name = np->nn_scope; ; name++) { 183 if (*name == '.' || *name == 0) { 184 *plen = lblen; 185 plen = cp++; 186 *plen = 0; 187 if (*name == 0) 188 break; 189 } else { 190 if (lblen < NB_MAXLABLEN) { 191 *cp++ = *name; 192 lblen++; 193 } 194 } 195 } 196 return nb_encname_len(dst); 197} 198 199