Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11 ion nm fails to handle hex string with blank space #69

Merged
merged 12 commits into from
Feb 7, 2024
1 change: 1 addition & 0 deletions ion/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ RUN cd /usr/src/ion-ios && \
patch -p1 <ion-4.1.1-anms-db.patch && \
patch -p1 <ion-4.1.2-local-deliver.patch && \
patch -p1 <ion-4.1.1-amp-filestructure.patch && \
patch -p1 <dtnma-tools-nm_rest.patch && \
autoreconf -vif && \
./configure --with-postgresql --enable-nmrest && \
make -j$(nproc) && \
Expand Down
152 changes: 152 additions & 0 deletions ion/dtnma-tools-nm_rest.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
diff --git a/nm/mgr/nm_rest.c b/nm/mgr/nm_rest.c
index 72b63294..054134a4 100644
--- a/nm/mgr/nm_rest.c
+++ b/nm/mgr/nm_rest.c
@@ -278,11 +278,12 @@ static int agentsHandler(struct mg_connection *conn, void *cbdata)
return agentsGETHandler(conn);
} else if (0 == strcmp(ri->request_method, "POST")) {
char buffer[AMP_MAX_EID_LEN+1];
- int dlen = mg_read(conn, buffer, sizeof(buffer));
+ int dlen = mg_read(conn, buffer, sizeof(buffer) - 1);
if ( dlen < 1 ) {
mg_send_http_error(conn, 400, "Invalid request body data (expect EID name) %d", dlen);
return 400;
} else {
+ buffer[dlen] = '\0';
return agentsCreateHandler(conn, buffer);
}
} else {
@@ -300,34 +301,64 @@ static int agentSendRaw(struct mg_connection *conn, time_t ts, agent_t *agent, c
msg_ctrl_t *msg = NULL;
int success;

- blob_t *data = utils_string_to_hex(hex);
- if (data == NULL) {
+ if((msg = msg_ctrl_create()) == NULL)
+ {
mg_send_http_error(conn,
500,
- "Error creating blob from input");
+ "Error creating message");
return 500;
}
- id = ari_deserialize_raw(data, &success);
- blob_release(data, 1);
- if (id == NULL) {
+ if((msg->ac = ac_create()) == NULL)
+ {
+ msg_ctrl_release(msg, 1);
mg_send_http_error(conn,
500,
- "Error creating blob from input");
+ "Error creating AC");
return 500;
}

-
- ui_postprocess_ctrl(id);
+ msg->start = ts;

- if((msg = msg_ctrl_create_ari(id)) == NULL)
- {
- ari_release(id, 1);
- mg_send_http_error(conn,
- 500,
- "Error creating ARI from input");
- return HTTP_INTERNAL_ERROR;
+ char *part = NULL;
+ const char *ctrlsep = " \f\n\r\t\v"; // Identical to isspace()
+ char *saveptr = NULL;
+ part = strtok_r(hex, ctrlsep, &saveptr);
+ while(part != NULL) {
+ fprintf(stderr, "Handling message part %s\n", part);
+
+ blob_t *data = utils_string_to_hex(part);
+ if (data == NULL) {
+ mg_send_http_error(conn,
+ HTTP_INTERNAL_ERROR,
+ "Error creating blob from input");
+ msg_ctrl_release(msg, 1);
+ return HTTP_INTERNAL_ERROR;
+ }
+ id = ari_deserialize_raw(data, &success);
+ blob_release(data, 1);
+ if (id == NULL) {
+ mg_send_http_error(conn,
+ HTTP_INTERNAL_ERROR,
+ "Error decoding CTRL");
+ msg_ctrl_release(msg, 1);
+ return HTTP_INTERNAL_ERROR;
+ }
+
+ ui_postprocess_ctrl(id);
+ if(vec_push(&(msg->ac->values), id) != VEC_OK) {
+ mg_send_http_error(conn,
+ HTTP_INTERNAL_ERROR,
+ "Error adding CTRL to message");
+ ari_release(id, 1);
+ msg_ctrl_release(msg, 1);
+ return HTTP_INTERNAL_ERROR;
+ }
+
+ part = strtok_r(NULL, ctrlsep, &saveptr);
}
- msg->start = ts;
+
+ fprintf(stderr, "Sending message with %d controls\n", ac_get_count(msg->ac));
+
iif_send_msg(&ion_ptr, MSG_TYPE_PERF_CTRL, msg, agent->eid.name);
ui_log_transmit_msg(agent, msg);
msg_ctrl_release(msg, 1);
@@ -460,21 +491,23 @@ static int agentEidHandler(struct mg_connection *conn, void *cbdata)
}
else if (0 == strcmp(cmd, "hex"))
{
- // URL idx field translates to agent name
- // Optional query parameter "ts" will translate to timestamp (TODO: Always 0 for initial cut)
- // Request body contains CBOR-encoded HEX string
- char buffer[MAX_INPUT_BYTES];
- int dlen = mg_read(conn, buffer, sizeof(buffer));
- if (dlen <= 0) {
- return HTTP_BAD_REQUEST;
- }
- buffer[dlen] = 0; // Ensure string is NULL-terminated
- int ts = 0;
- if (cnt == 3) {
- // Optional Timestamp as last element of URL path
- ts = atoi(cmd2);
- }
- return agentSendRaw(conn,
+ // URL idx field translates to agent name
+ // Optional query parameter "ts" will translate to timestamp (TODO: Always 0 for initial cut)
+ // Request body contains CBOR-encoded HEX string
+ char buffer[MAX_INPUT_BYTES];
+ int dlen = mg_read(conn, buffer, sizeof(buffer) - 1);
+ if (dlen <= 0) {
+ return HTTP_BAD_REQUEST;
+ }
+ buffer[dlen] = '\0';
+
+ int ts = 0;
+ if (cnt == 3) {
+ // Optional Timestamp as last element of URL path
+ ts = atoi(cmd2);
+ }
+
+ return agentSendRaw(conn,
ts,
agent,
buffer
@@ -557,7 +590,11 @@ static int agentIdxHandler(struct mg_connection *conn, void *cbdata)
// Optional query parameter "ts" will translate to timestamp (TODO: Always 0 for initial cut)
// Request body contains CBOR-encoded HEX string
char buffer[MAX_INPUT_BYTES];
- int dlen = mg_read(conn, buffer, sizeof(buffer));
+ int dlen = mg_read(conn, buffer, sizeof(buffer) - 1);
+ if (dlen <= 0) {
+ return HTTP_BAD_REQUEST;
+ }
+ buffer[dlen] = '\0';
return agentSendRaw(conn,
0, // Timestamp TODO. This will be an optional query param
agent,
Loading