Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : NBT netbios routines and daemon - version 2
4 : Copyright (C) Andrew Tridgell 1994-1998
5 : Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6 : Copyright (C) Jeremy Allison 1994-1998
7 : Copyright (C) Christopher R. Hertel 1998
8 :
9 : This program is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation; either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program. If not, see <http://www.gnu.org/licenses/>.
21 :
22 : */
23 : /* -------------------------------------------------------------------------- **
24 : * Modified July 1998 by CRH.
25 : * I converted this module to use the canned doubly-linked lists. I also
26 : * added comments above the functions where possible.
27 : */
28 :
29 : #include "includes.h"
30 : #include "nmbd/nmbd.h"
31 : #include "lib/util/string_wrappers.h"
32 :
33 : /* -------------------------------------------------------------------------- **
34 : * Variables...
35 : *
36 : * lmb_browserlist - This is our local master browser list.
37 : */
38 :
39 : struct browse_cache_record *lmb_browserlist;
40 :
41 : /* -------------------------------------------------------------------------- **
42 : * Functions...
43 : */
44 :
45 : /* ************************************************************************** **
46 : * Remove and free a browser list entry.
47 : *
48 : * Input: browc - A pointer to the entry to be removed from the list and
49 : * freed.
50 : * Output: none.
51 : *
52 : * ************************************************************************** **
53 : */
54 0 : static void remove_lmb_browser_entry( struct browse_cache_record *browc )
55 : {
56 0 : DLIST_REMOVE(lmb_browserlist, browc);
57 0 : SAFE_FREE(browc);
58 0 : }
59 :
60 : /* ************************************************************************** **
61 : * Update a browser death time.
62 : *
63 : * Input: browc - Pointer to the entry to be updated.
64 : * Output: none.
65 : *
66 : * ************************************************************************** **
67 : */
68 0 : void update_browser_death_time( struct browse_cache_record *browc )
69 : {
70 : /* Allow the new lmb to miss an announce period before we remove it. */
71 0 : browc->death_time = time(NULL) + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
72 0 : }
73 :
74 : /* ************************************************************************** **
75 : * Create a browser entry and add it to the local master browser list.
76 : *
77 : * Input: work_name
78 : * browser_name
79 : * ip
80 : *
81 : * Output: Pointer to the new entry, or NULL if malloc() failed.
82 : *
83 : * ************************************************************************** **
84 : */
85 0 : struct browse_cache_record *create_browser_in_lmb_cache( const char *work_name,
86 : const char *browser_name,
87 : struct in_addr ip )
88 : {
89 : struct browse_cache_record *browc;
90 0 : time_t now = time( NULL );
91 :
92 0 : browc = SMB_MALLOC_P(struct browse_cache_record);
93 :
94 0 : if( NULL == browc ) {
95 0 : DEBUG( 0, ("create_browser_in_lmb_cache: malloc fail !\n") );
96 0 : return( NULL );
97 : }
98 :
99 0 : memset( (char *)browc, '\0', sizeof( *browc ) );
100 :
101 : /* For a new lmb entry we want to sync with it after one minute. This
102 : will allow it time to send out a local announce and build its
103 : browse list.
104 : */
105 :
106 0 : browc->sync_time = now + 60;
107 :
108 : /* Allow the new lmb to miss an announce period before we remove it. */
109 0 : browc->death_time = now + ( (CHECK_TIME_MST_ANNOUNCE + 2) * 60 );
110 :
111 0 : unstrcpy( browc->lmb_name, browser_name);
112 0 : unstrcpy( browc->work_group, work_name);
113 0 : if (!strupper_m( browc->lmb_name )) {
114 0 : SAFE_FREE(browc);
115 0 : return NULL;
116 : }
117 0 : if (!strupper_m( browc->work_group )) {
118 0 : SAFE_FREE(browc);
119 0 : return NULL;
120 : }
121 :
122 0 : browc->ip = ip;
123 :
124 0 : DLIST_ADD_END(lmb_browserlist, browc);
125 :
126 0 : DEBUG(3, ("nmbd_browserdb:create_browser_in_lmb_cache()\n"));
127 0 : DEBUGADD(3, (" Added lmb cache entry for workgroup %s name %s IP %s "
128 : "ttl %d\n", browc->work_group, browc->lmb_name,
129 : inet_ntoa(ip), (int)browc->death_time));
130 :
131 0 : return( browc );
132 : }
133 :
134 : /* ************************************************************************** **
135 : * Find a browser entry in the local master browser list.
136 : *
137 : * Input: browser_name - The name for which to search.
138 : *
139 : * Output: A pointer to the matching entry, or NULL if no match was found.
140 : *
141 : * ************************************************************************** **
142 : */
143 0 : struct browse_cache_record *find_browser_in_lmb_cache( const char *browser_name )
144 : {
145 : struct browse_cache_record *browc;
146 :
147 0 : for( browc = lmb_browserlist; browc; browc = browc->next ) {
148 0 : if( strequal( browser_name, browc->lmb_name ) ) {
149 0 : break;
150 : }
151 : }
152 :
153 0 : return browc;
154 : }
155 :
156 : /* ************************************************************************** **
157 : * Expire timed out browsers in the browserlist.
158 : *
159 : * Input: t - Expiration time. Entries with death times less than this
160 : * value will be removed from the list.
161 : * Output: none.
162 : *
163 : * ************************************************************************** **
164 : */
165 181 : void expire_lmb_browsers( time_t t )
166 : {
167 : struct browse_cache_record *browc;
168 : struct browse_cache_record *nextbrowc;
169 :
170 181 : for( browc = lmb_browserlist; browc; browc = nextbrowc) {
171 0 : nextbrowc = browc->next;
172 :
173 0 : if( browc->death_time < t ) {
174 0 : DEBUG(3, ("nmbd_browserdb:expire_lmb_browsers()\n"));
175 0 : DEBUGADD(3, (" Removing timed out lmb entry %s\n",
176 : browc->lmb_name));
177 0 : remove_lmb_browser_entry( browc );
178 : }
179 : }
180 181 : }
|