This is *not* a big change (although it looks like one).
[nivanova/samba-autobuild/.git] / source3 / utils / nmblookup.c
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
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 #ifdef SYSLOG
24 #undef SYSLOG
25 #endif
26
27 #include "includes.h"
28
29 extern int DEBUGLEVEL;
30
31 extern pstring scope;
32
33 extern pstring myhostname;
34 extern struct in_addr ipzero;
35
36 int ServerFD= -1;
37
38 int RootPort = 0;
39
40 /****************************************************************************
41   open the socket communication
42   **************************************************************************/
43 static BOOL open_sockets(void)
44 {
45   ServerFD = open_socket_in( SOCK_DGRAM,
46                              (RootPort ? 137 :0),
47                              3,
48                              interpret_addr(lp_socket_address()) );
49
50   if (ServerFD == -1)
51     return(False);
52
53   set_socket_options(ServerFD,"SO_BROADCAST");
54
55   DEBUG(3, ("Socket opened.\n"));
56   return True;
57 }
58
59
60 /****************************************************************************
61   initialise connect, service and file structs
62 ****************************************************************************/
63 static BOOL init_structs(void )
64 {
65   if (!get_myname(myhostname,NULL))
66     return(False);
67
68   return True;
69 }
70
71 /****************************************************************************
72 usage on the program
73 ****************************************************************************/
74 static void usage(void)
75 {
76   printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
77   printf("Version %s\n",VERSION);
78   printf("\t-d debuglevel         set the debuglevel\n");
79   printf("\t-B broadcast address  the address to use for broadcasts\n");
80   printf("\t-U unicast   address  the address to use for unicast\n");
81   printf("\t-M                    searches for a master browser\n");
82   printf("\t-R                    set recursion desired in packet\n");
83   printf("\t-S                    lookup node status as well\n");
84   printf("\t-r                    Use root port 137 (Win95 only replies to this)\n");
85   printf("\t-A                    Do a node status on <name> as an IP Address\n");
86   printf("\n");
87 }
88
89
90 /****************************************************************************
91   main program
92 ****************************************************************************/
93 int main(int argc,char *argv[])
94 {
95   int opt;
96   unsigned int lookup_type = 0x0;
97   pstring lookup;
98   extern int optind;
99   extern char *optarg;
100   BOOL find_master=False;
101   BOOL find_status=False;
102   int i;
103   static pstring servicesf = CONFIGFILE;
104   struct in_addr bcast_addr;
105   BOOL use_bcast = True;
106   BOOL got_bcast = False;
107   BOOL lookup_by_ip = False;
108   BOOL recursion_desired = False;
109
110   DEBUGLEVEL = 1;
111   *lookup = 0;
112
113   TimeInit();
114
115   setup_logging(argv[0],True);
116
117   charset_initialise();
118
119   while ((opt = getopt(argc, argv, "d:B:U:i:s:SMrhAR")) != EOF)
120     switch (opt)
121       {
122       case 'B':
123         iface_set_default(NULL,optarg,NULL);
124         bcast_addr = *interpret_addr2(optarg);
125         got_bcast = True;
126         use_bcast = True;
127         break;
128       case 'U':
129         iface_set_default(NULL,optarg,NULL);
130         bcast_addr = *interpret_addr2(optarg);
131         got_bcast = True;
132         use_bcast = False;
133         break;
134       case 'i':
135         fstrcpy(scope,optarg);
136         strupper(scope);
137         break;
138       case 'M':
139         find_master = True;
140         break;
141       case 'S':
142         find_status = True;
143         break;
144       case 'R':
145         recursion_desired = True;
146         break;
147       case 'd':
148         DEBUGLEVEL = atoi(optarg);
149         break;
150       case 's':
151         pstrcpy(servicesf, optarg);
152         break;
153       case 'r':
154         RootPort = -1;
155         break;
156       case 'h':
157         usage();
158         exit(0);
159         break;
160       case 'A':
161         lookup_by_ip = True;
162         break;
163       default:
164         usage();
165         exit(1);
166       }
167
168   if (argc < 2) {
169     usage();
170     exit(1);
171   }
172
173   if (!lp_load(servicesf,True)) {
174     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
175   }
176
177   load_interfaces();
178   init_structs();
179   if (!open_sockets()) return(1);
180
181   if (!got_bcast)
182     bcast_addr = *iface_bcast(ipzero);
183
184   DEBUG(1,("Sending queries to %s\n",inet_ntoa(bcast_addr)));
185
186
187   for (i=optind;i<argc;i++)
188   {
189       int j, count, retries = 2;
190       char *p;
191       struct in_addr ip;
192       struct in_addr *ip_list;
193
194       fstrcpy(lookup,argv[i]);
195
196       if(lookup_by_ip)
197       {
198         strcpy(lookup,"*");
199         ip = *interpret_addr2(argv[i]);
200         printf("Looking up status of %s\n",inet_ntoa(ip));
201         name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
202         printf("\n");
203         continue;
204       }
205
206       if (find_master) {
207         if (*lookup == '-') {
208           strcpy(lookup,"\01\02__MSBROWSE__\02");
209           lookup_type = 1;
210         } else {
211           lookup_type = 0x1d;
212         }
213       }
214
215       p = strchr(lookup,'#');
216
217       if (p) {
218         *p = 0;
219         sscanf(p+1,"%x",&lookup_type);
220         retries = 1;
221       }
222
223       if ((ip_list = name_query(ServerFD,lookup,lookup_type,use_bcast,recursion_desired,
224                                 bcast_addr,&count,NULL))) {
225               for (j=0;j<count;j++)
226                       printf("%s %s<%02x>\n",inet_ntoa(ip_list[j]),lookup, lookup_type);
227               
228               /* We can only do find_status if the ip address returned
229                  was valid - ie. name_query returned true.
230                  */
231               if (find_status) {
232                       printf("Looking up status of %s\n",inet_ntoa(ip_list[0]));
233                       name_status(ServerFD,lookup,lookup_type,True,ip_list[0],NULL,NULL,NULL);
234                       printf("\n");
235               }
236       } else {
237               printf("name_query failed to find name %s\n", lookup);
238       }
239   }
240   
241   return(0);
242 }