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 use_bcast = True;
29 static BOOL got_bcast = False;
30 static struct in_addr bcast_addr;
31 static BOOL recursion_desired = False;
32 static BOOL translate_addresses = False;
33 static int ServerFD= -1;
34 static int RootPort = False;
35 static BOOL find_status=False;
37 /****************************************************************************
38 open the socket communication
39 **************************************************************************/
40 static BOOL open_sockets(void)
42 ServerFD = open_socket_in( SOCK_DGRAM,
45 interpret_addr(lp_socket_address()), True );
50 set_socket_options( ServerFD, "SO_BROADCAST" );
52 DEBUG(3, ("Socket opened.\n"));
57 /****************************************************************************
59 ****************************************************************************/
60 static void usage(void)
62 d_printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
63 d_printf("Version %s\n",VERSION);
64 d_printf("\t-d debuglevel set the debuglevel\n");
65 d_printf("\t-B broadcast address the address to use for broadcasts\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(unsigned char 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 do a node status query
103 ****************************************************************************/
104 static void do_node_status(int fd, char *name, int type, struct in_addr ip)
106 struct nmb_name nname;
108 struct node_status *status;
111 d_printf("Looking up status of %s\n",inet_ntoa(ip));
112 make_nmb_name(&nname, name, type);
113 status = node_status_query(fd,&nname,ip, &count);
115 for (i=0;i<count;i++) {
116 fstrcpy(cleanname, status[i].name);
117 for (j=0;cleanname[j];j++) {
118 if (!isprint((int)cleanname[j])) cleanname[j] = '.';
120 d_printf("\t%-15s <%02x> - %s\n",
121 cleanname,status[i].type,
122 node_status_flags(status[i].flags));
130 /****************************************************************************
132 ****************************************************************************/
133 static BOOL query_one(char *lookup, unsigned int lookup_type)
136 struct in_addr *ip_list=NULL;
139 d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
140 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
141 use_bcast?True:recursion_desired,
144 struct in_addr *bcast;
145 for (j=iface_count() - 1;
148 bcast = iface_n_bcast(j);
149 d_printf("querying %s on %s\n",
150 lookup, inet_ntoa(*bcast));
151 ip_list = name_query(ServerFD,lookup,lookup_type,
153 use_bcast?True:recursion_desired,
158 if (!ip_list) return False;
160 for (j=0;j<count;j++) {
161 if (translate_addresses) {
162 struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
164 d_printf("%s, ", host -> h_name);
167 d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
170 /* We can only do find_status if the ip address returned
171 was valid - ie. name_query returned true.
174 do_node_status(ServerFD, lookup, lookup_type, ip_list[0]);
179 return (ip_list != NULL);
183 /****************************************************************************
185 ****************************************************************************/
186 int main(int argc,char *argv[])
189 unsigned int lookup_type = 0x0;
193 BOOL find_master=False;
195 BOOL lookup_by_ip = False;
196 int commandline_debuglevel = -2;
199 /* Prevent smb.conf setting from overridding */
200 AllowDebugChange = False;
204 setup_logging(argv[0],True);
206 while ((opt = getopt(argc, argv, "d:B:U:i:s:SMrhART")) != EOF)
210 bcast_addr = *interpret_addr2(optarg);
215 bcast_addr = *interpret_addr2(optarg);
220 translate_addresses = !translate_addresses;
224 extern pstring global_scope;
225 pstrcpy(global_scope,optarg);
226 strupper(global_scope);
236 recursion_desired = True;
239 commandline_debuglevel = DEBUGLEVEL = atoi(optarg);
242 pstrcpy(dyn_CONFIGFILE, optarg);
264 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
265 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
269 * Ensure we reset DEBUGLEVEL if someone specified it
270 * on the command line.
273 if(commandline_debuglevel != -2)
274 DEBUGLEVEL = commandline_debuglevel;
277 if (!open_sockets()) return(1);
279 for (i=optind;i<argc;i++)
284 fstrcpy(lookup,argv[i]);
289 ip = *interpret_addr2(argv[i]);
290 do_node_status(ServerFD, lookup, lookup_type, ip);
295 if (*lookup == '-') {
296 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
303 p = strchr_m(lookup,'#');
306 sscanf(++p,"%x",&lookup_type);
309 if (!query_one(lookup, lookup_type)) {
310 d_printf( "name_query failed to find name %s", lookup );
311 if( 0 != lookup_type )
312 d_printf( "#%02x", lookup_type );