This commit is contained in:
tezc 2020-12-28 11:03:24 +03:00
parent 50990f326a
commit c5bf33be59
11 changed files with 337 additions and 48 deletions

View File

@ -26,6 +26,7 @@ add_subdirectory(mutex)
add_subdirectory(option)
add_subdirectory(queue)
add_subdirectory(perf)
add_subdirectory(rc4)
add_subdirectory(signal)
add_subdirectory(socket)
add_subdirectory(string)

93
rc4/CMakeLists.txt Normal file
View File

@ -0,0 +1,93 @@
cmake_minimum_required(VERSION 3.5.1)
project(sc_rc4 C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
add_executable(sc_rc4 rc4_example.c sc_rc4.h sc_rc4.c)
if (NOT CMAKE_C_COMPILER_ID MATCHES "MSVC")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -pedantic -Werror -D_GNU_SOURCE")
endif ()
# --------------------------------------------------------------------------- #
# --------------------- Test Configuration Start ---------------------------- #
# --------------------------------------------------------------------------- #
include(CTest)
include(CheckCCompilerFlag)
enable_testing()
add_executable(${PROJECT_NAME}_test rc4_test.c sc_rc4.c)
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_SIZE_MAX=4000ul)
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_HAVE_WRAP)
target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-builtin)
endif ()
endif ()
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-omit-frame-pointer)
if (SANITIZER)
target_compile_options(${PROJECT_NAME}_test PRIVATE -fsanitize=${SANITIZER})
target_link_options(${PROJECT_NAME}_test PRIVATE -fsanitize=${SANITIZER})
endif ()
endif ()
add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test)
SET(MEMORYCHECK_COMMAND_OPTIONS
"-q --log-fd=2 --trace-children=yes --track-origins=yes \
--leak-check=full --show-leak-kinds=all \
--error-exitcode=255")
add_custom_target(valgrind_${PROJECT_NAME} ${CMAKE_COMMAND}
-E env CTEST_OUTPUT_ON_FAILURE=1
${CMAKE_CTEST_COMMAND} -C $<CONFIG>
--overwrite MemoryCheckCommandOptions=${MEMORYCHECK_COMMAND_OPTIONS}
--verbose -T memcheck WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_custom_target(check_${PROJECT_NAME} ${CMAKE_COMMAND}
-E env CTEST_OUTPUT_ON_FAILURE=1
${CMAKE_CTEST_COMMAND} -C $<CONFIG> --verbose
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
# ----------------------- - Code Coverage Start ----------------------------- #
if (${CMAKE_BUILD_TYPE} MATCHES "Coverage")
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(${PROJECT_NAME}_test PRIVATE --coverage)
target_link_libraries(${PROJECT_NAME}_test gcov)
else()
message(FATAL_ERROR "Only GCC is supported for coverage")
endif()
endif ()
add_custom_target(coverage_${PROJECT_NAME})
add_custom_command(
TARGET coverage_${PROJECT_NAME}
COMMAND lcov --capture --directory ..
--output-file coverage.info --rc lcov_branch_coverage=1
COMMAND lcov --remove coverage.info '/usr/*' '*example*' '*test*'
--output-file coverage.info --rc lcov_branch_coverage=1
COMMAND lcov --list coverage.info --rc lcov_branch_coverage=1
)
add_dependencies(coverage_${PROJECT_NAME} check_${PROJECT_NAME})
# -------------------------- Code Coverage End ------------------------------ #
# ----------------------- Test Configuration End ---------------------------- #

3
rc4/README.md Normal file
View File

@ -0,0 +1,3 @@
# RC4 Random generator
https://en.wikipedia.org/wiki/RC4

5
rc4/rc4_example.c Normal file
View File

@ -0,0 +1,5 @@
int main(int argc, char *argv[])
{
return 0;
}

43
rc4/rc4_test.c Normal file
View File

@ -0,0 +1,43 @@
#include "sc_rc4.h"
#include <assert.h>
#include <memory.h>
void test()
{
unsigned char tmp[256] = {0, 1, 2, 3, 4, 5, 6, 6, 6, 6, 1, 5, 3, 5, 5, 6};
unsigned char tmp2[256] = {0, 1, 2, 3, 4, 3, 6, 6, 6, 6, 1, 2, 3, 5, 5, 6};
unsigned char out1[256];
unsigned char out2[256];
struct sc_rc4 rc4_1;
struct sc_rc4 rc4_2;
sc_rc4_init(&rc4_1, tmp);
sc_rc4_init(&rc4_2, tmp);
sc_rc4_random(&rc4_1, out1, sizeof(out1));
sc_rc4_random(&rc4_2, out2, sizeof(out2));
assert(memcmp(out1, out2, sizeof(out1)) == 0);
sc_rc4_init(&rc4_1, tmp);
sc_rc4_init(&rc4_2, tmp2);
sc_rc4_random(&rc4_1, out1, sizeof(out1));
sc_rc4_random(&rc4_2, out2, sizeof(out2));
assert(memcmp(out1, out2, sizeof(out1)) != 0);
sc_rc4_random(&rc4_1, out1, 0);
sc_rc4_random(&rc4_1, NULL, 0);
}
int main(int argc, char *argv[])
{
test();
return 0;
}

66
rc4/sc_rc4.c Normal file
View File

@ -0,0 +1,66 @@
/*
* MIT License
*
* Copyright (c) 2020 Ozan Tezcan
*
* 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.
*/
#include "sc_rc4.h"
#include <memory.h>
#include <stdint.h>
void sc_rc4_init(struct sc_rc4 *rc4, const unsigned char rand[256])
{
unsigned char t;
rc4->j = 0;
rc4->i = 0;
memcpy(rc4->s, rand, 256);
for (int i = 0; i < 256; i++) {
rc4->j += rc4->s[i] + rand[i];
t = rc4->s[rc4->j];
rc4->s[rc4->j] = rc4->s[i];
rc4->s[i] = t;
}
}
void sc_rc4_random(struct sc_rc4 *rc4, void *buf, int size)
{
unsigned char t;
unsigned char *p = buf;
if (size <= 0 || buf == NULL) {
return;
}
do {
rc4->i++;
t = rc4->s[rc4->i];
rc4->j += t;
rc4->s[rc4->i] = rc4->s[rc4->j];
rc4->s[rc4->j] = t;
t += rc4->s[rc4->i];
*(p++) = rc4->s[t];
} while (--size);
}

48
rc4/sc_rc4.h Normal file
View File

@ -0,0 +1,48 @@
/*
* MIT License
*
* Copyright (c) 2020 Ozan Tezcan
*
* 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.
*/
#ifndef SC_RC4_H
#define SC_RC4_H
struct sc_rc4
{
unsigned char i, j;
unsigned char s[256];
};
/**
*
* @param rc4 rc4
* @param rand rand source, possible from /dev/urandom
*/
void sc_rc4_init(struct sc_rc4 *rc4, const unsigned char rand[256]);
/**
* @param rc4 rc4
* @param buf out buf
* @param size buf size
*/
void sc_rc4_random(struct sc_rc4 *rc4, void *buf, int size);
#endif

View File

@ -65,7 +65,7 @@ static void sc_sock_errstr(struct sc_sock *sock, int gai_err)
}
}
static int sc_sock_set_blocking(struct sc_sock *sock, bool blocking)
int sc_sock_set_blocking(struct sc_sock *sock, bool blocking)
{
int mode = blocking ? 0 : 1;
int rc = ioctlsocket(sock->fdt.fd, FIONBIO, &mode);
@ -102,7 +102,7 @@ static void sc_sock_errstr(struct sc_sock *sock, int gai_err)
strncpy(sock->err, str, sizeof(sock->err) - 1);
}
static int sc_sock_set_blocking(struct sc_sock *sock, bool blocking)
int sc_sock_set_blocking(struct sc_sock *sock, bool blocking)
{
int flags = fcntl(sock->fdt.fd, F_GETFL, 0);
if (flags == -1) {
@ -152,6 +152,40 @@ int sc_sock_term(struct sc_sock *sock)
return rc;
}
int sc_sock_set_rcvtimeo(struct sc_sock *sock, int ms)
{
int rc;
struct timeval tv = {
.tv_usec = ms % 1000,
.tv_sec = ms / 1000,
};
rc = setsockopt(sock->fdt.fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
if (rc != 0) {
sc_sock_errstr(sock, 0);
}
return rc == 0 ? SC_SOCK_OK : SC_SOCK_ERROR;
}
int sc_sock_set_sndtimeo(struct sc_sock *sock, int ms)
{
int rc;
struct timeval tv = {
.tv_usec = ms % 1000,
.tv_sec = ms / 1000,
};
rc = setsockopt(sock->fdt.fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
if (rc != 0) {
sc_sock_errstr(sock, 0);
}
return rc == 0 ? SC_SOCK_OK : SC_SOCK_ERROR;
}
static int sc_sock_bind_unix(struct sc_sock *sock, const char *host)
{
int rc;
@ -282,11 +316,10 @@ int sc_sock_finish_connect(struct sc_sock *sock)
rc = getsockopt(sock->fdt.fd, SOL_SOCKET, SO_ERROR, (void *) &result, &len);
if (rc != 0 || result != 0) {
sc_sock_errstr(sock, 0);
sc_sock_close(sock);
rc = -1;
return SC_SOCK_ERROR;
}
return rc;
return SC_SOCK_OK;
}
static int sc_sock_connect_unix(struct sc_sock *sock, const char *addr)
@ -320,7 +353,7 @@ int sc_sock_connect(struct sc_sock *sock, const char *dest_addr,
const int bf = SC_SOCK_BUF_SIZE;
const int sz = sizeof(bf);
int rc = 0, rv = 0;
int rc = 0, rv = SC_SOCK_OK;
struct addrinfo inf = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM};
struct addrinfo *servinfo = NULL, *bindinfo = NULL, *p, *s;
@ -421,7 +454,7 @@ error_unix:
if (rc != 0) {
if (!sock->blocking && (sc_sock_err() == SC_EINPROGRESS ||
sc_sock_err() == SC_EAGAIN)) {
rv = 0;
rv = SC_SOCK_WANT_WRITE;
goto end;
}
@ -447,7 +480,7 @@ end:
return rv;
}
int sc_sock_send(struct sc_sock *sock, char *buf, int len)
int sc_sock_send(struct sc_sock *sock, char *buf, int len, int flags)
{
int n;
@ -458,24 +491,25 @@ int sc_sock_send(struct sc_sock *sock, char *buf, int len)
}
retry:
n = send(sock->fdt.fd, buf, len, 0);
n = send(sock->fdt.fd, buf, len, flags);
if (n == SC_ERR) {
int err = sc_sock_err();
if (err == SC_EINTR) {
goto retry;
}
if (!sock->blocking && err == SC_EAGAIN) {
if (err == SC_EAGAIN) {
return SC_SOCK_WANT_WRITE;
}
sc_sock_errstr(sock, 0);
n = SC_SOCK_ERROR;
}
return n;
}
int sc_sock_recv(struct sc_sock *sock, char *buf, int len)
int sc_sock_recv(struct sc_sock *sock, char *buf, int len, int flags)
{
int n;
@ -486,20 +520,21 @@ int sc_sock_recv(struct sc_sock *sock, char *buf, int len)
}
retry:
n = recv(sock->fdt.fd, buf, len, 0);
n = recv(sock->fdt.fd, buf, len, flags);
if (n == 0) {
return -1;
return SC_SOCK_ERROR;
} else if (n == SC_ERR) {
int err = sc_sock_err();
if (err == SC_EINTR) {
goto retry;
}
if (!sock->blocking && err == SC_EAGAIN) {
return 0;
if (err == SC_EAGAIN) {
return SC_SOCK_WANT_READ;
}
sc_sock_errstr(sock, 0);
n = SC_SOCK_ERROR;
}
return n;
@ -516,7 +551,7 @@ int sc_sock_accept(struct sc_sock *sock, struct sc_sock *in)
fd = accept(sock->fdt.fd, NULL, NULL);
if (fd == SC_INVALID) {
sc_sock_errstr(sock, 0);
return -1;
return SC_SOCK_ERROR;
}
in->fdt.fd = fd;
@ -545,7 +580,7 @@ int sc_sock_accept(struct sc_sock *sock, struct sc_sock *in)
goto error;
}
return 0;
return SC_SOCK_OK;
error:
sc_sock_errstr(sock, 0);
@ -629,7 +664,7 @@ static const char *sc_sock_print_storage(struct sc_sock *sock,
return buf;
}
static const char *sc_sock_local_str(struct sc_sock *sock, char *buf, int len)
const char *sc_sock_local_str(struct sc_sock *sock, char *buf, int len)
{
int rc;
struct sockaddr_storage st;
@ -645,7 +680,7 @@ static const char *sc_sock_local_str(struct sc_sock *sock, char *buf, int len)
return sc_sock_print_storage(sock, &st, buf, len);
}
static const char *sc_sock_remote_str(struct sc_sock *sock, char *buf, int len)
const char *sc_sock_remote_str(struct sc_sock *sock, char *buf, int len)
{
int rc;
struct sockaddr_storage st;
@ -861,12 +896,6 @@ retry:
goto retry;
}
if (n > 0 && n != len) {
len -= n;
b += n;
goto retry;
}
return n;
}
@ -944,7 +973,7 @@ int sc_sock_poll_add(struct sc_sock_poll *poll, struct sc_sock_fd *fdt,
{
int rc;
int op = EPOLL_CTL_MOD;
int mask = fdt->op | events;
enum sc_sock_ev mask = fdt->op | events;
struct epoll_event ep_ev = {.data.ptr = data,
.events = EPOLLERR | EPOLLHUP | EPOLLRDHUP};
@ -1051,10 +1080,6 @@ int sc_sock_poll_wait(struct sc_sock_poll *poll, int timeout)
n = epoll_wait(poll->fds, &poll->events[0], poll->cap, timeout);
} while (n < 0 && errno == EINTR);
if (n > 2) {
printf("epollwait : %d\n", n);
}
if (n == -1) {
sc_sock_on_error("epoll_wait : %s ", strerror(errno));
}

View File

@ -92,12 +92,17 @@ int sc_sock_connect(struct sc_sock* sock, const char* dest_addr,
const char* dest_port, const char* source_addr,
const char* source_port);
int sc_sock_set_blocking(struct sc_sock *sock, bool blocking);
int sc_sock_set_rcvtimeo(struct sc_sock* sock, int ms);
int sc_sock_set_sndtimeo(struct sc_sock* sock, int ms);
int sc_sock_finish_connect(struct sc_sock* sock);
int sc_sock_send(struct sc_sock* sock, char* buf, int len);
int sc_sock_recv(struct sc_sock* sock, char* buf, int len);
int sc_sock_send(struct sc_sock* sock, char* buf, int len, int flags);
int sc_sock_recv(struct sc_sock* sock, char* buf, int len, int flags);
const char* sc_sock_error(struct sc_sock* sock);
const char *sc_sock_local_str(struct sc_sock *sock, char *buf, int len);
const char *sc_sock_remote_str(struct sc_sock *sock, char *buf, int len);
void sc_sock_print(struct sc_sock* sock, char* buf, int len);

View File

@ -160,7 +160,7 @@ void* server_ip4(void* arg)
assert(strcmp(tmp, "Local(127.0.0.1:8004), Remote() ") == 0);
assert(sc_sock_accept(&sock, &accepted) == 0);
assert(sc_sock_recv(&accepted, buf, 5) == 5);
assert(sc_sock_recv(&accepted, buf, 5, 0) == 5);
assert(strcmp("test", buf) == 0);
assert(sc_sock_term(&sock) == 0);
@ -188,7 +188,7 @@ void* client_ip4(void* arg)
assert(rc == 0);
sc_sock_print(&sock, tmp, sizeof(tmp));
assert(sc_sock_send(&sock, "test", 5) == 5);
assert(sc_sock_send(&sock, "test", 5, 0) == 5);
assert(sc_sock_term(&sock) == 0);
return NULL;
@ -220,7 +220,7 @@ void* server_ip6(void* arg)
sc_sock_print(&sock, tmp, sizeof(tmp));
assert(strcmp(tmp, "Local(::1:8006), Remote() ") == 0);
assert(sc_sock_accept(&sock, &accepted) == 0);
assert(sc_sock_recv(&accepted, buf, 5) == 5);
assert(sc_sock_recv(&accepted, buf, 5, 0) == 5);
assert(strcmp("test", buf) == 0);
assert(sc_sock_term(&sock) == 0);
@ -245,7 +245,7 @@ void* client_ip6(void* arg)
sleep(1);
}
assert(rc == 0);
assert(sc_sock_send(&sock, "test", 5) == 5);
assert(sc_sock_send(&sock, "test", 5, 0) == 5);
assert(sc_sock_term(&sock) == 0);
@ -283,7 +283,7 @@ void* server_unix(void* arg)
}
assert(rc == 0);
assert(sc_sock_recv(&accepted, buf, 5) == 5);
assert(sc_sock_recv(&accepted, buf, 5, 0) == 5);
assert(strcmp("test", buf) == 0);
assert(sc_sock_term(&sock) == 0);
@ -312,7 +312,7 @@ void* client_unix(void* arg)
}
assert(rc == 0);
assert(sc_sock_send(&sock, "test", 5) == 5);
assert(sc_sock_send(&sock, "test", 5, 0) == 5);
assert(sc_sock_term(&sock) == 0);
return NULL;
@ -346,8 +346,8 @@ void test1()
assert(sc_sock_connect(&sock, "3127.0.0.1", "2131", "127.90.1.1", "50") !=
0);
assert(sc_sock_finish_connect(&sock) != 0);
assert(sc_sock_send(&sock, "test", 5) == -1);
assert(sc_sock_recv(&sock, tmp, sizeof(tmp)) == -1);
assert(sc_sock_send(&sock, "test", 5, 0) == -1);
assert(sc_sock_recv(&sock, tmp, sizeof(tmp), 0) == -1);
assert(sc_sock_connect(&sock, "131s::1", "2131", "::1", "50") != 0);
assert(sc_sock_finish_connect(&sock) != 0);
assert(sc_sock_connect(&sock, "d::1", "2131", "dsad", "50") != 0);
@ -467,7 +467,7 @@ void* server(void* arg)
else {
if (ev & SC_SOCK_READ) {
char buf[8];
rc = sc_sock_recv(&accepted, buf, sizeof(buf));
rc = sc_sock_recv(&accepted, buf, sizeof(buf), 0);
if (rc == 8) {
assert(strcmp(buf, "dataxxx") == 0);
received++;
@ -526,7 +526,7 @@ void* client(void* arg)
}
for (int j = 0; j < 10000; j++) {
rc = sc_sock_send(&sock, "dataxxx", 8);
rc = sc_sock_send(&sock, "dataxxx", 8, 0);
assert(rc == 8);
}

View File

@ -24,13 +24,13 @@
#include "sc_time.h"
#if defined(_WIN32) || defined(_WIN64)
#include <assert.h>
#include <assert.h>
#include <windows.h>
#else
#include <assert.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#endif
uint64_t sc_time_ms()
@ -48,7 +48,7 @@ uint64_t sc_time_ms()
int rc;
struct timespec ts;
rc = clock_gettime(CLOCK_REALTIME, &ts);
rc = clock_gettime(CLOCK_REALTIME_COARSE, &ts);
assert(rc == 0);
return ts.tv_sec * 1000 + (uint64_t)(ts.tv_nsec / 10e6);