2 Unix SMB/Netbios implementation.
4 NBT client - used to lookup netbios names
5 Copyright (C) Andrew Tridgell 1994-1998
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 extern int DEBUGLEVEL;
31 extern struct in_addr ipzero;
33 static BOOL use_bcast = True;
34 static BOOL got_bcast = False;
35 static struct in_addr bcast_addr;
36 static BOOL recursion_desired = False;
37 static BOOL translate_addresses = False;
38 static int ServerFD= -1;
39 static int RootPort = 0;
40 static BOOL find_status=False;
42 /****************************************************************************
43 open the socket communication
44 **************************************************************************/
45 static BOOL open_sockets(void)
47 ServerFD = open_socket_in( SOCK_DGRAM,
50 interpret_addr(lp_socket_address()), True );
55 set_socket_options(ServerFD,"SO_BROADCAST");
57 DEBUG(3, ("Socket opened.\n"));
62 /****************************************************************************
64 ****************************************************************************/
65 static void usage(void)
67 printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
68 printf("Version %s\n",VERSION);
69 printf("\t-d debuglevel set the debuglevel\n");
70 printf("\t-B broadcast address the address to use for broadcasts\n");
71 printf("\t-U unicast address the address to use for unicast\n");
72 printf("\t-M searches for a master browser\n");
73 printf("\t-R set recursion desired in packet\n");
74 printf("\t-S lookup node status as well\n");
75 printf("\t-T translate IP addresses into names\n");
76 printf("\t-r Use root port 137 (Win95 only replies to this)\n");
77 printf("\t-A Do a node status on <name> as an IP Address\n");
78 printf("\t-i NetBIOS scope Use the given NetBIOS scope for name queries\n");
79 printf("\t-s smb.conf file Use the given path to the smb.conf file\n");
80 printf("\t-h Print this help message.\n");
81 printf("\n If you specify -M and name is \"-\", nmblookup looks up __MSBROWSE__<01>\n");
86 /****************************************************************************
88 ****************************************************************************/
89 static BOOL query_one(char *lookup, unsigned int lookup_type)
92 struct in_addr *ip_list=NULL;
95 printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
96 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
97 use_bcast?True:recursion_desired,
98 bcast_addr,&count,NULL);
100 struct in_addr *bcast;
101 for (j=iface_count() - 1;
104 bcast = iface_n_bcast(j);
105 printf("querying %s on %s\n",
106 lookup, inet_ntoa(*bcast));
107 ip_list = name_query(ServerFD,lookup,lookup_type,
109 use_bcast?True:recursion_desired,
114 if (!ip_list) return False;
116 for (j=0;j<count;j++) {
117 if (translate_addresses) {
118 struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
120 printf("%s, ", host -> h_name);
123 printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
126 /* We can only do find_status if the ip address returned
127 was valid - ie. name_query returned true.
130 printf("Looking up status of %s\n",inet_ntoa(ip_list[0]));
131 name_status(ServerFD,lookup,lookup_type,True,ip_list[0],NULL,NULL,NULL);
135 return (ip_list != NULL);
139 /****************************************************************************
141 ****************************************************************************/
142 int main(int argc,char *argv[])
145 unsigned int lookup_type = 0x0;
149 BOOL find_master=False;
151 static pstring servicesf = CONFIGFILE;
152 BOOL lookup_by_ip = False;
153 int commandline_debuglevel = -2;
160 setup_logging(argv[0],True);
162 charset_initialise();
164 while ((opt = getopt(argc, argv, "d:B:U:i:s:SMrhART")) != EOF)
168 bcast_addr = *interpret_addr2(optarg);
173 bcast_addr = *interpret_addr2(optarg);
178 translate_addresses = !translate_addresses;
181 fstrcpy(scope,optarg);
191 recursion_desired = True;
194 commandline_debuglevel = DEBUGLEVEL = atoi(optarg);
197 pstrcpy(servicesf, optarg);
219 if (!lp_load(servicesf,True,False,False)) {
220 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
224 * Ensure we reset DEBUGLEVEL if someone specified it
225 * on the command line.
228 if(commandline_debuglevel != -2)
229 DEBUGLEVEL = commandline_debuglevel;
232 if (!open_sockets()) return(1);
234 for (i=optind;i<argc;i++)
239 fstrcpy(lookup,argv[i]);
244 ip = *interpret_addr2(argv[i]);
245 printf("Looking up status of %s\n",inet_ntoa(ip));
246 name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
252 if (*lookup == '-') {
253 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
260 p = strchr(lookup,'#');
264 sscanf(p+1,"%x",&lookup_type);
267 if (!query_one(lookup, lookup_type)) {
268 printf("name_query failed to find name %s\n", lookup);