udp: format

This commit is contained in:
giomba 2021-12-26 16:07:18 +01:00
parent 09febbfb75
commit 7cfec6898e

View File

@ -1,77 +1,90 @@
#include "udp.h" #include "udp.h"
namespace udp { namespace udp
{
int (*server_handler)(char *output_buffer, const char *input_buffer, int size) = NULL;
uint16_t server_port = 0;
int(*server_handler) (char* output_buffer, const char* input_buffer, int size) = NULL; void UDPPacket::setSrcPort(uint16_t port)
uint16_t server_port = 0; {
*(packet + 0) = port >> 8;
*(packet + 1) = port & 0xff;
}
void UDPPacket::setSrcPort(uint16_t port) { void UDPPacket::setDstPort(uint16_t port)
*(packet + 0) = port >> 8; {
*(packet + 1) = port & 0xff; *(packet + 2) = port >> 8;
} *(packet + 3) = port & 0xff;
}
void UDPPacket::setDstPort(uint16_t port) { uint16_t UDPPacket::getSrcPort()
*(packet + 2) = port >> 8; {
*(packet + 3) = port & 0xff; return (*(packet + 0) << 8) + (uint8_t) * (packet + 1);
} }
uint16_t UDPPacket::getDstPort()
{
return (*(packet + 2) << 8) + (uint8_t) * (packet + 3);
}
uint16_t UDPPacket::getSrcPort() { /* sets the payload and also updates the packet length field */
return (*(packet + 0) << 8) + (uint8_t)*(packet + 1); void UDPPacket::setPayload(const char *payload, int len)
} {
uint16_t UDPPacket::getDstPort() { memcpy(packet + 8, payload, len);
return (*(packet + 2) << 8) + (uint8_t)*(packet + 3); *(packet + 4) = (len + 8) >> 8;
} *(packet + 5) = (len + 8) & 0xff;
}
const char *UDPPacket::getPayload()
{
return packet + 8;
}
/* sets the payload and also updates the packet length field */ /* computes the checksum and also updates it inside the UDP packet */
void UDPPacket::setPayload(const char* payload, int len) { void UDPPacket::computeChecksum(const ipv6::IPv6Addr *src_addr, const ipv6::IPv6Addr *dst_addr, int len)
memcpy(packet + 8, payload, len); {
*(packet + 4) = (len + 8) >> 8; memset(packet + 6, 0, 2);
*(packet + 5) = (len + 8) & 0xff; uint16_t checksum = ipv6::compute_checksum(src_addr, dst_addr, ipv6::NH_UDP, len, packet, len);
} memcpy(packet + 6, &checksum, 2);
const char* UDPPacket::getPayload() { }
return packet + 8;
}
/* computes the checksum and also updates it inside the UDP packet */ /* sets an handler (a function) which will be called every time there is a packet for a certain given port */
void UDPPacket::computeChecksum(const ipv6::IPv6Addr* src_addr, const ipv6::IPv6Addr* dst_addr, int len) { void set_handler(uint16_t port, int (*server_handler)(char *output_buffer, const char *input_buffer, int size))
memset(packet + 6, 0, 2); {
uint16_t checksum = ipv6::compute_checksum(src_addr, dst_addr, ipv6::NH_UDP, len, packet, len); udp::server_port = port;
memcpy(packet + 6, &checksum, 2); udp::server_handler = server_handler;
} }
/* sets an handler (a function) which will be called every time there is a packet for a certain given port */ /* handles an UDP packet, and (possibly) produces a reply UDP packet
void set_handler(uint16_t port, int(* server_handler) (char* output_buffer, const char* input_buffer, int size)) { * returns length of reply packet, which is written in output_buffer, or -1 if there is no reply packet
udp::server_port = port; */
udp::server_handler = server_handler; int handleUDP(char *output_buffer, const char *input_buffer, int ilen)
} {
UDPPacket *incoming_packet = (UDPPacket *)input_buffer;
UDPPacket *outgoing_packet = (UDPPacket *)output_buffer;
int olen;
/* handles an UDP packet, and (possibly) produces a reply UDP packet if (incoming_packet->getDstPort() == server_port)
* returns length of reply packet, which is written in output_buffer, or -1 if there is no reply packet { /* call user-defined server handler */
*/ if (server_port != 0 && server_handler != NULL)
int handleUDP(char* output_buffer, const char* input_buffer, int ilen) { {
UDPPacket* incoming_packet = (UDPPacket*)input_buffer; olen = server_handler(outgoing_packet->getPayload(), incoming_packet->getPayload(), ilen - 8);
UDPPacket* outgoing_packet = (UDPPacket*)output_buffer;
int olen;
if (incoming_packet->getDstPort() == server_port) { /* call user-defined server handler */ outgoing_packet->setSrcPort(incoming_packet->getDstPort());
if (server_port != 0 && server_handler != NULL) { outgoing_packet->setDstPort(incoming_packet->getSrcPort());
olen = server_handler(outgoing_packet->getPayload(), incoming_packet->getPayload(), ilen - 8); outgoing_packet->setPayload(outgoing_packet->getPayload(), olen);
outgoing_packet->computeChecksum(ipv6::packetin.getDstAddress(), ipv6::packetin.getSrcAddress(), olen + 8);
return olen + 8;
}
}
else
{ /* default UDP application handler: echoes back the packet content */
outgoing_packet->setSrcPort(incoming_packet->getDstPort()); outgoing_packet->setSrcPort(incoming_packet->getDstPort());
outgoing_packet->setDstPort(incoming_packet->getSrcPort()); outgoing_packet->setDstPort(incoming_packet->getSrcPort());
outgoing_packet->setPayload(outgoing_packet->getPayload(), olen); outgoing_packet->setPayload(incoming_packet->getPayload(), ilen - 8);
outgoing_packet->computeChecksum(ipv6::packetin.getDstAddress(), ipv6::packetin.getSrcAddress(), olen + 8); outgoing_packet->computeChecksum(ipv6::packetin.getSrcAddress(), ipv6::packetin.getDstAddress(), ilen);
return olen + 8; return ilen;
} }
} else { /* default UDP application handler: echoes back the packet content */
outgoing_packet->setSrcPort(incoming_packet->getDstPort()); return -1;
outgoing_packet->setDstPort(incoming_packet->getSrcPort());
outgoing_packet->setPayload(incoming_packet->getPayload(), ilen - 8);
outgoing_packet->computeChecksum(ipv6::packetin.getSrcAddress(), ipv6::packetin.getDstAddress(), ilen);
return ilen;
} }
return -1;
}
} }