sorted out various timer delay bugs: nameannounce.c nameserv.c
[Samba.git] / source / namedbsubnet.c
blob6b187b21bb8a10db9f01ff194b38a2907046dd28
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1996
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;
37 extern int DEBUGLEVEL;
39 extern struct in_addr ipgrp;
40 extern struct in_addr ipzero;
42 extern pstring myname;
44 BOOL updatedlists = True;
45 int updatecount = 0;
47 /* local interfaces structure */
48 extern struct interface *local_interfaces;
50 /* remote interfaces structure */
51 extern struct interface *remote_interfaces;
53 /* this is our domain/workgroup/server database */
54 struct subnet_record *subnetlist = NULL;
57 /****************************************************************************
58 add a domain into the list
59 **************************************************************************/
60 static void add_subnet(struct subnet_record *d)
62 struct subnet_record *d2;
64 if (!subnetlist)
66 subnetlist = d;
67 d->prev = NULL;
68 d->next = NULL;
69 return;
72 for (d2 = subnetlist; d2->next; d2 = d2->next);
74 d2->next = d;
75 d->next = NULL;
76 d->prev = d2;
80 /****************************************************************************
81 find a subnet in the subnetlist
82 **************************************************************************/
83 struct subnet_record *find_subnet(struct in_addr bcast_ip)
85 struct subnet_record *d;
86 struct in_addr wins_ip = ipgrp;
88 /* search through subnet list for broadcast/netmask that matches
89 the source ip address. a subnet 255.255.255.255 represents the
90 WINS list. */
92 for (d = subnetlist; d; d = d->next)
94 if (ip_equal(bcast_ip, wins_ip))
96 if (ip_equal(bcast_ip, d->bcast_ip))
98 return d;
101 else if (same_net(bcast_ip, d->bcast_ip, d->mask_ip))
103 return(d);
107 return (NULL);
111 /****************************************************************************
112 finds the appropriate subnet structure. directed packets (non-bcast) are
113 assumed to come from a point-to-point (P or M node), and so the subnet we
114 return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
115 ****************************************************************************/
116 struct subnet_record *find_req_subnet(struct in_addr ip, BOOL bcast)
118 if (bcast)
120 /* identify the subnet the broadcast request came from */
121 return find_subnet(*iface_bcast(ip));
123 /* find the subnet under the pseudo-ip of 255.255.255.255 */
124 return find_subnet(ipgrp);
128 /****************************************************************************
129 create a domain entry
130 ****************************************************************************/
131 static struct subnet_record *make_subnet(struct in_addr bcast_ip, struct in_addr mask_ip)
133 struct subnet_record *d;
134 d = (struct subnet_record *)malloc(sizeof(*d));
136 if (!d) return(NULL);
138 bzero((char *)d,sizeof(*d));
140 DEBUG(4, ("making domain %s ", inet_ntoa(bcast_ip)));
141 DEBUG(4, ("%s\n", inet_ntoa(mask_ip)));
143 d->bcast_ip = bcast_ip;
144 d->mask_ip = mask_ip;
145 d->workgrouplist = NULL;
146 d->my_interface = False; /* True iff the interface is on the samba host */
148 add_subnet(d);
150 return d;
154 /****************************************************************************
155 add the remote interfaces from lp_remote_interfaces() and lp_interfaces()
156 to the netbios subnet database.
157 ****************************************************************************/
158 void add_subnet_interfaces(void)
160 struct interface *i;
162 /* loop on all local interfaces */
163 for (i = local_interfaces; i; i = i->next)
165 /* add the interface into our subnet database */
166 if (!find_subnet(i->bcast))
168 struct subnet_record *d = make_subnet(i->bcast,i->nmask);
169 if (d)
171 /* short-cut method to identifying local interfaces */
172 d->my_interface = True;
177 /* loop on all remote interfaces */
178 for (i = remote_interfaces; i; i = i->next)
180 /* add the interface into our subnet database */
181 if (!find_subnet(i->bcast))
183 make_subnet(i->bcast,i->nmask);
187 /* add the pseudo-ip interface for WINS: 255.255.255.255 */
188 if (lp_wins_support())
190 struct in_addr wins_bcast = ipgrp;
191 struct in_addr wins_nmask = ipzero;
192 make_subnet(wins_bcast, wins_nmask);
198 /****************************************************************************
199 add the default workgroup into my domain
200 **************************************************************************/
201 void add_my_subnets(char *group)
203 struct interface *i;
205 /* add or find domain on our local subnet, in the default workgroup */
207 if (*group == '*') return;
209 /* the coding choice is up to you, andrew: i can see why you don't want
210 global access to the local_interfaces structure: so it can't get
211 messed up! */
212 for (i = local_interfaces; i; i = i->next)
214 add_subnet_entry(i->bcast,i->nmask,group, True, False);
219 /****************************************************************************
220 add a domain entry. creates a workgroup, if necessary, and adds the domain
221 to the named a workgroup.
222 ****************************************************************************/
223 struct subnet_record *add_subnet_entry(struct in_addr bcast_ip,
224 struct in_addr mask_ip,
225 char *name, BOOL add, BOOL lmhosts)
227 struct subnet_record *d;
229 /* XXXX andrew: struct in_addr ip appears not to be referenced at all except
230 in the DEBUG comment. i assume that the DEBUG comment below actually
231 intends to refer to bcast_ip? i don't know.
233 struct in_addr ip = ipgrp;
237 if (zero_ip(bcast_ip))
238 bcast_ip = *iface_bcast(bcast_ip);
240 /* add the domain into our domain database */
241 if ((d = find_subnet(bcast_ip)) ||
242 (d = make_subnet(bcast_ip, mask_ip)))
244 struct work_record *w = find_workgroupstruct(d, name, add);
245 extern pstring ServerComment;
247 if (!w) return NULL;
249 /* add WORKGROUP(1e) and WORKGROUP(00) entries into name database
250 or register with WINS server, if it's our workgroup */
251 if (strequal(lp_workgroup(), name) && d->my_interface)
253 add_my_name_entry(d,name,0x1e,NB_ACTIVE|NB_GROUP);
254 add_my_name_entry(d,name,0x0 ,NB_ACTIVE|NB_GROUP);
256 /* add samba server name to workgroup list */
257 if ((strequal(lp_workgroup(), name) && d->my_interface) || lmhosts)
259 add_server_entry(d,w,myname,w->ServerType,0,ServerComment,True);
262 DEBUG(3,("Added domain name entry %s at %s\n", name,inet_ntoa(bcast_ip)));
263 return d;
265 return NULL;
269 /*******************************************************************
270 write out browse.dat
271 ******************************************************************/
272 void write_browse_list(void)
274 struct subnet_record *d;
275 pstring fname,fnamenew;
276 FILE *f;
278 static time_t lasttime = 0;
279 time_t t = time(NULL);
281 if (!lasttime) lasttime = t;
282 if (!updatedlists || t - lasttime < 5) return;
284 lasttime = t;
285 updatedlists = False;
286 updatecount++;
288 dump_names();
289 dump_workgroups();
291 strcpy(fname,lp_lockdir());
292 trim_string(fname,NULL,"/");
293 strcat(fname,"/");
294 strcat(fname,SERVER_LIST);
295 strcpy(fnamenew,fname);
296 strcat(fnamenew,".");
298 f = fopen(fnamenew,"w");
300 if (!f)
302 DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
303 return;
306 for (d = subnetlist; d ; d = d->next)
308 struct work_record *work;
309 for (work = d->workgrouplist; work ; work = work->next)
311 struct server_record *s;
312 for (s = work->serverlist; s ; s = s->next)
314 fstring tmp;
316 /* don't list domains I don't have a master for */
317 if ((s->serv.type & SV_TYPE_DOMAIN_ENUM) && !s->serv.comment[0])
319 continue;
322 /* output server details, plus what workgroup/domain
323 they're in. without the domain information, the
324 combined list of all servers in all workgroups gets
325 sent to anyone asking about any workgroup! */
327 sprintf(tmp, "\"%s\"", s->serv.name);
328 fprintf(f, "%-25s ", tmp);
329 fprintf(f, "%08x ", s->serv.type);
330 sprintf(tmp, "\"%s\" ", s->serv.comment);
331 fprintf(f, "%-30s", tmp);
332 fprintf(f, "\"%s\"\n", work->work_group);
337 fclose(f);
338 unlink(fname);
339 chmod(fnamenew,0644);
340 rename(fnamenew,fname);
341 DEBUG(3,("Wrote browse list %s\n",fname));