xref: /freebsd/contrib/smbfs/lib/smb/nb_net.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*
2  * Copyright (c) 2000, Boris Popov
3  * All rights reserved.
4  *
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.
19  *
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.
31  *
32  * $Id: nb_net.c,v 1.4 2001/02/16 02:46:12 bp Exp $
33  * $FreeBSD$
34  */
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 
38 #include <net/if.h>
39 
40 #include <ctype.h>
41 #include <netdb.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <ifaddrs.h>
49 
50 #include <netsmb/netbios.h>
51 #include <netsmb/smb_lib.h>
52 #include <netsmb/nb_lib.h>
53 
54 int
55 nb_getlocalname(char *name)
56 {
57 	char buf[1024], *cp;
58 
59 	if (gethostname(buf, sizeof(buf)) != 0)
60 		return errno;
61 	cp = strchr(buf, '.');
62 	if (cp)
63 		*cp = 0;
64 	strcpy(name, buf);
65 	return 0;
66 }
67 
68 int
69 nb_resolvehost_in(const char *name, struct sockaddr **dest, long smbtcpport)
70 {
71 	struct hostent* h;
72 	struct sockaddr_in *sinp;
73 	int len;
74 
75 	h = gethostbyname(name);
76 	if (!h) {
77 		warnx("can't get server address `%s': ", name);
78 		herror(NULL);
79 		return ENETDOWN;
80 	}
81 	if (h->h_addrtype != AF_INET) {
82 		warnx("address for `%s' is not in the AF_INET family", name);
83 		return EAFNOSUPPORT;
84 	}
85 	if (h->h_length != 4) {
86 		warnx("address for `%s' has invalid length", name);
87 		return EAFNOSUPPORT;
88 	}
89 	len = sizeof(struct sockaddr_in);
90 	sinp = malloc(len);
91 	if (sinp == NULL)
92 		return ENOMEM;
93 	bzero(sinp, len);
94 	sinp->sin_len = len;
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;
99 	return 0;
100 }
101 
102 int
103 nb_enum_if(struct nb_ifdesc **iflist, int maxif)
104 {
105 	struct nb_ifdesc *ifd;
106 	struct ifaddrs *ifp, *p;
107 	int i;
108 
109 	if (getifaddrs(&ifp) < 0)
110 		return errno;
111 
112 	*iflist = NULL;
113 	i = 0;
114 	for (p = ifp; p; p = p->ifa_next) {
115 
116 		if (i >= maxif)
117 			break;
118 
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;
125 
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;
131 		}
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++;
140 	}
141 
142 	freeifaddrs(ifp);
143 	return 0;
144 }
145 
146 /*ARGSUSED*/
147 /*int
148 nbns_resolvename(const char *name, struct sockaddr **dest)
149 {
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");
152 	return EHOSTUNREACH;
153 }*/
154 /*
155 int
156 nb_hostlookup(struct nb_name *np, const char *server, const char *hint,
157 	struct sockaddr_nb **dst)
158 {
159 	struct sockaddr_nb *snb;
160 	int error;
161 
162 	error = nb_sockaddr(NULL, np, &snb);
163 	if (error)
164 		return error;
165 	do {
166 		if (hint) {
167 			error = nb_resolvehost_in(host, snb, SMB_TCP_PORT);
168 			if (error)
169 				break;
170 		} else {
171 			error = nb_resolvename(server);
172 		}
173 	} while(0);
174 	if (!error) {
175 		*dst = snb;
176 	} else
177 		nb_snbfree(snb);
178 	return error;
179 }
180 */
181