From b3b78cea8b235f5a3d06281c812a7f927b11cf24 Mon Sep 17 00:00:00 2001 From: Jennifer Richards Date: Tue, 17 May 2016 19:03:07 -0400 Subject: [PATCH] Handle tids connections from rudimentary event loop in main(). Has debugging code in place, not ready for release. --- include/trust_router/tid.h | 4 +++ tid/example/tids_main.c | 34 ++++++++++++++++++--- tid/tids.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 5 deletions(-) diff --git a/include/trust_router/tid.h b/include/trust_router/tid.h index 4d245b8..d87e0e0 100644 --- a/include/trust_router/tid.h +++ b/include/trust_router/tid.h @@ -148,6 +148,10 @@ TR_EXPORT TIDS_INSTANCE *tids_create (void); TR_EXPORT int tids_start (TIDS_INSTANCE *tids, TIDS_REQ_FUNC *req_handler, tids_auth_func *auth_handler, const char *hostname, unsigned int port, void *cookie); +TR_EXPORT int tids_get_listener (TIDS_INSTANCE *tids, TIDS_REQ_FUNC *req_handler, + tids_auth_func *auth_handler, const char *hostname, + unsigned int port, void *cookie); +TR_EXPORT int tids_accept(TIDS_INSTANCE *tids, int listen); TR_EXPORT int tids_send_response (TIDS_INSTANCE *tids, TID_REQ *req, TID_RESP *resp); TR_EXPORT int tids_send_err_response (TIDS_INSTANCE *tids, TID_REQ *req, const char *err_msg); TR_EXPORT void tids_destroy (TIDS_INSTANCE *tids); diff --git a/tid/example/tids_main.c b/tid/example/tids_main.c index 89b5d52..29fa9b5 100644 --- a/tid/example/tids_main.c +++ b/tid/example/tids_main.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -345,9 +346,10 @@ int main (int argc, char *argv[]) { TIDS_INSTANCE *tids; - int rc = 0; TR_NAME *gssname = NULL; struct cmdline_args opts={NULL}; + int tids_socket=-1; + struct pollfd *poll_fds=NULL; /* parse the command line*/ argp_parse(&argp, argc, argv, 0, 0, &opts); @@ -380,10 +382,32 @@ int main (int argc, tids->ipaddr = opts.ip_address; - /* Start-up the server, won't return unless there is an error. */ - rc = tids_start(tids, &tids_req_handler , auth_handler, opts.hostname, TID_PORT, gssname); - - tr_crit("Error in tids_start(), rc = %d. Exiting.", rc); + /* get listener for tids port */ + tids_socket = tids_get_listener(tids, &tids_req_handler , auth_handler, opts.hostname, TID_PORT, gssname); + + poll_fds=malloc(sizeof(*poll_fds)); + if (poll_fds == NULL) { + tr_crit("Could not allocate event polling list, exiting."); + return 1; + } + + poll_fds[0].fd=tids_socket; + poll_fds[0].events=POLLIN; /* poll on ready for reading */ + poll_fds[0].revents=0; + + /* main event loop */ + while (1) { + if(poll(poll_fds, 1, 1000) > 0) { + if (poll_fds[0].revents & POLLIN) { + if (0 != tids_accept(tids, tids_socket)) { + tr_err("Error handling tids request."); + } + } + } else { + printf("Idle..."); fflush(stdout); + } + + } /* Clean-up the TID server instance */ tids_destroy(tids); diff --git a/tid/tids.c b/tid/tids.c index eb081dd..2ebe887 100644 --- a/tid/tids.c +++ b/tid/tids.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -372,6 +373,81 @@ TIDS_INSTANCE *tids_create (void) return tids; } +/* Get a listener for tids requests, returns its socket fd. Accept + * connections with tids_accept() */ +int tids_get_listener(TIDS_INSTANCE *tids, + TIDS_REQ_FUNC *req_handler, + tids_auth_func *auth_handler, + const char *hostname, + unsigned int port, + void *cookie) +{ + int listen = -1; + + tids->tids_port = port; + if (0 > (listen = tids_listen(tids, port))) { + char errbuf[256]; + if (0 == strerror_r(errno, errbuf, 256)) { + tr_debug("tids_get_listener: Error opening port %d: %s.", port, errbuf); + } else { + tr_debug("tids_get_listener: Unknown error openining port %d.", port); + } + } + + if (listen > 0) { + /* opening port succeeded */ + tr_debug("tids_get_listener: Opened port %d.", port); + + /* make this socket non-blocking */ + if (0 != fcntl(listen, F_SETFD, O_NONBLOCK)) { + tr_debug("tids_get_listener: Error setting O_NONBLOCK."); + close(listen); + listen=-1; + } + } + + if (listen > 0) { + /* store the caller's request handler & cookie */ + tids->req_handler = req_handler; + tids->auth_handler = auth_handler; + tids->hostname = hostname; + tids->cookie = cookie; + } + + return listen; +} + +/* Accept and process a connection on a port opened with tids_get_listener() */ +int tids_accept(TIDS_INSTANCE *tids, int listen) +{ + int conn=-1; + int pid=-1; + + if (0 > (conn = accept(listen, NULL, NULL))) { + perror("Error from TIDS Server accept()"); + return 1; + } + + if (0 > (pid = fork())) { + perror("Error on fork()"); + return 1; + } + + if (pid == 0) { + close(listen); + tids_handle_connection(tids, conn); + close(conn); + exit(0); /* exit to kill forked child process */ + } else { + close(conn); + } + + /* clean up any processes that have completed (TBD: move to main loop?) */ + while (waitpid(-1, 0, WNOHANG) > 0); + + return 0; +} + /* Process tids requests forever. Should not return except on error. */ int tids_start (TIDS_INSTANCE *tids, TIDS_REQ_FUNC *req_handler, -- 2.1.4