2 Unix SMB/CIFS implementation.
4 general name resolution interface
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/composite/composite.h"
27 struct resolve_state {
30 struct smbcli_composite *req;
31 const char *reply_addr;
34 static struct smbcli_composite *setup_next_method(struct smbcli_composite *c);
37 handle completion of one name resolve method
39 static void resolve_handler(struct smbcli_composite *req)
41 struct smbcli_composite *c = req->async.private;
42 struct resolve_state *state = talloc_get_type(c->private, struct resolve_state);
43 const char *method = state->methods[0];
45 if (strcasecmp(method, "bcast")) {
46 c->status = resolve_name_bcast_recv(req, state, &state->reply_addr);
47 } else if (strcasecmp(method, "wins")) {
48 c->status = resolve_name_wins_recv(req, state, &state->reply_addr);
50 c->status = NT_STATUS_INTERNAL_ERROR;
53 if (!NT_STATUS_IS_OK(c->status)) {
55 state->req = setup_next_method(c);
56 if (state->req != NULL) {
61 if (!NT_STATUS_IS_OK(c->status)) {
62 c->state = SMBCLI_REQUEST_ERROR;
64 c->state = SMBCLI_REQUEST_DONE;
72 static struct smbcli_composite *setup_next_method(struct smbcli_composite *c)
74 struct resolve_state *state = talloc_get_type(c->private, struct resolve_state);
76 struct smbcli_composite *req = NULL;
79 method = state->methods[0];
80 if (method == NULL) break;
81 if (strcasecmp(method, "bcast")) {
82 req = resolve_name_bcast_send(&state->name, c->event_ctx);
83 } else if (strcasecmp(method, "wins")) {
84 req = resolve_name_wins_send(&state->name, c->event_ctx);
86 if (req == NULL) state->methods++;
87 } while (!req && state->methods[0]);
90 req->async.fn = resolve_handler;
91 req->async.private = c;
98 general name resolution - async send
100 struct smbcli_composite *resolve_name_send(struct nbt_name *name, struct event_context *event_ctx)
102 struct smbcli_composite *c;
103 struct resolve_state *state;
106 c = talloc_zero(NULL, struct smbcli_composite);
107 if (c == NULL) goto failed;
109 state = talloc(c, struct resolve_state);
110 if (state == NULL) goto failed;
112 status = nbt_name_dup(state, name, &state->name);
113 if (!NT_STATUS_IS_OK(status)) goto failed;
115 state->methods = lp_name_resolve_order();
116 if (state->methods == NULL) {
120 c->state = SMBCLI_REQUEST_SEND;
122 c->event_ctx = talloc_reference(c, event_ctx);
124 state->req = setup_next_method(c);
125 if (state->req == NULL) goto failed;
135 general name resolution method - recv side
137 NTSTATUS resolve_name_recv(struct smbcli_composite *c,
138 TALLOC_CTX *mem_ctx, const char **reply_addr)
142 status = smb_composite_wait(c);
144 if (NT_STATUS_IS_OK(status)) {
145 struct resolve_state *state = talloc_get_type(c->private, struct resolve_state);
146 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
154 general name resolution - sync call
156 NTSTATUS resolve_name(struct nbt_name *name, TALLOC_CTX *mem_ctx, const char **reply_addr)
158 struct smbcli_composite *c = resolve_name_send(name, NULL);
159 return resolve_name_recv(c, mem_ctx, reply_addr);