minor async DNS cleanups
[Samba.git] / source / namedbsubnet.c
blob27c1a0470e3e9f04321f4e4475b964a778c36f43
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 NBT netbios routines and daemon - version 2
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.
21 Revision History:
23 14 jan 96: lkcl@pires.co.uk
24 added multiple workgroup domain master support
26 04 jul 96: lkcl@pires.co.uk
27 created module namedbsubnet containing subnet database functions
31 #include "includes.h"
32 #include "smb.h"
34 extern int ClientNMB;
35 extern int ClientDGRAM;
36 extern int global_nmb_port;
38 extern int DEBUGLEVEL;
40 extern struct in_addr wins_ip;
41 extern struct in_addr ipzero;
43 extern pstring myname;
44 extern fstring myworkgroup;
45 extern char **my_netbios_names;
47 BOOL updatedlists = True;
48 int updatecount = 0;
50 /* local interfaces structure */
51 extern struct interface *local_interfaces;
53 /* this is our domain/workgroup/server database */
54 struct subnet_record *subnetlist = NULL;
56 /* WINS subnet - keep this separate so enumeration code doesn't
57 run onto it by mistake. */
58 struct subnet_record *wins_client_subnet = NULL;
60 extern uint16 nb_type; /* samba's NetBIOS name type */
62 /****************************************************************************
63 add a domain into the list
64 **************************************************************************/
65 static void add_subnet(struct subnet_record *d)
67 struct subnet_record *d2;
69 if (!subnetlist)
71 subnetlist = d;
72 d->prev = NULL;
73 d->next = NULL;
74 return;
77 for (d2 = subnetlist; d2->next; d2 = d2->next);
79 d2->next = d;
80 d->next = NULL;
81 d->prev = d2;
85 /****************************************************************************
86 find a subnet in the subnetlist that a given IP address could
87 match - not including WINS. Returns NULL if no match.
88 **************************************************************************/
89 struct subnet_record *find_subnet(struct in_addr ip)
91 struct subnet_record *d = NULL;
93 /* search through subnet list for broadcast/netmask that matches
94 the source ip address. */
96 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_EXCLUDING_WINS(d))
98 if (same_net(ip, d->bcast_ip, d->mask_ip))
99 break;
102 return d;
105 /****************************************************************************
106 find a subnet in the subnetlist - if the subnet is not found
107 then return the WINS client subnet.
108 **************************************************************************/
109 struct subnet_record *find_subnet_all(struct in_addr ip)
111 struct subnet_record *d = find_subnet(ip);
112 if(!d)
113 return wins_client_subnet;
114 return d;
117 /****************************************************************************
118 create a subnet entry
119 ****************************************************************************/
120 static struct subnet_record *make_subnet(struct in_addr myip, struct in_addr bcast_ip,
121 struct in_addr mask_ip, BOOL add)
123 struct subnet_record *d = NULL;
124 int nmb_sock, dgram_sock;
126 /* Check if we are creating the WINS subnet - if so don't create
127 sockets, use the ClientNMB and ClientDGRAM sockets instead.
130 if(ip_equal(bcast_ip, wins_ip))
132 nmb_sock = -1;
133 dgram_sock = -1;
135 else
138 * Attempt to open the sockets on port 137/138 for this interface
139 * and bind them.
140 * Fail the subnet creation if this fails.
143 if((nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,0, myip.s_addr)) == -1)
145 DEBUG(0,("make_subnet: Failed to open nmb socket on interface %s \
146 for port %d. Error was %s\n", inet_ntoa(myip), global_nmb_port, strerror(errno)));
147 return NULL;
150 if((dgram_sock = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3, myip.s_addr)) == -1)
152 DEBUG(0,("make_subnet: Failed to open dgram socket on interface %s \
153 for port %d. Error was %s\n", inet_ntoa(myip), DGRAM_PORT, strerror(errno)));
154 return NULL;
157 /* Make sure we can broadcast from these sockets. */
158 set_socket_options(nmb_sock,"SO_BROADCAST");
159 set_socket_options(dgram_sock,"SO_BROADCAST");
163 d = (struct subnet_record *)malloc(sizeof(*d));
165 if (!d)
167 DEBUG(0,("make_subnet: malloc fail !\n"));
168 close(nmb_sock);
169 close(dgram_sock);
170 return(NULL);
173 bzero((char *)d,sizeof(*d));
175 DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip)));
176 DEBUG(4, ("%s\n", inet_ntoa(mask_ip)));
178 d->bcast_ip = bcast_ip;
179 d->mask_ip = mask_ip;
180 d->myip = myip;
181 d->nmb_sock = nmb_sock;
182 d->dgram_sock = dgram_sock;
183 d->workgrouplist = NULL;
185 if(add)
186 add_subnet(d);
188 return d;
191 /****************************************************************************
192 add a domain entry. creates a workgroup, if necessary, and adds the domain
193 to the named a workgroup.
194 ****************************************************************************/
195 static struct subnet_record *add_subnet_entry(struct in_addr myip,
196 struct in_addr bcast_ip,
197 struct in_addr mask_ip, char *name,
198 BOOL create_subnets, BOOL add)
200 struct subnet_record *d = NULL;
202 if (zero_ip(bcast_ip))
203 bcast_ip = *iface_bcast(bcast_ip);
205 /* Note that we should also add into the WINS subnet as add_subnet_entry
206 should be called to add NetBIOS names and server entries on all
207 interfaces, including the WINS interface
210 if(create_subnets == True)
212 /* Create new subnets. */
213 if((d = make_subnet(myip, bcast_ip, mask_ip, add)) == NULL)
215 DEBUG(0,("add_subnet_entry: Unable to create subnet %s\n",
216 inet_ntoa(bcast_ip) ));
217 return NULL;
219 return d;
221 if(ip_equal(bcast_ip, wins_ip))
222 return wins_client_subnet;
223 return find_subnet(bcast_ip);
226 /****************************************************************************
227 Add a workgroup into a subnet, and if it's our primary workgroup,
228 add the required names to it.
229 **************************************************************************/
231 void add_workgroup_to_subnet( struct subnet_record *d, char *group)
233 struct work_record *w = NULL;
235 DEBUG(5,("add_workgroup_to_subnet: Adding workgroup %s to subnet %s\n",
236 group, inet_ntoa(d->bcast_ip)));
238 /* This next statement creates the workgroup struct if it doesn't
239 already exist.
241 if((w = find_workgroupstruct(d, group, True)) == NULL)
243 DEBUG(0,("add_workgroup_to_subnet: Unable to add workgroup %s to subnet %s\n",
244 group, inet_ntoa(d->bcast_ip) ));
245 return;
248 /* add WORKGROUP(00) entries into name database
249 or register with WINS server, if it's our workgroup.
251 if (strequal(myworkgroup, group))
253 int n;
255 add_my_name_entry(d,group,0x0 ,nb_type|NB_ACTIVE|NB_GROUP);
257 /* Only register the WORKGROUP<0x1e> name if we could be a local master
258 browser. */
259 if(lp_local_master())
260 add_my_name_entry(d,group,0x1e,nb_type|NB_ACTIVE|NB_GROUP);
262 /* Add all our server names to the workgroup list. We remove any
263 browser or logon server flags from all but the primary name.
265 for( n = 0; my_netbios_names[n]; n++)
267 char *name = my_netbios_names[n];
268 int stype = w->ServerType;
270 if(!strequal(myname, name))
271 stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|
272 SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
274 add_server_entry(d,w,name,stype|SV_TYPE_LOCAL_LIST_ONLY,0,
275 lp_serverstring(),True);
276 DEBUG(3,("add_workgroup_to_subnet: Added server name entry %s \
277 to subnet %s\n", name, inet_ntoa(d->bcast_ip)));
282 /****************************************************************************
283 create subnet / workgroup / server entries
285 - add or create the subnet lists
286 - add or create the workgroup entries in each subnet entry
287 - register appropriate NetBIOS names for the workgroup entries
289 **************************************************************************/
290 void add_my_subnets(char *group)
292 static BOOL create_subnets = True;
293 struct subnet_record *d = NULL;
294 struct interface *i = NULL;
296 if (*group == '*') return;
298 /* Create subnets from all the local interfaces and thread them onto
299 the linked list.
301 for (i = local_interfaces; i; i = i->next)
303 add_subnet_entry(i->ip, i->bcast,i->nmask,group, create_subnets, True);
306 /* If we are using WINS, then we must add the workgroup to the WINS
307 subnet. This is used as a place to keep collated server lists.
310 /* Create the WINS subnet if we are using WINS - but don't thread it
311 onto the linked subnet list.
313 if (lp_wins_support() || lp_wins_server())
315 struct in_addr wins_nmask = ipzero;
316 wins_client_subnet = add_subnet_entry(ipzero, wins_ip, wins_nmask, group, create_subnets, False);
319 /* Ensure we only create the subnets once. */
320 create_subnets = False;
322 /* Now we have created all the subnets - we can add the names
323 that make us a client member in the workgroup.
325 for (d = FIRST_SUBNET; d; d = NEXT_SUBNET_INCLUDING_WINS(d))
326 add_workgroup_to_subnet(d, group);
329 /*******************************************************************
330 write out browse.dat
331 ******************************************************************/
332 void write_browse_list(time_t t)
334 struct subnet_record *d;
335 pstring fname,fnamenew;
336 FILE *f;
338 static time_t lasttime = 0;
340 if (!lasttime) lasttime = t;
341 if (!updatedlists || t - lasttime < 5) return;
343 lasttime = t;
344 updatedlists = False;
345 updatecount++;
347 dump_names();
348 dump_workgroups();
350 pstrcpy(fname,lp_lockdir());
351 trim_string(fname,NULL,"/");
352 strcat(fname,"/");
353 strcat(fname,SERVER_LIST);
354 pstrcpy(fnamenew,fname);
355 strcat(fnamenew,".");
357 f = fopen(fnamenew,"w");
359 if (!f)
361 DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
362 return;
365 for (d = FIRST_SUBNET; d ; d = NEXT_SUBNET_INCLUDING_WINS(d))
367 struct work_record *work;
368 for (work = d->workgrouplist; work ; work = work->next)
370 struct server_record *s;
371 for (s = work->serverlist; s ; s = s->next)
373 fstring tmp;
375 /* don't list domains I don't have a master for */
376 if ((s->serv.type & SV_TYPE_DOMAIN_ENUM) && !s->serv.comment[0])
378 continue;
381 /* output server details, plus what workgroup/domain
382 they're in. without the domain information, the
383 combined list of all servers in all workgroups gets
384 sent to anyone asking about any workgroup! */
386 sprintf(tmp, "\"%s\"", s->serv.name);
387 fprintf(f, "%-25s ", tmp);
388 fprintf(f, "%08x ", s->serv.type);
389 sprintf(tmp, "\"%s\" ", s->serv.comment);
390 fprintf(f, "%-30s", tmp);
391 fprintf(f, "\"%s\"\n", work->work_group);
396 fclose(f);
397 unlink(fname);
398 chmod(fnamenew,0644);
399 rename(fnamenew,fname);
400 DEBUG(3,("Wrote browse list %s\n",fname));