2024-06-26 19:33:20 +00:00
|
|
|
#include "../../include/error.h"
|
|
|
|
#include "../../include/mptp.h"
|
|
|
|
#include "../../include/util.h"
|
|
|
|
|
2024-08-07 01:49:33 +00:00
|
|
|
#include <errno.h>
|
2024-06-26 19:33:20 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
2024-08-07 01:49:33 +00:00
|
|
|
#include <sys/socket.h>
|
2024-06-26 19:33:20 +00:00
|
|
|
|
|
|
|
int lm_mptp_server_listen(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;
|
|
|
|
|
|
|
|
if (bind(sock, &saddr, sizeof(struct sockaddr)) < 0) {
|
|
|
|
lm_mptp_close(sock);
|
2024-08-07 23:30:51 +00:00
|
|
|
lm_error_set(LM_ERR_MPTPBindFail, strerror(errno));
|
2024-06-26 19:33:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-08-07 02:35:13 +00:00
|
|
|
if(listen(sock, SOMAXCONN) < 0){
|
|
|
|
lm_mptp_close(sock);
|
|
|
|
lm_error_set(LM_ERR_MPTPListenFail, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-06-26 19:33:20 +00:00
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2024-08-07 01:49:33 +00:00
|
|
|
int lm_mptp_server_accept(int sock, struct sockaddr *addr){
|
|
|
|
socklen_t sl = sizeof(struct sockaddr);
|
|
|
|
int s = -1;
|
|
|
|
|
2024-08-07 02:35:13 +00:00
|
|
|
if((s = accept(sock, addr, &sl)) < 0){
|
2024-08-07 01:49:33 +00:00
|
|
|
lm_error_set(LM_ERR_MPTPAcceptFail, strerror(errno));
|
|
|
|
s = -1;
|
|
|
|
}
|
|
|
|
|
2024-08-07 23:30:51 +00:00
|
|
|
if(!lm_mptp_socket_opts(s)){
|
|
|
|
close(s);
|
|
|
|
s = -1;
|
|
|
|
}
|
|
|
|
|
2024-08-07 01:49:33 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lm_mptp_server_close(int sock){
|
|
|
|
close(sock); // you didn't see that comming, did you?
|
|
|
|
}
|
|
|
|
|
2024-06-26 19:33:20 +00:00
|
|
|
bool lm_mptp_server_verify(lm_mptp_t *packet) {
|
|
|
|
if (!lm_mptp_verify(packet))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!MPTP_IS_REQUEST(packet)) {
|
|
|
|
lm_error_set(LM_ERR_MPTPNotRequest);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->header.host_size > MPTP_HOST_MAX || packet->header.host_size <= 0) {
|
|
|
|
lm_error_set(LM_ERR_MPTPBadHost);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MPTP_IS_LAST(packet)) {
|
|
|
|
lm_error_set(LM_ERR_MPTPNotLast);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-08-07 01:49:33 +00:00
|
|
|
bool lm_mptp_server_recv(int sock, lm_mptp_t *packet) {
|
|
|
|
if (NULL == packet) {
|
2024-06-26 19:33:20 +00:00
|
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[sizeof(packet->header) + MPTP_HOST_MAX + MPTP_DATA_MAX];
|
|
|
|
ssize_t total = sizeof(buffer), used = 0;
|
|
|
|
|
|
|
|
bzero(buffer, sizeof(buffer));
|
|
|
|
bzero(packet, sizeof(lm_mptp_t));
|
|
|
|
|
2024-08-07 01:49:33 +00:00
|
|
|
if (recv(sock, buffer, sizeof(buffer), 0) <= 0) {
|
2024-06-26 19:33:20 +00:00
|
|
|
lm_error_set(LM_ERR_MPTPRecvFail);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_from_buffer(&packet->header, buffer, sizeof(packet->header), &total, &used);
|
|
|
|
packet->header.flags = ntohs(packet->header.flags);
|
|
|
|
// packet->header.host_size = ntohs(packet->header.host_size);
|
|
|
|
// packet->header.data_size = ntohs(packet->header.data_size);
|
|
|
|
|
|
|
|
if (packet->header.host_size <= MPTP_HOST_MAX)
|
|
|
|
copy_from_buffer(&packet->host, buffer, packet->header.host_size, &total, &used);
|
|
|
|
|
|
|
|
if (packet->header.data_size <= MPTP_DATA_MAX)
|
|
|
|
copy_from_buffer(&packet->data, buffer, packet->header.data_size, &total, &used);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-08-07 01:49:33 +00:00
|
|
|
bool lm_mptp_server_send(int sock, lm_mptp_t *packet) {
|
2024-06-26 19:33:20 +00:00
|
|
|
if (NULL == packet) {
|
|
|
|
lm_error_set(LM_ERR_ArgNULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MPTP_FLAGS_VERSION(packet) != MPTP_VERSION_SUPPORTED) {
|
|
|
|
lm_error_set(LM_ERR_MPTPBadVersion);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->header.data_size > MPTP_DATA_MAX) {
|
|
|
|
lm_error_set(LM_ERR_MPTPBadData);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->header.host_size != 0) {
|
|
|
|
lm_error_set(LM_ERR_MPTPBadHost);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[sizeof(packet->header) + packet->header.host_size + packet->header.data_size];
|
|
|
|
ssize_t total = sizeof(buffer), used = 0;
|
|
|
|
|
|
|
|
packet->header.flags = htons(packet->header.flags);
|
|
|
|
|
|
|
|
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->data, packet->header.data_size, &total, &used);
|
|
|
|
|
2024-08-07 23:30:51 +00:00
|
|
|
pdebug(__func__, "printing the packet dump");
|
|
|
|
pdebug_binary(buffer, sizeof(buffer));
|
|
|
|
|
2024-08-07 01:49:33 +00:00
|
|
|
if (send(sock, buffer, sizeof(buffer), 0) < 0) {
|
2024-06-26 19:33:20 +00:00
|
|
|
lm_error_set(LM_ERR_MPTPSendFail);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|