summaryrefslogtreecommitdiffstats
path: root/src/plugin.c
diff options
context:
space:
mode:
authorNalin Dahyabhai <nalin.dahyabhai@pobox.com>2007-11-07 17:38:34 -0500
committerNalin Dahyabhai <nalin.dahyabhai@pobox.com>2007-11-07 17:38:34 -0500
commit226ce762936ee32c02ebf3d6542eb905500ba31f (patch)
tree66f411c56f40ce3b1fcdaab512b4c5953ba34066 /src/plugin.c
parent81ddb16d97dad7e154e481129f5ae647dd310c3b (diff)
downloadslapi-nis-226ce762936ee32c02ebf3d6542eb905500ba31f.tar.gz
slapi-nis-226ce762936ee32c02ebf3d6542eb905500ba31f.tar.xz
slapi-nis-226ce762936ee32c02ebf3d6542eb905500ba31f.zip
- more initial plugin testing
Diffstat (limited to 'src/plugin.c')
-rw-r--r--src/plugin.c161
1 files changed, 152 insertions, 9 deletions
diff --git a/src/plugin.c b/src/plugin.c
index 91a6faf..3d1976b 100644
--- a/src/plugin.c
+++ b/src/plugin.c
@@ -3,35 +3,177 @@
#endif
#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <poll.h>
+#include <pthread.h>
#include <stdlib.h>
#include <string.h>
+#include <syslog.h>
#include <unistd.h>
+#include <nspr.h>
+#include <secport.h>
+#include <plarenas.h>
#include <dirsrv/slapi-plugin.h>
#define PACKAGE_VERSION "0.0"
-
+#define PORT 2380
/* the module initialization function */
static Slapi_PluginDesc
-my_plugin_desc = {
+plugin_description = {
.spd_id = "my-plugin",
.spd_vendor = "hamdingers.org",
.spd_version = PACKAGE_VERSION,
.spd_description = "sample plugin",
};
-/* Set up the plugin. */
+struct state {
+ pthread_t tid;
+ PLArenaPool *arena;
+ int listenfd[2];
+};
+
static int
-start(Slapi_PBlock *pb)
+setup_listener(struct state **lstate)
{
+ int sockfd = -1, sockfd6 = -1, err;
+ struct sockaddr_in addr;
+ struct sockaddr_in6 addr6;
+ struct state *state;
+ PLArenaPool *arena = NULL;
+
+ sockfd = socket(PF_INET, SOCK_STREAM, 0);
+ if (sockfd == -1) {
+ slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id,
+ "error creating ipv4 listening socket\n");
+ goto failed;
+ }
+ sockfd6 = socket(PF_INET6, SOCK_STREAM, 0);
+ if (sockfd6 == -1) {
+ slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id,
+ "error creating ipv6 listening socket\n");
+ goto failed;
+ }
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_port = htons(PORT);
+ if (bind(sockfd, (struct sockaddr*) &addr,
+ sizeof(struct sockaddr_in)) == -1) {
+ slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id,
+ "error binding to ipv4 port\n");
+ goto failed;
+ }
+ memset(&addr6, 0, sizeof(addr6));
+ addr6.sin6_port = htons(PORT);
+ if (bind(sockfd6, (struct sockaddr*) &addr6,
+ sizeof(struct sockaddr_in6)) == -1) {
+ slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id,
+ "error binding to ipv6 port\n");
+ close(sockfd6);
+ sockfd6 = -1;
+ }
+ if (listen(sockfd, 128) == -1) {
+ slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id,
+ "error marking ipv4 socket for listening\n");
+ goto failed;
+ }
+ if ((sockfd6 != -1) && (listen(sockfd6, 128) == -1)) {
+ slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id,
+ "error marking ipv6 socket for listening\n");
+ goto failed;
+ }
+ arena = PORT_NewArena(sizeof(double));
+ if (arena == NULL) {
+ goto failed;
+ }
+ state = PORT_ArenaZAlloc(arena, sizeof(*state));
+ if (state == NULL) {
+ goto failed;
+ }
+ state->arena = arena;
+ state->listenfd[0] = sockfd;
+ state->listenfd[1] = sockfd6;
+ *lstate = state;
return 0;
+failed:
+ err = errno;
+ if (arena != NULL) {
+ PORT_FreeArena(arena, PR_TRUE);
+ }
+ if (sockfd != -1) {
+ close(sockfd);
+ }
+ if (sockfd != -1) {
+ close(sockfd6);
+ }
+ errno = err;
+ return -1;
}
+static void *
+process_requests(void *p)
+{
+ struct state *state = p;
+ struct pollfd fds[2];
+ int client;
+ for (;;) {
+ memset(&fds, 0, sizeof(fds));
+ fds[0].fd = state->listenfd[0];
+ fds[0].events = POLLIN;
+ fds[1].fd = state->listenfd[1];
+ fds[1].events = POLLIN;
+ switch (poll(fds, fds[1].fd == -1 ? 1 : 2, -1) != -1) {
+ case -1:
+ return NULL;
+ break;
+ case 0:
+ continue;
+ default:
+ if (fds[0].revents & POLLIN) {
+ client = accept(fds[0].fd, NULL, NULL);
+ } else
+ if (fds[1].revents & POLLIN) {
+ client = accept(fds[1].fd, NULL, NULL);
+ }
+ slapi_log_error(SLAPI_LOG_PLUGIN,
+ plugin_description.spd_id,
+ "answering client request %d\n",
+ client);
+ write(client, "FOO!\n", 5);
+ close(client);
+ }
+ }
+ return state;
+}
+
+/* Set up the plugin. */
+static int
+plugin_start(Slapi_PBlock *pb)
+{
+ struct state *state;
+ slapi_log_error(SLAPI_LOG_PLUGIN, "my_init_function",
+ "plugin starting\n");
+ if (setup_listener(&state) == -1) {
+ slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id,
+ "error setting up listening sockets\n");
+ return -1;
+ }
+ slapi_pblock_set(pb, SLAPI_PLUGIN_PRIVATE, state);
+ if (pthread_create(&state->tid, NULL, &process_requests, state) != 0) {
+ slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id,
+ "error starting listener thread\n");
+ return -1;
+ }
+ return 0;
+}
/* Prepare for shutdown. */
static int
-cleanup(Slapi_PBlock *pb)
+plugin_close(Slapi_PBlock *pb)
{
+ slapi_log_error(SLAPI_LOG_PLUGIN, "my_init_function",
+ "plugin closing\n");
return 0;
}
@@ -39,9 +181,10 @@ int
my_init_function(Slapi_PBlock *pb)
{
slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_03);
- slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, &my_plugin_desc);
- slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN, &start);
- slapi_pblock_set(pb, SLAPI_PLUGIN_CLEANUP_FN, &cleanup);
- slapi_log_error(SLAPI_LOG_PLUGIN, "my_init_function", "plugin registered\n");
+ slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, &plugin_description);
+ slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN, &plugin_start);
+ slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN, &plugin_close);
+ slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id,
+ "registering plugin hooks\n");
return 0;
}