Skip to content

Commit

Permalink
Merge pull request #1234 from atsign-foundation/jt/c-refactors
Browse files Browse the repository at this point in the history
chore: apply c sdk refactors
  • Loading branch information
XavierChanth authored Aug 8, 2024
2 parents 4a476b3 + e183875 commit f22570a
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 108 deletions.
2 changes: 1 addition & 1 deletion packages/c/cmake/atsdk.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ if(NOT atsdk_FOUND)
FetchContent_Declare(
atsdk
GIT_REPOSITORY https://github.com/atsign-foundation/at_c.git
GIT_TAG 18625590fdf4a24705ccad56e3d04b1bf1bdf5ee
GIT_TAG dc9fc44e789b91887a5be13c433d6039b72aaea6
)
FetchContent_MakeAvailable(atsdk)
install(TARGETS atclient atchops atlogger)
Expand Down
2 changes: 1 addition & 1 deletion packages/c/sshnpd/include/sshnpd/handle_npt_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
#define HANDLE_NPT_REQUEST_H
#include "sshnpd/params.h"
#include <atclient/monitor.h>
void handle_npt_request(sshnpd_params *params, atclient_monitor_message *message);
void handle_npt_request(sshnpd_params *params, atclient_monitor_response *message);
#endif
2 changes: 1 addition & 1 deletion packages/c/sshnpd/include/sshnpd/handle_ping.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
#include "sshnpd/params.h"
#include <atclient/monitor.h>
#include <pthread.h>
void handle_ping(sshnpd_params *params, atclient_monitor_message *message, char *ping_response, atclient *atclient,
void handle_ping(sshnpd_params *params, atclient_monitor_response *message, char *ping_response, atclient *atclient,
pthread_mutex_t *atclient_lock);
#endif
6 changes: 3 additions & 3 deletions packages/c/sshnpd/include/sshnpd/handle_ssh_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <pthread.h>

void handle_ssh_request(atclient *atclient, pthread_mutex_t *atclient_lock, sshnpd_params *params,
bool *is_child_process, atclient_monitor_message *message, char *home_dir, FILE *authkeys_file,
char *authkeys_filename, atchops_rsakey_privatekey signing_key);
bool *is_child_process, atclient_monitor_response *message, char *home_dir, FILE *authkeys_file,
char *authkeys_filename, atchops_rsa_key_private_key signing_key);

int verify_envelope_signature(atchops_rsakey_publickey publickey, const unsigned char *payload,
int verify_envelope_signature(atchops_rsa_key_public_key publickey, const unsigned char *payload,
unsigned char *signature, const char *hashing_algo, const char *signing_algo);

#endif
2 changes: 1 addition & 1 deletion packages/c/sshnpd/include/sshnpd/handle_sshpublickey.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
#define HANDLE_SSHPUBLICKEY_H
#include "sshnpd/params.h"
#include <atclient/monitor.h>
void handle_sshpublickey(sshnpd_params *params, atclient_monitor_message *message, FILE *authkeys_file,
void handle_sshpublickey(sshnpd_params *params, atclient_monitor_response *message, FILE *authkeys_file,
char *authkeys_filename);
#endif
20 changes: 10 additions & 10 deletions packages/c/sshnpd/src/background_jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) {
char atkey_buffer[buffer_len];
// example: @client_atsign:device_info.device_name.sshnp@client_atsign
snprintf(atkey_buffer, buffer_len, "%s%s", params->params->manager_list[index], infokey_base);
ret = atclient_atkey_from_string(infokeys + index, atkey_buffer, buffer_len);
ret = atclient_atkey_from_string(infokeys + index, atkey_buffer);

if (ret != 0) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create device_info atkey for %s\n",
Expand All @@ -66,36 +66,36 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) {
}

atclient_atkey_metadata *metadata = &(infokeys + index)->metadata;
atclient_atkey_metadata_set_ispublic(metadata, false);
atclient_atkey_metadata_set_isencrypted(metadata, true);
atclient_atkey_metadata_set_is_public(metadata, false);
atclient_atkey_metadata_set_is_encrypted(metadata, true);
atclient_atkey_metadata_set_ttr(metadata, -1);
atclient_atkey_metadata_set_ccd(metadata, true);
atclient_atkey_metadata_set_ttl(metadata, (long)30 * 24 * 60 * 60 * 1000); // 30 days in ms

buffer_len = strlen(params->params->manager_list[index]) + usernamekey_base_len;
// example: @client_atsign:device_info.device_name.sshnp@client_atsign
snprintf(atkey_buffer, buffer_len, "%s%s", params->params->manager_list[index], username_key_base);
ret = atclient_atkey_from_string(usernamekeys + index, atkey_buffer, buffer_len);
ret = atclient_atkey_from_string(usernamekeys + index, atkey_buffer);
if (ret != 0) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create username atkey for %s\n",
params->params->manager_list[index]);
break;
}

atclient_atkey_metadata *metadata2 = &(usernamekeys + index)->metadata;
atclient_atkey_metadata_set_ispublic(metadata2, false);
atclient_atkey_metadata_set_isencrypted(metadata2, true);
atclient_atkey_metadata_set_is_public(metadata2, false);
atclient_atkey_metadata_set_is_encrypted(metadata2, true);
atclient_atkey_metadata_set_ttr(metadata2, -1);
atclient_atkey_metadata_set_ccd(metadata2, true);
if (params->params->hide) {
ret = atclient_delete(params->atclient, usernamekeys + index);
ret = atclient_delete(params->atclient, usernamekeys + index, NULL, NULL);
if (ret != 0) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to delete username atkey for %s\n",
params->params->manager_list[index]);
break;
}
} else {
ret = atclient_put(params->atclient, usernamekeys + index, params->username, strlen(params->username), NULL);
ret = atclient_put_shared_key(params->atclient, usernamekeys + index, params->username, NULL, NULL);
if (ret != 0) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to put username atkey for %s\n",
params->params->manager_list[index]);
Expand Down Expand Up @@ -142,9 +142,9 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) {

for (int i = 0; i < num_managers; i++) {
if (params->params->hide) {
ret = atclient_delete(params->atclient, infokeys + i);
ret = atclient_delete(params->atclient, infokeys + i, NULL, NULL);
} else {
ret = atclient_put(params->atclient, infokeys + i, params->payload, strlen(params->payload), NULL);
ret = atclient_put_shared_key(params->atclient, infokeys + i, params->payload, NULL, NULL);
}
if (ret != 0) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to refresh device entry for %s\n",
Expand Down
2 changes: 1 addition & 1 deletion packages/c/sshnpd/src/handle_npt_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

#define LOGGER_TAG "NPT_REQUEST"

void handle_npt_request(sshnpd_params *params, atclient_monitor_message *message) { int res = 0; }
void handle_npt_request(sshnpd_params *params, atclient_monitor_response *response) { int res = 0; }
30 changes: 20 additions & 10 deletions packages/c/sshnpd/src/handle_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,40 @@

#define LOGGER_TAG "PING RESPONSE"

void handle_ping(sshnpd_params *params, atclient_monitor_message *message, char *ping_response, atclient *atclient,
void handle_ping(sshnpd_params *params, atclient_monitor_response *message, char *ping_response, atclient *atclient,
pthread_mutex_t *atclient_lock) {
int ret = 1;
atclient_atkey pingkey;
atclient_atkey_init(&pingkey);

size_t keynamelen = strlen("heartbeat") + strlen(params->device) + 2; // + 1 for '.' +1 for '\0'
char keyname[keynamelen];
snprintf(keyname, keynamelen, "heartbeat.%s", params->device);
atclient_atkey_create_sharedkey(&pingkey, keyname, keynamelen, params->atsign, strlen(params->atsign),
message->notification.from, strlen(message->notification.from), SSHNP_NS,
SSHNP_NS_LEN);
atclient_atkey_create_shared_key(&pingkey, keyname, params->atsign, message->notification.from, SSHNP_NS);

atclient_atkey_metadata *metadata = &pingkey.metadata;
atclient_atkey_metadata_set_ispublic(metadata, false);
atclient_atkey_metadata_set_isencrypted(metadata, true);
atclient_atkey_metadata_set_is_public(metadata, false);
atclient_atkey_metadata_set_is_encrypted(metadata, true);
atclient_atkey_metadata_set_ttl(metadata, 10000);

atclient_notify_params notify_params;
atclient_notify_params_init(&notify_params);
notify_params.atkey = &pingkey;
notify_params.value = ping_response;
notify_params.operation = ATCLIENT_NOTIFY_OPERATION_UPDATE;
if((ret = atclient_notify_params_set_atkey(&notify_params, &pingkey)) != 0) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set atkey in notify params\n");
goto exit_ping;
}

if((ret = atclient_notify_params_set_operation(&notify_params, ATCLIENT_NOTIFY_OPERATION_UPDATE)) != 0) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set operation in notify params\n");
goto exit_ping;
}

if((ret = atclient_notify_params_set_value(&notify_params, ping_response)) != 0) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to set value in notify params\n");
goto exit_ping;
}

int ret = pthread_mutex_lock(atclient_lock);
ret = pthread_mutex_lock(atclient_lock);
if (ret != 0) {
atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR,
"Failed to get a lock on atclient for sending a notification\n");
Expand Down
Loading

0 comments on commit f22570a

Please sign in to comment.