summaryrefslogtreecommitdiffstats
path: root/src/stream.c
diff options
context:
space:
mode:
authorNalin Dahyabhai <nalin.dahyabhai@pobox.com>2008-03-27 15:24:05 -0400
committerNalin Dahyabhai <nalin.dahyabhai@pobox.com>2008-03-27 15:24:05 -0400
commit8f884257d95ea9ebec742b59dc83d7defdae117d (patch)
tree8d49b573e9f96e2d4fddcb8fa01031265e03934f /src/stream.c
parentbdd74a017f4f1456b63e3b621064adde8e739ac8 (diff)
more cleanup of client-handling code
Diffstat (limited to 'src/stream.c')
-rw-r--r--src/stream.c171
1 files changed, 0 insertions, 171 deletions
diff --git a/src/stream.c b/src/stream.c
deleted file mode 100644
index dbb53bf..0000000
--- a/src/stream.c
+++ /dev/null
@@ -1,171 +0,0 @@
-#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;
-}