First cut toward adding WINS server failover.
[kai/samba.git] / source / nsswitch / wins.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4    a WINS nsswitch module 
5    Copyright (C) Andrew Tridgell 1999
6    
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.
11    
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.
16    
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.
20    
21 */
22
23 #define NO_SYSLOG
24
25 #include "includes.h"
26 #include <nss.h>
27
28 extern int DEBUGLEVEL;
29
30 #ifndef INADDRSZ
31 #define INADDRSZ 4
32 #endif
33
34 struct in_addr *lookup_backend(const char *name, int *count)
35 {
36         int fd;
37         static int initialised;
38         struct in_addr *ret;
39         char *p;
40         int j;
41
42         if (!initialised) {
43                 initialised = 1;
44                 DEBUGLEVEL = 0;
45                 TimeInit();
46                 setup_logging("nss_wins",True);
47                 charset_initialise();
48                 lp_load(CONFIGFILE,True,False,False);
49                 load_interfaces();
50         }
51
52         *count = 0;
53
54         fd = open_socket_in(SOCK_DGRAM,0, 3, interpret_addr("0.0.0.0"), True);
55         if (fd == -1) return NULL;
56
57         set_socket_options(fd,"SO_BROADCAST");
58
59         p = wins_srv();
60         if (p && *p) {
61                 ret = name_query(fd,name,0x20,False,True, *interpret_addr2(p), count);
62                 goto out;
63         }
64
65         if (lp_wins_support()) {
66                 /* we are our own WINS server */
67                 ret = name_query(fd,name,0x20,False,True, *interpret_addr2("127.0.0.1"), count);
68                 goto out;
69         }
70
71         /* uggh, we have to broadcast to each interface in turn */
72         for (j=iface_count() - 1;
73              j >= 0;
74              j--) {
75                 struct in_addr *bcast = iface_n_bcast(j);
76                 ret = name_query(fd,name,0x20,True,True,*bcast,count);
77                 if (ret) break;
78         }
79
80  out:
81         close(fd);
82         return ret;
83 }
84
85
86 /****************************************************************************
87 gethostbyname() - we ignore any domain portion of the name and only
88 handle names that are at most 15 characters long
89   **************************************************************************/
90 enum nss_status 
91 _nss_wins_gethostbyname_r(const char *name, struct hostent *he,
92                           char *buffer, size_t buflen, int *errnop,
93                           int *h_errnop)
94 {
95         char **host_addresses;
96         struct in_addr *ip_list;
97         int i, count;
98
99         ip_list = lookup_backend(name, &count);
100         if (!ip_list) {
101                 return NSS_STATUS_NOTFOUND;
102         }
103
104         if (buflen < (2*count+1)*INADDRSZ) {
105                 /* no ENOMEM error type?! */
106                 return NSS_STATUS_NOTFOUND;
107         }
108
109
110         host_addresses = (char **)buffer;
111         he->h_addr_list = host_addresses;
112         host_addresses[count] = NULL;
113         buffer += (count + 1) * INADDRSZ;
114         buflen += (count + 1) * INADDRSZ;
115         he->h_addrtype = AF_INET;
116         he->h_length = INADDRSZ;
117
118         for (i=0;i<count;i++) {
119                 memcpy(buffer, &ip_list[i].s_addr, INADDRSZ);
120                 *host_addresses = buffer;
121                 buffer += INADDRSZ;
122                 buflen -= INADDRSZ;
123                 host_addresses++;
124         }
125
126         if (ip_list) free(ip_list);
127
128         return NSS_STATUS_SUCCESS;
129 }