dnscrypto-proxy: Support files updated.
[tomato.git] / release / src / router / samba / source / utils / nmblookup.c
blob5d86d4cdae18516334139283eb922a829ffc723a
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
23 #define NO_SYSLOG
25 #include "includes.h"
27 extern int DEBUGLEVEL;
29 extern struct in_addr ipzero;
31 static BOOL use_bcast = True;
32 static BOOL got_bcast = False;
33 static struct in_addr bcast_addr;
34 static BOOL recursion_desired = False;
35 static BOOL translate_addresses = False;
36 static int ServerFD= -1;
37 static int RootPort = 0;
38 static BOOL find_status=False;
40 /****************************************************************************
41 open the socket communication
42 **************************************************************************/
43 static BOOL open_sockets(void)
45 ServerFD = open_socket_in( SOCK_DGRAM,
46 (RootPort ? 137 :0),
48 interpret_addr(lp_socket_address()), True );
50 if (ServerFD == -1)
51 return(False);
53 set_socket_options(ServerFD,"SO_BROADCAST");
55 DEBUG(3, ("Socket opened.\n"));
56 return True;
60 /****************************************************************************
61 usage on the program
62 ****************************************************************************/
63 static void usage(void)
65 printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
66 printf("Version %s\n",VERSION);
67 printf("\t-d debuglevel set the debuglevel\n");
68 printf("\t-B broadcast address the address to use for broadcasts\n");
69 printf("\t-U unicast address the address to use for unicast\n");
70 printf("\t-M searches for a master browser\n");
71 printf("\t-R set recursion desired in packet\n");
72 printf("\t-S lookup node status as well\n");
73 printf("\t-T translate IP addresses into names\n");
74 printf("\t-r Use root port 137 (Win95 only replies to this)\n");
75 printf("\t-A Do a node status on <name> as an IP Address\n");
76 printf("\t-i NetBIOS scope Use the given NetBIOS scope for name queries\n");
77 printf("\t-s smb.conf file Use the given path to the smb.conf file\n");
78 printf("\t-h Print this help message.\n");
79 printf("\n If you specify -M and name is \"-\", nmblookup looks up __MSBROWSE__<01>\n");
80 printf("\n");
84 /****************************************************************************
85 send out one query
86 ****************************************************************************/
87 static BOOL query_one(char *lookup, unsigned int lookup_type)
89 int j, count;
90 struct in_addr *ip_list=NULL;
92 if (got_bcast) {
93 printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
94 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
95 use_bcast?True:recursion_desired,
96 bcast_addr,&count,NULL);
97 } else {
98 struct in_addr *bcast;
99 for (j=iface_count() - 1;
100 !ip_list && j >= 0;
101 j--) {
102 bcast = iface_n_bcast(j);
103 printf("querying %s on %s\n",
104 lookup, inet_ntoa(*bcast));
105 ip_list = name_query(ServerFD,lookup,lookup_type,
106 use_bcast,
107 use_bcast?True:recursion_desired,
108 *bcast,&count,NULL);
112 if (!ip_list) return False;
114 for (j=0;j<count;j++) {
115 if (translate_addresses) {
116 struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
117 if (host) {
118 printf("%s, ", host -> h_name);
121 printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
124 /* We can only do find_status if the ip address returned
125 was valid - ie. name_query returned true.
127 if (find_status) {
128 printf("Looking up status of %s\n",inet_ntoa(ip_list[0]));
129 name_status(ServerFD,lookup,lookup_type,True,ip_list[0],NULL,NULL,NULL);
130 printf("\n");
133 if (ip_list) free(ip_list);
135 return (ip_list != NULL);
139 /****************************************************************************
140 main program
141 ****************************************************************************/
142 int main(int argc,char *argv[])
144 int opt;
145 unsigned int lookup_type = 0x0;
146 pstring lookup;
147 extern int optind;
148 extern char *optarg;
149 BOOL find_master=False;
150 int i;
151 static pstring servicesf = CONFIGFILE;
152 BOOL lookup_by_ip = False;
153 int commandline_debuglevel = -2;
155 DEBUGLEVEL = 1;
156 *lookup = 0;
158 TimeInit();
160 setup_logging(argv[0],True);
162 charset_initialise();
164 while ((opt = getopt(argc, argv, "d:B:U:i:s:SMrhART")) != EOF)
165 switch (opt)
167 case 'B':
168 bcast_addr = *interpret_addr2(optarg);
169 got_bcast = True;
170 use_bcast = True;
171 break;
172 case 'U':
173 bcast_addr = *interpret_addr2(optarg);
174 got_bcast = True;
175 use_bcast = False;
176 break;
177 case 'T':
178 translate_addresses = !translate_addresses;
179 break;
180 case 'i':
182 extern pstring global_scope;
183 pstrcpy(global_scope,optarg);
184 strupper(global_scope);
186 break;
187 case 'M':
188 find_master = True;
189 break;
190 case 'S':
191 find_status = True;
192 break;
193 case 'R':
194 recursion_desired = True;
195 break;
196 case 'd':
197 commandline_debuglevel = DEBUGLEVEL = atoi(optarg);
198 break;
199 case 's':
200 pstrcpy(servicesf, optarg);
201 break;
202 case 'r':
203 RootPort = -1;
204 break;
205 case 'h':
206 usage();
207 exit(0);
208 break;
209 case 'A':
210 lookup_by_ip = True;
211 break;
212 default:
213 usage();
214 exit(1);
217 if (argc < 2) {
218 usage();
219 exit(1);
222 if (!lp_load(servicesf,True,False,False)) {
223 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
227 * Ensure we reset DEBUGLEVEL if someone specified it
228 * on the command line.
231 if(commandline_debuglevel != -2)
232 DEBUGLEVEL = commandline_debuglevel;
234 load_interfaces();
235 if (!open_sockets()) return(1);
237 for (i=optind;i<argc;i++)
239 char *p;
240 struct in_addr ip;
242 fstrcpy(lookup,argv[i]);
244 if(lookup_by_ip)
246 fstrcpy(lookup,"*");
247 ip = *interpret_addr2(argv[i]);
248 printf("Looking up status of %s\n",inet_ntoa(ip));
249 name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
250 printf("\n");
251 continue;
254 if (find_master) {
255 if (*lookup == '-') {
256 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
257 lookup_type = 1;
258 } else {
259 lookup_type = 0x1d;
263 p = strchr(lookup,'#');
265 if (p) {
266 *p = 0;
267 sscanf(p+1,"%x",&lookup_type);
270 if (!query_one(lookup, lookup_type)) {
271 printf("name_query failed to find name %s\n", lookup);
275 return(0);