This is the ubiqx binary tree and linked list library.
[Samba.git] / source / utils / nmblookup.c
blob63ca156449f062a28283f400d15ad7480aa3f676
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 NBT client - used to lookup netbios names
5 Copyright (C) Andrew Tridgell 1994-1997
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 #ifdef SYSLOG
24 #undef SYSLOG
25 #endif
27 #include "includes.h"
29 extern int DEBUGLEVEL;
31 extern pstring scope;
33 extern pstring myhostname;
34 extern struct in_addr ipzero;
36 int ServerFD= -1;
38 int RootPort = 0;
40 /****************************************************************************
41 open the socket communication
42 **************************************************************************/
43 static BOOL open_sockets(void)
45 struct hostent *hp;
47 /* get host info */
48 if ((hp = Get_Hostbyname(myhostname)) == 0)
50 DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
51 return False;
54 ServerFD = open_socket_in( SOCK_DGRAM,
55 (RootPort ? 137 :0),
57 interpret_addr(lp_socket_address()) );
59 if (ServerFD == -1)
60 return(False);
62 set_socket_options(ServerFD,"SO_BROADCAST");
64 DEBUG(3, ("Socket opened.\n"));
65 return True;
69 /****************************************************************************
70 initialise connect, service and file structs
71 ****************************************************************************/
72 static BOOL init_structs(void )
74 if (!get_myname(myhostname,NULL))
75 return(False);
77 return True;
80 /****************************************************************************
81 usage on the program
82 ****************************************************************************/
83 static void usage(void)
85 printf("Usage: nmblookup [-M] [-B bcast address] [-d debuglevel] name\n");
86 printf("Version %s\n",VERSION);
87 printf("\t-d debuglevel set the debuglevel\n");
88 printf("\t-B broadcast address the address to use for broadcasts\n");
89 printf("\t-M searches for a master browser\n");
90 printf("\t-S lookup node status as well\n");
91 printf("\t-r Use root port 137 (Win95 only replies to this)\n");
92 printf("\t-A Do a node status on <name> as an IP Address\n");
93 printf("\n");
97 /****************************************************************************
98 main program
99 ****************************************************************************/
100 int main(int argc,char *argv[])
102 int opt;
103 unsigned int lookup_type = 0x0;
104 pstring lookup;
105 extern int optind;
106 extern char *optarg;
107 BOOL find_master=False;
108 BOOL find_status=False;
109 int i;
110 static pstring servicesf = CONFIGFILE;
111 struct in_addr bcast_addr;
112 BOOL got_bcast = False;
113 BOOL lookup_by_ip = False;
115 DEBUGLEVEL = 1;
116 *lookup = 0;
118 TimeInit();
120 setup_logging(argv[0],True);
122 charset_initialise();
124 while ((opt = getopt(argc, argv, "d:B:i:s:SMrhA")) != EOF)
125 switch (opt)
127 case 'B':
128 iface_set_default(NULL,optarg,NULL);
129 bcast_addr = *interpret_addr2(optarg);
130 got_bcast = True;
131 break;
132 case 'i':
133 fstrcpy(scope,optarg);
134 strupper(scope);
135 break;
136 case 'M':
137 find_master = True;
138 break;
139 case 'S':
140 find_status = True;
141 break;
142 case 'd':
143 DEBUGLEVEL = atoi(optarg);
144 break;
145 case 's':
146 pstrcpy(servicesf, optarg);
147 break;
148 case 'r':
149 RootPort = -1;
150 break;
151 case 'h':
152 usage();
153 exit(0);
154 break;
155 case 'A':
156 lookup_by_ip = True;
157 break;
158 default:
159 usage();
160 exit(1);
163 if (argc < 2) {
164 usage();
165 exit(1);
168 if (!lp_load(servicesf,True)) {
169 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
172 load_interfaces();
173 init_structs();
174 if (!open_sockets()) return(1);
176 if (!got_bcast)
177 bcast_addr = *iface_bcast(ipzero);
179 DEBUG(1,("Sending queries to %s\n",inet_ntoa(bcast_addr)));
182 for (i=optind;i<argc;i++)
184 BOOL bcast = True;
185 int retries = 2;
186 char *p;
187 struct in_addr ip;
189 fstrcpy(lookup,argv[i]);
191 if(lookup_by_ip)
193 strcpy(lookup,"*");
194 ip = *interpret_addr2(argv[i]);
195 printf("Looking up status of %s\n",inet_ntoa(ip));
196 name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
197 printf("\n");
198 continue;
201 if (find_master) {
202 if (*lookup == '-') {
203 strcpy(lookup,"\01\02__MSBROWSE__\02");
204 lookup_type = 1;
205 } else {
206 lookup_type = 0x1d;
210 p = strchr(lookup,'#');
212 if (p) {
213 *p = 0;
214 sscanf(p+1,"%x",&lookup_type);
215 bcast = False;
216 retries = 1;
219 if (name_query(ServerFD,lookup,lookup_type,bcast,True,
220 bcast_addr,&ip,NULL))
222 printf("%s %s\n",inet_ntoa(ip),lookup);
224 /* We can only do find_status if the ip address returned
225 was valid - ie. name_query returned true.
227 if (find_status)
229 printf("Looking up status of %s\n",inet_ntoa(ip));
230 name_status(ServerFD,lookup,lookup_type,True,ip,NULL,NULL,NULL);
231 printf("\n");
234 else
236 printf("name_query failed to find name %s\n", lookup);
240 return(0);