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.
156 lines
4.3 KiB
156 lines
4.3 KiB
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <ecdaa.h>
|
|
#include "server.h"
|
|
#include "client.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 issuer {
|
|
struct ecdaa_issuer_public_key_FP256BN ipk;
|
|
struct ecdaa_issuer_secret_key_FP256BN isk;
|
|
} issuer_t;
|
|
|
|
typedef enum issuer_state {
|
|
ON,
|
|
SETUPDONE,
|
|
JOINSTART,
|
|
JOINPROCEED,
|
|
PUBLISH,
|
|
READY
|
|
} issuerstate_e;
|
|
|
|
int process_issuer(int connfd);
|
|
|
|
int main() {
|
|
int err = 0;
|
|
server_t *server;
|
|
|
|
server = server_open(&process_issuer, ISSUERPORT);
|
|
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_issuer(int connfd) {
|
|
char inbuf[MAX_BUFSIZE];
|
|
char outbuf[MAX_BUFSIZE];
|
|
int ret = 2;
|
|
issuerstate_e state = ON;
|
|
|
|
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("> ISSUER: %s\n", inbuf);
|
|
|
|
|
|
if (0 == strncmp("OK", inbuf, 2)) {
|
|
switch (state) {
|
|
case SETUPDONE:
|
|
case JOINPROCEED:
|
|
case PUBLISH:
|
|
state = READY;
|
|
break;
|
|
default:
|
|
strncpy(outbuf, "ERR\n", 4);
|
|
break;
|
|
}
|
|
} else if (0 == strncmp("ERR", inbuf, 3)) {
|
|
switch (state) {
|
|
case SETUPDONE:
|
|
case JOINPROCEED:
|
|
case PUBLISH:
|
|
printf("command failed at client\n");
|
|
state = READY;
|
|
break;
|
|
default:
|
|
printf("invalid instruction\n");
|
|
strncpy(outbuf, "ERR\n", 4);
|
|
break;
|
|
}
|
|
} else if (0 == strncmp("SETUP", inbuf, 5)) {
|
|
switch (state) {
|
|
case ON:
|
|
printf("setup()\n");
|
|
strncpy(outbuf, "SETUPDONE\n", 10);
|
|
state = SETUPDONE;
|
|
break;
|
|
default:
|
|
strncpy(outbuf, "ERR\n", 4);
|
|
break;
|
|
}
|
|
} else if (0 == strncmp("JOIN", inbuf, 4)) {
|
|
switch (state) {
|
|
case READY:
|
|
printf("join()\n");
|
|
strncpy(outbuf, "JOINSTART\n", 10);
|
|
state = JOINSTART;
|
|
break;
|
|
default:
|
|
strncpy(outbuf, "ERR\n", 4);
|
|
break;
|
|
}
|
|
} else if (0 == strncmp("APPEND", inbuf, 6)) {
|
|
switch (state) {
|
|
case JOINSTART:
|
|
printf("append()\n");
|
|
strncpy(outbuf, "JOINPROCEED\n", 12);
|
|
state = JOINPROCEED;
|
|
break;
|
|
default:
|
|
strncpy(outbuf, "ERR\n", 4);
|
|
break;
|
|
}
|
|
} else if (0 == strncmp("PUBLISH", inbuf, 7)) {
|
|
switch (state) {
|
|
case READY:
|
|
printf("publish()\n");
|
|
strncpy(outbuf, "PUBLISH\n", 8);
|
|
state = PUBLISH;
|
|
break;
|
|
default:
|
|
strncpy(outbuf, "ERR\n", 4);
|
|
break;
|
|
}
|
|
} 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 = 0;
|
|
}
|
|
|
|
printf("< ISSUER: %s\n", outbuf);
|
|
write(connfd, outbuf, sizeof(outbuf));
|
|
}
|
|
|
|
return
|
|
ret;
|
|
}
|
|
|