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
;
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
;
70 for (d2
= subnetlist
; d2
->next
; d2
= d2
->next
);
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
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
))
99 else if (same_net(bcast_ip
, d
->bcast_ip
, d
->mask_ip
))
101 if (!ip_equal(d
->bcast_ip
, wins_ip
))
112 /****************************************************************************
113 finds the appropriate subnet structure. directed packets (non-bcast) are
114 assumed to come from a point-to-point (P or M node), and so the subnet we
115 return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
116 ****************************************************************************/
117 struct subnet_record
*find_req_subnet(struct in_addr ip
, BOOL bcast
)
121 /* identify the subnet the broadcast request came from */
122 return find_subnet(*iface_bcast(ip
));
124 /* find the subnet under the pseudo-ip of 255.255.255.255 */
125 return find_subnet(ipgrp
);
129 /****************************************************************************
130 create a domain entry
131 ****************************************************************************/
132 static struct subnet_record
*make_subnet(struct in_addr bcast_ip
, struct in_addr mask_ip
)
134 struct subnet_record
*d
;
135 d
= (struct subnet_record
*)malloc(sizeof(*d
));
137 if (!d
) return(NULL
);
139 bzero((char *)d
,sizeof(*d
));
141 DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip
)));
142 DEBUG(4, ("%s\n", inet_ntoa(mask_ip
)));
144 d
->bcast_ip
= bcast_ip
;
145 d
->mask_ip
= mask_ip
;
146 d
->workgrouplist
= NULL
;
154 /****************************************************************************
155 add the remote interfaces from lp_interfaces()
156 to the netbios subnet database.
157 ****************************************************************************/
158 void add_subnet_interfaces(void)
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 make_subnet(i
->bcast
,i
->nmask
);
172 /* add the pseudo-ip interface for WINS: 255.255.255.255 */
173 if (lp_wins_support() || (*lp_wins_server()))
175 struct in_addr wins_bcast
= ipgrp
;
176 struct in_addr wins_nmask
= ipzero
;
177 make_subnet(wins_bcast
, wins_nmask
);
183 /****************************************************************************
184 add the default workgroup into my domain
185 **************************************************************************/
186 void add_my_subnets(char *group
)
190 /* add or find domain on our local subnet, in the default workgroup */
192 if (*group
== '*') return;
194 /* the coding choice is up to you, andrew: i can see why you don't want
195 global access to the local_interfaces structure: so it can't get
197 for (i
= local_interfaces
; i
; i
= i
->next
)
199 add_subnet_entry(i
->bcast
,i
->nmask
,group
, True
, False
);
204 /****************************************************************************
205 add a domain entry. creates a workgroup, if necessary, and adds the domain
206 to the named a workgroup.
207 ****************************************************************************/
208 struct subnet_record
*add_subnet_entry(struct in_addr bcast_ip
,
209 struct in_addr mask_ip
,
210 char *name
, BOOL add
, BOOL lmhosts
)
212 struct subnet_record
*d
;
214 /* XXXX andrew: struct in_addr ip appears not to be referenced at all except
215 in the DEBUG comment. i assume that the DEBUG comment below actually
216 intends to refer to bcast_ip? i don't know.
218 struct in_addr ip = ipgrp;
222 if (zero_ip(bcast_ip
))
223 bcast_ip
= *iface_bcast(bcast_ip
);
225 /* add the domain into our domain database */
226 if ((d
= find_subnet(bcast_ip
)) ||
227 (d
= make_subnet(bcast_ip
, mask_ip
)))
229 struct work_record
*w
= find_workgroupstruct(d
, name
, add
);
233 /* add WORKGROUP(1e) and WORKGROUP(00) entries into name database
234 or register with WINS server, if it's our workgroup */
235 if (strequal(lp_workgroup(), name
))
237 add_my_name_entry(d
,name
,0x1e,nb_type
|NB_ACTIVE
|NB_GROUP
);
238 add_my_name_entry(d
,name
,0x0 ,nb_type
|NB_ACTIVE
|NB_GROUP
);
240 /* add samba server name to workgroup list. don't add
241 lmhosts server entries to local interfaces */
242 if (strequal(lp_workgroup(), name
))
244 add_server_entry(d
,w
,myname
,w
->ServerType
,0,lp_serverstring(),True
);
245 DEBUG(3,("Added server name entry %s at %s\n",
246 name
,inet_ntoa(bcast_ip
)));
255 /*******************************************************************
257 ******************************************************************/
258 void write_browse_list(time_t t
)
260 struct subnet_record
*d
;
261 pstring fname
,fnamenew
;
264 static time_t lasttime
= 0;
266 if (!lasttime
) lasttime
= t
;
267 if (!updatedlists
|| t
- lasttime
< 5) return;
270 updatedlists
= False
;
276 strcpy(fname
,lp_lockdir());
277 trim_string(fname
,NULL
,"/");
279 strcat(fname
,SERVER_LIST
);
280 strcpy(fnamenew
,fname
);
281 strcat(fnamenew
,".");
283 f
= fopen(fnamenew
,"w");
287 DEBUG(4,("Can't open %s - %s\n",fnamenew
,strerror(errno
)));
291 for (d
= subnetlist
; d
; d
= d
->next
)
293 struct work_record
*work
;
294 for (work
= d
->workgrouplist
; work
; work
= work
->next
)
296 struct server_record
*s
;
297 for (s
= work
->serverlist
; s
; s
= s
->next
)
301 /* don't list domains I don't have a master for */
302 if ((s
->serv
.type
& SV_TYPE_DOMAIN_ENUM
) && !s
->serv
.comment
[0])
307 /* output server details, plus what workgroup/domain
308 they're in. without the domain information, the
309 combined list of all servers in all workgroups gets
310 sent to anyone asking about any workgroup! */
312 sprintf(tmp
, "\"%s\"", s
->serv
.name
);
313 fprintf(f
, "%-25s ", tmp
);
314 fprintf(f
, "%08x ", s
->serv
.type
);
315 sprintf(tmp
, "\"%s\" ", s
->serv
.comment
);
316 fprintf(f
, "%-30s", tmp
);
317 fprintf(f
, "\"%s\"\n", work
->work_group
);
324 chmod(fnamenew
,0644);
325 rename(fnamenew
,fname
);
326 DEBUG(3,("Wrote browse list %s\n",fname
));