r7372: abartet's patch for BUG 2391 (segv caused by free a static pointer)
[Samba/gbeck.git] / source / lib / wins_srv.c
blobc139f427ca89c8e3c6b572e923c4429e6032300d
1 /*
2 Unix SMB/CIFS implementation.
3 Samba wins server helper functions
4 Copyright (C) Andrew Tridgell 1992-2002
5 Copyright (C) Christopher R. Hertel 2000
6 Copyright (C) Tim Potter 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 extern struct in_addr loopback_ip;
28 This is pretty much a complete rewrite of the earlier code. The main
29 aim of the rewrite is to add support for having multiple wins server
30 lists, so Samba can register with multiple groups of wins servers
31 and each group has a failover list of wins servers.
33 Central to the way it all works is the idea of a wins server
34 'tag'. A wins tag is a label for a group of wins servers. For
35 example if you use
37 wins server = fred:192.168.2.10 mary:192.168.3.199 fred:192.168.2.61
39 then you would have two groups of wins servers, one tagged with the
40 name 'fred' and the other with the name 'mary'. I would usually
41 recommend using interface names instead of 'fred' and 'mary' but
42 they can be any alpha string.
44 Now, how does it all work. Well, nmbd needs to register each of its
45 IPs with each of its names once with each group of wins servers. So
46 it tries registering with the first one mentioned in the list, then
47 if that fails it marks that WINS server dead and moves onto the next
48 one.
50 In the client code things are a bit different. As each of the groups
51 of wins servers is a separate name space we need to try each of the
52 groups until we either succeed or we run out of wins servers to
53 try. If we get a negative response from a wins server then that
54 means the name doesn't exist in that group, so we give up on that
55 group and move to the next group. If we don't get a response at all
56 then maybe the wins server is down, in which case we need to
57 failover to the next one for that group.
59 confused yet? (tridge)
62 /* how long a server is marked dead for */
63 #define DEATH_TIME 600
65 /* The list of dead wins servers is stored in gencache.tdb. Each server is
66 marked dead from the point of view of a given source address. We keep a
67 separate dead list for each src address to cope with multiple interfaces
68 that are not routable to each other.
71 #define WINS_SRV_FMT "WINS_SRV_DEAD/%s,%s" /* wins_ip,src_ip */
73 static char *wins_srv_keystr(struct in_addr wins_ip, struct in_addr src_ip)
75 char *keystr = NULL, *wins_ip_addr = NULL, *src_ip_addr = NULL;
77 wins_ip_addr = SMB_STRDUP(inet_ntoa(wins_ip));
78 src_ip_addr = SMB_STRDUP(inet_ntoa(src_ip));
80 if ( !wins_ip_addr || !src_ip_addr ) {
81 DEBUG(0,("wins_srv_keystr: malloc error\n"));
82 goto done;
85 if (asprintf(&keystr, WINS_SRV_FMT, wins_ip_addr, src_ip_addr) == -1) {
86 DEBUG(0, (": ns_srv_keystr: malloc error for key string\n"));
89 done:
90 SAFE_FREE(wins_ip_addr);
91 SAFE_FREE(src_ip_addr);
93 return keystr;
97 see if an ip is on the dead list
100 BOOL wins_srv_is_dead(struct in_addr wins_ip, struct in_addr src_ip)
102 char *keystr = wins_srv_keystr(wins_ip, src_ip);
103 BOOL result;
105 /* If the key exists then the WINS server has been marked as dead */
107 result = gencache_get(keystr, NULL, NULL);
108 SAFE_FREE(keystr);
110 DEBUG(4, ("wins_srv_is_dead: %s is %s\n", inet_ntoa(wins_ip),
111 result ? "dead" : "alive"));
113 return result;
118 mark a wins server as being alive (for the moment)
120 void wins_srv_alive(struct in_addr wins_ip, struct in_addr src_ip)
122 char *keystr = wins_srv_keystr(wins_ip, src_ip);
124 gencache_del(keystr);
125 SAFE_FREE(keystr);
127 DEBUG(4, ("wins_srv_alive: marking wins server %s alive\n",
128 inet_ntoa(wins_ip)));
132 mark a wins server as temporarily dead
134 void wins_srv_died(struct in_addr wins_ip, struct in_addr src_ip)
136 char *keystr;
138 if (is_zero_ip(wins_ip) || wins_srv_is_dead(wins_ip, src_ip))
139 return;
141 keystr = wins_srv_keystr(wins_ip, src_ip);
143 gencache_set(keystr, "DOWN", time(NULL) + DEATH_TIME);
145 SAFE_FREE(keystr);
147 DEBUG(4,("Marking wins server %s dead for %u seconds from source %s\n",
148 inet_ntoa(wins_ip), DEATH_TIME, inet_ntoa(src_ip)));
152 return the total number of wins servers, dead or not
154 unsigned wins_srv_count(void)
156 const char **list;
157 int count = 0;
159 if (lp_wins_support()) {
160 /* simple - just talk to ourselves */
161 return 1;
164 list = lp_wins_server_list();
165 for (count=0; list && list[count]; count++)
166 /* nop */ ;
168 return count;
171 /* an internal convenience structure for an IP with a short string tag
172 attached */
173 struct tagged_ip {
174 fstring tag;
175 struct in_addr ip;
179 parse an IP string that might be in tagged format
180 the result is a tagged_ip structure containing the tag
181 and the ip in in_addr format. If there is no tag then
182 use the tag '*'
184 static void parse_ip(struct tagged_ip *ip, const char *str)
186 char *s = strchr(str, ':');
187 if (!s) {
188 fstrcpy(ip->tag, "*");
189 ip->ip = *interpret_addr2(str);
190 return;
193 ip->ip = *interpret_addr2(s+1);
194 fstrcpy(ip->tag, str);
195 s = strchr(ip->tag, ':');
196 if (s) *s = 0;
202 return the list of wins server tags. A 'tag' is used to distinguish
203 wins server as either belonging to the same name space or a separate
204 name space. Usually you would setup your 'wins server' option to
205 list one or more wins server per interface and use the interface
206 name as your tag, but you are free to use any tag you like.
208 char **wins_srv_tags(void)
210 char **ret = NULL;
211 int count=0, i, j;
212 const char **list;
214 if (lp_wins_support()) {
215 /* give the caller something to chew on. This makes
216 the rest of the logic simpler (ie. less special cases) */
217 ret = SMB_MALLOC_ARRAY(char *, 2);
218 if (!ret) return NULL;
219 ret[0] = SMB_STRDUP("*");
220 ret[1] = NULL;
221 return ret;
224 list = lp_wins_server_list();
225 if (!list)
226 return NULL;
228 /* yes, this is O(n^2) but n is very small */
229 for (i=0;list[i];i++) {
230 struct tagged_ip t_ip;
232 parse_ip(&t_ip, list[i]);
234 /* see if we already have it */
235 for (j=0;j<count;j++) {
236 if (strcmp(ret[j], t_ip.tag) == 0) {
237 break;
241 if (j != count) {
242 /* we already have it. Move along */
243 continue;
246 /* add it to the list */
247 ret = SMB_REALLOC_ARRAY(ret, char *, count+2);
248 ret[count] = SMB_STRDUP(t_ip.tag);
249 if (!ret[count]) break;
250 count++;
253 if (count) {
254 /* make sure we null terminate */
255 ret[count] = NULL;
258 return ret;
261 /* free a list of wins server tags given by wins_srv_tags */
262 void wins_srv_tags_free(char **list)
264 int i;
265 if (!list) return;
266 for (i=0; list[i]; i++) {
267 free(list[i]);
269 free(list);
274 return the IP of the currently active wins server for the given tag,
275 or the zero IP otherwise
277 struct in_addr wins_srv_ip_tag(const char *tag, struct in_addr src_ip)
279 const char **list;
280 int i;
281 struct tagged_ip t_ip;
283 /* if we are a wins server then we always just talk to ourselves */
284 if (lp_wins_support()) {
285 return loopback_ip;
288 list = lp_wins_server_list();
289 if (!list || !list[0]) {
290 struct in_addr ip;
291 zero_ip(&ip);
292 return ip;
295 /* find the first live one for this tag */
296 for (i=0; list[i]; i++) {
297 parse_ip(&t_ip, list[i]);
298 if (strcmp(tag, t_ip.tag) != 0) {
299 /* not for the right tag. Move along */
300 continue;
302 if (!wins_srv_is_dead(t_ip.ip, src_ip)) {
303 fstring src_name;
304 fstrcpy(src_name, inet_ntoa(src_ip));
305 DEBUG(6,("Current wins server for tag '%s' with source %s is %s\n",
306 tag,
307 src_name,
308 inet_ntoa(t_ip.ip)));
309 return t_ip.ip;
313 /* they're all dead - try the first one until they revive */
314 for (i=0; list[i]; i++) {
315 parse_ip(&t_ip, list[i]);
316 if (strcmp(tag, t_ip.tag) != 0) {
317 continue;
319 return t_ip.ip;
322 /* this can't happen?? */
323 zero_ip(&t_ip.ip);
324 return t_ip.ip;
329 return a count of the number of IPs for a particular tag, including
330 dead ones
332 unsigned wins_srv_count_tag(const char *tag)
334 const char **list;
335 int i, count=0;
337 /* if we are a wins server then we always just talk to ourselves */
338 if (lp_wins_support()) {
339 return 1;
342 list = lp_wins_server_list();
343 if (!list || !list[0]) {
344 return 0;
347 /* find the first live one for this tag */
348 for (i=0; list[i]; i++) {
349 struct tagged_ip t_ip;
350 parse_ip(&t_ip, list[i]);
351 if (strcmp(tag, t_ip.tag) == 0) {
352 count++;
356 return count;