2 Unix SMB/CIFS implementation.
3 NBT client - used to lookup netbios names
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern BOOL AllowDebugChange;
26 static BOOL give_flags = False;
27 static BOOL use_bcast = True;
28 static BOOL got_bcast = False;
29 static struct ipv4_addr bcast_addr;
30 static BOOL recursion_desired = False;
31 static BOOL translate_addresses = False;
32 static int ServerFD= -1;
33 static int RootPort = False;
34 static BOOL find_status=False;
36 /****************************************************************************
37 open the socket communication
38 **************************************************************************/
39 static BOOL open_sockets(void)
41 ServerFD = open_socket_in( SOCK_DGRAM,
44 interpret_addr(lp_socket_address()), True );
49 set_socket_options( ServerFD, "SO_BROADCAST" );
51 DEBUG(3, ("Socket opened.\n"));
56 /****************************************************************************
58 ****************************************************************************/
59 static void usage(void)
61 d_printf("Usage: nmblookup [options] name\n");
62 d_printf("Version %s\n",VERSION);
63 d_printf("\t-d debuglevel set the debuglevel\n");
64 d_printf("\t-B broadcast address the address to use for broadcasts\n");
65 d_printf("\t-f list the NMB flags returned\n");
66 d_printf("\t-U unicast address the address to use for unicast\n");
67 d_printf("\t-M searches for a master browser\n");
68 d_printf("\t-R set recursion desired in packet\n");
69 d_printf("\t-S lookup node status as well\n");
70 d_printf("\t-T translate IP addresses into names\n");
71 d_printf("\t-r Use root port 137 (Win95 only replies to this)\n");
72 d_printf("\t-A Do a node status on <name> as an IP Address\n");
73 d_printf("\t-i NetBIOS scope Use the given NetBIOS scope for name queries\n");
74 d_printf("\t-s smb.conf file Use the given path to the smb.conf file\n");
75 d_printf("\t-h Print this help message.\n");
76 d_printf("\n If you specify -M and name is \"-\", nmblookup looks up __MSBROWSE__<01>\n");
80 /****************************************************************************
81 turn a node status flags field into a string
82 ****************************************************************************/
83 static char *node_status_flags(uint8_t flags)
88 fstrcat(ret, (flags & 0x80) ? "<GROUP> " : " ");
89 if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
90 if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
91 if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
92 if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
93 if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
94 if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
95 if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
96 if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
101 /****************************************************************************
102 turn the NMB Query flags into a string
103 ****************************************************************************/
104 static char *query_flags(int flags)
109 if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
110 if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
111 if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
112 if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
113 if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
114 if (flags & NM_FLAGS_B) fstrcat(ret1, "Broadcast ");
119 /****************************************************************************
120 do a node status query
121 ****************************************************************************/
122 static void do_node_status(int fd, const char *name, int type, struct ipv4_addr ip)
124 struct nmb_name nname;
126 struct node_status *status;
129 d_printf("Looking up status of %s\n",inet_ntoa(ip));
130 make_nmb_name(&nname, name, type);
131 status = node_status_query(fd,&nname,ip, &count);
133 for (i=0;i<count;i++) {
134 fstrcpy(cleanname, status[i].name);
135 for (j=0;cleanname[j];j++) {
136 if (!isprint((int)cleanname[j])) cleanname[j] = '.';
138 d_printf("\t%-15s <%02x> - %s\n",
139 cleanname,status[i].type,
140 node_status_flags(status[i].flags));
148 /****************************************************************************
150 ****************************************************************************/
151 static BOOL query_one(const char *lookup, uint_t lookup_type)
153 int j, count, flags = 0;
154 struct ipv4_addr *ip_list=NULL;
157 d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
158 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
159 use_bcast?True:recursion_desired,
160 bcast_addr,&count, &flags, NULL);
162 struct ipv4_addr *bcast;
163 for (j=iface_count() - 1;
166 bcast = iface_n_bcast(j);
167 d_printf("querying %s on %s\n",
168 lookup, inet_ntoa(*bcast));
169 ip_list = name_query(ServerFD,lookup,lookup_type,
171 use_bcast?True:recursion_desired,
172 *bcast,&count, &flags, NULL);
176 if (!ip_list) return False;
179 d_printf("Flags: %s\n", query_flags(flags));
181 for (j=0;j<count;j++) {
182 if (translate_addresses) {
183 struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
185 d_printf("%s, ", host -> h_name);
188 d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
191 /* We can only do find_status if the ip address returned
192 was valid - ie. name_query returned true.
195 do_node_status(ServerFD, lookup, lookup_type, ip_list[0]);
200 return (ip_list != NULL);
204 /****************************************************************************
206 ****************************************************************************/
207 int main(int argc,char *argv[])
210 uint_t lookup_type = 0x0;
214 BOOL find_master=False;
216 BOOL lookup_by_ip = False;
217 int commandline_debuglevel = -2;
220 /* Prevent smb.conf setting from overridding */
221 AllowDebugChange = False;
225 setup_logging(argv[0], DEBUG_STDOUT);
227 while ((opt = getopt(argc, argv, "d:fB:U:i:s:SMrhART")) != EOF)
231 bcast_addr = interpret_addr2(optarg);
239 bcast_addr = interpret_addr2(optarg);
244 translate_addresses = !translate_addresses;
247 lp_set_cmdline("netbios scope", optarg);
256 recursion_desired = True;
259 commandline_debuglevel = DEBUGLEVEL = atoi(optarg);
262 pstrcpy(dyn_CONFIGFILE, optarg);
284 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
285 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
289 * Ensure we reset DEBUGLEVEL if someone specified it
290 * on the command line.
293 if(commandline_debuglevel != -2)
294 DEBUGLEVEL = commandline_debuglevel;
297 if (!open_sockets()) return(1);
299 for (i=optind;i<argc;i++)
304 fstrcpy(lookup,argv[i]);
309 ip = interpret_addr2(argv[i]);
310 do_node_status(ServerFD, lookup, lookup_type, ip);
315 if (*lookup == '-') {
316 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
323 p = strchr_m(lookup,'#');
326 sscanf(++p,"%x",&lookup_type);
329 if (!query_one(lookup, lookup_type)) {
330 d_printf( "name_query failed to find name %s", lookup );
331 if( 0 != lookup_type )
332 d_printf( "#%02x", lookup_type );