You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
3.7 KiB
151 lines
3.7 KiB
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <ecdaa-tpm.h>
|
|
#include "client.h"
|
|
#include "server.h"
|
|
|
|
#define ISSUERIP "127.0.0.1"
|
|
#define ISSUERPORT 6590
|
|
#define MEMBERIP "127.0.0.1"
|
|
#define MEMBERPORT 6591
|
|
#define VERIFIERIP "127.0.0.1"
|
|
#define VERIFIERPORT 6592
|
|
|
|
|
|
typedef struct member {
|
|
struct ecdaa_member_public_key_FP256BN mpk;
|
|
} member_t;
|
|
|
|
typedef enum memberstate {
|
|
ON,
|
|
JOIN,
|
|
APPEND,
|
|
JOINED,
|
|
ATTEST,
|
|
PUBLISH,
|
|
SHUTDOWN
|
|
}memberstate_t;
|
|
|
|
int process_member(int connfd);
|
|
int member_join(int connfd);
|
|
|
|
int main() {
|
|
int err = 0;
|
|
server_t *server;
|
|
|
|
server = server_open(&process_member, MEMBERPORT);
|
|
if (NULL == server) {
|
|
printf("listen failed, closing...\n");
|
|
}
|
|
|
|
for (; !err;) {
|
|
err = server_accept(server);
|
|
if (1 == err) {
|
|
printf("graceful shutdown...\n");
|
|
} else if (err) {
|
|
printf("accept failed, closing...\n");
|
|
}
|
|
}
|
|
printf("shut down server\n");
|
|
server_close(server);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int process_member(int connfd) {
|
|
char inbuf[MAX_BUFSIZE];
|
|
char outbuf[MAX_BUFSIZE];
|
|
int ret = 2;
|
|
|
|
while (ret == 2) {
|
|
bzero(inbuf, MAX_BUFSIZE);
|
|
bzero(outbuf, MAX_BUFSIZE);
|
|
|
|
if (0 >= read(connfd, inbuf, sizeof(inbuf))) {
|
|
printf("process_member: cannot read from socket\n");
|
|
ret = -1;
|
|
}
|
|
printf("> MEMBER: %s\n", inbuf);
|
|
|
|
if (0 == strncmp("ATTEST", inbuf, 6)) {
|
|
printf("attest()\n");
|
|
strncpy(outbuf, "OK\n", 3);
|
|
}
|
|
else if (0 == strncmp("PUBLISH", inbuf, 7)) {
|
|
printf("publish()\n");
|
|
strncpy(outbuf, "OK\n", 3);
|
|
}
|
|
else if (0 == strncmp("JOIN", inbuf, 4)) {
|
|
printf("join()\n");
|
|
client_t *client;
|
|
client = client_open(&member_join);
|
|
if(0 != client_connect(client, ISSUERIP, ISSUERPORT)) {
|
|
strncpy(outbuf, "ERR\n", 4);
|
|
ret = -1;
|
|
} else {
|
|
if(0 != client_close(client)) {
|
|
strncpy(outbuf, "ERR\n", 4);
|
|
}
|
|
strncpy(outbuf, "OK\n", 3);
|
|
}
|
|
}
|
|
else if (0 == strncmp("EXIT", inbuf, 4)) {
|
|
printf("exit()\n");
|
|
strncpy(outbuf, "OK\n", 3);
|
|
ret = 0;
|
|
}
|
|
else if (0 == strncmp("SHUTDOWN", inbuf, 8)) {
|
|
strncpy(outbuf, "OK\n", 3);
|
|
ret = 1;
|
|
}
|
|
else {
|
|
printf("error()\n");
|
|
strncpy(outbuf, "ERR\n", 4);
|
|
ret = -1;
|
|
}
|
|
|
|
printf("< MEMBER: %s\n", outbuf);
|
|
write(connfd, outbuf, sizeof(outbuf));
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int member_join(int connfd) {
|
|
char inbuf[MAX_BUFSIZE];
|
|
char outbuf[MAX_BUFSIZE];
|
|
int n = 0;
|
|
memberstate_t state = JOIN;
|
|
|
|
for (;;) {
|
|
switch(state) {
|
|
case JOIN:
|
|
strncpy(outbuf, "JOIN", 4);
|
|
state = APPEND;
|
|
break;
|
|
case APPEND:
|
|
if (0 == strncmp("JOINSTART", inbuf, 9)) {
|
|
strncpy(outbuf, "APPEND\n", 7);
|
|
state = JOINED;
|
|
}
|
|
break;
|
|
case JOINED:
|
|
if (0 == strncmp("JOINPROCEED", inbuf, 11)) {
|
|
strncpy(outbuf, "OK\n", 3);
|
|
return 0;
|
|
}
|
|
break;
|
|
default:
|
|
return -1;
|
|
}
|
|
|
|
bzero(outbuf, MAX_BUFSIZE);
|
|
printf("ISSUER < MEMBER: %s\n", outbuf);
|
|
write(connfd, outbuf, sizeof(outbuf));
|
|
|
|
bzero(inbuf, MAX_BUFSIZE);
|
|
if (0 >= read(connfd, inbuf, sizeof(inbuf))) {
|
|
printf("process_issuer: cannot read from socket\n");
|
|
return -1;
|
|
}
|
|
}
|
|
}
|