Skip to content

Instantly share code, notes, and snippets.

@hgomersall
Last active November 23, 2018 13:33
Show Gist options
  • Save hgomersall/e5362764e69e2e1ff5ca3dc61e0f979d to your computer and use it in GitHub Desktop.
Save hgomersall/e5362764e69e2e1ff5ca3dc61e0f979d to your computer and use it in GitHub Desktop.
Simple networking
#ifndef CURIOUS_NETWORKING_H
#define CURIOUS_NETWORKING_H
static const size_t packet_len = 1395;
static const char packet[] = {
0x31, 0x2c, 0x20, 0x32, 0x2c, 0x20, 0x33, 0x2c, 0x20, 0x34, 0x2c, 0x20,
0x35, 0x2c, 0x20, 0x36, 0x2c, 0x20, 0x37, 0x2c, 0x20, 0x38, 0x2c, 0x20,
0x39, 0x2c, 0x20, 0x31, 0x30, 0x2c, 0x20, 0x31, 0x31, 0x2c, 0x20, 0x31,
0x32, 0x2c, 0x20, 0x31, 0x33, 0x2c, 0x20, 0x31, 0x34, 0x2c, 0x20, 0x31,
0x35, 0x2c, 0x20, 0x31, 0x36, 0x2c, 0x20, 0x31, 0x37, 0x2c, 0x20, 0x31,
0x38, 0x2c, 0x20, 0x31, 0x39, 0x2c, 0x20, 0x32, 0x30, 0x2c, 0x20, 0x32,
0x31, 0x2c, 0x20, 0x32, 0x32, 0x2c, 0x20, 0x32, 0x33, 0x2c, 0x20, 0x32,
0x34, 0x2c, 0x20, 0x32, 0x35, 0x2c, 0x20, 0x32, 0x36, 0x2c, 0x20, 0x32,
0x37, 0x2c, 0x20, 0x32, 0x38, 0x2c, 0x20, 0x32, 0x39, 0x2c, 0x20, 0x33,
0x30, 0x2c, 0x20, 0x33, 0x31, 0x2c, 0x20, 0x33, 0x32, 0x2c, 0x20, 0x33,
0x33, 0x2c, 0x20, 0x33, 0x34, 0x2c, 0x20, 0x33, 0x35, 0x2c, 0x20, 0x33,
0x36, 0x2c, 0x20, 0x33, 0x37, 0x2c, 0x20, 0x33, 0x38, 0x2c, 0x20, 0x33,
0x39, 0x2c, 0x20, 0x34, 0x30, 0x2c, 0x20, 0x34, 0x31, 0x2c, 0x20, 0x34,
0x32, 0x2c, 0x20, 0x34, 0x33, 0x2c, 0x20, 0x34, 0x34, 0x2c, 0x20, 0x34,
0x35, 0x2c, 0x20, 0x34, 0x36, 0x2c, 0x20, 0x34, 0x37, 0x2c, 0x20, 0x34,
0x38, 0x2c, 0x20, 0x34, 0x39, 0x2c, 0x20, 0x35, 0x30, 0x2c, 0x20, 0x35,
0x31, 0x2c, 0x20, 0x35, 0x32, 0x2c, 0x20, 0x35, 0x33, 0x2c, 0x20, 0x35,
0x34, 0x2c, 0x20, 0x35, 0x35, 0x2c, 0x20, 0x35, 0x36, 0x2c, 0x20, 0x35,
0x37, 0x2c, 0x20, 0x35, 0x38, 0x2c, 0x20, 0x35, 0x39, 0x2c, 0x20, 0x36,
0x30, 0x2c, 0x20, 0x36, 0x31, 0x2c, 0x20, 0x36, 0x32, 0x2c, 0x20, 0x36,
0x33, 0x2c, 0x20, 0x36, 0x34, 0x2c, 0x20, 0x36, 0x35, 0x2c, 0x20, 0x36,
0x36, 0x2c, 0x20, 0x36, 0x37, 0x2c, 0x20, 0x36, 0x38, 0x2c, 0x20, 0x36,
0x39, 0x2c, 0x20, 0x37, 0x30, 0x2c, 0x20, 0x37, 0x31, 0x2c, 0x20, 0x37,
0x32, 0x2c, 0x20, 0x37, 0x33, 0x2c, 0x20, 0x37, 0x34, 0x2c, 0x20, 0x37,
0x35, 0x2c, 0x20, 0x37, 0x36, 0x2c, 0x20, 0x37, 0x37, 0x2c, 0x20, 0x37,
0x38, 0x2c, 0x20, 0x37, 0x39, 0x2c, 0x20, 0x38, 0x30, 0x2c, 0x20, 0x38,
0x31, 0x2c, 0x20, 0x38, 0x32, 0x2c, 0x20, 0x38, 0x33, 0x2c, 0x20, 0x38,
0x34, 0x2c, 0x20, 0x38, 0x35, 0x2c, 0x20, 0x38, 0x36, 0x2c, 0x20, 0x38,
0x37, 0x2c, 0x20, 0x38, 0x38, 0x2c, 0x20, 0x38, 0x39, 0x2c, 0x20, 0x39,
0x30, 0x2c, 0x20, 0x39, 0x31, 0x2c, 0x20, 0x39, 0x32, 0x2c, 0x20, 0x39,
0x33, 0x2c, 0x20, 0x39, 0x34, 0x2c, 0x20, 0x39, 0x35, 0x2c, 0x20, 0x39,
0x36, 0x2c, 0x20, 0x39, 0x37, 0x2c, 0x20, 0x39, 0x38, 0x2c, 0x20, 0x39,
0x39, 0x2c, 0x20, 0x31, 0x30, 0x30, 0x2c, 0x20, 0x31, 0x30, 0x31, 0x2c,
0x20, 0x31, 0x30, 0x32, 0x2c, 0x20, 0x31, 0x30, 0x33, 0x2c, 0x20, 0x31,
0x30, 0x34, 0x2c, 0x20, 0x31, 0x30, 0x35, 0x2c, 0x20, 0x31, 0x30, 0x36,
0x2c, 0x20, 0x31, 0x30, 0x37, 0x2c, 0x20, 0x31, 0x30, 0x38, 0x2c, 0x20,
0x31, 0x30, 0x39, 0x2c, 0x20, 0x31, 0x31, 0x30, 0x2c, 0x20, 0x31, 0x31,
0x31, 0x2c, 0x20, 0x31, 0x31, 0x32, 0x2c, 0x20, 0x31, 0x31, 0x33, 0x2c,
0x20, 0x31, 0x31, 0x34, 0x2c, 0x20, 0x31, 0x31, 0x35, 0x2c, 0x20, 0x31,
0x31, 0x36, 0x2c, 0x20, 0x31, 0x31, 0x37, 0x2c, 0x20, 0x31, 0x31, 0x38,
0x2c, 0x20, 0x31, 0x31, 0x39, 0x2c, 0x20, 0x31, 0x32, 0x30, 0x2c, 0x20,
0x31, 0x32, 0x31, 0x2c, 0x20, 0x31, 0x32, 0x32, 0x2c, 0x20, 0x31, 0x32,
0x33, 0x2c, 0x20, 0x31, 0x32, 0x34, 0x2c, 0x20, 0x31, 0x32, 0x35, 0x2c,
0x20, 0x31, 0x32, 0x36, 0x2c, 0x20, 0x31, 0x32, 0x37, 0x2c, 0x20, 0x31,
0x32, 0x38, 0x2c, 0x20, 0x31, 0x32, 0x39, 0x2c, 0x20, 0x31, 0x33, 0x30,
0x2c, 0x20, 0x31, 0x33, 0x31, 0x2c, 0x20, 0x31, 0x33, 0x32, 0x2c, 0x20,
0x31, 0x33, 0x33, 0x2c, 0x20, 0x31, 0x33, 0x34, 0x2c, 0x20, 0x31, 0x33,
0x35, 0x2c, 0x20, 0x31, 0x33, 0x36, 0x2c, 0x20, 0x31, 0x33, 0x37, 0x2c,
0x20, 0x31, 0x33, 0x38, 0x2c, 0x20, 0x31, 0x33, 0x39, 0x2c, 0x20, 0x31,
0x34, 0x30, 0x2c, 0x20, 0x31, 0x34, 0x31, 0x2c, 0x20, 0x31, 0x34, 0x32,
0x2c, 0x20, 0x31, 0x34, 0x33, 0x2c, 0x20, 0x31, 0x34, 0x34, 0x2c, 0x20,
0x31, 0x34, 0x35, 0x2c, 0x20, 0x31, 0x34, 0x36, 0x2c, 0x20, 0x31, 0x34,
0x37, 0x2c, 0x20, 0x31, 0x34, 0x38, 0x2c, 0x20, 0x31, 0x34, 0x39, 0x2c,
0x20, 0x31, 0x35, 0x30, 0x2c, 0x20, 0x31, 0x35, 0x31, 0x2c, 0x20, 0x31,
0x35, 0x32, 0x2c, 0x20, 0x31, 0x35, 0x33, 0x2c, 0x20, 0x31, 0x35, 0x34,
0x2c, 0x20, 0x31, 0x35, 0x35, 0x2c, 0x20, 0x31, 0x35, 0x36, 0x2c, 0x20,
0x31, 0x35, 0x37, 0x2c, 0x20, 0x31, 0x35, 0x38, 0x2c, 0x20, 0x31, 0x35,
0x39, 0x2c, 0x20, 0x31, 0x36, 0x30, 0x2c, 0x20, 0x31, 0x36, 0x31, 0x2c,
0x20, 0x31, 0x36, 0x32, 0x2c, 0x20, 0x31, 0x36, 0x33, 0x2c, 0x20, 0x31,
0x36, 0x34, 0x2c, 0x20, 0x31, 0x36, 0x35, 0x2c, 0x20, 0x31, 0x36, 0x36,
0x2c, 0x20, 0x31, 0x36, 0x37, 0x2c, 0x20, 0x31, 0x36, 0x38, 0x2c, 0x20,
0x31, 0x36, 0x39, 0x2c, 0x20, 0x31, 0x37, 0x30, 0x2c, 0x20, 0x31, 0x37,
0x31, 0x2c, 0x20, 0x31, 0x37, 0x32, 0x2c, 0x20, 0x31, 0x37, 0x33, 0x2c,
0x20, 0x31, 0x37, 0x34, 0x2c, 0x20, 0x31, 0x37, 0x35, 0x2c, 0x20, 0x31,
0x37, 0x36, 0x2c, 0x20, 0x31, 0x37, 0x37, 0x2c, 0x20, 0x31, 0x37, 0x38,
0x2c, 0x20, 0x31, 0x37, 0x39, 0x2c, 0x20, 0x31, 0x38, 0x30, 0x2c, 0x20,
0x31, 0x38, 0x31, 0x2c, 0x20, 0x31, 0x38, 0x32, 0x2c, 0x20, 0x31, 0x38,
0x33, 0x2c, 0x20, 0x31, 0x38, 0x34, 0x2c, 0x20, 0x31, 0x38, 0x35, 0x2c,
0x20, 0x31, 0x38, 0x36, 0x2c, 0x20, 0x31, 0x38, 0x37, 0x2c, 0x20, 0x31,
0x38, 0x38, 0x2c, 0x20, 0x31, 0x38, 0x39, 0x2c, 0x20, 0x31, 0x39, 0x30,
0x2c, 0x20, 0x31, 0x39, 0x31, 0x2c, 0x20, 0x31, 0x39, 0x32, 0x2c, 0x20,
0x31, 0x39, 0x33, 0x2c, 0x20, 0x31, 0x39, 0x34, 0x2c, 0x20, 0x31, 0x39,
0x35, 0x2c, 0x20, 0x31, 0x39, 0x36, 0x2c, 0x20, 0x31, 0x39, 0x37, 0x2c,
0x20, 0x31, 0x39, 0x38, 0x2c, 0x20, 0x31, 0x39, 0x39, 0x2c, 0x20, 0x32,
0x30, 0x30, 0x2c, 0x20, 0x32, 0x30, 0x31, 0x2c, 0x20, 0x32, 0x30, 0x32,
0x2c, 0x20, 0x32, 0x30, 0x33, 0x2c, 0x20, 0x32, 0x30, 0x34, 0x2c, 0x20,
0x32, 0x30, 0x35, 0x2c, 0x20, 0x32, 0x30, 0x36, 0x2c, 0x20, 0x32, 0x30,
0x37, 0x2c, 0x20, 0x32, 0x30, 0x38, 0x2c, 0x20, 0x32, 0x30, 0x39, 0x2c,
0x20, 0x32, 0x31, 0x30, 0x2c, 0x20, 0x32, 0x31, 0x31, 0x2c, 0x20, 0x32,
0x31, 0x32, 0x2c, 0x20, 0x32, 0x31, 0x33, 0x2c, 0x20, 0x32, 0x31, 0x34,
0x2c, 0x20, 0x32, 0x31, 0x35, 0x2c, 0x20, 0x32, 0x31, 0x36, 0x2c, 0x20,
0x32, 0x31, 0x37, 0x2c, 0x20, 0x32, 0x31, 0x38, 0x2c, 0x20, 0x32, 0x31,
0x39, 0x2c, 0x20, 0x32, 0x32, 0x30, 0x2c, 0x20, 0x32, 0x32, 0x31, 0x2c,
0x20, 0x32, 0x32, 0x32, 0x2c, 0x20, 0x32, 0x32, 0x33, 0x2c, 0x20, 0x32,
0x32, 0x34, 0x2c, 0x20, 0x32, 0x32, 0x35, 0x2c, 0x20, 0x32, 0x32, 0x36,
0x2c, 0x20, 0x32, 0x32, 0x37, 0x2c, 0x20, 0x32, 0x32, 0x38, 0x2c, 0x20,
0x32, 0x32, 0x39, 0x2c, 0x20, 0x32, 0x33, 0x30, 0x2c, 0x20, 0x32, 0x33,
0x31, 0x2c, 0x20, 0x32, 0x33, 0x32, 0x2c, 0x20, 0x32, 0x33, 0x33, 0x2c,
0x20, 0x32, 0x33, 0x34, 0x2c, 0x20, 0x32, 0x33, 0x35, 0x2c, 0x20, 0x32,
0x33, 0x36, 0x2c, 0x20, 0x32, 0x33, 0x37, 0x2c, 0x20, 0x32, 0x33, 0x38,
0x2c, 0x20, 0x32, 0x33, 0x39, 0x2c, 0x20, 0x32, 0x34, 0x30, 0x2c, 0x20,
0x32, 0x34, 0x31, 0x2c, 0x20, 0x32, 0x34, 0x32, 0x2c, 0x20, 0x32, 0x34,
0x33, 0x2c, 0x20, 0x32, 0x34, 0x34, 0x2c, 0x20, 0x32, 0x34, 0x35, 0x2c,
0x20, 0x32, 0x34, 0x36, 0x2c, 0x20, 0x32, 0x34, 0x37, 0x2c, 0x20, 0x32,
0x34, 0x38, 0x2c, 0x20, 0x32, 0x34, 0x39, 0x2c, 0x20, 0x32, 0x35, 0x30,
0x2c, 0x20, 0x32, 0x35, 0x31, 0x2c, 0x20, 0x32, 0x35, 0x32, 0x2c, 0x20,
0x32, 0x35, 0x33, 0x2c, 0x20, 0x32, 0x35, 0x34, 0x2c, 0x20, 0x32, 0x35,
0x35, 0x2c, 0x20, 0x32, 0x35, 0x36, 0x2c, 0x20, 0x32, 0x35, 0x37, 0x2c,
0x20, 0x32, 0x35, 0x38, 0x2c, 0x20, 0x32, 0x35, 0x39, 0x2c, 0x20, 0x32,
0x36, 0x30, 0x2c, 0x20, 0x32, 0x36, 0x31, 0x2c, 0x20, 0x32, 0x36, 0x32,
0x2c, 0x20, 0x32, 0x36, 0x33, 0x2c, 0x20, 0x32, 0x36, 0x34, 0x2c, 0x20,
0x32, 0x36, 0x35, 0x2c, 0x20, 0x32, 0x36, 0x36, 0x2c, 0x20, 0x32, 0x36,
0x37, 0x2c, 0x20, 0x32, 0x36, 0x38, 0x2c, 0x20, 0x32, 0x36, 0x39, 0x2c,
0x20, 0x32, 0x37, 0x30, 0x2c, 0x20, 0x32, 0x37, 0x31, 0x2c, 0x20, 0x32,
0x37, 0x32, 0x2c, 0x20, 0x32, 0x37, 0x33, 0x2c, 0x20, 0x32, 0x37, 0x34,
0x2c, 0x20, 0x32, 0x37, 0x35, 0x2c, 0x20, 0x32, 0x37, 0x36, 0x2c, 0x20,
0x32, 0x37, 0x37, 0x2c, 0x20, 0x32, 0x37, 0x38, 0x2c, 0x20, 0x32, 0x37,
0x39, 0x2c, 0x20, 0x32, 0x38, 0x30, 0x2c, 0x20, 0x32, 0x38, 0x31, 0x2c,
0x20, 0x32, 0x38, 0x32, 0x2c, 0x20, 0x32, 0x38, 0x33, 0x2c, 0x20, 0x32,
0x38, 0x34, 0x2c, 0x20, 0x32, 0x38, 0x35, 0x2c, 0x20, 0x32, 0x38, 0x36,
0x2c, 0x20, 0x32, 0x38, 0x37, 0x2c, 0x20, 0x32, 0x38, 0x38, 0x2c, 0x20,
0x32, 0x38, 0x39, 0x2c, 0x20, 0x32, 0x39, 0x30, 0x2c, 0x20, 0x32, 0x39,
0x31, 0x2c, 0x20, 0x32, 0x39, 0x32, 0x2c, 0x20, 0x32, 0x39, 0x33, 0x2c,
0x20, 0x32, 0x39, 0x34, 0x2c, 0x20, 0x32, 0x39, 0x35, 0x2c, 0x20, 0x32,
0x39, 0x36, 0x2c, 0x20, 0x32, 0x39, 0x37, 0x2c, 0x20, 0x32, 0x39, 0x38,
0x2c, 0x20, 0x32, 0x39, 0x39, 0x2c, 0x20, 0x33, 0x30, 0x30, 0x2c, 0x20,
0x33, 0x30, 0x31};
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <time.h>
#include "packet.h"
static const unsigned int send_buffer_size = 2097152;
static const char dest_addr[] = {0xc0, 0xa8, 0x08, 0x6a};
static const char dest_port[] = {0x13, 0x56};
static void write_packets(size_t n_packets, int loops, const char *id)
{
printf("%s: Writing simple data packets...\n", id);
int fd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
memcpy(&addr.sin_port, dest_port, sizeof(addr.sin_port));
memcpy(&addr.sin_addr, dest_addr, sizeof(addr.sin_addr));
int ret;
for(int n=0; n<loops; n++){
struct timespec time_before, time_after;
clock_gettime(CLOCK_MONOTONIC, &time_before);
for(int i=0; i<n_packets; i++){
ret = sendto(fd, packet, packet_len, 0,
(const struct sockaddr *)&addr, sizeof(struct sockaddr_in));
}
clock_gettime(CLOCK_MONOTONIC, &time_after);
uint64_t before_nanoseconds = (
((uint64_t)time_before.tv_sec) * 1000000000 +
(uint64_t)time_before.tv_nsec);
uint64_t after_nanoseconds = (
((uint64_t)time_after.tv_sec) * 1000000000 +
(uint64_t)time_after.tv_nsec);
uint64_t runtime_nanoseconds = after_nanoseconds - before_nanoseconds;
double runtime_seconds = ((double)runtime_nanoseconds)/1e9;
double mbps = (8 * n_packets * packet_len)/(1e6 * runtime_seconds);
printf("%s: Runtime to send %d packets: %f seconds (%f Mbits per second)\n",
id, n_packets, runtime_seconds, mbps);
}
close(fd);
}
int main(int argc, char **argv)
{
write_packets(131072, 3, argv[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment