Clean up a comment noticed by Jonathan Shao@Panasas.com and remove an
[Samba/gebeck_regimport.git] / source3 / nmbd / nmbd_winsproxy.c
blobbace112752f7071261be7f9f82d27daa68c66f1d
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 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 #include "includes.h"
25 /****************************************************************************
26 Function called when the name lookup succeeded.
27 ****************************************************************************/
29 static void wins_proxy_name_query_request_success( struct subnet_record *subrec,
30 struct userdata_struct *userdata,
31 struct nmb_name *nmbname, struct in_addr ip, struct res_rec *rrec)
33 nstring name;
34 struct packet_struct *original_packet;
35 struct subnet_record *orig_broadcast_subnet;
36 struct name_record *namerec;
37 uint16 nb_flags;
38 int num_ips;
39 int i;
40 int ttl = 3600; /* By default one hour in the cache. */
41 struct in_addr *iplist;
43 /* Extract the original packet and the original broadcast subnet from
44 the userdata. */
46 memcpy( (char *)&orig_broadcast_subnet, userdata->data, sizeof(struct subnet_record *) );
47 memcpy( (char *)&original_packet, &userdata->data[sizeof(struct subnet_record *)],
48 sizeof(struct packet_struct *) );
50 nb_flags = get_nb_flags( rrec->rdata );
52 num_ips = rrec->rdlength / 6;
53 if(num_ips == 0) {
54 DEBUG(0,("wins_proxy_name_query_request_success: Invalid number of IP records (0) \
55 returned for name %s.\n", nmb_namestr(nmbname) ));
56 return;
59 if(num_ips == 1) {
60 iplist = &ip;
61 } else {
62 if((iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) )) == NULL) {
63 DEBUG(0,("wins_proxy_name_query_request_success: malloc fail !\n"));
64 return;
67 for(i = 0; i < num_ips; i++)
68 putip( (char *)&iplist[i], (char *)&rrec->rdata[ (i*6) + 2]);
71 /* Add the queried name to the original subnet as a WINS_PROXY_NAME. */
73 if(rrec == PERMANENT_TTL)
74 ttl = lp_max_ttl();
76 pull_ascii_nstring(name, nmbname->name);
77 namerec = add_name_to_subnet( orig_broadcast_subnet, name,
78 nmbname->name_type, nb_flags, ttl,
79 WINS_PROXY_NAME, num_ips, iplist );
81 if(iplist != &ip)
82 SAFE_FREE(iplist);
85 * Check that none of the IP addresses we are returning is on the
86 * same broadcast subnet as the original requesting packet. If it
87 * is then don't reply (although we still need to add the name
88 * to the cache) as the actual machine will be replying also
89 * and we don't want two replies to a broadcast query.
92 if(namerec && original_packet->packet.nmb.header.nm_flags.bcast) {
93 for( i = 0; i < namerec->data.num_ips; i++) {
94 if( same_net( namerec->data.ip[i], orig_broadcast_subnet->myip,
95 orig_broadcast_subnet->mask_ip ) ) {
96 DEBUG( 5, ( "wins_proxy_name_query_request_success: name %s is a WINS \
97 proxy name and is also on the same subnet (%s) as the requestor. \
98 Not replying.\n", nmb_namestr(&namerec->name), orig_broadcast_subnet->subnet_name ) );
99 return;
104 /* Finally reply to the original name query. */
105 reply_netbios_packet(original_packet, /* Packet to reply to. */
106 0, /* Result code. */
107 NMB_QUERY, /* nmbd type code. */
108 NMB_NAME_QUERY_OPCODE, /* opcode. */
109 ttl, /* ttl. */
110 rrec->rdata, /* data to send. */
111 rrec->rdlength); /* data length. */
114 /****************************************************************************
115 Function called when the name lookup failed.
116 ****************************************************************************/
118 static void wins_proxy_name_query_request_fail(struct subnet_record *subrec,
119 struct response_record *rrec,
120 struct nmb_name *question_name, int fail_code)
122 DEBUG(4,("wins_proxy_name_query_request_fail: WINS server returned error code %d for lookup \
123 of name %s.\n", fail_code, nmb_namestr(question_name) ));
126 /****************************************************************************
127 Function to make a deep copy of the userdata we will need when the WINS
128 proxy query returns.
129 ****************************************************************************/
131 static struct userdata_struct *wins_proxy_userdata_copy_fn(struct userdata_struct *userdata)
133 struct packet_struct *p, *copy_of_p;
134 struct userdata_struct *new_userdata = (struct userdata_struct *)malloc( userdata->userdata_len );
136 if(new_userdata == NULL)
137 return NULL;
139 new_userdata->copy_fn = userdata->copy_fn;
140 new_userdata->free_fn = userdata->free_fn;
141 new_userdata->userdata_len = userdata->userdata_len;
143 /* Copy the subnet_record pointer. */
144 memcpy( new_userdata->data, userdata->data, sizeof(struct subnet_record *) );
146 /* Extract the pointer to the packet struct */
147 memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)], sizeof(struct packet_struct *) );
149 /* Do a deep copy of the packet. */
150 if((copy_of_p = copy_packet(p)) == NULL) {
151 SAFE_FREE(new_userdata);
152 return NULL;
155 /* Lock the copy. */
156 copy_of_p->locked = True;
158 memcpy( &new_userdata->data[sizeof(struct subnet_record *)], (char *)&copy_of_p,
159 sizeof(struct packet_struct *) );
161 return new_userdata;
164 /****************************************************************************
165 Function to free the deep copy of the userdata we used when the WINS
166 proxy query returned.
167 ****************************************************************************/
169 static void wins_proxy_userdata_free_fn(struct userdata_struct *userdata)
171 struct packet_struct *p;
173 /* Extract the pointer to the packet struct */
174 memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)],
175 sizeof(struct packet_struct *));
177 /* Unlock the packet. */
178 p->locked = False;
180 free_packet(p);
181 ZERO_STRUCTP(userdata);
182 SAFE_FREE(userdata);
185 /****************************************************************************
186 Make a WINS query on behalf of a broadcast client name query request.
187 ****************************************************************************/
189 void make_wins_proxy_name_query_request( struct subnet_record *subrec,
190 struct packet_struct *incoming_packet,
191 struct nmb_name *question_name)
193 long *ud[(sizeof(struct userdata_struct) + sizeof(struct subrec *) +
194 sizeof(struct packet_struct *))/sizeof(long *) + 1];
195 struct userdata_struct *userdata = (struct userdata_struct *)ud;
196 nstring qname;
198 memset(ud, '\0', sizeof(ud));
200 userdata->copy_fn = wins_proxy_userdata_copy_fn;
201 userdata->free_fn = wins_proxy_userdata_free_fn;
202 userdata->userdata_len = sizeof(ud);
203 memcpy( userdata->data, (char *)&subrec, sizeof(struct subnet_record *));
204 memcpy( &userdata->data[sizeof(struct subnet_record *)], (char *)&incoming_packet,
205 sizeof(struct packet_struct *));
207 /* Now use the unicast subnet to query the name with the WINS server. */
208 pull_ascii_nstring(qname, question_name->name);
209 query_name( unicast_subnet, qname, question_name->name_type,
210 wins_proxy_name_query_request_success,
211 wins_proxy_name_query_request_fail,
212 userdata);