about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile17
-rw-r--r--src/main.c32
-rwxr-xr-xsrc/oscdrpbin0 -> 28368 bytes
-rw-r--r--src/protocol.c97
-rw-r--r--src/protocol.h35
-rw-r--r--src/protocol.obin0 -> 15304 bytes
-rw-r--r--src/utils.c110
-rw-r--r--src/utils.h32
-rw-r--r--src/utils.obin0 -> 19288 bytes
9 files changed, 323 insertions, 0 deletions
diff --git a/src/Makefile b/src/Makefile
new file mode 100644
index 0000000..5f5aa6a
--- /dev/null
+++ b/src/Makefile
@@ -0,0 +1,17 @@
+CC=clang
+INC_DIRS="."
+CFLAGS=-std=c99 -g -Wall -Werror -O2 -I$(INC_DIRS)
+LDFLAGS=-lpthread
+OBJS=protocol.o \
+     utils.o 		
+
+all: oscdrp
+
+%.o: %.c
+	$(CC) $(CFLAGS) -c $^
+
+oscdrp: main.c $(OBJS)
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
+
+clean:
+	rm $(OBJS) oscdrp
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..520cff3
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,32 @@
+#include <protocol.h>
+#include <utils.h>
+
+int main(int argc, char** argv)
+{
+    socket_info_t socket_info;
+    strncpy(socket_info.address, "127.0.0.1", sizeof(socket_info.address));
+    socket_info.port = 12345;
+
+    uint64_t packet_id = 1;
+    uint64_t data_size = 20000;
+    uint8_t* data = generate_random_data(data_size);
+
+    Packet_t* packet = generate_packet(packet_id, data, data_size);
+    free(data);
+
+    pthread_t wait_for_packet_thread;
+    pthread_create(&wait_for_packet_thread, NULL, (void*)wait_for_packet, &socket_info);
+
+    sleep(2); //wait until the wait_for_packet_thread is running
+    send_packet(&socket_info, (void*)packet, get_packet_size_without_data() + packet->data_size);
+    free(packet);
+
+    Packet_t* return_packet = NULL;
+    pthread_join(wait_for_packet_thread, (void**)&return_packet);
+
+    handle_packet(return_packet);
+
+    free(return_packet);
+
+    return 0;
+}
diff --git a/src/oscdrp b/src/oscdrp
new file mode 100755
index 0000000..57422ab
--- /dev/null
+++ b/src/oscdrp
Binary files differdiff --git a/src/protocol.c b/src/protocol.c
new file mode 100644
index 0000000..87ae81f
--- /dev/null
+++ b/src/protocol.c
@@ -0,0 +1,97 @@
+#include <protocol.h>
+
+Packet_t* generate_packet(uint64_t type, const uint8_t* data, uint64_t data_size)
+{
+    Packet_t* packet = calloc(1, get_packet_size_without_data() + data_size);
+
+    srand(time(NULL));
+
+    packet->magic = PACKET_MAGIC;
+    packet->id = rand();
+    packet->type = type;
+    packet->data_size = data_size;
+    packet->data = NULL;
+    memcpy(&packet->data, data, data_size);
+
+    packet->checksum = get_packet_checksum(packet);
+
+    printf("GENERATED PACKET:\n");
+    print_packet(packet);
+
+    return packet;
+}
+
+int handle_packet(const Packet_t* packet)
+{
+    int ret = is_packet_valid(packet);
+    if(ret < 0)
+    {
+        printf("Packet is invalid!\n");
+        return ret;
+    }
+
+    printf("PACKET VALIDATED!\n");
+    printf("RECIEVED PACKET:\n");
+
+    switch(packet->id)
+    {
+        default:
+            print_packet(packet);
+            break;
+    }
+
+    return 0;
+}
+
+int is_packet_valid(const Packet_t* packet)
+{
+    if(packet->magic != PACKET_MAGIC)
+    {
+        perror("Invalid packet magic!\n");
+        return -EINVAL;
+    }
+
+    uint64_t checksum = get_packet_checksum(packet);
+    if(packet->checksum != checksum)
+    {
+        printf("packet checksum: %llu\n", checksum);
+        printf("expected checksum: %llu\n", packet->checksum);
+        perror("Invalid packet checksum!\n");
+
+        printf("PACKET AFTER RECIEVED:\n");
+        print_packet(packet);
+
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+uint64_t get_packet_checksum(const Packet_t* packet)
+{
+    //FIXME: This is not a proper checksum implementation.
+
+    uint64_t sum = 0;
+    uint64_t i = sizeof(packet->checksum); //start calculating checksum from outside checksum var in struct
+    for(; i < get_packet_size_without_data() + packet->data_size; ++i)
+        sum += ((uint8_t*)packet)[i];
+
+    return sum;
+}
+
+void print_packet(const Packet_t* packet)
+{
+    printf("Packet:\n");
+    printf("\tmagic: %llu\n", packet->magic);
+    printf("\tchecksum: %llu\n", packet->checksum);
+    printf("\tid: %llu\n", packet->id);
+    printf("\ttype: %llu\n", packet->type);
+    printf("\tdata size: %llu\n", packet->data_size);
+    printf("\tdata string: %s\n\tdata raw: ", (char*)&packet->data);
+    show_raw((uint8_t*)&packet->data, packet->data_size);
+}
+
+size_t get_packet_size_without_data()
+{
+    return sizeof(Packet_t) - sizeof(uint8_t*);
+}
diff --git a/src/protocol.h b/src/protocol.h
new file mode 100644
index 0000000..f506d7f
--- /dev/null
+++ b/src/protocol.h
@@ -0,0 +1,35 @@
+#ifndef _PROTOCOL_H_
+#define _PROTOCOL_H_
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <pthread.h>
+#include <assert.h>
+#include <errno.h>
+
+#define PACKET_MAGIC 0xC0C0AF00
+
+typedef struct __attribute__((packed))
+{
+    uint64_t checksum;
+    uint64_t magic;
+    uint64_t id;
+    uint64_t type;
+    uint64_t data_size;
+    uint8_t* data;
+} Packet_t;
+
+#include <utils.h>
+
+Packet_t* generate_packet(uint64_t type, const uint8_t* data, uint64_t data_size);
+int handle_packet(const Packet_t* packet);
+int is_packet_valid(const Packet_t* packet);
+uint64_t get_packet_checksum(const Packet_t* packet);
+size_t get_packet_size_without_data();
+void print_packet(const Packet_t* packet);
+
+#endif
diff --git a/src/protocol.o b/src/protocol.o
new file mode 100644
index 0000000..2f32565
--- /dev/null
+++ b/src/protocol.o
Binary files differdiff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..65efd1f
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,110 @@
+#include <utils.h>
+
+int send_packet(const socket_info_t* socket_info, const void* packet, size_t packet_size)
+{
+    int fd = socket(AF_INET, SOCK_STREAM, 0);
+    assert(fd >= 0);
+
+    struct sockaddr_in server_addr = {0};
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_addr.s_addr = inet_addr(socket_info->address);
+    server_addr.sin_port = htons(socket_info->port);
+    assert(server_addr.sin_addr.s_addr != INADDR_NONE);
+
+    int ret = connect(fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
+    assert(ret == 0);
+
+    ssize_t total_bytes_sent = 0;
+    while(total_bytes_sent < packet_size)
+    {
+        ssize_t bytes_sent = send(fd, packet, packet_size, 0);
+        assert(bytes_sent >= 0);
+        total_bytes_sent += bytes_sent;
+    }
+
+    assert(total_bytes_sent == packet_size);
+
+    printf("SENT %lu bytes\n", total_bytes_sent);
+
+    close(fd);
+
+    return 0;
+}
+
+Packet_t* wait_for_packet(const socket_info_t* socket_info)
+{
+    printf("waiting for packet...\n");
+
+    int fd = socket(AF_INET, SOCK_STREAM, 0);
+    assert(fd >= 0);
+
+    struct sockaddr_in server_addr = {0};
+    server_addr.sin_family = AF_INET;
+    server_addr.sin_addr.s_addr = inet_addr(socket_info->address);
+    server_addr.sin_port = htons(socket_info->port);
+    assert(server_addr.sin_addr.s_addr != INADDR_NONE);
+
+    int ret = bind(fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
+    assert(ret >= 0);
+
+    ret = listen(fd, 0);
+    assert(ret >= 0);
+
+    struct sockaddr_in client = {0};
+    socklen_t sockaddr_len = sizeof(struct sockaddr_in);
+    int connection = accept(fd, (struct sockaddr*)&client, &sockaddr_len);
+    assert(connection >= 0);
+
+    Packet_t* packet = malloc(get_packet_size_without_data());
+    ssize_t total_bytes_read = read(connection, packet, get_packet_size_without_data());
+    assert(total_bytes_read >= 0);
+
+    packet = realloc(packet, get_packet_size_without_data() + packet->data_size);
+    assert(packet != NULL);
+
+    ssize_t data_bytes_read = 0;
+    ssize_t current_bytes_read = 0;
+    while((current_bytes_read = recv(connection, &packet->data + data_bytes_read, packet->data_size - data_bytes_read, MSG_WAITALL)) > 0)
+    {
+        printf("READ: %zd bytes into %p (%lu)\n", current_bytes_read, &packet->data + data_bytes_read, data_bytes_read);
+        data_bytes_read += current_bytes_read;
+    }
+
+    total_bytes_read += data_bytes_read;
+
+    //mayb doesnt work because of some memory alignment (thats why we have to read all in 1 with MSG_WAITALL)
+    /*
+    if(current_bytes_read == -1)
+    {
+       perror("Error");
+       printf("TRIED TO READ: %zd bytes into %p (%lu)\n", current_bytes_read, &packet->data + data_bytes_read - current_bytes_read, data_bytes_read);
+    }
+    */
+
+    assert(current_bytes_read != -1);
+    assert(total_bytes_read == get_packet_size_without_data() + packet->data_size);
+
+    close(connection);
+    close(fd);
+
+    return packet;
+}
+
+uint8_t* generate_random_data(size_t data_size)
+{
+    uint8_t* data = malloc(data_size);
+
+    int fd = open("/dev/urandom", O_RDONLY);
+    read(fd, data, data_size);
+    close(fd);
+
+    return data;
+}
+
+void show_raw(const uint8_t* data, size_t data_size)
+{
+    for(size_t i = 0; i < data_size; ++i)
+        printf("%x", data[i]);
+
+    putchar('\n');
+}
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..98f0387
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,32 @@
+#ifndef _UTILS_H_
+#define _UTILS_H_
+
+#include <protocol.h>
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <pthread.h>
+#include <assert.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+
+typedef struct
+{
+    char address[20];
+    int port;
+} socket_info_t;
+
+int send_packet(const socket_info_t* socket_info, const void* packet, size_t packet_size);
+Packet_t* wait_for_packet(const socket_info_t* socket_info);
+
+uint8_t* generate_random_data(size_t data_size);
+void show_raw(const uint8_t* data, size_t data_size);
+
+#endif
diff --git a/src/utils.o b/src/utils.o
new file mode 100644
index 0000000..7893509
--- /dev/null
+++ b/src/utils.o
Binary files differ