r13924: Split more prototypes out of include/proto.h + initial work on header
[kai/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 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 "nbt_server/nbt_server.h"
25 #include "nbt_server/wins/winsdb.h"
26 #include "nbt_server/wins/winsserver.h"
27 #include "system/time.h"
28 #include "libcli/composite/composite.h"
29 #include "smbd/service_task.h"
30 #include "libcli/resolve/resolve.h"
31
32 struct wins_dns_proxy_state {
33         struct nbt_name_socket *nbtsock;
34         struct nbt_name_packet *packet;
35         struct socket_address *src;
36 };
37
38 static void nbtd_wins_dns_proxy_handler(struct composite_context *creq)
39 {
40         NTSTATUS status;
41         struct wins_dns_proxy_state *s = talloc_get_type(creq->async.private_data,
42                                                          struct wins_dns_proxy_state);
43         struct nbt_name *name = &s->packet->questions[0].name;
44         const char *address;
45         const char **addresses;
46         uint16_t nb_flags = 0; /* TODO: ... */
47
48         status = resolve_name_recv(creq, s->packet, &address);
49         if (!NT_STATUS_IS_OK(status)) {
50                 goto notfound;
51         }
52
53         addresses = str_list_add(NULL, address);
54         talloc_steal(s->packet, addresses);
55         if (!addresses) goto notfound;
56
57         nbtd_name_query_reply(s->nbtsock, s->packet, s->src, name, 
58                               0, nb_flags, addresses);
59         return;
60 notfound:
61         nbtd_negative_name_query_reply(s->nbtsock, s->packet, s->src);
62 }
63
64 /*
65   dns proxy query a name
66 */
67 void nbtd_wins_dns_proxy_query(struct nbt_name_socket *nbtsock, 
68                                struct nbt_name_packet *packet, 
69                                struct socket_address *src)
70 {
71         struct nbt_name *name = &packet->questions[0].name;
72         struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private,
73                                                        struct nbtd_interface);
74         struct wins_dns_proxy_state *s;
75         struct composite_context *creq;
76         const char *methods[] = {
77                 "host",
78                 NULL
79         };
80
81         s = talloc(nbtsock, struct wins_dns_proxy_state);
82         if (!s) goto failed;
83         s->nbtsock      = nbtsock;
84         s->packet       = talloc_steal(s, packet);
85         s->src          = src;
86         if (!talloc_reference(s, src)) {
87                 goto failed;
88         }
89
90         creq = resolve_name_send(name, iface->nbtsrv->task->event_ctx, methods);
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 }