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 pstring myhostname;
32 extern struct in_addr ipzero;
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()) );
51 set_socket_options(ServerFD,"SO_BROADCAST");
53 DEBUG(3, ("Socket opened.\n"));
58 /****************************************************************************
59 initialise connect, service and file structs
60 ****************************************************************************/
61 static BOOL init_structs(void )
63 if (!get_myname(myhostname,NULL))
69 /****************************************************************************
71 ****************************************************************************/
72 static void usage(void)
74 printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
75 printf("Version %s\n",VERSION);
76 printf("\t-d debuglevel set the debuglevel\n");
77 printf("\t-B broadcast address the address to use for broadcasts\n");
78 printf("\t-U unicast address the address to use for unicast\n");
79 printf("\t-M searches for a master browser\n");
80 printf("\t-R set recursion desired in packet\n");
81 printf("\t-S lookup node status as well\n");
82 printf("\t-r Use root port 137 (Win95 only replies to this)\n");
83 printf("\t-A Do a node status on <name> as an IP Address\n");
84 printf("\t-i NetBIOS scope Use the given NetBIOS scope for name queries\n");
85 printf("\t-s smb.conf file Use the given path to the smb.conf file\n");
86 printf("\t-h Print this help message.\n");
91 /****************************************************************************
93 ****************************************************************************/
94 int main(int argc,char *argv[])
97 unsigned int lookup_type = 0x0;
101 BOOL find_master=False;
102 BOOL find_status=False;
104 static pstring servicesf = CONFIGFILE;
105 struct in_addr bcast_addr;
106 BOOL use_bcast = True;
107 BOOL got_bcast = False;
108 BOOL lookup_by_ip = False;
109 BOOL recursion_desired = False;
116 setup_logging(argv[0],True);
118 charset_initialise();
120 while ((opt = getopt(argc, argv, "d:B:U:i:s:SMrhAR")) != EOF)
124 iface_set_default(NULL,optarg,NULL);
125 bcast_addr = *interpret_addr2(optarg);
130 iface_set_default(NULL,optarg,NULL);
131 bcast_addr = *interpret_addr2(optarg);
136 fstrcpy(scope,optarg);
146 recursion_desired = True;
149 DEBUGLEVEL = atoi(optarg);
152 pstrcpy(servicesf, optarg);
176 if (!lp_load(servicesf,True,False,False)) {
177 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
181 if (!open_sockets()) return(1);
184 bcast_addr = *iface_bcast(ipzero);
186 DEBUG(1,("Sending queries to %s\n",inet_ntoa(bcast_addr)));
189 for (i=optind;i<argc;i++)
194 struct in_addr *ip_list;
196 fstrcpy(lookup,argv[i]);
201 ip = *interpret_addr2(argv[i]);
202 printf("Looking up status of %s\n",inet_ntoa(ip));
203 name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
209 if (*lookup == '-') {
210 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
217 p = strchr(lookup,'#');
221 sscanf(p+1,"%x",&lookup_type);
224 if ((ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
225 use_bcast?True:recursion_desired,
226 bcast_addr,&count,NULL))) {
227 for (j=0;j<count;j++)
228 printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
230 /* We can only do find_status if the ip address returned
231 was valid - ie. name_query returned true.
234 printf("Looking up status of %s\n",inet_ntoa(ip_list[0]));
235 name_status(ServerFD,lookup,lookup_type,True,ip_list[0],NULL,NULL,NULL);
239 printf("name_query failed to find name %s\n", lookup);