1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
/*
* Copyright (c) 2000, Boris Popov
* 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.
*
* $Id: nb_net.c,v 1.8 2004/03/19 01:49:47 lindak Exp $
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <ctype.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#include <err.h>
#include <netsmb/netbios.h>
#include <netsmb/smb_lib.h>
#include <netsmb/nb_lib.h>
int
nb_getlocalname(char *name, size_t maxlen)
{
char buf[1024], *cp;
if (gethostname(buf, sizeof (buf)) != 0)
return (errno);
cp = strchr(buf, '.');
if (cp)
*cp = 0;
strlcpy(name, buf, maxlen);
return (0);
}
int
nb_resolvehost_in(const char *name, struct sockaddr **dest)
{
struct hostent *h;
struct sockaddr_in *sinp;
in_addr_t addr;
struct in_addr in;
int len;
char **p;
h = gethostbyname(name);
if (!h) {
#ifdef DEBUG
warnx("can't get server address `%s': ", name);
#endif
return (ENETDOWN);
}
if (h->h_addrtype != AF_INET) {
#ifdef DEBUG
warnx("address for `%s' is not in the AF_INET family", name);
#endif
return (EAFNOSUPPORT);
}
if (h->h_length != 4) {
#ifdef DEBUG
warnx("address for `%s' has invalid length", name);
#endif
return (EAFNOSUPPORT);
}
len = sizeof (struct sockaddr_in);
sinp = malloc(len);
if (sinp == NULL)
return (ENOMEM);
bzero(sinp, len);
/*
* There is no sin_len in sockaddr_in structure on Solaris.
* sinp->sin_len = len;
*/
sinp->sin_family = h->h_addrtype;
memcpy(&sinp->sin_addr.s_addr, *h->h_addr_list,\
sizeof (sinp->sin_addr.s_addr));
sinp->sin_port = htons(SMB_TCP_PORT);
*dest = (struct sockaddr *)sinp;
return (0);
}
#ifdef NOT_DEFINED
int
nb_enum_if(struct nb_ifdesc **iflist) {
struct lifconf ifc;
struct lifreq *ifrqp;
struct nb_ifdesc *ifd;
struct in_addr iaddr, imask;
struct lifnum ifn;
char *ifrdata, *iname;
int s, rdlen, ifcnt, error, iflags, i;
*iflist = NULL;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1)
return (errno);
/* Get number of interfaces. */
ifn.lifn_family = AF_INET;
ifn.lifn_flags = 0;
ifn.lifn_count = 0;
if (ioctl(s, SIOCGLIFNUM, &ifn) != 0) {
error = errno;
goto bad;
}
rdlen = ifn.lifn_count * sizeof (struct lifreq);
ifrdata = malloc(rdlen);
if (ifrdata == NULL) {
error = ENOMEM;
goto bad;
}
ifc.lifc_flags = 0;
ifc.lifc_family = AF_INET;
ifc.lifc_len = rdlen;
ifc.lifc_buf = ifrdata;
if (ioctl(s, SIOCGLIFCONF, &ifc) != 0) {
error = errno;
goto bad;
}
ifrqp = ifc.lifc_req;
ifcnt = ifc.lifc_len / sizeof (struct lifreq);
error = 0;
for (i = 0; i < ifcnt; i++, ifrqp++) {
/* XXX for now, avoid IP6 broadcast performance costs */
if (ifrqp->lifr_addr.ss_family != AF_INET)
continue;
if (ioctl(s, SIOCGLIFFLAGS, ifrqp) != 0)
continue;
iflags = ifrqp->lifr_flags;
if ((iflags & IFF_UP) == 0 || (iflags & IFF_BROADCAST) == 0)
continue;
if (ioctl(s, SIOCGLIFADDR, ifrqp) != 0 ||
ifrqp->lifr_addr.ss_family != AF_INET) {
continue;
}
iname = ifrqp->lifr_name;
if (strlen(iname) >= sizeof (ifd->id_name))
continue;
iaddr = (*(struct sockaddr_in *)&ifrqp->lifr_addr).sin_addr;
if (ioctl(s, SIOCGLIFNETMASK, ifrqp) != 0)
continue;
imask = ((struct sockaddr_in *)&ifrqp->lifr_addr)->sin_addr;
ifd = malloc(sizeof (struct nb_ifdesc));
if (ifd == NULL)
return (ENOMEM);
bzero(ifd, sizeof (struct nb_ifdesc));
strcpy(ifd->id_name, iname);
ifd->id_flags = iflags;
ifd->id_addr = iaddr;
ifd->id_mask = imask;
ifd->id_next = *iflist;
*iflist = ifd;
}
bad:
free(ifrdata);
close(s);
return (error);
}
/*ARGSUSED*/
int
nbns_resolvename(const char *name, struct sockaddr **dest)
{
printf("NetBIOS name resolver is not included in this distribution.\n");
printf("Please use '-I' option to specify an IP address of server.\n");
return (EHOSTUNREACH);
}
int
nb_hostlookup(struct nb_name *np, const char *server, const char *hint,
struct sockaddr_nb **dst)
{
struct sockaddr_nb *snb;
int error;
error = nb_sockaddr(NULL, np, &snb);
if (error)
return (error);
do {
if (hint) {
error = nb_resolvehost_in(host, snb);
if (error)
break;
} else {
error = nb_resolvename(server);
}
} while (0);
if (!error) {
*dst = snb;
} else
nb_snbfree(snb);
return (error);
}
#endif
|