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.
26 extern BOOL AllowDebugChange;
28 static BOOL give_flags = False;
29 static BOOL use_bcast = True;
30 static BOOL got_bcast = False;
31 static struct in_addr bcast_addr;
32 static BOOL recursion_desired = False;
33 static BOOL translate_addresses = False;
34 static int ServerFD= -1;
35 static int RootPort = False;
36 static BOOL find_status=False;
38 /****************************************************************************
39 open the socket communication
40 **************************************************************************/
41 static BOOL open_sockets(void)
43 ServerFD = open_socket_in( SOCK_DGRAM,
46 interpret_addr(lp_socket_address()), True );
51 set_socket_options( ServerFD, "SO_BROADCAST" );
53 DEBUG(3, ("Socket opened.\n"));
58 /****************************************************************************
60 ****************************************************************************/
61 static void usage(void)
63 d_printf("Usage: nmblookup [options] name\n");
64 d_printf("Version %s\n",VERSION);
65 d_printf("\t-d debuglevel set the debuglevel\n");
66 d_printf("\t-B broadcast address the address to use for broadcasts\n");
67 d_printf("\t-f list the NMB flags returned\n");
68 d_printf("\t-U unicast address the address to use for unicast\n");
69 d_printf("\t-M searches for a master browser\n");
70 d_printf("\t-R set recursion desired in packet\n");
71 d_printf("\t-S lookup node status as well\n");
72 d_printf("\t-T translate IP addresses into names\n");
73 d_printf("\t-r Use root port 137 (Win95 only replies to this)\n");
74 d_printf("\t-A Do a node status on <name> as an IP Address\n");
75 d_printf("\t-i NetBIOS scope Use the given NetBIOS scope for name queries\n");
76 d_printf("\t-s smb.conf file Use the given path to the smb.conf file\n");
77 d_printf("\t-h Print this help message.\n");
78 d_printf("\n If you specify -M and name is \"-\", nmblookup looks up __MSBROWSE__<01>\n");
82 /****************************************************************************
83 turn a node status flags field into a string
84 ****************************************************************************/
85 static char *node_status_flags(unsigned char flags)
90 fstrcat(ret, (flags & 0x80) ? "<GROUP> " : " ");
91 if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
92 if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
93 if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
94 if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
95 if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
96 if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
97 if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
98 if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
103 /****************************************************************************
104 turn the NMB Query flags into a string
105 ****************************************************************************/
106 static char *query_flags(int flags)
111 if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
112 if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
113 if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
114 if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
115 if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
116 if (flags & NM_FLAGS_B) fstrcat(ret1, "Broadcast ");
121 /****************************************************************************
122 do a node status query
123 ****************************************************************************/
124 static void do_node_status(int fd, char *name, int type, struct in_addr ip)
126 struct nmb_name nname;
128 struct node_status *status;
131 d_printf("Looking up status of %s\n",inet_ntoa(ip));
132 make_nmb_name(&nname, name, type);
133 status = node_status_query(fd,&nname,ip, &count);
135 for (i=0;i<count;i++) {
136 fstrcpy(cleanname, status[i].name);
137 for (j=0;cleanname[j];j++) {
138 if (!isprint((int)cleanname[j])) cleanname[j] = '.';
140 d_printf("\t%-15s <%02x> - %s\n",
141 cleanname,status[i].type,
142 node_status_flags(status[i].flags));
150 /****************************************************************************
152 ****************************************************************************/
153 static BOOL query_one(char *lookup, unsigned int lookup_type)
155 int j, count, flags = 0;
156 struct in_addr *ip_list=NULL;
159 d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
160 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
161 use_bcast?True:recursion_desired,
162 bcast_addr,&count, &flags, NULL);
164 struct in_addr *bcast;
165 for (j=iface_count() - 1;
168 bcast = iface_n_bcast(j);
169 d_printf("querying %s on %s\n",
170 lookup, inet_ntoa(*bcast));
171 ip_list = name_query(ServerFD,lookup,lookup_type,
173 use_bcast?True:recursion_desired,
174 *bcast,&count, &flags, NULL);
178 if (!ip_list) return False;
181 d_printf("Flags: %s\n", query_flags(flags));
183 for (j=0;j<count;j++) {
184 if (translate_addresses) {
185 struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
187 d_printf("%s, ", host -> h_name);
190 d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
193 /* We can only do find_status if the ip address returned
194 was valid - ie. name_query returned true.
197 do_node_status(ServerFD, lookup, lookup_type, ip_list[0]);
202 return (ip_list != NULL);
206 /****************************************************************************
208 ****************************************************************************/
209 int main(int argc,char *argv[])
212 unsigned int lookup_type = 0x0;
216 BOOL find_master=False;
218 BOOL lookup_by_ip = False;
219 int commandline_debuglevel = -2;
222 /* Prevent smb.conf setting from overridding */
223 AllowDebugChange = False;
227 setup_logging(argv[0],True);
229 while ((opt = getopt(argc, argv, "d:fB:U:i:s:SMrhART")) != EOF)
233 bcast_addr = *interpret_addr2(optarg);
241 bcast_addr = *interpret_addr2(optarg);
246 translate_addresses = !translate_addresses;
249 set_global_scope(optarg);
258 recursion_desired = True;
261 commandline_debuglevel = DEBUGLEVEL = atoi(optarg);
264 pstrcpy(dyn_CONFIGFILE, optarg);
286 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
287 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
291 * Ensure we reset DEBUGLEVEL if someone specified it
292 * on the command line.
295 if(commandline_debuglevel != -2)
296 DEBUGLEVEL = commandline_debuglevel;
299 if (!open_sockets()) return(1);
301 for (i=optind;i<argc;i++)
306 fstrcpy(lookup,argv[i]);
311 ip = *interpret_addr2(argv[i]);
312 do_node_status(ServerFD, lookup, lookup_type, ip);
317 if (*lookup == '-') {
318 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
325 p = strchr_m(lookup,'#');
328 sscanf(++p,"%x",&lookup_type);
331 if (!query_one(lookup, lookup_type)) {
332 d_printf( "name_query failed to find name %s", lookup );
333 if( 0 != lookup_type )
334 d_printf( "#%02x", lookup_type );