2 Unix SMB/CIFS implementation.
3 NBT client - used to lookup netbios names
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jelmer Vernooij 2003 (Conversion to popt)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "popt_common.h"
24 #include "libsmb/nmblib.h"
25 #include "libsmb/namequery.h"
27 static bool give_flags = false;
28 static bool use_bcast = true;
29 static bool got_bcast = false;
30 static struct sockaddr_storage bcast_addr;
31 static bool recursion_desired = false;
32 static bool translate_addresses = false;
33 static int ServerFD= -1;
34 static bool RootPort = false;
35 static bool find_status = false;
37 /****************************************************************************
38 Open the socket communication.
39 **************************************************************************/
41 static bool open_sockets(void)
43 struct sockaddr_storage ss;
44 const char *sock_addr = lp_nbt_client_socket_address();
46 if (!interpret_string_addr(&ss, sock_addr,
47 AI_NUMERICHOST|AI_PASSIVE)) {
48 DEBUG(0,("open_sockets: unable to get socket address "
49 "from string %s", sock_addr));
52 ServerFD = open_socket_in( SOCK_DGRAM,
61 set_socket_options( ServerFD, "SO_BROADCAST" );
63 DEBUG(3, ("Socket opened.\n"));
67 /****************************************************************************
68 turn a node status flags field into a string
69 ****************************************************************************/
70 static char *node_status_flags(unsigned char flags)
75 fstrcat(ret, (flags & 0x80) ? "<GROUP> " : " ");
76 if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
77 if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
78 if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
79 if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
80 if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
81 if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
82 if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
83 if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
88 /****************************************************************************
89 Turn the NMB Query flags into a string.
90 ****************************************************************************/
92 static char *query_flags(int flags)
97 if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
98 if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
99 if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
100 if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
101 if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
102 if (flags & NM_FLAGS_B) fstrcat(ret1, "Broadcast ");
107 /****************************************************************************
108 Do a node status query.
109 ****************************************************************************/
111 static bool do_node_status(const char *name,
113 struct sockaddr_storage *pss)
115 struct nmb_name nname;
117 struct node_status *addrs;
118 struct node_status_extra extra;
120 char addr[INET6_ADDRSTRLEN];
123 print_sockaddr(addr, sizeof(addr), pss);
124 d_printf("Looking up status of %s\n",addr);
125 make_nmb_name(&nname, name, type);
126 status = node_status_query(talloc_tos(), &nname, pss,
127 &addrs, &count, &extra);
128 if (NT_STATUS_IS_OK(status)) {
129 for (i=0;i<count;i++) {
130 pull_ascii_fstring(cleanname, addrs[i].name);
131 for (j=0;cleanname[j];j++) {
132 if (!isprint((int)cleanname[j])) {
136 d_printf("\t%-15s <%02x> - %s\n",
137 cleanname,addrs[i].type,
138 node_status_flags(addrs[i].flags));
140 d_printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
141 extra.mac_addr[0], extra.mac_addr[1],
142 extra.mac_addr[2], extra.mac_addr[3],
143 extra.mac_addr[4], extra.mac_addr[5]);
148 d_printf("No reply from %s\n\n",addr);
154 /****************************************************************************
156 ****************************************************************************/
158 static bool query_one(const char *lookup, unsigned int lookup_type)
162 struct sockaddr_storage *ip_list=NULL;
163 NTSTATUS status = NT_STATUS_NOT_FOUND;
166 char addr[INET6_ADDRSTRLEN];
167 print_sockaddr(addr, sizeof(addr), &bcast_addr);
168 d_printf("querying %s on %s\n", lookup, addr);
169 status = name_query(lookup,lookup_type,use_bcast,
170 use_bcast?true:recursion_desired,
171 &bcast_addr, talloc_tos(),
172 &ip_list, &count, &flags);
174 status = name_resolve_bcast(
176 talloc_tos(), &ip_list, &count);
179 if (!NT_STATUS_IS_OK(status)) {
184 d_printf("Flags: %s\n", query_flags(flags));
187 for (j=0;j<count;j++) {
188 char addr[INET6_ADDRSTRLEN];
189 if (translate_addresses) {
190 char h_name[MAX_DNS_NAME_LENGTH];
192 if (sys_getnameinfo((const struct sockaddr *)&ip_list[j],
193 sizeof(struct sockaddr_storage),
194 h_name, sizeof(h_name),
199 d_printf("%s, ", h_name);
201 print_sockaddr(addr, sizeof(addr), &ip_list[j]);
202 d_printf("%s %s<%02x>\n", addr,lookup, lookup_type);
203 /* We can only do find_status if the ip address returned
204 was valid - ie. name_query returned true.
207 if (!do_node_status(lookup, lookup_type, &ip_list[j])) {
208 status = NT_STATUS_UNSUCCESSFUL;
213 TALLOC_FREE(ip_list);
215 return NT_STATUS_IS_OK(status);
219 /****************************************************************************
221 ****************************************************************************/
222 int main(int argc, const char *argv[])
225 unsigned int lookup_type = 0x0;
227 static bool find_master=False;
228 static bool lookup_by_ip = False;
229 poptContext pc = NULL;
230 TALLOC_CTX *frame = talloc_stackframe();
233 struct poptOption long_options[] = {
236 .longName = "broadcast",
238 .argInfo = POPT_ARG_STRING,
241 .descrip = "Specify address to use for broadcasts",
242 .argDescrip = "BROADCAST-ADDRESS",
247 .argInfo = POPT_ARG_NONE,
250 .descrip = "List the NMB flags returned",
253 .longName = "unicast",
255 .argInfo = POPT_ARG_STRING,
258 .descrip = "Specify address to use for unicast",
261 .longName = "master-browser",
263 .argInfo = POPT_ARG_NONE,
266 .descrip = "Search for a master browser",
269 .longName = "recursion",
271 .argInfo = POPT_ARG_NONE,
274 .descrip = "Set recursion desired in package",
277 .longName = "status",
279 .argInfo = POPT_ARG_NONE,
282 .descrip = "Lookup node status as well",
285 .longName = "translate",
287 .argInfo = POPT_ARG_NONE,
290 .descrip = "Translate IP addresses into names",
293 .longName = "root-port",
295 .argInfo = POPT_ARG_NONE,
298 .descrip = "Use root port 137 (Win95 only replies to this)",
301 .longName = "lookup-by-ip",
303 .argInfo = POPT_ARG_NONE,
306 .descrip = "Do a node status on <name> as an IP Address",
309 POPT_COMMON_CONNECTION
317 setup_logging(argv[0], DEBUG_STDOUT);
319 pc = poptGetContext("nmblookup", argc, argv,
320 long_options, POPT_CONTEXT_KEEP_FIRST);
322 poptSetOtherOptionHelp(pc, "<NODE> ...");
324 while ((opt = poptGetNextOpt(pc)) != -1) {
333 recursion_desired = true;
345 if (interpret_string_addr(&bcast_addr,
353 if (interpret_string_addr(&bcast_addr,
361 translate_addresses = !translate_addresses;
366 poptGetArg(pc); /* Remove argv[0] */
368 if(!poptPeekArg(pc)) {
369 poptPrintUsage(pc, stderr, 0);
374 if (!lp_load_global(get_dyn_CONFIGFILE())) {
375 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
376 get_dyn_CONFIGFILE());
380 if (!open_sockets()) {
385 while(poptPeekArg(pc)) {
390 fstrcpy(lookup,poptGetArg(pc));
393 struct sockaddr_storage ss;
394 ip = interpret_addr2(lookup);
395 in_addr_to_sockaddr_storage(&ss, ip);
397 if (!do_node_status(lookup, lookup_type, &ss)) {
404 if (*lookup == '-') {
405 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
412 p = strchr_m(lookup,'#');
415 sscanf(++p,"%x",&lookup_type);
418 nbt_len = strlen(lookup);
419 if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
420 d_printf("The specified netbios name [%s] is too long!\n",
426 if (!query_one(lookup, lookup_type)) {
428 d_printf( "name_query failed to find name %s", lookup );
429 if( 0 != lookup_type ) {
430 d_printf( "#%02x", lookup_type );