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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/*
Unix SMB/CIFS implementation.
implements a non-blocking connect operation that is aware of the samba4
events system
Copyright (C) Andrew Tridgell 2005
Copyright (C) Volker Lendecke 2005
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.
*/
#include "includes.h"
#include "lib/socket/socket.h"
#include "lib/events/events.h"
#include "libcli/composite/composite.h"
#include "libcli/resolve/resolve.h"
struct connect_state {
struct socket_context *sock;
const struct socket_address *my_address;
const struct socket_address *server_address;
uint32_t flags;
};
static void socket_connect_handler(struct event_context *ev,
struct fd_event *fde,
uint16_t flags, void *private);
static void continue_resolve_name(struct composite_context *ctx);
static void continue_socket_connect(struct composite_context *creq);
/*
call the real socket_connect() call, and setup event handler
*/
static void socket_send_connect(struct composite_context *result)
{
struct composite_context *creq;
struct fd_event *fde;
struct connect_state *state = talloc_get_type(result->private_data,
struct connect_state);
creq = talloc_zero(state, struct composite_context);
if (composite_nomem(creq, result)) return;
creq->state = COMPOSITE_STATE_IN_PROGRESS;
creq->event_ctx = result->event_ctx;
creq->async.fn = continue_socket_connect;
creq->async.private_data = result;
result->status = socket_connect(state->sock,
state->my_address,
state->server_address,
state->flags);
if (NT_STATUS_IS_ERR(result->status) &&
!NT_STATUS_EQUAL(result->status,
NT_STATUS_MORE_PROCESSING_REQUIRED)) {
composite_error(result, result->status);
return;
}
fde = event_add_fd(result->event_ctx, result,
socket_get_fd(state->sock),
EVENT_FD_READ|EVENT_FD_WRITE,
socket_connect_handler, result);
composite_nomem(fde, result);
}
/*
send a socket connect, potentially doing some name resolution first
*/
struct composite_context *socket_connect_send(struct socket_context *sock,
struct socket_address *my_address,
struct socket_address *server_address,
uint32_t flags,
struct event_context *event_ctx)
{
struct composite_context *result;
struct connect_state *state;
result = talloc_zero(sock, struct composite_context);
if (result == NULL) return NULL;
result->state = COMPOSITE_STATE_IN_PROGRESS;
result->event_ctx = event_ctx;
state = talloc_zero(result, struct connect_state);
if (composite_nomem(state, result)) return result;
result->private_data = state;
state->sock = talloc_reference(state, sock);
if (composite_nomem(state->sock, result)) return result;
if (my_address) {
void *ref = talloc_reference(state, my_address);
if (composite_nomem(ref, result)) {
return result;
}
state->my_address = my_address;
}
{
void *ref = talloc_reference(state, server_address);
if (composite_nomem(ref, result)) {
return result;
}
state->server_address = server_address;
}
state->flags = flags;
set_blocking(socket_get_fd(sock), False);
if (server_address->addr && strcmp(sock->backend_name, "ipv4") == 0) {
struct nbt_name name;
struct composite_context *creq;
make_nbt_name_client(&name, server_address->addr);
creq = resolve_name_send(&name, result->event_ctx,
lp_name_resolve_order());
if (composite_nomem(creq, result)) return result;
composite_continue(result, creq, continue_resolve_name, result);
return result;
}
socket_send_connect(result);
return result;
}
/*
handle write events on connect completion
*/
static void socket_connect_handler(struct event_context *ev,
struct fd_event *fde,
uint16_t flags, void *private)
{
struct composite_context *result =
talloc_get_type(private, struct composite_context);
struct connect_state *state = talloc_get_type(result->private_data,
struct connect_state);
result->status = socket_connect_complete(state->sock, state->flags);
if (!composite_is_ok(result)) return;
composite_done(result);
}
/*
recv name resolution reply then send the connect
*/
static void continue_resolve_name(struct composite_context *creq)
{
struct composite_context *result = talloc_get_type(creq->async.private_data,
struct composite_context);
struct connect_state *state = talloc_get_type(result->private_data, struct connect_state);
const char *addr;
result->status = resolve_name_recv(creq, state, &addr);
if (!composite_is_ok(result)) return;
state->server_address = socket_address_from_strings(state, state->sock->backend_name,
addr, state->server_address->port);
if (composite_nomem(state->server_address, result)) return;
socket_send_connect(result);
}
/*
called when a connect has finished. Complete the top level composite context
*/
static void continue_socket_connect(struct composite_context *creq)
{
struct composite_context *result = talloc_get_type(creq->async.private_data,
struct composite_context);
result->status = creq->status;
if (!composite_is_ok(result)) return;
composite_done(result);
}
/*
wait for a socket_connect_send() to finish
*/
NTSTATUS socket_connect_recv(struct composite_context *result)
{
NTSTATUS status = composite_wait(result);
talloc_free(result);
return status;
}
/*
like socket_connect() but takes an event context, doing a semi-async connect
*/
NTSTATUS socket_connect_ev(struct socket_context *sock,
struct socket_address *my_address,
struct socket_address *server_address,
uint32_t flags, struct event_context *ev)
{
struct composite_context *ctx;
ctx = socket_connect_send(sock, my_address,
server_address, flags, ev);
return socket_connect_recv(ctx);
}
|