Split lookup_name() and create a new functiong called
[Samba/ekacnet.git] / source3 / nmbd / nmbd_winsproxy.c
blobff80c15ffff4ecbf9c29312918e848e0c1837c37
1 /*
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/>.
22 #include "includes.h"
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)
32 unstring name;
33 struct packet_struct *original_packet;
34 struct subnet_record *orig_broadcast_subnet;
35 struct name_record *namerec = NULL;
36 uint16 nb_flags;
37 int num_ips;
38 int i;
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
43 the userdata. */
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 *) );
49 if (rrec) {
50 nb_flags = get_nb_flags( rrec->rdata );
51 num_ips = rrec->rdlength / 6;
52 } else {
53 nb_flags = 0;
54 num_ips = 0;
57 if(num_ips == 0) {
58 DEBUG(0,("wins_proxy_name_query_request_success: Invalid number of IP records (0) \
59 returned for name %s.\n", nmb_namestr(nmbname) ));
60 return;
63 if(num_ips == 1) {
64 iplist = &ip;
65 } else {
66 if((iplist = SMB_MALLOC_ARRAY( struct in_addr, num_ips )) == NULL) {
67 DEBUG(0,("wins_proxy_name_query_request_success: malloc fail !\n"));
68 return;
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) {
79 ttl = lp_max_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 );
87 if(iplist != &ip) {
88 SAFE_FREE(iplist);
91 namerec = find_name_on_subnet(orig_broadcast_subnet, nmbname, FIND_ANY_NAME);
92 if (!namerec) {
93 DEBUG(0,("wins_proxy_name_query_request_success: failed to add "
94 "name %s to subnet %s !\n",
95 name,
96 orig_broadcast_subnet->subnet_name ));
97 return;
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 ) );
115 return;
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. */
125 ttl, /* ttl. */
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
144 proxy query returns.
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)
153 return 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);
168 return NULL;
171 /* Lock the copy. */
172 copy_of_p->locked = True;
174 memcpy( &new_userdata->data[sizeof(struct subnet_record *)], (char *)&copy_of_p,
175 sizeof(struct packet_struct *) );
177 return new_userdata;
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. */
194 p->locked = False;
196 free_packet(p);
197 ZERO_STRUCTP(userdata);
198 SAFE_FREE(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)
209 union {
210 struct userdata_struct ud;
211 char c[sizeof(struct userdata_struct) + sizeof(struct subrec *) +
212 sizeof(struct packet_struct *)+sizeof(long*)];
213 } ud;
214 struct userdata_struct *userdata = &ud.ud;
215 unstring qname;
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,
231 userdata);