From 953420eb936fc97f37a159782f3901c7998f550f Mon Sep 17 00:00:00 2001 From: root Date: Wed, 30 Oct 2019 17:56:14 +0100 Subject: [PATCH] server working, client skeleton implemented --- client.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ client.h | 27 +++++++++++++++++++++ connection.c | 8 +++---- connection.h | 4 ++-- issuer.c | 13 +++++++--- server.c | 22 ++++++++++++----- 6 files changed, 126 insertions(+), 15 deletions(-) create mode 100644 client.c create mode 100644 client.h diff --git a/client.c b/client.c new file mode 100644 index 0000000..b430746 --- /dev/null +++ b/client.c @@ -0,0 +1,67 @@ +#include "client.h" + +client_t* client_open(conn_handler handler) { + struct sockaddr_in servaddr; + client_t *client = malloc(sizeof(client_t)); + if(NULL == handler) { + printf("client_open: received empty handler, stopping"); + return NULL; + } + if (NULL == client) { + printf("client_open: malloc failed"); + return NULL; + } + client->conn = connection_create(CONN_TYPE_CLIENT); + if (NULL == client->conn) { + free(client); + return NULL; + } + client->handler = handler; + client->conn->fd = socket(AF_INET, SOCK_STREAM, 0); + if (-1 == client->conn->fd) { + printf("client_listen: failed to create endpoint.\n"); + return NULL; + } + return client; +} + +int client_connect(client_t *client) { + struct sockaddr_in servaddr; + int servaddr_len = 0; + + if(client == NULL) { + return -1; + } + + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = inet_addr(SERVERIP); + servaddr.sin_port = htons(PORT); + + if (0 != connect(, MAX_CLIENTS)) { + printf("client_accept: listen failed\n"); + return -1; + } + printf("client_accept: listening\n"); + servaddr_len = sizeof(servaddr); + connfd = connect(client->conn->fd, &servaddr, &servaddr_len); + if(0 >= connfd) { + printf("client_accept: connection to client failed, "); + return -1; + } else { + client->handler(client->conn->fd); + } + return 0; +} + +int client_close(client_t *client) { + int err = 0; + if (NULL == client) { + return 0; + } + if (0 != connection_destroy(client->conn)) { + printf("client_close: closing socket failed\n"); + err = -1; + } + free(client); + return err; +} diff --git a/client.h b/client.h new file mode 100644 index 0000000..c6d32c3 --- /dev/null +++ b/client.h @@ -0,0 +1,27 @@ +// +// Created by root on 10/30/19. +// + +#ifndef ECDAA_ISSUER_SERVER_H +#define ECDAA_ISSUER_SERVER_H +#include +#include +#include +#include "connection.h" + +#define PORT 6590 +#define MAX_BUFSIZE 1024 +#define SERVERIP "127.0.0.1" + +typedef struct client { + connection_t* conn; + conn_handler handler; +}client_t; + +client_t* client_open(conn_handler handler); + +int client_connect(client_t* client); + +int client_close(client_t* client); + +#endif //ECDAA_ISSUER_SERVER_H diff --git a/connection.c b/connection.c index 2a0e249..e599a45 100644 --- a/connection.c +++ b/connection.c @@ -1,10 +1,10 @@ #include #include "connection.h" -connection_t* create_connection(conntype_e type){ +connection_t* connection_create(conntype_e type){ connection_t* conn = malloc(sizeof(connection_t)); if(NULL == conn) { - printf("create_connection: malloc failed\n"); + printf("connection_create: malloc failed\n"); } else { conn->type = type; conn->fd = -1; @@ -13,14 +13,14 @@ connection_t* create_connection(conntype_e type){ return conn; } -int destroy_connection(connection_t* conn) { +int connection_destroy(connection_t* conn) { int err = 0; if(NULL != conn) { if(-1 != conn->fd) { err = close(conn->fd); if(-1 == err) { - printf("destroy_connection: close connection failed"); + printf("connection_destroy: close connection failed"); } } free(conn); diff --git a/connection.h b/connection.h index 81037e8..c8bdc04 100644 --- a/connection.h +++ b/connection.h @@ -20,7 +20,7 @@ typedef struct connection { typedef int (*conn_handler)(int fd); -connection_t* create_connection(conntype_e type); -int destroy_connection(connection_t* conn); +connection_t* connection_create(conntype_e type); +int connection_destroy(connection_t* conn); #endif //ECDAA_ISSUER_CONNECTION_H diff --git a/issuer.c b/issuer.c index 9fb2b0e..5e6256b 100644 --- a/issuer.c +++ b/issuer.c @@ -2,6 +2,7 @@ #include #include #include "server.h" +#include "client.h" typedef struct issuer { struct ecdaa_issuer_public_key_FP256BN ipk; @@ -9,6 +10,7 @@ typedef struct issuer { } issuer_t; int process_issuer(int connfd); + int main() { int err = 0; server_t *server; @@ -20,7 +22,9 @@ int main() { for (; !err;) { err = server_accept(server); - if (err) { + if (1 == err) { + printf("graceful shutdown...\n"); + } else if (err) { printf("accept failed, closing...\n"); } } @@ -47,8 +51,11 @@ int process_issuer(int connfd) { write(connfd, outbuf, sizeof(outbuf)); if (0 == strncmp("exit", inbuf, 4)) { printf("process_issuer: exiting gracefully\n"); - break; + return 0; + } + if (0 == strncmp("shutdown", inbuf, 8)) { + printf("process_issuer: shutting down gracefully\n"); + return 1; } } - return 0; } \ No newline at end of file diff --git a/server.c b/server.c index 37eabee..8aa857b 100644 --- a/server.c +++ b/server.c @@ -11,9 +11,8 @@ server_t* server_open(conn_handler handler) { printf("server_open: malloc failed"); return NULL; } - server->conn = malloc(sizeof(connection_t)); + server->conn = connection_create(CONN_TYPE_SERVER); if (NULL == server->conn) { - printf("server_open: malloc failed"); free(server); return NULL; } @@ -46,16 +45,27 @@ int server_accept(server_t *server) { client_len = sizeof(client); connfd = accept(server->conn->fd, &client, &client_len); if(0 >= connfd) { + printf("server_accept: connection to client failed, "); + return -1; + } else { server->handler(connfd); - close(connfd); + if(0 != close(connfd)) { + printf("server_accept: failed to close client connection properly"); + return -1; + } } return 0; } int server_close(server_t *server) { - if (NULL != server && 0 != close(server->conn->fd)) { + int err = 0; + if (NULL == server) { + return 0; + } + if (0 != connection_destroy(server->conn)) { printf("server_close: closing socket failed\n"); - return -1; + err = -1; } - return 0; + free(server); + return err; }