nmbd/nmbd_winsproxy.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    NBT netbios routines and daemon - version 2
00004 
00005    Copyright (C) Jeremy Allison 1994-1998
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016    
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    
00021 */
00022 
00023 #include "includes.h"
00024 
00025 /****************************************************************************
00026 Function called when the name lookup succeeded.
00027 ****************************************************************************/
00028 
00029 static void wins_proxy_name_query_request_success( struct subnet_record *subrec,
00030                         struct userdata_struct *userdata,
00031                         struct nmb_name *nmbname, struct in_addr ip, struct res_rec *rrec)
00032 {
00033         unstring name;
00034         struct packet_struct *original_packet;
00035         struct subnet_record *orig_broadcast_subnet;
00036         struct name_record *namerec = NULL;
00037         uint16 nb_flags;
00038         int num_ips;
00039         int i;
00040         int ttl = 3600; /* By default one hour in the cache. */
00041         struct in_addr *iplist;
00042 
00043         /* Extract the original packet and the original broadcast subnet from
00044                         the userdata. */
00045 
00046         memcpy( (char *)&orig_broadcast_subnet, userdata->data, sizeof(struct subnet_record *) );
00047         memcpy( (char *)&original_packet, &userdata->data[sizeof(struct subnet_record *)],
00048                         sizeof(struct packet_struct *) );
00049 
00050         if (rrec) {
00051                 nb_flags = get_nb_flags( rrec->rdata );
00052                 num_ips = rrec->rdlength / 6;
00053         } else {
00054                 nb_flags = 0;
00055                 num_ips = 0;
00056         }
00057 
00058         if(num_ips == 0) {
00059                 DEBUG(0,("wins_proxy_name_query_request_success: Invalid number of IP records (0) \
00060 returned for name %s.\n", nmb_namestr(nmbname) ));
00061                 return;
00062         }
00063 
00064         if(num_ips == 1) {
00065                 iplist = &ip;
00066         } else {
00067                 if((iplist = SMB_MALLOC_ARRAY( struct in_addr, num_ips )) == NULL) {
00068                         DEBUG(0,("wins_proxy_name_query_request_success: malloc fail !\n"));
00069                         return;
00070                 }
00071 
00072                 for(i = 0; i < num_ips; i++) {
00073                         putip( (char *)&iplist[i], (char *)&rrec->rdata[ (i*6) + 2]);
00074                 }
00075         }
00076 
00077         /* Add the queried name to the original subnet as a WINS_PROXY_NAME. */
00078 
00079         if(rrec->ttl == PERMANENT_TTL) {
00080                 ttl = lp_max_ttl();
00081         }
00082 
00083         pull_ascii_nstring(name, sizeof(name), nmbname->name);
00084         add_name_to_subnet( orig_broadcast_subnet, name,
00085                                         nmbname->name_type, nb_flags, ttl,
00086                                         WINS_PROXY_NAME, num_ips, iplist );
00087 
00088         if(iplist != &ip) {
00089                 SAFE_FREE(iplist);
00090         }
00091 
00092         namerec = find_name_on_subnet(orig_broadcast_subnet, nmbname, FIND_ANY_NAME);
00093         if (!namerec) {
00094                 DEBUG(0,("wins_proxy_name_query_request_success: failed to add "
00095                         "name %s to subnet %s !\n",
00096                         name,
00097                         orig_broadcast_subnet->subnet_name ));
00098                 return;
00099         }
00100 
00101         /*
00102          * Check that none of the IP addresses we are returning is on the
00103          * same broadcast subnet as the original requesting packet. If it
00104          * is then don't reply (although we still need to add the name
00105          * to the cache) as the actual machine will be replying also
00106          * and we don't want two replies to a broadcast query.
00107          */
00108 
00109         if(namerec && original_packet->packet.nmb.header.nm_flags.bcast) {
00110                 for( i = 0; i < namerec->data.num_ips; i++) {
00111                         if( same_net( namerec->data.ip[i], orig_broadcast_subnet->myip,
00112                                         orig_broadcast_subnet->mask_ip ) ) {
00113                                 DEBUG( 5, ( "wins_proxy_name_query_request_success: name %s is a WINS \
00114 proxy name and is also on the same subnet (%s) as the requestor. \
00115 Not replying.\n", nmb_namestr(&namerec->name), orig_broadcast_subnet->subnet_name ) );
00116                                 return;
00117                         }
00118                 }
00119         }
00120 
00121         /* Finally reply to the original name query. */
00122         reply_netbios_packet(original_packet,                /* Packet to reply to. */
00123                                 0,                              /* Result code. */
00124                                 NMB_QUERY,                      /* nmbd type code. */
00125                                 NMB_NAME_QUERY_OPCODE,          /* opcode. */
00126                                 ttl,                            /* ttl. */
00127                                 rrec->rdata,                    /* data to send. */
00128                                 rrec->rdlength);                /* data length. */
00129 }
00130 
00131 /****************************************************************************
00132 Function called when the name lookup failed.
00133 ****************************************************************************/
00134 
00135 static void wins_proxy_name_query_request_fail(struct subnet_record *subrec,
00136                                     struct response_record *rrec,
00137                                     struct nmb_name *question_name, int fail_code)
00138 {
00139         DEBUG(4,("wins_proxy_name_query_request_fail: WINS server returned error code %d for lookup \
00140 of name %s.\n", fail_code, nmb_namestr(question_name) ));
00141 }
00142 
00143 /****************************************************************************
00144 Function to make a deep copy of the userdata we will need when the WINS
00145 proxy query returns.
00146 ****************************************************************************/
00147 
00148 static struct userdata_struct *wins_proxy_userdata_copy_fn(struct userdata_struct *userdata)
00149 {
00150         struct packet_struct *p, *copy_of_p;
00151         struct userdata_struct *new_userdata = (struct userdata_struct *)SMB_MALLOC( userdata->userdata_len );
00152 
00153         if(new_userdata == NULL)
00154                 return NULL;
00155 
00156         new_userdata->copy_fn = userdata->copy_fn;
00157         new_userdata->free_fn = userdata->free_fn;
00158         new_userdata->userdata_len = userdata->userdata_len;
00159 
00160         /* Copy the subnet_record pointer. */
00161         memcpy( new_userdata->data, userdata->data, sizeof(struct subnet_record *) );
00162 
00163         /* Extract the pointer to the packet struct */
00164         memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)], sizeof(struct packet_struct *) );
00165 
00166         /* Do a deep copy of the packet. */
00167         if((copy_of_p = copy_packet(p)) == NULL) {
00168                 SAFE_FREE(new_userdata);
00169                 return NULL;
00170         }
00171 
00172         /* Lock the copy. */
00173         copy_of_p->locked = True;
00174 
00175         memcpy( &new_userdata->data[sizeof(struct subnet_record *)], (char *)&copy_of_p,
00176                 sizeof(struct packet_struct *) );
00177 
00178         return new_userdata;
00179 }
00180 
00181 /****************************************************************************
00182 Function to free the deep copy of the userdata we used when the WINS
00183 proxy query returned.
00184 ****************************************************************************/
00185 
00186 static void wins_proxy_userdata_free_fn(struct userdata_struct *userdata)
00187 {
00188         struct packet_struct *p;
00189 
00190         /* Extract the pointer to the packet struct */
00191         memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)],
00192                 sizeof(struct packet_struct *));
00193 
00194         /* Unlock the packet. */
00195         p->locked = False;
00196 
00197         free_packet(p);
00198         ZERO_STRUCTP(userdata);
00199         SAFE_FREE(userdata);
00200 }
00201 
00202 /****************************************************************************
00203  Make a WINS query on behalf of a broadcast client name query request.
00204 ****************************************************************************/
00205 
00206 void make_wins_proxy_name_query_request( struct subnet_record *subrec, 
00207                                          struct packet_struct *incoming_packet,
00208                                          struct nmb_name *question_name)
00209 {
00210         union {
00211             struct userdata_struct ud;
00212             char c[sizeof(struct userdata_struct) + sizeof(struct subrec *) + 
00213                 sizeof(struct packet_struct *)+sizeof(long*)];
00214         } ud;
00215         struct userdata_struct *userdata = &ud.ud;
00216         unstring qname;
00217 
00218         memset(&ud, '\0', sizeof(ud));
00219  
00220         userdata->copy_fn = wins_proxy_userdata_copy_fn;
00221         userdata->free_fn = wins_proxy_userdata_free_fn;
00222         userdata->userdata_len = sizeof(ud);
00223         memcpy( userdata->data, (char *)&subrec, sizeof(struct subnet_record *));
00224         memcpy( &userdata->data[sizeof(struct subnet_record *)], (char *)&incoming_packet,
00225                         sizeof(struct packet_struct *));
00226 
00227         /* Now use the unicast subnet to query the name with the WINS server. */
00228         pull_ascii_nstring(qname, sizeof(qname), question_name->name);
00229         query_name( unicast_subnet, qname, question_name->name_type,
00230                 wins_proxy_name_query_request_success,
00231                 wins_proxy_name_query_request_fail,
00232                 userdata);
00233 }

Sambaに対してSat Aug 29 21:23:08 2009に生成されました。  doxygen 1.4.7