update: more debug messages for send functions
This commit is contained in:
@ -147,7 +147,8 @@ void __lm_ctx_serve_thread(void *_arg) {
|
||||
}
|
||||
|
||||
// send package archive and the signature file
|
||||
lm_mptp_sendfile(arg->sock, pkg->archive, NULL, NULL);
|
||||
if(!lm_mptp_sendfile(arg->sock, pkg->archive, NULL, NULL))
|
||||
goto end; // if we fail to send the archive no need to send the signature
|
||||
lm_mptp_sendfile(arg->sock, pkg->signature, NULL, NULL);
|
||||
break;
|
||||
}
|
||||
|
@ -9,10 +9,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
bool lm_mptp_init(lm_mptp_t *packet, bool is_request, uint8_t code, bool is_last) {
|
||||
packet->header.flags = 0;
|
||||
packet->header.data_size = 0;
|
||||
packet->header.host_size = 0;
|
||||
|
||||
bzero(&packet->header, sizeof(packet->header));
|
||||
bzero(packet->data, MPTP_DATA_MAX);
|
||||
bzero(packet->host, MPTP_HOST_MAX);
|
||||
|
||||
|
@ -112,7 +112,7 @@ bool lm_mptp_server_send(int sock, lm_mptp_t *packet) {
|
||||
}
|
||||
|
||||
char buffer[sizeof(packet->header) + packet->header.host_size + packet->header.data_size];
|
||||
ssize_t total = sizeof(buffer), used = 0;
|
||||
ssize_t total = sizeof(buffer), used = 0, buflen = total;
|
||||
|
||||
packet->header.flags = htons(packet->header.flags);
|
||||
|
||||
@ -120,13 +120,13 @@ bool lm_mptp_server_send(int sock, lm_mptp_t *packet) {
|
||||
copy_to_buffer(buffer, packet->host, packet->header.host_size, &total, &used);
|
||||
copy_to_buffer(buffer, packet->data, packet->header.data_size, &total, &used);
|
||||
|
||||
pdebug(__func__, "printing the packet dump");
|
||||
pdebug_binary(buffer, sizeof(buffer));
|
||||
|
||||
if (send(sock, buffer, sizeof(buffer), 0) < 0) {
|
||||
if (send(sock, buffer, buflen, 0) < 0) {
|
||||
lm_error_set(LM_ERR_MPTPSendFail);
|
||||
return false;
|
||||
}
|
||||
|
||||
pdebug(__func__, "printing the packet dump (%lu bytes)", buflen);
|
||||
pdebug_binary(buffer, buflen);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -27,15 +27,13 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
if(NULL == file){
|
||||
pdebug(__func__, "failed to open file: %s", path);
|
||||
lm_error_set(LM_ERR_SendOpenFail);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
goto end;
|
||||
goto end_1;
|
||||
}
|
||||
|
||||
if(fstat(fileno(file), &st)<0){
|
||||
pdebug(__func__, "failed to stat file: %s", path);
|
||||
lm_error_set(LM_ERR_SendStatFail);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
goto end;
|
||||
goto end_1;
|
||||
}
|
||||
|
||||
total = st.st_size;
|
||||
@ -44,32 +42,31 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
if((size = snprintf(packet.data, MPTP_DATA_MAX, "%lu", st.st_size)) <= 0){
|
||||
pdebug(__func__, "snprintf for stat size failed: %s", path);
|
||||
lm_error_set(LM_ERR_SendSnprintfFail);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
goto end;
|
||||
goto end_1;
|
||||
}
|
||||
|
||||
packet.header.data_size = size;
|
||||
lm_mptp_server_send(sock, &packet);
|
||||
|
||||
if(NULL != callback)
|
||||
if(!callback(path, current, total, data))
|
||||
goto end;
|
||||
if(NULL != callback && !callback(path, current, total, data))
|
||||
goto end_1;
|
||||
|
||||
// clear the packet
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
|
||||
|
||||
while ((read = fread(packet.data, 1, MPTP_DATA_MAX, file)) > 0) {
|
||||
packet.header.data_size = read;
|
||||
pdebug(__func__, "sending the %lu/%lu of %s", current+read, total, path);
|
||||
|
||||
if(!lm_mptp_server_send(sock, &packet)){
|
||||
pdebug(__func__, "failed to send packet for %s (left at %lu/%lu): %s", path, current, total, lm_strerror());
|
||||
goto end;
|
||||
goto end_2;
|
||||
}
|
||||
|
||||
current += read;
|
||||
|
||||
if(NULL != callback)
|
||||
if(!callback(path, current, st.st_size, data))
|
||||
goto end;
|
||||
if(NULL != callback && !callback(path, current, st.st_size, data))
|
||||
goto end_1;
|
||||
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
|
||||
}
|
||||
@ -77,19 +74,21 @@ bool lm_mptp_sendfile(int sock, char *path, lm_mptp_transfer_callback_t callback
|
||||
if(current != total){
|
||||
pdebug(__func__, "failed read the entire file (left at %lu/%lu): %s", current, total, path);
|
||||
lm_error_set(LM_ERR_SendReadFail);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
goto end;
|
||||
goto end_1;
|
||||
}
|
||||
|
||||
pdebug(__func__, "completed sending %s, sending last packet", path);
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_COOL, true);
|
||||
lm_mptp_server_send(sock, &packet);
|
||||
ret = true;
|
||||
goto end_2;
|
||||
|
||||
end:
|
||||
end_1:
|
||||
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
|
||||
lm_mptp_server_send(sock, &packet);
|
||||
end_2:
|
||||
if(NULL != file)
|
||||
fclose(file);
|
||||
|
||||
lm_mptp_server_send(sock, &packet);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user