2 Unix SMB/CIFS implementation.
4 NBT client - used to lookup netbios names
6 Copyright (C) Andrew Tridgell 1994-2005
7 Copyright (C) Jelmer Vernooij 2003 (Conversion to popt)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "dynconfig.h"
27 #include "libcli/nbt/libnbt.h"
28 #include "lib/cmdline/popt_common.h"
29 #include "system/iconv.h"
31 /* command line options */
33 const char *broadcast_address;
34 const char *unicast_address;
43 clean any binary from a node name
45 static const char *clean_name(TALLOC_CTX *mem_ctx, const char *name)
47 char *ret = talloc_strdup(mem_ctx, name);
49 for (i=0;ret[i];i++) {
50 if (!isprint(ret[i])) ret[i] = '.';
56 turn a node status flags field into a string
58 static char *node_status_flags(TALLOC_CTX *mem_ctx, uint16_t flags)
61 const char *group = " ";
62 const char *type = "B";
64 if (flags & NBT_NM_GROUP) {
68 switch (flags & NBT_NM_OWNER_TYPE) {
83 ret = talloc_asprintf(mem_ctx, "%s %s", group, type);
85 if (flags & NBT_NM_DEREGISTER) {
86 ret = talloc_asprintf_append(ret, " <DEREGISTERING>");
88 if (flags & NBT_NM_CONFLICT) {
89 ret = talloc_asprintf_append(ret, " <CONFLICT>");
91 if (flags & NBT_NM_ACTIVE) {
92 ret = talloc_asprintf_append(ret, " <ACTIVE>");
94 if (flags & NBT_NM_PERMANENT) {
95 ret = talloc_asprintf_append(ret, " <PERMANENT>");
101 /* do a single node status */
102 static void do_node_status(struct nbt_name_socket *nbtsock,
105 struct nbt_name_status io;
108 io.in.name.name = "*";
110 io.in.name.scope = NULL;
111 io.in.dest_addr = addr;
114 status = nbt_name_status(nbtsock, nbtsock, &io);
115 if (NT_STATUS_IS_OK(status)) {
117 printf("Node status reply from %s\n",
119 for (i=0;i<io.out.status.num_names;i++) {
120 d_printf("\t%-16s <%02x> %s\n",
121 clean_name(nbtsock, io.out.status.names[i].name),
122 io.out.status.names[i].type,
123 node_status_flags(nbtsock, io.out.status.names[i].nb_flags));
125 printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
126 io.out.status.statistics.unit_id[0],
127 io.out.status.statistics.unit_id[1],
128 io.out.status.statistics.unit_id[2],
129 io.out.status.statistics.unit_id[3],
130 io.out.status.statistics.unit_id[4],
131 io.out.status.statistics.unit_id[5]);
135 /* do a single node query */
136 static NTSTATUS do_node_query(struct nbt_name_socket *nbtsock,
138 const char *node_name,
139 enum nbt_name_type node_type,
142 struct nbt_name_query io;
146 io.in.name.name = node_name;
147 io.in.name.type = node_type;
148 io.in.name.scope = NULL;
149 io.in.dest_addr = addr;
150 io.in.broadcast = broadcast;
151 io.in.wins_lookup = options.wins_lookup;
154 status = nbt_name_query(nbtsock, nbtsock, &io);
155 NT_STATUS_NOT_OK_RETURN(status);
157 for (i=0;i<io.out.num_addrs;i++) {
158 printf("%s %s<%02x>\n",
159 io.out.reply_addrs[i],
163 if (options.node_status && io.out.num_addrs > 0) {
164 do_node_status(nbtsock, io.out.reply_addrs[0]);
171 static void process_one(const char *name)
173 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
174 enum nbt_name_type node_type = NBT_NAME_CLIENT;
176 struct nbt_name_socket *nbtsock;
178 if (options.find_master) {
179 node_type = NBT_NAME_MASTER;
181 name = "\01\02__MSBROWSE__\02";
182 node_type = NBT_NAME_MS;
186 p = strchr(name, '#');
188 node_name = talloc_strndup(tmp_ctx, name, PTR_DIFF(p,name));
189 node_type = (enum nbt_name_type)strtol(p+1, NULL, 16);
191 node_name = talloc_strdup(tmp_ctx, name);
194 nbtsock = nbt_name_socket_init(tmp_ctx, NULL);
196 if (options.root_port) {
198 status = socket_listen(nbtsock->sock, "0.0.0.0", NBT_NAME_SERVICE_PORT, 0, 0);
199 if (!NT_STATUS_IS_OK(status)) {
200 printf("Failed to bind to local port 137 - %s\n", nt_errstr(status));
205 if (options.lookup_by_ip) {
206 do_node_status(nbtsock, name);
207 talloc_free(tmp_ctx);
211 if (options.broadcast_address) {
212 do_node_query(nbtsock, options.broadcast_address, node_name, node_type, True);
213 } else if (options.unicast_address) {
214 do_node_query(nbtsock, options.unicast_address, node_name, node_type, False);
216 int i, num_interfaces = iface_count();
217 for (i=0;i<num_interfaces;i++) {
218 const char *bcast = sys_inet_ntoa(*iface_n_bcast(i));
219 NTSTATUS status = do_node_query(nbtsock, bcast, node_name, node_type, True);
220 if (NT_STATUS_IS_OK(status)) break;
224 talloc_free(tmp_ctx);
230 int main(int argc,char *argv[])
233 struct poptOption long_options[] = {
235 { "broadcast", 'B', POPT_ARG_STRING, &options.broadcast_address,
236 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
238 { "unicast", 'U', POPT_ARG_STRING, &options.unicast_address,
239 'U', "Specify address to use for unicast" },
241 { "master-browser", 'M', POPT_ARG_VAL, &options.find_master,
242 True, "Search for a master browser" },
244 { "wins", 'W', POPT_ARG_VAL, &options.wins_lookup, True, "Do a WINS lookup" },
246 { "status", 'S', POPT_ARG_VAL, &options.node_status,
247 True, "Lookup node status as well" },
249 { "root-port", 'r', POPT_ARG_VAL, &options.root_port,
250 True, "Use root port 137 (Win95 only replies to this)" },
252 { "lookup-by-ip", 'A', POPT_ARG_VAL, &options.lookup_by_ip,
253 True, "Do a node status on <name> as an IP Address" },
259 setup_logging(argv[0], True);
261 pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options,
262 POPT_CONTEXT_KEEP_FIRST);
264 poptSetOtherOptionHelp(pc, "<NODE> ...");
266 while ((poptGetNextOpt(pc) != -1)) /* noop */ ;
268 /* swallow argv[0] */
271 if(!poptPeekArg(pc)) {
272 poptPrintUsage(pc, stderr, 0);
276 lp_load(dyn_CONFIGFILE,True,False,False);
279 while (poptPeekArg(pc)) {
280 const char *name = poptGetArg(pc);