r5114: the nbtd task can now act as a basic B-node server. It registers its
[samba.git] / source / 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 /* check a condition on an incoming packet */
29 #define NBT_ASSERT_PACKET(packet, src_address, test) do { \
30         if (!(test)) { \
31                 nbt_bad_packet(packet, src_address, #test); \
32                 return; \
33         } \
34 } while (0)
35
36
37 /*
38   send a name query reply
39 */
40 static void nbt_name_query_reply(struct nbt_name_socket *nbtsock, 
41                                  struct nbt_name_packet *request_packet, 
42                                  const char *src_address, int src_port,
43                                  struct nbt_name *name, uint32_t ttl,
44                                  uint16_t nb_flags, const char *address)
45 {
46         struct nbt_name_packet *packet;
47
48         packet = talloc_zero(nbtsock, struct nbt_name_packet);
49         if (packet == NULL) return;
50
51         packet->name_trn_id = request_packet->name_trn_id;
52         packet->ancount = 1;
53         packet->operation = 
54                 NBT_FLAG_REPLY | 
55                 NBT_OPCODE_QUERY | 
56                 NBT_FLAG_AUTHORITIVE |
57                 NBT_FLAG_RECURSION_DESIRED |
58                 NBT_FLAG_RECURSION_AVAIL;
59
60         packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
61         if (packet->answers == NULL) goto failed;
62
63         packet->answers[0].name     = *name;
64         packet->answers[0].rr_type  = NBT_QTYPE_NETBIOS;
65         packet->answers[0].rr_class = NBT_QCLASS_IP;
66         packet->answers[0].ttl      = ttl;
67         packet->answers[0].rdata.netbios.length = 6;
68         packet->answers[0].rdata.netbios.addresses = talloc_array(packet->answers,
69                                                             struct nbt_rdata_address, 1);
70         if (packet->answers[0].rdata.netbios.addresses == NULL) goto failed;
71         packet->answers[0].rdata.netbios.addresses[0].nb_flags = nb_flags;
72         packet->answers[0].rdata.netbios.addresses[0].ipaddr = htonl(inet_addr(address));
73
74         DEBUG(7,("Sending name query reply for %s<%02x> at %s to %s:%d\n", 
75                  name->name, name->type, src_address, address, src_port));
76         
77         nbt_name_reply_send(nbtsock, src_address, src_port, packet);
78
79 failed:
80         talloc_free(packet);
81 }
82
83
84 /*
85   answer a name query
86 */
87 void nbt_request_query(struct nbt_name_socket *nbtsock, 
88                        struct nbt_name_packet *packet, 
89                        const char *src_address, int src_port)
90 {
91         struct nbt_interface *iface;
92         struct nbt_iface_name *iname;
93         struct nbt_name *name;
94
95         /* if its a WINS query then direct to our WINS server */
96         if ((packet->operation & NBT_FLAG_RECURSION_DESIRED) &&
97             !(packet->operation & NBT_FLAG_BROADCAST)) {
98                 nbt_query_wins(nbtsock, packet, src_address, src_port);
99                 return;
100         }
101
102         /* find the interface for this query */
103         iface = nbt_iface_find(nbtsock, src_address);
104
105         NBT_ASSERT_PACKET(packet, src_address, packet->qdcount == 1);
106         NBT_ASSERT_PACKET(packet, src_address, packet->questions[0].question_type == NBT_QTYPE_NETBIOS);
107         NBT_ASSERT_PACKET(packet, src_address, packet->questions[0].question_class == NBT_QCLASS_IP);
108
109         /* see if we have the requested name on this interface */
110         name = &packet->questions[0].name;
111
112         iname = nbt_find_iname(iface, name, NBT_NM_ACTIVE);
113         if (iname == NULL) {
114                 DEBUG(7,("Query for %s<%02x> from %s - not found on %s\n",
115                          name->name, name->type, src_address, iface->ip_address));
116                 return;
117         }
118
119         nbt_name_query_reply(nbtsock, packet, src_address, src_port,
120                              &iname->name, iname->ttl, iname->nb_flags, 
121                              iface->ip_address);
122 }