Last active
May 6, 2016 08:57
-
-
Save jmmlmendes/aaea56a8b7044487595ef7a6782fc018 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <time.h> | |
#include <errno.h> | |
#include <stdio.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <arpa/inet.h> | |
#define YIELD | |
//#define SLEEP | |
//#define SLEEP_US 1000 | |
#define PACKETS_TO_SEND 10000ULL | |
static int mtu = 1500; | |
static int port = 8080; | |
char *payload; | |
char *address = "192.168.122.1"; | |
static inline long long tv_to_us(struct timeval *tv1, struct timeval *tv2) | |
{ | |
return (long long)(tv2->tv_sec - tv1->tv_sec) * 1000000LL + | |
tv2->tv_usec - tv1->tv_usec; | |
} | |
void udp_flood(int fd, struct sockaddr *dst, int pktlen) | |
{ | |
struct timeval start, end; | |
long long total_us = 0; | |
unsigned long long pkt_sent = 0; | |
gettimeofday(&start, NULL); | |
while (pkt_sent < PACKETS_TO_SEND) { | |
if (sendto(fd, payload, mtu, MSG_NOSIGNAL, dst, pktlen) >= 0) | |
pkt_sent++; | |
else | |
printf("errno %d - %s\n", errno, strerror(errno)); | |
#ifdef YIELD | |
sched_yield(); | |
#endif | |
#ifdef SLEEP | |
usleep(SLEEP_US); | |
#endif | |
} | |
gettimeofday(&end, NULL); | |
total_us = tv_to_us(&start, &end); | |
printf("%llu packets sent in %lld us\n", pkt_sent, total_us); | |
} | |
int main(int argc, char **argv) | |
{ | |
int fd; | |
struct sockaddr_storage *ss; | |
payload = malloc(mtu); | |
ss = malloc (sizeof(struct sockaddr_storage)); | |
if (!payload || !ss) { | |
printf("Can't allocate memory\n"); | |
exit(1); | |
} | |
memset(payload, 0xF, mtu); | |
ss->ss_family = AF_INET; | |
((struct sockaddr_in *)ss)->sin_port = htons(port); | |
if (!inet_pton(ss->ss_family, address, &((struct sockaddr_in *)ss)->sin_addr)) { | |
printf("Invalid IP address\n"); | |
exit(1); | |
} | |
if ((fd = socket((*ss).ss_family, SOCK_DGRAM, 0)) == -1) { | |
printf("error creating socket\n"); | |
exit(1); | |
} | |
udp_flood(fd, (struct sockaddr *)ss, sizeof(struct sockaddr_in)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment