2007-11-25 19:42:40 +00:00
|
|
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
|
2008-08-14 17:19:13 +00:00
|
|
|
#include <base/system.h>
|
2007-07-13 13:40:04 +00:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2008-10-19 09:10:55 +00:00
|
|
|
struct PACKET
|
2007-07-13 13:40:04 +00:00
|
|
|
{
|
2008-10-19 09:10:55 +00:00
|
|
|
PACKET *prev;
|
|
|
|
PACKET *next;
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
NETADDR send_to;
|
2007-07-13 13:40:04 +00:00
|
|
|
int64 timestamp;
|
|
|
|
int id;
|
|
|
|
int data_size;
|
|
|
|
char data[1];
|
|
|
|
};
|
|
|
|
|
2008-10-19 09:10:55 +00:00
|
|
|
static PACKET *first = (PACKET *)0;
|
|
|
|
static PACKET *last = (PACKET *)0;
|
2007-07-13 13:40:04 +00:00
|
|
|
static int current_latency = 0;
|
2008-10-20 19:59:33 +00:00
|
|
|
|
2008-11-08 14:04:16 +00:00
|
|
|
static int config_log = 0;
|
|
|
|
static int config_ping = 50;
|
|
|
|
static int config_pingflux = 15;
|
2008-10-20 19:59:33 +00:00
|
|
|
static int config_pingspike = 0;
|
2008-11-08 14:04:16 +00:00
|
|
|
static int config_packetloss = 1; // in percent
|
|
|
|
static int config_reorder = 1;
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int run(int port, NETADDR dest)
|
2007-07-13 13:40:04 +00:00
|
|
|
{
|
2008-07-06 11:21:21 +00:00
|
|
|
NETADDR src = {NETTYPE_IPV4, {0,0,0,0},port};
|
|
|
|
NETSOCKET socket = net_udp_create(src);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
char buffer[1024*2];
|
|
|
|
int id = 0;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
// handle incomming packets
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
// fetch data
|
2008-03-18 01:30:47 +00:00
|
|
|
int data_trash = 0;
|
2008-07-06 11:21:21 +00:00
|
|
|
NETADDR from;
|
|
|
|
int bytes = net_udp_recv(socket, &from, buffer, 1024*2);
|
2007-07-13 13:40:04 +00:00
|
|
|
if(bytes <= 0)
|
|
|
|
break;
|
2007-07-29 22:09:15 +00:00
|
|
|
|
2008-10-20 19:59:33 +00:00
|
|
|
if((rand()%100) < config_packetloss) // drop the packet
|
|
|
|
{
|
2008-11-08 14:04:16 +00:00
|
|
|
if(config_log)
|
|
|
|
dbg_msg("crapnet", "dropped packet");
|
2008-10-19 09:10:55 +00:00
|
|
|
continue;
|
2008-10-20 19:59:33 +00:00
|
|
|
}
|
2007-07-13 13:40:04 +00:00
|
|
|
|
|
|
|
// create new packet
|
2008-10-19 09:10:55 +00:00
|
|
|
PACKET *p = (PACKET *)mem_alloc(sizeof(PACKET)+bytes, 1);
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
if(net_addr_comp(&from, &dest) == 0)
|
2008-03-18 01:30:47 +00:00
|
|
|
p->send_to = src; // from the server
|
2007-07-13 13:40:04 +00:00
|
|
|
else
|
|
|
|
{
|
2008-03-18 01:30:47 +00:00
|
|
|
src = from; // from the client
|
2007-07-13 13:40:04 +00:00
|
|
|
p->send_to = dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
// queue packet
|
|
|
|
p->prev = last;
|
|
|
|
p->next = 0;
|
|
|
|
if(last)
|
|
|
|
last->next = p;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
first = p;
|
|
|
|
last = p;
|
|
|
|
}
|
|
|
|
last = p;
|
|
|
|
|
|
|
|
// set data in packet
|
|
|
|
p->timestamp = time_get();
|
|
|
|
p->data_size = bytes;
|
|
|
|
p->id = id++;
|
|
|
|
mem_copy(p->data, buffer, bytes);
|
2008-03-18 01:30:47 +00:00
|
|
|
|
|
|
|
if(id > 20 && bytes > 6 && data_trash)
|
|
|
|
{
|
|
|
|
p->data[6+(rand()%(bytes-6))] = rand()&255; // modify a byte
|
|
|
|
if((rand()%10) == 0)
|
|
|
|
{
|
|
|
|
p->data_size -= rand()%32;
|
|
|
|
if(p->data_size < 6)
|
|
|
|
p->data_size = 6;
|
|
|
|
}
|
|
|
|
}
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2008-11-08 14:04:16 +00:00
|
|
|
if(config_log)
|
2007-07-13 13:40:04 +00:00
|
|
|
dbg_msg("crapnet", "<< %08d %d.%d.%d.%d:%5d (%d)", p->id, from.ip[0], from.ip[1], from.ip[2], from.ip[3], from.port, p->data_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
if(first && (time_get()-first->timestamp) > current_latency)
|
|
|
|
{
|
2008-10-19 09:10:55 +00:00
|
|
|
PACKET *p = first;
|
2008-10-20 19:59:33 +00:00
|
|
|
char flags[] = " ";
|
|
|
|
|
2008-11-08 14:04:16 +00:00
|
|
|
if(config_reorder && (rand()%2) == 0 && first->next)
|
2007-07-13 13:40:04 +00:00
|
|
|
{
|
2008-10-20 19:59:33 +00:00
|
|
|
flags[0] = 'R';
|
|
|
|
p = first->next;
|
2007-07-13 13:40:04 +00:00
|
|
|
}
|
|
|
|
|
2008-10-20 19:59:33 +00:00
|
|
|
if(p->next)
|
|
|
|
p->next->prev = 0;
|
|
|
|
else
|
|
|
|
last = p->prev;
|
|
|
|
|
|
|
|
if(p->prev)
|
|
|
|
p->prev->next = p->next;
|
|
|
|
else
|
|
|
|
first = p->next;
|
|
|
|
|
|
|
|
PACKET *cur = first;
|
|
|
|
while(cur)
|
|
|
|
{
|
|
|
|
dbg_assert(cur != p, "p still in list");
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
// send and remove packet
|
2007-10-28 19:28:09 +00:00
|
|
|
//if((rand()%20) != 0) // heavy packetloss
|
2008-07-06 11:21:21 +00:00
|
|
|
net_udp_send(socket, &p->send_to, p->data, p->data_size);
|
2007-07-13 13:40:04 +00:00
|
|
|
|
|
|
|
// update lag
|
2007-09-23 18:27:04 +00:00
|
|
|
double flux = rand()/(double)RAND_MAX;
|
2008-10-20 19:59:33 +00:00
|
|
|
int ms_spike = config_pingspike;
|
|
|
|
int ms_flux = config_pingflux;
|
|
|
|
int ms_ping = config_ping;
|
2007-07-13 13:40:04 +00:00
|
|
|
current_latency = ((time_freq()*ms_ping)/1000) + (int64)(((time_freq()*ms_flux)/1000)*flux); // 50ms
|
|
|
|
|
2007-09-09 18:21:14 +00:00
|
|
|
if(ms_spike && (p->id%100) == 0)
|
2008-10-20 19:59:33 +00:00
|
|
|
{
|
2007-07-13 13:40:04 +00:00
|
|
|
current_latency += (time_freq()*ms_spike)/1000;
|
2008-10-20 19:59:33 +00:00
|
|
|
flags[1] = 'S';
|
|
|
|
}
|
|
|
|
|
2008-11-08 14:04:16 +00:00
|
|
|
if(config_log)
|
2008-10-20 19:59:33 +00:00
|
|
|
{
|
|
|
|
dbg_msg("crapnet", ">> %08d %d.%d.%d.%d:%5d (%d) %s", p->id,
|
|
|
|
p->send_to.ip[0], p->send_to.ip[1],
|
|
|
|
p->send_to.ip[2], p->send_to.ip[3],
|
|
|
|
p->send_to.port, p->data_size, flags);
|
|
|
|
}
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
|
|
|
|
mem_free(p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_sleep(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2008-07-06 11:21:21 +00:00
|
|
|
NETADDR a = {NETTYPE_IPV4, {127,0,0,1},8303};
|
2008-10-20 19:59:33 +00:00
|
|
|
dbg_logger_stdout();
|
2007-08-22 07:52:33 +00:00
|
|
|
run(8302, a);
|
2007-07-13 13:40:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|