Merge commit 'release-4-0-0alpha2' into v4-0-test
[gd/samba-autobuild/.git] / source4 / nbt_server / wins / wins_dns_proxy.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    wins server dns proxy
5
6    Copyright (C) Stefan Metzmacher      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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "nbt_server/nbt_server.h"
24 #include "nbt_server/wins/winsdb.h"
25 #include "nbt_server/wins/winsserver.h"
26 #include "system/time.h"
27 #include "libcli/composite/composite.h"
28 #include "smbd/service_task.h"
29 #include "libcli/resolve/resolve.h"
30
31 struct wins_dns_proxy_state {
32         struct nbt_name_socket *nbtsock;
33         struct nbt_name_packet *packet;
34         struct socket_address *src;
35 };
36
37 static void nbtd_wins_dns_proxy_handler(struct composite_context *creq)
38 {
39         NTSTATUS status;
40         struct wins_dns_proxy_state *s = talloc_get_type(creq->async.private_data,
41                                                          struct wins_dns_proxy_state);
42         struct nbt_name *name = &s->packet->questions[0].name;
43         const char *address;
44         const char **addresses;
45         uint16_t nb_flags = 0; /* TODO: ... */
46
47         status = resolve_name_recv(creq, s->packet, &address);
48         if (!NT_STATUS_IS_OK(status)) {
49                 goto notfound;
50         }
51
52         addresses = str_list_add(NULL, address);
53         talloc_steal(s->packet, addresses);
54         if (!addresses) goto notfound;
55
56         nbtd_name_query_reply(s->nbtsock, s->packet, s->src, name, 
57                               0, nb_flags, addresses);
58         return;
59 notfound:
60         nbtd_negative_name_query_reply(s->nbtsock, s->packet, s->src);
61 }
62
63 /*
64   dns proxy query a name
65 */
66 void nbtd_wins_dns_proxy_query(struct nbt_name_socket *nbtsock, 
67                                struct nbt_name_packet *packet, 
68                                struct socket_address *src)
69 {
70         struct nbt_name *name = &packet->questions[0].name;
71         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private,
72                                                        struct nbtd_interface);
73         struct wins_dns_proxy_state *s;
74         struct composite_context *creq;
75         struct resolve_context *resolve_ctx;
76
77         s = talloc(nbtsock, struct wins_dns_proxy_state);
78         if (!s) goto failed;
79         s->nbtsock      = nbtsock;
80         s->packet       = talloc_steal(s, packet);
81         s->src          = src;
82         if (!talloc_reference(s, src)) {
83                 goto failed;
84         }
85
86         resolve_ctx = resolve_context_init(s);
87         if (resolve_ctx == NULL) goto failed;
88         resolve_context_add_host_method(resolve_ctx);
89
90         creq = resolve_name_send(resolve_ctx, name, iface->nbtsrv->task->event_ctx);
91         if (!creq) goto failed;
92
93         creq->async.fn          = nbtd_wins_dns_proxy_handler;
94         creq->async.private_data= s;
95         return;
96 failed:
97         nbtd_negative_name_query_reply(nbtsock, packet, src);
98 }