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 * from: Id: nb.c,v 1.4 2001/04/16 04:33:01 bp Exp
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: nb.c,v 1.4 2006/03/22 02:29:42 christos Exp $");
38 #include <sys/param.h>
39 #include <sys/socket.h>
51 #include <netsmb/netbios.h>
52 #include <netsmb/smb_lib.h>
53 #include <netsmb/nb_lib.h>
56 nb_ctx_create(struct nb_ctx
**ctxpp
)
60 ctx
= malloc(sizeof(struct nb_ctx
));
63 bzero(ctx
, sizeof(struct nb_ctx
));
69 nb_ctx_done(struct nb_ctx
*ctx
)
78 nb_ctx_setns(struct nb_ctx
*ctx
, const char *addr
)
80 if (addr
== NULL
|| addr
[0] == 0)
84 if ((ctx
->nb_nsname
= strdup(addr
)) == NULL
)
90 nb_ctx_setscope(struct nb_ctx
*ctx
, const char *scope
)
92 size_t slen
= strlen(scope
);
95 smb_error("scope '%s' is too long", 0, scope
);
100 ctx
->nb_scope
= malloc(slen
+ 1);
101 if (ctx
->nb_scope
== NULL
)
103 nls_str_upper(ctx
->nb_scope
, scope
);
108 nb_ctx_resolve(struct nb_ctx
*ctx
)
110 struct sockaddr
*sap
;
113 ctx
->nb_flags
&= ~NBCF_RESOLVED
;
115 if (ctx
->nb_nsname
== NULL
) {
116 ctx
->nb_ns
.sin_addr
.s_addr
= htonl(INADDR_BROADCAST
);
118 error
= nb_resolvehost_in(ctx
->nb_nsname
, &sap
);
120 smb_error("can't resolve %s", error
, ctx
->nb_nsname
);
123 if (sap
->sa_family
!= AF_INET
) {
124 smb_error("unsupported address family %d", 0,
129 bcopy(sap
, &ctx
->nb_ns
, sizeof(ctx
->nb_ns
));
132 ctx
->nb_ns
.sin_port
= htons(137);
133 ctx
->nb_ns
.sin_family
= AF_INET
;
134 ctx
->nb_ns
.sin_len
= sizeof(ctx
->nb_ns
);
135 ctx
->nb_flags
|= NBCF_RESOLVED
;
145 nb_ctx_readrcsection(struct rcfile
*rcfile
, struct nb_ctx
*ctx
,
146 const char *sname
, int level
)
153 rc_getint(rcfile
, sname
, "nbtimeout", &ctx
->nb_timo
);
154 rc_getstringptr(rcfile
, sname
, "nbns", &p
);
156 error
= nb_ctx_setns(ctx
, p
);
158 smb_error("invalid address specified in the section %s", 0, sname
);
162 rc_getstringptr(rcfile
, sname
, "nbscope", &p
);
164 nb_ctx_setscope(ctx
, p
);
168 static const char * const nb_err_rcode
[] = {
169 "bad request/response format",
170 "NBNS server failure",
172 "unsupported request",
174 "name already registered"
177 static const char * const nb_err
[] = {
179 "too many redirects",
181 "NETBIOS name too long",
182 "no interface to broadcast on and no NBNS server specified"
186 nb_strerror(int error
)
190 if (error
<= NBERR_ACTIVE
)
191 return nb_err_rcode
[error
- 1];
192 else if (error
>= NBERR_HOSTNOTFOUND
&& error
< NBERR_MAX
)
193 return nb_err
[error
- NBERR_HOSTNOTFOUND
];