Converted to call lib/wins_srv.c:wins_srv_ip() instead of lp_wins_server()
[Samba.git] / source3 / nsswitch / wins.c
blobbe76c2e54ee201307de2ea106b4fcf1562cac3c6
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 a WINS nsswitch module
5 Copyright (C) Andrew Tridgell 1999
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 #define NO_SYSLOG
25 #include "includes.h"
26 #include <nss.h>
28 extern int DEBUGLEVEL;
30 #ifndef INADDRSZ
31 #define INADDRSZ 4
32 #endif
34 struct in_addr *lookup_backend(const char *name, int *count)
36 int fd;
37 static int initialised;
38 struct in_addr *ret;
39 struct in_addr p;
40 int j;
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();
52 *count = 0;
54 fd = open_socket_in(SOCK_DGRAM,0, 3, interpret_addr("0.0.0.0"), True);
55 if (fd == -1) return NULL;
57 set_socket_options(fd,"SO_BROADCAST");
59 /* The next four lines commented out by JHT
60 and replaced with the four lines following */
61 /* if( !zero_ip( wins_ip ) ) {
62 * ret = name_query( fd, name, 0x20, False, True, wins_src_ip(), count );
63 * goto out;
64 * }
66 p = wins_srv_ip();
67 if( !zero_ip(p) ) {
68 ret = name_query(fd,name,0x20,False,True, p, count);
69 goto out;
72 if (lp_wins_support()) {
73 /* we are our own WINS server */
74 ret = name_query(fd,name,0x20,False,True, *interpret_addr2("127.0.0.1"), count);
75 goto out;
78 /* uggh, we have to broadcast to each interface in turn */
79 for (j=iface_count() - 1;
80 j >= 0;
81 j--) {
82 struct in_addr *bcast = iface_n_bcast(j);
83 ret = name_query(fd,name,0x20,True,True,*bcast,count);
84 if (ret) break;
87 out:
88 close(fd);
89 return ret;
93 /****************************************************************************
94 gethostbyname() - we ignore any domain portion of the name and only
95 handle names that are at most 15 characters long
96 **************************************************************************/
97 enum nss_status
98 _nss_wins_gethostbyname_r(const char *name, struct hostent *he,
99 char *buffer, size_t buflen, int *errnop,
100 int *h_errnop)
102 char **host_addresses;
103 struct in_addr *ip_list;
104 int i, count;
106 ip_list = lookup_backend(name, &count);
107 if (!ip_list) {
108 return NSS_STATUS_NOTFOUND;
111 if (buflen < (2*count+1)*INADDRSZ) {
112 /* no ENOMEM error type?! */
113 return NSS_STATUS_NOTFOUND;
117 host_addresses = (char **)buffer;
118 he->h_addr_list = host_addresses;
119 host_addresses[count] = NULL;
120 buffer += (count + 1) * INADDRSZ;
121 buflen += (count + 1) * INADDRSZ;
122 he->h_addrtype = AF_INET;
123 he->h_length = INADDRSZ;
125 for (i=0;i<count;i++) {
126 memcpy(buffer, &ip_list[i].s_addr, INADDRSZ);
127 *host_addresses = buffer;
128 buffer += INADDRSZ;
129 buflen -= INADDRSZ;
130 host_addresses++;
133 if (ip_list) free(ip_list);
135 return NSS_STATUS_SUCCESS;