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 $
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/ioctl.h>
49 #include <netsmb/netbios.h>
50 #include <netsmb/smb_lib.h>
51 #include <netsmb/nb_lib.h>
54 nb_getlocalname(char *name)
58 if (gethostname(buf, sizeof(buf)) != 0)
60 cp = strchr(buf, '.');
68 nb_resolvehost_in(const char *name, struct sockaddr **dest)
71 struct sockaddr_in *sinp;
74 h = gethostbyname(name);
76 warnx("can't get server address `%s': ", name);
80 if (h->h_addrtype != AF_INET) {
81 warnx("address for `%s' is not in the AF_INET family", name);
84 if (h->h_length != 4) {
85 warnx("address for `%s' has invalid length", name);
88 len = sizeof(struct sockaddr_in);
94 sinp->sin_family = h->h_addrtype;
95 memcpy(&sinp->sin_addr.s_addr, h->h_addr, 4);
96 sinp->sin_port = htons(SMB_TCP_PORT);
97 *dest = (struct sockaddr*)sinp;
102 nb_enum_if(struct nb_ifdesc **iflist, int maxif)
106 struct nb_ifdesc *ifd;
107 struct in_addr iaddr, imask;
108 char *ifrdata, *iname;
109 int s, rdlen, ifcnt, error, iflags, i;
112 s = socket(AF_INET, SOCK_DGRAM, 0);
116 rdlen = maxif * sizeof(struct ifreq);
117 ifrdata = malloc(rdlen);
118 if (ifrdata == NULL) {
123 ifc.ifc_buf = ifrdata;
124 if (ioctl(s, SIOCGIFCONF, &ifc) != 0) {
129 ifcnt = ifc.ifc_len / sizeof(struct ifreq);
131 for (i = 0; i < ifcnt; i++, ifrqp++) {
132 if (ioctl(s, SIOCGIFFLAGS, ifrqp) != 0)
134 iflags = ifrqp->ifr_flags;
135 if ((iflags & IFF_UP) == 0 || (iflags & IFF_BROADCAST) == 0)
138 if (ioctl(s, SIOCGIFADDR, ifrqp) != 0 ||
139 ifrqp->ifr_addr.sa_family != AF_INET)
141 iname = ifrqp->ifr_name;
142 if (strlen(iname) >= sizeof(ifd->id_name))
144 iaddr = (*(struct sockaddr_in *)&ifrqp->ifr_addr).sin_addr;
146 if (ioctl(s, SIOCGIFNETMASK, ifrqp) != 0)
148 imask = ((struct sockaddr_in *)&ifrqp->ifr_addr)->sin_addr;
150 ifd = malloc(sizeof(struct nb_ifdesc));
153 bzero(ifd, sizeof(struct nb_ifdesc));
154 strcpy(ifd->id_name, iname);
155 ifd->id_flags = iflags;
156 ifd->id_addr = iaddr;
157 ifd->id_mask = imask;
158 ifd->id_next = *iflist;
169 nbns_resolvename(const char *name, struct sockaddr **dest)
171 printf("NetBIOS name resolver is not included in this distribution.\n");
172 printf("Please use '-I' option to specify an IP address of server.\n");
177 nb_hostlookup(struct nb_name *np, const char *server, const char *hint,
178 struct sockaddr_nb **dst)
180 struct sockaddr_nb *snb;
183 error = nb_sockaddr(NULL, np, &snb);
188 error = nb_resolvehost_in(host, snb);
192 error = nb_resolvename(server);