security communication with openssl and simplify netio

This commit is contained in:
Rongsong Shen 2025-11-18 11:21:24 +08:00
parent 2a7b7c39bb
commit 8f796b3109
20 changed files with 1176 additions and 922 deletions

View file

@ -11,134 +11,94 @@
#include "utils.h"
#include "xce/types.h"
ioctx_t* ioctx_alloc(endpoint_t* ep, xce_peerinfo_t* peer)
{
ioctx_t* ctx;
ctx = (ioctx_t*)calloc(1, sizeof(*ctx));
if (ctx == NULL) {
errno = -ENOMEM;
return NULL;
}
ctx->ep = endpoint_ref(ep);
ctx->from = peer;
ctx->nrefs++;
return ctx;
}
int ioctx_ref(ioctx_t* ctx)
{
int old = ctx->nrefs;
ctx->nrefs++;
return old;
}
static void ioctx_kill(ioctx_t* ctx, int free_ctx)
{
if (ctx == NULL) {
return;
}
endpoint_deref(ctx->ep);
free_packet(&(ctx->pkt));
capn_free(&(ctx->c));
if (free_ctx) {
free(ctx);
}
}
void ioctx_deref(ioctx_t* ctx, int free_ctx)
{
if (ctx == NULL) {
return;
}
if (ctx->nrefs == 0) {
WARN("Something wrong. Have you forgotten to call ioctx_ref()?");
return;
}
ctx->nrefs--;
if (ctx->nrefs == 0) {
ioctx_kill(ctx, free_ctx);
}
}
int ioctx_encode(ioctx_t* ctx, xce_request_t* req)
int xce_encode(xce_request_t* req, io_packet_t* pkt)
{
struct capn c;
struct Request rq;
struct capn_segment* cs;
Request_ptr rp;
int rc;
capn_init_malloc(&(ctx->c));
cs = capn_root(&(ctx->c)).seg;
TRACE("%s request op %d seq %ld", __func__, req->opcode, req->seq);
encode_Request(cs, &(ctx->rq), req);
capn_init_malloc(&c);
cs = capn_root(&c).seg;
cs = capn_root(&(ctx->c)).seg;
encode_Request(cs, &rq, req);
cs = capn_root(&c).seg;
rp = new_Request(cs);
write_Request(&(ctx->rq), rp);
write_Request(&rq, rp);
capn_setp(capn_root(&(ctx->c)), 0, rp.p);
capn_setp(capn_root(&c), 0, rp.p);
ctx->pkt.sz = capn_size(&(ctx->c));
rc = alloc_packet(&(ctx->pkt));
pkt->sz = capn_size(&c);
rc = alloc_packet(pkt);
if (rc < 0) {
return rc;
capn_free(&c);
return -errno;
}
capn_write_mem(&(ctx->c), (uint8_t*)ctx->pkt.data, ctx->pkt.sz, 0);
return 0;
capn_write_mem(&c, (uint8_t*)pkt->data, pkt->sz, 0);
capn_free(&c);
return 1;
}
int ioctx_decode(ioctx_t* ctx, xce_request_t* req)
int xce_decode(io_packet_t* pkt, xce_request_t* req)
{
Request_ptr rp;
struct capn c;
struct Request rq;
Request_ptr rp;
capn_init_mem(&(ctx->c), (const uint8_t*)ctx->pkt.data, ctx->pkt.sz, 0);
rp.p = capn_getp(capn_root(&(ctx->c)), 0, 1);
capn_init_mem(&c, (const uint8_t*)pkt->data, pkt->sz, 0);
rp.p = capn_getp(capn_root(&c), 0, 1);
read_Request(&(ctx->rq), rp);
read_Request(&rq, rp);
decode_Request(req, &(ctx->rq));
decode_Request(req, &rq);
TRACE("%s request opcode %d seq %ld", __func__, req->opcode, req->seq);
capn_free(&c);
free_packet(pkt);
FREEUP(pkt);
return 0;
}
int ioctx_flush(ioctx_t* ctx, int fd)
{
int rc;
rc = write_packet(fd, &(ctx->pkt));
return rc;
}
void init_reply_by_req(xce_reply_t* reply, xce_request_t* req)
{
reply->seq = req->seq;
reply->opcode = req->opcode;
}
int xce_request(ioctx_t* ctx, xce_request_t* req)
int xce_request(endpoint_t* ep, xce_request_t* req)
{
int rc;
int rc;
io_packet_t* pkt;
req->seq = endpoint_getseq(ctx->ep);
req->from = ctx->from;
pkt = (io_packet_t*)calloc(1, sizeof(*pkt));
if (pkt == NULL) {
return -ENOMEM;
}
rc = ioctx_encode(ctx, req);
req->seq = endpoint_getseq(ep);
req->from = ep->peer;
rc = xce_encode(req, pkt);
if (rc < 0) {
free_packet(pkt);
free(pkt);
return rc;
}
endpoint_enq(ctx->ep, &(ctx->link));
endpoint_enq(ep, &(pkt->link));
return 0;
}
int xce_reply_with_ctx(ioctx_t* ctx, xce_peerinfo_t* from, xce_reply_t* reply)
int xce_reply(endpoint_t* ep, xce_peerinfo_t* from, xce_reply_t* reply)
{
/* clang-format off */
xce_request_t request = {
@ -146,50 +106,47 @@ int xce_reply_with_ctx(ioctx_t* ctx, xce_peerinfo_t* from, xce_reply_t* reply)
.body_which = Request_body_replymsg,
};
/* clang-format on */
int rc;
int rc;
io_packet_t* pkt;
TRACE("%s seq %lu opcode %d status %d data which %d", __func__, reply->seq,
reply->opcode, reply->status, reply->data_which);
request.from = from;
request.body.replymsg = reply;
request.seq = endpoint_getseq(ctx->ep);
rc = ioctx_encode(ctx, &request);
if (rc < 0) {
return rc;
}
endpoint_enq(ctx->ep, &(ctx->link));
return 0;
}
int xce_reply(endpoint_t* ep, xce_peerinfo_t* from, xce_reply_t* reply)
{
ioctx_t* ctx;
ctx = ioctx_alloc(ep, from);
if (ctx == NULL) {
pkt = (io_packet_t*)calloc(1, sizeof(*pkt));
if (pkt == NULL) {
return -ENOMEM;
}
return xce_reply_with_ctx(ctx, from, reply);
request.from = from;
request.body.replymsg = reply;
request.seq = endpoint_getseq(ep);
rc = xce_encode(&request, pkt);
if (rc < 0) {
free_packet(pkt);
free(pkt);
return rc;
}
endpoint_enq(ep, &(pkt->link));
return 0;
}
int xce_nop_req(ioctx_t* ctx)
int xce_nop_req(endpoint_t* ep)
{
xce_request_t request
= { .opcode = OPCode_nop, .body_which = Request_body_nomsg };
if (ctx == NULL) {
if (ep == NULL) {
return -EINVAL;
}
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_hello_req(ioctx_t* ctx, char* msg)
int xce_hello_req(endpoint_t* ep, char* msg)
{
/* clang-format off */
xce_request_t request = {
@ -201,13 +158,13 @@ int xce_hello_req(ioctx_t* ctx, char* msg)
};
/* clang-format on */
if ((ctx == NULL) || (msg == NULL)) {
if ((ep == NULL) || (msg == NULL)) {
return -EINVAL;
}
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_join_req(ioctx_t* ctx, int role, hostattrs_t* attrs)
int xce_join_req(endpoint_t* ep, int role, hostattrs_t* attrs)
{
/* clang-format off */
xce_joindata_t joinmsg = {
@ -222,8 +179,8 @@ int xce_join_req(ioctx_t* ctx, int role, hostattrs_t* attrs)
};
if ((role != HostRole_client) &&
(role != HostRole_worker) &&
(role != HostRole_scheduler)) {
(role != HostRole_worker) &&
(role != HostRole_scheduler)) {
return -EINVAL;
}
/* clang-format on */
@ -236,26 +193,26 @@ int xce_join_req(ioctx_t* ctx, int role, hostattrs_t* attrs)
joinmsg.data_which = JoinData_data_attrs;
joinmsg.data.attrs = attrs;
}
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_shutdown_req(ioctx_t* ctx)
int xce_shutdown_req(endpoint_t* ep)
{
int rc;
dataq_t* outq;
xce_request_t request
= { .opcode = OPCode_shutdown, .body_which = Request_body_nomsg };
if (ctx == NULL) {
if (ep == NULL) {
return -EINVAL;
}
rc = xce_request(ctx, &request);
rc = xce_request(ep, &request);
if (rc < 0) {
return rc;
}
outq = &(ctx->ep->outq);
outq = &(ep->outq);
while (!DATAQ_EMPTY(outq)) {
sleep(1);
@ -267,30 +224,25 @@ int xce_shutdown_req(ioctx_t* ctx)
int xce_pushconfig_req(endpoint_t* ep, xce_peerinfo_t* from,
xce_configdata_t* config)
{
int rc = 0;
int rc = 0;
/* clang-format off */
xce_request_t request
= { .opcode = OPCode_push, .body_which = Request_body_configpushmsg };
ioctx_t* ctx = NULL;
/* clang-format on */
if ((ep == NULL) || (config == NULL)) {
return -EINVAL;
}
ctx = ioctx_alloc(ep, from);
if (ctx == NULL) {
return -ENOMEM;
}
request.body.configpushmsg = config;
rc = xce_request(ctx, &request);
rc = xce_request(ep, &request);
return rc;
}
static int xce_push_req(endpoint_t* ep, xce_peerinfo_t* from, xce_data_t* data)
{
int rc = 0;
ioctx_t* ctx = NULL;
int rc = 0;
xce_request_t request
= { .opcode = OPCode_push, .body_which = Request_body_pushmsg };
@ -298,14 +250,9 @@ static int xce_push_req(endpoint_t* ep, xce_peerinfo_t* from, xce_data_t* data)
return -EINVAL;
}
ctx = ioctx_alloc(ep, from);
if (ctx == NULL) {
return -ENOMEM;
}
request.body.pushmsg = data;
rc = xce_request(ctx, &request);
rc = xce_request(ep, &request);
return rc;
}
@ -416,7 +363,7 @@ int xce_push_data_req(endpoint_t* ep, xce_peerinfo_t* from, xce_data_t* data,
return rc;
}
int xce_get_req(ioctx_t* ctx, xce_get_t* get)
int xce_get_req(endpoint_t* ep, xce_get_t* get)
{
/* clang-format off */
xce_request_t request = {
@ -428,7 +375,7 @@ int xce_get_req(ioctx_t* ctx, xce_get_t* get)
};
/* clang-format on */
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_reply_get(endpoint_t* ep, xce_peerinfo_t* from, xce_reply_t* reply,
@ -699,7 +646,7 @@ int extract_data(dataset_t* dataset, xce_data_t* getdata)
return 1;
}
int xce_jobdispatch_req(ioctx_t* ctx, xce_job_dispatch_t* info)
int xce_jobdispatch_req(endpoint_t* ep, xce_job_dispatch_t* info)
{
/* clang-format off */
xce_request_t request = {
@ -712,16 +659,16 @@ int xce_jobdispatch_req(ioctx_t* ctx, xce_job_dispatch_t* info)
/* clang-format on */
/* clang-format off */
if ((ctx == NULL) ||
(info == NULL)) {
if ((ep == NULL) ||
(info == NULL)) {
return -EINVAL;
}
/* clang-format on */
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_notify_req(ioctx_t* ctx, xce_notify_data_t* data)
int xce_notify_req(endpoint_t* ep, xce_notify_data_t* data)
{
/* clang-format off */
xce_request_t request = {
@ -731,17 +678,16 @@ int xce_notify_req(ioctx_t* ctx, xce_notify_data_t* data)
.notifymsg = data
}
};
/* clang-forn on */
/* clang-format on */
if ((ctx == NULL) || (data == NULL)) {
return -EINVAL;
if ((ep == NULL) || (data == NULL)) {
return -EINVAL;
}
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_jobsubmit_req(ioctx_t* ctx,
xce_job_submit_t* submit)
int xce_jobsubmit_req(endpoint_t* ep, xce_job_submit_t* submit)
{
/* clang-format off */
xce_request_t request = {
@ -754,16 +700,16 @@ int xce_jobsubmit_req(ioctx_t* ctx,
/*clang-format on*/
/* clang-format off */
if ((ctx == NULL) ||
(submit == NULL)) {
return -EINVAL;
if ((ep == NULL) ||
(submit == NULL)) {
return -EINVAL;
}
/* clang-format on */
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_startjob_req(ioctx_t* ctx, xce_job_exec_t* execinfo)
int xce_startjob_req(endpoint_t* ep, xce_job_exec_t* execinfo)
{
/* clang-format off */
xce_request_t request = {
@ -775,10 +721,10 @@ int xce_startjob_req(ioctx_t* ctx, xce_job_exec_t* execinfo)
};
/* clang-format on */
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_clusterconf_req(ioctx_t* ctx)
int xce_clusterconf_req(endpoint_t* ep)
{
/* clang-format off */
xce_request_t request = {
@ -787,10 +733,10 @@ int xce_clusterconf_req(ioctx_t* ctx)
};
/* clang-format on */
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_update_req(ioctx_t* ctx, xce_update_data_t* data)
int xce_update_req(endpoint_t* ep, xce_update_data_t* data)
{
/* clang-format off */
xce_request_t request = {
@ -802,14 +748,14 @@ int xce_update_req(ioctx_t* ctx, xce_update_data_t* data)
};
/* clang-format on */
if ((ctx == NULL) || (data == NULL)) {
if ((ep == NULL) || (data == NULL)) {
return -EINVAL;
}
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_jobctrl_req(ioctx_t* ctx, xce_jobctrl_t* ctrinfo)
int xce_jobctrl_req(endpoint_t* ep, xce_jobctrl_t* ctrinfo)
{
/* clang-format off */
xce_request_t request = {
@ -821,7 +767,7 @@ int xce_jobctrl_req(ioctx_t* ctx, xce_jobctrl_t* ctrinfo)
};
/* clang-format on */
if ((ctx == NULL) || (ctrinfo == NULL)) {
if ((ep == NULL) || (ctrinfo == NULL)) {
return -EINVAL;
}
@ -841,10 +787,10 @@ int xce_jobctrl_req(ioctx_t* ctx, xce_jobctrl_t* ctrinfo)
return -EINVAL;
break;
}
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_queuectrl_req(ioctx_t* ctx, xce_queuectrl_t* ctrlinfo)
int xce_queuectrl_req(endpoint_t* ep, xce_queuectrl_t* ctrlinfo)
{
/* clang-format off */
xce_request_t request = {
@ -856,7 +802,7 @@ int xce_queuectrl_req(ioctx_t* ctx, xce_queuectrl_t* ctrlinfo)
};
/* clang-format on */
if ((ctx == NULL) || (ctrlinfo == NULL)) {
if ((ep == NULL) || (ctrlinfo == NULL)) {
return -EINVAL;
}
@ -872,10 +818,10 @@ int xce_queuectrl_req(ioctx_t* ctx, xce_queuectrl_t* ctrlinfo)
return -EINVAL;
}
return xce_request(ctx, &request);
return xce_request(ep, &request);
}
int xce_runtask_req(ioctx_t* ctx, xce_taskrun_t* runinfo)
int xce_runtask_req(endpoint_t* ep, xce_taskrun_t* runinfo)
{
/* clang-format off */
xce_request_t request = {
@ -887,7 +833,7 @@ int xce_runtask_req(ioctx_t* ctx, xce_taskrun_t* runinfo)
};
/* clang-format on */
if ((ctx == NULL) || (runinfo == NULL)) {
if ((ep == NULL) || (runinfo == NULL)) {
return -EINVAL;
}
@ -895,5 +841,5 @@ int xce_runtask_req(ioctx_t* ctx, xce_taskrun_t* runinfo)
return -EINVAL;
}
return xce_request(ctx, &request);
return xce_request(ep, &request);
}