summaryrefslogtreecommitdiff
path: root/source4/libcli/smb_composite/connect_multi.c
blob: a539ba4bc624ad2f900482c2a5afedd56d29623e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* 
   Unix SMB/CIFS implementation.

   Copyright (C) Volker Lendecke
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
  a composite API to fire connect calls to multiple targets, picking the first
  one.
*/

#include "includes.h"
#include "libcli/raw/libcliraw.h"
#include "libcli/composite/composite.h"
#include "libcli/smb_composite/smb_composite.h"

struct connectmulti_state {
	struct smb_composite_connectmulti *io;
	struct composite_context *creq;
	struct smbcli_socket *result;
	int num_socks, socks_left;
	struct smbcli_socket **socks;
	struct composite_context **creqs;
};

static void connect_receive(struct composite_context *c)

{
	struct connectmulti_state *state =
		talloc_get_type(c->async.private_data,
				struct connectmulti_state);
	int i;

	for (i=0; i<state->num_socks; i++) {
		if (state->creqs[i] == c) {
			break;
		}
	}

	if (i == state->num_socks) {
		c->status = NT_STATUS_INTERNAL_ERROR;
		c->state = COMPOSITE_STATE_ERROR;
		if (state->creq->async.fn != NULL) {
			state->creq->async.fn(state->creq);
		}
		return;
	}

	state->creq->status = smbcli_sock_connect_recv(c);
	if (!NT_STATUS_IS_OK(state->creq->status)) {
		talloc_free(state->socks[i]);
		state->socks[i] = NULL;
		state->creqs[i] = NULL;
		state->socks_left -= 1;
		if (state->socks_left == 0) {
			state->creq->state = COMPOSITE_STATE_ERROR;
			if (state->creq->async.fn != NULL) {
				state->creq->async.fn(state->creq);
			}
		}
		return;
	}

	state->result = talloc_steal(state, state->socks[i]);
	talloc_free(state->socks);

	state->creq->state = COMPOSITE_STATE_DONE;
	if (state->creq->async.fn != NULL) {
		state->creq->async.fn(state->creq);
	}
}

struct composite_context *smb_composite_connectmulti_send(struct smb_composite_connectmulti *io,
							  TALLOC_CTX *mem_ctx,
							  struct event_context *event_ctx)
{
	struct composite_context *c;
	struct connectmulti_state *state;
	int num_socks = io->in.num_dests;
	const char **hostnames = io->in.hostnames;
	const char **addresses = io->in.addresses;
	int *ports = io->in.ports;
	int i;

	c = talloc_zero(mem_ctx, struct composite_context);
	if (c == NULL) goto failed;

	state = talloc(c, struct connectmulti_state);
	if (state == NULL) goto failed;

	c->state = COMPOSITE_STATE_IN_PROGRESS;
	c->event_ctx = talloc_reference(c, event_ctx);
	c->private_data = state;

	if (ports == NULL) {
		int j, nports;
		const char **smb_ports = lp_smb_ports();

		for (nports=0; smb_ports[nports]; nports++) /* noop */;

		num_socks *= nports;
		hostnames = talloc_array(state, const char *, num_socks);
		if (hostnames == NULL) goto failed;
		addresses = talloc_array(state, const char *, num_socks);
		if (addresses == NULL) goto failed;
		ports = talloc_array(state, int, num_socks);
		if (ports == NULL) goto failed;

		for (i=0; i<io->in.num_dests; i++) {
			for (j=0; j<nports; j++) {
				hostnames[i*nports+j] = io->in.hostnames[i];
				addresses[i*nports+j] = io->in.addresses[i];
				ports[i*nports+j] = atoi(smb_ports[j]);
			}
		}
	}

	state->io = io;
	state->creq = c;
	state->num_socks = num_socks;
	state->socks_left = num_socks;
	state->socks = talloc_array(state, struct smbcli_socket *, num_socks);
	state->creqs = talloc_array(state, struct composite_context *,
				    num_socks);
	if ((state->socks == NULL) || (state->creqs == NULL)) goto failed;

	for (i=0; i<num_socks; i++) {
		state->socks[i] = smbcli_sock_init(state->socks, event_ctx);
		if (state->socks[i] == NULL) goto failed;

		/* If the event_ctx we got given is NULL, the first socket
		 * creates one and all others need to refer to it. */
		event_ctx = state->socks[i]->event.ctx;

		state->creqs[i] = smbcli_sock_connect_send(state->socks[i],
							   addresses[i],
							   ports[i],
							   hostnames[i]);
		if (state->creqs[i] == NULL) goto failed;
		state->creqs[i]->async.fn = connect_receive;
		state->creqs[i]->async.private_data = state;
	}

	return c;

 failed:
	talloc_free(c);
	return NULL;
}

NTSTATUS smb_composite_connectmulti_recv(struct composite_context *c,
					 TALLOC_CTX *mem_ctx)
{
	NTSTATUS status;
	status = composite_wait(c);

	if (NT_STATUS_IS_OK(status)) {
		struct connectmulti_state *state =
			talloc_get_type(c->private_data,
					struct connectmulti_state);
		state->io->out.socket = talloc_steal(mem_ctx, state->result);
	}

	talloc_free(c);
	return status;
}

NTSTATUS smb_composite_connectmulti(struct smb_composite_connectmulti *io,
				    TALLOC_CTX *mem_ctx,
				    struct event_context *ev)
{
	struct composite_context *c =
		smb_composite_connectmulti_send(io, mem_ctx, ev);
	return smb_composite_connectmulti_recv(c, mem_ctx);
}