summaryrefslogtreecommitdiffstats
path: root/src/stream.c
blob: dbb53bf9ed9cc0a5a15e12328daef79bc73e4242 (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
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <unistd.h>

#include "nis.h"
#include "stream.h"
#define MAX_CLIENT_IDLE (60 * 1000)

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

static void *
stream_client_thread(void *arg)
{
	int i, fraglen, last, ret, frag_len, record_len;
	int32_t len, nlen;
	char fragment[65536], record[65536];
	struct pollfd pollfd;

	struct stream_client_thread_parms *parms = arg;
	struct plugin_state *state = parms->state;
	int done, client = parms->client;
	free(parms);

	slapi_log_error(SLAPI_LOG_PLUGIN,
			state->plugin_desc->spd_id,
			"opened client connection %d, thread started\n",
			client);
	last = fcntl(client, F_GETFD);
	if ((last == -1) ||
	    (fcntl(client, F_SETFD, last | 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");
	frag_len = 0;
	record_len = 0;
	done = 0;
	while (!done) {
		pollfd.fd = client;
		pollfd.events = POLLIN | POLLHUP;
		if (poll(&pollfd, 1, MAX_CLIENT_IDLE) > 0) {
			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");
				done = 1;
				break;
			case 0:
				slapi_log_error(SLAPI_LOG_PLUGIN,
						state->plugin_desc->spd_id,
						"client closed connection\n");
				done = 1;
				break;
			default:
				slapi_log_error(SLAPI_LOG_PLUGIN,
						state->plugin_desc->spd_id,
						"read %d bytes\n", i);
				frag_len += i;
				break;
			}
			if (frag_len > 4) {
				memcpy(&nlen, fragment, 4);
				len = ntohl(nlen);
				last = ((len & 0x80000000) != 0);
				len &= 0x7fffffff;
				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);
				if (frag_len >= len + 4) {
					slapi_log_error(SLAPI_LOG_PLUGIN,
							state->plugin_desc->spd_id,
							"got whole fragment "
							"(%d bytes)\n", len);
					/* we have a whole fragment */
					memmove(record + record_len,
						fragment + 4, len);
					record_len += len;
					if (last) {
						slapi_log_error(SLAPI_LOG_PLUGIN,
								state->plugin_desc->spd_id,
								"got whole "
								"record (%d "
								"bytes)\n",
								record_len);
						/* we have a whole record */
						nis_process_request(state,
								    client,
								    NULL, 0,
								    record,
								    record_len);
						record_len = 0;
					}
					memmove(fragment,
						fragment + (len + 4),
						frag_len - (len + 4));
					frag_len -= (len + 4);
				}
				slapi_log_error(SLAPI_LOG_PLUGIN,
						state->plugin_desc->spd_id,
						"out: frag_len=%d,record=%d\n",
						frag_len, record_len);
			}
		} else {
			slapi_log_error(SLAPI_LOG_PLUGIN,
					state->plugin_desc->spd_id,
					"connection timeout\n");
			done = 1;
		}
	}
	close(client);
	slapi_log_error(SLAPI_LOG_PLUGIN,
			state->plugin_desc->spd_id,
			"closed client connection %d, thread ending\n", client);
	return NULL;
}

void
stream_client_start(struct plugin_state *state, int client)
{
	pthread_t thread;
	struct stream_client_thread_parms *parms;

	parms = malloc(sizeof(*parms));
	if (parms == NULL) {
		slapi_log_error(SLAPI_LOG_PLUGIN,
				state->plugin_desc->spd_id,
				"out of memory\n");
		close(client);
		return;
	}
	parms->client = client;
	parms->state = state;

	if (pthread_create(&thread, NULL, &stream_client_thread, parms) != 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\n");
	return;
}