2 Unix SMB/CIFS implementation.
4 broadcast name resolution module
6 Copyright (C) Andrew Tridgell 1994-1998,2005
7 Copyright (C) Jeremy Allison 2007
8 Copyright (C) Jelmer Vernooij 2007
9 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009-2010
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libcli/composite/composite.h"
26 #include "libcli/resolve/resolve.h"
27 #include "lib/socket/socket.h"
28 #include "system/network.h"
29 #include "lib/socket/netif.h"
30 #include "param/param.h"
31 #include "lib/util/util_net.h"
32 #include "libcli/nbt/libnbt.h"
34 struct resolve_file_data {
35 const char *lookup_file;
38 struct resolve_file_state {
39 struct socket_address **addrs;
44 broadcast name resolution method - async send
47 general name resolution - async send
49 struct composite_context *resolve_name_file_send(TALLOC_CTX *mem_ctx,
50 struct tevent_context *event_ctx,
51 void *userdata, uint32_t flags,
53 struct nbt_name *name)
55 struct composite_context *c;
56 struct resolve_file_data *data = talloc_get_type_abort(userdata, struct resolve_file_data);
57 struct resolve_file_state *state;
58 struct sockaddr_storage *resolved_iplist;
59 int resolved_count, i;
61 bool srv_lookup = (flags & RESOLVE_NAME_FLAG_DNS_SRV);
63 if (event_ctx == NULL) {
67 c = composite_create(mem_ctx, event_ctx);
68 if (c == NULL) return NULL;
70 /* This isn't an NBT layer resolver */
71 if (flags & RESOLVE_NAME_FLAG_FORCE_NBT || name->type != NBT_NAME_SERVER) {
72 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
76 if (composite_nomem(c->event_ctx, c)) return c;
78 state = talloc_zero(c, struct resolve_file_state);
79 if (composite_nomem(state, c)) return c;
80 c->private_data = state;
83 c->status = resolve_dns_hosts_file_as_sockaddr(data->lookup_file, name->name, srv_lookup, state, &resolved_iplist, &resolved_count);
84 if (!composite_is_ok(c)) return c;
86 for (i=0; i < resolved_count; i++) {
87 state->addrs = talloc_realloc(state, state->addrs, struct socket_address *, i+2);
88 if (composite_nomem(state->addrs, c)) return c;
90 if (!(flags & RESOLVE_NAME_FLAG_OVERWRITE_PORT)) {
91 set_sockaddr_port((struct sockaddr *)&resolved_iplist[i], port);
94 state->addrs[i] = socket_address_from_sockaddr(state->addrs, (struct sockaddr *)&resolved_iplist[i], sizeof(resolved_iplist[i]));
95 if (composite_nomem(state->addrs[i], c)) return c;
97 state->addrs[i+1] = NULL;
100 state->names = talloc_realloc(state, state->names, char *, i+2);
101 if (composite_nomem(state->addrs, c)) return c;
103 state->names[i] = talloc_strdup(state->names, name->name);
104 if (composite_nomem(state->names[i], c)) return c;
106 state->names[i+1] = NULL;
116 general name resolution method - recv side
118 NTSTATUS resolve_name_file_recv(struct composite_context *c,
120 struct socket_address ***addrs,
125 status = composite_wait(c);
127 if (NT_STATUS_IS_OK(status)) {
128 struct resolve_file_state *state = talloc_get_type(c->private_data, struct resolve_file_state);
129 *addrs = talloc_steal(mem_ctx, state->addrs);
131 *names = talloc_steal(mem_ctx, state->names);
140 bool resolve_context_add_file_method(struct resolve_context *ctx, const char *lookup_file)
142 struct resolve_file_data *data = talloc(ctx, struct resolve_file_data);
143 data->lookup_file = lookup_file;
144 return resolve_context_add_method(ctx, resolve_name_file_send, resolve_name_file_recv, data);
147 bool resolve_context_add_file_method_lp(struct resolve_context *ctx, struct loadparm_context *lp_ctx)
149 return resolve_context_add_file_method(ctx, lp_parm_string(lp_ctx, NULL, "resolv", "host file"));