2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
6 Copyright (C) Jeremy Allison 1994-1998
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 extern int DEBUGLEVEL
;
28 /****************************************************************************
29 Function called when the name lookup succeeded.
30 ****************************************************************************/
32 static void wins_proxy_name_query_request_success( struct subnet_record
*subrec
,
33 struct userdata_struct
*userdata
,
34 struct nmb_name
*nmbname
, struct in_addr ip
, struct res_rec
*rrec
)
36 struct packet_struct
*original_packet
;
37 struct subnet_record
*orig_broadcast_subnet
;
38 struct name_record
*namerec
;
42 int ttl
= 3600; /* By default one hour in the cache. */
43 struct in_addr
*iplist
;
45 /* Extract the original packet and the original broadcast subnet from
48 memcpy( (char *)&orig_broadcast_subnet
, userdata
->data
, sizeof(struct subnet_record
*) );
49 memcpy( (char *)&original_packet
, &userdata
->data
[sizeof(struct subnet_record
*)],
50 sizeof(struct packet_struct
*) );
52 nb_flags
= get_nb_flags( rrec
->rdata
);
54 num_ips
= rrec
->rdlength
/ 6;
57 DEBUG(0,("wins_proxy_name_query_request_success: Invalid number of IP records (0) \
58 returned for name %s.\n", nmb_namestr(nmbname
) ));
66 if((iplist
= (struct in_addr
*)malloc( num_ips
* sizeof(struct in_addr
) )) == NULL
)
68 DEBUG(0,("wins_proxy_name_query_request_success: malloc fail !\n"));
72 for(i
= 0; i
< num_ips
; i
++)
73 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
== PERMANENT_TTL
)
81 namerec
= add_name_to_subnet( orig_broadcast_subnet
, nmbname
->name
,
82 nmbname
->name_type
, nb_flags
, ttl
,
83 WINS_PROXY_NAME
, num_ips
, iplist
);
89 * Check that none of the IP addresses we are returning is on the
90 * same broadcast subnet as the original requesting packet. If it
91 * is then don't reply (although we still need to add the name
92 * to the cache) as the actual machine will be replying also
93 * and we don't want two replies to a broadcast query.
96 if(namerec
&& original_packet
->packet
.nmb
.header
.nm_flags
.bcast
)
98 for( i
= 0; i
< namerec
->data
.num_ips
; i
++)
100 if( same_net( namerec
->data
.ip
[i
],
101 orig_broadcast_subnet
->myip
,
102 orig_broadcast_subnet
->mask_ip
) )
104 DEBUG( 5, ( "wins_proxy_name_query_request_success: name %s is a WINS \
105 proxy name and is also on the same subnet (%s) as the requestor. \
107 nmb_namestr(&namerec
->name
),
108 orig_broadcast_subnet
->subnet_name
) );
114 /* Finally reply to the original name query. */
115 reply_netbios_packet(original_packet
, /* Packet to reply to. */
116 0, /* Result code. */
117 NMB_QUERY
, /* nmbd type code. */
118 NMB_NAME_QUERY_OPCODE
, /* opcode. */
120 rrec
->rdata
, /* data to send. */
121 rrec
->rdlength
); /* data length. */
124 /****************************************************************************
125 Function called when the name lookup failed.
126 ****************************************************************************/
128 static void wins_proxy_name_query_request_fail(struct subnet_record
*subrec
,
129 struct response_record
*rrec
,
130 struct nmb_name
*question_name
, int fail_code
)
132 DEBUG(4,("wins_proxy_name_query_request_fail: WINS server returned error code %d for lookup \
133 of name %s.\n", fail_code
, nmb_namestr(question_name
) ));
136 /****************************************************************************
137 Function to make a deep copy of the userdata we will need when the WINS
139 ****************************************************************************/
141 static struct userdata_struct
*wins_proxy_userdata_copy_fn(struct userdata_struct
*userdata
)
143 struct packet_struct
*p
, *copy_of_p
;
144 struct userdata_struct
*new_userdata
=
145 (struct userdata_struct
*)malloc( userdata
->userdata_len
);
147 if(new_userdata
== NULL
)
150 new_userdata
->copy_fn
= userdata
->copy_fn
;
151 new_userdata
->free_fn
= userdata
->free_fn
;
152 new_userdata
->userdata_len
= userdata
->userdata_len
;
154 /* Copy the subnet_record pointer. */
155 memcpy( new_userdata
->data
, userdata
->data
, sizeof(struct subnet_record
*) );
157 /* Extract the pointer to the packet struct */
158 memcpy((char *)&p
, &userdata
->data
[sizeof(struct subnet_record
*)],
159 sizeof(struct packet_struct
*) );
161 /* Do a deep copy of the packet. */
162 if((copy_of_p
= copy_packet(p
)) == NULL
)
164 free((char *)new_userdata
);
169 copy_of_p
->locked
= True
;
171 memcpy( &new_userdata
->data
[sizeof(struct subnet_record
*)], (char *)©_of_p
,
172 sizeof(struct packet_struct
*) );
177 /****************************************************************************
178 Function to free the deep copy of the userdata we used when the WINS
179 proxy query returned.
180 ****************************************************************************/
182 static void wins_proxy_userdata_free_fn(struct userdata_struct
*userdata
)
184 struct packet_struct
*p
;
186 /* Extract the pointer to the packet struct */
187 memcpy((char *)&p
, &userdata
->data
[sizeof(struct subnet_record
*)],
188 sizeof(struct packet_struct
*));
190 /* Unlock the packet. */
194 ZERO_STRUCTP(userdata
);
195 free((char *)userdata
);
198 /****************************************************************************
199 Make a WINS query on behalf of a broadcast client name query request.
200 ****************************************************************************/
202 void make_wins_proxy_name_query_request( struct subnet_record
*subrec
,
203 struct packet_struct
*incoming_packet
,
204 struct nmb_name
*question_name
)
206 long *ud
[(sizeof(struct userdata_struct
) + sizeof(struct subrec
*) +
207 sizeof(struct packet_struct
*))/sizeof(long *) + 1];
208 struct userdata_struct
*userdata
= (struct userdata_struct
*)ud
;
210 memset(ud
, '\0', sizeof(ud
));
212 userdata
->copy_fn
= wins_proxy_userdata_copy_fn
;
213 userdata
->free_fn
= wins_proxy_userdata_free_fn
;
214 userdata
->userdata_len
= sizeof(ud
);
215 memcpy( userdata
->data
, (char *)&subrec
, sizeof(struct subnet_record
*));
216 memcpy( &userdata
->data
[sizeof(struct subnet_record
*)], (char *)&incoming_packet
,
217 sizeof(struct packet_struct
*));
219 /* Now use the unicast subnet to query the name with the WINS server. */
220 query_name( unicast_subnet
, question_name
->name
, question_name
->name_type
,
221 wins_proxy_name_query_request_success
,
222 wins_proxy_name_query_request_fail
,