source: branches/samba-3.0/source/nmbd/nmbd_winsproxy.c@ 105

Last change on this file since 105 was 1, checked in by Paul Smedley, 19 years ago

Initial code import

File size: 8.0 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 NBT netbios routines and daemon - version 2
4
5 Copyright (C) Jeremy Allison 1994-1998
6
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.
11
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.
16
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.
20
21*/
22
23#include "includes.h"
24
25/****************************************************************************
26Function called when the name lookup succeeded.
27****************************************************************************/
28
29static 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)
32{
33 unstring name;
34 struct packet_struct *original_packet;
35 struct subnet_record *orig_broadcast_subnet;
36 struct name_record *namerec = NULL;
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;
42
43 /* Extract the original packet and the original broadcast subnet from
44 the userdata. */
45
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 *) );
49
50 if (rrec) {
51 nb_flags = get_nb_flags( rrec->rdata );
52 num_ips = rrec->rdlength / 6;
53 } else {
54 nb_flags = 0;
55 num_ips = 0;
56 }
57
58 if(num_ips == 0) {
59 DEBUG(0,("wins_proxy_name_query_request_success: Invalid number of IP records (0) \
60returned for name %s.\n", nmb_namestr(nmbname) ));
61 return;
62 }
63
64 if(num_ips == 1) {
65 iplist = &ip;
66 } else {
67 if((iplist = SMB_MALLOC_ARRAY( struct in_addr, num_ips )) == NULL) {
68 DEBUG(0,("wins_proxy_name_query_request_success: malloc fail !\n"));
69 return;
70 }
71
72 for(i = 0; i < num_ips; i++) {
73 putip( (char *)&iplist[i], (char *)&rrec->rdata[ (i*6) + 2]);
74 }
75 }
76
77 /* Add the queried name to the original subnet as a WINS_PROXY_NAME. */
78
79 if(rrec->ttl == PERMANENT_TTL) {
80 ttl = lp_max_ttl();
81 }
82
83 pull_ascii_nstring(name, sizeof(name), nmbname->name);
84 add_name_to_subnet( orig_broadcast_subnet, name,
85 nmbname->name_type, nb_flags, ttl,
86 WINS_PROXY_NAME, num_ips, iplist );
87
88 if(iplist != &ip) {
89 SAFE_FREE(iplist);
90 }
91
92 namerec = find_name_on_subnet(orig_broadcast_subnet, nmbname, FIND_ANY_NAME);
93 if (!namerec) {
94 DEBUG(0,("wins_proxy_name_query_request_success: failed to add "
95 "name %s to subnet %s !\n",
96 name,
97 orig_broadcast_subnet->subnet_name ));
98 return;
99 }
100
101 /*
102 * Check that none of the IP addresses we are returning is on the
103 * same broadcast subnet as the original requesting packet. If it
104 * is then don't reply (although we still need to add the name
105 * to the cache) as the actual machine will be replying also
106 * and we don't want two replies to a broadcast query.
107 */
108
109 if(namerec && original_packet->packet.nmb.header.nm_flags.bcast) {
110 for( i = 0; i < namerec->data.num_ips; i++) {
111 if( same_net( namerec->data.ip[i], orig_broadcast_subnet->myip,
112 orig_broadcast_subnet->mask_ip ) ) {
113 DEBUG( 5, ( "wins_proxy_name_query_request_success: name %s is a WINS \
114proxy name and is also on the same subnet (%s) as the requestor. \
115Not replying.\n", nmb_namestr(&namerec->name), orig_broadcast_subnet->subnet_name ) );
116 return;
117 }
118 }
119 }
120
121 /* Finally reply to the original name query. */
122 reply_netbios_packet(original_packet, /* Packet to reply to. */
123 0, /* Result code. */
124 NMB_QUERY, /* nmbd type code. */
125 NMB_NAME_QUERY_OPCODE, /* opcode. */
126 ttl, /* ttl. */
127 rrec->rdata, /* data to send. */
128 rrec->rdlength); /* data length. */
129}
130
131/****************************************************************************
132Function called when the name lookup failed.
133****************************************************************************/
134
135static void wins_proxy_name_query_request_fail(struct subnet_record *subrec,
136 struct response_record *rrec,