This commit was manufactured by cvs2svn to create tag
[Samba.git] / source / namedbsubnet.c
blob0bad79246ad7a968a590080c651f01e3a0492cff
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);
228 if (!w) return NULL;
230 /* add WORKGROUP(1e) and WORKGROUP(00) entries into name database
231 or register with WINS server, if it's our workgroup */
232 if (strequal(lp_workgroup(), name))
234 add_my_name_entry(d,name,0x1e,nb_type|NB_ACTIVE|NB_GROUP);
235 add_my_name_entry(d,name,0x0 ,nb_type|NB_ACTIVE|NB_GROUP);
237 /* add samba server name to workgroup list. don't add
238 lmhosts server entries to local interfaces */
239 if (strequal(lp_workgroup(), name))
241 add_server_entry(d,w,myname,w->ServerType,0,lp_serverstring(),True);
242 DEBUG(3,("Added server name entry %s at %s\n",
243 name,inet_ntoa(bcast_ip)));
246 return d;
248 return NULL;
252 /*******************************************************************
253 write out browse.dat
254 ******************************************************************/
255 void write_browse_list(void)
257 struct subnet_record *d;
258 pstring fname,fnamenew;
259 FILE *f;
261 static time_t lasttime = 0;
262 time_t t = time(NULL);
264 if (!lasttime) lasttime = t;
265 if (!updatedlists || t - lasttime < 5) return;
267 lasttime = t;
268 updatedlists = False;
269 updatecount++;
271 dump_names();
272 dump_workgroups();
274 strcpy(fname,lp_lockdir());
275 trim_string(fname,NULL,"/");
276 strcat(fname,"/");
277 strcat(fname,SERVER_LIST);
278 strcpy(fnamenew,fname);
279 strcat(fnamenew,".");
281 f = fopen(fnamenew,"w");
283 if (!f)
285 DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
286 return;
289 for (d = subnetlist; d ; d = d->next)
291 struct work_record *work;
292 for (work = d->workgrouplist; work ; work = work->next)
294 struct server_record *s;
295 for (s = work->serverlist; s ; s = s->next)
297 fstring tmp;
299 /* don't list domains I don't have a master for */
300 if ((s->serv.type & SV_TYPE_DOMAIN_ENUM) && !s->serv.comment[0])
302 continue;
305 /* output server details, plus what workgroup/domain
306 they're in. without the domain information, the
307 combined list of all servers in all workgroups gets
308 sent to anyone asking about any workgroup! */
310 sprintf(tmp, "\"%s\"", s->serv.name);
311 fprintf(f, "%-25s ", tmp);
312 fprintf(f, "%08x ", s->serv.type);
313 sprintf(tmp, "\"%s\" ", s->serv.comment);
314 fprintf(f, "%-30s", tmp);
315 fprintf(f, "\"%s\"\n", work->work_group);
320 fclose(f);
321 unlink(fname);
322 chmod(fnamenew,0644);
323 rename(fnamenew,fname);
324 DEBUG(3,("Wrote browse list %s\n",fname));