#include "config.h"
#include "checksum.h"
#include "ethernet.h"
#include "ip.h"
#include "macros.h"
#include "nethelp.h"
#include "udp.h"
// some kind of "token bucket" for UDP echo
#define UDP_ECHO_TICKS 10 ///< allowed rate of UDP echo replies (in 200ms)
static unsigned int udp_echo_tick_cnt = 0; ///< tick counter
#define UDP_ECHO_REPLIES_MAX 3 ///< maximum value for udp_echo_replies
static unsigned int udp_echo_replies = 0; // # of allowed UDP echo replies
/// tick procedure - call every 200ms
void udp_tick200(void)
{
// count ticks
udp_echo_tick_cnt++;
// time to allow one reply more
if (udp_echo_tick_cnt >= UDP_ECHO_TICKS) {
udp_echo_tick_cnt = 0;
// increase reply count if not at maximum
if (udp_echo_replies < UDP_ECHO_REPLIES_MAX)
udp_echo_replies++;
}
}
/**
* @brief process a received UDP echo packet
* @param[in] ptr pointer to data of packet
* @param[in] sz size of packet
*/
static void udp_echo_recv(void *ptr, unsigned int sz)
{
struct udp_packet *udp_pack;
udp_pack = ptr;
// source port is UDP echo port
if (udp_pack->udp_hdr.src_port == htons(7))
// ignore this packet
// - UDP echo answer to another UDP echo port will result
// in endless echoing
return;
// only reply with allowed packet rate
if (udp_echo_replies <= 0)
return;
udp_echo_replies--;