From 2865148349fbc97ed040095179729f5c0d74c549 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 30 Oct 2019 16:11:19 +0100 Subject: [PATCH] basic server implementation --- CMakeLists.txt | 3 ++- connection.c | 29 ++++++++++++++++++++++++ connection.h | 26 +++++++++++++++++++++ issuer.c | 48 ++++++++++++++++++++++++++++++++++++++- server.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ server.h | 27 ++++++++++++++++++++++ 6 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 connection.c create mode 100644 connection.h create mode 100644 server.c create mode 100644 server.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c60d76..02fadda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,9 @@ cmake_minimum_required(VERSION 3.15) project(ecdaa_issuer C) set(CMAKE_C_STANDARD 11) +set(ECDAA_AMCL "/opt/amcl") -add_executable(ecdaa_issuer issuer.c) +add_executable(ecdaa_issuer issuer.c connection.h connection.c server.h server.c) target_include_directories(ecdaa_issuer PUBLIC ${ECDAA_AMCL} diff --git a/connection.c b/connection.c new file mode 100644 index 0000000..2a0e249 --- /dev/null +++ b/connection.c @@ -0,0 +1,29 @@ +#include +#include "connection.h" + +connection_t* create_connection(conntype_e type){ + connection_t* conn = malloc(sizeof(connection_t)); + if(NULL == conn) { + printf("create_connection: malloc failed\n"); + } else { + conn->type = type; + conn->fd = -1; + } + + return conn; +} + +int destroy_connection(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"); + } + } + free(conn); + } + return err; +} diff --git a/connection.h b/connection.h new file mode 100644 index 0000000..81037e8 --- /dev/null +++ b/connection.h @@ -0,0 +1,26 @@ +// +// Created by root on 10/30/19. +// + +#ifndef ECDAA_ISSUER_CONNECTION_H +#define ECDAA_ISSUER_CONNECTION_H + +#include +#include + +typedef enum conntype { + CONN_TYPE_SERVER, + CONN_TYPE_CLIENT +}conntype_e; + +typedef struct connection { + int fd; + conntype_e type; +} connection_t; + +typedef int (*conn_handler)(int fd); + +connection_t* create_connection(conntype_e type); +int destroy_connection(connection_t* conn); + +#endif //ECDAA_ISSUER_CONNECTION_H diff --git a/issuer.c b/issuer.c index e9c27ad..9fb2b0e 100644 --- a/issuer.c +++ b/issuer.c @@ -1,8 +1,54 @@ #include #include #include +#include "server.h" +typedef struct issuer { + struct ecdaa_issuer_public_key_FP256BN ipk; + struct ecdaa_issuer_secret_key_FP256BN isk; +} issuer_t; + +int process_issuer(int connfd); int main() { - printf("Hello, World!\n"); + int err = 0; + server_t *server; + + server = server_open(&process_issuer); + if (NULL == server) { + printf("listen failed, closing...\n"); + } + + for (; !err;) { + err = server_accept(server); + if (err) { + printf("accept failed, closing...\n"); + } + } + printf("shut down server\n"); + server_close(server); + return 0; +} + +int process_issuer(int connfd) { + char inbuf[MAX_BUFSIZE]; + char outbuf[MAX_BUFSIZE]; + int n = 0; + for (;;) { + bzero(inbuf, MAX_BUFSIZE); + bzero(outbuf, MAX_BUFSIZE); + + if (0 >= read(connfd, inbuf, sizeof(inbuf))) { + printf("process_issuer: cannot read from socket\n"); + return -1; + } + printf("> ISSUER: %s\n", inbuf); + strncpy(outbuf, "OK", 2); + printf("< ISSUER: %s\n", outbuf); + write(connfd, outbuf, sizeof(outbuf)); + if (0 == strncmp("exit", inbuf, 4)) { + printf("process_issuer: exiting gracefully\n"); + break; + } + } return 0; } \ No newline at end of file diff --git a/server.c b/server.c new file mode 100644 index 0000000..37eabee --- /dev/null +++ b/server.c @@ -0,0 +1,61 @@ +#include "server.h" + +server_t* server_open(conn_handler handler) { + struct sockaddr_in servaddr; + server_t *server = malloc(sizeof(server_t)); + if(NULL == handler) { + printf("server_open: received empty handler, stopping"); + return NULL; + } + if (NULL == server) { + printf("server_open: malloc failed"); + return NULL; + } + server->conn = malloc(sizeof(connection_t)); + if (NULL == server->conn) { + printf("server_open: malloc failed"); + free(server); + return NULL; + } + server->handler = handler; + server->conn->fd = socket(AF_INET, SOCK_STREAM, 0); + if (-1 == server->conn->fd) { + printf("server_listen: failed to create endpoint.\n"); + return NULL; + } + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(PORT); + + if (0 != bind(server->conn->fd, (const struct sockaddr *) &servaddr, sizeof(servaddr))) { + printf("server_listen: failed to bind socket\n"); + return NULL; + } + return server; +} + +int server_accept(server_t *server) { + struct sockaddr_in client; + int client_len = 0; + int connfd = -1; + if (NULL == server || 0 != listen(server->conn->fd, MAX_CLIENTS)) { + printf("server_accept: listen failed\n"); + return -1; + } + printf("server_accept: listening\n"); + client_len = sizeof(client); + connfd = accept(server->conn->fd, &client, &client_len); + if(0 >= connfd) { + server->handler(connfd); + close(connfd); + } + return 0; +} + +int server_close(server_t *server) { + if (NULL != server && 0 != close(server->conn->fd)) { + printf("server_close: closing socket failed\n"); + return -1; + } + return 0; +} diff --git a/server.h b/server.h new file mode 100644 index 0000000..e62a434 --- /dev/null +++ b/server.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_CLIENTS 10 +#define MAX_BUFSIZE 1024 + +typedef struct server { + connection_t* conn; + conn_handler handler; +}server_t; + +server_t* server_open(conn_handler handler); + +int server_accept(server_t *server); + +int server_close(server_t* server); + +#endif //ECDAA_ISSUER_SERVER_H