xref: /titanic_50/usr/src/lib/libsmbfs/smb/nb_net.c (revision 753a6d457b330b1b29b2d3eefcd0831116ce950d)
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.8 2004/03/19 01:49:47 lindak Exp $
33  */
34 
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38 #include <sys/sockio.h>
39 #include <net/if.h>
40 #include <ctype.h>
41 #include <netdb.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <strings.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 
49 #include <err.h>
50 
51 #include <netsmb/netbios.h>
52 #include <netsmb/smb_lib.h>
53 #include <netsmb/nb_lib.h>
54 #include "private.h"
55 
56 /*
57  * General networking stuff, in spite of the names
58  * that imply they're specific to NetBIOS.
59  */
60 
61 int
62 nb_getlocalname(char *name, size_t maxlen)
63 {
64 	char buf[1024], *cp;
65 
66 	if (gethostname(buf, sizeof (buf)) != 0)
67 		return (errno);
68 	cp = strchr(buf, '.');
69 	if (cp)
70 		*cp = 0;
71 	strlcpy(name, buf, maxlen);
72 	return (0);
73 }
74 
75 int
76 nb_resolvehost_in(const char *name, struct sockaddr **dest)
77 {
78 	struct hostent *h;
79 	struct sockaddr_in *sinp;
80 	in_addr_t	addr;
81 	struct in_addr in;
82 	int len;
83 	char **p;
84 
85 
86 	h = gethostbyname(name);
87 	if (!h) {
88 #ifdef DEBUG
89 		warnx("can't get server address `%s': ", name);
90 #endif
91 		return (ENETDOWN);
92 	}
93 	if (h->h_addrtype != AF_INET) {
94 #ifdef DEBUG
95 		warnx("address for `%s' is not in the AF_INET family", name);
96 #endif
97 		return (EAFNOSUPPORT);
98 	}
99 	if (h->h_length != 4) {
100 #ifdef DEBUG
101 		warnx("address for `%s' has invalid length", name);
102 #endif
103 		return (EAFNOSUPPORT);
104 	}
105 	len = sizeof (struct sockaddr_in);
106 	sinp = malloc(len);
107 	if (sinp == NULL)
108 		return (ENOMEM);
109 	bzero(sinp, len);
110 	/*
111 	 * There is no sin_len in sockaddr_in structure on Solaris.
112 	 * sinp->sin_len = len;
113 	 */
114 	sinp->sin_family = h->h_addrtype;
115 	memcpy(&sinp->sin_addr.s_addr, *h->h_addr_list,\
116 	    sizeof (sinp->sin_addr.s_addr));
117 	sinp->sin_port = htons(SMB_TCP_PORT);
118 	*dest = (struct sockaddr *)sinp;
119 	return (0);
120 }
121