source: branches/samba-3.0/source/utils/nmblookup.c@ 891

Last change on this file since 891 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 8.8 KB
Line 
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)
6
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.
11
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.
16
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.
20
21*/
22
23#include "includes.h"
24
25extern BOOL AllowDebugChange;
26
27static BOOL give_flags = False;
28static BOOL use_bcast = True;
29static BOOL got_bcast = False;
30static struct in_addr bcast_addr;
31static BOOL recursion_desired = False;
32static BOOL translate_addresses = False;
33static int ServerFD= -1;
34static int RootPort = False;
35static BOOL find_status=False;
36
37/****************************************************************************
38 open the socket communication
39 **************************************************************************/
40static BOOL open_sockets(void)
41{
42 ServerFD = open_socket_in( SOCK_DGRAM,
43 (RootPort ? 137 : 0),
44 (RootPort ? 0 : 3),
45 interpret_addr(lp_socket_address()), True );
46
47 if (ServerFD == -1)
48 return(False);
49
50 set_socket_options( ServerFD, "SO_BROADCAST" );
51
52 DEBUG(3, ("Socket opened.\n"));
53 return True;
54}
55
56/****************************************************************************
57turn a node status flags field into a string
58****************************************************************************/
59static char *node_status_flags(unsigned char flags)
60{
61 static fstring ret;
62 fstrcpy(ret,"");
63
64 fstrcat(ret, (flags & 0x80) ? "<GROUP> " : " ");
65 if ((flags & 0x60) == 0x00) fstrcat(ret,"B ");
66 if ((flags & 0x60) == 0x20) fstrcat(ret,"P ");
67 if ((flags & 0x60) == 0x40) fstrcat(ret,"M ");
68 if ((flags & 0x60) == 0x60) fstrcat(ret,"H ");
69 if (flags & 0x10) fstrcat(ret,"<DEREGISTERING> ");
70 if (flags & 0x08) fstrcat(ret,"<CONFLICT> ");
71 if (flags & 0x04) fstrcat(ret,"<ACTIVE> ");
72 if (flags & 0x02) fstrcat(ret,"<PERMANENT> ");
73
74 return ret;
75}
76
77/****************************************************************************
78turn the NMB Query flags into a string
79****************************************************************************/
80static char *query_flags(int flags)
81{
82 static fstring ret1;
83 fstrcpy(ret1, "");
84
85 if (flags & NM_FLAGS_RS) fstrcat(ret1, "Response ");
86 if (flags & NM_FLAGS_AA) fstrcat(ret1, "Authoritative ");
87 if (flags & NM_FLAGS_TC) fstrcat(ret1, "Truncated ");
88 if (flags & NM_FLAGS_RD) fstrcat(ret1, "Recursion_Desired ");
89 if (flags & NM_FLAGS_RA) fstrcat(ret1, "Recursion_Available ");
90 if (flags & NM_FLAGS_B) fstrcat(ret1, "Broadcast ");
91
92 return ret1;
93}
94
95/****************************************************************************
96do a node status query
97****************************************************************************/
98static void do_node_status(int fd, const char *name, int type, struct in_addr ip)
99{
100 struct nmb_name nname;
101 int count, i, j;
102 NODE_STATUS_STRUCT *status;
103 struct node_status_extra extra;
104 fstring cleanname;
105
106 d_printf("Looking up status of %s\n",inet_ntoa(ip));
107 make_nmb_name(&nname, name, type);
108 status = node_status_query(fd,&nname,ip, &count, &extra);
109 if (status) {
110 for (i=0;i<count;i++) {
111 pull_ascii_fstring(cleanname, status[i].name);
112 for (j=0;cleanname[j];j++) {
113 if (!isprint((int)cleanname[j])) cleanname[j] = '.';
114 }
115 d_printf("\t%-15s <%02x> - %s\n",
116 cleanname,status[i].type,
117 node_status_flags(status[i].flags));
118 }
119 d_printf("\n\tMAC Address = %02X-%02X-%02X-%02X-%02X-%02X\n",
120 extra.mac_addr[0], extra.mac_addr[1],
121 extra.mac_addr[2], extra.mac_addr[3],
122 extra.mac_addr[4], extra.mac_addr[5]);
123 d_printf("\n");
124 SAFE_FREE(status);
125 } else {
126 d_printf("No reply from %s\n\n",inet_ntoa(ip));
127 }
128}
129
130
131/****************************************************************************
132send out one query
133****************************************************************************/
134static BOOL query_one(const char *lookup, unsigned int lookup_type)
135{
136 int j, count, flags = 0;
137 struct in_addr *ip_list=NULL;
138
139 if (got_bcast) {
140 d_printf("querying %s on %s\n", lookup, inet_ntoa(bcast_addr));
141 ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,
142 use_bcast?True:recursion_desired,
143 bcast_addr,&count, &flags, NULL);
144 } else {
145 struct in_addr *bcast;
146 for (j=iface_count() - 1;
147 !ip_list && j >= 0;
148 j--) {
149 bcast = iface_n_bcast(j);
150 d_printf("querying %s on %s\n",
151 lookup, inet_ntoa(*bcast));
152 ip_list = name_query(ServerFD,lookup,lookup_type,
153 use_bcast,
154 use_bcast?True:recursion_desired,
155 *bcast,&count, &flags, NULL);
156 }
157 }
158
159 if (!ip_list) return False;
160
161 if (give_flags)
162 d_printf("Flags: %s\n", query_flags(flags));
163
164 for (j=0;j<count;j++) {
165 if (translate_addresses) {
166 struct hostent *host = gethostbyaddr((char *)&ip_list[j], sizeof(ip_list[j]), AF_INET);
167 if (host) {
168 d_printf("%s, ", host -> h_name);
169 }
170 }
171 d_printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
172 /* We can only do find_status if the ip address returned
173 was valid - ie. name_query returned true.
174 */
175 if (find_status) {
176 do_node_status(ServerFD, lookup, lookup_type, ip_list[j]);
177 }
178 }
179
180 safe_free(ip_list);
181
182 return (ip_list != NULL);
183}
184
185
186/****************************************************************************
187 main program
188****************************************************************************/
189int main(int argc,char *argv[])
190{
191 int opt;
192 unsigned int lookup_type = 0x0;
193 fstring lookup;
194 static BOOL find_master=False;
195 static BOOL lookup_by_ip = False;
196 poptContext pc;
197
198 struct poptOption long_options[] = {
199 POPT_AUTOHELP
200 { "broadcast", 'B', POPT_ARG_STRING, NULL, 'B', "Specify address to use for broadcasts", "BROADCAST-ADDRESS" },
201 { "flags", 'f', POPT_ARG_VAL, &give_flags, True, "List the NMB flags returned" },
202 { "unicast", 'U', POPT_ARG_STRING, NULL, 'U', "Specify address to use for unicast" },
203 { "master-browser", 'M', POPT_ARG_VAL, &find_master, True, "Search for a master browser" },
204 { "recursion", 'R', POPT_ARG_VAL, &recursion_desired, True, "Set recursion desired in package" },
205 { "status", 'S', POPT_ARG_VAL, &find_status, True, "Lookup node status as well" },
206 { "translate", 'T', POPT_ARG_NONE, NULL, 'T', "Translate IP addresses into names" },
207 { "root-port", 'r', POPT_ARG_VAL, &RootPort, True, "Use root port 137 (Win95 only replies to this)" },
208 { "lookup-by-ip", 'A', POPT_ARG_VAL, &lookup_by_ip, True, "Do a node status on <name> as an IP Address" },
209 POPT_COMMON_SAMBA
210 POPT_COMMON_CONNECTION
211 { 0, 0, 0, 0 }
212 };
213
214 *lookup = 0;
215
216 load_case_tables();
217
218 setup_logging(argv[0],True);
219
220 pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options,
221 POPT_CONTEXT_KEEP_FIRST);
222
223 poptSetOtherOptionHelp(pc, "<NODE> ...");
224
225 while ((opt = poptGetNextOpt(pc)) != -1) {
226 switch (opt) {
227 case 'B':
228 bcast_addr = *interpret_addr2(poptGetOptArg(pc));
229 got_bcast = True;
230 use_bcast = True;
231 break;
232 case 'U':
233 bcast_addr = *interpret_addr2(poptGetOptArg(pc));
234 got_bcast = True;
235 use_bcast = False;
236 break;
237 case 'T':
238 translate_addresses = !translate_addresses;
239 break;
240 }
241 }
242
243 poptGetArg(pc); /* Remove argv[0] */
244
245 if(!poptPeekArg(pc)) {
246 poptPrintUsage(pc, stderr, 0);
247 exit(1);
248 }
249
250 if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
251 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
252 }
253
254 load_interfaces();
255 if (!open_sockets()) return(1);
256
257 while(poptPeekArg(pc))
258 {
259 char *p;
260 struct in_addr ip;
261
262 fstrcpy(lookup,poptGetArg(pc));
263
264 if(lookup_by_ip)
265 {
266 ip = *interpret_addr2(lookup);
267 fstrcpy(lookup,"*");
268 do_node_status(ServerFD, lookup, lookup_type, ip);
269 continue;
270 }
271
272 if (find_master) {
273 if (*lookup == '-') {
274 fstrcpy(lookup,"\01\02__MSBROWSE__\02");
275 lookup_type = 1;
276 } else {
277 lookup_type = 0x1d;
278 }
279 }
280
281 p = strchr_m(lookup,'#');
282 if (p) {
283 *p = '\0';
284 sscanf(++p,"%x",&lookup_type);
285 }
286
287 if (!query_one(lookup, lookup_type)) {
288 d_printf( "name_query failed to find name %s", lookup );
289 if( 0 != lookup_type )
290 d_printf( "#%02x", lookup_type );
291 d_printf( "\n" );
292 }
293 }
294
295 poptFreeContext(pc);
296
297 return(0);
298}
Note: See TracBrowser for help on using the repository browser.