summaryrefslogtreecommitdiffstats
path: root/src/dispatch.c
blob: 3dd623cf1f0d203111a52ec55c93cdac1ec9acbb (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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#include <rpc/rpc.h>
#include <rpc/pmap_clnt.h>
#include <rpcsvc/yp_prot.h>

#include <nspr.h>
#include <secport.h>
#include <plarenas.h>

#include "dispatch.h"
#include "nis.h"
#include "plugin.h"
#include "portmap.h"

#define MAX_CLIENT_IDLE (60 * 1000)

struct dispatch_stream_client_params {
	int client;
	struct plugin_state *state;
};

/* Handle requests from one stream client. */
static void *
dispatch_stream_handler_thread(void *arg)
{
	int fraglen, flags, ret, frag_len, record_len, client;
	ssize_t i;
	int32_t len, nlen;
	char fragment[65540], record[65536];
	struct pollfd pollfd;
	struct dispatch_stream_client_params *params;
	struct plugin_state *state;
	bool_t done, last;

	/* Recover the full set of parameters. */
	params = arg;
	state = params->state;
	client = params->client;
	free(arg);

	slapi_log_error(SLAPI_LOG_PLUGIN,
			state->plugin_desc->spd_id,
			"opened client connection %d, thread started\n",
			client);

	/* Set the connection to be non-blocking. */
	flags = fcntl(client, F_GETFD);
	if ((flags == -1) ||
	    (fcntl(client, F_SETFD, flags | O_NONBLOCK) == -1)) {
		slapi_log_error(SLAPI_LOG_PLUGIN,
				state->plugin_desc->spd_id,
				"error setting new client connection to be "
				"non-blocking\n");
		close(client);
		return NULL;
	}
	slapi_log_error(SLAPI_LOG_PLUGIN,
			state->plugin_desc->spd_id,
			"set new client connection to be non-blocking\n");

	/* Assemble fragments and responses. */
	frag_len = 0;
	record_len = 0;
	for (;;) {
		/* Wait for incoming data. */
		pollfd.fd = client;
		pollfd.events = POLLIN | POLLHUP;
		switch (poll(&pollfd, 1, MAX_CLIENT_IDLE)) {
		case -1:
			slapi_log_error(SLAPI_LOG_PLUGIN,
					state->plugin_desc->spd_id,
					"communication error\n");
			goto done;
			break;
		case 0:
			slapi_log_error(SLAPI_LOG_PLUGIN,
					state->plugin_desc->spd_id,
					"client timeout\n");
			goto done;
			break;
		default:
			break;
		}
		/* Try to read as much data as we have space to hold. */
		i = read(client, fragment + frag_len,
			 sizeof(fragment) - frag_len);
		switch (i) {
		case -1:
			if (errno == EAGAIN) {
				continue;
			}
			slapi_log_error(SLAPI_LOG_PLUGIN,
					state->plugin_desc->spd_id,
					"communication error\n");
			goto done;
			break;
		case 0:
			slapi_log_error(SLAPI_LOG_PLUGIN,
					state->plugin_desc->spd_id,
					"client closed connection\n");
			goto done;
			break;
		default:
			break;
		}
		/* We managed to read some piece of a fragment. */
		slapi_log_error(SLAPI_LOG_PLUGIN,
				state->plugin_desc->spd_id,
				"read %d bytes\n", i);
		frag_len += i;
		/* If we can't know the fragment size yet, then wait for more
		 * data. */
		if (frag_len < 4) {
			continue;
		}
		/* Convert the fragment length information to local byte order,
		 * and determine if this is the last fragment in the record. */
		memcpy(&nlen, fragment, 4);
		len = ntohl(nlen);
		last = ((len & 0x80000000) != 0);
		len &= 0x7fffffff;
		/* If we don't have a complete fragment, then wait for more
		 * data. */
		if (frag_len < (len + 4)) {
			continue;
		}
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"expecting %d bytes\n", len);
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"in: frag_len=%d,record=%d\n",
				frag_len, record_len);
		/* Pull the fragment out of the buffer and append it to the
		 * record buffer. */
		slapi_log_error(SLAPI_LOG_PLUGIN,
				state->plugin_desc->spd_id,
				"got whole fragment (%d bytes)\n", len);
		memmove(record + record_len, fragment + 4, len);
		record_len += len;
		memmove(fragment, fragment + (len + 4), frag_len - (len + 4));
		frag_len -= (len + 4);
		/* If there are more fragments to come, then we need to wait
		 * for more data. */
		if (!last) {
			continue;
		}
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"got whole record (%d bytes)\n", record_len);
		nis_process_request(state, client, NULL, 0, record, record_len);
		record_len = 0;
		/* Note that we did it! */
		slapi_log_error(SLAPI_LOG_PLUGIN,
				state->plugin_desc->spd_id,
				"out: frag_len=%d,record=%d\n",
				frag_len, record_len);
	}
done:
	close(client);
	slapi_log_error(SLAPI_LOG_PLUGIN,
			state->plugin_desc->spd_id,
			"closed client connection %d, thread ending\n",
			client);
	return NULL;
}

/* Handle a stream client -- answer the connection and spawn a thread to handle
 * its actual requests. */
static void
dispatch_stream(struct plugin_state *state, int fd)
{
	struct sockaddr_storage client_addr;
	socklen_t client_addrlen;
	int client;
	pthread_t thread;
	struct dispatch_stream_client_params *params;

	/* Answer the connection request. */
	client = accept(fd, (struct sockaddr *) &client_addr, &client_addrlen);
	if (client == -1) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"failed to answer new stream request\n");
		return;
	} else {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"new stream client on %d\n", client);
	}

	/* Bundle up enough info for the thread to do its work. */
	params = malloc(sizeof(*params));
	if (params == NULL) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"out of memory\n");
		close(client);
		return;
	}
	params->client = client;
	params->state = state;

	/* Kick off the thread. */
	if (pthread_create(&thread, NULL,
			   &dispatch_stream_handler_thread, params) != 0) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error starting thread\n");
		close(client);
	}
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"started client-specific thread for client on %d\n",
			client);
	return;
}

/* Handle a datagram client -- read the request and handle it immediately. */
static void
dispatch_dgram(struct plugin_state *state, int fd)
{
	struct sockaddr_storage client_addr;
	socklen_t client_addrlen;
	char dgram[65536];
	int reqsize;

	/* Read the request. */
	client_addrlen = sizeof(client_addr);
	reqsize = recvfrom(fd, dgram, sizeof(dgram), 0,
			   (struct sockaddr *) &client_addr, &client_addrlen);
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"datagram request (%d bytes)\n", reqsize);

	/* Handle the request. */
	nis_process_request(state, fd,
			    (struct sockaddr *) &client_addr, client_addrlen,
			    dgram, reqsize);
}


/* Handle all incoming data. */
void *
dispatch_thread(void *p)
{
	struct plugin_state *state = p;
	struct pollfd fds[4];
	int i;
	for (;;) {
		/* Set up for polling. */
		memset(&fds, 0, sizeof(fds));
		for (i = 0; i < state->n_listeners; i++) {
			fds[i].fd = state->listener[i].fd;
			fds[i].events = POLLIN;
		}
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"listening for next request\n");
		switch (poll(fds, state->n_listeners, -1)) {
		case -1:
			slapi_log_error(SLAPI_LOG_PLUGIN,
					state->plugin_desc->spd_id,
					"done waiting\n");
			return NULL;
			break;
		case 0:
			slapi_log_error(SLAPI_LOG_PLUGIN,
					state->plugin_desc->spd_id,
					"no request(?)\n");
			continue;
		default:
			/* Iterate over listening sockets which have work for
			 * us to do. */
			for (i = 0; i < state->n_listeners; i++) {
				if ((fds[i].revents & POLLIN) == 0) {
					continue;
				}
				switch (state->listener[i].type) {
				case SOCK_DGRAM:
					dispatch_dgram(state, fds[i].fd);
					break;
				case SOCK_STREAM:
					dispatch_stream(state, fds[i].fd);
					break;
				default:
					/* never reached */
					assert(0);
					break;
				}
			}
		}
	}
	return state;
}