diff options
Diffstat (limited to 'lib/libostp/server.c')
-rw-r--r-- | lib/libostp/server.c | 76 |
1 files changed, 40 insertions, 36 deletions
diff --git a/lib/libostp/server.c b/lib/libostp/server.c index 0013ce2..588a6d2 100644 --- a/lib/libostp/server.c +++ b/lib/libostp/server.c @@ -32,6 +32,7 @@ #include <net/stpsession.h> #include <arpa/inet.h> #include <string.h> +#include <stdlib.h> #include <unistd.h> #include <stdio.h> @@ -39,41 +40,34 @@ #define LISTEN_PORT 5352 static int -handle_client(struct sockaddr_in *caddr, struct ostp_session *sp, struct ostp_listener *lp, - int clientno) +handle_client(struct sockaddr_in *caddr, struct ostp_client *c, struct ostp_listener *lp) { struct session_request srq; ssize_t nread; - sp->sockfd = lp->clients[clientno]; - /* Try to read in the session request */ - if ((nread = read(sp->sockfd, &srq, sizeof(srq))) < 0) { + if ((nread = read(c->sockfd, &srq, sizeof(srq))) < 0) { printf("Read failure...\n"); - close(sp->sockfd); - lp->clients[clientno] = -1; + listener_close(lp, c); return -1; } if (nread == 0) { printf("Connection closed by peer\n"); - close(sp->sockfd); - lp->clients[clientno] = -1; + listener_close(lp, c); return -1; } /* Is this even a session request? */ if (nread != sizeof(srq)) { printf("Rejecting data - not a session request...\n"); - close(sp->sockfd); - lp->clients[clientno] = -1; + listener_close(lp, c); return -1; } /* Handle the session request */ - if (handle_srq(sp, lp, &srq) < 0) { - close(sp->sockfd); - lp->clients[clientno] = -1; + if (handle_srq(c, lp, &srq) < 0) { + listener_close(lp, c); return -1; } @@ -92,13 +86,14 @@ listener_init(struct ostp_listener *lp) } int -listener_bind(struct ostp_session *sp, struct ostp_listener *lp) +listener_bind(struct ostp_listener *lp) { + struct ostp_session *session; struct sockaddr_in saddr; int error; lp->serv_sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sp->sockfd < 0) { + if (lp->serv_sockfd < 0) { perror("Failed to create socket\n"); return -1; } @@ -122,22 +117,24 @@ listener_bind(struct ostp_session *sp, struct ostp_listener *lp) } int -listener_poll(struct ostp_session *sp, struct ostp_listener *lp) +listener_poll(struct ostp_listener *lp) { struct sockaddr_in caddr; + struct ostp_client *c; socklen_t caddr_len; + pthread_t client_td; int client_sock, error = 0; char *ip; memset(lp->clients, -1, sizeof(lp->clients)); - lp->clients[0] = lp->serv_sockfd; + lp->clients[0].sockfd = lp->serv_sockfd; while (1) { FD_ZERO(&lp->client_fds); for (int i = 0; i < MAX_CLIENTS; ++i) { - if (lp->clients[i] >= 0) - FD_SET(lp->clients[i], &lp->client_fds); + if (lp->clients[i].sockfd >= 0) + FD_SET(lp->clients[i].sockfd, &lp->client_fds); } if (select(1024, &lp->client_fds, NULL, NULL, NULL) < 0) { @@ -157,25 +154,22 @@ listener_poll(struct ostp_session *sp, struct ostp_listener *lp) } for (int i = 0; i < MAX_CLIENTS; ++i) { - if (lp->clients[i] < 0) { - lp->clients[i] = client_sock; + c = &lp->clients[i]; + if (lp->client_count >= MAX_CLIENTS) { + printf("New connection rejected, max clients reached\n"); + continue; + } + if (c->sockfd < 0) { + c->sockfd = client_sock; ip = inet_ntoa(caddr.sin_addr); + printf("Incoming connection from %s\n", ip); + ++lp->client_count; + handle_client(&caddr, c, lp); break; } } } - - /* Handle from data from lp->clients */ - for (int i = 1; i < MAX_CLIENTS; ++i) { - if (lp->clients[i] <= 0) - continue; - if (FD_ISSET(lp->clients[i], &lp->client_fds) <= 0) - continue; - - handle_client(&caddr, sp, lp, i); - break; - } } close(client_sock); @@ -185,11 +179,21 @@ listener_poll(struct ostp_session *sp, struct ostp_listener *lp) void listener_cleanup(struct ostp_listener *lp) { + struct ostp_client *c; + for (int i = 0; i < MAX_CLIENTS; ++i) { - if (lp->clients[i] > 0) { - close(lp->clients[i]); - } + c = &lp->clients[i]; + listener_close(lp, c); } close(lp->serv_sockfd); } + +void +listener_close(struct ostp_listener *lp, struct ostp_client *c) +{ + close(c->sockfd); + c->sockfd = -1; + memset(&c->session, 0, sizeof(c->session)); + --lp->client_count; +} |