Commit 09366a58 authored by Andreas Schmidt's avatar Andreas Schmidt
Browse files

Socket now have to be bound.

parent c92fcfed
Pipeline #79 passed with stage
......@@ -458,7 +458,7 @@ PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *payloadP
PrrtPacket *PrrtPacket_create_feedback_packet(uint8_t priority, uint8_t index, uint16_t sequenceNumber,
uint32_t roundTripTime, uint32_t packetLossRate, uint16_t gap,
uint16_t ngap, uint16_t burst, uint16_t nburst, uint32_t bandwidth,
uint32_t bufferFeedback)
uint32_t bufferFeedback, uint32_t receiverAddr)
{
PrrtPacket *packet = create_header(priority, sequenceNumber, PRRT_PACKET_FEEDBACK_HEADER_SIZE, PACKET_TYPE_FEEDBACK,
index);
......@@ -467,7 +467,7 @@ PrrtPacket *PrrtPacket_create_feedback_packet(uint8_t priority, uint8_t index, u
check_mem(payload);
packet->payload = payload;
payload->receiver_addr = inet_addr("10.0.0.1");
payload->receiver_addr = receiverAddr;
payload->group_round_trip_time = roundTripTime;
payload->forward_trip_timestamp = 0;
payload->packet_loss_rate = packetLossRate;
......
......@@ -70,7 +70,7 @@ PrrtPacket * PrrtPacket_create_data_packet(uint8_t priority, const void *payload
PrrtPacket *PrrtPacket_create_feedback_packet(uint8_t priority, uint8_t index, uint16_t sequenceNumber,
uint32_t roundTripTime, uint32_t packetLossRate, uint16_t gap,
uint16_t ngap, uint16_t burst, uint16_t nburst, uint32_t bandwidth,
uint32_t bufferFeedback);
uint32_t bufferFeedback, uint32_t receiverAddr);
PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *payloadPointer, uint32_t payloadLength,
uint16_t sequenceNumber, uint8_t index, uint16_t baseSequenceNumber, PrrtCodingParams codingParams);
......
......@@ -62,7 +62,7 @@ bool send_feedback(const PrrtSocket *sock_ptr, struct sockaddr_in remote)
hp = gethostbyname(remote_host);
memcpy((void *) &targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
PrrtPacket *feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 3, 50, 4, 6, 8, 9, 5, 1);
PrrtPacket *feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 3, 50, 4, 6, 8, 9, 5, 1, sock_ptr->address->sin_addr.s_addr);
uint32_t length = PrrtPacket_size(feedback_pkt_ptr);
void *buf = calloc(1, length);
check_mem(buf);
......
......@@ -17,11 +17,13 @@
#include "receiver.h"
#include "clock.h"
PrrtSocket* PrrtSocket_create(const uint16_t port, const uint8_t is_sender) {
PrrtSocket *PrrtSocket_create(const uint8_t is_sender)
{
PrrtSocket *sock_ptr = (PrrtSocket*) calloc(1, sizeof(PrrtSocket));
check_mem(sock_ptr);
sock_ptr->is_sender = is_sender;
sock_ptr->isBound = false;
sock_ptr->sequenceNumberSource = 1;
sock_ptr->sequenceNumberRedundancy = 1;
......@@ -38,37 +40,11 @@ PrrtSocket* PrrtSocket_create(const uint16_t port, const uint8_t is_sender) {
check(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0, "Setting type failed.");
check(pthread_mutex_init(&sock_ptr->closingMutex, &attr) == 0, "Mutex init failed.");
// Bind Data Socket
struct sockaddr_in address;
memset((char *) &address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(port);
check(bind(sock_ptr->dataSocketFd, (struct sockaddr *) &address, sizeof(address)) == EXIT_SUCCESS,
"Cannot bind data socket.");
if (is_sender) {
// Bind Feedback Socket
struct sockaddr_in address;
memset((char *) &address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
check(port <= 65534, "Port %d cannot be bound to.", port);
address.sin_port = htons((uint16_t) (port + 1));
check(bind(sock_ptr->feedbackSocketFd, (struct sockaddr *) &address, sizeof(address)) == EXIT_SUCCESS,
"Cannot bind feedback socket.");
check(pthread_mutex_init(&sock_ptr->outQueueFilledMutex, NULL) == 0, "Mutex init failed.");
check(pthread_cond_init(&sock_ptr->outQueueFilledCv, NULL) == 0, "Cond init failed.");
sock_ptr->outQueue = List_create();
check(pthread_create(&sock_ptr->receiveFeedbackThread, NULL, receive_feedback_loop, (void *) sock_ptr) ==
EXIT_SUCCESS, "Cannot create receive feedback thread.");
check(pthread_create(&sock_ptr->sendThread, NULL, send_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
"Cannot create send thread.");
sock_ptr->receivers = List_create();
} else {
sock_ptr->forwardPacketTable = calloc(1, sizeof(PrrtForwardPacketTable));
......@@ -78,9 +54,6 @@ PrrtSocket* PrrtSocket_create(const uint16_t port, const uint8_t is_sender) {
check(pthread_mutex_init(&sock_ptr->inQueueFilledMutex, NULL) == 0, "Mutex init failed.");
check(pthread_cond_init(&sock_ptr->inQueueFilledCv, NULL) == 0, "Cond init failed.");
sock_ptr->inQueue = List_create();
check(pthread_create(&sock_ptr->receiveDataThread, NULL, receive_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
"Cannot create data receiving thread.");
}
return sock_ptr;
......@@ -90,6 +63,43 @@ PrrtSocket* PrrtSocket_create(const uint16_t port, const uint8_t is_sender) {
return NULL;
}
bool PrrtSocket_bind(PrrtSocket *sock_ptr, const char* ipAddress, const uint16_t port) {
check(port <= 65534, "Port %d cannot be bound to.", port);
size_t size = sizeof(struct sockaddr_in);
struct sockaddr_in* address = calloc(1, size);
check_mem(address);
address->sin_family = AF_INET;
address->sin_addr.s_addr = inet_addr(ipAddress);
address->sin_port = htons((uint16_t) (port + 1));
sock_ptr->address = address;
check(bind(sock_ptr->feedbackSocketFd, (struct sockaddr *) address, size) == EXIT_SUCCESS,
"Cannot bind feedback socket.");
address->sin_port = htons((uint16_t) (port));
check(bind(sock_ptr->dataSocketFd, (struct sockaddr *) address, size) == EXIT_SUCCESS,
"Cannot bind data socket.");
if(sock_ptr->is_sender) {
check(pthread_create(&sock_ptr->receiveFeedbackThread, NULL, receive_feedback_loop, (void *) sock_ptr) ==
EXIT_SUCCESS, "Cannot create receive feedback thread.");
check(pthread_create(&sock_ptr->sendThread, NULL, send_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
"Cannot create send thread.");
} else {
check(pthread_create(&sock_ptr->receiveDataThread, NULL, receive_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
"Cannot create data receiving thread.");
}
return true;
error:
PrrtSocket_close(sock_ptr);
return false;
}
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port) {
PrrtReceiver *recv = PrrtReceiver_create(host, port);
List_push(sock_ptr->receivers, recv);
......@@ -245,6 +255,10 @@ int PrrtSocket_close(PrrtSocket *sock_ptr) {
check(pthread_mutex_destroy(&sock_ptr->closingMutex) == 0, "Mutex destroy failed.");
if(sock_ptr->address != NULL) {
free(sock_ptr->address);
}
close(sock_ptr->dataSocketFd);
close(sock_ptr->feedbackSocketFd);
debug("Socket closed.");
......@@ -281,6 +295,11 @@ PrrtPacket *PrrtSocket_recv_feedback(PrrtSocket *prrtSocket, const size_t length
check_mem(prrtPacket);
PrrtPacket_decode(bufin, (uint16_t) n, prrtPacket);
PrrtPacketFeedbackPayload* payload = prrtPacket->payload;
struct in_addr a;
a.s_addr = payload->receiver_addr;
printf("%s\n", inet_ntoa(a));
uint32_t forwardTripTimestamp = ((PrrtPacketFeedbackPayload*) prrtPacket->payload)->forward_trip_timestamp;
PrrtChannelStateInformation_update_rtt(&prrtSocket->csi, receiveTime - forwardTripTimestamp);
......
......@@ -14,6 +14,9 @@ typedef struct prrtSocket {
int feedbackSocketFd;
pthread_t receiveFeedbackThread;
struct sockaddr_in* address;
bool isBound;
pthread_t sendThread;
pthread_mutex_t outQueueFilledMutex;
pthread_cond_t outQueueFilledCv;
......@@ -51,7 +54,8 @@ typedef struct prrtSocket {
} PrrtSocket;
PrrtSocket* PrrtSocket_create(const uint16_t port, const uint8_t is_sender);
PrrtSocket *PrrtSocket_create(const uint8_t is_sender);
bool PrrtSocket_bind(PrrtSocket *sock_ptr, const char* ipAddress, const uint16_t port);
int PrrtSocket_interrupt(PrrtSocket *sock_ptr);
int PrrtSocket_close(PrrtSocket *sock_ptr);
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port);
......
......@@ -25,7 +25,8 @@ int main(int argc, char* const argv[]) {
printf("PRRT - RECEIVER\n");
sock = PrrtSocket_create(port, false);
sock = PrrtSocket_create(false);
PrrtSocket_bind(sock, "127.0.0.1", port);
check(sock != NULL, "Could not create socket.");
int i = 1;
......
......@@ -14,10 +14,10 @@ int main(int argc, char *const argv[]) {
uint16_t rounds = (uint16_t) atoi(argv[1]);
uint16_t local_port = 6000;
PrrtSocket* socket = NULL;
PrrtSocket* socket = PrrtSocket_create(true);
printf("PRRT - SENDER\n");
socket = PrrtSocket_create(local_port, true);
PrrtSocket_bind(socket, "127.0.1.0", local_port);
check(socket != NULL, "Socket creation failed.");
char *remote_host = "127.0.0.1";
......
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