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 * from: Id: nb_net.c,v 1.4 2001/02/16 02:46:12 bp Exp
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: nb_net.c,v 1.4 2003/04/04 08:05:34 jdolecek Exp $");
38 #include <sys/param.h>
39 #include <sys/socket.h>
53 #include <netsmb/netbios.h>
54 #include <netsmb/smb_lib.h>
55 #include <netsmb/nb_lib.h>
58 nb_getlocalname(char *name
)
62 if (gethostname(buf
, sizeof(buf
)) != 0)
64 cp
= strchr(buf
, '.');
72 nb_resolvehost_in(const char *name
, struct sockaddr
**dest
)
74 struct addrinfo
*res
, hints
;
75 struct sockaddr_in
*sinp
;
79 memset(&hints
, 0, sizeof(hints
));
80 hints
.ai_family
= PF_INET
;
81 hints
.ai_socktype
= SOCK_STREAM
;
82 snprintf(port
, sizeof(port
), "%d", SMB_TCP_PORT
);
84 error
= getaddrinfo(name
, port
, &hints
, &res
);
86 warnx("server address `%s': %s", name
, gai_strerror(error
));
90 /* Use first address as the address to connect to */
91 sinp
= malloc(res
[0].ai_addrlen
);
94 memcpy(sinp
, res
[0].ai_addr
, res
[0].ai_addrlen
);
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
;