added comments for UDP part

This commit is contained in:
giomba 2019-12-24 23:51:00 +01:00
parent 260f537125
commit ac955647cf
2 changed files with 12 additions and 20 deletions

15
udp.h
View File

@ -2,30 +2,25 @@
#define COPPINO_UDP_H
namespace udp {
const uint8_t LEN = 128;
class UDPPacket {
private:
char packet[LEN];
char packet[ipv6::LEN - 40];
public:
UDPPacket();
UDPPacket(const char* packet, int len);
void setSrcPort(uint16_t port);
uint16_t getSrcPort();
void setDstPort(uint16_t port);
uint16_t getDstPort();
const char* UDPPacket::getPayload();
void setPayload(const char* payload, int len);
void computeChecksum(const ipv6::IPv6Addr* src_addr, const ipv6::IPv6Addr* dst_addr, int len);
void setPayload(const char* payload, int len); /* also sets proper length */
void computeChecksum(const ipv6::IPv6Addr* src_addr, const ipv6::IPv6Addr* dst_addr, int len); /* computes and updates checksum in the packet */
};
/* UDP handler for lower layer protocol */
int handleUDP(char* output_buffer, const char* input_buffer, int len);
/* pointer to server handler function */
extern int(*server_handler) (char* output_buffer, const char* buffer, int size);
extern uint16_t server_port;
void set_handler(uint16_t port, int(* handler) (char* output_buffer, const char* input_buffer, int size));
}

17
udp.ino
View File

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