diff options
-rw-r--r-- | OSCDRP.cflags | 1 | ||||
-rw-r--r-- | OSCDRP.config | 2 | ||||
-rw-r--r-- | OSCDRP.creator | 1 | ||||
-rw-r--r-- | OSCDRP.creator.user | 233 | ||||
-rw-r--r-- | OSCDRP.cxxflags | 1 | ||||
-rw-r--r-- | OSCDRP.files | 6 | ||||
-rw-r--r-- | OSCDRP.includes | 1 | ||||
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | src/Makefile | 17 | ||||
-rw-r--r-- | src/main.c | 32 | ||||
-rwxr-xr-x | src/oscdrp | bin | 0 -> 28368 bytes | |||
-rw-r--r-- | src/protocol.c | 97 | ||||
-rw-r--r-- | src/protocol.h | 35 | ||||
-rw-r--r-- | src/protocol.o | bin | 0 -> 15304 bytes | |||
-rw-r--r-- | src/utils.c | 110 | ||||
-rw-r--r-- | src/utils.h | 32 | ||||
-rw-r--r-- | src/utils.o | bin | 0 -> 19288 bytes |
17 files changed, 571 insertions, 0 deletions
diff --git a/OSCDRP.cflags b/OSCDRP.cflags new file mode 100644 index 0000000..68d5165 --- /dev/null +++ b/OSCDRP.cflags @@ -0,0 +1 @@ +-std=c17 \ No newline at end of file diff --git a/OSCDRP.config b/OSCDRP.config new file mode 100644 index 0000000..e0284f4 --- /dev/null +++ b/OSCDRP.config @@ -0,0 +1,2 @@ +// Add predefined macros for your project here. For example: +// #define THE_ANSWER 42 diff --git a/OSCDRP.creator b/OSCDRP.creator new file mode 100644 index 0000000..e94cbbd --- /dev/null +++ b/OSCDRP.creator @@ -0,0 +1 @@ +[General] diff --git a/OSCDRP.creator.user b/OSCDRP.creator.user new file mode 100644 index 0000000..0373e01 --- /dev/null +++ b/OSCDRP.creator.user @@ -0,0 +1,233 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE QtCreatorProject> +<!-- Written by QtCreator 4.15.0, 2021-08-20T19:40:54. --> +<qtcreator> + <data> + <variable>EnvironmentId</variable> + <value type="QByteArray">{bec1b8fb-35c6-486c-8528-a4c07ca21028}</value> + </data> + <data> + <variable>ProjectExplorer.Project.ActiveTarget</variable> + <value type="int">0</value> + </data> + <data> + <variable>ProjectExplorer.Project.EditorSettings</variable> + <valuemap type="QVariantMap"> + <value type="bool" key="EditorConfiguration.AutoIndent">true</value> + <value type="bool" key="EditorConfiguration.AutoSpacesForTabs">false</value> + <value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value> + <valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0"> + <value type="QString" key="language">Cpp</value> + <valuemap type="QVariantMap" key="value"> + <value type="QByteArray" key="CurrentPreferences">CppGlobal</value> + </valuemap> + </valuemap> + <valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.1"> + <value type="QString" key="language">QmlJS</value> + <valuemap type="QVariantMap" key="value"> + <value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value> + </valuemap> + </valuemap> + <value type="int" key="EditorConfiguration.CodeStyle.Count">2</value> + <value type="QByteArray" key="EditorConfiguration.Codec">UTF-8</value> + <value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value> + <value type="int" key="EditorConfiguration.IndentSize">4</value> + <value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value> + <value type="int" key="EditorConfiguration.MarginColumn">80</value> + <value type="bool" key="EditorConfiguration.MouseHiding">true</value> + <value type="bool" key="EditorConfiguration.MouseNavigation">true</value> + <value type="int" key="EditorConfiguration.PaddingMode">1</value> + <value type="bool" key="EditorConfiguration.PreferSingleLineComments">false</value> + <value type="bool" key="EditorConfiguration.ScrollWheelZooming">true</value> + <value type="bool" key="EditorConfiguration.ShowMargin">false</value> + <value type="int" key="EditorConfiguration.SmartBackspaceBehavior">0</value> + <value type="bool" key="EditorConfiguration.SmartSelectionChanging">true</value> + <value type="bool" key="EditorConfiguration.SpacesForTabs">true</value> + <value type="int" key="EditorConfiguration.TabKeyBehavior">0</value> + <value type="int" key="EditorConfiguration.TabSize">8</value> + <value type="bool" key="EditorConfiguration.UseGlobal">true</value> + <value type="bool" key="EditorConfiguration.UseIndenter">false</value> + <value type="int" key="EditorConfiguration.Utf8BomBehavior">1</value> + <value type="bool" key="EditorConfiguration.addFinalNewLine">true</value> + <value type="bool" key="EditorConfiguration.cleanIndentation">true</value> + <value type="bool" key="EditorConfiguration.cleanWhitespace">true</value> + <value type="QString" key="EditorConfiguration.ignoreFileTypes">*.md, *.MD, Makefile</value> + <value type="bool" key="EditorConfiguration.inEntireDocument">false</value> + <value type="bool" key="EditorConfiguration.skipTrailingWhitespace">true</value> + </valuemap> + </data> + <data> + <variable>ProjectExplorer.Project.PluginSettings</variable> + <valuemap type="QVariantMap"> + <valuemap type="QVariantMap" key="AutoTest.ActiveFrameworks"> + <value type="bool" key="AutoTest.Framework.Boost">true</value> + <value type="bool" key="AutoTest.Framework.CTest">false</value> + <value type="bool" key="AutoTest.Framework.Catch">true</value> + <value type="bool" key="AutoTest.Framework.GTest">true</value> + <value type="bool" key="AutoTest.Framework.QtQuickTest">true</value> + <value type="bool" key="AutoTest.Framework.QtTest">true</value> + </valuemap> + <valuemap type="QVariantMap" key="AutoTest.CheckStates"/> + <value type="int" key="AutoTest.RunAfterBuild">0</value> + <value type="bool" key="AutoTest.UseGlobal">true</value> + <valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey"/> + <value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value> + <value type="QString" key="ClangCodeModel.WarningConfigId">Builtin.BuildSystem</value> + <valuemap type="QVariantMap" key="ClangTools"> + <value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value> + <value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value> + <value type="QString" key="ClangTools.DiagnosticConfig">Builtin.DefaultTidyAndClazy</value> + <value type="int" key="ClangTools.ParallelJobs">2</value> + <valuelist type="QVariantList" key="ClangTools.SelectedDirs"/> + <valuelist type="QVariantList" key="ClangTools.SelectedFiles"/> + <valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/> + <value type="bool" key="ClangTools.UseGlobalSettings">true</value> + </valuemap> + </valuemap> + </data> + <data> + <variable>ProjectExplorer.Project.Target.0</variable> + <valuemap type="QVariantMap"> + <value type="QString" key="DeviceType">Desktop</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{4fb376e9-1b57-4065-9d56-a5b4d7c8740a}</value> + <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value> + <value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value> + <value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value> + <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0"> + <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/home/baitinq/OSCDRP</value> + <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> + <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0"> + <valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets"> + <value type="QString">all</value> + </valuelist> + <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value> + </valuemap> + <value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value> + </valuemap> + <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1"> + <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0"> + <valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets"> + <value type="QString">clean</value> + </valuelist> + <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value> + </valuemap> + <value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value> + </valuemap> + <value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value> + <value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value> + <valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/> + <valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Default</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericBuildConfiguration</value> + </valuemap> + <value type="int" key="ProjectExplorer.Target.BuildConfigurationCount">1</value> + <valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0"> + <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> + <value type="int" key="ProjectExplorer.BuildStepList.StepsCount">0</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Deploy</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Deploy</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value> + </valuemap> + <value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value> + <valuemap type="QVariantMap" key="ProjectExplorer.DeployConfiguration.CustomData"/> + <value type="bool" key="ProjectExplorer.DeployConfiguration.CustomDataEnabled">false</value> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value> + </valuemap> + <value type="int" key="ProjectExplorer.Target.DeployConfigurationCount">1</value> + <valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0"> + <value type="QString" key="Analyzer.Perf.CallgraphMode">dwarf</value> + <valuelist type="QVariantList" key="Analyzer.Perf.Events"> + <value type="QString">cpu-cycles</value> + </valuelist> + <valuelist type="QVariantList" key="Analyzer.Perf.ExtraArguments"/> + <value type="int" key="Analyzer.Perf.Frequency">250</value> + <valuelist type="QVariantList" key="Analyzer.Perf.RecordArguments"> + <value type="QString">-e</value> + <value type="QString">cpu-cycles</value> + <value type="QString">--call-graph</value> + <value type="QString">dwarf,4096</value> + <value type="QString">-F</value> + <value type="QString">250</value> + </valuelist> + <value type="QString" key="Analyzer.Perf.SampleMode">-F</value> + <value type="bool" key="Analyzer.Perf.Settings.UseGlobalSettings">true</value> + <value type="int" key="Analyzer.Perf.StackSize">4096</value> + <value type="bool" key="Analyzer.QmlProfiler.AggregateTraces">false</value> + <value type="bool" key="Analyzer.QmlProfiler.FlushEnabled">false</value> + <value type="uint" key="Analyzer.QmlProfiler.FlushInterval">1000</value> + <value type="QString" key="Analyzer.QmlProfiler.LastTraceFile"></value> + <value type="bool" key="Analyzer.QmlProfiler.Settings.UseGlobalSettings">true</value> + <valuelist type="QVariantList" key="Analyzer.Valgrind.AddedSuppressionFiles"/> + <value type="QString" key="Analyzer.Valgrind.Callgrind.Arguments"></value> + <value type="bool" key="Analyzer.Valgrind.Callgrind.CollectBusEvents">false</value> + <value type="bool" key="Analyzer.Valgrind.Callgrind.CollectSystime">false</value> + <value type="bool" key="Analyzer.Valgrind.Callgrind.EnableBranchSim">false</value> + <value type="bool" key="Analyzer.Valgrind.Callgrind.EnableCacheSim">false</value> + <value type="bool" key="Analyzer.Valgrind.Callgrind.EnableEventToolTips">true</value> + <value type="double" key="Analyzer.Valgrind.Callgrind.MinimumCostRatio">0.01</value> + <value type="double" key="Analyzer.Valgrind.Callgrind.VisualisationMinimumCostRatio">10</value> + <value type="bool" key="Analyzer.Valgrind.FilterExternalIssues">true</value> + <value type="QString" key="Analyzer.Valgrind.KCachegrindExecutable">kcachegrind</value> + <value type="int" key="Analyzer.Valgrind.LeakCheckOnFinish">1</value> + <value type="QString" key="Analyzer.Valgrind.Memcheck.Arguments"></value> + <value type="int" key="Analyzer.Valgrind.NumCallers">25</value> + <valuelist type="QVariantList" key="Analyzer.Valgrind.RemovedSuppressionFiles"/> + <value type="int" key="Analyzer.Valgrind.SelfModifyingCodeDetection">1</value> + <value type="bool" key="Analyzer.Valgrind.Settings.UseGlobalSettings">true</value> + <value type="bool" key="Analyzer.Valgrind.ShowReachable">false</value> + <value type="bool" key="Analyzer.Valgrind.TrackOrigins">true</value> + <value type="QString" key="Analyzer.Valgrind.ValgrindArguments"></value> + <value type="QString" key="Analyzer.Valgrind.ValgrindExecutable">valgrind</value> + <valuelist type="QVariantList" key="Analyzer.Valgrind.VisibleErrorKinds"> + <value type="int">0</value> + <value type="int">1</value> + <value type="int">2</value> + <value type="int">3</value> + <value type="int">4</value> + <value type="int">5</value> + <value type="int">6</value> + <value type="int">7</value> + <value type="int">8</value> + <value type="int">9</value> + <value type="int">10</value> + <value type="int">11</value> + <value type="int">12</value> + <value type="int">13</value> + <value type="int">14</value> + </valuelist> + <valuelist type="QVariantList" key="CustomOutputParsers"/> + <value type="int" key="PE.EnvironmentAspect.Base">2</value> + <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/> + <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value> + <value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value> + <value type="bool" key="RunConfiguration.UseCppDebugger">false</value> + <value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value> + <value type="bool" key="RunConfiguration.UseQmlDebugger">false</value> + <value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value> + </valuemap> + <value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value> + </valuemap> + </data> + <data> + <variable>ProjectExplorer.Project.TargetCount</variable> + <value type="int">1</value> + </data> + <data> + <variable>ProjectExplorer.Project.Updater.FileVersion</variable> + <value type="int">22</value> + </data> + <data> + <variable>Version</variable> + <value type="int">22</value> + </data> +</qtcreator> diff --git a/OSCDRP.cxxflags b/OSCDRP.cxxflags new file mode 100644 index 0000000..6435dfc --- /dev/null +++ b/OSCDRP.cxxflags @@ -0,0 +1 @@ +-std=c++17 \ No newline at end of file diff --git a/OSCDRP.files b/OSCDRP.files new file mode 100644 index 0000000..28de468 --- /dev/null +++ b/OSCDRP.files @@ -0,0 +1,6 @@ +README.md +src/main.c +src/protocol.c +src/protocol.h +src/utils.c +src/utils.h diff --git a/OSCDRP.includes b/OSCDRP.includes new file mode 100644 index 0000000..85de9cf --- /dev/null +++ b/OSCDRP.includes @@ -0,0 +1 @@ +src diff --git a/README.md b/README.md new file mode 100644 index 0000000..18dabe7 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +#Overly Simple Code Distribution and Running Protocol + +One machine distributes code across different machines which run the code 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 |