Sync usage with man page.
[netbsd-mini2440.git] / dist / smbfs / lib / smb / nb_net.c
blobcaf913f8004c55364554ae06927e86cc8c4d2bf2
1 /*
2 * Copyright (c) 2000, Boris Popov
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
30 * SUCH DAMAGE.
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>
41 #include <net/if.h>
43 #include <ctype.h>
44 #include <netdb.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <stdio.h>
50 #include <unistd.h>
51 #include <ifaddrs.h>
53 #include <netsmb/netbios.h>
54 #include <netsmb/smb_lib.h>
55 #include <netsmb/nb_lib.h>
57 int
58 nb_getlocalname(char *name)
60 char buf[1024], *cp;
62 if (gethostname(buf, sizeof(buf)) != 0)
63 return errno;
64 cp = strchr(buf, '.');
65 if (cp)
66 *cp = 0;
67 strcpy(name, buf);
68 return 0;
71 int
72 nb_resolvehost_in(const char *name, struct sockaddr **dest)
74 struct addrinfo *res, hints;
75 struct sockaddr_in *sinp;
76 int error;
77 char port[6];
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);
85 if (error) {
86 warnx("server address `%s': %s", name, gai_strerror(error));
87 return ENETDOWN;
90 /* Use first address as the address to connect to */
91 sinp = malloc(res[0].ai_addrlen);
92 if (sinp == NULL)
93 return ENOMEM;
94 memcpy(sinp, res[0].ai_addr, res[0].ai_addrlen);
96 freeaddrinfo(res);
98 *dest = (struct sockaddr*)sinp;
99 return 0;
103 nb_enum_if(struct nb_ifdesc **iflist, int maxif)
105 struct nb_ifdesc *ifd;
106 struct ifaddrs *ifp, *p;
107 int i;
109 if (getifaddrs(&ifp) < 0)
110 return errno;
112 *iflist = NULL;
113 i = 0;
114 for (p = ifp; p; p = p->ifa_next) {
116 if (i >= maxif)
117 break;
119 if ((p->ifa_addr->sa_family != AF_INET) ||
120 ((p->ifa_flags & (IFF_UP|IFF_BROADCAST))
121 != (IFF_UP|IFF_BROADCAST)))
122 continue;
123 if (strlen(p->ifa_name) >= sizeof(ifd->id_name))
124 continue;
126 ifd = malloc(sizeof(struct nb_ifdesc));
127 if (ifd == NULL) {
128 freeifaddrs(ifp);
129 /* XXX should free stuff already in *iflist */
130 return ENOMEM;
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;
138 *iflist = ifd;
139 i++;
142 freeifaddrs(ifp);
143 return 0;