always compile before commit :-)
[Samba.git] / source / nsswitch / wins.c
blobbfd1835b4e533ab5bba162d7e8bdc90e2a501bf9
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 /* Use our own create socket code so we don't recurse.... */
36 static int wins_lookup_open_socket_in(void)
38 struct sockaddr_in sock;
39 int val=1;
40 int res;
42 memset((char *)&sock,'\0',sizeof(sock));
44 #ifdef HAVE_SOCK_SIN_LEN
45 sock.sin_len = sizeof(sock);
46 #endif
47 sock.sin_port = 0;
48 sock.sin_family = AF_INET;
49 sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
50 res = socket(AF_INET, SOCK_DGRAM, 0);
51 if (res == -1)
52 return -1;
54 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
55 #ifdef SO_REUSEPORT
56 setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val));
57 #endif /* SO_REUSEPORT */
59 /* now we've got a socket - we need to bind it */
61 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
62 return(-1);
64 return res;
67 struct in_addr *lookup_backend(const char *name, int *count)
69 int fd;
70 static int initialised;
71 struct in_addr *ret;
72 struct in_addr p;
73 int j;
75 if (!initialised) {
76 initialised = 1;
77 DEBUGLEVEL = 0;
78 TimeInit();
79 setup_logging("nss_wins",True);
80 lp_load(CONFIGFILE,True,False,False);
81 load_interfaces();
84 *count = 0;
86 fd = wins_lookup_open_socket_in();
87 if (fd == -1)
88 return NULL;
90 set_socket_options(fd,"SO_BROADCAST");
92 /* The next four lines commented out by JHT
93 and replaced with the four lines following */
94 /* if( !zero_ip( wins_ip ) ) {
95 * ret = name_query( fd, name, 0x20, False, True, wins_src_ip(), count );
96 * goto out;
97 * }
99 p = wins_srv_ip();
100 if( !zero_ip(p) ) {
101 ret = name_query(fd,name,0x20,False,True, p, count);
102 goto out;
105 if (lp_wins_support()) {
106 /* we are our own WINS server */
107 ret = name_query(fd,name,0x20,False,True, *interpret_addr2("127.0.0.1"), count);
108 goto out;
111 /* uggh, we have to broadcast to each interface in turn */
112 for (j=iface_count() - 1;
113 j >= 0;
114 j--) {
115 struct in_addr *bcast = iface_n_bcast(j);
116 ret = name_query(fd,name,0x20,True,True,*bcast,count);
117 if (ret) break;
120 out:
122 close(fd);
123 return ret;
127 /****************************************************************************
128 gethostbyname() - we ignore any domain portion of the name and only
129 handle names that are at most 15 characters long
130 **************************************************************************/
131 enum nss_status
132 _nss_wins_gethostbyname_r(const char *name, struct hostent *he,
133 char *buffer, size_t buflen, int *errnop,
134 int *h_errnop)
136 char **host_addresses;
137 struct in_addr *ip_list;
138 int i, count;
139 size_t namelen = strlen(name) + 1;
141 memset(he, '\0', sizeof(*he));
143 ip_list = lookup_backend(name, &count);
144 if (!ip_list) {
145 return NSS_STATUS_NOTFOUND;
148 if (buflen < namelen + (2*count+1)*INADDRSZ) {
149 /* no ENOMEM error type?! */
150 return NSS_STATUS_NOTFOUND;
154 host_addresses = (char **)buffer;
155 he->h_addr_list = host_addresses;
156 host_addresses[count] = NULL;
157 buffer += (count + 1) * INADDRSZ;
158 buflen += (count + 1) * INADDRSZ;
159 he->h_addrtype = AF_INET;
160 he->h_length = INADDRSZ;
162 for (i=0;i<count;i++) {
163 memcpy(buffer, &ip_list[i].s_addr, INADDRSZ);
164 *host_addresses = buffer;
165 buffer += INADDRSZ;
166 buflen -= INADDRSZ;
167 host_addresses++;
170 if (ip_list)
171 free(ip_list);
173 memcpy(buffer, name, namelen);
174 he->h_name = buffer;
176 return NSS_STATUS_SUCCESS;