2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1995
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
33 extern int DEBUGLEVEL
;
35 extern struct in_addr wins_ip
;
37 /* this is our browse master/backup cache database */
38 static struct browse_cache_record
*browserlist
= NULL
;
41 /***************************************************************************
42 add a browser into the list
43 **************************************************************************/
44 static void add_browse_cache(struct browse_cache_record
*b
)
46 struct browse_cache_record
*b2
;
56 for (b2
= browserlist
; b2
->next
; b2
= b2
->next
) ;
64 /*******************************************************************
65 remove old browse entries
66 ******************************************************************/
67 void expire_browse_cache(time_t t
)
69 struct browse_cache_record
*b
;
70 struct browse_cache_record
*nextb
;
72 /* expire old entries in the serverlist */
73 for (b
= browserlist
; b
; b
= nextb
)
75 if (b
->synced
&& b
->sync_time
< t
)
77 DEBUG(3,("Removing dead cached browser %s\n",b
->name
));
80 if (b
->prev
) b
->prev
->next
= b
->next
;
81 if (b
->next
) b
->next
->prev
= b
->prev
;
83 if (browserlist
== b
) browserlist
= b
->next
;
94 /****************************************************************************
96 ****************************************************************************/
97 struct browse_cache_record
*add_browser_entry(char *name
, int type
, char *wg
,
98 time_t ttl
, struct in_addr ip
, BOOL local
)
102 struct browse_cache_record
*b
;
104 /* search for the entry: if it's already in the cache, update that entry */
105 for (b
= browserlist
; b
; b
= b
->next
)
107 if (ip_equal(ip
,b
->ip
) && strequal(b
->group
, wg
)) break;
112 /* entries get left in the cache for a while. this stops sync'ing too
113 often if the network is large */
114 DEBUG(4, ("browser %s %s %s already sync'd at time %d\n",
115 b
->name
, b
->group
, inet_ntoa(b
->ip
), b
->sync_time
));
122 b
= (struct browse_cache_record
*)malloc(sizeof(*b
));
124 if (!b
) return(NULL
);
126 bzero((char *)b
,sizeof(*b
));
129 /* update the entry */
130 ttl
= time(NULL
)+ttl
;
132 StrnCpy(b
->name
,name
,sizeof(b
->name
)-1);
133 StrnCpy(b
->group
,wg
,sizeof(b
->group
)-1);
139 b
->local
= local
; /* local server list sync or complete sync required */
141 if (newentry
|| ttl
< b
->sync_time
)
149 DEBUG(3,("Added cache entry %s %s(%2x) %s ttl %d\n",
150 wg
, name
, type
, inet_ntoa(ip
),ttl
));
154 DEBUG(3,("Updated cache entry %s %s(%2x) %s ttl %d\n",
155 wg
, name
, type
, inet_ntoa(ip
),ttl
));
162 /****************************************************************************
163 find a server responsible for a workgroup, and sync browse lists
164 **************************************************************************/
165 static void start_sync_browse_entry(struct browse_cache_record
*b
)
167 struct subnet_record
*d
;
168 struct work_record
*work
;
170 /* Look for the workgroup first on the local subnet. If this
171 fails try WINS - we may need to sync with the domain master,
172 or we may be the domain master and need to sync with subnet
176 if (!(d
= find_subnet_all(b
->ip
))) {
177 DEBUG(0, ("start_sync_browse_entry: failed to get a \
178 subnet for a browse cache entry workgroup %s, server %s\n",
183 if (!(work
= find_workgroupstruct(d
, b
->group
, False
))) {
184 DEBUG(0, ("start_sync_browse_entry: failed to get a \
185 workgroup for a browse cache entry workgroup %s, server %s\n",
190 /* only sync if we are a subnet master or domain master - but
191 we sync if we are a master for this workgroup on *any*
192 of our interfaces. */
193 if (AM_MASTER(work
) || AM_DOMMST(work
) || AM_ANY_MASTER(work
)) {
195 DEBUG(4, ("start_sync_browse_entry: Initiating %s sync with %s<0x20>, \
197 b
->local
? "local" : "remote", b
->name
, b
->group
));
199 /* first check whether the server we intend to sync with exists. if it
200 doesn't, the server must have died. o dear. */
202 /* see response_netbios_packet() or expire_netbios_response_entries() */
203 /* We cheat here by using the my_comment field of the response_record
204 struct as the workgroup name we are going to do the sync for.
205 This is because the reply packet doesn't include the workgroup, but
206 we need it when the reply comes back.
208 queue_netbios_packet(d
,ClientNMB
,NMB_QUERY
,
209 b
->local
?NAME_QUERY_SYNC_LOCAL
:NAME_QUERY_SYNC_REMOTE
,
210 b
->name
,0x20,0,0,0,NULL
,b
->group
,
211 False
,False
,b
->ip
,b
->ip
);
218 /****************************************************************************
219 search through browser list for an entry to sync with
220 **************************************************************************/
221 void do_browser_lists(time_t t
)
223 struct browse_cache_record
*b
;
224 static time_t last
= 0;
228 DEBUG(9,("do_browser_lists: returning due to t(%d) - last(%d) < 20\n",
230 return; /* don't do too many of these at once! */
231 /* XXXX equally this period should not be too long
232 the server may die in the intervening gap */
236 /* pick any entry in the list, preferably one whose time is up */
237 for (b
= browserlist
; b
&& b
->next
; b
= b
->next
)
239 if (b
->sync_time
< t
&& b
->synced
== False
) break;
244 /* sync with the selected entry then remove some dead entries */
245 DEBUG(4,("do_browser_lists: Initiating sync with %s, workgroup %s\n",
247 start_sync_browse_entry(b
);
248 expire_browse_cache(t
- 60);
252 DEBUG(9, ("do_browser_lists: no entries to sync.\n"));