diff options
author | Aaditya Dhruv <[email protected]> | 2025-05-25 15:12:59 -0500 |
---|---|---|
committer | Aaditya Dhruv <[email protected]> | 2025-05-25 15:12:59 -0500 |
commit | 420ce14859e41d1e8b616ebf1c472763eabe93be (patch) | |
tree | d569161b9f5f81166d58f264bd88ca592af3eadc |
init
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | CMakeLists.txt | 8 | ||||
-rw-r--r-- | LICENSE | 22 | ||||
-rw-r--r-- | include/junk/network.h | 4 | ||||
-rw-r--r-- | include/junk/test.h | 2 | ||||
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/network.c | 55 | ||||
-rw-r--r-- | src/test.c | 6 |
8 files changed, 101 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e3d37f5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.cache +build +tags diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a460044 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.16...4.0) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +project(libjunk VERSION 1.0 + DESCRIPTION "General purpose library" + LANGUAGES C) +add_library(junk SHARED) +add_subdirectory(src) +target_include_directories(junk PUBLIC include) @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2025 Aaditya Dhruv + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/include/junk/network.h b/include/junk/network.h new file mode 100644 index 0000000..494142f --- /dev/null +++ b/include/junk/network.h @@ -0,0 +1,4 @@ +#pragma once + + +int tcp_ipv4_send_packet(char* ip, char* port, char* data); diff --git a/include/junk/test.h b/include/junk/test.h new file mode 100644 index 0000000..2ee6d89 --- /dev/null +++ b/include/junk/test.h @@ -0,0 +1,2 @@ +#pragma once +void test_print(char* value); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..21ed023 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1 @@ +target_sources(junk PUBLIC test.c network.c) diff --git a/src/network.c b/src/network.c new file mode 100644 index 0000000..64d217b --- /dev/null +++ b/src/network.c @@ -0,0 +1,55 @@ +#include "junk/network.h" +#include <unistd.h> +#include <errno.h> +#include <netinet/in.h> +#include <stdio.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <arpa/inet.h> +#include <netdb.h> + +#define TAG "network" + +int tcp_ipv4_send_packet(char* ip, char* port, char* data) { + + int sock; + struct addrinfo hints, *p, *res; + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + + if (getaddrinfo(ip, port, &hints, &res) != 0) { + fprintf(stderr, "%s: getaddrinfo (%d): %s\n", TAG, errno, gai_strerror(errno)); + return errno; + } + + if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { + fprintf(stderr, "%s: socket (%d): %s\n", TAG, errno, strerror(errno)); + return errno; + } + + if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) { + fprintf(stderr, "%s: connect (%d): %s\n", TAG, errno, strerror(errno)); + return errno; + } + + int len = strlen(data); + int bytes_sent = 0; + int total_bytes_sent = 0; + + while (1) { + if ((bytes_sent = send(sock, data, len, 0)) == -1) { + fprintf(stderr, "%s: send (%d): %s\n", TAG, errno, strerror(errno)); + return errno; + } + total_bytes_sent += bytes_sent; + if (bytes_sent == len) { + break; + } + } + close(sock); + freeaddrinfo(res); + return 0; +} diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..d8a0654 --- /dev/null +++ b/src/test.c @@ -0,0 +1,6 @@ +#include <stdio.h> +#include "junk/test.h" + +void test_print(char* value) { + printf("Hello %s from libjunk\n", value); +} |