Skip to content

Commit

Permalink
Add connect, dataPtr, reorg struct features. Consolidated headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
billphipps committed Apr 18, 2024
1 parent 506e100 commit a06c0c4
Show file tree
Hide file tree
Showing 19 changed files with 664 additions and 357 deletions.
56 changes: 53 additions & 3 deletions port/posix/posix_transport_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <poll.h>

#include "wolfhsm/wh_error.h"
#include "wolfhsm/wh_transport.h"
#include "wolfhsm/wh_comm.h"
#include "port/posix/posix_transport_tcp.h"


Expand Down Expand Up @@ -215,7 +215,8 @@ static int posixTransportTcp_Recv(int fd, uint16_t* buffer_offset,
}

/** Client functions */
int posixTransportTcp_InitConnect(void* context, const void* config)
int posixTransportTcp_InitConnect(void* context, const void* config,
whCommSetConnectedCb connectcb, void* connectcb_arg)
{
int rc = 0;
posixTransportTcpClientContext* c = context;
Expand Down Expand Up @@ -270,6 +271,15 @@ int posixTransportTcp_InitConnect(void* context, const void* config)
}
}

c->connectcb = connectcb;
c->connectcb_arg = connectcb_arg;

/* Since we have started connecting already, invoke the connectcb so that
* we can continue to monitor connect status during recv.
*/
if (c->connectcb != NULL) {
c->connectcb(connectcb_arg, WH_COMM_CONNECTED);
}
/* All good */
return 0;
}
Expand Down Expand Up @@ -327,6 +337,11 @@ int posixTransportTcp_SendRequest(void* context,
c->buffer_offset = 0;
if (rc == 0) {
c->request_sent = 1;
} else {
/* Assume fatal error and trigger disconnect */
if (c->connectcb != NULL) {
c->connectcb(c->connectcb_arg, WH_COMM_DISCONNECTED);
}
}
}
return rc;
Expand Down Expand Up @@ -357,6 +372,12 @@ int posixTransportTcp_RecvResponse(void* context,
/* Success or fatal. Reset state either way */
c->buffer_offset = 0;
c->request_sent = 0;
if (rc != 0) {
/* Assume fatal error and trigger disconnect */
if (c->connectcb != NULL) {
c->connectcb(c->connectcb_arg, WH_COMM_DISCONNECTED);
}
}
}
return rc;
}
Expand All @@ -368,6 +389,11 @@ int posixTransportTcp_CleanupConnect(void* context)
return WH_ERROR_BADARGS;
}

/* Trigger disconnect */
if (c->connectcb != NULL) {
c->connectcb(c->connectcb_arg, WH_COMM_DISCONNECTED);
}

if (c->connect_fd_p1 != 0) {
close(c->connect_fd_p1 -1);
c->connect_fd_p1 = 0;
Expand All @@ -380,7 +406,8 @@ int posixTransportTcp_CleanupConnect(void* context)
/** Server Functions */


int posixTransportTcp_InitListen(void* context, const void* config)
int posixTransportTcp_InitListen(void* context, const void* config,
whCommSetConnectedCb connectcb, void* connectcb_arg)
{
int rc = 0;
posixTransportTcpServerContext* c = context;
Expand Down Expand Up @@ -435,6 +462,14 @@ int posixTransportTcp_InitListen(void* context, const void* config)
return WH_ERROR_ABORTED;
}

c->connectcb = connectcb;
c->connectcb_arg = connectcb_arg;

/* Connecting is handled internally so we need server to call recv */
if (c->connectcb != NULL) {
c->connectcb(c->connectcb_arg, WH_COMM_CONNECTED);
}

/* All good */
return 0;
}
Expand Down Expand Up @@ -489,6 +524,11 @@ int posixTransportTcp_RecvRequest(void* context,
c->buffer_offset = 0;
if (rc == 0) {
c->request_recv = 1;
} else {
/* Assume fatal error and trigger disconnect */
if (c->connectcb != NULL) {
c->connectcb(c->connectcb_arg, WH_COMM_DISCONNECTED);
}
}
}
return rc;
Expand Down Expand Up @@ -523,6 +563,11 @@ int posixTransportTcp_SendResponse(void* context,
c->buffer_offset = 0;
if (rc == 0) {
c->request_recv = 0;
} else {
/* Assume fatal error and trigger disconnect */
if (c->connectcb != NULL) {
c->connectcb(c->connectcb_arg, WH_COMM_DISCONNECTED);
}
}
}
return rc;
Expand All @@ -535,6 +580,11 @@ int posixTransportTcp_CleanupListen(void* context)
return WH_ERROR_BADARGS;
}

/* Trigger disconnect */
if (c->connectcb != NULL) {
c->connectcb(c->connectcb_arg, WH_COMM_DISCONNECTED);
}

if (c->accept_fd_p1 != 0) {
close(c->accept_fd_p1 - 1);
c->accept_fd_p1 = 0;
Expand Down
14 changes: 9 additions & 5 deletions port/posix/posix_transport_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@
#include <stdint.h>
#include <netinet/in.h>

#include "wolfhsm/wh_comm.h" /* For WH_COMM_MTU */
#include "wolfhsm/wh_transport.h"

#include "wolfhsm/wh_comm.h"

#define PTT_PACKET_MAX_SIZE WH_COMM_MTU
#define PTT_BUFFER_SIZE (sizeof(uint32_t) + PTT_PACKET_MAX_SIZE)
Expand All @@ -62,11 +60,14 @@ typedef struct {
int connect_fd_p1; /* fd plus 1 so 0 is invalid */
int connected;
int request_sent;
whCommSetConnectedCb connectcb;
void* connectcb_arg;
uint16_t buffer_offset;
uint8_t buffer[PTT_BUFFER_SIZE];
} posixTransportTcpClientContext;

int posixTransportTcp_InitConnect(void* context, const void* config);
int posixTransportTcp_InitConnect(void* context, const void* config,
whCommSetConnectedCb connectcb, void* connectcb_arg);
int posixTransportTcp_SendRequest(void* context, uint16_t size,
const void* data);
int posixTransportTcp_RecvResponse(void* context, uint16_t *out_size,
Expand All @@ -90,11 +91,14 @@ typedef struct {
int listen_fd_p1; /* fd plus 1 so 0 is invalid */
int accept_fd_p1; /* fd plus 1 so 0 is invalid */
int request_recv;
whCommSetConnectedCb connectcb;
void* connectcb_arg;
uint16_t buffer_offset;
uint8_t buffer[PTT_BUFFER_SIZE];
} posixTransportTcpServerContext;

int posixTransportTcp_InitListen(void* context, const void* config);
int posixTransportTcp_InitListen(void* context, const void* config,
whCommSetConnectedCb connectcb, void* connectcb_arg);
int posixTransportTcp_RecvRequest(void* context, uint16_t *out_size,
void* data);
int posixTransportTcp_SendResponse(void* context, uint16_t size,
Expand Down
134 changes: 132 additions & 2 deletions src/wh_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,142 @@ int wh_Client_RecvResponse(whClientContext *c,
return rc;
}

int wh_Client_CommInitRequest(whClientContext* c)
{
whMessageCommInitRequest msg = {0};

if (c == NULL) {
return WH_ERROR_BADARGS;
}

/* Populate the message.*/
msg.client_id = c->comm->client_id;

return wh_Client_SendRequest(c,
WH_MESSAGE_GROUP_COMM, WH_MESSAGE_COMM_ACTION_INIT,
sizeof(msg), &msg);
}

int wh_Client_CommInitResponse(whClientContext* c,
uint32_t *out_clientid,
uint32_t *out_serverid)
{
int rc = 0;
whMessageCommInitResponse msg = {0};
uint16_t resp_group = 0;
uint16_t resp_action = 0;
uint16_t resp_size = 0;

if (c == NULL) {
return WH_ERROR_BADARGS;
}

rc = wh_Client_RecvResponse(c,
&resp_group, &resp_action,
&resp_size, &msg);
if (rc == 0) {
/* Validate response */
if ( (resp_group != WH_MESSAGE_GROUP_COMM) ||
(resp_action != WH_MESSAGE_COMM_ACTION_INIT) ||
(resp_size != sizeof(msg)) ){
/* Invalid message */
rc = WH_ERROR_ABORTED;
} else {
/* Valid message */
if (out_clientid != NULL) {
*out_clientid = msg.client_id;
}
if (out_serverid != NULL) {
*out_serverid = msg.server_id;
}
}
}
return rc;
}

int wh_Client_CommInit(whClientContext* c,
uint32_t *out_clientid,
uint32_t *out_serverid)
{
int rc = 0;
if (c == NULL) {
return WH_ERROR_BADARGS;
}
do {
rc = wh_Client_CommInitRequest(c);
} while (rc == WH_ERROR_NOTREADY);

if (rc == 0) {
do {
rc = wh_Client_CommInitResponse(c, out_clientid, out_serverid);
} while (rc == WH_ERROR_NOTREADY);
}
return rc;
}

int wh_Client_CommCloseRequest(whClientContext* c)
{
if (c == NULL) {
return WH_ERROR_BADARGS;
}

return wh_Client_SendRequest(c,
WH_MESSAGE_GROUP_COMM, WH_MESSAGE_COMM_ACTION_CLOSE,
0, NULL);
}

int wh_Client_CommCloseResponse(whClientContext* c)
{
int rc = 0;
uint16_t resp_group = 0;
uint16_t resp_action = 0;
uint16_t resp_size = 0;

if (c == NULL) {
return WH_ERROR_BADARGS;
}

rc = wh_Client_RecvResponse(c,
&resp_group, &resp_action,
&resp_size, NULL);
if (rc == 0) {
/* Validate response */
if ( (resp_group != WH_MESSAGE_GROUP_COMM) ||
(resp_action != WH_MESSAGE_COMM_ACTION_CLOSE) ){
/* Invalid message */
rc = WH_ERROR_ABORTED;
} else {
/* Valid message. Server is now disconnected */
/* TODO: Cleanup the client */
}
}
return rc;
}

int wh_Client_CommClose(whClientContext* c)
{
int rc = 0;
if (c == NULL) {
return WH_ERROR_BADARGS;
}
do {
rc = wh_Client_CommCloseRequest(c);
} while (rc == WH_ERROR_NOTREADY);

if (rc == 0) {
do {
rc = wh_Client_CommCloseResponse(c);
} while (rc == WH_ERROR_NOTREADY);
}
return rc;
}

int wh_Client_EchoRequest(whClientContext* c, uint16_t size, const void* data)
{
whMessageCommLenData msg = {0};

if ( (c == NULL) ||
((size > 0) && (data == NULL)) ){
((size > 0) && (data == NULL)) ) {
return WH_ERROR_BADARGS;
}

Expand All @@ -154,7 +284,7 @@ int wh_Client_EchoResponse(whClientContext* c, uint16_t *out_size, void* data)
uint16_t resp_action = 0;
uint16_t resp_size = 0;

if (c == NULL){
if (c == NULL) {
return WH_ERROR_BADARGS;
}

Expand Down
Loading

0 comments on commit a06c0c4

Please sign in to comment.