4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Functions to get list of addresses (TCP and/or NetBIOS)
41 #include <sys/types.h>
43 #include <sys/byteorder.h>
44 #include <sys/socket.h>
45 #include <sys/fcntl.h>
47 #include <netinet/in.h>
48 #include <netinet/tcp.h>
49 #include <arpa/inet.h>
51 #include <netsmb/smb.h>
52 #include <netsmb/smb_lib.h>
53 #include <netsmb/netbios.h>
54 #include <netsmb/nb_lib.h>
55 #include <netsmb/smb_dev.h>
61 dump_addrinfo(struct addrinfo
*ai
)
70 for (i
= 0; ai
; i
++, ai
= ai
->ai_next
) {
71 printf("ai[%d]: af=%d, len=%d", i
,
72 ai
->ai_family
, ai
->ai_addrlen
);
73 dump_sockaddr(ai
->ai_addr
);
74 if (ai
->ai_canonname
) {
75 printf("ai[%d]: cname=\"%s\"\n",
82 dump_sockaddr(struct sockaddr
*sa
)
84 char paddrbuf
[INET6_ADDRSTRLEN
];
85 struct sockaddr_in
*sin
;
86 struct sockaddr_in6
*sin6
;
87 int af
= sa
->sa_family
;
90 printf(" saf=%d,", af
);
92 case AF_NETBIOS
: /* see nbns_rq.c */
95 ip
= inet_ntop(AF_INET
, &sin
->sin_addr
,
96 paddrbuf
, sizeof (paddrbuf
));
100 ip
= inet_ntop(AF_INET6
, &sin6
->sin6_addr
,
101 paddrbuf
, sizeof (paddrbuf
));
107 printf(" IP=%s\n", ip
);
112 * SMB client name resolution - normal, and/or NetBIOS.
113 * Returns an EAI_xxx error number like getaddrinfo(3)
116 smb_ctx_getaddr(struct smb_ctx
*ctx
)
118 struct nb_ctx
*nbc
= ctx
->ct_nb
;
119 struct addrinfo hints
, *res
;
123 if (ctx
->ct_fullserver
== NULL
|| ctx
->ct_fullserver
[0] == '\0')
126 if (ctx
->ct_addrinfo
!= NULL
) {
127 freeaddrinfo(ctx
->ct_addrinfo
);
128 ctx
->ct_addrinfo
= NULL
;
132 * If the user specified an address, use it,
133 * and don't do NetBIOS lookup.
135 if (ctx
->ct_srvaddr_s
) {
136 srvaddr_str
= ctx
->ct_srvaddr_s
;
137 nbc
->nb_flags
&= ~NBCF_NS_ENABLE
;
139 srvaddr_str
= ctx
->ct_fullserver
;
142 * Default the server name we'll use in the
143 * protocol (i.e. NTLM, tree connect).
145 strlcpy(ctx
->ct_srvname
, ctx
->ct_fullserver
,
146 sizeof (ctx
->ct_srvname
));
149 * Try to lookup the host address using the
150 * normal name-to-IP address mechanisms.
151 * If that fails, we MAY try NetBIOS.
153 memset(&hints
, 0, sizeof (hints
));
154 hints
.ai_flags
= AI_CANONNAME
;
155 hints
.ai_family
= PF_UNSPEC
;
156 hints
.ai_socktype
= SOCK_STREAM
;
157 gaierr
= getaddrinfo(srvaddr_str
, NULL
, &hints
, &res
);
159 ctx
->ct_addrinfo
= res
;
164 * If regular IP name lookup failed, try NetBIOS,
165 * but only if given a valid NetBIOS name and if
166 * NetBIOS name lookup is enabled.
168 if (nbc
->nb_flags
& NBCF_NS_ENABLE
) {
169 gaierr2
= nbns_getaddrinfo(ctx
->ct_fullserver
, nbc
, &res
);
171 if (res
->ai_canonname
)
172 strlcpy(ctx
->ct_srvname
,
174 sizeof (ctx
->ct_srvname
));
175 ctx
->ct_addrinfo
= res
;
181 * Return the original error from getaddrinfo
184 smb_error(dgettext(TEXT_DOMAIN
,
185 "getaddrinfo: %s: %s"), 0,
187 gai_strerror(gaierr
));