From d709dcf8f5d9e311b6703cd444ff220809eeff7a Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Mon, 14 Nov 2011 17:38:53 -0500 Subject: ipa-cldap: Implement worker thread. --- daemons/ipa-slapi-plugins/ipa-cldap/ipa_cldap.h | 11 ++++ .../ipa-slapi-plugins/ipa-cldap/ipa_cldap_worker.c | 71 +++++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/daemons/ipa-slapi-plugins/ipa-cldap/ipa_cldap.h b/daemons/ipa-slapi-plugins/ipa-cldap/ipa_cldap.h index 013dad2fd..ff9818a64 100644 --- a/daemons/ipa-slapi-plugins/ipa-cldap/ipa_cldap.h +++ b/daemons/ipa-slapi-plugins/ipa-cldap/ipa_cldap.h @@ -58,6 +58,7 @@ #define IPA_PLUGIN_NAME IPA_CLDAP_PLUGIN_NAME #define CLDAP_PORT 389 +#define MAX_DG_SIZE 4096 struct ipa_cldap_ctx { Slapi_ComponentId *plugin_id; @@ -67,6 +68,16 @@ struct ipa_cldap_ctx { int sd; }; +struct ipa_cldap_req { + int fd; + + struct sockaddr_storage ss; + socklen_t ss_len; + + char dgram[MAX_DG_SIZE]; + size_t dgsize; +}; + void *ipa_cldap_worker(struct ipa_cldap_ctx *ctx); #endif /* _IPA_CLDAP_H_ */ diff --git a/daemons/ipa-slapi-plugins/ipa-cldap/ipa_cldap_worker.c b/daemons/ipa-slapi-plugins/ipa-cldap/ipa_cldap_worker.c index 4bbba5359..474c5327c 100644 --- a/daemons/ipa-slapi-plugins/ipa-cldap/ipa_cldap_worker.c +++ b/daemons/ipa-slapi-plugins/ipa-cldap/ipa_cldap_worker.c @@ -38,12 +38,81 @@ * END COPYRIGHT BLOCK **/ #include "ipa_cldap.h" +#include + +static void ipa_cldap_process(struct ipa_cldap_ctx *ctx, + struct ipa_cldap_req *req) +{ + free(req); + return; +} + +static struct ipa_cldap_req *ipa_cldap_recv_dgram(struct ipa_cldap_ctx *ctx) +{ + struct ipa_cldap_req *req; + + req = calloc(1, sizeof(struct ipa_cldap_req)); + if (!req) { + LOG("Failed to allocate memory for req"); + return NULL; + } + + req->fd = ctx->sd; + req->ss_len = sizeof(struct sockaddr_storage); + + req->dgsize = recvfrom(req->fd, req->dgram, MAX_DG_SIZE, 0, + (struct sockaddr *)&req->ss, &req->ss_len); + if (req->dgsize == -1) { + LOG_TRACE("Failed to get datagram\n"); + free(req); + return NULL; + } + + return req; +} void *ipa_cldap_worker(struct ipa_cldap_ctx *ctx) { + struct ipa_cldap_req *req; + struct pollfd fds[2]; bool stop = false; + int ret; while (!stop) { - sleep(1); + + fds[0].fd = ctx->stopfd[0]; + fds[0].events = POLLIN; + fds[0].revents = 0; + fds[1].fd = ctx->sd; + fds[1].events = POLLIN; + fds[1].revents = 0; + + /* wait until a request comes in */ + ret = poll(fds, 2, -1); + if (ret == -1) { + if (errno != EINTR) { + LOG_FATAL("poll() failed with [%d, %s]. Can't continue.\n", + errno, strerror(errno)); + stop = true; + } + } + if (ret <= 0) { + continue; + } + + /* got a stop signal, exit the loop */ + if (fds[0].revents & POLLIN) { + stop = true; + continue; + } + + /* got a CLDAP packet, handle it */ + if (fds[1].revents & POLLIN) { + req = ipa_cldap_recv_dgram(ctx); + if (req) { + ipa_cldap_process(ctx, req); + } + } } + return NULL; } -- cgit