Browse Source

server working, client skeleton implemented

master
root 6 years ago
parent
commit
953420eb93
  1. 67
      client.c
  2. 27
      client.h
  3. 8
      connection.c
  4. 4
      connection.h
  5. 13
      issuer.c
  6. 22
      server.c

67
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;
}

27
client.h

@ -0,0 +1,27 @@
//
// Created by root on 10/30/19.
//
#ifndef ECDAA_ISSUER_SERVER_H
#define ECDAA_ISSUER_SERVER_H
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#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

8
connection.c

@ -1,10 +1,10 @@
#include <unistd.h> #include <unistd.h>
#include "connection.h" #include "connection.h"
connection_t* create_connection(conntype_e type){ connection_t* connection_create(conntype_e type){
connection_t* conn = malloc(sizeof(connection_t)); connection_t* conn = malloc(sizeof(connection_t));
if(NULL == conn) { if(NULL == conn) {
printf("create_connection: malloc failed\n"); printf("connection_create: malloc failed\n");
} else { } else {
conn->type = type; conn->type = type;
conn->fd = -1; conn->fd = -1;
@ -13,14 +13,14 @@ connection_t* create_connection(conntype_e type){
return conn; return conn;
} }
int destroy_connection(connection_t* conn) { int connection_destroy(connection_t* conn) {
int err = 0; int err = 0;
if(NULL != conn) { if(NULL != conn) {
if(-1 != conn->fd) { if(-1 != conn->fd) {
err = close(conn->fd); err = close(conn->fd);
if(-1 == err) { if(-1 == err) {
printf("destroy_connection: close connection failed"); printf("connection_destroy: close connection failed");
} }
} }
free(conn); free(conn);

4
connection.h

@ -20,7 +20,7 @@ typedef struct connection {
typedef int (*conn_handler)(int fd); typedef int (*conn_handler)(int fd);
connection_t* create_connection(conntype_e type); connection_t* connection_create(conntype_e type);
int destroy_connection(connection_t* conn); int connection_destroy(connection_t* conn);
#endif //ECDAA_ISSUER_CONNECTION_H #endif //ECDAA_ISSUER_CONNECTION_H

13
issuer.c

@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include <ecdaa.h> #include <ecdaa.h>
#include "server.h" #include "server.h"
#include "client.h"
typedef struct issuer { typedef struct issuer {
struct ecdaa_issuer_public_key_FP256BN ipk; struct ecdaa_issuer_public_key_FP256BN ipk;
@ -9,6 +10,7 @@ typedef struct issuer {
} issuer_t; } issuer_t;
int process_issuer(int connfd); int process_issuer(int connfd);
int main() { int main() {
int err = 0; int err = 0;
server_t *server; server_t *server;
@ -20,7 +22,9 @@ int main() {
for (; !err;) { for (; !err;) {
err = server_accept(server); err = server_accept(server);
if (err) { if (1 == err) {
printf("graceful shutdown...\n");
} else if (err) {
printf("accept failed, closing...\n"); printf("accept failed, closing...\n");
} }
} }
@ -47,8 +51,11 @@ int process_issuer(int connfd) {
write(connfd, outbuf, sizeof(outbuf)); write(connfd, outbuf, sizeof(outbuf));
if (0 == strncmp("exit", inbuf, 4)) { if (0 == strncmp("exit", inbuf, 4)) {
printf("process_issuer: exiting gracefully\n"); 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;
} }

22
server.c

@ -11,9 +11,8 @@ server_t* server_open(conn_handler handler) {
printf("server_open: malloc failed"); printf("server_open: malloc failed");
return NULL; return NULL;
} }
server->conn = malloc(sizeof(connection_t)); server->conn = connection_create(CONN_TYPE_SERVER);
if (NULL == server->conn) { if (NULL == server->conn) {
printf("server_open: malloc failed");
free(server); free(server);
return NULL; return NULL;
} }
@ -46,16 +45,27 @@ int server_accept(server_t *server) {
client_len = sizeof(client); client_len = sizeof(client);
connfd = accept(server->conn->fd, &client, &client_len); connfd = accept(server->conn->fd, &client, &client_len);
if(0 >= connfd) { if(0 >= connfd) {
printf("server_accept: connection to client failed, ");
return -1;
} else {
server->handler(connfd); server->handler(connfd);
close(connfd); if(0 != close(connfd)) {
printf("server_accept: failed to close client connection properly");
return -1;
}
} }
return 0; return 0;
} }
int server_close(server_t *server) { 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"); printf("server_close: closing socket failed\n");
return -1; err = -1;
} }
return 0; free(server);
return err;
} }

Loading…
Cancel
Save