Minor comment updates ...
[Samba/gebeck_regimport.git] / source3 / utils / nmblookup.c
blob3c5a22841eacda18f5bbc2e4dd6118d34d38d108
1 /*
2 Unix SMB/CIFS implementation.
3 NBT client - used to lookup netbios names
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jelmer Vernooij 2003 (Conversion to popt)
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 BOOL AllowDebugChange;
29 static BOOL give_flags = False;
30 static BOOL use_bcast = True;
31 static BOOL got_bcast = False;
32 static struct in_addr bcast_addr;
33 static BOOL recursion_desired = False;
34 static BOOL translate_addresses = False;
35 static int ServerFD= -1;
36 static int RootPort = False;
37 static BOOL find_status=False;
39 /****************************************************************************
40 open the socket communication
41 **************************************************************************/
42 static BOOL open_sockets(void)
44 ServerFD = open_socket_in( SOCK_DGRAM,
45 (RootPort ? 137 : 0),
46 (RootPort ? 0 : 3),
47 interpret_addr(lp_socket_address()), True );
49 if (ServerFD == -1)
50 return(False);
52 set_socket_options( ServerFD, "SO_BROADCAST" );
54 DEBUG(3, ("Socket opened.\n"));
55 return True;
58 /****************************************************************************
59 turn a node status flags field into a string
60 ****************************************************************************/
61 static char *node_status_flags(unsigned char flags)
63 static fstring ret;
64 fstrcpy(ret,"");
66 fstrcat(ret, (flags & 0x80) ? "<GROUP> " : " ");
67 if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
68 if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
69 if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
70 if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
71 if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
72 if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
73 if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
74 if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
76 return ret;
79 /****************************************************************************
80 turn the NMB Query flags into a string
81 ****************************************************************************/
82 static char *query_flags(int flags)
84 static fstring ret1;
85 fstrcpy(ret1, "");
87 if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
88 if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
89 if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
90 if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
91 if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
92 if (flags & NM_FLAGS_B) fstrcat(ret1, "Broadcast ");
94 return ret1;
97 /****************************************************************************
98 do a node status query
99 ****************************************************************************/
100 static void do_node_status(int fd, const char *name, int type, struct in_addr ip)
102 struct nmb_name nname;
103 int count, i, j;
104 struct node_status *status;
105 fstring cleanname;
107 d_printf("Looking up status of %s\n",inet_ntoa(ip));
108 make_nmb_name(&nname, name, type);
109 status = node_status_query(fd,&nname,ip, &count);
110 if (status) {
111 for (i=0;i<count;i++) {
112 pull_ascii_fstring(cleanname, status[i].name);
113 for (j=0;cleanname[j];j++) {
114 if (!isprint((int)cleanname[j])) cleanname[j] = '.';
116 d_printf("\t%-15s <%02x> - %s\n",
117 cleanname,status[i].type,
118 node_status_flags(status[i].flags));
120 SAFE_FREE(status);
122 d_printf("\n");
126 /****************************************************************************
127 send out one query
128 ****************************************************************************/
129 static BOOL query_one(const char *lookup, unsigned int lookup_type)
131 int j, count, flags = 0;
132 struct in_addr *ip_list=NULL;
134 if (got_bcast) {
135 d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
136 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
137 use_bcast?True:recursion_desired,
138 bcast_addr,&count, &flags, NULL);
139 } else {
140 struct in_addr *bcast;
141 for (j=iface_count() - 1;
142 !ip_list && j >= 0;
143 j--) {
144 bcast = iface_n_bcast(j);
145 d_printf("querying %s on %s\n",
146 lookup, inet_ntoa(*bcast));
147 ip_list = name_query(ServerFD,lookup,lookup_type,
148 use_bcast,
149 use_bcast?True:recursion_desired,
150 *bcast,&count, &flags, NULL);
154 if (!ip_list) return False;
156 if (give_flags)
157 d_printf("Flags: %s\n", query_flags(flags));
159 for (j=0;j<count;j++) {
160 if (translate_addresses) {
161 struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
162 if (host) {
163 d_printf("%s, ", host -> h_name);
166 d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
169 /* We can only do find_status if the ip address returned
170 was valid - ie. name_query returned true.
172 if (find_status) {
173 do_node_status(ServerFD, lookup, lookup_type, ip_list[0]);
176 safe_free(ip_list);
178 return (ip_list != NULL);
182 /****************************************************************************
183 main program
184 ****************************************************************************/
185 int main(int argc,char *argv[])
187 int opt;
188 unsigned int lookup_type = 0x0;
189 fstring lookup;
190 static BOOL find_master=False;
191 static BOOL lookup_by_ip = False;
192 poptContext pc;
194 struct poptOption long_options[] = {
195 POPT_AUTOHELP
196 { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
197 { "flags", 'f', POPT_ARG_VAL, &give_flags, True, "List the NMB flags returned" },
198 { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" },
199 { "master-browser", 'M', POPT_ARG_VAL, &find_master, True, "Search for a master browser" },
200 { "recursion", 'R', POPT_ARG_VAL, &recursion_desired, True, "Set recursion desired in package" },
201 { "status", 'S', POPT_ARG_VAL, &find_status, True, "Lookup node status as well" },
202 { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" },
203 { "root-port", 'r', POPT_ARG_VAL, &RootPort, True, "Use root port 137 (Win95 only replies to this)" },
204 { "lookup-by-ip", 'A', POPT_ARG_VAL, &lookup_by_ip, True, "Do a node status on <name> as an IP Address" },
205 POPT_COMMON_SAMBA
206 POPT_COMMON_CONNECTION
207 { 0, 0, 0, 0 }
210 *lookup = 0;
212 setup_logging(argv[0],True);
214 pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options,
215 POPT_CONTEXT_KEEP_FIRST);
217 poptSetOtherOptionHelp(pc, "<NODE> ...");
219 while ((opt = poptGetNextOpt(pc)) != -1) {
220 switch (opt) {
221 case 'B':
222 bcast_addr = *interpret_addr2(poptGetOptArg(pc));
223 got_bcast = True;
224 use_bcast = True;
225 break;
226 case 'U':
227 bcast_addr = *interpret_addr2(poptGetOptArg(pc));
228 got_bcast = True;
229 use_bcast = False;
230 break;
231 case 'T':
232 translate_addresses = !translate_addresses;
233 break;
237 poptGetArg(pc); /* Remove argv[0] */
239 if(!poptPeekArg(pc)) {
240 poptPrintUsage(pc, stderr, 0);
241 exit(1);
244 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
245 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
248 load_interfaces();
249 if (!open_sockets()) return(1);
251 while(poptPeekArg(pc))
253 char *p;
254 struct in_addr ip;
256 fstrcpy(lookup,poptGetArg(pc));
258 if(lookup_by_ip)
260 ip = *interpret_addr2(lookup);
261 fstrcpy(lookup,"*");
262 do_node_status(ServerFD, lookup, lookup_type, ip);
263 continue;
266 if (find_master) {
267 if (*lookup == '-') {
268 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
269 lookup_type = 1;
270 } else {
271 lookup_type = 0x1d;
275 p = strchr_m(lookup,'#');
276 if (p) {
277 *p = '\0';
278 sscanf(++p,"%x",&lookup_type);
281 if (!query_one(lookup, lookup_type)) {
282 d_printf( "name_query failed to find name %s", lookup );
283 if( 0 != lookup_type )
284 d_printf( "#%02x", lookup_type );
285 d_printf( "\n" );
289 poptFreeContext(pc);
291 return(0);