2 Unix SMB/Netbios implementation.
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.
28 extern int DEBUGLEVEL
;
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
;
42 memset((char *)&sock
,'\0',sizeof(sock
));
44 #ifdef HAVE_SOCK_SIN_LEN
45 sock
.sin_len
= sizeof(sock
);
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);
54 setsockopt(res
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&val
,sizeof(val
));
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)
67 struct in_addr
*lookup_backend(const char *name
, int *count
)
70 static int initialised
;
79 setup_logging("nss_wins",True
);
81 lp_load(CONFIGFILE
,True
,False
,False
);
87 fd
= wins_lookup_open_socket_in();
91 set_socket_options(fd
,"SO_BROADCAST");
93 /* The next four lines commented out by JHT
94 and replaced with the four lines following */
95 /* if( !zero_ip( wins_ip ) ) {
96 * ret = name_query( fd, name, 0x20, False, True, wins_src_ip(), count );
102 ret
= name_query(fd
,name
,0x20,False
,True
, p
, count
);
106 if (lp_wins_support()) {
107 /* we are our own WINS server */
108 ret
= name_query(fd
,name
,0x20,False
,True
, *interpret_addr2("127.0.0.1"), count
);
112 /* uggh, we have to broadcast to each interface in turn */
113 for (j
=iface_count() - 1;
116 struct in_addr
*bcast
= iface_n_bcast(j
);
117 ret
= name_query(fd
,name
,0x20,True
,True
,*bcast
,count
);
128 /****************************************************************************
129 gethostbyname() - we ignore any domain portion of the name and only
130 handle names that are at most 15 characters long
131 **************************************************************************/
133 _nss_wins_gethostbyname_r(const char *name
, struct hostent
*he
,
134 char *buffer
, size_t buflen
, int *errnop
,
137 char **host_addresses
;
138 struct in_addr
*ip_list
;
140 size_t namelen
= strlen(name
) + 1;
142 memset(he
, '\0', sizeof(*he
));
144 ip_list
= lookup_backend(name
, &count
);
146 return NSS_STATUS_NOTFOUND
;
149 if (buflen
< namelen
+ (2*count
+1)*INADDRSZ
) {
150 /* no ENOMEM error type?! */
151 return NSS_STATUS_NOTFOUND
;
155 host_addresses
= (char **)buffer
;
156 he
->h_addr_list
= host_addresses
;
157 host_addresses
[count
] = NULL
;
158 buffer
+= (count
+ 1) * INADDRSZ
;
159 buflen
+= (count
+ 1) * INADDRSZ
;
160 he
->h_addrtype
= AF_INET
;
161 he
->h_length
= INADDRSZ
;
163 for (i
=0;i
<count
;i
++) {
164 memcpy(buffer
, &ip_list
[i
].s_addr
, INADDRSZ
);
165 *host_addresses
= buffer
;
174 memcpy(buffer
, name
, namelen
);
177 return NSS_STATUS_SUCCESS
;