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
;
36 const char *default_domain
;
39 struct resolve_file_state
{
40 struct socket_address
**addrs
;
45 broadcast name resolution method - async send
48 general name resolution - async send
50 struct composite_context
*resolve_name_file_send(TALLOC_CTX
*mem_ctx
,
51 struct tevent_context
*event_ctx
,
52 void *userdata
, uint32_t flags
,
54 struct nbt_name
*name
)
56 struct composite_context
*c
;
57 struct resolve_file_data
*data
= talloc_get_type_abort(userdata
, struct resolve_file_data
);
58 struct resolve_file_state
*state
;
59 struct sockaddr_storage
*resolved_iplist
;
60 int resolved_count
, i
;
63 bool srv_lookup
= (flags
& RESOLVE_NAME_FLAG_DNS_SRV
);
65 if (event_ctx
== NULL
) {
69 c
= composite_create(mem_ctx
, event_ctx
);
70 if (c
== NULL
) return NULL
;
72 /* This isn't an NBT layer resolver */
73 if (flags
& RESOLVE_NAME_FLAG_FORCE_NBT
) {
74 composite_error(c
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
78 if (composite_nomem(c
->event_ctx
, c
)) return c
;
80 state
= talloc_zero(c
, struct resolve_file_state
);
81 if (composite_nomem(state
, c
)) return c
;
82 c
->private_data
= state
;
84 dns_name
= name
->name
;
85 if (strchr(dns_name
, '.') == NULL
) {
86 dns_name
= talloc_asprintf(state
, "%s.%s", dns_name
, data
->default_domain
);
89 c
->status
= resolve_dns_hosts_file_as_sockaddr(data
->lookup_file
, dns_name
,
90 srv_lookup
, state
, &resolved_iplist
, &resolved_count
);
91 if (!composite_is_ok(c
)) return c
;
93 for (i
=0; i
< resolved_count
; i
++) {
94 state
->addrs
= talloc_realloc(state
, state
->addrs
, struct socket_address
*, i
+2);
95 if (composite_nomem(state
->addrs
, c
)) return c
;
97 if (!(flags
& RESOLVE_NAME_FLAG_OVERWRITE_PORT
)) {
98 set_sockaddr_port((struct sockaddr
*)&resolved_iplist
[i
], port
);
101 state
->addrs
[i
] = socket_address_from_sockaddr(state
->addrs
, (struct sockaddr
*)&resolved_iplist
[i
], sizeof(resolved_iplist
[i
]));
102 if (composite_nomem(state
->addrs
[i
], c
)) return c
;
104 state
->addrs
[i
+1] = NULL
;
107 state
->names
= talloc_realloc(state
, state
->names
, char *, i
+2);
108 if (composite_nomem(state
->addrs
, c
)) return c
;
110 state
->names
[i
] = talloc_strdup(state
->names
, dns_name
);
111 if (composite_nomem(state
->names
[i
], c
)) return c
;
113 state
->names
[i
+1] = NULL
;
123 general name resolution method - recv side
125 NTSTATUS
resolve_name_file_recv(struct composite_context
*c
,
127 struct socket_address
***addrs
,
132 status
= composite_wait(c
);
134 if (NT_STATUS_IS_OK(status
)) {
135 struct resolve_file_state
*state
= talloc_get_type(c
->private_data
, struct resolve_file_state
);
136 *addrs
= talloc_steal(mem_ctx
, state
->addrs
);
138 *names
= talloc_steal(mem_ctx
, state
->names
);
147 bool resolve_context_add_file_method(struct resolve_context
*ctx
, const char *lookup_file
, const char *default_domain
)
149 struct resolve_file_data
*data
= talloc(ctx
, struct resolve_file_data
);
150 data
->lookup_file
= talloc_strdup(data
, lookup_file
);
151 data
->default_domain
= talloc_strdup(data
, default_domain
);
152 return resolve_context_add_method(ctx
, resolve_name_file_send
, resolve_name_file_recv
, data
);
155 bool resolve_context_add_file_method_lp(struct resolve_context
*ctx
, struct loadparm_context
*lp_ctx
)
157 return resolve_context_add_file_method(ctx
,
158 lpcfg_parm_string(lp_ctx
, NULL
, "resolv", "host file"),
159 lpcfg_dnsdomain(lp_ctx
));