feat: Populate repo

This commit is contained in:
2023-03-29 14:02:05 +02:00
commit 28bec5ebb8
168 changed files with 17860 additions and 0 deletions

10
include/active.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef ACTIVE_H
#define ACTIVE_H
#include <stdlib.h>
extern size_t connections_size;
extern char* connections[100];
extern int connections_head;
#endif

11
include/array.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef ARRAY_H
#define ARRAY_H
void print_array(int arr[], int size);
int find_index(char* arr[], int size, char* needle);
int* largest_three(int arr[], int size);
int* inverse_array(int arr[], int size);
void sort_char_array(char arr[], int size);
void filter_lowercase_and_sort(char arr[], int size);
#endif

19
include/base64.h Normal file
View File

@@ -0,0 +1,19 @@
/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#ifndef BASE64_H
#define BASE64_H
#include <sys/types.h>
unsigned char * base64_encode(const unsigned char *src, size_t len,
size_t *out_len);
unsigned char * base64_decode(const unsigned char *src, size_t len,
size_t *out_len);
#endif /* BASE64_H */

7
include/config.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef CONFIG_H
#define CONFIG_H
int SLEEP_DURATION = 30;
char GPIO_PIN[3] = "25";
#endif

18
include/database.h Normal file
View File

@@ -0,0 +1,18 @@
#include <mysql/mysql.h>
#ifndef DATABASE_H
#define DATABASE_H
extern char _server[56];
extern char _user[56];
extern char _password[56];
extern char _database[56];
MYSQL* init_mysql();
void kill_mysql();
void finish_with_error();
int construct_and_exec(char* command);
int get_sensor_state(int sensorId);
#endif

10
include/gpio.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef GPIO_H
#define GPIO_H
#include <stdlib.h>
void set_pin(char* pin, int state);
void init_pin(char* pin, char* direction);
#endif

23
include/json.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef JSON_H
#define JSON_H
typedef struct {
char* sensorId;
char* sensorData;
} Message;
typedef struct {
Message payload;
char* auth;
} Envelope;
enum RESPONSE_CODES {
SUCCESS,
MISSING_JSON,
MISSING_PAYLOAD,
MISSING_AUTH
};
int parse_envelope_json(const char* json, Envelope* envelope);
#endif

115
include/libtap.h Normal file
View File

@@ -0,0 +1,115 @@
/*
libtap - Write tests in C
Copyright 2012 Jake Gelbman <gelbman@gmail.com>
This file is licensed under the LGPL
*/
#ifndef __TAP_H__
#define __TAP_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifndef va_copy
#ifdef __va_copy
#define va_copy __va_copy
#else
#define va_copy(d, s) ((d) = (s))
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
int vok_at_loc (const char *file, int line, int test, const char *fmt,
va_list args);
int ok_at_loc (const char *file, int line, int test, const char *fmt,
...);
int is_at_loc (const char *file, int line, const char *got,
const char *expected, const char *fmt, ...);
int isnt_at_loc (const char *file, int line, const char *got,
const char *expected, const char *fmt, ...);
int cmp_ok_at_loc (const char *file, int line, int a, const char *op,
int b, const char *fmt, ...);
int cmp_mem_at_loc (const char *file, int line, const void *got,
const void *expected, size_t n, const char *fmt, ...);
int bail_out (int ignore, const char *fmt, ...);
void tap_plan (int tests, const char *fmt, ...);
int diag (const char *fmt, ...);
int exit_status (void);
void tap_skip (int n, const char *fmt, ...);
void tap_todo (int ignore, const char *fmt, ...);
void tap_end_todo (void);
#define NO_PLAN -1
#define SKIP_ALL -2
#define ok(...) ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
#define is(...) is_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
#define isnt(...) isnt_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
#define cmp_ok(...) cmp_ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
#define cmp_mem(...) cmp_mem_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
#define plan(...) tap_plan(__VA_ARGS__, NULL)
#define done_testing() return exit_status()
#define BAIL_OUT(...) bail_out(0, "" __VA_ARGS__, NULL)
#define pass(...) ok(1, "" __VA_ARGS__)
#define fail(...) ok(0, "" __VA_ARGS__)
#define skip(test, ...) do {if (test) {tap_skip(__VA_ARGS__, NULL); break;}
#define end_skip } while (0)
#define todo(...) tap_todo(0, "" __VA_ARGS__, NULL)
#define end_todo tap_end_todo()
#define dies_ok(...) dies_ok_common(1, __VA_ARGS__)
#define lives_ok(...) dies_ok_common(0, __VA_ARGS__)
#ifdef _WIN32
#define like(...) tap_skip(1, "like is not implemented on Windows")
#define unlike(...) tap_skip(1, "unlike is not implemented on Windows")
#define dies_ok_common(...) \
tap_skip(1, "Death detection is not supported on Windows")
#else
#define like(...) like_at_loc(1, __FILE__, __LINE__, __VA_ARGS__, NULL)
#define unlike(...) like_at_loc(0, __FILE__, __LINE__, __VA_ARGS__, NULL)
int like_at_loc (int for_match, const char *file, int line,
const char *got, const char *expected,
const char *fmt, ...);
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int tap_test_died (int status);
#define dies_ok_common(for_death, code, ...) \
do { \
int cpid; \
int it_died; \
tap_test_died(1); \
cpid = fork(); \
switch (cpid) { \
case -1: \
perror("fork error"); \
exit(1); \
case 0: \
close(1); \
close(2); \
code \
tap_test_died(0); \
exit(0); \
} \
if (waitpid(cpid, NULL, 0) < 0) { \
perror("waitpid error"); \
exit(1); \
} \
it_died = tap_test_died(0); \
if (!it_died) \
{code} \
ok(for_death ? it_died : !it_died, "" __VA_ARGS__); \
} while (0)
#endif
#ifdef __cplusplus
}
#endif
#endif

25
include/macros.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef MACROS_H
#define MACROS_H
enum DEBUG_STATE { INACTIVE, ACTIVE };
// const char* PREFIX_HEARTBEAT = "[\033[31mHRTB\033[0m]\t";
// const char* PREFIX_DATABASE = "[\033[36mDTBS\033[0m]\t";
// const char* PREFIX_GPIO = "[\033[35mGPIO\033[0m]\t";
#ifndef __DEBUG__
#define __DEBUG__ ACTIVE
#endif
/** @brief Prints to the console only if DEBUG = 1 **/
#define dbg(fmt, ...) do { \
if (__DEBUG__ == ACTIVE) \
fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, __VA_ARGS__); \
} while (0)
#define dbg_no_info(fmt, ...) do { \
if (__DEBUG__ == ACTIVE) \
fprintf(stderr, fmt, __VA_ARGS__); \
} while (0)
#endif

73
include/sha1.h Normal file
View File

@@ -0,0 +1,73 @@
/*
* sha1.h
*
* Description:
* This is the header file for code which implements the Secure
* Hashing Algorithm 1 as defined in FIPS PUB 180-1 published
* April 17, 1995.
*
* Many of the variable names in this code, especially the
* single character names, were used because those were the names
* used in the publication.
*
* Please read the file sha1.c for more information.
*
*/
#ifndef _SHA1_H_
#define _SHA1_H_
#include <stdint.h>
/*
* If you do not have the ISO standard stdint.h header file, then you
* must typdef the following:
* name meaning
* uint32_t unsigned 32 bit integer
* uint8_t unsigned 8 bit integer (i.e., unsigned char)
* int_least16_t integer of >= 16 bits
*
*/
#ifndef _SHA_enum_
#define _SHA_enum_
enum
{
shaSuccess = 0,
shaNull, /* Null pointer parameter */
shaInputTooLong, /* input data too long */
shaStateError /* called Input after Result */
};
#endif
#define SHA1HashSize 20
/*
* This structure will hold context information for the SHA-1
* hashing operation
*/
typedef struct SHA1Context
{
uint32_t Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */
uint32_t Length_Low; /* Message length in bits */
uint32_t Length_High; /* Message length in bits */
/* Index into message block array */
int_least16_t Message_Block_Index;
uint8_t Message_Block[64]; /* 512-bit message blocks */
int Computed; /* Is the digest computed? */
int Corrupted; /* Is the message digest corrupted? */
} SHA1Context;
/*
* Function Prototypes
*/
int SHA1Reset( SHA1Context *);
int SHA1Input( SHA1Context *,
const uint8_t *,
unsigned int);
int SHA1Result( SHA1Context *,
uint8_t Message_Digest[SHA1HashSize]);
#endif

7
include/tests.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef UTILS_H
#define UTILS_H
void run_argp_tests(int argc, char* argv[], char* env[]);
void run_json_tests(int argc, char* argv[], char* env[]);
#endif

37
include/utf8.h Normal file
View File

@@ -0,0 +1,37 @@
/**
* Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
*
* 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 UTF8_DECODE_H
#define UTF8_DECODE_H
#include <inttypes.h>
#include <stddef.h>
/* UTF8 return state. */
#define UTF8_ACCEPT 0
#define UTF8_REJECT 1
extern int is_utf8(uint8_t* s);
extern int is_utf8_len(uint8_t *s, size_t len);
extern uint32_t is_utf8_len_state(uint8_t *s, size_t len, uint32_t state);
#endif

6
include/utils.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef UTILS_H
#define UTILS_H
const unsigned long hash(const char *str);
#endif

281
include/ws.h Normal file
View File

@@ -0,0 +1,281 @@
/*
* Copyright (C) 2016-2022 Davidson Francis <davidsondfgl@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
/**
* @dir include/
* @brief wsServer include directory
*
* @file ws.h
* @brief wsServer constants and functions.
*/
#ifndef WS_H
#define WS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
/**
* @name Global configurations
*/
/**@{*/
/**
* @brief Max clients connected simultaneously.
*/
#ifndef MAX_CLIENTS
#define MAX_CLIENTS 8
#endif
/**
* @name Key and message configurations.
*/
/**@{*/
/**
* @brief Message buffer length.
*/
#define MESSAGE_LENGTH 2048
/**
* @brief Maximum frame/message length.
*/
#define MAX_FRAME_LENGTH (16*1024*1024)
/**
* @brief WebSocket key length.
*/
#define WS_KEY_LEN 24
/**
* @brief Magic string length.
*/
#define WS_MS_LEN 36
/**
* @brief Accept message response length.
*/
#define WS_KEYMS_LEN (WS_KEY_LEN + WS_MS_LEN)
/**
* @brief Magic string.
*/
#define MAGIC_STRING "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
/**@}*/
/**
* @name Handshake constants.
*/
/**@{*/
/**
* @brief Alias for 'Sec-WebSocket-Key'.
*/
#define WS_HS_REQ "Sec-WebSocket-Key"
/**
* @brief Handshake accept message length.
*/
#define WS_HS_ACCLEN 130
/**
* @brief Handshake accept message.
*/
#define WS_HS_ACCEPT \
"HTTP/1.1 101 Switching Protocols\r\n" \
"Upgrade: websocket\r\n" \
"Connection: Upgrade\r\n" \
"Sec-WebSocket-Accept: "
/**@}*/
/**
* @name Frame types.
*/
/**@{*/
/**
* @brief Frame FIN.
*/
#define WS_FIN 128
/**
* @brief Frame FIN shift
*/
#define WS_FIN_SHIFT 7
/**
* @brief Continuation frame.
*/
#define WS_FR_OP_CONT 0
/**
* @brief Text frame.
*/
#define WS_FR_OP_TXT 1
/**
* @brief Binary frame.
*/
#define WS_FR_OP_BIN 2
/**
* @brief Close frame.
*/
#define WS_FR_OP_CLSE 8
/**
* @brief Ping frame.
*/
#define WS_FR_OP_PING 0x9
/**
* @brief Pong frame.
*/
#define WS_FR_OP_PONG 0xA
/**
* @brief Unsupported frame.
*/
#define WS_FR_OP_UNSUPPORTED 0xF
/**@}*/
/**
* @name Close codes
*/
/**@{*/
/**
* @brief Normal close
*/
#define WS_CLSE_NORMAL 1000
/**
* @brief Protocol error
*/
#define WS_CLSE_PROTERR 1002
/**@}*/
/**
* @brief Inconsistent message (invalid utf-8)
*/
#define WS_CLSE_INVUTF8 1007
/**
* @name Connection states
*/
/**@{*/
/**
* @brief Connection not established yet.
*/
#define WS_STATE_CONNECTING 0
/**
* @brief Communicating.
*/
#define WS_STATE_OPEN 1
/**
* @brief Closing state.
*/
#define WS_STATE_CLOSING 2
/**
* @brief Closed.
*/
#define WS_STATE_CLOSED 3
/**@}*/
/**
* @name Timeout util
*/
/**@{*/
/**
* @brief Nanoseconds macro converter
*/
#define MS_TO_NS(x) ((x)*1000000)
/**
* @brief Timeout in milliseconds.
*/
#define TIMEOUT_MS (500)
/**@}*/
/**
* @name Handshake constants.
*/
/**@{*/
/**
* @brief Debug
*/
#ifdef VERBOSE_MODE
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#else
#define DEBUG(...)
#endif
/**@}*/
#ifndef AFL_FUZZ
#define SEND(client,buf,len) send_all((client), (buf), (len), MSG_NOSIGNAL)
#define RECV(fd,buf,len) recv((fd)->client_sock, (buf), (len), 0)
#else
#define SEND(client,buf,len) write(fileno(stdout), (buf), (len))
#define RECV(fd,buf,len) read((fd)->client_sock, (buf), (len))
#endif
/* Opaque client connection type. */
typedef struct ws_connection ws_cli_conn_t;
/**
* @brief events Web Socket events types.
*/
struct ws_events
{
/**
* @brief On open event, called when a new client connects.
*/
void (*onopen)(ws_cli_conn_t *client);
/**
* @brief On close event, called when a client disconnects.
*/
void (*onclose)(ws_cli_conn_t *client);
/**
* @brief On message event, called when a client sends a text
* or binary message.
*/
void (*onmessage)(ws_cli_conn_t *client,
const unsigned char *msg, uint64_t msg_size, int type);
};
/* Forward declarations. */
/* Internal usage. */
extern int get_handshake_accept(char *wsKey, unsigned char **dest);
extern int get_handshake_response(char *hsrequest, char **hsresponse);
/* External usage. */
extern char *ws_getaddress(ws_cli_conn_t *client);
extern int ws_sendframe(
ws_cli_conn_t *cli, const char *msg, uint64_t size, int type);
extern int ws_sendframe_txt(ws_cli_conn_t *cli, const char *msg);
extern int ws_sendframe_bin(ws_cli_conn_t *cli, const char *msg, uint64_t size);
extern int ws_get_state(ws_cli_conn_t *cli);
extern int ws_close_client(ws_cli_conn_t *cli);
extern int ws_socket(struct ws_events *evs, uint16_t port, int thread_loop,
uint32_t timeout_ms);
/* Ping routines. */
extern void ws_ping(ws_cli_conn_t *cli, int threshold);
#ifdef AFL_FUZZ
extern int ws_file(struct ws_events *evs, const char *file);
#endif
#ifdef __cplusplus
}
#endif
#endif /* WS_H */