This is it ! The mega-merge of the JRA_NMBD_REWRITE branch
[abartlet/samba.git/.git] / source3 / nmbd / nmbd_winsproxy.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios routines and daemon - version 2
5
6    Copyright (C) Jeremy Allison 1994-1997
7
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.
12    
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.
17    
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.
21    
22 */
23
24 #include "includes.h"
25
26 extern int DEBUGLEVEL;
27
28 /****************************************************************************
29 Function called when the name lookup succeeded.
30 ****************************************************************************/
31
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)
35 {
36   struct packet_struct *original_packet;
37   struct subnet_record *orig_broadcast_subnet;
38   uint16 nb_flags;
39   int num_ips;
40   int i;
41   int ttl;
42   struct in_addr *iplist;
43
44   /* Extract the original packet and the original broadcast subnet from
45      the userdata. */
46
47   memcpy( (char *)&orig_broadcast_subnet, userdata->data, sizeof(struct subnet_record *) );
48   memcpy( (char *)&original_packet, &userdata->data[sizeof(struct subnet_record *)],
49           sizeof(struct packet_struct *) );
50
51   nb_flags = get_nb_flags( rrec->rdata );
52
53   num_ips = rrec->rdlength / 6;
54   if(num_ips == 0)
55   {
56     DEBUG(0,("wins_proxy_name_query_request_success: Invalid number of IP records (0) \
57 returned for name %s.\n", namestr(nmbname) ));
58     return;
59   }
60
61   if(num_ips == 1)
62     iplist = &ip;
63   else
64   {
65     if((iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) )) == NULL)
66     {
67       DEBUG(0,("wins_proxy_name_query_request_success: malloc fail !\n"));
68       return;
69     }
70
71     for(i = 0; i < num_ips; i++)
72       putip( (char *)&iplist[i], (char *)&rrec->rdata[ (i*6) + 2]);
73   }
74
75   /* Add the queried name to the original subnet as a WINS_PROXY_NAME. */
76
77   if(rrec == PERMANENT_TTL)
78     ttl = lp_max_ttl();
79
80   add_name_to_subnet( orig_broadcast_subnet, nmbname->name, nmbname->name_type,
81                          nb_flags, ttl, WINS_PROXY_NAME, num_ips, iplist);
82
83   if(iplist != &ip)
84     free((char *)iplist);
85
86   /* Finally reply to the original name query. */
87   reply_netbios_packet(original_packet,                /* Packet to reply to. */
88                        0,                              /* Result code. */
89                        NMB_QUERY,                      /* nmbd type code. */
90                        NMB_NAME_QUERY_OPCODE,          /* opcode. */
91                        ttl,                            /* ttl. */
92                        rrec->rdata,                    /* data to send. */
93                        rrec->rdlength);                /* data length. */
94 }
95
96 /****************************************************************************
97 Function called when the name lookup failed.
98 ****************************************************************************/
99
100 static void wins_proxy_name_query_request_fail(struct subnet_record *subrec,
101                                     struct response_record *rrec,
102                                     struct nmb_name *question_name, int fail_code)
103 {
104   DEBUG(4,("wins_proxy_name_query_request_fail: WINS server returned error code %d for lookup \
105 of name %s.\n", fail_code, namestr(question_name) ));
106 }
107
108 /****************************************************************************
109 Function to make a deep copy of the userdata we will need when the WINS
110 proxy query returns.
111 ****************************************************************************/
112
113 static struct userdata_struct *wins_proxy_userdata_copy_fn(struct userdata_struct *userdata)
114 {
115   struct packet_struct *p, *copy_of_p;
116   struct userdata_struct *new_userdata = 
117         (struct userdata_struct *)malloc( userdata->userdata_len );
118
119   if(new_userdata == NULL)
120     return NULL;
121
122   new_userdata->copy_fn = userdata->copy_fn;
123   new_userdata->free_fn = userdata->free_fn;
124   new_userdata->userdata_len = userdata->userdata_len;
125
126   /* Copy the subnet_record pointer. */
127   memcpy( new_userdata->data, userdata->data, sizeof(struct subnet_record *) );
128
129   /* Extract the pointer to the packet struct */
130   memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)],
131          sizeof(struct packet_struct *) );
132
133   /* Do a deep copy of the packet. */
134   if((copy_of_p = copy_packet(p)) == NULL)
135   {
136     free((char *)new_userdata);
137     return NULL;
138   }
139
140   /* Lock the copy. */
141   copy_of_p->locked = True;
142
143   memcpy( &new_userdata->data[sizeof(struct subnet_record *)], (char *)&copy_of_p,
144           sizeof(struct packet_struct *) );
145
146   return new_userdata;
147 }
148
149 /****************************************************************************
150 Function to free the deep copy of the userdata we used when the WINS
151 proxy query returned.
152 ****************************************************************************/
153
154 static void wins_proxy_userdata_free_fn(struct userdata_struct *userdata)
155 {
156   struct packet_struct *p;
157
158   /* Extract the pointer to the packet struct */
159   memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)],
160          sizeof(struct packet_struct *));
161
162   /* Unlock the packet. */
163   p->locked = False;
164
165   free_packet(p);
166   free((char *)userdata);
167 }
168
169 /****************************************************************************
170  Make a WINS query on behalf of a broadcast client name query request.
171 ****************************************************************************/
172
173 void make_wins_proxy_name_query_request( struct subnet_record *subrec, 
174                                          struct packet_struct *incoming_packet,
175                                          struct nmb_name *question_name)
176 {
177   char ud[sizeof(struct userdata_struct) + sizeof(struct subrec *) + 
178           sizeof(struct packet_struct *)];
179   struct userdata_struct *userdata = (struct userdata_struct *)ud;
180
181   bzero(ud, sizeof(ud));
182  
183   userdata->copy_fn = wins_proxy_userdata_copy_fn;
184   userdata->free_fn = wins_proxy_userdata_free_fn;
185   userdata->userdata_len = sizeof(ud);
186   memcpy( userdata->data, (char *)&subrec, sizeof(struct subnet_record *));
187   memcpy( &userdata->data[sizeof(struct subnet_record *)], (char *)&incoming_packet,
188           sizeof(struct packet_struct *));
189
190   /* Now use the unicast subnet to query the name with the WINS server. */
191   query_name( unicast_subnet, question_name->name, question_name->name_type,
192               wins_proxy_name_query_request_success,
193               wins_proxy_name_query_request_fail,
194               userdata);
195 }