updating documentation to reflect code a little bit.
[Samba.git] / source / namedbsubnet.c
blob5c683e5e49bfbcdb86826da8422d42dae4701907
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 /* this is our domain/workgroup/server database */
51 struct subnet_record *subnetlist = NULL;
53 extern uint16 nb_type; /* samba's NetBIOS name type */
55 /****************************************************************************
56 add a domain into the list
57 **************************************************************************/
58 static void add_subnet(struct subnet_record *d)
60 struct subnet_record *d2;
62 if (!subnetlist)
64 subnetlist = d;
65 d->prev = NULL;
66 d->next = NULL;
67 return;
70 for (d2 = subnetlist; d2->next; d2 = d2->next);
72 d2->next = d;
73 d->next = NULL;
74 d->prev = d2;
78 /****************************************************************************
79 find a subnet in the subnetlist
80 **************************************************************************/
81 struct subnet_record *find_subnet(struct in_addr bcast_ip)
83 struct subnet_record *d;
84 struct in_addr wins_ip = ipgrp;
86 /* search through subnet list for broadcast/netmask that matches
87 the source ip address. a subnet 255.255.255.255 represents the
88 WINS list. */
90 for (d = subnetlist; d; d = d->next)
92 if (ip_equal(bcast_ip, wins_ip))
94 if (ip_equal(bcast_ip, d->bcast_ip))
96 return d;
99 else if (same_net(bcast_ip, d->bcast_ip, d->mask_ip))
101 return(d);
105 return (NULL);
109 /****************************************************************************
110 finds the appropriate subnet structure. directed packets (non-bcast) are
111 assumed to come from a point-to-point (P or M node), and so the subnet we
112 return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
113 ****************************************************************************/
114 struct subnet_record *find_req_subnet(struct in_addr ip, BOOL bcast)
116 if (bcast)
118 /* identify the subnet the broadcast request came from */
119 return find_subnet(*iface_bcast(ip));
121 /* find the subnet under the pseudo-ip of 255.255.255.255 */
122 return find_subnet(ipgrp);
126 /****************************************************************************
127 create a domain entry
128 ****************************************************************************/
129 static struct subnet_record *make_subnet(struct in_addr bcast_ip, struct in_addr mask_ip)
131 struct subnet_record *d;
132 d = (struct subnet_record *)malloc(sizeof(*d));
134 if (!d) return(NULL);
136 bzero((char *)d,sizeof(*d));
138 DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip)));
139 DEBUG(4, ("%s\n", inet_ntoa(mask_ip)));
141 d->bcast_ip = bcast_ip;
142 d->mask_ip = mask_ip;
143 d->workgrouplist = NULL;
145 add_subnet(d);
147 return d;
151 /****************************************************************************
152 add the remote interfaces from lp_interfaces()
153 to the netbios subnet database.
154 ****************************************************************************/
155 void add_subnet_interfaces(void)
157 struct interface *i;
159 /* loop on all local interfaces */
160 for (i = local_interfaces; i; i = i->next)
162 /* add the interface into our subnet database */
163 if (!find_subnet(i->bcast))
165 make_subnet(i->bcast,i->nmask);
169 /* add the pseudo-ip interface for WINS: 255.255.255.255 */
170 if (lp_wins_support() || (*lp_wins_server()))
172 struct in_addr wins_bcast = ipgrp;
173 struct in_addr wins_nmask = ipzero;
174 make_subnet(wins_bcast, wins_nmask);
180 /****************************************************************************
181 add the default workgroup into my domain
182 **************************************************************************/
183 void add_my_subnets(char *group)
185 struct interface *i;
187 /* add or find domain on our local subnet, in the default workgroup */
189 if (*group == '*') return;
191 /* the coding choice is up to you, andrew: i can see why you don't want
192 global access to the local_interfaces structure: so it can't get
193 messed up! */
194 for (i = local_interfaces; i; i = i->next)
196 add_subnet_entry(i->bcast,i->nmask,group, True, False);
201 /****************************************************************************
202 add a domain entry. creates a workgroup, if necessary, and adds the domain
203 to the named a workgroup.
204 ****************************************************************************/
205 struct subnet_record *add_subnet_entry(struct in_addr bcast_ip,
206 struct in_addr mask_ip,
207 char *name, BOOL add, BOOL lmhosts)
209 struct subnet_record *d;
211 /* XXXX andrew: struct in_addr ip appears not to be referenced at all except
212 in the DEBUG comment. i assume that the DEBUG comment below actually
213 intends to refer to bcast_ip? i don't know.
215 struct in_addr ip = ipgrp;
219 if (zero_ip(bcast_ip))
220 bcast_ip = *iface_bcast(bcast_ip);
222 /* add the domain into our domain database */
223 if ((d = find_subnet(bcast_ip)) ||
224 (d = make_subnet(bcast_ip, mask_ip)))
226 struct work_record *w = find_workgroupstruct(d, name, add);
227 extern pstring ServerComment;
229 if (!w) return NULL;
231 /* add WORKGROUP(1e) and WORKGROUP(00) entries into name database
232 or register with WINS server, if it's our workgroup */
233 if (strequal(lp_workgroup(), name))
235 add_my_name_entry(d,name,0x1e,nb_type|NB_ACTIVE|NB_GROUP);
236 add_my_name_entry(d,name,0x0 ,nb_type|NB_ACTIVE|NB_GROUP);
238 /* add samba server name to workgroup list. don't add
239 lmhosts server entries to local interfaces */
240 if (strequal(lp_workgroup(), name))
242 add_server_entry(d,w,myname,w->ServerType,0,ServerComment,True);
243 DEBUG(3,("Added server name entry %s at %s\n",
244 name,inet_ntoa(bcast_ip)));
247 return d;
249 return NULL;
253 /*******************************************************************
254 write out browse.dat
255 ******************************************************************/
256 void write_browse_list(void)
258 struct subnet_record *d;
259 pstring fname,fnamenew;
260 FILE *f;
262 static time_t lasttime = 0;
263 time_t t = time(NULL);
265 if (!lasttime) lasttime = t;
266 if (!updatedlists || t - lasttime < 5) return;
268 lasttime = t;
269 updatedlists = False;
270 updatecount++;
272 dump_names();
273 dump_workgroups();
275 strcpy(fname,lp_lockdir());
276 trim_string(fname,NULL,"/");
277 strcat(fname,"/");
278 strcat(fname,SERVER_LIST);
279 strcpy(fnamenew,fname);
280 strcat(fnamenew,".");
282 f = fopen(fnamenew,"w");
284 if (!f)
286 DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
287 return;
290 for (d = subnetlist; d ; d = d->next)
292 struct work_record *work;
293 for (work = d->workgrouplist; work ; work = work->next)
295 struct server_record *s;
296 for (s = work->serverlist; s ; s = s->next)
298 fstring tmp;
300 /* don't list domains I don't have a master for */
301 if ((s->serv.type & SV_TYPE_DOMAIN_ENUM) && !s->serv.comment[0])
303 continue;
306 /* output server details, plus what workgroup/domain
307 they're in. without the domain information, the
308 combined list of all servers in all workgroups gets
309 sent to anyone asking about any workgroup! */
311 sprintf(tmp, "\"%s\"", s->serv.name);
312 fprintf(f, "%-25s ", tmp);
313 fprintf(f, "%08x ", s->serv.type);
314 sprintf(tmp, "\"%s\" ", s->serv.comment);
315 fprintf(f, "%-30s", tmp);
316 fprintf(f, "\"%s\"\n", work->work_group);
321 fclose(f);
322 unlink(fname);
323 chmod(fnamenew,0644);
324 rename(fnamenew,fname);
325 DEBUG(3,("Wrote browse list %s\n",fname));