r5346: - a bit more preparation for the WINS server going in
[bbaumbach/samba-autobuild/.git] / source4 / nbt_server / query.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    answer name queries
5
6    Copyright (C) Andrew Tridgell        2005
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 #include "includes.h"
24 #include "dlinklist.h"
25 #include "system/network.h"
26 #include "nbt_server/nbt_server.h"
27
28
29 /*
30   answer a name query
31 */
32 void nbtd_request_query(struct nbt_name_socket *nbtsock, 
33                         struct nbt_name_packet *packet, 
34                         const char *src_address, int src_port)
35 {
36         struct nbtd_iface_name *iname;
37         struct nbt_name *name;
38         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private, 
39                                                        struct nbtd_interface);
40
41         /* see if its a node status query */
42         if (packet->qdcount == 1 &&
43             packet->questions[0].question_type == NBT_QTYPE_STATUS) {
44                 nbtd_query_status(nbtsock, packet, src_address, src_port);
45                 return;
46         }
47
48         NBTD_ASSERT_PACKET(packet, src_address, packet->qdcount == 1);
49         NBTD_ASSERT_PACKET(packet, src_address, 
50                            packet->questions[0].question_type == NBT_QTYPE_NETBIOS);
51         NBTD_ASSERT_PACKET(packet, src_address, 
52                            packet->questions[0].question_class == NBT_QCLASS_IP);
53
54         /* see if we have the requested name on this interface */
55         name = &packet->questions[0].name;
56
57         iname = nbtd_find_iname(iface, name, 0);
58
59         if (iname == NULL) {
60                 /* don't send negative replies to broadcast queries */
61                 if (packet->operation & NBT_FLAG_BROADCAST) {
62                         return;
63                 }
64
65                 /* if the name does not exist, then redirect to WINS
66                    server if recursion has been asked for */
67                 if (packet->operation & NBT_FLAG_RECURSION_DESIRED) {
68                         nbtd_winsserver_request(nbtsock, packet, src_address, src_port);
69                         return;
70                 }
71
72                 /* otherwise send a negative reply */
73                 nbtd_negative_name_query_reply(nbtsock, packet, 
74                                                src_address, src_port);
75                 return;
76         }
77
78         /* if the name is not yet active and its a broadcast query then
79            ignore it for now */
80         if (!(iname->nb_flags & NBT_NM_ACTIVE) && 
81             (packet->operation & NBT_FLAG_BROADCAST)) {
82                 DEBUG(7,("Query for %s<%02x> from %s - name not active yet on %s\n",
83                          name->name, name->type, src_address, iface->ip_address));
84                 return;
85         }
86
87         nbtd_name_query_reply(nbtsock, packet, src_address, src_port,
88                               &iname->name, iname->ttl, iname->nb_flags, 
89                               nbtd_address_list(iface, packet));
90 }