2 Unix SMB/CIFS implementation.
3 NBT netbios routines and daemon - version 2
5 Copyright (C) Jeremy Allison 1994-1998
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 3 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, see <http://www.gnu.org/licenses/>.
24 /****************************************************************************
25 Function called when the name lookup succeeded.
26 ****************************************************************************/
28 static void wins_proxy_name_query_request_success( struct subnet_record
*subrec
,
29 struct userdata_struct
*userdata
,
30 struct nmb_name
*nmbname
, struct in_addr ip
, struct res_rec
*rrec
)
33 struct packet_struct
*original_packet
;
34 struct subnet_record
*orig_broadcast_subnet
;
35 struct name_record
*namerec
= NULL
;
39 int ttl
= 3600; /* By default one hour in the cache. */
40 struct in_addr
*iplist
;
42 /* Extract the original packet and the original broadcast subnet from
45 memcpy( (char *)&orig_broadcast_subnet
, userdata
->data
, sizeof(struct subnet_record
*) );
46 memcpy( (char *)&original_packet
, &userdata
->data
[sizeof(struct subnet_record
*)],
47 sizeof(struct packet_struct
*) );
50 nb_flags
= get_nb_flags( rrec
->rdata
);
51 num_ips
= rrec
->rdlength
/ 6;
58 DEBUG(0,("wins_proxy_name_query_request_success: Invalid number of IP records (0) \
59 returned for name %s.\n", nmb_namestr(nmbname
) ));
66 if((iplist
= SMB_MALLOC_ARRAY( struct in_addr
, num_ips
)) == NULL
) {
67 DEBUG(0,("wins_proxy_name_query_request_success: malloc fail !\n"));
71 for(i
= 0; i
< num_ips
; i
++) {
72 putip( (char *)&iplist
[i
], (char *)&rrec
->rdata
[ (i
*6) + 2]);
76 /* Add the queried name to the original subnet as a WINS_PROXY_NAME. */
78 if(rrec
->ttl
== PERMANENT_TTL
) {
82 pull_ascii_nstring(name
, sizeof(name
), nmbname
->name
);
83 add_name_to_subnet( orig_broadcast_subnet
, name
,
84 nmbname
->name_type
, nb_flags
, ttl
,
85 WINS_PROXY_NAME
, num_ips
, iplist
);
91 namerec
= find_name_on_subnet(orig_broadcast_subnet
, nmbname
, FIND_ANY_NAME
);
93 DEBUG(0,("wins_proxy_name_query_request_success: failed to add "
94 "name %s to subnet %s !\n",
96 orig_broadcast_subnet
->subnet_name
));
101 * Check that none of the IP addresses we are returning is on the
102 * same broadcast subnet as the original requesting packet. If it
103 * is then don't reply (although we still need to add the name
104 * to the cache) as the actual machine will be replying also
105 * and we don't want two replies to a broadcast query.
108 if(namerec
&& original_packet
->packet
.nmb
.header
.nm_flags
.bcast
) {
109 for( i
= 0; i
< namerec
->data
.num_ips
; i
++) {
110 if( same_net_v4( namerec
->data
.ip
[i
], orig_broadcast_subnet
->myip
,
111 orig_broadcast_subnet
->mask_ip
) ) {
112 DEBUG( 5, ( "wins_proxy_name_query_request_success: name %s is a WINS \
113 proxy name and is also on the same subnet (%s) as the requestor. \
114 Not replying.\n", nmb_namestr(&namerec
->name
), orig_broadcast_subnet
->subnet_name
) );
120 /* Finally reply to the original name query. */
121 reply_netbios_packet(original_packet
, /* Packet to reply to. */
122 0, /* Result code. */
123 NMB_QUERY
, /* nmbd type code. */
124 NMB_NAME_QUERY_OPCODE
, /* opcode. */
126 rrec
->rdata
, /* data to send. */
127 rrec
->rdlength
); /* data length. */
130 /****************************************************************************
131 Function called when the name lookup failed.
132 ****************************************************************************/
134 static void wins_proxy_name_query_request_fail(struct subnet_record
*subrec
,
135 struct response_record
*rrec
,
136 struct nmb_name
*question_name
, int fail_code
)
138 DEBUG(4,("wins_proxy_name_query_request_fail: WINS server returned error code %d for lookup \
139 of name %s.\n", fail_code
, nmb_namestr(question_name
) ));
142 /****************************************************************************
143 Function to make a deep copy of the userdata we will need when the WINS
145 ****************************************************************************/
147 static struct userdata_struct
*wins_proxy_userdata_copy_fn(struct userdata_struct
*userdata
)
149 struct packet_struct
*p
, *copy_of_p
;
150 struct userdata_struct
*new_userdata
= (struct userdata_struct
*)SMB_MALLOC( userdata
->userdata_len
);
152 if(new_userdata
== NULL
)
155 new_userdata
->copy_fn
= userdata
->copy_fn
;
156 new_userdata
->free_fn
= userdata
->free_fn
;
157 new_userdata
->userdata_len
= userdata
->userdata_len
;
159 /* Copy the subnet_record pointer. */
160 memcpy( new_userdata
->data
, userdata
->data
, sizeof(struct subnet_record
*) );
162 /* Extract the pointer to the packet struct */
163 memcpy((char *)&p
, &userdata
->data
[sizeof(struct subnet_record
*)], sizeof(struct packet_struct
*) );
165 /* Do a deep copy of the packet. */
166 if((copy_of_p
= copy_packet(p
)) == NULL
) {
167 SAFE_FREE(new_userdata
);
172 copy_of_p
->locked
= True
;
174 memcpy( &new_userdata
->data
[sizeof(struct subnet_record
*)], (char *)©_of_p
,
175 sizeof(struct packet_struct
*) );
180 /****************************************************************************
181 Function to free the deep copy of the userdata we used when the WINS
182 proxy query returned.
183 ****************************************************************************/
185 static void wins_proxy_userdata_free_fn(struct userdata_struct
*userdata
)
187 struct packet_struct
*p
;
189 /* Extract the pointer to the packet struct */
190 memcpy((char *)&p
, &userdata
->data
[sizeof(struct subnet_record
*)],
191 sizeof(struct packet_struct
*));
193 /* Unlock the packet. */
197 ZERO_STRUCTP(userdata
);
201 /****************************************************************************
202 Make a WINS query on behalf of a broadcast client name query request.
203 ****************************************************************************/
205 void make_wins_proxy_name_query_request( struct subnet_record
*subrec
,
206 struct packet_struct
*incoming_packet
,
207 struct nmb_name
*question_name
)
210 struct userdata_struct ud
;
211 char c
[sizeof(struct userdata_struct
) + sizeof(struct subrec
*) +
212 sizeof(struct packet_struct
*)+sizeof(long*)];
214 struct userdata_struct
*userdata
= &ud
.ud
;
217 memset(&ud
, '\0', sizeof(ud
));
219 userdata
->copy_fn
= wins_proxy_userdata_copy_fn
;
220 userdata
->free_fn
= wins_proxy_userdata_free_fn
;
221 userdata
->userdata_len
= sizeof(ud
);
222 memcpy( userdata
->data
, (char *)&subrec
, sizeof(struct subnet_record
*));
223 memcpy( &userdata
->data
[sizeof(struct subnet_record
*)], (char *)&incoming_packet
,
224 sizeof(struct packet_struct
*));
226 /* Now use the unicast subnet to query the name with the WINS server. */
227 pull_ascii_nstring(qname
, sizeof(qname
), question_name
->name
);
228 query_name( unicast_subnet
, qname
, question_name
->name_type
,
229 wins_proxy_name_query_request_success
,
230 wins_proxy_name_query_request_fail
,