Commit f6420c8a authored by Andreas Schmidt's avatar Andreas Schmidt

Fix SIGSEGV and reformat code.

parent d3a1dda6
Pipeline #2094 failed with stages
in 24 seconds
......@@ -240,7 +240,6 @@ bool PrrtSocket_bind(PrrtSocket *s, const char *ipAddress, const uint16_t port)
EXIT_SUCCESS,
"Cannot create data receiving thread.");
}
s->isBound = true;
return true;
......@@ -386,7 +385,7 @@ int32_t PrrtSocket_receive_ordered_timedwait(PrrtSocket *s, void *buf_ptr, prrtT
int PrrtSocket_interrupt(PrrtSocket *s) {
atomic_store_explicit(&s->closing, true, memory_order_release);
if (s->packetDeliveryStore) {
if (s->packetDeliveryStore != NULL) {
PrrtPacketDeliveryStore_interrupt(s->packetDeliveryStore);
}
......
......@@ -17,70 +17,70 @@ typedef int socket_t;
static inline socket_t _open_sender(struct arguments *args)
{
struct addrinfo hints = {
.ai_socktype = SOCK_STREAM,
.ai_family = AF_UNSPEC,
.ai_flags = AI_ADDRCONFIG,
};
char port[16];
snprintf(port, sizeof(port), "%d", (int) args->port);
struct addrinfo *head;
int fail = getaddrinfo(args->target, port, &hints, &head);
assert(!fail);
int sock;
struct addrinfo *iter;
for (iter = head; iter != NULL; iter = iter->ai_next) {
sock = socket(iter->ai_family, iter->ai_socktype, iter->ai_protocol);
if (0 ==connect(sock, iter->ai_addr, iter->ai_addrlen))
break;
close(sock);
}
assert(iter && "failed to connect via TCP");
freeaddrinfo(head);
return sock;
struct addrinfo hints = {
.ai_socktype = SOCK_STREAM,
.ai_family = AF_UNSPEC,
.ai_flags = AI_ADDRCONFIG,
};
char port[16];
snprintf(port, sizeof(port), "%d", (int) args->port);
struct addrinfo *head;
int fail = getaddrinfo(args->target, port, &hints, &head);
assert(!fail);
int sock;
struct addrinfo *iter;
for (iter = head; iter != NULL; iter = iter->ai_next) {
sock = socket(iter->ai_family, iter->ai_socktype, iter->ai_protocol);
if (0 ==connect(sock, iter->ai_addr, iter->ai_addrlen))
break;
close(sock);
}
assert(iter && "failed to connect via TCP");
freeaddrinfo(head);
return sock;
}
static inline socket_t _open_receiver(struct arguments *args)
{
int ssock = socket(AF_INET6, SOCK_STREAM, 0);
assert(ssock >= 0);
int ssock = socket(AF_INET6, SOCK_STREAM, 0);
assert(ssock >= 0);
int yes = 1;
setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
int yes = 1;
setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
struct sockaddr_in6 address = {
.sin6_family = AF_INET6,
.sin6_port = htons(args->port),
.sin6_addr = in6addr_any,
};
assert(0 == bind(ssock, (const struct sockaddr *) &address, sizeof(address)));
struct sockaddr_in6 address = {
.sin6_family = AF_INET6,
.sin6_port = htons(args->port),
.sin6_addr = in6addr_any,
};
assert(0 == bind(ssock, (const struct sockaddr *) &address, sizeof(address)));
assert(0 == listen(ssock, SOMAXCONN));
assert(0 == listen(ssock, SOMAXCONN));
int sock = accept(ssock, NULL, NULL);
assert(sock >= 0);
int sock = accept(ssock, NULL, NULL);
assert(sock >= 0);
close(ssock);
close(ssock);
return sock;
return sock;
}
static inline void _send(socket_t conn, const char *buf, size_t size)
{
write(conn, buf, size);
write(conn, buf, size);
}
static inline ssize_t _recv(socket_t conn, char *buf, size_t size)
{
return read(conn, buf, size);
return read(conn, buf, size);
}
static inline void _close(socket_t conn)
{
close(conn);
close(conn);
}
#else
......@@ -88,57 +88,51 @@ static inline void _close(socket_t conn)
typedef PrrtSocket *socket_t;
static inline socket_t _open_sender(struct arguments *args)
{
static inline socket_t _open_sender(struct arguments *args) {
PrrtSocket *s = PrrtSocket_create(true, 10 * 1000 * 1000);
assert(s != NULL && "Socket create failed.");
if(args->thread_pinning) {
if (args->thread_pinning) {
PrrtSocket_enable_thread_pinning(s);
}
assert(PrrtSocket_bind(s, "0.0.0.0", args->local_port) && "bind failed");
PrrtSocket_connect(s, args->target, args->port);
return s;
return s;
}
static inline socket_t _open_receiver(struct arguments *args)
{
socket_t s = PrrtSocket_create(false, HALF_TIMESTAMP-1);
static inline socket_t _open_receiver(struct arguments *args) {
socket_t s = PrrtSocket_create(false, HALF_TIMESTAMP - 1);
assert(s != NULL && "Could not create socket.");
if(args->thread_pinning) {
if (args->thread_pinning) {
PrrtSocket_enable_thread_pinning(s);
}
assert(PrrtSocket_bind(s, "0.0.0.0", args->port) && "bind failed");
return s;
return s;
}
static inline void _send(socket_t conn, const char *buf, size_t size)
{
PrrtSocket_send(conn, (unsigned char *) buf, sizeof(buf));
static inline void _send(socket_t conn, const char *buf, size_t size) {
PrrtSocket_send(conn, (unsigned char *) buf, size);
}
static inline ssize_t _recv(socket_t conn, char *buf, size_t size)
{
return PrrtSocket_receive_asap_wait(conn, buf);
static inline ssize_t _recv(socket_t conn, char *buf, size_t size) {
return PrrtSocket_receive_asap_wait(conn, buf);
}
static inline void _close(socket_t conn)
{
static inline void _close(socket_t conn) {
PrrtSocket_close(conn);
free(conn);
}
#endif
static inline socket_t _open(bool sender, struct arguments *args)
{
if (sender)
return _open_sender(args);
else
return _open_receiver(args);
static inline socket_t _open(bool sender, struct arguments *args) {
if (sender)
return _open_sender(args);
else
return _open_receiver(args);
}
......@@ -5,38 +5,39 @@
#include "util/dbg.h"
#include "proto/socket.h"
PrrtSocket *s = NULL;
static volatile int keepRunning = true;
static char doc[] = "PRRT Receiver";
static struct argp_option options[] = {
{"port", 'p', "PORT", 0, "Target Port" },
{"rounds", 'r', "ROUNDS", 0, "Rounds" },
{"output", 'o', "FILE", 0, "Output to FILE instead of standard output" },
{"threadpinning", 'T', 0, 0, "Enable thread pinning"},
{ 0 }
{"port", 'p', "PORT", 0, "Target Port"},
{"rounds", 'r', "ROUNDS", 0, "Rounds"},
{"output", 'o', "FILE", 0, "Output to FILE instead of standard output"},
{"threadpinning", 'T', 0, 0, "Enable thread pinning"},
{0}
};
struct arguments
{
struct arguments {
uint16_t port;
uint16_t local_port;
uint16_t local_port;
uint16_t rounds;
char* outfile;
char *target;
char *outfile;
char *target;
bool thread_pinning;
};
#include "time-protocol.h"
socket_t s = NULL;
static volatile int keepRunning = true;
static char args_doc[] = "";
static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
struct arguments *arguments = state->input;
char* pEnd;
switch (key)
{
char *pEnd;
switch (key) {
case 'p':
arguments->port = (uint16_t) strtol(arg, &pEnd, 10);
break;
......@@ -61,23 +62,22 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state)
void intHandler(int dummy) {
keepRunning = false;
PrrtSocket_interrupt(s);
if (s != NULL) {
PrrtSocket_interrupt(s);
}
}
static long long timedelta(struct timespec *t1, struct timespec *t2)
{
long long delta = t1->tv_sec - t2->tv_sec;
delta *= 1000000000;
delta += t1->tv_nsec - t2->tv_nsec;
return delta;
static long long timedelta(struct timespec *t1, struct timespec *t2) {
long long delta = t1->tv_sec - t2->tv_sec;
delta *= 1000000000;
delta += t1->tv_nsec - t2->tv_nsec;
return delta;
}
#include "time-protocol.h"
int main(int argc, char **argv) {
signal(SIGINT, intHandler);
struct argp argp = { options, parse_opt, args_doc, doc };
struct argp argp = {options, parse_opt, args_doc, doc};
struct arguments arguments = {
.port = 5000,
......@@ -85,7 +85,7 @@ int main(int argc, char **argv) {
.outfile = "-",
.thread_pinning = false,
};
argp_parse (&argp, argc, argv, 0, 0, &arguments);
argp_parse(&argp, argc, argv, 0, 0, &arguments);
bool file_output = false;
FILE *out_desc = stdout;
......@@ -96,20 +96,21 @@ int main(int argc, char **argv) {
}
uint32_t rounds = arguments.rounds;
#ifdef XLAP
#ifdef XLAP
if (rounds >= TS_ROWS) {
printf("Receiver: Too many rounds for use with XLAP (max: %d).\n", TS_ROWS-1);
return -1;
}
#endif
#endif
long long int *rtts = calloc((size_t) rounds, sizeof(long long int));
long long int *rtts2 = calloc((size_t) rounds, sizeof(long long int));
assert(rtts && rtts2);
socket_t s = _open(false, &arguments);
long long int *rtts = calloc((size_t) rounds, sizeof(long long int));
long long int *rtts2 = calloc((size_t) rounds, sizeof(long long int));
assert(rtts && rtts2);
s = _open(false, &arguments);
uint32_t i = 0;
while (i < rounds && keepRunning) {
usleep_nano(1000);
char buffer[MAX_PAYLOAD_LENGTH + 1];
debug(DEBUG_RECEIVER, "About to receive.");
int n = _recv(s, buffer, sizeof(buffer));
......@@ -117,14 +118,14 @@ int main(int argc, char **argv) {
continue;
}
int round = 0;
struct timespec now;
struct timespec * old;
struct timespec * channel;
clock_gettime(CLOCK_REALTIME, &now);
struct timespec now;
struct timespec *old;
struct timespec *channel;
clock_gettime(CLOCK_REALTIME, &now);
sscanf(buffer, "%10d", &round);
old = (struct timespec *) (buffer + 20);
channel = (struct timespec *) (buffer + n - sizeof(struct timespec));
sscanf(buffer, "%10d", &round);
old = (struct timespec *) (buffer + 20);
channel = (struct timespec *) (buffer + n - sizeof(struct timespec));
rtts[round] = timedelta(channel, old);
rtts2[round] = timedelta(&now, old);
......@@ -140,7 +141,7 @@ int main(int argc, char **argv) {
fclose(out_desc);
}
_close(s);
_close(s);
free(rtts);
free(rtts2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment