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 3 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, see <http://www.gnu.org/licenses/>.
24 extern struct in_addr loopback_ip
;
27 This is pretty much a complete rewrite of the earlier code. The main
28 aim of the rewrite is to add support for having multiple wins server
29 lists, so Samba can register with multiple groups of wins servers
30 and each group has a failover list of wins servers.
32 Central to the way it all works is the idea of a wins server
33 'tag'. A wins tag is a label for a group of wins servers. For
36 wins server = fred:192.168.2.10 mary:192.168.3.199 fred:192.168.2.61
38 then you would have two groups of wins servers, one tagged with the
39 name 'fred' and the other with the name 'mary'. I would usually
40 recommend using interface names instead of 'fred' and 'mary' but
41 they can be any alpha string.
43 Now, how does it all work. Well, nmbd needs to register each of its
44 IPs with each of its names once with each group of wins servers. So
45 it tries registering with the first one mentioned in the list, then
46 if that fails it marks that WINS server dead and moves onto the next
49 In the client code things are a bit different. As each of the groups
50 of wins servers is a separate name space we need to try each of the
51 groups until we either succeed or we run out of wins servers to
52 try. If we get a negative response from a wins server then that
53 means the name doesn't exist in that group, so we give up on that
54 group and move to the next group. If we don't get a response at all
55 then maybe the wins server is down, in which case we need to
56 failover to the next one for that group.
58 confused yet? (tridge)
61 /* how long a server is marked dead for */
62 #define DEATH_TIME 600
64 /* The list of dead wins servers is stored in gencache.tdb. Each server is
65 marked dead from the point of view of a given source address. We keep a
66 separate dead list for each src address to cope with multiple interfaces
67 that are not routable to each other.
70 #define WINS_SRV_FMT "WINS_SRV_DEAD/%s,%s" /* wins_ip,src_ip */
72 static char *wins_srv_keystr(struct in_addr wins_ip
, struct in_addr src_ip
)
74 char *keystr
= NULL
, *wins_ip_addr
= NULL
, *src_ip_addr
= NULL
;
76 wins_ip_addr
= SMB_STRDUP(inet_ntoa(wins_ip
));
77 src_ip_addr
= SMB_STRDUP(inet_ntoa(src_ip
));
79 if ( !wins_ip_addr
|| !src_ip_addr
) {
80 DEBUG(0,("wins_srv_keystr: malloc error\n"));
84 if (asprintf(&keystr
, WINS_SRV_FMT
, wins_ip_addr
, src_ip_addr
) == -1) {
85 DEBUG(0, (": ns_srv_keystr: malloc error for key string\n"));
89 SAFE_FREE(wins_ip_addr
);
90 SAFE_FREE(src_ip_addr
);
96 see if an ip is on the dead list
99 BOOL
wins_srv_is_dead(struct in_addr wins_ip
, struct in_addr src_ip
)
101 char *keystr
= wins_srv_keystr(wins_ip
, src_ip
);
104 /* If the key exists then the WINS server has been marked as dead */
106 result
= gencache_get(keystr
, NULL
, NULL
);
109 DEBUG(4, ("wins_srv_is_dead: %s is %s\n", inet_ntoa(wins_ip
),
110 result
? "dead" : "alive"));
117 mark a wins server as being alive (for the moment)
119 void wins_srv_alive(struct in_addr wins_ip
, struct in_addr src_ip
)
121 char *keystr
= wins_srv_keystr(wins_ip
, src_ip
);
123 gencache_del(keystr
);
126 DEBUG(4, ("wins_srv_alive: marking wins server %s alive\n",
127 inet_ntoa(wins_ip
)));
131 mark a wins server as temporarily dead
133 void wins_srv_died(struct in_addr wins_ip
, struct in_addr src_ip
)
137 if (is_zero_ip(wins_ip
) || wins_srv_is_dead(wins_ip
, src_ip
))
140 keystr
= wins_srv_keystr(wins_ip
, src_ip
);
142 gencache_set(keystr
, "DOWN", time(NULL
) + DEATH_TIME
);
146 DEBUG(4,("Marking wins server %s dead for %u seconds from source %s\n",
147 inet_ntoa(wins_ip
), DEATH_TIME
, inet_ntoa(src_ip
)));
151 return the total number of wins servers, dead or not
153 unsigned wins_srv_count(void)
158 if (lp_wins_support()) {
159 /* simple - just talk to ourselves */
163 list
= lp_wins_server_list();
164 for (count
=0; list
&& list
[count
]; count
++)
170 /* an internal convenience structure for an IP with a short string tag
178 parse an IP string that might be in tagged format
179 the result is a tagged_ip structure containing the tag
180 and the ip in in_addr format. If there is no tag then
183 static void parse_ip(struct tagged_ip
*ip
, const char *str
)
185 char *s
= strchr(str
, ':');
187 fstrcpy(ip
->tag
, "*");
188 ip
->ip
= *interpret_addr2(str
);
192 ip
->ip
= *interpret_addr2(s
+1);
193 fstrcpy(ip
->tag
, str
);
194 s
= strchr(ip
->tag
, ':');
201 return the list of wins server tags. A 'tag' is used to distinguish
202 wins server as either belonging to the same name space or a separate
203 name space. Usually you would setup your 'wins server' option to
204 list one or more wins server per interface and use the interface
205 name as your tag, but you are free to use any tag you like.
207 char **wins_srv_tags(void)
213 if (lp_wins_support()) {
214 /* give the caller something to chew on. This makes
215 the rest of the logic simpler (ie. less special cases) */
216 ret
= SMB_MALLOC_ARRAY(char *, 2);
217 if (!ret
) return NULL
;
218 ret
[0] = SMB_STRDUP("*");
223 list
= lp_wins_server_list();
227 /* yes, this is O(n^2) but n is very small */
228 for (i
=0;list
[i
];i
++) {
229 struct tagged_ip t_ip
;
231 parse_ip(&t_ip
, list
[i
]);
233 /* see if we already have it */
234 for (j
=0;j
<count
;j
++) {
235 if (strcmp(ret
[j
], t_ip
.tag
) == 0) {
241 /* we already have it. Move along */
245 /* add it to the list */
246 ret
= SMB_REALLOC_ARRAY(ret
, char *, count
+2);
250 ret
[count
] = SMB_STRDUP(t_ip
.tag
);
251 if (!ret
[count
]) break;
256 /* make sure we null terminate */
263 /* free a list of wins server tags given by wins_srv_tags */
264 void wins_srv_tags_free(char **list
)
268 for (i
=0; list
[i
]; i
++) {
276 return the IP of the currently active wins server for the given tag,
277 or the zero IP otherwise
279 struct in_addr
wins_srv_ip_tag(const char *tag
, struct in_addr src_ip
)
283 struct tagged_ip t_ip
;
285 /* if we are a wins server then we always just talk to ourselves */
286 if (lp_wins_support()) {
290 list
= lp_wins_server_list();
291 if (!list
|| !list
[0]) {
297 /* find the first live one for this tag */
298 for (i
=0; list
[i
]; i
++) {
299 parse_ip(&t_ip
, list
[i
]);
300 if (strcmp(tag
, t_ip
.tag
) != 0) {
301 /* not for the right tag. Move along */
304 if (!wins_srv_is_dead(t_ip
.ip
, src_ip
)) {
306 fstrcpy(src_name
, inet_ntoa(src_ip
));
307 DEBUG(6,("Current wins server for tag '%s' with source %s is %s\n",
310 inet_ntoa(t_ip
.ip
)));
315 /* they're all dead - try the first one until they revive */
316 for (i
=0; list
[i
]; i
++) {
317 parse_ip(&t_ip
, list
[i
]);
318 if (strcmp(tag
, t_ip
.tag
) != 0) {
324 /* this can't happen?? */
331 return a count of the number of IPs for a particular tag, including
334 unsigned wins_srv_count_tag(const char *tag
)
339 /* if we are a wins server then we always just talk to ourselves */
340 if (lp_wins_support()) {
344 list
= lp_wins_server_list();
345 if (!list
|| !list
[0]) {
349 /* find the first live one for this tag */
350 for (i
=0; list
[i
]; i
++) {
351 struct tagged_ip t_ip
;
352 parse_ip(&t_ip
, list
[i
]);
353 if (strcmp(tag
, t_ip
.tag
) == 0) {