|
|
|
@ -2,6 +2,7 @@ |
|
|
|
#include <string.h> |
|
|
|
#include <ecdaa-tpm.h> |
|
|
|
#include "client.h" |
|
|
|
#include "server.h" |
|
|
|
|
|
|
|
#define ISSUERIP "127.0.0.1" |
|
|
|
#define ISSUERPORT 6590 |
|
|
|
@ -21,80 +22,130 @@ typedef enum memberstate { |
|
|
|
APPEND, |
|
|
|
JOINED, |
|
|
|
ATTEST, |
|
|
|
PUBLISH |
|
|
|
PUBLISH, |
|
|
|
SHUTDOWN |
|
|
|
}memberstate_t; |
|
|
|
|
|
|
|
int process_member_client(int connfd); |
|
|
|
int process_member(int connfd); |
|
|
|
int member_join(int connfd); |
|
|
|
|
|
|
|
int main() { |
|
|
|
int err = 0; |
|
|
|
client_t *client; |
|
|
|
server_t *server; |
|
|
|
|
|
|
|
client = client_open(&process_member_client); |
|
|
|
if (NULL == client) { |
|
|
|
server = server_open(&process_member, MEMBERPORT); |
|
|
|
if (NULL == server) { |
|
|
|
printf("listen failed, closing...\n"); |
|
|
|
} |
|
|
|
|
|
|
|
for (; !err;) { |
|
|
|
err = client_connect(client, ISSUERPORT, ISSUERIP); |
|
|
|
err = server_accept(server); |
|
|
|
if (1 == err) { |
|
|
|
printf("graceful shutdown...\n"); |
|
|
|
} else if (err) { |
|
|
|
printf("accept failed, closing...\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
printf("shut down client\n"); |
|
|
|
client_close(client); |
|
|
|
return 0; |
|
|
|
printf("shut down server\n"); |
|
|
|
server_close(server); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int process_member_client(int connfd) { |
|
|
|
int process_member(int connfd) { |
|
|
|
char inbuf[MAX_BUFSIZE]; |
|
|
|
char outbuf[MAX_BUFSIZE]; |
|
|
|
int n = 0; |
|
|
|
memberstate_t state = JOIN; |
|
|
|
int ret = 2; |
|
|
|
|
|
|
|
strncpy(outbuf, "JOIN", 4); |
|
|
|
printf("< MEMBER: %s\n", outbuf); |
|
|
|
write(connfd, outbuf, sizeof(outbuf)); |
|
|
|
for (;;) { |
|
|
|
while (ret == 2) { |
|
|
|
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("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: |
|
|
|
state = JOINED; |
|
|
|
if (0 == strncmp("JOINSTART", inbuf, 9)) { |
|
|
|
strncpy(outbuf, "APPEND\n", 7); |
|
|
|
state = JOINED; |
|
|
|
} |
|
|
|
break; |
|
|
|
case JOINED: |
|
|
|
state = ATTEST; |
|
|
|
state = PUBLISH; |
|
|
|
break; |
|
|
|
case ATTEST: |
|
|
|
state = JOINED; |
|
|
|
break; |
|
|
|
case PUBLISH: |
|
|
|
if (0 == strncmp("JOINPROCEED", inbuf, 11)) { |
|
|
|
strncpy(outbuf, "OK\n", 3); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
break; |
|
|
|
default: |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
printf("> MEMBER: %s\n", inbuf); |
|
|
|
strncpy(outbuf, "OK", 2); |
|
|
|
printf("< MEMBER: %s\n", outbuf); |
|
|
|
bzero(outbuf, MAX_BUFSIZE); |
|
|
|
printf("ISSUER < MEMBER: %s\n", outbuf); |
|
|
|
write(connfd, outbuf, sizeof(outbuf)); |
|
|
|
if (0 == strncmp("exit", inbuf, 4)) { |
|
|
|
printf("process_issuer: exiting gracefully\n"); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
if (0 == strncmp("shutdown", inbuf, 8)) { |
|
|
|
printf("process_issuer: shutting down gracefully\n"); |
|
|
|
return 1; |
|
|
|
|
|
|
|
bzero(inbuf, MAX_BUFSIZE); |
|
|
|
if (0 >= read(connfd, inbuf, sizeof(inbuf))) { |
|
|
|
printf("process_issuer: cannot read from socket\n"); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
} |
|
|
|
} |