2 Unix SMB/Netbios implementation.
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.
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
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;
47 /* local interfaces structure */
48 extern struct interface *local_interfaces;
50 /* this is our domain/workgroup/server database */
51 struct subnet_record *subnetlist = NULL;
54 /****************************************************************************
55 add a domain into the list
56 **************************************************************************/
57 static void add_subnet(struct subnet_record *d)
59 struct subnet_record *d2;
69 for (d2 = subnetlist; d2->next; d2 = d2->next);
77 /****************************************************************************
78 find a subnet in the subnetlist
79 **************************************************************************/
80 struct subnet_record *find_subnet(struct in_addr bcast_ip)
82 struct subnet_record *d;
83 struct in_addr wins_ip = ipgrp;
85 /* search through subnet list for broadcast/netmask that matches
86 the source ip address. a subnet 255.255.255.255 represents the
89 for (d = subnetlist; d; d = d->next)
91 if (ip_equal(bcast_ip, wins_ip))
93 if (ip_equal(bcast_ip, d->bcast_ip))
98 else if (same_net(bcast_ip, d->bcast_ip, d->mask_ip))
108 /****************************************************************************
109 finds the appropriate subnet structure. directed packets (non-bcast) are
110 assumed to come from a point-to-point (P or M node), and so the subnet we
111 return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
112 ****************************************************************************/
113 struct subnet_record *find_req_subnet(struct in_addr ip, BOOL bcast)
117 /* identify the subnet the broadcast request came from */
118 return find_subnet(*iface_bcast(ip));
120 /* find the subnet under the pseudo-ip of 255.255.255.255 */
121 return find_subnet(ipgrp);
125 /****************************************************************************
126 create a domain entry
127 ****************************************************************************/
128 static struct subnet_record *make_subnet(struct in_addr bcast_ip, struct in_addr mask_ip)
130 struct subnet_record *d;
131 d = (struct subnet_record *)malloc(sizeof(*d));
133 if (!d) return(NULL);
135 bzero((char *)d,sizeof(*d));
137 DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip)));
138 DEBUG(4, ("%s\n", inet_ntoa(mask_ip)));
140 d->bcast_ip = bcast_ip;
141 d->mask_ip = mask_ip;
142 d->workgrouplist = NULL;
150 /****************************************************************************
151 add the remote interfaces from lp_interfaces()
152 to the netbios subnet database.
153 ****************************************************************************/
154 void add_subnet_interfaces(void)
158 /* loop on all local interfaces */
159 for (i = local_interfaces; i; i = i->next)
161 /* add the interface into our subnet database */
162 if (!find_subnet(i->bcast))
164 make_subnet(i->bcast,i->nmask);
168 /* add the pseudo-ip interface for WINS: 255.255.255.255 */
169 if (lp_wins_support())
171 struct in_addr wins_bcast = ipgrp;
172 struct in_addr wins_nmask = ipzero;
173 make_subnet(wins_bcast, wins_nmask);
179 /****************************************************************************
180 add the default workgroup into my domain
181 **************************************************************************/
182 void add_my_subnets(char *group)
186 /* add or find domain on our local subnet, in the default workgroup */
188 if (*group == '*') return;
190 /* the coding choice is up to you, andrew: i can see why you don't want
191 global access to the local_interfaces structure: so it can't get
193 for (i = local_interfaces; i; i = i->next)
195 add_subnet_entry(i->bcast,i->nmask,group, True, False);
200 /****************************************************************************
201 add a domain entry. creates a workgroup, if necessary, and adds the domain
202 to the named a workgroup.
203 ****************************************************************************/
204 struct subnet_record *add_subnet_entry(struct in_addr bcast_ip,
205 struct in_addr mask_ip,
206 char *name, BOOL add, BOOL lmhosts)
208 struct subnet_record *d;
210 /* XXXX andrew: struct in_addr ip appears not to be referenced at all except
211 in the DEBUG comment. i assume that the DEBUG comment below actually
212 intends to refer to bcast_ip? i don't know.
214 struct in_addr ip = ipgrp;
218 if (zero_ip(bcast_ip))
219 bcast_ip = *iface_bcast(bcast_ip);
221 /* add the domain into our domain database */
222 if ((d = find_subnet(bcast_ip)) ||
223 (d = make_subnet(bcast_ip, mask_ip)))
225 struct work_record *w = find_workgroupstruct(d, name, add);
226 extern pstring ServerComment;
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_ACTIVE|NB_GROUP);
235 add_my_name_entry(d,name,0x0 ,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,ServerComment,True);
242 DEBUG(3,("Added server name entry %s at %s\n",
243 name,inet_ntoa(bcast_ip)));
252 /*******************************************************************
254 ******************************************************************/
255 void write_browse_list(void)
257 struct subnet_record *d;
258 pstring fname,fnamenew;
261 static time_t lasttime = 0;
262 time_t t = time(NULL);
264 if (!lasttime) lasttime = t;
265 if (!updatedlists || t - lasttime < 5) return;
268 updatedlists = False;
274 strcpy(fname,lp_lockdir());
275 trim_string(fname,NULL,"/");
277 strcat(fname,SERVER_LIST);
278 strcpy(fnamenew,fname);
279 strcat(fnamenew,".");
281 f = fopen(fnamenew,"w");
285 DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
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)
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])
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);
322 chmod(fnamenew,0644);
323 rename(fnamenew,fname);
324 DEBUG(3,("Wrote browse list %s\n",fname));