libmp/src/mptp/client.c

127 lines
3.1 KiB
C
Raw Normal View History

#include "../../include/error.h"
#include "../../include/mptp.h"
#include "../../include/util.h"
#include <stdbool.h>
#include <string.h>
#include <errno.h>
int lm_mptp_client_connect(char *addr, uint16_t port) {
struct sockaddr saddr;
int sock;
bzero(&saddr, sizeof(saddr));
if ((sock = lm_mptp_socket(addr, port, &saddr)) < 0)
return -1;
2024-08-07 23:30:51 +00:00
if(!lm_mptp_socket_opts(sock))
return -1;
if (connect(sock, &saddr, sizeof(saddr)) < 0) {
lm_mptp_close(sock);
lm_error_set(LM_ERR_MPTPConnectFail);
return -1;
}
return sock;
}
bool lm_mptp_client_verify(lm_mptp_t *packet) {
if (!lm_mptp_verify(packet)){
pdebug(__func__, "failed to verify the packet: %s", lm_strerror());
return false;
}
if (MPTP_IS_REQUEST(packet)) {
pdebug(__func__, "MPTP packet is a request");
lm_error_set(LM_ERR_MPTPNotResponse);
return false;
}
if (packet->header.host_size != 0) {
pdebug(__func__, "MPTP response has host section");
lm_error_set(LM_ERR_MPTPBadHost);
return false;
}
return true;
}
bool lm_mptp_client_send(int sock, lm_mptp_t *packet) {
if (NULL == packet) {
lm_error_set(LM_ERR_ArgNULL);
lm_mptp_free(packet);
return false;
}
if (MPTP_FLAGS_VERSION(packet) != MPTP_VERSION_SUPPORTED) {
lm_error_set(LM_ERR_MPTPBadVersion);
lm_mptp_free(packet);
return false;
}
if (packet->header.data_size > MPTP_DATA_MAX) {
lm_error_set(LM_ERR_MPTPBadData);
lm_mptp_free(packet);
return false;
}
if (packet->header.host_size > MPTP_HOST_MAX || packet->header.host_size <= 0) {
lm_error_set(LM_ERR_MPTPBadHost);
lm_mptp_free(packet);
return false;
}
char buffer[
sizeof(packet->header) +
packet->header.host_size +
packet->header.path_size +
packet->header.data_size
];
ssize_t total = sizeof(buffer), used = 0, buflen = total;
bool ret = false;
copy_to_buffer(buffer, &packet->header, sizeof(packet->header), &total, &used);
copy_to_buffer(buffer, packet->host, packet->header.host_size, &total, &used);
copy_to_buffer(buffer, packet->path, packet->header.path_size, &total, &used);
copy_to_buffer(buffer, packet->data, packet->header.data_size, &total, &used);
packet->header.flags = htons(packet->header.flags);
//packet->header.host_size = htons(packet->header.host_size);
//packet->header.path_size = htons(packet->header.path_size);
packet->header.data_size = htons(packet->header.data_size);
if (send(sock, buffer, sizeof(buffer), MSG_MORE) < 0) {
lm_error_set(LM_ERR_MPTPSendFail);
goto end;
}
pdebug(__func__, "printing the packet dump (%lu bytes)", buflen);
pdebug_binary(buffer, buflen);
ret = true;
end:
lm_mptp_free(packet);
return ret;
}
bool lm_mptp_client_recv(int sock, lm_mptp_t *packet) {
if (NULL == packet) {
lm_error_set(LM_ERR_ArgNULL);
return false;
}
lm_mptp_free(packet);
2024-08-08 12:38:26 +00:00
if(!lm_mptp_recv(sock, packet)){
pdebug(__func__, "failed to receive the packet: %s", lm_strerror());
return false; // error set by function
}
2024-08-09 20:52:49 +00:00
pdebug(__func__, "printing the header dump");
pdebug_binary((char*)&packet->header, sizeof(packet->header));
2024-08-09 20:52:49 +00:00
return true;
}