2 * Copyright (c) 2000, 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_net.c,v 1.4 2001/02/16 02:46:12 bp Exp $
35 #include <sys/param.h>
36 #include <sys/socket.h>
50 #include <netsmb/netbios.h>
51 #include <netsmb/smb_lib.h>
52 #include <netsmb/nb_lib.h>
55 nb_getlocalname(char *name)
59 if (gethostname(buf, sizeof(buf)) != 0)
61 cp = strchr(buf, '.');
69 nb_resolvehost_in(const char *name, struct sockaddr **dest, long smbtcpport)
72 struct sockaddr_in *sinp;
75 h = gethostbyname(name);
77 warnx("can't get server address `%s': ", name);
81 if (h->h_addrtype != AF_INET) {
82 warnx("address for `%s' is not in the AF_INET family", name);
85 if (h->h_length != 4) {
86 warnx("address for `%s' has invalid length", name);
89 len = sizeof(struct sockaddr_in);
95 sinp->sin_family = h->h_addrtype;
96 memcpy(&sinp->sin_addr.s_addr, h->h_addr, 4);
97 sinp->sin_port = htons(smbtcpport);
98 *dest = (struct sockaddr*)sinp;
103 nb_enum_if(struct nb_ifdesc **iflist, int maxif)
105 struct nb_ifdesc *ifd;
106 struct ifaddrs *ifp, *p;
109 if (getifaddrs(&ifp) < 0)
114 for (p = ifp; p; p = p->ifa_next) {
119 if ((p->ifa_addr->sa_family != AF_INET) ||
120 ((p->ifa_flags & (IFF_UP|IFF_BROADCAST))
121 != (IFF_UP|IFF_BROADCAST)))
123 if (strlen(p->ifa_name) >= sizeof(ifd->id_name))
126 ifd = malloc(sizeof(struct nb_ifdesc));
129 /* XXX should free stuff already in *iflist */
132 bzero(ifd, sizeof(struct nb_ifdesc));
133 strcpy(ifd->id_name, p->ifa_name);
134 ifd->id_flags = p->ifa_flags;
135 ifd->id_addr = ((struct sockaddr_in *)p->ifa_addr)->sin_addr;
136 ifd->id_mask = ((struct sockaddr_in *)p->ifa_netmask)->sin_addr;
137 ifd->id_next = *iflist;
148 nbns_resolvename(const char *name, struct sockaddr **dest)
150 printf("NetBIOS name resolver is not included in this distribution.\n");
151 printf("Please use '-I' option to specify an IP address of server.\n");
156 nb_hostlookup(struct nb_name *np, const char *server, const char *hint,
157 struct sockaddr_nb **dst)
159 struct sockaddr_nb *snb;
162 error = nb_sockaddr(NULL, np, &snb);
167 error = nb_resolvehost_in(host, snb, SMB_TCP_PORT);
171 error = nb_resolvename(server);