6289ef74b1e027169388629724de58d8d4c13ae3
[kai/samba.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-1995
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 struct in_addr bcast_ip;
34 extern pstring myhostname;
35
36 static BOOL got_bcast = False;
37 struct in_addr ipzero;
38
39 int ServerFD= -1;
40
41 /****************************************************************************
42   open the socket communication
43   **************************************************************************/
44 static BOOL open_sockets(void)
45 {
46   struct hostent *hp;
47  
48   /* get host info */
49   if ((hp = Get_Hostbyname(myhostname)) == 0) 
50     {
51       DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
52       return False;
53     }   
54
55   ServerFD = open_socket_in(SOCK_DGRAM, 0,3);
56
57   if (ServerFD == -1)
58     return(False);
59
60   set_socket_options(ServerFD,"SO_BROADCAST");
61
62   DEBUG(3, ("Socket opened.\n"));
63   return True;
64 }
65
66
67 /****************************************************************************
68   initialise connect, service and file structs
69 ****************************************************************************/
70 static BOOL init_structs(void )
71 {
72   struct in_addr myip;
73
74   if (!get_myname(myhostname,&myip))
75     return(False);
76
77   /* Read the broadcast address from the interface */
78   {
79     struct in_addr ip0,ip2;
80
81     ip0 = myip;
82
83     if (!got_bcast) {
84       get_broadcast(&ip0,&bcast_ip,&ip2);
85
86       DEBUG(2,("Using broadcast %s\n",inet_ntoa(bcast_ip)));
87     }
88   }
89
90   return True;
91 }
92
93 /****************************************************************************
94 usage on the program
95 ****************************************************************************/
96 static void usage(void)
97 {
98   printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
99   printf("Version %s\n",VERSION);
100   printf("\t-d debuglevel         set the debuglevel\n");
101   printf("\t-B broadcast address  the address to use for broadcasts\n");
102   printf("\t-M                    searches for a master browser\n");
103   printf("\t-S                    lookup node status as well\n");
104   printf("\n");
105 }
106
107
108 /****************************************************************************
109   main program
110 ****************************************************************************/
111 int main(int argc,char *argv[])
112 {
113   int opt;
114   unsigned int lookup_type = 0x20;
115   pstring lookup;
116   extern int optind;
117   extern char *optarg;
118   BOOL find_master=False;
119   BOOL find_status=False;
120   int i;
121   
122   DEBUGLEVEL = 1;
123   *lookup = 0;
124
125   TimeInit();
126
127   ipzero = *interpret_addr2("0.0.0.0");
128
129   setup_logging(argv[0],True);
130
131   charset_initialise();
132
133   while ((opt = getopt(argc, argv, "p:d:B:i:SMh")) != EOF)
134     switch (opt)
135       {
136       case 'B':
137         {
138           unsigned long a = interpret_addr(optarg);
139           putip((char *)&bcast_ip,(char *)&a);
140           got_bcast = True;
141         }
142         break;
143       case 'i':
144         strcpy(scope,optarg);
145         strupper(scope);
146         break;
147       case 'M':
148         find_master = True;
149         break;
150       case 'S':
151         find_status = True;
152         break;
153       case 'd':
154         DEBUGLEVEL = atoi(optarg);
155         break;
156       case 'h':
157         usage();
158         exit(0);
159         break;
160       default:
161         usage();
162         exit(1);
163       }
164
165   if (argc < 2) {
166     usage();
167     exit(1);
168   }
169
170   init_structs();
171   if (!open_sockets()) return(1);
172
173   DEBUG(1,("Sending queries to %s\n",inet_ntoa(bcast_ip)));
174
175
176   for (i=optind;i<argc;i++)
177     {
178       BOOL bcast = True;
179       int retries = 2;
180       char *p;
181       struct in_addr ip;
182
183       strcpy(lookup,argv[i]);
184
185       if (find_master) {
186         if (*lookup == '-') {
187           strcpy(lookup,"\01\02__MSBROWSE__\02");
188           lookup_type = 1;
189         } else {
190           lookup_type = 0x1d;
191         }
192       }
193
194       p = strchr(lookup,'#');
195
196       if (p) {
197         *p = 0;
198         sscanf(p+1,"%x",&lookup_type);
199         bcast = False;
200         retries = 1;
201       }
202
203       if (name_query(ServerFD,lookup,lookup_type,bcast,True,
204                      bcast_ip,&ip,NULL)) 
205         {
206           printf("%s %s\n",inet_ntoa(ip),lookup);
207           if (find_status) 
208             {
209               printf("Looking up status of %s\n",inet_ntoa(ip));
210               name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
211               printf("\n");
212             }
213       } else {
214         printf("couldn't find name %s\n",lookup);
215       }
216     }
217
218   return(0);
219 }