param: Use IDL-based constants for NBT and NBT dgram ports
[Samba.git] / source4 / libcli / resolve / lmhosts.c
blob21cc3e4f00bc4a337eb504f2754d3b6496832089
1 /*
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-2014
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/>.
24 #include "includes.h"
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"
33 #include "dynconfig.h"
35 struct resolve_lmhosts_state {
36 struct socket_address **addrs;
37 char **names;
40 /**
41 broadcast name resolution method - async send
44 general name resolution - async send
46 struct composite_context *resolve_name_lmhosts_send(TALLOC_CTX *mem_ctx,
47 struct tevent_context *event_ctx,
48 void *userdata, uint32_t flags,
49 uint16_t port,
50 struct nbt_name *name)
52 struct composite_context *c;
53 struct resolve_lmhosts_state *state;
54 struct sockaddr_storage *resolved_iplist;
55 int resolved_count, i;
57 if (event_ctx == NULL) {
58 return NULL;
61 c = composite_create(mem_ctx, event_ctx);
62 if (c == NULL) return NULL;
64 if (composite_nomem(c->event_ctx, c)) return c;
66 state = talloc_zero(c, struct resolve_lmhosts_state);
67 if (composite_nomem(state, c)) return c;
68 c->private_data = state;
70 c->status = resolve_lmhosts_file_as_sockaddr(dyn_LMHOSTSFILE, name->name, name->type,
71 state, &resolved_iplist, &resolved_count);
72 if (!composite_is_ok(c)) return c;
74 for (i=0; i < resolved_count; i++) {
75 state->addrs = talloc_realloc(state, state->addrs, struct socket_address *, i+2);
76 if (composite_nomem(state->addrs, c)) return c;
78 set_sockaddr_port((struct sockaddr *)&resolved_iplist[i], port);
80 state->addrs[i] = socket_address_from_sockaddr(state->addrs, (struct sockaddr *)&resolved_iplist[i], sizeof(resolved_iplist[i]));
81 if (composite_nomem(state->addrs[i], c)) return c;
83 state->addrs[i+1] = NULL;
86 state->names = talloc_realloc(state, state->names, char *, i+2);
87 if (composite_nomem(state->addrs, c)) return c;
89 state->names[i] = talloc_strdup(state->names, name->name);
90 if (composite_nomem(state->names[i], c)) return c;
92 state->names[i+1] = NULL;
94 i++;
97 composite_done(c);
98 return c;
102 general name resolution method - recv side
104 NTSTATUS resolve_name_lmhosts_recv(struct composite_context *c,
105 TALLOC_CTX *mem_ctx,
106 struct socket_address ***addrs,
107 char ***names)
109 NTSTATUS status;
111 status = composite_wait(c);
113 if (NT_STATUS_IS_OK(status)) {
114 struct resolve_lmhosts_state *state = talloc_get_type(c->private_data, struct resolve_lmhosts_state);
115 *addrs = talloc_steal(mem_ctx, state->addrs);
116 if (names) {
117 *names = talloc_steal(mem_ctx, state->names);
121 talloc_free(c);
122 return status;
126 bool resolve_context_add_lmhosts_method(struct resolve_context *ctx)
128 return resolve_context_add_method(ctx, resolve_name_lmhosts_send, resolve_name_lmhosts_recv, NULL);