vfs_io_uring: move error handling out of vfs_io_uring_pread_recv()
[Samba.git] / source3 / winbindd / winbindd_wins_byname.c
blob8661401331a52531749f83e47a8e9b755c23b694
1 /*
2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_WINS_BYNAME
4 Copyright (C) Volker Lendecke 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "libsmb/namequery.h"
23 #include "librpc/gen_ndr/ndr_winbind_c.h"
24 #include "libsmb/nmblib.h"
25 #include "lib/util/string_wrappers.h"
27 struct winbindd_wins_byname_state {
28 struct tevent_context *ev;
29 struct winbindd_request *request;
30 struct sockaddr_storage *addrs;
31 int num_addrs;
34 static void winbindd_wins_byname_wins_done(struct tevent_req *subreq);
35 static void winbindd_wins_byname_bcast_done(struct tevent_req *subreq);
37 struct tevent_req *winbindd_wins_byname_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct winbindd_cli_state *cli,
40 struct winbindd_request *request)
42 struct tevent_req *req, *subreq;
43 struct winbindd_wins_byname_state *state;
45 req = tevent_req_create(mem_ctx, &state,
46 struct winbindd_wins_byname_state);
47 if (req == NULL) {
48 return NULL;
50 state->ev = ev;
51 state->request = request;
53 /* Ensure null termination */
54 request->data.winsreq[sizeof(request->data.winsreq)-1]='\0';
56 DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)cli->pid,
57 request->data.winsreq));
59 subreq = resolve_wins_send(state, ev, state->request->data.winsreq,
60 0x20);
61 if (tevent_req_nomem(subreq, req)) {
62 return tevent_req_post(req, ev);
64 tevent_req_set_callback(subreq, winbindd_wins_byname_wins_done, req);
65 return req;
68 static void winbindd_wins_byname_wins_done(struct tevent_req *subreq)
70 struct tevent_req *req = tevent_req_callback_data(
71 subreq, struct tevent_req);
72 struct winbindd_wins_byname_state *state = tevent_req_data(
73 req, struct winbindd_wins_byname_state);
74 NTSTATUS status;
76 status = resolve_wins_recv(subreq, talloc_tos(), &state->addrs,
77 &state->num_addrs, NULL);
78 TALLOC_FREE(subreq);
79 if (NT_STATUS_IS_OK(status)) {
80 tevent_req_done(req);
81 return;
83 subreq = name_resolve_bcast_send(state, state->ev,
84 state->request->data.winsreq, 0x20);
85 if (tevent_req_nomem(subreq, req)) {
86 return;
88 tevent_req_set_callback(subreq, winbindd_wins_byname_bcast_done, req);
91 static void winbindd_wins_byname_bcast_done(struct tevent_req *subreq)
93 struct tevent_req *req = tevent_req_callback_data(
94 subreq, struct tevent_req);
95 struct winbindd_wins_byname_state *state = tevent_req_data(
96 req, struct winbindd_wins_byname_state);
97 NTSTATUS status;
99 status = name_resolve_bcast_recv(subreq, talloc_tos(), &state->addrs,
100 &state->num_addrs);
101 TALLOC_FREE(subreq);
102 if (tevent_req_nterror(req, status)) {
103 return;
105 tevent_req_done(req);
108 NTSTATUS winbindd_wins_byname_recv(struct tevent_req *req,
109 struct winbindd_response *presp)
111 struct winbindd_wins_byname_state *state = tevent_req_data(
112 req, struct winbindd_wins_byname_state);
113 char *response;
114 NTSTATUS status;
115 int i;
117 if (tevent_req_is_nterror(req, &status)) {
118 return status;
121 response = talloc_strdup(talloc_tos(), "");
122 if (response == NULL) {
123 return NT_STATUS_NO_MEMORY;
126 for (i=0; i<state->num_addrs; i++) {
127 char addr[INET6_ADDRSTRLEN];
128 print_sockaddr(addr, sizeof(addr), &state->addrs[i]);
130 response = talloc_asprintf_append_buffer(
131 response, "%s%s", addr,
132 i < (state->num_addrs-1) ? " " : "");
133 if (response == NULL) {
134 return NT_STATUS_NO_MEMORY;
138 response = talloc_asprintf_append_buffer(
139 response, "\t%s\n", state->request->data.winsreq);
140 if (response == NULL) {
141 return NT_STATUS_NO_MEMORY;
144 if (talloc_get_size(response) > sizeof(presp->data.winsresp)) {
145 TALLOC_FREE(response);
146 return NT_STATUS_MARSHALL_OVERFLOW;
148 fstrcpy(presp->data.winsresp, response);
149 TALLOC_FREE(response);
150 return NT_STATUS_OK;