r4898: - removed the unused wins_srv_*() code
[bbaumbach/samba-autobuild/.git] / source4 / libcli / resolve / resolve.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    general name resolution interface
5
6    Copyright (C) Andrew Tridgell 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 "libcli/raw/libcliraw.h"
25 #include "libcli/composite/composite.h"
26
27 struct resolve_state {
28         struct nbt_name name;
29         const char **methods;
30         struct smbcli_composite *req;
31         const char *reply_addr;
32 };
33
34 static struct smbcli_composite *setup_next_method(struct smbcli_composite *c);
35
36 /*
37   handle completion of one name resolve method
38 */
39 static void resolve_handler(struct smbcli_composite *req)
40 {
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];
44
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);
49         } else {
50                 c->status = NT_STATUS_INTERNAL_ERROR;
51         }
52         
53         if (!NT_STATUS_IS_OK(c->status)) {
54                 state->methods++;
55                 state->req = setup_next_method(c);
56                 if (state->req != NULL) {
57                         return;
58                 }
59         }
60
61         if (!NT_STATUS_IS_OK(c->status)) {
62                 c->state = SMBCLI_REQUEST_ERROR;
63         } else {
64                 c->state = SMBCLI_REQUEST_DONE;
65         }
66         if (c->async.fn) {
67                 c->async.fn(c);
68         }
69 }
70
71
72 static struct smbcli_composite *setup_next_method(struct smbcli_composite *c)
73 {
74         struct resolve_state *state = talloc_get_type(c->private, struct resolve_state);
75         const char *method;
76         struct smbcli_composite *req = NULL;
77
78         do {
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);
85                 }
86                 if (req == NULL) state->methods++;
87         } while (!req && state->methods[0]);
88
89         if (req) {
90                 req->async.fn = resolve_handler;
91                 req->async.private = c;
92         }
93
94         return req;
95 }
96
97 /*
98   general name resolution - async send
99  */
100 struct smbcli_composite *resolve_name_send(struct nbt_name *name, struct event_context *event_ctx)
101 {
102         struct smbcli_composite *c;
103         struct resolve_state *state;
104         NTSTATUS status;
105
106         c = talloc_zero(NULL, struct smbcli_composite);
107         if (c == NULL) goto failed;
108
109         state = talloc(c, struct resolve_state);
110         if (state == NULL) goto failed;
111
112         status = nbt_name_dup(state, name, &state->name);
113         if (!NT_STATUS_IS_OK(status)) goto failed;
114
115         state->methods = lp_name_resolve_order();
116         if (state->methods == NULL) {
117                 return NULL;
118         }
119
120         c->state = SMBCLI_REQUEST_SEND;
121         c->private = state;
122         c->event_ctx = talloc_reference(c, event_ctx);
123
124         state->req = setup_next_method(c);
125         if (state->req == NULL) goto failed;
126         
127         return c;
128
129 failed:
130         talloc_free(c);
131         return NULL;
132 }
133
134 /*
135   general name resolution method - recv side
136  */
137 NTSTATUS resolve_name_recv(struct smbcli_composite *c, 
138                            TALLOC_CTX *mem_ctx, const char **reply_addr)
139 {
140         NTSTATUS status;
141
142         status = smb_composite_wait(c);
143
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);
147         }
148
149         talloc_free(c);
150         return status;
151 }
152
153 /*
154   general name resolution - sync call
155  */
156 NTSTATUS resolve_name(struct nbt_name *name, TALLOC_CTX *mem_ctx, const char **reply_addr)
157 {
158         struct smbcli_composite *c = resolve_name_send(name, NULL);
159         return resolve_name_recv(c, mem_ctx, reply_addr);
160 }