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 wins_ip
;
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 /* Forward references. */
56 static struct subnet_record
*add_subnet_entry(struct in_addr bcast_ip
,
57 struct in_addr mask_ip
,
58 char *name
, BOOL add
, BOOL lmhosts
);
60 /****************************************************************************
61 add a domain into the list
62 **************************************************************************/
63 static void add_subnet(struct subnet_record
*d
)
65 struct subnet_record
*d2
;
75 for (d2
= subnetlist
; d2
->next
; d2
= d2
->next
);
83 /****************************************************************************
84 find a subnet in the subnetlist
85 **************************************************************************/
86 struct subnet_record
*find_subnet(struct in_addr bcast_ip
)
88 struct subnet_record
*d
;
90 /* search through subnet list for broadcast/netmask that matches
91 the source ip address. a subnet 255.255.255.255 represents the
94 for (d
= subnetlist
; d
; d
= d
->next
)
96 if (ip_equal(bcast_ip
, wins_ip
))
98 if (ip_equal(bcast_ip
, d
->bcast_ip
))
103 else if (same_net(bcast_ip
, d
->bcast_ip
, d
->mask_ip
))
105 if (!ip_equal(d
->bcast_ip
, wins_ip
))
116 /****************************************************************************
117 finds the appropriate subnet structure. directed packets (non-bcast) are
118 assumed to come from a point-to-point (P or M node), and so the subnet we
119 return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
120 ****************************************************************************/
121 struct subnet_record
*find_req_subnet(struct in_addr ip
, BOOL bcast
)
125 /* identify the subnet the broadcast request came from */
126 return find_subnet(*iface_bcast(ip
));
128 /* find the subnet under the pseudo-ip of 255.255.255.255 */
129 return find_subnet(wins_ip
);
132 /****************************************************************************
133 find a subnet in the subnetlist - if the subnet is not found
134 then return the WINS subnet.
135 **************************************************************************/
136 struct subnet_record
*find_subnet_all(struct in_addr bcast_ip
)
138 struct subnet_record
*d
= find_subnet(bcast_ip
);
140 return find_subnet( wins_ip
);
143 /****************************************************************************
144 create a domain entry
145 ****************************************************************************/
146 static struct subnet_record
*make_subnet(struct in_addr bcast_ip
, struct in_addr mask_ip
)
148 struct subnet_record
*d
;
149 d
= (struct subnet_record
*)malloc(sizeof(*d
));
151 if (!d
) return(NULL
);
153 bzero((char *)d
,sizeof(*d
));
155 DEBUG(4, ("making subnet %s ", inet_ntoa(bcast_ip
)));
156 DEBUG(4, ("%s\n", inet_ntoa(mask_ip
)));
158 d
->bcast_ip
= bcast_ip
;
159 d
->mask_ip
= mask_ip
;
160 d
->workgrouplist
= NULL
;
168 /****************************************************************************
169 add the remote interfaces from lp_interfaces()
170 to the netbios subnet database.
171 ****************************************************************************/
172 void add_subnet_interfaces(void)
176 /* loop on all local interfaces */
177 for (i
= local_interfaces
; i
; i
= i
->next
)
179 /* add the interface into our subnet database */
180 if (!find_subnet(i
->bcast
))
182 make_subnet(i
->bcast
,i
->nmask
);
186 /* add the pseudo-ip interface for WINS: 255.255.255.255 */
187 if (lp_wins_support() || (*lp_wins_server()))
189 struct in_addr wins_bcast
= wins_ip
;
190 struct in_addr wins_nmask
= ipzero
;
191 make_subnet(wins_bcast
, wins_nmask
);
197 /****************************************************************************
198 add the default workgroup into my domain
199 **************************************************************************/
200 void add_my_subnets(char *group
)
204 /* add or find domain on our local subnet, in the default workgroup */
206 if (*group
== '*') return;
208 /* the coding choice is up to you, andrew: i can see why you don't want
209 global access to the local_interfaces structure: so it can't get
211 for (i
= local_interfaces
; i
; i
= i
->next
)
213 add_subnet_entry(i
->bcast
,i
->nmask
,group
, True
, False
);
218 /****************************************************************************
219 add a domain entry. creates a workgroup, if necessary, and adds the domain
220 to the named a workgroup.
221 ****************************************************************************/
222 static struct subnet_record
*add_subnet_entry(struct in_addr bcast_ip
,
223 struct in_addr mask_ip
,
224 char *name
, BOOL add
, BOOL lmhosts
)
226 struct subnet_record
*d
;
228 /* XXXX andrew: struct in_addr ip appears not to be referenced at all except
229 in the DEBUG comment. i assume that the DEBUG comment below actually
230 intends to refer to bcast_ip? i don't know.
232 struct in_addr ip = wins_ip;
236 if (zero_ip(bcast_ip
))
237 bcast_ip
= *iface_bcast(bcast_ip
);
239 /* add the domain into our domain database */
240 /* Note that we never add into the WINS subnet as add_subnet_entry
241 is only called to add our local interfaces. */
242 if ((d
= find_subnet(bcast_ip
)) ||
243 (d
= make_subnet(bcast_ip
, mask_ip
)))
245 struct work_record
*w
= find_workgroupstruct(d
, name
, add
);
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
))
253 add_my_name_entry(d
,name
,0x1e,nb_type
|NB_ACTIVE
|NB_GROUP
);
254 add_my_name_entry(d
,name
,0x0 ,nb_type
|NB_ACTIVE
|NB_GROUP
);
256 /* add samba server name to workgroup list. don't add
257 lmhosts server entries to local interfaces */
258 if (strequal(lp_workgroup(), name
))
260 add_server_entry(d
,w
,myname
,w
->ServerType
,0,lp_serverstring(),True
);
261 DEBUG(3,("Added server name entry %s at %s\n",
262 name
,inet_ntoa(bcast_ip
)));
271 /*******************************************************************
273 ******************************************************************/
274 void write_browse_list(time_t t
)
276 struct subnet_record
*d
;
277 pstring fname
,fnamenew
;
280 static time_t lasttime
= 0;
282 if (!lasttime
) lasttime
= t
;
283 if (!updatedlists
|| t
- lasttime
< 5) return;
286 updatedlists
= False
;
292 strcpy(fname
,lp_lockdir());
293 trim_string(fname
,NULL
,"/");
295 strcat(fname
,SERVER_LIST
);
296 strcpy(fnamenew
,fname
);
297 strcat(fnamenew
,".");
299 f
= fopen(fnamenew
,"w");
303 DEBUG(4,("Can't open %s - %s\n",fnamenew
,strerror(errno
)));
307 for (d
= subnetlist
; d
; d
= d
->next
)
309 struct work_record
*work
;
310 for (work
= d
->workgrouplist
; work
; work
= work
->next
)
312 struct server_record
*s
;
313 for (s
= work
->serverlist
; s
; s
= s
->next
)
317 /* don't list domains I don't have a master for */
318 if ((s
->serv
.type
& SV_TYPE_DOMAIN_ENUM
) && !s
->serv
.comment
[0])
323 /* output server details, plus what workgroup/domain
324 they're in. without the domain information, the
325 combined list of all servers in all workgroups gets
326 sent to anyone asking about any workgroup! */
328 sprintf(tmp
, "\"%s\"", s
->serv
.name
);
329 fprintf(f
, "%-25s ", tmp
);
330 fprintf(f
, "%08x ", s
->serv
.type
);
331 sprintf(tmp
, "\"%s\" ", s
->serv
.comment
);
332 fprintf(f
, "%-30s", tmp
);
333 fprintf(f
, "\"%s\"\n", work
->work_group
);
340 chmod(fnamenew
,0644);
341 rename(fnamenew
,fname
);
342 DEBUG(3,("Wrote browse list %s\n",fname
));