├── .github └── dependabot.yml ├── .gitignore ├── .gitmodules ├── .travis.yml ├── 3pp └── mbedtls │ ├── .gitignore │ ├── CONTRIBUTING.md │ ├── ChangeLog │ ├── DartConfiguration.tcl │ ├── LICENSE │ ├── Makefile │ ├── README.md │ ├── apache-2.0.txt │ ├── configs │ ├── README.txt │ ├── config-ccm-psk-tls1_2.h │ ├── config-mini-tls1_1.h │ ├── config-no-entropy.h │ ├── config-suite-b.h │ └── config-thread.h │ ├── include │ ├── .gitignore │ ├── CMakeLists.txt │ └── mbedtls │ │ ├── aes.h │ │ ├── aesni.h │ │ ├── arc4.h │ │ ├── aria.h │ │ ├── asn1.h │ │ ├── asn1write.h │ │ ├── base64.h │ │ ├── bignum.h │ │ ├── blowfish.h │ │ ├── bn_mul.h │ │ ├── camellia.h │ │ ├── ccm.h │ │ ├── certs.h │ │ ├── chacha20.h │ │ ├── chachapoly.h │ │ ├── check_config.h │ │ ├── cipher.h │ │ ├── cipher_internal.h │ │ ├── cmac.h │ │ ├── compat-1.3.h │ │ ├── config.h │ │ ├── ctr_drbg.h │ │ ├── debug.h │ │ ├── des.h │ │ ├── dhm.h │ │ ├── ecdh.h │ │ ├── ecdsa.h │ │ ├── ecjpake.h │ │ ├── ecp.h │ │ ├── ecp_internal.h │ │ ├── entropy.h │ │ ├── entropy_poll.h │ │ ├── error.h │ │ ├── gcm.h │ │ ├── havege.h │ │ ├── hkdf.h │ │ ├── hmac_drbg.h │ │ ├── md.h │ │ ├── md2.h │ │ ├── md4.h │ │ ├── md5.h │ │ ├── md_internal.h │ │ ├── memory_buffer_alloc.h │ │ ├── net.h │ │ ├── net_sockets.h │ │ ├── nist_kw.h │ │ ├── oid.h │ │ ├── padlock.h │ │ ├── pem.h │ │ ├── pk.h │ │ ├── pk_internal.h │ │ ├── pkcs11.h │ │ ├── pkcs12.h │ │ ├── pkcs5.h │ │ ├── platform.h │ │ ├── platform_time.h │ │ ├── platform_util.h │ │ ├── poly1305.h │ │ ├── ripemd160.h │ │ ├── rsa.h │ │ ├── rsa_internal.h │ │ ├── sha1.h │ │ ├── sha256.h │ │ ├── sha512.h │ │ ├── ssl.h │ │ ├── ssl_cache.h │ │ ├── ssl_ciphersuites.h │ │ ├── ssl_cookie.h │ │ ├── ssl_internal.h │ │ ├── ssl_ticket.h │ │ ├── threading.h │ │ ├── timing.h │ │ ├── version.h │ │ ├── x509.h │ │ ├── x509_crl.h │ │ ├── x509_crt.h │ │ ├── x509_csr.h │ │ └── xtea.h │ └── library │ ├── .gitignore │ ├── CMakeLists.txt │ ├── Makefile │ ├── aes.c │ ├── aesni.c │ ├── arc4.c │ ├── aria.c │ ├── asn1parse.c │ ├── asn1write.c │ ├── base64.c │ ├── bignum.c │ ├── blowfish.c │ ├── camellia.c │ ├── ccm.c │ ├── certs.c │ ├── chacha20.c │ ├── chachapoly.c │ ├── cipher.c │ ├── cipher_wrap.c │ ├── cmac.c │ ├── ctr_drbg.c │ ├── debug.c │ ├── des.c │ ├── dhm.c │ ├── ecdh.c │ ├── ecdsa.c │ ├── ecjpake.c │ ├── ecp.c │ ├── ecp_curves.c │ ├── entropy.c │ ├── entropy_poll.c │ ├── error.c │ ├── gcm.c │ ├── havege.c │ ├── hkdf.c │ ├── hmac_drbg.c │ ├── md.c │ ├── md2.c │ ├── md4.c │ ├── md5.c │ ├── md_wrap.c │ ├── memory_buffer_alloc.c │ ├── net_sockets.c │ ├── nist_kw.c │ ├── oid.c │ ├── padlock.c │ ├── pem.c │ ├── pk.c │ ├── pk_wrap.c │ ├── pkcs11.c │ ├── pkcs12.c │ ├── pkcs5.c │ ├── pkparse.c │ ├── pkwrite.c │ ├── platform.c │ ├── platform_util.c │ ├── poly1305.c │ ├── ripemd160.c │ ├── rsa.c │ ├── rsa_internal.c │ ├── sha1.c │ ├── sha256.c │ ├── sha512.c │ ├── ssl_cache.c │ ├── ssl_ciphersuites.c │ ├── ssl_cli.c │ ├── ssl_cookie.c │ ├── ssl_srv.c │ ├── ssl_ticket.c │ ├── ssl_tls.c │ ├── threading.c │ ├── timing.c │ ├── version.c │ ├── version_features.c │ ├── x509.c │ ├── x509_create.c │ ├── x509_crl.c │ ├── x509_crt.c │ ├── x509_csr.c │ ├── x509write_crt.c │ ├── x509write_csr.c │ └── xtea.c ├── LICENSE ├── Makefile ├── README.rst ├── codecov.yml ├── examples ├── call_threadsafe │ ├── Makefile │ ├── README.rst │ └── main.c ├── call_worker_pool │ ├── Makefile │ ├── README.rst │ └── main.c ├── conversation │ ├── Makefile │ ├── README.rst │ ├── bob.c │ ├── bob.h │ └── main.c ├── counter │ ├── Makefile │ ├── README.rst │ ├── counter.c │ ├── counter.h │ └── main.c ├── hello_world │ ├── Makefile │ ├── README.rst │ └── main.c ├── http_get │ ├── Makefile │ ├── README.rst │ ├── http_get.c │ ├── http_get.h │ └── main.c ├── mqtt_client │ ├── Makefile │ ├── README.rst │ ├── ca.crt │ ├── generate_certificates.sh │ ├── main.c │ ├── publisher.c │ ├── publisher.h │ ├── server.crt │ └── server.key ├── ping_pong │ ├── Makefile │ └── main.c ├── shell │ ├── Makefile │ ├── README.rst │ ├── main.c │ ├── my_shell.c │ └── my_shell.h ├── stcp_echo_client │ ├── Makefile │ ├── README.rst │ ├── echo_client.c │ ├── echo_client.h │ ├── main.c │ ├── server.crt │ ├── server.key │ ├── server.py │ └── ssl_server.py ├── stcp_echo_server │ ├── Makefile │ ├── README.rst │ ├── echo_server.c │ ├── echo_server.h │ └── main.c └── timers │ ├── Makefile │ ├── README.rst │ ├── main.c │ ├── timers.c │ └── timers.h ├── include ├── async.h └── async │ ├── core.h │ ├── core │ ├── channel.h │ ├── core.h │ ├── runtime.h │ ├── tcp_client.h │ └── tcp_server.h │ ├── modules │ ├── mqtt_client.h │ ├── shell.h │ ├── ssl.h │ ├── stcp_client.h │ └── stcp_server.h │ ├── runtimes │ ├── linux.h │ └── monolinux.h │ └── utils │ └── linux.h ├── make ├── app.mk ├── common.mk ├── library.mk └── lsan-suppressions.txt ├── setup.sh ├── src ├── core │ ├── async_channel.c │ ├── async_core.c │ ├── async_runtime_null.c │ ├── async_tcp_client.c │ ├── async_tcp_server.c │ ├── async_timer.c │ └── internal.h ├── modules │ ├── async_mqtt_client.c │ ├── async_shell.c │ ├── async_ssl.c │ ├── async_stcp_client.c │ └── async_stcp_server.c ├── runtimes │ ├── async_runtime.c │ └── async_runtime_linux.c └── utils │ └── async_utils_linux.c └── tst ├── Makefile ├── test.mk ├── test_core_channel.c ├── test_core_core.c ├── test_core_runtime_null.c ├── test_core_tcp_client.c ├── test_core_tcp_server.c ├── test_core_timer.c ├── test_mqtt_client.c ├── test_runtime.c ├── test_shell.c └── utils ├── runtime_test.c ├── runtime_test.h ├── runtime_test_impl.c ├── subprocess.c ├── subprocess.h ├── utils.c └── utils.h /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: gitsubmodule 4 | directory: "/" 5 | schedule: 6 | interval: weekly 7 | time: "04:00" 8 | open-pull-requests-limit: 10 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | nala_* 3 | report.json 4 | *.gcno 5 | tests.pp.c 6 | async-core-* -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "3pp/humanfriendly"] 2 | path = 3pp/humanfriendly 3 | url = https://github.com/eerimoq/humanfriendly 4 | [submodule "3pp/bitstream"] 5 | path = 3pp/bitstream 6 | url = https://github.com/eerimoq/bitstream 7 | [submodule "3pp/monolinux-c-library"] 8 | path = 3pp/monolinux-c-library 9 | url = https://github.com/eerimoq/monolinux-c-library 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | 3 | python: 4 | - "3.7" 5 | 6 | install: 7 | - pip install nala 8 | 9 | script: 10 | - source setup.sh 11 | - make -s -j $(nproc) SANITIZE=yes all 12 | - make release 13 | - make release-compile 14 | 15 | after_success: 16 | - bash <(curl -s https://codecov.io/bash) -X coveragepy 17 | -------------------------------------------------------------------------------- /3pp/mbedtls/.gitignore: -------------------------------------------------------------------------------- 1 | # Random seed file created by test scripts and sample programs 2 | seedfile 3 | 4 | # CMake build artifacts: 5 | CMakeCache.txt 6 | CMakeFiles 7 | CTestTestfile.cmake 8 | cmake_install.cmake 9 | Testing 10 | # CMake generates *.dir/ folders for in-tree builds (used by MSVC projects), ignore all of those: 11 | *.dir/ 12 | # MSVC files generated by CMake: 13 | /*.sln 14 | /*.vcxproj 15 | /*.filters 16 | 17 | # Test coverage build artifacts: 18 | Coverage 19 | *.gcno 20 | *.gcda 21 | 22 | # generated by scripts/memory.sh 23 | massif-* 24 | 25 | # MSVC build artifacts: 26 | *.exe 27 | *.pdb 28 | *.ilk 29 | *.lib 30 | 31 | # Python build artifacts: 32 | *.pyc 33 | 34 | # Generated documentation: 35 | /apidoc 36 | 37 | # Editor navigation files: 38 | /GPATH 39 | /GRTAGS 40 | /GSYMS 41 | /GTAGS 42 | /TAGS 43 | /tags 44 | -------------------------------------------------------------------------------- /3pp/mbedtls/DartConfiguration.tcl: -------------------------------------------------------------------------------- 1 | Site: localhost 2 | BuildName: mbed TLS-test 3 | CoverageCommand: /usr/bin/gcov 4 | MemoryCheckCommand: /usr/bin/valgrind 5 | -------------------------------------------------------------------------------- /3pp/mbedtls/LICENSE: -------------------------------------------------------------------------------- 1 | Unless specifically indicated otherwise in a file, files are licensed 2 | under the Apache 2.0 license, as can be found in: apache-2.0.txt 3 | -------------------------------------------------------------------------------- /3pp/mbedtls/configs/README.txt: -------------------------------------------------------------------------------- 1 | This directory contains example configuration files. 2 | 3 | The examples are generally focused on a particular usage case (eg, support for 4 | a restricted number of ciphersuites) and aim at minimizing resource usage for 5 | this target. They can be used as a basis for custom configurations. 6 | 7 | These files are complete replacements for the default config.h. To use one of 8 | them, you can pick one of the following methods: 9 | 10 | 1. Replace the default file include/mbedtls/config.h with the chosen one. 11 | (Depending on your compiler, you may need to adjust the line with 12 | #include "mbedtls/check_config.h" then.) 13 | 14 | 2. Define MBEDTLS_CONFIG_FILE and adjust the include path accordingly. 15 | For example, using make: 16 | 17 | CFLAGS="-I$PWD/configs -DMBEDTLS_CONFIG_FILE=''" make 18 | 19 | Or, using cmake: 20 | 21 | find . -iname '*cmake*' -not -name CMakeLists.txt -exec rm -rf {} + 22 | CFLAGS="-I$PWD/configs -DMBEDTLS_CONFIG_FILE=''" cmake . 23 | make 24 | 25 | Note that the second method also works if you want to keep your custom 26 | configuration file outside the mbed TLS tree. 27 | -------------------------------------------------------------------------------- /3pp/mbedtls/configs/config-ccm-psk-tls1_2.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file config-ccm-psk-tls1_2.h 3 | * 4 | * \brief Minimal configuration for TLS 1.2 with PSK and AES-CCM ciphersuites 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | /* 25 | * Minimal configuration for TLS 1.2 with PSK and AES-CCM ciphersuites 26 | * Distinguishing features: 27 | * - no bignum, no PK, no X509 28 | * - fully modern and secure (provided the pre-shared keys have high entropy) 29 | * - very low record overhead with CCM-8 30 | * - optimized for low RAM usage 31 | * 32 | * See README.txt for usage instructions. 33 | */ 34 | #ifndef MBEDTLS_CONFIG_H 35 | #define MBEDTLS_CONFIG_H 36 | 37 | /* System support */ 38 | //#define MBEDTLS_HAVE_TIME /* Optionally used in Hello messages */ 39 | /* Other MBEDTLS_HAVE_XXX flags irrelevant for this configuration */ 40 | 41 | /* mbed TLS feature support */ 42 | #define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED 43 | #define MBEDTLS_SSL_PROTO_TLS1_2 44 | 45 | /* mbed TLS modules */ 46 | #define MBEDTLS_AES_C 47 | #define MBEDTLS_CCM_C 48 | #define MBEDTLS_CIPHER_C 49 | #define MBEDTLS_CTR_DRBG_C 50 | #define MBEDTLS_ENTROPY_C 51 | #define MBEDTLS_MD_C 52 | #define MBEDTLS_NET_C 53 | #define MBEDTLS_SHA256_C 54 | #define MBEDTLS_SSL_CLI_C 55 | #define MBEDTLS_SSL_SRV_C 56 | #define MBEDTLS_SSL_TLS_C 57 | 58 | /* Save RAM at the expense of ROM */ 59 | #define MBEDTLS_AES_ROM_TABLES 60 | 61 | /* Save some RAM by adjusting to your exact needs */ 62 | #define MBEDTLS_PSK_MAX_LEN 16 /* 128-bits keys are generally enough */ 63 | 64 | /* 65 | * You should adjust this to the exact number of sources you're using: default 66 | * is the "platform_entropy_poll" source, but you may want to add other ones 67 | * Minimum is 2 for the entropy test suite. 68 | */ 69 | #define MBEDTLS_ENTROPY_MAX_SOURCES 2 70 | 71 | /* 72 | * Use only CCM_8 ciphersuites, and 73 | * save ROM and a few bytes of RAM by specifying our own ciphersuite list 74 | */ 75 | #define MBEDTLS_SSL_CIPHERSUITES \ 76 | MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8, \ 77 | MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8 78 | 79 | /* 80 | * Save RAM at the expense of interoperability: do this only if you control 81 | * both ends of the connection! (See comments in "mbedtls/ssl.h".) 82 | * The optimal size here depends on the typical size of records. 83 | */ 84 | #define MBEDTLS_SSL_MAX_CONTENT_LEN 1024 85 | 86 | #include "mbedtls/check_config.h" 87 | 88 | #endif /* MBEDTLS_CONFIG_H */ 89 | -------------------------------------------------------------------------------- /3pp/mbedtls/configs/config-mini-tls1_1.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file config-mini-tls1_1.h 3 | * 4 | * \brief Minimal configuration for TLS 1.1 (RFC 4346) 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | /* 25 | * Minimal configuration for TLS 1.1 (RFC 4346), implementing only the 26 | * required ciphersuite: MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA 27 | * 28 | * See README.txt for usage instructions. 29 | */ 30 | 31 | #ifndef MBEDTLS_CONFIG_H 32 | #define MBEDTLS_CONFIG_H 33 | 34 | /* System support */ 35 | #define MBEDTLS_HAVE_ASM 36 | #define MBEDTLS_HAVE_TIME 37 | 38 | /* mbed TLS feature support */ 39 | #define MBEDTLS_CIPHER_MODE_CBC 40 | #define MBEDTLS_PKCS1_V15 41 | #define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED 42 | #define MBEDTLS_SSL_PROTO_TLS1_1 43 | 44 | /* mbed TLS modules */ 45 | #define MBEDTLS_AES_C 46 | #define MBEDTLS_ASN1_PARSE_C 47 | #define MBEDTLS_ASN1_WRITE_C 48 | #define MBEDTLS_BIGNUM_C 49 | #define MBEDTLS_CIPHER_C 50 | #define MBEDTLS_CTR_DRBG_C 51 | #define MBEDTLS_DES_C 52 | #define MBEDTLS_ENTROPY_C 53 | #define MBEDTLS_MD_C 54 | #define MBEDTLS_MD5_C 55 | #define MBEDTLS_NET_C 56 | #define MBEDTLS_OID_C 57 | #define MBEDTLS_PK_C 58 | #define MBEDTLS_PK_PARSE_C 59 | #define MBEDTLS_RSA_C 60 | #define MBEDTLS_SHA1_C 61 | #define MBEDTLS_SHA256_C 62 | #define MBEDTLS_SSL_CLI_C 63 | #define MBEDTLS_SSL_SRV_C 64 | #define MBEDTLS_SSL_TLS_C 65 | #define MBEDTLS_X509_CRT_PARSE_C 66 | #define MBEDTLS_X509_USE_C 67 | 68 | /* For test certificates */ 69 | #define MBEDTLS_BASE64_C 70 | #define MBEDTLS_CERTS_C 71 | #define MBEDTLS_PEM_PARSE_C 72 | 73 | /* For testing with compat.sh */ 74 | #define MBEDTLS_FS_IO 75 | 76 | #include "mbedtls/check_config.h" 77 | 78 | #endif /* MBEDTLS_CONFIG_H */ 79 | -------------------------------------------------------------------------------- /3pp/mbedtls/configs/config-no-entropy.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file config-no-entropy.h 3 | * 4 | * \brief Minimal configuration of features that do not require an entropy source 5 | */ 6 | /* 7 | * Copyright (C) 2016, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | /* 25 | * Minimal configuration of features that do not require an entropy source 26 | * Distinguishing reatures: 27 | * - no entropy module 28 | * - no TLS protocol implementation available due to absence of an entropy 29 | * source 30 | * 31 | * See README.txt for usage instructions. 32 | */ 33 | 34 | #ifndef MBEDTLS_CONFIG_H 35 | #define MBEDTLS_CONFIG_H 36 | 37 | /* System support */ 38 | #define MBEDTLS_HAVE_ASM 39 | #define MBEDTLS_HAVE_TIME 40 | 41 | /* mbed TLS feature support */ 42 | #define MBEDTLS_CIPHER_MODE_CBC 43 | #define MBEDTLS_CIPHER_PADDING_PKCS7 44 | #define MBEDTLS_REMOVE_ARC4_CIPHERSUITES 45 | #define MBEDTLS_ECP_DP_SECP256R1_ENABLED 46 | #define MBEDTLS_ECP_DP_SECP384R1_ENABLED 47 | #define MBEDTLS_ECP_DP_CURVE25519_ENABLED 48 | #define MBEDTLS_ECP_NIST_OPTIM 49 | #define MBEDTLS_ECDSA_DETERMINISTIC 50 | #define MBEDTLS_PK_RSA_ALT_SUPPORT 51 | #define MBEDTLS_PKCS1_V15 52 | #define MBEDTLS_PKCS1_V21 53 | #define MBEDTLS_SELF_TEST 54 | #define MBEDTLS_VERSION_FEATURES 55 | #define MBEDTLS_X509_CHECK_KEY_USAGE 56 | #define MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE 57 | 58 | /* mbed TLS modules */ 59 | #define MBEDTLS_AES_C 60 | #define MBEDTLS_ASN1_PARSE_C 61 | #define MBEDTLS_ASN1_WRITE_C 62 | #define MBEDTLS_BASE64_C 63 | #define MBEDTLS_BIGNUM_C 64 | #define MBEDTLS_CCM_C 65 | #define MBEDTLS_CIPHER_C 66 | #define MBEDTLS_ECDSA_C 67 | #define MBEDTLS_ECP_C 68 | #define MBEDTLS_ERROR_C 69 | #define MBEDTLS_GCM_C 70 | #define MBEDTLS_HMAC_DRBG_C 71 | #define MBEDTLS_MD_C 72 | #define MBEDTLS_OID_C 73 | #define MBEDTLS_PEM_PARSE_C 74 | #define MBEDTLS_PK_C 75 | #define MBEDTLS_PK_PARSE_C 76 | #define MBEDTLS_PK_WRITE_C 77 | #define MBEDTLS_PLATFORM_C 78 | #define MBEDTLS_RSA_C 79 | #define MBEDTLS_SHA256_C 80 | #define MBEDTLS_SHA512_C 81 | #define MBEDTLS_VERSION_C 82 | #define MBEDTLS_X509_USE_C 83 | #define MBEDTLS_X509_CRT_PARSE_C 84 | #define MBEDTLS_X509_CRL_PARSE_C 85 | //#define MBEDTLS_CMAC_C 86 | 87 | /* Miscellaneous options */ 88 | #define MBEDTLS_AES_ROM_TABLES 89 | 90 | #include "check_config.h" 91 | 92 | #endif /* MBEDTLS_CONFIG_H */ 93 | -------------------------------------------------------------------------------- /3pp/mbedtls/configs/config-suite-b.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file config-suite-b.h 3 | * 4 | * \brief Minimal configuration for TLS NSA Suite B Profile (RFC 6460) 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | /* 25 | * Minimal configuration for TLS NSA Suite B Profile (RFC 6460) 26 | * 27 | * Distinguishing features: 28 | * - no RSA or classic DH, fully based on ECC 29 | * - optimized for low RAM usage 30 | * 31 | * Possible improvements: 32 | * - if 128-bit security is enough, disable secp384r1 and SHA-512 33 | * - use embedded certs in DER format and disable PEM_PARSE_C and BASE64_C 34 | * 35 | * See README.txt for usage instructions. 36 | */ 37 | 38 | #ifndef MBEDTLS_CONFIG_H 39 | #define MBEDTLS_CONFIG_H 40 | 41 | /* System support */ 42 | #define MBEDTLS_HAVE_ASM 43 | #define MBEDTLS_HAVE_TIME 44 | 45 | /* mbed TLS feature support */ 46 | #define MBEDTLS_ECP_DP_SECP256R1_ENABLED 47 | #define MBEDTLS_ECP_DP_SECP384R1_ENABLED 48 | #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED 49 | #define MBEDTLS_SSL_PROTO_TLS1_2 50 | 51 | /* mbed TLS modules */ 52 | #define MBEDTLS_AES_C 53 | #define MBEDTLS_ASN1_PARSE_C 54 | #define MBEDTLS_ASN1_WRITE_C 55 | #define MBEDTLS_BIGNUM_C 56 | #define MBEDTLS_CIPHER_C 57 | #define MBEDTLS_CTR_DRBG_C 58 | #define MBEDTLS_ECDH_C 59 | #define MBEDTLS_ECDSA_C 60 | #define MBEDTLS_ECP_C 61 | #define MBEDTLS_ENTROPY_C 62 | #define MBEDTLS_GCM_C 63 | #define MBEDTLS_MD_C 64 | #define MBEDTLS_NET_C 65 | #define MBEDTLS_OID_C 66 | #define MBEDTLS_PK_C 67 | #define MBEDTLS_PK_PARSE_C 68 | #define MBEDTLS_SHA256_C 69 | #define MBEDTLS_SHA512_C 70 | #define MBEDTLS_SSL_CLI_C 71 | #define MBEDTLS_SSL_SRV_C 72 | #define MBEDTLS_SSL_TLS_C 73 | #define MBEDTLS_X509_CRT_PARSE_C 74 | #define MBEDTLS_X509_USE_C 75 | 76 | /* For test certificates */ 77 | #define MBEDTLS_BASE64_C 78 | #define MBEDTLS_CERTS_C 79 | #define MBEDTLS_PEM_PARSE_C 80 | 81 | /* Save RAM at the expense of ROM */ 82 | #define MBEDTLS_AES_ROM_TABLES 83 | 84 | /* Save RAM by adjusting to our exact needs */ 85 | #define MBEDTLS_ECP_MAX_BITS 384 86 | #define MBEDTLS_MPI_MAX_SIZE 48 // 384 bits is 48 bytes 87 | 88 | /* Save RAM at the expense of speed, see ecp.h */ 89 | #define MBEDTLS_ECP_WINDOW_SIZE 2 90 | #define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 91 | 92 | /* Significant speed benefit at the expense of some ROM */ 93 | #define MBEDTLS_ECP_NIST_OPTIM 94 | 95 | /* 96 | * You should adjust this to the exact number of sources you're using: default 97 | * is the "mbedtls_platform_entropy_poll" source, but you may want to add other ones. 98 | * Minimum is 2 for the entropy test suite. 99 | */ 100 | #define MBEDTLS_ENTROPY_MAX_SOURCES 2 101 | 102 | /* Save ROM and a few bytes of RAM by specifying our own ciphersuite list */ 103 | #define MBEDTLS_SSL_CIPHERSUITES \ 104 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, \ 105 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 106 | 107 | /* 108 | * Save RAM at the expense of interoperability: do this only if you control 109 | * both ends of the connection! (See coments in "mbedtls/ssl.h".) 110 | * The minimum size here depends on the certificate chain used as well as the 111 | * typical size of records. 112 | */ 113 | #define MBEDTLS_SSL_MAX_CONTENT_LEN 1024 114 | 115 | #include "mbedtls/check_config.h" 116 | 117 | #endif /* MBEDTLS_CONFIG_H */ 118 | -------------------------------------------------------------------------------- /3pp/mbedtls/configs/config-thread.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file config-thread.h 3 | * 4 | * \brief Minimal configuration for using TLS as part of Thread 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | 25 | /* 26 | * Minimal configuration for using TLS a part of Thread 27 | * http://threadgroup.org/ 28 | * 29 | * Distinguishing features: 30 | * - no RSA or classic DH, fully based on ECC 31 | * - no X.509 32 | * - support for experimental EC J-PAKE key exchange 33 | * 34 | * See README.txt for usage instructions. 35 | */ 36 | 37 | #ifndef MBEDTLS_CONFIG_H 38 | #define MBEDTLS_CONFIG_H 39 | 40 | /* System support */ 41 | #define MBEDTLS_HAVE_ASM 42 | 43 | /* mbed TLS feature support */ 44 | #define MBEDTLS_AES_ROM_TABLES 45 | #define MBEDTLS_ECP_DP_SECP256R1_ENABLED 46 | #define MBEDTLS_ECP_NIST_OPTIM 47 | #define MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED 48 | #define MBEDTLS_SSL_MAX_FRAGMENT_LENGTH 49 | #define MBEDTLS_SSL_PROTO_TLS1_2 50 | #define MBEDTLS_SSL_PROTO_DTLS 51 | #define MBEDTLS_SSL_DTLS_ANTI_REPLAY 52 | #define MBEDTLS_SSL_DTLS_HELLO_VERIFY 53 | #define MBEDTLS_SSL_EXPORT_KEYS 54 | 55 | /* mbed TLS modules */ 56 | #define MBEDTLS_AES_C 57 | #define MBEDTLS_ASN1_PARSE_C 58 | #define MBEDTLS_ASN1_WRITE_C 59 | #define MBEDTLS_BIGNUM_C 60 | #define MBEDTLS_CCM_C 61 | #define MBEDTLS_CIPHER_C 62 | #define MBEDTLS_CTR_DRBG_C 63 | #define MBEDTLS_CMAC_C 64 | #define MBEDTLS_ECJPAKE_C 65 | #define MBEDTLS_ECP_C 66 | #define MBEDTLS_ENTROPY_C 67 | #define MBEDTLS_HMAC_DRBG_C 68 | #define MBEDTLS_MD_C 69 | #define MBEDTLS_OID_C 70 | #define MBEDTLS_PK_C 71 | #define MBEDTLS_PK_PARSE_C 72 | #define MBEDTLS_SHA256_C 73 | #define MBEDTLS_SSL_COOKIE_C 74 | #define MBEDTLS_SSL_CLI_C 75 | #define MBEDTLS_SSL_SRV_C 76 | #define MBEDTLS_SSL_TLS_C 77 | 78 | /* For tests using ssl-opt.sh */ 79 | #define MBEDTLS_NET_C 80 | #define MBEDTLS_TIMING_C 81 | 82 | /* Save RAM at the expense of ROM */ 83 | #define MBEDTLS_AES_ROM_TABLES 84 | 85 | /* Save RAM by adjusting to our exact needs */ 86 | #define MBEDTLS_ECP_MAX_BITS 256 87 | #define MBEDTLS_MPI_MAX_SIZE 32 // 256 bits is 32 bytes 88 | 89 | /* Save ROM and a few bytes of RAM by specifying our own ciphersuite list */ 90 | #define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8 91 | 92 | #include "mbedtls/check_config.h" 93 | 94 | #endif /* MBEDTLS_CONFIG_H */ 95 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/.gitignore: -------------------------------------------------------------------------------- 1 | Makefile 2 | *.sln 3 | *.vcxproj 4 | mbedtls/check_config 5 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | option(INSTALL_MBEDTLS_HEADERS "Install mbed TLS headers." ON) 2 | 3 | if(INSTALL_MBEDTLS_HEADERS) 4 | 5 | file(GLOB headers "mbedtls/*.h") 6 | 7 | install(FILES ${headers} 8 | DESTINATION include/mbedtls 9 | PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) 10 | 11 | endif(INSTALL_MBEDTLS_HEADERS) 12 | 13 | # Make config.h available in an out-of-source build. ssl-opt.sh requires it. 14 | if (ENABLE_TESTING AND NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) 15 | link_to_source(mbedtls) 16 | endif() 17 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/base64.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file base64.h 3 | * 4 | * \brief RFC 1521 base64 encoding/decoding 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_BASE64_H 25 | #define MBEDTLS_BASE64_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include 34 | 35 | #define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */ 36 | #define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */ 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /** 43 | * \brief Encode a buffer into base64 format 44 | * 45 | * \param dst destination buffer 46 | * \param dlen size of the destination buffer 47 | * \param olen number of bytes written 48 | * \param src source buffer 49 | * \param slen amount of data to be encoded 50 | * 51 | * \return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL. 52 | * *olen is always updated to reflect the amount 53 | * of data that has (or would have) been written. 54 | * If that length cannot be represented, then no data is 55 | * written to the buffer and *olen is set to the maximum 56 | * length representable as a size_t. 57 | * 58 | * \note Call this function with dlen = 0 to obtain the 59 | * required buffer size in *olen 60 | */ 61 | int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen, 62 | const unsigned char *src, size_t slen ); 63 | 64 | /** 65 | * \brief Decode a base64-formatted buffer 66 | * 67 | * \param dst destination buffer (can be NULL for checking size) 68 | * \param dlen size of the destination buffer 69 | * \param olen number of bytes written 70 | * \param src source buffer 71 | * \param slen amount of data to be decoded 72 | * 73 | * \return 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or 74 | * MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is 75 | * not correct. *olen is always updated to reflect the amount 76 | * of data that has (or would have) been written. 77 | * 78 | * \note Call this function with *dst = NULL or dlen = 0 to obtain 79 | * the required buffer size in *olen 80 | */ 81 | int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, 82 | const unsigned char *src, size_t slen ); 83 | 84 | #if defined(MBEDTLS_SELF_TEST) 85 | /** 86 | * \brief Checkup routine 87 | * 88 | * \return 0 if successful, or 1 if the test failed 89 | */ 90 | int mbedtls_base64_self_test( int verbose ); 91 | 92 | #endif /* MBEDTLS_SELF_TEST */ 93 | 94 | #ifdef __cplusplus 95 | } 96 | #endif 97 | 98 | #endif /* base64.h */ 99 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/cipher_internal.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file cipher_internal.h 3 | * 4 | * \brief Cipher wrappers. 5 | * 6 | * \author Adriaan de Jong 7 | */ 8 | /* 9 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * http://www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | * 24 | * This file is part of mbed TLS (https://tls.mbed.org) 25 | */ 26 | #ifndef MBEDTLS_CIPHER_WRAP_H 27 | #define MBEDTLS_CIPHER_WRAP_H 28 | 29 | #if !defined(MBEDTLS_CONFIG_FILE) 30 | #include "config.h" 31 | #else 32 | #include MBEDTLS_CONFIG_FILE 33 | #endif 34 | 35 | #include "cipher.h" 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /** 42 | * Base cipher information. The non-mode specific functions and values. 43 | */ 44 | struct mbedtls_cipher_base_t 45 | { 46 | /** Base Cipher type (e.g. MBEDTLS_CIPHER_ID_AES) */ 47 | mbedtls_cipher_id_t cipher; 48 | 49 | /** Encrypt using ECB */ 50 | int (*ecb_func)( void *ctx, mbedtls_operation_t mode, 51 | const unsigned char *input, unsigned char *output ); 52 | 53 | #if defined(MBEDTLS_CIPHER_MODE_CBC) 54 | /** Encrypt using CBC */ 55 | int (*cbc_func)( void *ctx, mbedtls_operation_t mode, size_t length, 56 | unsigned char *iv, const unsigned char *input, 57 | unsigned char *output ); 58 | #endif 59 | 60 | #if defined(MBEDTLS_CIPHER_MODE_CFB) 61 | /** Encrypt using CFB (Full length) */ 62 | int (*cfb_func)( void *ctx, mbedtls_operation_t mode, size_t length, size_t *iv_off, 63 | unsigned char *iv, const unsigned char *input, 64 | unsigned char *output ); 65 | #endif 66 | 67 | #if defined(MBEDTLS_CIPHER_MODE_OFB) 68 | /** Encrypt using OFB (Full length) */ 69 | int (*ofb_func)( void *ctx, size_t length, size_t *iv_off, 70 | unsigned char *iv, 71 | const unsigned char *input, 72 | unsigned char *output ); 73 | #endif 74 | 75 | #if defined(MBEDTLS_CIPHER_MODE_CTR) 76 | /** Encrypt using CTR */ 77 | int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, 78 | unsigned char *nonce_counter, unsigned char *stream_block, 79 | const unsigned char *input, unsigned char *output ); 80 | #endif 81 | 82 | #if defined(MBEDTLS_CIPHER_MODE_XTS) 83 | /** Encrypt or decrypt using XTS. */ 84 | int (*xts_func)( void *ctx, mbedtls_operation_t mode, size_t length, 85 | const unsigned char data_unit[16], 86 | const unsigned char *input, unsigned char *output ); 87 | #endif 88 | 89 | #if defined(MBEDTLS_CIPHER_MODE_STREAM) 90 | /** Encrypt using STREAM */ 91 | int (*stream_func)( void *ctx, size_t length, 92 | const unsigned char *input, unsigned char *output ); 93 | #endif 94 | 95 | /** Set key for encryption purposes */ 96 | int (*setkey_enc_func)( void *ctx, const unsigned char *key, 97 | unsigned int key_bitlen ); 98 | 99 | /** Set key for decryption purposes */ 100 | int (*setkey_dec_func)( void *ctx, const unsigned char *key, 101 | unsigned int key_bitlen); 102 | 103 | /** Allocate a new context */ 104 | void * (*ctx_alloc_func)( void ); 105 | 106 | /** Free the given context */ 107 | void (*ctx_free_func)( void *ctx ); 108 | 109 | }; 110 | 111 | typedef struct 112 | { 113 | mbedtls_cipher_type_t type; 114 | const mbedtls_cipher_info_t *info; 115 | } mbedtls_cipher_definition_t; 116 | 117 | extern const mbedtls_cipher_definition_t mbedtls_cipher_definitions[]; 118 | 119 | extern int mbedtls_cipher_supported[]; 120 | 121 | #ifdef __cplusplus 122 | } 123 | #endif 124 | 125 | #endif /* MBEDTLS_CIPHER_WRAP_H */ 126 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/entropy_poll.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file entropy_poll.h 3 | * 4 | * \brief Platform-specific and custom entropy polling functions 5 | */ 6 | /* 7 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_ENTROPY_POLL_H 25 | #define MBEDTLS_ENTROPY_POLL_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | /* 40 | * Default thresholds for built-in sources, in bytes 41 | */ 42 | #define MBEDTLS_ENTROPY_MIN_PLATFORM 32 /**< Minimum for platform source */ 43 | #define MBEDTLS_ENTROPY_MIN_HAVEGE 32 /**< Minimum for HAVEGE */ 44 | #define MBEDTLS_ENTROPY_MIN_HARDCLOCK 4 /**< Minimum for mbedtls_timing_hardclock() */ 45 | #if !defined(MBEDTLS_ENTROPY_MIN_HARDWARE) 46 | #define MBEDTLS_ENTROPY_MIN_HARDWARE 32 /**< Minimum for the hardware source */ 47 | #endif 48 | 49 | /** 50 | * \brief Entropy poll callback that provides 0 entropy. 51 | */ 52 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) 53 | int mbedtls_null_entropy_poll( void *data, 54 | unsigned char *output, size_t len, size_t *olen ); 55 | #endif 56 | 57 | #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) 58 | /** 59 | * \brief Platform-specific entropy poll callback 60 | */ 61 | int mbedtls_platform_entropy_poll( void *data, 62 | unsigned char *output, size_t len, size_t *olen ); 63 | #endif 64 | 65 | #if defined(MBEDTLS_HAVEGE_C) 66 | /** 67 | * \brief HAVEGE based entropy poll callback 68 | * 69 | * Requires an HAVEGE state as its data pointer. 70 | */ 71 | int mbedtls_havege_poll( void *data, 72 | unsigned char *output, size_t len, size_t *olen ); 73 | #endif 74 | 75 | #if defined(MBEDTLS_TIMING_C) 76 | /** 77 | * \brief mbedtls_timing_hardclock-based entropy poll callback 78 | */ 79 | int mbedtls_hardclock_poll( void *data, 80 | unsigned char *output, size_t len, size_t *olen ); 81 | #endif 82 | 83 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) 84 | /** 85 | * \brief Entropy poll callback for a hardware source 86 | * 87 | * \warning This is not provided by mbed TLS! 88 | * See \c MBEDTLS_ENTROPY_HARDWARE_ALT in config.h. 89 | * 90 | * \note This must accept NULL as its first argument. 91 | */ 92 | int mbedtls_hardware_poll( void *data, 93 | unsigned char *output, size_t len, size_t *olen ); 94 | #endif 95 | 96 | #if defined(MBEDTLS_ENTROPY_NV_SEED) 97 | /** 98 | * \brief Entropy poll callback for a non-volatile seed file 99 | * 100 | * \note This must accept NULL as its first argument. 101 | */ 102 | int mbedtls_nv_seed_poll( void *data, 103 | unsigned char *output, size_t len, size_t *olen ); 104 | #endif 105 | 106 | #ifdef __cplusplus 107 | } 108 | #endif 109 | 110 | #endif /* entropy_poll.h */ 111 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/error.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file error.h 3 | * 4 | * \brief Error to string translation 5 | */ 6 | /* 7 | * Copyright (C) 2006-2018, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_ERROR_H 25 | #define MBEDTLS_ERROR_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include 34 | 35 | /** 36 | * Error code layout. 37 | * 38 | * Currently we try to keep all error codes within the negative space of 16 39 | * bits signed integers to support all platforms (-0x0001 - -0x7FFF). In 40 | * addition we'd like to give two layers of information on the error if 41 | * possible. 42 | * 43 | * For that purpose the error codes are segmented in the following manner: 44 | * 45 | * 16 bit error code bit-segmentation 46 | * 47 | * 1 bit - Unused (sign bit) 48 | * 3 bits - High level module ID 49 | * 5 bits - Module-dependent error code 50 | * 7 bits - Low level module errors 51 | * 52 | * For historical reasons, low-level error codes are divided in even and odd, 53 | * even codes were assigned first, and -1 is reserved for other errors. 54 | * 55 | * Low-level module errors (0x0002-0x007E, 0x0003-0x007F) 56 | * 57 | * Module Nr Codes assigned 58 | * MPI 7 0x0002-0x0010 59 | * GCM 3 0x0012-0x0014 0x0013-0x0013 60 | * BLOWFISH 3 0x0016-0x0018 0x0017-0x0017 61 | * THREADING 3 0x001A-0x001E 62 | * AES 5 0x0020-0x0022 0x0021-0x0025 63 | * CAMELLIA 3 0x0024-0x0026 0x0027-0x0027 64 | * XTEA 2 0x0028-0x0028 0x0029-0x0029 65 | * BASE64 2 0x002A-0x002C 66 | * OID 1 0x002E-0x002E 0x000B-0x000B 67 | * PADLOCK 1 0x0030-0x0030 68 | * DES 2 0x0032-0x0032 0x0033-0x0033 69 | * CTR_DBRG 4 0x0034-0x003A 70 | * ENTROPY 3 0x003C-0x0040 0x003D-0x003F 71 | * NET 13 0x0042-0x0052 0x0043-0x0049 72 | * ARIA 4 0x0058-0x005E 73 | * ASN1 7 0x0060-0x006C 74 | * CMAC 1 0x007A-0x007A 75 | * PBKDF2 1 0x007C-0x007C 76 | * HMAC_DRBG 4 0x0003-0x0009 77 | * CCM 3 0x000D-0x0011 78 | * ARC4 1 0x0019-0x0019 79 | * MD2 1 0x002B-0x002B 80 | * MD4 1 0x002D-0x002D 81 | * MD5 1 0x002F-0x002F 82 | * RIPEMD160 1 0x0031-0x0031 83 | * SHA1 1 0x0035-0x0035 0x0073-0x0073 84 | * SHA256 1 0x0037-0x0037 0x0074-0x0074 85 | * SHA512 1 0x0039-0x0039 0x0075-0x0075 86 | * CHACHA20 3 0x0051-0x0055 87 | * POLY1305 3 0x0057-0x005B 88 | * CHACHAPOLY 2 0x0054-0x0056 89 | * PLATFORM 1 0x0070-0x0072 90 | * 91 | * High-level module nr (3 bits - 0x0...-0x7...) 92 | * Name ID Nr of Errors 93 | * PEM 1 9 94 | * PKCS#12 1 4 (Started from top) 95 | * X509 2 20 96 | * PKCS5 2 4 (Started from top) 97 | * DHM 3 11 98 | * PK 3 15 (Started from top) 99 | * RSA 4 11 100 | * ECP 4 10 (Started from top) 101 | * MD 5 5 102 | * HKDF 5 1 (Started from top) 103 | * CIPHER 6 8 104 | * SSL 6 23 (Started from top) 105 | * SSL 7 32 106 | * 107 | * Module dependent error code (5 bits 0x.00.-0x.F8.) 108 | */ 109 | 110 | #ifdef __cplusplus 111 | extern "C" { 112 | #endif 113 | 114 | /** 115 | * \brief Translate a mbed TLS error code into a string representation, 116 | * Result is truncated if necessary and always includes a terminating 117 | * null byte. 118 | * 119 | * \param errnum error code 120 | * \param buffer buffer to place representation in 121 | * \param buflen length of the buffer 122 | */ 123 | void mbedtls_strerror( int errnum, char *buffer, size_t buflen ); 124 | 125 | #ifdef __cplusplus 126 | } 127 | #endif 128 | 129 | #endif /* error.h */ 130 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/havege.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file havege.h 3 | * 4 | * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_HAVEGE_H 25 | #define MBEDTLS_HAVEGE_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include 34 | 35 | #define MBEDTLS_HAVEGE_COLLECT_SIZE 1024 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /** 42 | * \brief HAVEGE state structure 43 | */ 44 | typedef struct mbedtls_havege_state 45 | { 46 | int PT1, PT2, offset[2]; 47 | int pool[MBEDTLS_HAVEGE_COLLECT_SIZE]; 48 | int WALK[8192]; 49 | } 50 | mbedtls_havege_state; 51 | 52 | /** 53 | * \brief HAVEGE initialization 54 | * 55 | * \param hs HAVEGE state to be initialized 56 | */ 57 | void mbedtls_havege_init( mbedtls_havege_state *hs ); 58 | 59 | /** 60 | * \brief Clear HAVEGE state 61 | * 62 | * \param hs HAVEGE state to be cleared 63 | */ 64 | void mbedtls_havege_free( mbedtls_havege_state *hs ); 65 | 66 | /** 67 | * \brief HAVEGE rand function 68 | * 69 | * \param p_rng A HAVEGE state 70 | * \param output Buffer to fill 71 | * \param len Length of buffer 72 | * 73 | * \return 0 74 | */ 75 | int mbedtls_havege_random( void *p_rng, unsigned char *output, size_t len ); 76 | 77 | #ifdef __cplusplus 78 | } 79 | #endif 80 | 81 | #endif /* havege.h */ 82 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/md_internal.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file md_internal.h 3 | * 4 | * \brief Message digest wrappers. 5 | * 6 | * \warning This in an internal header. Do not include directly. 7 | * 8 | * \author Adriaan de Jong 9 | */ 10 | /* 11 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 12 | * SPDX-License-Identifier: Apache-2.0 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 15 | * not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at 17 | * 18 | * http://www.apache.org/licenses/LICENSE-2.0 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | * This file is part of mbed TLS (https://tls.mbed.org) 27 | */ 28 | #ifndef MBEDTLS_MD_WRAP_H 29 | #define MBEDTLS_MD_WRAP_H 30 | 31 | #if !defined(MBEDTLS_CONFIG_FILE) 32 | #include "config.h" 33 | #else 34 | #include MBEDTLS_CONFIG_FILE 35 | #endif 36 | 37 | #include "md.h" 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /** 44 | * Message digest information. 45 | * Allows message digest functions to be called in a generic way. 46 | */ 47 | struct mbedtls_md_info_t 48 | { 49 | /** Digest identifier */ 50 | mbedtls_md_type_t type; 51 | 52 | /** Name of the message digest */ 53 | const char * name; 54 | 55 | /** Output length of the digest function in bytes */ 56 | int size; 57 | 58 | /** Block length of the digest function in bytes */ 59 | int block_size; 60 | 61 | /** Digest initialisation function */ 62 | int (*starts_func)( void *ctx ); 63 | 64 | /** Digest update function */ 65 | int (*update_func)( void *ctx, const unsigned char *input, size_t ilen ); 66 | 67 | /** Digest finalisation function */ 68 | int (*finish_func)( void *ctx, unsigned char *output ); 69 | 70 | /** Generic digest function */ 71 | int (*digest_func)( const unsigned char *input, size_t ilen, 72 | unsigned char *output ); 73 | 74 | /** Allocate a new context */ 75 | void * (*ctx_alloc_func)( void ); 76 | 77 | /** Free the given context */ 78 | void (*ctx_free_func)( void *ctx ); 79 | 80 | /** Clone state from a context */ 81 | void (*clone_func)( void *dst, const void *src ); 82 | 83 | /** Internal use only */ 84 | int (*process_func)( void *ctx, const unsigned char *input ); 85 | }; 86 | 87 | #if defined(MBEDTLS_MD2_C) 88 | extern const mbedtls_md_info_t mbedtls_md2_info; 89 | #endif 90 | #if defined(MBEDTLS_MD4_C) 91 | extern const mbedtls_md_info_t mbedtls_md4_info; 92 | #endif 93 | #if defined(MBEDTLS_MD5_C) 94 | extern const mbedtls_md_info_t mbedtls_md5_info; 95 | #endif 96 | #if defined(MBEDTLS_RIPEMD160_C) 97 | extern const mbedtls_md_info_t mbedtls_ripemd160_info; 98 | #endif 99 | #if defined(MBEDTLS_SHA1_C) 100 | extern const mbedtls_md_info_t mbedtls_sha1_info; 101 | #endif 102 | #if defined(MBEDTLS_SHA256_C) 103 | extern const mbedtls_md_info_t mbedtls_sha224_info; 104 | extern const mbedtls_md_info_t mbedtls_sha256_info; 105 | #endif 106 | #if defined(MBEDTLS_SHA512_C) 107 | extern const mbedtls_md_info_t mbedtls_sha384_info; 108 | extern const mbedtls_md_info_t mbedtls_sha512_info; 109 | #endif 110 | 111 | #ifdef __cplusplus 112 | } 113 | #endif 114 | 115 | #endif /* MBEDTLS_MD_WRAP_H */ 116 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/net.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file net.h 3 | * 4 | * \brief Deprecated header file that includes net_sockets.h 5 | * 6 | * \deprecated Superseded by mbedtls/net_sockets.h 7 | */ 8 | /* 9 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * http://www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | * 24 | * This file is part of mbed TLS (https://tls.mbed.org) 25 | */ 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) 33 | #include "net_sockets.h" 34 | #if defined(MBEDTLS_DEPRECATED_WARNING) 35 | #warning "Deprecated header file: Superseded by mbedtls/net_sockets.h" 36 | #endif /* MBEDTLS_DEPRECATED_WARNING */ 37 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ 38 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/padlock.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file padlock.h 3 | * 4 | * \brief VIA PadLock ACE for HW encryption/decryption supported by some 5 | * processors 6 | * 7 | * \warning These functions are only for internal use by other library 8 | * functions; you must not call them directly. 9 | */ 10 | /* 11 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 12 | * SPDX-License-Identifier: Apache-2.0 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 15 | * not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at 17 | * 18 | * http://www.apache.org/licenses/LICENSE-2.0 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | * This file is part of mbed TLS (https://tls.mbed.org) 27 | */ 28 | #ifndef MBEDTLS_PADLOCK_H 29 | #define MBEDTLS_PADLOCK_H 30 | 31 | #if !defined(MBEDTLS_CONFIG_FILE) 32 | #include "config.h" 33 | #else 34 | #include MBEDTLS_CONFIG_FILE 35 | #endif 36 | 37 | #include "aes.h" 38 | 39 | #define MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED -0x0030 /**< Input data should be aligned. */ 40 | 41 | #if defined(__has_feature) 42 | #if __has_feature(address_sanitizer) 43 | #define MBEDTLS_HAVE_ASAN 44 | #endif 45 | #endif 46 | 47 | /* Some versions of ASan result in errors about not enough registers */ 48 | #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && defined(__i386__) && \ 49 | !defined(MBEDTLS_HAVE_ASAN) 50 | 51 | #ifndef MBEDTLS_HAVE_X86 52 | #define MBEDTLS_HAVE_X86 53 | #endif 54 | 55 | #include 56 | 57 | #define MBEDTLS_PADLOCK_RNG 0x000C 58 | #define MBEDTLS_PADLOCK_ACE 0x00C0 59 | #define MBEDTLS_PADLOCK_PHE 0x0C00 60 | #define MBEDTLS_PADLOCK_PMM 0x3000 61 | 62 | #define MBEDTLS_PADLOCK_ALIGN16(x) (uint32_t *) (16 + ((int32_t) (x) & ~15)) 63 | 64 | #ifdef __cplusplus 65 | extern "C" { 66 | #endif 67 | 68 | /** 69 | * \brief Internal PadLock detection routine 70 | * 71 | * \note This function is only for internal use by other library 72 | * functions; you must not call it directly. 73 | * 74 | * \param feature The feature to detect 75 | * 76 | * \return 1 if CPU has support for the feature, 0 otherwise 77 | */ 78 | int mbedtls_padlock_has_support( int feature ); 79 | 80 | /** 81 | * \brief Internal PadLock AES-ECB block en(de)cryption 82 | * 83 | * \note This function is only for internal use by other library 84 | * functions; you must not call it directly. 85 | * 86 | * \param ctx AES context 87 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT 88 | * \param input 16-byte input block 89 | * \param output 16-byte output block 90 | * 91 | * \return 0 if success, 1 if operation failed 92 | */ 93 | int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx, 94 | int mode, 95 | const unsigned char input[16], 96 | unsigned char output[16] ); 97 | 98 | /** 99 | * \brief Internal PadLock AES-CBC buffer en(de)cryption 100 | * 101 | * \note This function is only for internal use by other library 102 | * functions; you must not call it directly. 103 | * 104 | * \param ctx AES context 105 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT 106 | * \param length length of the input data 107 | * \param iv initialization vector (updated after use) 108 | * \param input buffer holding the input data 109 | * \param output buffer holding the output data 110 | * 111 | * \return 0 if success, 1 if operation failed 112 | */ 113 | int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx, 114 | int mode, 115 | size_t length, 116 | unsigned char iv[16], 117 | const unsigned char *input, 118 | unsigned char *output ); 119 | 120 | #ifdef __cplusplus 121 | } 122 | #endif 123 | 124 | #endif /* HAVE_X86 */ 125 | 126 | #endif /* padlock.h */ 127 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/pkcs5.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file pkcs5.h 3 | * 4 | * \brief PKCS#5 functions 5 | * 6 | * \author Mathias Olsson 7 | */ 8 | /* 9 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * http://www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | * 24 | * This file is part of mbed TLS (https://tls.mbed.org) 25 | */ 26 | #ifndef MBEDTLS_PKCS5_H 27 | #define MBEDTLS_PKCS5_H 28 | 29 | #if !defined(MBEDTLS_CONFIG_FILE) 30 | #include "config.h" 31 | #else 32 | #include MBEDTLS_CONFIG_FILE 33 | #endif 34 | 35 | #include "asn1.h" 36 | #include "md.h" 37 | 38 | #include 39 | #include 40 | 41 | #define MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA -0x2f80 /**< Bad input parameters to function. */ 42 | #define MBEDTLS_ERR_PKCS5_INVALID_FORMAT -0x2f00 /**< Unexpected ASN.1 data. */ 43 | #define MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE -0x2e80 /**< Requested encryption or digest alg not available. */ 44 | #define MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH -0x2e00 /**< Given private key password does not allow for correct decryption. */ 45 | 46 | #define MBEDTLS_PKCS5_DECRYPT 0 47 | #define MBEDTLS_PKCS5_ENCRYPT 1 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif 52 | 53 | #if defined(MBEDTLS_ASN1_PARSE_C) 54 | 55 | /** 56 | * \brief PKCS#5 PBES2 function 57 | * 58 | * \param pbe_params the ASN.1 algorithm parameters 59 | * \param mode either MBEDTLS_PKCS5_DECRYPT or MBEDTLS_PKCS5_ENCRYPT 60 | * \param pwd password to use when generating key 61 | * \param pwdlen length of password 62 | * \param data data to process 63 | * \param datalen length of data 64 | * \param output output buffer 65 | * 66 | * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails. 67 | */ 68 | int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode, 69 | const unsigned char *pwd, size_t pwdlen, 70 | const unsigned char *data, size_t datalen, 71 | unsigned char *output ); 72 | 73 | #endif /* MBEDTLS_ASN1_PARSE_C */ 74 | 75 | /** 76 | * \brief PKCS#5 PBKDF2 using HMAC 77 | * 78 | * \param ctx Generic HMAC context 79 | * \param password Password to use when generating key 80 | * \param plen Length of password 81 | * \param salt Salt to use when generating key 82 | * \param slen Length of salt 83 | * \param iteration_count Iteration count 84 | * \param key_length Length of generated key in bytes 85 | * \param output Generated key. Must be at least as big as key_length 86 | * 87 | * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails. 88 | */ 89 | int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *password, 90 | size_t plen, const unsigned char *salt, size_t slen, 91 | unsigned int iteration_count, 92 | uint32_t key_length, unsigned char *output ); 93 | 94 | #if defined(MBEDTLS_SELF_TEST) 95 | 96 | /** 97 | * \brief Checkup routine 98 | * 99 | * \return 0 if successful, or 1 if the test failed 100 | */ 101 | int mbedtls_pkcs5_self_test( int verbose ); 102 | 103 | #endif /* MBEDTLS_SELF_TEST */ 104 | 105 | #ifdef __cplusplus 106 | } 107 | #endif 108 | 109 | #endif /* pkcs5.h */ 110 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/platform_time.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file platform_time.h 3 | * 4 | * \brief mbed TLS Platform time abstraction 5 | */ 6 | /* 7 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_PLATFORM_TIME_H 25 | #define MBEDTLS_PLATFORM_TIME_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | /** 38 | * \name SECTION: Module settings 39 | * 40 | * The configuration options you can set for this module are in this section. 41 | * Either change them in config.h or define them on the compiler command line. 42 | * \{ 43 | */ 44 | 45 | /* 46 | * The time_t datatype 47 | */ 48 | #if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO) 49 | typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t; 50 | #else 51 | /* For time_t */ 52 | #include 53 | typedef time_t mbedtls_time_t; 54 | #endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */ 55 | 56 | /* 57 | * The function pointers for time 58 | */ 59 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) 60 | extern mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* time ); 61 | 62 | /** 63 | * \brief Set your own time function pointer 64 | * 65 | * \param time_func the time function implementation 66 | * 67 | * \return 0 68 | */ 69 | int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* time ) ); 70 | #else 71 | #if defined(MBEDTLS_PLATFORM_TIME_MACRO) 72 | #define mbedtls_time MBEDTLS_PLATFORM_TIME_MACRO 73 | #else 74 | #define mbedtls_time time 75 | #endif /* MBEDTLS_PLATFORM_TIME_MACRO */ 76 | #endif /* MBEDTLS_PLATFORM_TIME_ALT */ 77 | 78 | #ifdef __cplusplus 79 | } 80 | #endif 81 | 82 | #endif /* platform_time.h */ 83 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/ssl_cookie.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file ssl_cookie.h 3 | * 4 | * \brief DTLS cookie callbacks implementation 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_SSL_COOKIE_H 25 | #define MBEDTLS_SSL_COOKIE_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include "ssl.h" 34 | 35 | #if defined(MBEDTLS_THREADING_C) 36 | #include "threading.h" 37 | #endif 38 | 39 | /** 40 | * \name SECTION: Module settings 41 | * 42 | * The configuration options you can set for this module are in this section. 43 | * Either change them in config.h or define them on the compiler command line. 44 | * \{ 45 | */ 46 | #ifndef MBEDTLS_SSL_COOKIE_TIMEOUT 47 | #define MBEDTLS_SSL_COOKIE_TIMEOUT 60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */ 48 | #endif 49 | 50 | /* \} name SECTION: Module settings */ 51 | 52 | #ifdef __cplusplus 53 | extern "C" { 54 | #endif 55 | 56 | /** 57 | * \brief Context for the default cookie functions. 58 | */ 59 | typedef struct mbedtls_ssl_cookie_ctx 60 | { 61 | mbedtls_md_context_t hmac_ctx; /*!< context for the HMAC portion */ 62 | #if !defined(MBEDTLS_HAVE_TIME) 63 | unsigned long serial; /*!< serial number for expiration */ 64 | #endif 65 | unsigned long timeout; /*!< timeout delay, in seconds if HAVE_TIME, 66 | or in number of tickets issued */ 67 | 68 | #if defined(MBEDTLS_THREADING_C) 69 | mbedtls_threading_mutex_t mutex; 70 | #endif 71 | } mbedtls_ssl_cookie_ctx; 72 | 73 | /** 74 | * \brief Initialize cookie context 75 | */ 76 | void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx ); 77 | 78 | /** 79 | * \brief Setup cookie context (generate keys) 80 | */ 81 | int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx, 82 | int (*f_rng)(void *, unsigned char *, size_t), 83 | void *p_rng ); 84 | 85 | /** 86 | * \brief Set expiration delay for cookies 87 | * (Default MBEDTLS_SSL_COOKIE_TIMEOUT) 88 | * 89 | * \param ctx Cookie contex 90 | * \param delay Delay, in seconds if HAVE_TIME, or in number of cookies 91 | * issued in the meantime. 92 | * 0 to disable expiration (NOT recommended) 93 | */ 94 | void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay ); 95 | 96 | /** 97 | * \brief Free cookie context 98 | */ 99 | void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx ); 100 | 101 | /** 102 | * \brief Generate cookie, see \c mbedtls_ssl_cookie_write_t 103 | */ 104 | mbedtls_ssl_cookie_write_t mbedtls_ssl_cookie_write; 105 | 106 | /** 107 | * \brief Verify cookie, see \c mbedtls_ssl_cookie_write_t 108 | */ 109 | mbedtls_ssl_cookie_check_t mbedtls_ssl_cookie_check; 110 | 111 | #ifdef __cplusplus 112 | } 113 | #endif 114 | 115 | #endif /* ssl_cookie.h */ 116 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/version.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file version.h 3 | * 4 | * \brief Run-time version information 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | /* 25 | * This set of compile-time defines and run-time variables can be used to 26 | * determine the version number of the mbed TLS library used. 27 | */ 28 | #ifndef MBEDTLS_VERSION_H 29 | #define MBEDTLS_VERSION_H 30 | 31 | #if !defined(MBEDTLS_CONFIG_FILE) 32 | #include "config.h" 33 | #else 34 | #include MBEDTLS_CONFIG_FILE 35 | #endif 36 | 37 | /** 38 | * The version number x.y.z is split into three parts. 39 | * Major, Minor, Patchlevel 40 | */ 41 | #define MBEDTLS_VERSION_MAJOR 2 42 | #define MBEDTLS_VERSION_MINOR 16 43 | #define MBEDTLS_VERSION_PATCH 3 44 | 45 | /** 46 | * The single version number has the following structure: 47 | * MMNNPP00 48 | * Major version | Minor version | Patch version 49 | */ 50 | #define MBEDTLS_VERSION_NUMBER 0x02100300 51 | #define MBEDTLS_VERSION_STRING "2.16.3" 52 | #define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.16.3" 53 | 54 | #if defined(MBEDTLS_VERSION_C) 55 | 56 | #ifdef __cplusplus 57 | extern "C" { 58 | #endif 59 | 60 | /** 61 | * Get the version number. 62 | * 63 | * \return The constructed version number in the format 64 | * MMNNPP00 (Major, Minor, Patch). 65 | */ 66 | unsigned int mbedtls_version_get_number( void ); 67 | 68 | /** 69 | * Get the version string ("x.y.z"). 70 | * 71 | * \param string The string that will receive the value. 72 | * (Should be at least 9 bytes in size) 73 | */ 74 | void mbedtls_version_get_string( char *string ); 75 | 76 | /** 77 | * Get the full version string ("mbed TLS x.y.z"). 78 | * 79 | * \param string The string that will receive the value. The mbed TLS version 80 | * string will use 18 bytes AT MOST including a terminating 81 | * null byte. 82 | * (So the buffer should be at least 18 bytes to receive this 83 | * version string). 84 | */ 85 | void mbedtls_version_get_string_full( char *string ); 86 | 87 | /** 88 | * \brief Check if support for a feature was compiled into this 89 | * mbed TLS binary. This allows you to see at runtime if the 90 | * library was for instance compiled with or without 91 | * Multi-threading support. 92 | * 93 | * \note only checks against defines in the sections "System 94 | * support", "mbed TLS modules" and "mbed TLS feature 95 | * support" in config.h 96 | * 97 | * \param feature The string for the define to check (e.g. "MBEDTLS_AES_C") 98 | * 99 | * \return 0 if the feature is present, 100 | * -1 if the feature is not present and 101 | * -2 if support for feature checking as a whole was not 102 | * compiled in. 103 | */ 104 | int mbedtls_version_check_feature( const char *feature ); 105 | 106 | #ifdef __cplusplus 107 | } 108 | #endif 109 | 110 | #endif /* MBEDTLS_VERSION_C */ 111 | 112 | #endif /* version.h */ 113 | -------------------------------------------------------------------------------- /3pp/mbedtls/include/mbedtls/xtea.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file xtea.h 3 | * 4 | * \brief XTEA block cipher (32-bit) 5 | */ 6 | /* 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_XTEA_H 25 | #define MBEDTLS_XTEA_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include 34 | #include 35 | 36 | #define MBEDTLS_XTEA_ENCRYPT 1 37 | #define MBEDTLS_XTEA_DECRYPT 0 38 | 39 | #define MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */ 40 | 41 | /* MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED is deprecated and should not be used. */ 42 | #define MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED -0x0029 /**< XTEA hardware accelerator failed. */ 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | #if !defined(MBEDTLS_XTEA_ALT) 49 | // Regular implementation 50 | // 51 | 52 | /** 53 | * \brief XTEA context structure 54 | */ 55 | typedef struct mbedtls_xtea_context 56 | { 57 | uint32_t k[4]; /*!< key */ 58 | } 59 | mbedtls_xtea_context; 60 | 61 | #else /* MBEDTLS_XTEA_ALT */ 62 | #include "xtea_alt.h" 63 | #endif /* MBEDTLS_XTEA_ALT */ 64 | 65 | /** 66 | * \brief Initialize XTEA context 67 | * 68 | * \param ctx XTEA context to be initialized 69 | */ 70 | void mbedtls_xtea_init( mbedtls_xtea_context *ctx ); 71 | 72 | /** 73 | * \brief Clear XTEA context 74 | * 75 | * \param ctx XTEA context to be cleared 76 | */ 77 | void mbedtls_xtea_free( mbedtls_xtea_context *ctx ); 78 | 79 | /** 80 | * \brief XTEA key schedule 81 | * 82 | * \param ctx XTEA context to be initialized 83 | * \param key the secret key 84 | */ 85 | void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] ); 86 | 87 | /** 88 | * \brief XTEA cipher function 89 | * 90 | * \param ctx XTEA context 91 | * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT 92 | * \param input 8-byte input block 93 | * \param output 8-byte output block 94 | * 95 | * \return 0 if successful 96 | */ 97 | int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, 98 | int mode, 99 | const unsigned char input[8], 100 | unsigned char output[8] ); 101 | 102 | #if defined(MBEDTLS_CIPHER_MODE_CBC) 103 | /** 104 | * \brief XTEA CBC cipher function 105 | * 106 | * \param ctx XTEA context 107 | * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT 108 | * \param length the length of input, multiple of 8 109 | * \param iv initialization vector for CBC mode 110 | * \param input input block 111 | * \param output output block 112 | * 113 | * \return 0 if successful, 114 | * MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0 115 | */ 116 | int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, 117 | int mode, 118 | size_t length, 119 | unsigned char iv[8], 120 | const unsigned char *input, 121 | unsigned char *output); 122 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ 123 | 124 | #if defined(MBEDTLS_SELF_TEST) 125 | 126 | /** 127 | * \brief Checkup routine 128 | * 129 | * \return 0 if successful, or 1 if the test failed 130 | */ 131 | int mbedtls_xtea_self_test( int verbose ); 132 | 133 | #endif /* MBEDTLS_SELF_TEST */ 134 | 135 | #ifdef __cplusplus 136 | } 137 | #endif 138 | 139 | #endif /* xtea.h */ 140 | -------------------------------------------------------------------------------- /3pp/mbedtls/library/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | libmbed* 3 | *.sln 4 | *.vcxproj 5 | -------------------------------------------------------------------------------- /3pp/mbedtls/library/version.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Version information 3 | * 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 | * not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * This file is part of mbed TLS (https://tls.mbed.org) 20 | */ 21 | 22 | #if !defined(MBEDTLS_CONFIG_FILE) 23 | #include "mbedtls/config.h" 24 | #else 25 | #include MBEDTLS_CONFIG_FILE 26 | #endif 27 | 28 | #if defined(MBEDTLS_VERSION_C) 29 | 30 | #include "mbedtls/version.h" 31 | #include 32 | 33 | unsigned int mbedtls_version_get_number( void ) 34 | { 35 | return( MBEDTLS_VERSION_NUMBER ); 36 | } 37 | 38 | void mbedtls_version_get_string( char *string ) 39 | { 40 | memcpy( string, MBEDTLS_VERSION_STRING, 41 | sizeof( MBEDTLS_VERSION_STRING ) ); 42 | } 43 | 44 | void mbedtls_version_get_string_full( char *string ) 45 | { 46 | memcpy( string, MBEDTLS_VERSION_STRING_FULL, 47 | sizeof( MBEDTLS_VERSION_STRING_FULL ) ); 48 | } 49 | 50 | #endif /* MBEDTLS_VERSION_C */ 51 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Erik Moqvist 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: examples library install 2 | 3 | BUILD ?= build 4 | LIBRARY = $(BUILD)/libasync.a 5 | PREFIX ?= /usr/local 6 | VERSION = $(shell grep ASYNC_VERSION include/async/core.h | awk '{print $$3}' | tr -d '"') 7 | 8 | default: run 9 | 10 | all: run examples 11 | 12 | run: 13 | $(MAKE) -C tst 14 | 15 | test: 16 | $(MAKE) -C tst 17 | 18 | examples: 19 | $(MAKE) -C examples/timers build 20 | $(MAKE) -C examples/conversation build 21 | $(MAKE) -C examples/shell build 22 | $(MAKE) -C examples/mqtt_client build 23 | $(MAKE) -C examples/stcp_echo_client build 24 | $(MAKE) -C examples/hello_world build 25 | $(MAKE) -C examples/counter build 26 | $(MAKE) -C examples/http_get build 27 | $(MAKE) -C examples/call_worker_pool build 28 | $(MAKE) -C examples/call_threadsafe build 29 | $(MAKE) -C examples/ping_pong build 30 | 31 | clean: 32 | rm -rf $(BUILD) 33 | $(MAKE) -C tst clean 34 | $(MAKE) -C examples/timers clean 35 | $(MAKE) -C examples/conversation clean 36 | $(MAKE) -C examples/shell clean 37 | $(MAKE) -C examples/mqtt_client clean 38 | $(MAKE) -C examples/stcp_echo_client clean 39 | $(MAKE) -C examples/hello_world clean 40 | $(MAKE) -C examples/counter clean 41 | $(MAKE) -C examples/http_get clean 42 | $(MAKE) -C examples/call_worker_pool clean 43 | $(MAKE) -C examples/call_threadsafe clean 44 | $(MAKE) -C examples/ping_pong clean 45 | 46 | release: 47 | rm -rf async-core-$(VERSION) 48 | mkdir async-core-$(VERSION) 49 | cp -r --parents include/async/core* async-core-$(VERSION) 50 | cp -r --parents include/async/utils* async-core-$(VERSION) 51 | cp -r --parents src/core async-core-$(VERSION) 52 | cp -r --parents src/utils async-core-$(VERSION) 53 | tar czf async-core-$(VERSION).tar.gz async-core-$(VERSION) 54 | 55 | release-compile: 56 | cd async-core-$(VERSION) && \ 57 | for f in $$(find -name "*.c") ; do \ 58 | gcc -c -Iinclude $$f ; \ 59 | done 60 | 61 | library: $(LIBRARY) 62 | 63 | install: 64 | find include -type f -exec install -Dm 644 "{}" "$(PREFIX)/{}" \; 65 | mkdir -p $(PREFIX)/lib 66 | install -m 644 $(LIBRARY) $(PREFIX)/lib 67 | 68 | include make/library.mk 69 | 70 | $(LIBRARY): $(OBJ) 71 | mkdir -p $(BUILD) 72 | $(AR) cr $(LIBRARY) $^ 73 | 74 | help: 75 | @echo "TARGET DESCRIPTION" 76 | @echo "------------------------------------------------------" 77 | @echo "all run + examples" 78 | @echo "run Build and run all tests." 79 | @echo "test run + coverage report." 80 | @echo "examples Build all examples." 81 | @echo "clean Remove build and run files." 82 | @echo "release Create a release." 83 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | |buildstatus|_ 2 | |codecov|_ 3 | |nala|_ 4 | 5 | 🔀 Async 6 | ======= 7 | 8 | Asynchronous framework in C for systems where low memory usage is 9 | important. 10 | 11 | Features 12 | ======== 13 | 14 | - Delayed (asynchronous) function calls. 15 | 16 | - Timers. 17 | 18 | - An MQTT client (only QoS 0 is supported). 19 | 20 | - A simple shell. 21 | 22 | - TCP client and server. 23 | 24 | - Secure communication with SSL/TLS. 25 | 26 | Project homepage: https://github.com/eerimoq/async 27 | 28 | Releases: https://github.com/eerimoq/async/releases 29 | 30 | Examples 31 | ======== 32 | 33 | The hello world example, printing 'Hello world!' periodically. 34 | 35 | .. code-block:: c 36 | 37 | #include 38 | #include "async.h" 39 | 40 | static void on_timeout() 41 | { 42 | printf("Hello world!\n"); 43 | } 44 | 45 | int main() 46 | { 47 | struct async_t async; 48 | struct async_timer_t timer; 49 | 50 | async_init(&async); 51 | async_set_runtime(&async, async_runtime_create()); 52 | async_timer_init(&timer, on_timeout, NULL, 0, 1000, &async); 53 | async_timer_start(&timer); 54 | async_run_forever(&async); 55 | 56 | return (0); 57 | } 58 | 59 | There are more examples in the `examples folder`_. 60 | 61 | Runtimes 62 | ======== 63 | 64 | A runtime implements zero or more of the following features: 65 | 66 | - Timer handling. 67 | 68 | - Networking (TCP). 69 | 70 | - Call functions in the worker pool. 71 | 72 | - Call functions from any thread. 73 | 74 | Default 75 | ------- 76 | 77 | The default runtime does not implement any runtime features. It's 78 | designed for minimal dependencies and easy integration in any 79 | application. 80 | 81 | Typical usage: 82 | 83 | .. code-block:: c 84 | 85 | async_init(&async); 86 | ... 87 | while (true) { 88 | epoll_wait(...); 89 | ... 90 | if (timeout) { 91 | async_tick(&async); 92 | } 93 | async_process(&async); 94 | } 95 | 96 | Native 97 | ------ 98 | 99 | The native runtime implements all runtime features. 100 | 101 | Typical usage: 102 | 103 | .. code-block:: c 104 | 105 | async_init(&async); 106 | async_set_runtime(&async, async_runtime_create()); 107 | ... 108 | async_run_forever(&async); 109 | 110 | Design 111 | ====== 112 | 113 | Input 114 | ----- 115 | 116 | First ``*_input(self_p)`` is called to signal that data is 117 | available. Then read data with ``*_read(self_p, buf_p, size)``. 118 | 119 | Output 120 | ------ 121 | 122 | Write data with ``*_write(self_p, buf_p, size)``. 123 | 124 | Unit testing 125 | ============ 126 | 127 | Source the development environment setup script. 128 | 129 | .. code-block:: shell 130 | 131 | $ source setup.sh 132 | 133 | Execute all unit tests. 134 | 135 | .. code-block:: shell 136 | 137 | $ make -s -j4 test 138 | ... 139 | 140 | Execute tests matching given pattern. 141 | 142 | .. code-block:: shell 143 | 144 | $ make -s -j4 ARGS=core_timer test 145 | ... 146 | 147 | .. |buildstatus| image:: https://travis-ci.org/eerimoq/async.svg?branch=master 148 | .. _buildstatus: https://travis-ci.org/eerimoq/async 149 | 150 | .. |codecov| image:: https://codecov.io/gh/eerimoq/async/branch/master/graph/badge.svg 151 | .. _codecov: https://codecov.io/gh/eerimoq/async 152 | 153 | .. |nala| image:: https://img.shields.io/badge/nala-test-blue.svg 154 | .. _nala: https://github.com/eerimoq/nala 155 | 156 | .. _examples folder: https://github.com/eerimoq/async/tree/master/examples 157 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | ignore: 2 | - "tst" 3 | - "3pp" 4 | -------------------------------------------------------------------------------- /examples/call_threadsafe/Makefile: -------------------------------------------------------------------------------- 1 | include $(ASYNC_ROOT)/make/app.mk 2 | -------------------------------------------------------------------------------- /examples/call_threadsafe/README.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | Showcase the threadsafe call function. 5 | 6 | Compile and run 7 | =============== 8 | 9 | .. code-block:: text 10 | 11 | $ make -s 12 | ... 13 | Called! 14 | Called! 15 | Called! 16 | Called! 17 | Called! 18 | -------------------------------------------------------------------------------- /examples/call_threadsafe/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include 31 | #include "async.h" 32 | 33 | static void called(void *obj_p, void *arg_p) 34 | { 35 | (void)obj_p; 36 | (void)arg_p; 37 | 38 | printf("Called!\n"); 39 | } 40 | 41 | static void caller(struct async_t *async_p, void *arg_p) 42 | { 43 | (void)arg_p; 44 | 45 | while (true) { 46 | async_call_threadsafe(async_p, called, NULL, NULL); 47 | sleep(1); 48 | } 49 | } 50 | 51 | int main() 52 | { 53 | struct async_t async; 54 | 55 | async_init(&async); 56 | async_set_runtime(&async, async_runtime_create()); 57 | async_call_worker_pool(&async, (async_func_t)caller, &async, NULL, NULL); 58 | async_run_forever(&async); 59 | 60 | return (0); 61 | } 62 | -------------------------------------------------------------------------------- /examples/call_worker_pool/Makefile: -------------------------------------------------------------------------------- 1 | include $(ASYNC_ROOT)/make/app.mk 2 | -------------------------------------------------------------------------------- /examples/call_worker_pool/README.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | Call a function that sleeps for 5 seconds in the worker pool. In 5 | parallel a periodic timer is running. 6 | 7 | Compile and run 8 | =============== 9 | 10 | .. code-block:: text 11 | 12 | $ make -s 13 | ... 14 | Sleeping for 5 seconds... 15 | Timeout! 16 | Timeout! 17 | Timeout! 18 | Timeout! 19 | Timeout! 20 | Woke up! 21 | Completed! 22 | Timeout! 23 | Timeout! 24 | Timeout! 25 | -------------------------------------------------------------------------------- /examples/call_worker_pool/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include 31 | #include "async.h" 32 | 33 | static void hello(void *obj_p, void *arg_p) 34 | { 35 | (void)obj_p; 36 | (void)arg_p; 37 | 38 | printf("Sleeping for 5 seconds...\n"); 39 | sleep(5); 40 | printf("Woke up!\n"); 41 | } 42 | 43 | static void on_complete(void *obj_p, void *arg_p) 44 | { 45 | (void)obj_p; 46 | (void)arg_p; 47 | 48 | printf("Completed!\n"); 49 | } 50 | 51 | static void on_timeout() 52 | { 53 | printf("Timeout!\n"); 54 | } 55 | 56 | int main() 57 | { 58 | struct async_t async; 59 | struct async_timer_t timer; 60 | 61 | async_init(&async); 62 | async_set_runtime(&async, async_runtime_create()); 63 | async_timer_init(&timer, on_timeout, NULL, 0, 1000, &async); 64 | async_timer_start(&timer); 65 | async_call_worker_pool(&async, hello, NULL, NULL, on_complete); 66 | async_run_forever(&async); 67 | 68 | return (0); 69 | } 70 | -------------------------------------------------------------------------------- /examples/conversation/Makefile: -------------------------------------------------------------------------------- 1 | SRC += bob.c 2 | 3 | include $(ASYNC_ROOT)/make/app.mk 4 | -------------------------------------------------------------------------------- /examples/conversation/README.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | Bob asking you a few personal questions. 5 | 6 | Compile and run 7 | =============== 8 | 9 | .. code-block:: text 10 | 11 | $ make -s 12 | ... 13 | Bob: Hello! 14 | You: Hi! 15 | Bob: What is your name? 16 | You: Erik 17 | Bob: How old are you? 18 | Erik: 19 | Bob: Do you hear me? 20 | Erik: Yes 21 | Bob: How old are you? 22 | Erik: 22 23 | Bob: That's it, thanks! 24 | 25 | Bob: Hello! 26 | You: 27 | -------------------------------------------------------------------------------- /examples/conversation/bob.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef BOB_H 30 | #define BOB_H 31 | 32 | #include 33 | #include "async.h" 34 | 35 | enum state_t { 36 | state_init_t = 0, 37 | state_wait_for_greeting_t, 38 | state_wait_for_name_t, 39 | state_wait_for_age_t 40 | }; 41 | 42 | struct bob_t { 43 | enum state_t state; 44 | struct async_timer_t response_timer; 45 | int no_response_count; 46 | char name[32]; 47 | struct { 48 | char buf[128]; 49 | size_t length; 50 | bool complete; 51 | } line; 52 | struct async_channel_t *channel_p; 53 | }; 54 | 55 | void bob_init(struct bob_t *self_p, 56 | struct async_channel_t *channel_p, 57 | struct async_t *async_p); 58 | 59 | /** 60 | * Call when there is data available on stdin. 61 | */ 62 | void bob_on_stdin(struct bob_t *self_p); 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /examples/conversation/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include 31 | #include "async.h" 32 | #include "async/utils/linux.h" 33 | #include "bob.h" 34 | 35 | int main() 36 | { 37 | struct async_t async; 38 | int timer_fd; 39 | int epoll_fd; 40 | struct epoll_event event; 41 | int nfds; 42 | struct bob_t bob; 43 | struct async_channel_t channel; 44 | 45 | async_init(&async); 46 | async_utils_linux_channel_stdin_init(&channel, &async); 47 | bob_init(&bob, &channel, &async); 48 | epoll_fd = async_utils_linux_epoll_create(); 49 | timer_fd = async_utils_linux_init_periodic_timer(&async, epoll_fd); 50 | async_utils_linux_init_stdin(epoll_fd); 51 | 52 | while (true) { 53 | nfds = epoll_wait(epoll_fd, &event, 1, -1); 54 | 55 | if (nfds == 1) { 56 | if (event.data.fd == timer_fd) { 57 | async_utils_linux_handle_timeout(&async, timer_fd); 58 | } else if (event.data.fd == fileno(stdin)) { 59 | async_utils_linux_channel_stdin_handle(&channel); 60 | } 61 | } 62 | 63 | async_process(&async); 64 | } 65 | 66 | return (1); 67 | } 68 | -------------------------------------------------------------------------------- /examples/counter/Makefile: -------------------------------------------------------------------------------- 1 | SRC += counter.c 2 | 3 | include $(ASYNC_ROOT)/make/app.mk 4 | -------------------------------------------------------------------------------- /examples/counter/README.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | Incremet or decrement a counter by pressing ``+`` and ``-``. Prints 5 | the count periodically. 6 | 7 | Compile and run 8 | =============== 9 | 10 | .. code-block:: text 11 | 12 | $ make -s 13 | ... 14 | Change the count by pressing + and -. 15 | Count: 0 16 | Count: 0 17 | Count: 0 18 | Count: 1 19 | Count: 1 20 | Count: 2 21 | Count: 1 22 | Count: -1 23 | Count: 0 24 | Count: 2 25 | Count: 5 26 | Count: 3 27 | Count: 3 28 | Count: 3 29 | -------------------------------------------------------------------------------- /examples/counter/counter.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include "counter.h" 31 | 32 | static void on_print_timeout(struct counter_t *self_p) 33 | { 34 | printf("Count: %d\n", self_p->count); 35 | } 36 | 37 | void counter_init(struct counter_t *self_p, struct async_t *async_p) 38 | { 39 | self_p->count = 0; 40 | async_timer_init(&self_p->print_timer, 41 | (async_timer_timeout_t)on_print_timeout, 42 | self_p, 43 | 0, 44 | 300, 45 | async_p); 46 | async_timer_start(&self_p->print_timer); 47 | } 48 | 49 | void counter_incremect(struct counter_t *self_p) 50 | { 51 | self_p->count++; 52 | } 53 | 54 | void counter_decrement(struct counter_t *self_p) 55 | { 56 | self_p->count--; 57 | } 58 | -------------------------------------------------------------------------------- /examples/counter/counter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef COUNTER_H 30 | #define COUNTER_H 31 | 32 | #include "async.h" 33 | 34 | struct counter_t { 35 | int count; 36 | struct async_timer_t print_timer; 37 | }; 38 | 39 | /** 40 | * Initialize the counter. 41 | */ 42 | void counter_init(struct counter_t *self_p, struct async_t *async_p); 43 | 44 | void counter_incremect(struct counter_t *self_p); 45 | 46 | void counter_decrement(struct counter_t *self_p); 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /examples/counter/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include "async.h" 33 | #include "async/utils/linux.h" 34 | #include "counter.h" 35 | 36 | static void handle_stdin(struct counter_t *counter_p) 37 | { 38 | char ch; 39 | ssize_t res; 40 | 41 | res = read(fileno(stdin), &ch, sizeof(ch)); 42 | 43 | if (res != sizeof(ch)) { 44 | exit(1); 45 | } 46 | 47 | switch (ch) { 48 | 49 | case '+': 50 | counter_incremect(counter_p); 51 | break; 52 | 53 | case '-': 54 | counter_decrement(counter_p); 55 | break; 56 | 57 | default: 58 | printf("Press + or -, not %c.\n", ch); 59 | break; 60 | } 61 | } 62 | 63 | int main() 64 | { 65 | int timer_fd; 66 | int epoll_fd; 67 | int nfds; 68 | struct async_t async; 69 | struct counter_t counter; 70 | struct epoll_event event; 71 | 72 | async_init(&async); 73 | counter_init(&counter, &async); 74 | epoll_fd = async_utils_linux_epoll_create(); 75 | timer_fd = async_utils_linux_init_periodic_timer(&async, epoll_fd); 76 | async_utils_linux_epoll_add_in(epoll_fd, fileno(stdin)); 77 | async_utils_linux_make_stdin_unbuffered(); 78 | 79 | printf("Press + and - to increment and decrement the counter.\n"); 80 | 81 | while (true) { 82 | nfds = epoll_wait(epoll_fd, &event, 1, -1); 83 | 84 | if (nfds == 1) { 85 | if (event.data.fd == timer_fd) { 86 | async_utils_linux_handle_timeout(&async, timer_fd); 87 | } else if (event.data.fd == fileno(stdin)) { 88 | handle_stdin(&counter); 89 | } 90 | } 91 | 92 | async_process(&async); 93 | } 94 | 95 | return (1); 96 | } 97 | -------------------------------------------------------------------------------- /examples/hello_world/Makefile: -------------------------------------------------------------------------------- 1 | include $(ASYNC_ROOT)/make/app.mk 2 | -------------------------------------------------------------------------------- /examples/hello_world/README.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | Print 'Hello world!' periodically. 5 | 6 | Compile and run 7 | =============== 8 | 9 | .. code-block:: text 10 | 11 | $ make -s 12 | ... 13 | Hello world! 14 | Hello world! 15 | Hello world! 16 | Hello world! 17 | -------------------------------------------------------------------------------- /examples/hello_world/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include "async.h" 31 | 32 | static void on_timeout() 33 | { 34 | printf("Hello world!\n"); 35 | } 36 | 37 | int main() 38 | { 39 | struct async_t async; 40 | struct async_timer_t timer; 41 | 42 | async_init(&async); 43 | async_set_runtime(&async, async_runtime_create()); 44 | async_timer_init(&timer, on_timeout, NULL, 0, 1000, &async); 45 | async_timer_start(&timer); 46 | async_run_forever(&async); 47 | 48 | return (0); 49 | } 50 | -------------------------------------------------------------------------------- /examples/http_get/Makefile: -------------------------------------------------------------------------------- 1 | SRC += http_get.c 2 | 3 | include $(ASYNC_ROOT)/make/app.mk 4 | -------------------------------------------------------------------------------- /examples/http_get/README.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | A basic HTTP client. 5 | 6 | Compile and run 7 | =============== 8 | 9 | First of all, start the HTTP server in another terminal. 10 | 11 | .. code-block:: text 12 | 13 | $ python3 -m http.server 8080 14 | 15 | Then build and run the example program. 16 | 17 | .. code-block:: text 18 | 19 | $ make -s 20 | ... 21 | Getting 'http://localhost:8080/main.c'... 22 | --------------------- HTTP GET BEGIN --------------------- 23 | HTTP/1.0 200 OK 24 | Server: SimpleHTTP/0.6 Python/3.7.3 25 | Date: Tue, 24 Dec 2019 18:45:26 GMT 26 | Content-type: text/plain 27 | Content-Length: 1480 28 | Last-Modified: Tue, 24 Dec 2019 18:16:37 GMT 29 | 30 | /* 31 | * The MIT License (MIT) 32 | * 33 | * Copyright (c) 2019, Erik Moqvist 34 | * 35 | * Permission is hereby granted, free of charge, to any person 36 | * obtaining a copy of this software and associated documentation 37 | * files (the "Software"), to deal in the Software without 38 | * restriction, including without limitation the rights to use, copy, 39 | * modify, merge, publish, distribute, sublicense, and/or sell copies 40 | * of the Software, and to permit persons to whom the Software is 41 | * furnished to do so, subject to the following conditions: 42 | * 43 | * The above copyright notice and this permission notice shall be 44 | * included in all copies or substantial portions of the Software. 45 | * 46 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 47 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 48 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 49 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 50 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 51 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 52 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 53 | * SOFTWARE. 54 | * 55 | * This file is part of the Async project. 56 | */ 57 | 58 | #include "async.h" 59 | #include "http_get.h" 60 | 61 | int main() 62 | { 63 | struct async_t async; 64 | struct http_get_t http_get; 65 | 66 | async_init(&async); 67 | async_set_runtime(&async, async_runtime_create()); 68 | http_get_init(&http_get, &async); 69 | async_run_forever(&async); 70 | 71 | return (0); 72 | } 73 | ---------------------- HTTP GET END ---------------------- 74 | -------------------------------------------------------------------------------- /examples/http_get/http_get.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include "async.h" 31 | #include "http_get.h" 32 | 33 | static void do_get(struct http_get_t *self_p, void *arg_p) 34 | { 35 | (void)arg_p; 36 | 37 | printf("Getting 'http://localhost:8080/main.c'...\n"); 38 | async_tcp_client_connect(&self_p->tcp, "localhost", 8080); 39 | } 40 | 41 | static void on_connected(struct async_tcp_client_t *tcp_p, int res) 42 | { 43 | struct http_get_t *self_p; 44 | static const char request[] = ( 45 | "GET /main.c HTTP/1.1\r\n" 46 | "\r\n"); 47 | 48 | self_p = async_container_of(tcp_p, typeof(*self_p), tcp); 49 | 50 | if (res == 0) { 51 | async_tcp_client_write(tcp_p, &request[0], sizeof(request) - 1); 52 | printf("--------------------- HTTP GET BEGIN ---------------------\n"); 53 | } else { 54 | printf("Connect failed.\n"); 55 | } 56 | } 57 | 58 | static void on_disconnected(struct async_tcp_client_t *tcp_p) 59 | { 60 | (void)tcp_p; 61 | 62 | printf("---------------------- HTTP GET END ----------------------\n"); 63 | } 64 | 65 | static void on_input(struct async_tcp_client_t *tcp_p) 66 | { 67 | char buf[64]; 68 | ssize_t size; 69 | struct http_get_t *self_p; 70 | 71 | self_p = async_container_of(tcp_p, typeof(*self_p), tcp); 72 | size = async_tcp_client_read(&self_p->tcp, &buf[0], sizeof(buf)); 73 | 74 | fwrite(&buf[0], 1, size, stdout); 75 | } 76 | 77 | void http_get_init(struct http_get_t *self_p, struct async_t *async_p) 78 | { 79 | async_tcp_client_init(&self_p->tcp, 80 | on_connected, 81 | on_disconnected, 82 | on_input, 83 | async_p); 84 | async_call(async_p, (async_func_t)do_get, self_p, NULL); 85 | } 86 | -------------------------------------------------------------------------------- /examples/http_get/http_get.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef HTTP_GET_H 30 | #define HTTP_GET_H 31 | 32 | #include "async.h" 33 | 34 | struct http_get_t { 35 | struct async_tcp_client_t tcp; 36 | }; 37 | 38 | void http_get_init(struct http_get_t *self_p, struct async_t *async_p); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /examples/http_get/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include "async.h" 30 | #include "http_get.h" 31 | 32 | int main() 33 | { 34 | struct async_t async; 35 | struct http_get_t http_get; 36 | 37 | async_init(&async); 38 | async_set_runtime(&async, async_runtime_create()); 39 | http_get_init(&http_get, &async); 40 | async_run_forever(&async); 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /examples/mqtt_client/Makefile: -------------------------------------------------------------------------------- 1 | SRC += publisher.c 2 | 3 | include $(ASYNC_ROOT)/make/app.mk 4 | -------------------------------------------------------------------------------- /examples/mqtt_client/README.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | Showcase the MQTT client. 5 | 6 | Compile and run 7 | =============== 8 | 9 | First of all, start an `MQTT broker`_ in another terminal. 10 | 11 | .. code-block:: text 12 | 13 | $ mqttools -l debug broker \ 14 | --cafile ca.crt --certfile server.crt --keyfile server.key --no-check-hostname 15 | 16 | Then build and run the example program. 17 | 18 | In another terminal, publish on ``async/start`` to start the publish 19 | timer, ``async/stop`` to stop the publish timer and 20 | ``async/reconnect`` to reconnect to the broker. 21 | 22 | .. code-block:: text 23 | 24 | $ make -s 25 | ... 26 | Starting the MQTT client. 27 | Connected. 28 | Starting the publish timer with timeout 1000 ms. 29 | Subscribe with transaction id 1 completed. 30 | Subscribe with transaction id 2 completed. 31 | Subscribe with transaction id 3 completed. 32 | Publishing 'count: 0' on 'async/hello'. 33 | Publishing 'count: 1' on 'async/hello'. 34 | Publishing 'count: 2' on 'async/hello'. 35 | Publishing 'count: 3' on 'async/hello'. 36 | Publishing 'count: 4' on 'async/hello'. 37 | Publishing 'count: 5' on 'async/hello'. 38 | Stopping the publish timer. 39 | Starting the publish timer with timeout 2000 ms. 40 | Publishing 'count: 6' on 'async/hello'. 41 | Publishing 'count: 7' on 'async/hello'. 42 | Publishing 'count: 8' on 'async/hello'. 43 | Publishing 'count: 9' on 'async/hello'. 44 | Stopping the MQTT client for 3000 ms. 45 | Stopping the publish timer. 46 | Starting the MQTT client. 47 | Connected. 48 | Starting the publish timer with timeout 2000 ms. 49 | Publishing 'count: 10' on 'async/hello'. 50 | Publishing 'count: 11' on 'async/hello'. 51 | Publishing 'count: 12' on 'async/hello'. 52 | 53 | .. _MQTT broker: https://github.com/eerimoq/mqttools 54 | -------------------------------------------------------------------------------- /examples/mqtt_client/ca.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDazCCAlOgAwIBAgIUNFjz0PDCt+MRWWsKyXTVzYD9SqowDQYJKoZIhvcNAQEL 3 | BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM 4 | GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDAxMDEwOTE2MTZaFw00NzA1 5 | MTkwOTE2MTZaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw 6 | HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB 7 | AQUAA4IBDwAwggEKAoIBAQCw84ZqGYBYHpDzde6q9t5KFEKr/pTmPxEbM4SYNEFa 8 | cZXn2sA3stN5jvvbn1nV3/tRHZIsStxkAMuUCxF2r9USRxzV8EUkWJk8BSIuF5J1 9 | Sf9xX9vkJ8g9VfIjL66WQsXVGr0mybCUlVldb93Na3lcuK1BkghsWFJdqaKn5GoM 10 | XyAGKR1lb/mA7o6/t6N/QhoEXy+TDpEOEP9wvFI0ANEiC9B6bdBUm0iYWYRnNEq9 11 | 67aPKARn9zTfuXuHq2q7n/sIJV9a2YLwjGUud3ZvsH2PL8u12pzq/CSDk3TlfDQe 12 | ljtVsKHjZjxv/6HCm+mGvMCcH3r4eIhsDjfDCn4Uq5zRAgMBAAGjUzBRMB0GA1Ud 13 | DgQWBBRNv7mIkNVjoVxsNFSnUtStStHCXDAfBgNVHSMEGDAWgBRNv7mIkNVjoVxs 14 | NFSnUtStStHCXDAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAf 15 | sizVGws2loUREzVxuMuYg+WyJmH/qCcOhg7yjODuMVUs8IaTbsMV//+EChJnQL0H 16 | 13gSUTgJZaxIHKE8BKDeovrmCfpV/nIgjgMMCDvn9CfIU0jawQK2YkbVoeVuC8DA 17 | AWD6959rVhje3A5pmNGATu7sccRGDZ6KCyKmfIK98C9BRVmJ+cS796Fa30w4bnpl 18 | e2WOmvgeR+vDiV5kDKMqY9sdH6D51Ty4l88V3rapcT+hPg99cFh+z9DHZAO3Tsxs 19 | QqpKXQjKXLT6WPJhf1I5cabk6b+HqVuPhNTRvcftKaOhMtLXta6FFsN7qZHslZHF 20 | 6yjJ1mmLodD0sPt6mTVF 21 | -----END CERTIFICATE----- 22 | -------------------------------------------------------------------------------- /examples/mqtt_client/generate_certificates.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Certificate Authority 4 | openssl req -new -x509 -days 10000 -keyout ca.key -out ca.crt 5 | 6 | # Server 7 | openssl genrsa -out server.key 2048 8 | openssl req -out server.csr -key server.key -new 9 | openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 10000 10 | -------------------------------------------------------------------------------- /examples/mqtt_client/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include "async.h" 31 | #include "publisher.h" 32 | 33 | static const char server_crt[] = ( 34 | "-----BEGIN CERTIFICATE-----\n" 35 | "MIIDETCCAfkCFFVRo6tydPDAjBCbuF2lBCRPAhrUMA0GCSqGSIb3DQEBCwUAMEUx\n" 36 | "CzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRl\n" 37 | "cm5ldCBXaWRnaXRzIFB0eSBMdGQwHhcNMjAwMTAxMDkxNjIxWhcNNDcwNTE5MDkx\n" 38 | "NjIxWjBFMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UE\n" 39 | "CgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" 40 | "AQ8AMIIBCgKCAQEAt2cIM+Vkpkhwcdb3cE/dxyRU1nCodWKyO/YLSqDKfZXGmLEp\n" 41 | "FelzuJFA3ZzoCMdEywn1WbzPzWFBLFm6YP95HQQcWT6PQYTs2NmXfsQpU23ECdaf\n" 42 | "yf/1QJU9YVeZ/rYXeVReFMxPaAmqdaEe6lRgXXO98q1J9NDArNqETE6gajbVKAEE\n" 43 | "Sj7+MYs6iBWIyXHh9rzOHXApRi+zcf7nzHaeolw3LwOpV4MyuY010Qcaww4J6btD\n" 44 | "qEPA0zCVLuwgPlMkeKqpwcR5mgv4wW8F4eaNVdT2aDqIbKcCxWT3h/zTfq7fp3oX\n" 45 | "ryUUvAhHP4h+IKfX8UTQYKBzPJKfCIMdElwidwIDAQABMA0GCSqGSIb3DQEBCwUA\n" 46 | "A4IBAQAD6YyMDh/SaOV+XZoDTpn8+dgaIlSFj41yIrr0kTvvtOFoQhTbfAA/kfRy\n" 47 | "uE42i8XDIWw8Euc/mw1ACJtmXJE3KakwtiRHpphZtfWVUR1vmbvinUEbO8Rmi5wI\n" 48 | "0q/JBJbitDc1l5/U0bc3VIZ2WOqhkEd4En4YjhAUizicSS5a/eScirNVoZwzXdUY\n" 49 | "B/JepoMH4SkL9qvOjf+fBCrgrlslhZ5pKRPDJUy7Slqk4EIYuyE8YGvLQnPgV4/7\n" 50 | "l6PymsbuwVsMOvwz6FEdBVR3dNO3pMT667V/rSilLQGnRFpUcHFILyqg35LONFn3\n" 51 | "cwT/ciBUfMi/giQyFNpB1RdktfHd\n" 52 | "-----END CERTIFICATE-----\n"); 53 | 54 | static void log_stdout(void *log_object_p, 55 | int level, 56 | const char *fmt_p, 57 | ...) 58 | { 59 | (void)log_object_p; 60 | (void)level; 61 | 62 | va_list vlist; 63 | 64 | va_start(vlist, fmt_p); 65 | printf("DEBUG "); 66 | vprintf(fmt_p, vlist); 67 | printf("\n"); 68 | va_end(vlist); 69 | } 70 | 71 | int main() 72 | { 73 | struct async_t async; 74 | struct publisher_t publishers[2]; 75 | struct async_ssl_context_t ssl_context; 76 | 77 | async_ssl_module_init(); 78 | async_init(&async); 79 | async_set_log_object_callbacks(&async, log_stdout, NULL); 80 | async_set_runtime(&async, async_runtime_create()); 81 | publisher_init(&publishers[0], 82 | "tcp", 83 | 1883, 84 | NULL, 85 | &async); 86 | async_ssl_context_init(&ssl_context, async_ssl_protocol_tls_v1_0_t); 87 | async_ssl_context_load_verify_location(&ssl_context, &server_crt[0]); 88 | publisher_init(&publishers[1], 89 | "ssl", 90 | 8883, 91 | &ssl_context, 92 | &async); 93 | async_run_forever(&async); 94 | 95 | return (0); 96 | } 97 | -------------------------------------------------------------------------------- /examples/mqtt_client/publisher.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef PUBLISHER_H 30 | #define PUBLISHER_H 31 | 32 | #include "async.h" 33 | 34 | struct publisher_t { 35 | struct async_mqtt_client_t client; 36 | struct async_timer_t publish_timer; 37 | struct async_timer_t reconnect_timer; 38 | }; 39 | 40 | void publisher_init(struct publisher_t *self_p, 41 | const char *client_id_p, 42 | int port, 43 | struct async_ssl_context_t *ssl_context_p, 44 | struct async_t *async_p); 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /examples/mqtt_client/server.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDETCCAfkCFFVRo6tydPDAjBCbuF2lBCRPAhrUMA0GCSqGSIb3DQEBCwUAMEUx 3 | CzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRl 4 | cm5ldCBXaWRnaXRzIFB0eSBMdGQwHhcNMjAwMTAxMDkxNjIxWhcNNDcwNTE5MDkx 5 | NjIxWjBFMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UE 6 | CgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOC 7 | AQ8AMIIBCgKCAQEAt2cIM+Vkpkhwcdb3cE/dxyRU1nCodWKyO/YLSqDKfZXGmLEp 8 | FelzuJFA3ZzoCMdEywn1WbzPzWFBLFm6YP95HQQcWT6PQYTs2NmXfsQpU23ECdaf 9 | yf/1QJU9YVeZ/rYXeVReFMxPaAmqdaEe6lRgXXO98q1J9NDArNqETE6gajbVKAEE 10 | Sj7+MYs6iBWIyXHh9rzOHXApRi+zcf7nzHaeolw3LwOpV4MyuY010Qcaww4J6btD 11 | qEPA0zCVLuwgPlMkeKqpwcR5mgv4wW8F4eaNVdT2aDqIbKcCxWT3h/zTfq7fp3oX 12 | ryUUvAhHP4h+IKfX8UTQYKBzPJKfCIMdElwidwIDAQABMA0GCSqGSIb3DQEBCwUA 13 | A4IBAQAD6YyMDh/SaOV+XZoDTpn8+dgaIlSFj41yIrr0kTvvtOFoQhTbfAA/kfRy 14 | uE42i8XDIWw8Euc/mw1ACJtmXJE3KakwtiRHpphZtfWVUR1vmbvinUEbO8Rmi5wI 15 | 0q/JBJbitDc1l5/U0bc3VIZ2WOqhkEd4En4YjhAUizicSS5a/eScirNVoZwzXdUY 16 | B/JepoMH4SkL9qvOjf+fBCrgrlslhZ5pKRPDJUy7Slqk4EIYuyE8YGvLQnPgV4/7 17 | l6PymsbuwVsMOvwz6FEdBVR3dNO3pMT667V/rSilLQGnRFpUcHFILyqg35LONFn3 18 | cwT/ciBUfMi/giQyFNpB1RdktfHd 19 | -----END CERTIFICATE----- 20 | -------------------------------------------------------------------------------- /examples/mqtt_client/server.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEowIBAAKCAQEAt2cIM+Vkpkhwcdb3cE/dxyRU1nCodWKyO/YLSqDKfZXGmLEp 3 | FelzuJFA3ZzoCMdEywn1WbzPzWFBLFm6YP95HQQcWT6PQYTs2NmXfsQpU23ECdaf 4 | yf/1QJU9YVeZ/rYXeVReFMxPaAmqdaEe6lRgXXO98q1J9NDArNqETE6gajbVKAEE 5 | Sj7+MYs6iBWIyXHh9rzOHXApRi+zcf7nzHaeolw3LwOpV4MyuY010Qcaww4J6btD 6 | qEPA0zCVLuwgPlMkeKqpwcR5mgv4wW8F4eaNVdT2aDqIbKcCxWT3h/zTfq7fp3oX 7 | ryUUvAhHP4h+IKfX8UTQYKBzPJKfCIMdElwidwIDAQABAoIBAEHM1HOSFKdYDAe7 8 | 4XDJJr+g+FMrfjUHYP0ETI7M23ltOCBmR2BWLq+EZapc9h7YJ4VVIFK+3CtKflCE 9 | LMtKPVC8vdMDD/Co2TT/9EuF43HMSu5gs1mM3Jf8M1ZuUJDDEe705wA9QSwJc7/a 10 | O7ZE6YH92aNsqsEL+GTLzXV3LDSOtVupxJWYg+v98VtBX5VoQpXv7SCzgUA/snRK 11 | PiUH/6m2Kb/olulwXtxKAqNVrZW+VAhmkNkNxqHUYGS2wG0cqOIlyLW31ZUp79Px 12 | uwLeOXGJWn7ActWW/3SdSD0BF7bB7EjG8LZ/+U9dvxZBqhC3GVCev/5wOi7R0fvV 13 | +7i4LNECgYEA8JUw5uHT9xHtfgxxvvq1gKbHtB1BNJUv5CumCxYKHiO60Y7lkVN3 14 | 5rtSP+FoFBNJCnytmmwCgOZg3m41YJrGT0rHcFKOySu1/JHoJ6S2X55v3iX0C4Iu 15 | A5RTtpWRD9KXRo075ncmqCH7o9v9iRn9j+YLZGthVE98sBy4Xim3DBkCgYEAwyfJ 16 | WdoSlQ8+IOWnvgTImJjucAnd0m5KoegFMnif0Yi/Nov1/yuE+/ZLbzWMfESdYVdQ 17 | sXs+Y8KqWGDNr+33RRhNFgL4YLvcXCDgfsq2pdpKtroHUphs8ZuxRTCj09/mOOy9 18 | wcH0NrchKAsSquCcecJ5AmwokNp3xXcASOxTdQ8CgYA0T5xEd5IJROKo57fL2OOi 19 | R6FgroPBFTdGm58NBgvJJee3LSUJhmapys1FipOA2filyRpOW3Hn6S/i7X+5s62v 20 | DAl1iN33LAP0GR2rkQr+m4qjeQkpf3LV4GibaMYT9Kxj8M23fOzw3gLDTFBCgV46 21 | 7cBIY3jURjxMZtJH1zYv8QKBgGVTRATl594C0wQrmSNcwnYQeUQsq0J1x0Q2/nPN 22 | E1WshXMhyjCZfOuBXns4N1/SFFCVEC2GSsdrLBzE7iaZDFiQnH2AP0uiXnBrFYRf 23 | ZePI6h/jiKfGwlHTv3+oGHeKebM2n/N46355KLrsvN2Bz1ycr02lsI49EBCzj69N 24 | HKvbAoGBAJ3o4aXdRiUc7sGjkNjns2H6IjczObrCroutpDcIDZkJkEeB2b4kNexK 25 | Ldf5DfQAoyqz0aKx3dvpArZNKgxjNuGcPU4A6bh+MMQkWWUqqJ19gg6wn1I2v8UO 26 | f/fydWndbO86pYJmNvLix31wWwZvB2rRHGO4f4OTMr4Ftgx4bCE7 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /examples/ping_pong/Makefile: -------------------------------------------------------------------------------- 1 | include $(ASYNC_ROOT)/make/app.mk 2 | -------------------------------------------------------------------------------- /examples/ping_pong/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | /* 30 | * +--------+ +--------+ 31 | * | client | | server | 32 | * +--------+ +--------+ 33 | * | | 34 | * |------- ping ------>| 35 | * |<------ pong -------| 36 | * |------- ping ------>| 37 | * |<------ pong -------| 38 | * . . 39 | * . . 40 | * . . 41 | */ 42 | 43 | #include "async.h" 44 | 45 | struct client_t { 46 | struct server_t *server_p; 47 | struct async_t *async_p; 48 | }; 49 | 50 | struct server_t { 51 | struct client_t *client_p; 52 | struct async_t *async_p; 53 | }; 54 | 55 | static void client_on_pong(struct client_t *self_p, int *count_p); 56 | 57 | static void server_init(struct server_t *self_p, 58 | struct client_t *client_p, 59 | struct async_t *async_p) 60 | { 61 | self_p->client_p = client_p; 62 | self_p->async_p = async_p; 63 | } 64 | 65 | static void server_on_ping(struct server_t *self_p, int *count_p) 66 | { 67 | printf("Ping count %d.\n", *count_p); 68 | 69 | *count_p += 2; 70 | 71 | async_call(self_p->async_p, 72 | (async_func_t)client_on_pong, 73 | self_p->client_p, 74 | count_p); 75 | } 76 | 77 | static void client_init(struct client_t *self_p, 78 | struct server_t *server_p, 79 | struct async_t *async_p) 80 | { 81 | int *count_p; 82 | 83 | self_p->server_p = server_p; 84 | self_p->async_p = async_p; 85 | 86 | count_p = malloc(sizeof(*count_p)); 87 | 88 | if (count_p == NULL) { 89 | exit(1); 90 | } 91 | 92 | *count_p = 1; 93 | 94 | async_call(async_p, (async_func_t)server_on_ping, server_p, count_p); 95 | } 96 | 97 | static void client_on_pong(struct client_t *self_p, int *count_p) 98 | { 99 | printf("Pong count %d.\n", *count_p); 100 | 101 | *count_p -= 1; 102 | 103 | async_call(self_p->async_p, 104 | (async_func_t)server_on_ping, 105 | self_p->server_p, 106 | count_p); 107 | } 108 | 109 | int main() 110 | { 111 | struct async_t async; 112 | struct client_t client; 113 | struct server_t server; 114 | 115 | async_init(&async); 116 | async_set_runtime(&async, async_runtime_create()); 117 | server_init(&server, &client, &async); 118 | client_init(&client, &server, &async); 119 | async_run_forever(&async); 120 | 121 | return (0); 122 | } 123 | -------------------------------------------------------------------------------- /examples/shell/Makefile: -------------------------------------------------------------------------------- 1 | SRC += my_shell.c 2 | 3 | include $(ASYNC_ROOT)/make/app.mk 4 | -------------------------------------------------------------------------------- /examples/shell/README.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | Showcase the shell. 5 | 6 | Compile and run 7 | =============== 8 | 9 | .. code-block:: text 10 | 11 | $ make -s 12 | ... 13 | 14 | Welcome to the async shell! 15 | 16 | $ hello Erik 17 | Hello Erik! 18 | $ 19 | -------------------------------------------------------------------------------- /examples/shell/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include "async.h" 33 | #include "async/utils/linux.h" 34 | #include "my_shell.h" 35 | 36 | int main() 37 | { 38 | int epoll_fd; 39 | int timer_fd; 40 | int nfds; 41 | struct async_t async; 42 | struct my_shell_t my_shell; 43 | struct epoll_event event; 44 | struct async_channel_t channel; 45 | 46 | async_init(&async); 47 | async_utils_linux_channel_stdin_init(&channel, &async); 48 | my_shell_init(&my_shell, &channel, &async); 49 | epoll_fd = async_utils_linux_epoll_create(); 50 | timer_fd = async_utils_linux_init_periodic_timer(&async, epoll_fd); 51 | async_utils_linux_init_stdin(epoll_fd); 52 | async_utils_linux_make_stdin_unbuffered(); 53 | 54 | while (true) { 55 | nfds = epoll_wait(epoll_fd, &event, 1, -1); 56 | 57 | if (nfds == 1) { 58 | if (event.data.fd == timer_fd) { 59 | async_utils_linux_handle_timeout(&async, timer_fd); 60 | } else if (event.data.fd == fileno(stdin)) { 61 | async_utils_linux_channel_stdin_handle(&channel); 62 | } 63 | } 64 | 65 | async_process(&async); 66 | } 67 | 68 | return (1); 69 | } 70 | -------------------------------------------------------------------------------- /examples/shell/my_shell.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include "my_shell.h" 40 | 41 | static int command_hello(struct async_shell_t *self_p, 42 | int argc, 43 | const char *argv[]) 44 | { 45 | (void)self_p; 46 | 47 | const char *name_p; 48 | 49 | if (argc == 2) { 50 | name_p = argv[1]; 51 | } else { 52 | name_p = "stranger"; 53 | } 54 | 55 | printf("Hello %s!\n", name_p); 56 | 57 | return (0); 58 | } 59 | 60 | void my_shell_init(struct my_shell_t *self_p, 61 | struct async_channel_t *channel_p, 62 | struct async_t *async_p) 63 | { 64 | async_shell_init(&self_p->shell, channel_p, async_p); 65 | async_shell_command_init(&self_p->hello, 66 | "hello", 67 | "My command.", 68 | command_hello); 69 | async_shell_register_command(&self_p->shell, &self_p->hello); 70 | async_shell_start(&self_p->shell); 71 | } 72 | -------------------------------------------------------------------------------- /examples/shell/my_shell.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef MY_SHELL_H 30 | #define MY_SHELL_H 31 | 32 | #include "async.h" 33 | 34 | struct my_shell_t { 35 | struct async_shell_t shell; 36 | struct async_shell_command_t hello; 37 | }; 38 | 39 | void my_shell_init(struct my_shell_t *self_p, 40 | struct async_channel_t *channel_p, 41 | struct async_t *async_p); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /examples/stcp_echo_client/Makefile: -------------------------------------------------------------------------------- 1 | SRC += echo_client.c 2 | 3 | include $(ASYNC_ROOT)/make/app.mk 4 | -------------------------------------------------------------------------------- /examples/stcp_echo_client/README.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | Showcase the secure TCP client, both with and without TLS. 5 | 6 | Compile and run 7 | =============== 8 | 9 | First of all, start the TCP echo server in another terminal. 10 | 11 | .. code-block:: text 12 | 13 | $ ./server.py 14 | 15 | Then build and run the example program. 16 | 17 | .. code-block:: text 18 | 19 | $ make -s 20 | ... 21 | tcp: Connecting to 'localhost:33000'... 22 | tcp: Connected. 23 | tcp: TX: 'Hello!' 24 | tcp: RX: 'He' 25 | tcp: RX: 'l' 26 | tcp: RX: '' 27 | tcp: RX: 'lo' 28 | tcp: RX: '!' 29 | tcp: RX: '' 30 | tcp: TX: 'Hello!' 31 | tcp: RX: 'He' 32 | tcp: RX: 'l' 33 | tcp: RX: 'l' 34 | tcp: RX: 'o!' 35 | tcp: RX: '' 36 | tcp: TX: 'Hello!' 37 | tcp: RX: 'He' 38 | tcp: RX: 'l' 39 | tcp: RX: 'lo!' 40 | tcp: RX: '' 41 | -------------------------------------------------------------------------------- /examples/stcp_echo_client/echo_client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include "async.h" 31 | #include "echo_client.h" 32 | 33 | static void do_connect(struct echo_client_t *self_p) 34 | { 35 | printf("%s: Connecting to 'localhost:%d'...\n", self_p->name_p, self_p->port); 36 | async_stcp_client_connect(&self_p->stcp, "localhost", self_p->port); 37 | } 38 | 39 | static void on_start(struct echo_client_t *self_p, void *arg_p) 40 | { 41 | (void)arg_p; 42 | 43 | do_connect(self_p); 44 | } 45 | 46 | static void on_connected(struct async_stcp_client_t *stcp_p, int res) 47 | { 48 | struct echo_client_t *self_p; 49 | 50 | self_p = async_container_of(stcp_p, typeof(*self_p), stcp); 51 | 52 | if (res == 0) { 53 | printf("%s: Connected.\n", self_p->name_p); 54 | async_timer_start(&self_p->transmit_timer); 55 | } else { 56 | printf("%s: Connect failed.\n", self_p->name_p); 57 | async_timer_start(&self_p->reconnect_timer); 58 | } 59 | } 60 | 61 | static void on_disconnected(struct async_stcp_client_t *stcp_p) 62 | { 63 | struct echo_client_t *self_p; 64 | 65 | self_p = async_container_of(stcp_p, typeof(*self_p), stcp); 66 | printf("%s: Disconnected.\n", self_p->name_p); 67 | async_timer_stop(&self_p->transmit_timer); 68 | async_timer_start(&self_p->reconnect_timer); 69 | } 70 | 71 | static void on_input(struct async_stcp_client_t *stcp_p) 72 | { 73 | char buf[8]; 74 | ssize_t size; 75 | struct echo_client_t *self_p; 76 | 77 | self_p = async_container_of(stcp_p, typeof(*self_p), stcp); 78 | size = async_stcp_client_read(&self_p->stcp, &buf[0], sizeof(buf)); 79 | 80 | if (size > 0) { 81 | printf("%s: RX: '", self_p->name_p); 82 | fwrite(&buf[0], 1, size, stdout); 83 | printf("'\n"); 84 | } 85 | } 86 | 87 | static void on_transmit_timeout(struct echo_client_t *self_p) 88 | { 89 | printf("%s: TX: 'Hello!'\n", self_p->name_p); 90 | async_stcp_client_write(&self_p->stcp, "Hello!", 6); 91 | } 92 | 93 | static void on_reconnect_timeout(struct echo_client_t *self_p) 94 | { 95 | do_connect(self_p); 96 | } 97 | 98 | void echo_client_init(struct echo_client_t *self_p, 99 | const char *name_p, 100 | int port, 101 | struct async_ssl_context_t *ssl_context_p, 102 | struct async_t *async_p) 103 | { 104 | self_p->name_p = name_p; 105 | self_p->port = port; 106 | async_stcp_client_init(&self_p->stcp, 107 | ssl_context_p, 108 | on_connected, 109 | on_disconnected, 110 | on_input, 111 | async_p); 112 | async_timer_init(&self_p->transmit_timer, 113 | (async_timer_timeout_t)on_transmit_timeout, 114 | self_p, 115 | 0, 116 | 1000, 117 | async_p); 118 | async_timer_init(&self_p->reconnect_timer, 119 | (async_timer_timeout_t)on_reconnect_timeout, 120 | self_p, 121 | 1000, 122 | 0, 123 | async_p); 124 | async_call(async_p, (async_func_t)on_start, self_p, NULL); 125 | } 126 | -------------------------------------------------------------------------------- /examples/stcp_echo_client/echo_client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef ECHO_CLIENT_H 30 | #define ECHO_CLIENT_H 31 | 32 | #include "async.h" 33 | 34 | struct echo_client_t { 35 | const char *name_p; 36 | int port; 37 | struct async_stcp_client_t stcp; 38 | struct async_timer_t transmit_timer; 39 | struct async_timer_t reconnect_timer; 40 | }; 41 | 42 | void echo_client_init(struct echo_client_t *self_p, 43 | const char *name_p, 44 | int port, 45 | struct async_ssl_context_t *ssl_context_p, 46 | struct async_t *async_p); 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /examples/stcp_echo_client/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include "async.h" 30 | #include "echo_client.h" 31 | 32 | static const char ca_certificate[] = ( 33 | "-----BEGIN CERTIFICATE-----\n" 34 | "MIIDCjCCAfICCQCoXfSQydXTczANBgkqhkiG9w0BAQsFADBHMQswCQYDVQQGEwJT\n" 35 | "RTETMBEGA1UECAwKU29tZS1TdGF0ZTEOMAwGA1UECgwFU2ltYmExEzARBgNVBAMM\n" 36 | "CmZvb2Jhci5vcmcwHhcNMTcwMTA3MTgxOTQ3WhcNMTgwMTA3MTgxOTQ3WjBHMQsw\n" 37 | "CQYDVQQGEwJTRTETMBEGA1UECAwKU29tZS1TdGF0ZTEOMAwGA1UECgwFU2ltYmEx\n" 38 | "EzARBgNVBAMMCmZvb2Jhci5vcmcwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\n" 39 | "AoIBAQDugoc9amynDB/ODJzh2aqB6cnubSJEPlgB87jNG0akcbmUFt6BhPhMPSoP\n" 40 | "esHWl5OWscoga0cKrPURmMVVhfaeZLQGmrv5N4/liVlwae1n0gUEruX4d6MqSSDW\n" 41 | "3C/WKjCn647udZwyzCvyrvPOq0qAzaxR4EFRdwjSEPO5sDw2zxeTjGW2WxaH9PEu\n" 42 | "C8vaNqTsLYl3YBkR3zVCbuQXTQhStsv3gT4Yhz2wJpY0yyWyDiaBkGKpdxJQiNAd\n" 43 | "x5JKSqtRshlYZM3+cdKLywNoYUnezp6Wm4mzz09TCFv+esJ0h7/6pMdVjhxLsAg5\n" 44 | "ZbZyrtNIapN07AjIJS4qjkJ/HUC3AgMBAAEwDQYJKoZIhvcNAQELBQADggEBAGHS\n" 45 | "U5AvDESzTNoak5HHx166bp5+bvECvJg45GMDGhqKeFoOlR34u2b+wyTm/piz3nYJ\n" 46 | "12kn+BbG/PwGfndL0kZYRz46fY8Rf+MxCFfcglweDJhA6ULNpera11OC35Q/lKn5\n" 47 | "M6w6eQkZMB4VqwigvDGHGpXRTIhJHHoR2VFBFGoPTLrXilChUpiXi9DmuYXJ/19x\n" 48 | "sxOVwvvO/m/6g68G+uZYUoCsQsKllM2fgdNLTzbYvnFtsq5QnZS8m7CoZgEy2c3m\n" 49 | "VrrPsfwmyhwejDawjz2epSLNXaaDeSz4g1cQes+oehaA1IwFfKhb9tdiWUm48VuU\n" 50 | "oaFZ8e1HofYUy+65pws=\n" 51 | "-----END CERTIFICATE-----\n"); 52 | 53 | int main() 54 | { 55 | struct async_t async; 56 | struct echo_client_t echo_clients[2]; 57 | struct async_ssl_context_t ssl_context; 58 | 59 | async_ssl_module_init(); 60 | async_init(&async); 61 | async_set_runtime(&async, async_runtime_create()); 62 | echo_client_init(&echo_clients[0], "tcp", 33000, NULL, &async); 63 | async_ssl_context_init(&ssl_context, 64 | async_ssl_protocol_tls_v1_0_t); 65 | async_ssl_context_load_verify_location(&ssl_context, &ca_certificate[0]); 66 | async_ssl_context_set_verify_mode(&ssl_context, 67 | async_ssl_verify_mode_cert_none_t); 68 | echo_client_init(&echo_clients[1], "tls", 33001, &ssl_context, &async); 69 | async_run_forever(&async); 70 | 71 | return (0); 72 | } 73 | -------------------------------------------------------------------------------- /examples/stcp_echo_client/server.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDCjCCAfICCQCoXfSQydXTczANBgkqhkiG9w0BAQsFADBHMQswCQYDVQQGEwJT 3 | RTETMBEGA1UECAwKU29tZS1TdGF0ZTEOMAwGA1UECgwFU2ltYmExEzARBgNVBAMM 4 | CmZvb2Jhci5vcmcwHhcNMTcwMTA3MTgxOTQ3WhcNMTgwMTA3MTgxOTQ3WjBHMQsw 5 | CQYDVQQGEwJTRTETMBEGA1UECAwKU29tZS1TdGF0ZTEOMAwGA1UECgwFU2ltYmEx 6 | EzARBgNVBAMMCmZvb2Jhci5vcmcwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK 7 | AoIBAQDugoc9amynDB/ODJzh2aqB6cnubSJEPlgB87jNG0akcbmUFt6BhPhMPSoP 8 | esHWl5OWscoga0cKrPURmMVVhfaeZLQGmrv5N4/liVlwae1n0gUEruX4d6MqSSDW 9 | 3C/WKjCn647udZwyzCvyrvPOq0qAzaxR4EFRdwjSEPO5sDw2zxeTjGW2WxaH9PEu 10 | C8vaNqTsLYl3YBkR3zVCbuQXTQhStsv3gT4Yhz2wJpY0yyWyDiaBkGKpdxJQiNAd 11 | x5JKSqtRshlYZM3+cdKLywNoYUnezp6Wm4mzz09TCFv+esJ0h7/6pMdVjhxLsAg5 12 | ZbZyrtNIapN07AjIJS4qjkJ/HUC3AgMBAAEwDQYJKoZIhvcNAQELBQADggEBAGHS 13 | U5AvDESzTNoak5HHx166bp5+bvECvJg45GMDGhqKeFoOlR34u2b+wyTm/piz3nYJ 14 | 12kn+BbG/PwGfndL0kZYRz46fY8Rf+MxCFfcglweDJhA6ULNpera11OC35Q/lKn5 15 | M6w6eQkZMB4VqwigvDGHGpXRTIhJHHoR2VFBFGoPTLrXilChUpiXi9DmuYXJ/19x 16 | sxOVwvvO/m/6g68G+uZYUoCsQsKllM2fgdNLTzbYvnFtsq5QnZS8m7CoZgEy2c3m 17 | VrrPsfwmyhwejDawjz2epSLNXaaDeSz4g1cQes+oehaA1IwFfKhb9tdiWUm48VuU 18 | oaFZ8e1HofYUy+65pws= 19 | -----END CERTIFICATE----- 20 | -------------------------------------------------------------------------------- /examples/stcp_echo_client/server.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEpQIBAAKCAQEA7oKHPWpspwwfzgyc4dmqgenJ7m0iRD5YAfO4zRtGpHG5lBbe 3 | gYT4TD0qD3rB1peTlrHKIGtHCqz1EZjFVYX2nmS0Bpq7+TeP5YlZcGntZ9IFBK7l 4 | +HejKkkg1twv1iowp+uO7nWcMswr8q7zzqtKgM2sUeBBUXcI0hDzubA8Ns8Xk4xl 5 | tlsWh/TxLgvL2jak7C2Jd2AZEd81Qm7kF00IUrbL94E+GIc9sCaWNMslsg4mgZBi 6 | qXcSUIjQHceSSkqrUbIZWGTN/nHSi8sDaGFJ3s6elpuJs89PUwhb/nrCdIe/+qTH 7 | VY4cS7AIOWW2cq7TSGqTdOwIyCUuKo5Cfx1AtwIDAQABAoIBAQDtIH7es6Fkj2xL 8 | ikSrUqHv2YT29C++U5FiF3LjELwyIBMWRYpgkNffzGu1yiFPfZGxn/9KLf4KghyA 9 | qzd+KNshbRODAXHje7cnyV9PoTD0KFzu5Agl+KQTFXN9ypoQvg9z04FeWTW7VzxW 10 | YlRpY+mFl3Y8TM4D9WmimFIIikvDXERHtBLJRr8dde5u/6xKju1Q8qs7aSBOggws 11 | ryv/ZI/WWR7EjFy2XegU7fvByHS5MlbhaplvKLKkHSihRdXOCD66Uo+/TYiOkb9A 12 | ZVUZ0IjKS6uN3zFsaN2B3ZQQb9dWL7DZsMPXeL1u1+0WgtnVcbPyRW2fEeZJAy8M 13 | ILljkRPxAoGBAPcbhWHkTCT1HF/UHGveqqq3gLYLDDMhKTHUAxaLAyBBisRWem3L 14 | BRao478pSIOa2LthJJ9/rkWd2XWCZlvjF9PYbbO/c4PpWbvE1WfO0LX80G2YRskR 15 | kOBf3sCm25u2i6fi7E6pTf6INPRo01kxNbZG++W40lyhVfVGE7kSqDjpAoGBAPcX 16 | zTO9pzOYYbeAfFdjl8vUI+yQeAu9CSfWlqxVjU+glGIBKAAspuP4JWXFCxidBneK 17 | CqaKpxP5ZT0jUgDO/HIuvcY5ISkYJ1B/RON50/UH/PtaYEZlrHjxOacrHVrPClEc 18 | wU85pt/9cYe/xY2x5AHeSEP5BlkwEndir5x7YaifAoGBAPQMtEEoTqO5iRqDzMYk 19 | lf4hWfdxFc1xlNPnhl0/UgkLclfS6PuCpTXp+3SS+1EvH5qAqMD2Q1HyQhEyhpF9 20 | ycyqVj5OeXHwh3VRQ0tREQSL+fYjjNWMib9wL/mpUf+J3IglMCnIQIWAVHyHAyV1 21 | 2ox5stMBJJHjrZeebGA+svHxAoGBAPMhYnK0/J7SI+SR8sdyYdzYQvj1NoomWanv 22 | hpbQH5noYdkFVjzFsvnCI9o9xqtTZxecpto0uC35SlqcNIZDJTedMnKnqC9tatHU 23 | SQ6jw6HpWE5jITRjuw/OHHKXA+1kp7wkVToFemQQV/PBCTxhe/8yn1zy3gJIQQNk 24 | AXQyG2f1AoGAPzB+wQeKkL2CDH9uma8s17fH8lL5zAPOsNhlakTiCNiEj9qkOolT 25 | GkSL1HnwtCV+btGymVWPMgAwabRD4YYSDkcOqkaguerUFVc8W2rIXSzh+PaSXp9Q 26 | QSQRmqHx9I/kb1lHWPbm9z2URpwoqqNSB/475G2+XUTWxVNsTf/aA1w= 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /examples/stcp_echo_client/server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import socket 4 | 5 | port = 33000 6 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) 7 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 8 | sock.bind(('', port)) 9 | sock.listen() 10 | 11 | print(f"Waiting for the client to connect to ':{port}'.") 12 | client = sock.accept()[0] 13 | print('Client connected.') 14 | 15 | while True: 16 | client.send(client.recv(1)) 17 | -------------------------------------------------------------------------------- /examples/stcp_echo_client/ssl_server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import sys 4 | import socket 5 | import ssl 6 | 7 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) 8 | context.load_cert_chain(certfile="server.crt", 9 | keyfile="server.key") 10 | 11 | port = 33001 12 | sock = socket.socket() 13 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 14 | sock.bind(('', port)) 15 | sock.listen() 16 | 17 | print(f"Waiting for the client to connect to ':{port}'.") 18 | client = sock.accept()[0] 19 | print('Client connected.') 20 | client = context.wrap_socket(client, server_side=True) 21 | 22 | while True: 23 | client.send(client.recv(1)) 24 | -------------------------------------------------------------------------------- /examples/stcp_echo_server/Makefile: -------------------------------------------------------------------------------- 1 | SRC += echo_server.c 2 | 3 | include $(ASYNC_ROOT)/make/app.mk 4 | -------------------------------------------------------------------------------- /examples/stcp_echo_server/README.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | Showcase the secure TCP server, both with and without TLS. 5 | 6 | Compile and run 7 | =============== 8 | 9 | Start the TCP echo server. 10 | 11 | .. code-block:: text 12 | 13 | $ make -s 14 | ... 15 | 16 | Now, in another terminal, connect a client. 17 | 18 | .. code-block:: text 19 | 20 | $ ./client.py 21 | -------------------------------------------------------------------------------- /examples/stcp_echo_server/echo_server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include "async.h" 31 | #include "echo_server.h" 32 | 33 | static void on_client_connected() 34 | { 35 | printf("Client connected!\n"); 36 | } 37 | 38 | static void on_client_disconnected() 39 | { 40 | printf("Client disconnected!\n"); 41 | } 42 | 43 | static void on_client_input(struct async_stcp_server_client_t *client_p) 44 | { 45 | char buf[16]; 46 | size_t size; 47 | 48 | while (true) { 49 | size = async_stcp_server_client_read(client_p, &buf[0], sizeof(buf)); 50 | 51 | if (size == 0) { 52 | break; 53 | } 54 | 55 | async_stcp_server_client_write(client_p, &buf[0], size); 56 | } 57 | } 58 | 59 | void echo_server_init(struct echo_server_t *self_p, 60 | const char *name_p, 61 | int port, 62 | struct async_ssl_context_t *ssl_context_p, 63 | struct async_t *async_p) 64 | { 65 | self_p->name_p = name_p; 66 | self_p->port = port; 67 | async_stcp_server_init(&self_p->stcp, 68 | "127.0.0.1", 69 | port, 70 | ssl_context_p, 71 | on_client_connected, 72 | on_client_disconnected, 73 | on_client_input, 74 | async_p); 75 | async_stcp_server_add_client(&self_p->stcp, &self_p->client); 76 | async_stcp_server_start(&self_p->stcp); 77 | } 78 | -------------------------------------------------------------------------------- /examples/stcp_echo_server/echo_server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef ECHO_SERVER_H 30 | #define ECHO_SERVER_H 31 | 32 | #include "async.h" 33 | 34 | struct echo_server_t { 35 | const char *name_p; 36 | int port; 37 | struct async_stcp_server_t stcp; 38 | struct async_stcp_server_client_t client; 39 | struct async_timer_t transmit_timer; 40 | struct async_timer_t reconnect_timer; 41 | }; 42 | 43 | void echo_server_init(struct echo_server_t *self_p, 44 | const char *name_p, 45 | int port, 46 | struct async_ssl_context_t *ssl_context_p, 47 | struct async_t *async_p); 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /examples/stcp_echo_server/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include "async.h" 30 | #include "echo_server.h" 31 | 32 | static const char ca_certificate[] = ( 33 | "-----BEGIN CERTIFICATE-----\n" 34 | "MIIDCjCCAfICCQCoXfSQydXTczANBgkqhkiG9w0BAQsFADBHMQswCQYDVQQGEwJT\n" 35 | "RTETMBEGA1UECAwKU29tZS1TdGF0ZTEOMAwGA1UECgwFU2ltYmExEzARBgNVBAMM\n" 36 | "CmZvb2Jhci5vcmcwHhcNMTcwMTA3MTgxOTQ3WhcNMTgwMTA3MTgxOTQ3WjBHMQsw\n" 37 | "CQYDVQQGEwJTRTETMBEGA1UECAwKU29tZS1TdGF0ZTEOMAwGA1UECgwFU2ltYmEx\n" 38 | "EzARBgNVBAMMCmZvb2Jhci5vcmcwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\n" 39 | "AoIBAQDugoc9amynDB/ODJzh2aqB6cnubSJEPlgB87jNG0akcbmUFt6BhPhMPSoP\n" 40 | "esHWl5OWscoga0cKrPURmMVVhfaeZLQGmrv5N4/liVlwae1n0gUEruX4d6MqSSDW\n" 41 | "3C/WKjCn647udZwyzCvyrvPOq0qAzaxR4EFRdwjSEPO5sDw2zxeTjGW2WxaH9PEu\n" 42 | "C8vaNqTsLYl3YBkR3zVCbuQXTQhStsv3gT4Yhz2wJpY0yyWyDiaBkGKpdxJQiNAd\n" 43 | "x5JKSqtRshlYZM3+cdKLywNoYUnezp6Wm4mzz09TCFv+esJ0h7/6pMdVjhxLsAg5\n" 44 | "ZbZyrtNIapN07AjIJS4qjkJ/HUC3AgMBAAEwDQYJKoZIhvcNAQELBQADggEBAGHS\n" 45 | "U5AvDESzTNoak5HHx166bp5+bvECvJg45GMDGhqKeFoOlR34u2b+wyTm/piz3nYJ\n" 46 | "12kn+BbG/PwGfndL0kZYRz46fY8Rf+MxCFfcglweDJhA6ULNpera11OC35Q/lKn5\n" 47 | "M6w6eQkZMB4VqwigvDGHGpXRTIhJHHoR2VFBFGoPTLrXilChUpiXi9DmuYXJ/19x\n" 48 | "sxOVwvvO/m/6g68G+uZYUoCsQsKllM2fgdNLTzbYvnFtsq5QnZS8m7CoZgEy2c3m\n" 49 | "VrrPsfwmyhwejDawjz2epSLNXaaDeSz4g1cQes+oehaA1IwFfKhb9tdiWUm48VuU\n" 50 | "oaFZ8e1HofYUy+65pws=\n" 51 | "-----END CERTIFICATE-----\n"); 52 | 53 | int main() 54 | { 55 | struct async_t async; 56 | struct echo_server_t echo_servers[2]; 57 | struct async_ssl_context_t ssl_context; 58 | 59 | async_ssl_module_init(); 60 | async_init(&async); 61 | async_set_runtime(&async, async_runtime_create()); 62 | echo_server_init(&echo_servers[0], "tcp", 33000, NULL, &async); 63 | async_ssl_context_init(&ssl_context, 64 | async_ssl_protocol_tls_v1_0_t); 65 | async_ssl_context_load_verify_location(&ssl_context, &ca_certificate[0]); 66 | async_ssl_context_set_verify_mode(&ssl_context, 67 | async_ssl_verify_mode_cert_none_t); 68 | echo_server_init(&echo_servers[1], "tls", 33001, &ssl_context, &async); 69 | async_run_forever(&async); 70 | 71 | return (0); 72 | } 73 | -------------------------------------------------------------------------------- /examples/timers/Makefile: -------------------------------------------------------------------------------- 1 | SRC += timers.c 2 | 3 | include $(ASYNC_ROOT)/make/app.mk 4 | -------------------------------------------------------------------------------- /examples/timers/README.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | Showcase periodic and single shot timers. 5 | 6 | Compile and run 7 | =============== 8 | 9 | .. code-block:: text 10 | 11 | $ make -s 12 | ... 13 | Timer 1 expired. 14 | Timer 1 expired. 15 | Timer 1 expired. 16 | Timer 2 expired. 17 | Timer 1 expired. 18 | Timer 1 expired. 19 | Timer 3 expired. Stopping timer 1. 20 | Timer 2 expired. 21 | Timer 2 expired. 22 | Timer 2 expired. 23 | Timer 2 expired. 24 | Timer 2 expired. 25 | -------------------------------------------------------------------------------- /examples/timers/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include "async.h" 31 | #include "async/utils/linux.h" 32 | #include "timers.h" 33 | 34 | int main() 35 | { 36 | struct async_t async; 37 | struct timers_t timers; 38 | 39 | async_init(&async); 40 | timers_init(&timers, &async); 41 | 42 | while (true) { 43 | usleep(100 * 1000); 44 | async_tick(&async); 45 | async_process(&async); 46 | } 47 | 48 | return (1); 49 | } 50 | -------------------------------------------------------------------------------- /examples/timers/timers.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include "timers.h" 31 | 32 | static void on_timeout_1() 33 | { 34 | printf("Timer 1 expired.\n"); 35 | } 36 | 37 | static void on_timeout_2() 38 | { 39 | printf("Timer 2 expired.\n"); 40 | } 41 | 42 | static void on_timeout_3(struct timers_t *self_p) 43 | { 44 | printf("Timer 3 expired. Stopping timer 1.\n"); 45 | async_timer_stop(&self_p->timer_1); 46 | } 47 | 48 | void timers_init(struct timers_t *self_p, struct async_t *async_p) 49 | { 50 | async_timer_init(&self_p->timer_1, 51 | on_timeout_1, 52 | self_p, 53 | 1000, 54 | 1000, 55 | async_p); 56 | async_timer_init(&self_p->timer_2, 57 | on_timeout_2, 58 | self_p, 59 | 3000, 60 | 3000, 61 | async_p); 62 | async_timer_init(&self_p->timer_3, 63 | (async_timer_timeout_t)on_timeout_3, 64 | self_p, 65 | 5000, 66 | 5000, 67 | async_p); 68 | async_timer_start(&self_p->timer_1); 69 | async_timer_start(&self_p->timer_2); 70 | async_timer_start(&self_p->timer_3); 71 | } 72 | -------------------------------------------------------------------------------- /examples/timers/timers.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef TIMERS_H 30 | #define TIMERS_H 31 | 32 | #include "async.h" 33 | 34 | struct timers_t { 35 | struct async_timer_t timer_1; 36 | struct async_timer_t timer_2; 37 | struct async_timer_t timer_3; 38 | }; 39 | 40 | /** 41 | * Initialize the timers. 42 | */ 43 | void timers_init(struct timers_t *self_p, struct async_t *async_p); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /include/async.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef ASYNC_H 30 | #define ASYNC_H 31 | 32 | #include "async/core.h" 33 | #include "async/modules/ssl.h" 34 | #include "async/modules/stcp_client.h" 35 | #include "async/modules/stcp_server.h" 36 | #include "async/modules/mqtt_client.h" 37 | #include "async/modules/shell.h" 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/async/core.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef ASYNC_CORE_H 30 | #define ASYNC_CORE_H 31 | 32 | #define ASYNC_VERSION "0.10.0" 33 | 34 | #include "async/core/core.h" 35 | #include "async/core/channel.h" 36 | #include "async/core/tcp_client.h" 37 | #include "async/core/tcp_server.h" 38 | #include "async/core/runtime.h" 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /include/async/core/tcp_client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef ASYNC_CORE_TCP_CLIENT_H 30 | #define ASYNC_CORE_TCP_CLIENT_H 31 | 32 | #include "async/core/core.h" 33 | 34 | struct async_tcp_client_t { 35 | struct async_t *async_p; 36 | void *obj_p; 37 | }; 38 | 39 | typedef void (*async_tcp_client_connected_t)(struct async_tcp_client_t *self_p, 40 | int res); 41 | 42 | typedef void (*async_tcp_client_disconnected_t)(struct async_tcp_client_t *self_p); 43 | 44 | typedef void (*async_tcp_client_input_t)(struct async_tcp_client_t *self_p); 45 | 46 | /** 47 | * Initialize given TCP client object. 48 | */ 49 | void async_tcp_client_init(struct async_tcp_client_t *self_p, 50 | async_tcp_client_connected_t on_connected, 51 | async_tcp_client_disconnected_t on_disconnected, 52 | async_tcp_client_input_t on_input, 53 | struct async_t *async_p); 54 | 55 | /** 56 | * Opens a TCP connection to a remote host. on_connect_complete is 57 | * called once completed. 58 | */ 59 | void async_tcp_client_connect(struct async_tcp_client_t *self_p, 60 | const char *host_p, 61 | int port); 62 | 63 | /** 64 | * Disconnect from the remote host. 65 | */ 66 | void async_tcp_client_disconnect(struct async_tcp_client_t *self_p); 67 | 68 | /** 69 | * Write size bytes to the remote host. 70 | */ 71 | void async_tcp_client_write(struct async_tcp_client_t *self_p, 72 | const void *buf_p, 73 | size_t size); 74 | 75 | /** 76 | * Read up to size bytes from the remote host. Returns the number of 77 | * read bytes (0..size). 78 | */ 79 | size_t async_tcp_client_read(struct async_tcp_client_t *self_p, 80 | void *buf_p, 81 | size_t size); 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /include/async/core/tcp_server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef ASYNC_CORE_TCP_SERVER_H 30 | #define ASYNC_CORE_TCP_SERVER_H 31 | 32 | #include "async/core/core.h" 33 | 34 | struct async_tcp_server_client_t; 35 | 36 | typedef void (*async_tcp_server_client_connected_t)( 37 | struct async_tcp_server_client_t *self_p); 38 | 39 | typedef void (*async_tcp_server_client_disconnected_t)( 40 | struct async_tcp_server_client_t *self_p); 41 | 42 | typedef void (*async_tcp_server_client_input_t)( 43 | struct async_tcp_server_client_t *self_p); 44 | 45 | struct async_tcp_server_t { 46 | struct async_t *async_p; 47 | struct { 48 | struct async_tcp_server_client_t *used_p; 49 | struct async_tcp_server_client_t *free_p; 50 | } clients; 51 | void *obj_p; 52 | }; 53 | 54 | struct async_tcp_server_client_t { 55 | struct async_tcp_server_t *server_p; 56 | struct async_tcp_server_client_t *next_p; 57 | struct async_tcp_server_client_t *prev_p; 58 | void *obj_p; 59 | }; 60 | 61 | /** 62 | * Initialize given TCP server object. 63 | */ 64 | void async_tcp_server_init(struct async_tcp_server_t *self_p, 65 | const char *host_p, 66 | int port, 67 | async_tcp_server_client_connected_t on_connected, 68 | async_tcp_server_client_disconnected_t on_disconnected, 69 | async_tcp_server_client_input_t on_input, 70 | struct async_t *async_p); 71 | 72 | /** 73 | * Add given client to given server. 74 | */ 75 | void async_tcp_server_add_client(struct async_tcp_server_t *self_p, 76 | struct async_tcp_server_client_t *client_p); 77 | 78 | /** 79 | * Start listening for clients. Returns zero(0) on success. 80 | */ 81 | int async_tcp_server_start(struct async_tcp_server_t *self_p); 82 | 83 | /** 84 | * Disconnect any connected clients and stop listening for clients. 85 | */ 86 | void async_tcp_server_stop(struct async_tcp_server_t *self_p); 87 | 88 | /** 89 | * Write size bytes to the remote host. 90 | */ 91 | void async_tcp_server_client_write(struct async_tcp_server_client_t *self_p, 92 | const void *buf_p, 93 | size_t size); 94 | 95 | /** 96 | * Read up to size bytes from the remote host. Returns the number of 97 | * read bytes (0..size). 98 | */ 99 | size_t async_tcp_server_client_read(struct async_tcp_server_client_t *self_p, 100 | void *buf_p, 101 | size_t size); 102 | 103 | /** 104 | * Disconnect given client. 105 | */ 106 | void async_tcp_server_client_disconnect(struct async_tcp_server_client_t *self_p); 107 | 108 | #endif 109 | -------------------------------------------------------------------------------- /include/async/modules/stcp_client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | /* 30 | * The secure TCP client is a TCP client with optional encryption 31 | * using SSL/TLS. 32 | */ 33 | 34 | #ifndef ASYNC_STCP_CLIENT_H 35 | #define ASYNC_STCP_CLIENT_H 36 | 37 | #include "async/core.h" 38 | #include "async/modules/ssl.h" 39 | 40 | struct async_stcp_client_t; 41 | 42 | typedef void (*async_stcp_client_connected_t)(struct async_stcp_client_t *self_p, 43 | int res); 44 | 45 | typedef void (*async_stcp_client_disconnected_t)(struct async_stcp_client_t *self_p); 46 | 47 | typedef void (*async_stcp_client_input_t)(struct async_stcp_client_t *self_p); 48 | 49 | struct async_stcp_client_t { 50 | struct async_tcp_client_t tcp; 51 | struct { 52 | struct async_ssl_context_t *context_p; 53 | struct async_ssl_connection_t connection; 54 | } ssl; 55 | async_stcp_client_connected_t on_connected; 56 | async_stcp_client_disconnected_t on_disconnected; 57 | async_stcp_client_input_t on_input; 58 | struct async_t *async_p; 59 | }; 60 | 61 | typedef void (*async_stcp_client_connected_t)( 62 | struct async_stcp_client_t *self_p, 63 | int res); 64 | 65 | typedef void (*async_stcp_client_disconnected_t)( 66 | struct async_stcp_client_t *self_p); 67 | 68 | typedef void (*async_stcp_client_input_t)( 69 | struct async_stcp_client_t *self_p); 70 | 71 | /** 72 | * Initialize given secure TCP client object. 73 | */ 74 | void async_stcp_client_init(struct async_stcp_client_t *self_p, 75 | struct async_ssl_context_t *ssl_context_p, 76 | async_stcp_client_connected_t on_connected, 77 | async_stcp_client_disconnected_t on_disconnected, 78 | async_stcp_client_input_t on_input, 79 | struct async_t *async_p); 80 | 81 | /** 82 | * Opens a secure TCP connection to a remote host. on_connect_complete 83 | * is called once completed. 84 | */ 85 | void async_stcp_client_connect(struct async_stcp_client_t *self_p, 86 | const char *host_p, 87 | int port); 88 | 89 | /** 90 | * Disconnect from the remote host. 91 | */ 92 | void async_stcp_client_disconnect(struct async_stcp_client_t *self_p); 93 | 94 | /** 95 | * Write size bytes to the remote host. 96 | */ 97 | void async_stcp_client_write(struct async_stcp_client_t *self_p, 98 | const void *buf_p, 99 | size_t size); 100 | 101 | /** 102 | * Read up to size bytes from the remote host. Returns the number of 103 | * read bytes (0..size). 104 | */ 105 | size_t async_stcp_client_read(struct async_stcp_client_t *self_p, 106 | void *buf_p, 107 | size_t size); 108 | 109 | #endif 110 | -------------------------------------------------------------------------------- /include/async/runtimes/linux.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef ASYNC_RUNTIME_LINUX_H 30 | #define ASYNC_RUNTIME_LINUX_H 31 | 32 | #include "async/core/runtime.h" 33 | 34 | struct async_runtime_t *async_runtime_linux_create(void); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /include/async/runtimes/monolinux.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef ASYNC_RUNTIME_MONOLINUX_H 30 | #define ASYNC_RUNTIME_MONOLINUX_H 31 | 32 | #include "async/core/runtime.h" 33 | 34 | struct async_runtime_t *async_runtime_monolinux_create(void); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /include/async/utils/linux.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef ASYNC_UTILS_LINUX_H 30 | #define ASYNC_UTILS_LINUX_H 31 | 32 | #include "async/core.h" 33 | 34 | /** 35 | * Create a periodic timer and start it with the async period. Returns 36 | * the timer file descriptor. 37 | */ 38 | int async_utils_linux_create_periodic_timer(struct async_t *async_p); 39 | 40 | /** 41 | * Called when the timer expirs. Reads from the timer file descriptor 42 | * and calls the async tick function. 43 | */ 44 | void async_utils_linux_handle_timeout(struct async_t *async_p, 45 | int timer_fd); 46 | 47 | /** 48 | * Both creates and add the timer file descriptor to given epoll 49 | * instance. Returns the timer file descriptor. 50 | */ 51 | int async_utils_linux_init_periodic_timer(struct async_t *async_p, 52 | int epoll_fd); 53 | 54 | /** 55 | * Create an epoll instance and return its handle. 56 | */ 57 | int async_utils_linux_epoll_create(void); 58 | 59 | /** 60 | * Add given file descriptor to given epoll instance with EPOLLIN. 61 | */ 62 | void async_utils_linux_epoll_add_in(int epoll_fd, int fd); 63 | 64 | void async_utils_linux_channel_stdin_init(struct async_channel_t *channel_p, 65 | struct async_t *async_p); 66 | 67 | void async_utils_linux_channel_stdin_handle(struct async_channel_t *channel_p); 68 | 69 | void async_utils_linux_fatal_perror(const char *message_p); 70 | 71 | void async_utils_linux_init_stdin(int epoll_fd); 72 | 73 | void async_utils_linux_make_stdin_unbuffered(void); 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /make/app.mk: -------------------------------------------------------------------------------- 1 | BUILD = $(shell readlink -f build) 2 | EXE = $(BUILD)/app 3 | CFLAGS += -fno-omit-frame-pointer 4 | CFLAGS += -Wall -Wextra -std=gnu11 5 | CFLAGS += -g -Og 6 | SRC += main.c 7 | 8 | .PHONY: all run build clean 9 | 10 | all: run 11 | 12 | build: $(EXE) 13 | 14 | run: $(EXE) 15 | $(EXE) 16 | 17 | clean: 18 | rm -rf $(BUILD) $(CLEAN) 19 | 20 | include $(ASYNC_ROOT)/make/common.mk 21 | -------------------------------------------------------------------------------- /make/library.mk: -------------------------------------------------------------------------------- 1 | CC = $(CROSS_COMPILE)gcc 2 | 3 | INC += $(ASYNC_ROOT)/include 4 | 5 | SRC += $(ASYNC_ROOT)/src/core/async_core.c 6 | SRC += $(ASYNC_ROOT)/src/core/async_timer.c 7 | SRC += $(ASYNC_ROOT)/src/core/async_channel.c 8 | SRC += $(ASYNC_ROOT)/src/core/async_tcp_client.c 9 | SRC += $(ASYNC_ROOT)/src/core/async_tcp_server.c 10 | SRC += $(ASYNC_ROOT)/src/core/async_runtime_null.c 11 | SRC += $(ASYNC_ROOT)/src/modules/async_stcp_client.c 12 | SRC += $(ASYNC_ROOT)/src/modules/async_stcp_server.c 13 | SRC += $(ASYNC_ROOT)/src/modules/async_ssl.c 14 | SRC += $(ASYNC_ROOT)/src/modules/async_shell.c 15 | SRC += $(ASYNC_ROOT)/src/modules/async_mqtt_client.c 16 | SRC += $(ASYNC_ROOT)/src/runtimes/async_runtime.c 17 | SRC += $(ASYNC_ROOT)/src/runtimes/async_runtime_linux.c 18 | SRC += $(ASYNC_ROOT)/src/utils/async_utils_linux.c 19 | 20 | OBJ = $(patsubst %,$(BUILD)%,$(abspath $(SRC:%.c=%.o))) 21 | 22 | CFLAGS += $(INC:%=-I%) 23 | CFLAGS += -ffunction-sections -fdata-sections 24 | CFLAGS += -D_GNU_SOURCE=1 25 | CFLAGS += $(CFLAGS_EXTRA) 26 | 27 | LDFLAGS += -Wl,--gc-sections 28 | 29 | DEPSDIR = $(BUILD)/deps 30 | 31 | .PHONY: clean 32 | 33 | app: $(EXE) 34 | 35 | $(EXE): $(OBJ) 36 | @echo "LD $@" 37 | mkdir -p $(BUILD) 38 | $(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_MOCKS) -lpthread -o $@ 39 | 40 | define COMPILE_template 41 | -include $(patsubst %.c,$(DEPSDIR)%.o.dep,$(abspath $1)) 42 | $(patsubst %.c,$(BUILD)%.o,$(abspath $1)): $1 43 | @echo "CC $1" 44 | mkdir -p $(BUILD)$(abspath $(dir $1)) 45 | mkdir -p $(DEPSDIR)$(abspath $(dir $1)) 46 | $$(CC) $$(CFLAGS) -c -o $$@ $$< 47 | $(CC) -MM -MT $$@ $$(CFLAGS) -o $(patsubst %.c,$(DEPSDIR)%.o.dep,$(abspath $1)) $$< 48 | endef 49 | $(foreach file,$(SRC),$(eval $(call COMPILE_template,$(file)))) 50 | 51 | print-%: 52 | @echo $($*) 53 | -------------------------------------------------------------------------------- /make/lsan-suppressions.txt: -------------------------------------------------------------------------------- 1 | # Known leaks 2 | leak:ml.c 3 | leak:async_runtime_linux_create 4 | leak:io_main 5 | -------------------------------------------------------------------------------- /setup.sh: -------------------------------------------------------------------------------- 1 | export ASYNC_ROOT=$(readlink -f .) 2 | -------------------------------------------------------------------------------- /src/core/async_tcp_client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include 31 | #include "async/core.h" 32 | #include "async/core/runtime.h" 33 | 34 | static void on_connected_default(struct async_tcp_client_t *self_p, int res) 35 | { 36 | (void)self_p; 37 | (void)res; 38 | } 39 | 40 | static void on_disconnected_default(struct async_tcp_client_t *self_p) 41 | { 42 | (void)self_p; 43 | } 44 | 45 | static void on_input_default(struct async_tcp_client_t *self_p) 46 | { 47 | char buf[32]; 48 | size_t size; 49 | 50 | do { 51 | size = async_tcp_client_read(self_p, &buf[0], sizeof(buf)); 52 | } while (size > 0); 53 | } 54 | 55 | void async_tcp_client_init(struct async_tcp_client_t *self_p, 56 | async_tcp_client_connected_t on_connected, 57 | async_tcp_client_disconnected_t on_disconnected, 58 | async_tcp_client_input_t on_input, 59 | struct async_t *async_p) 60 | { 61 | if (on_connected == NULL) { 62 | on_connected = on_connected_default; 63 | } 64 | 65 | if (on_disconnected == NULL) { 66 | on_disconnected = on_disconnected_default; 67 | } 68 | 69 | if (on_input == NULL) { 70 | on_input = on_input_default; 71 | } 72 | 73 | self_p->async_p = async_p; 74 | async_p->runtime_p->tcp_client.init(self_p, 75 | on_connected, 76 | on_disconnected, 77 | on_input); 78 | } 79 | 80 | void async_tcp_client_connect(struct async_tcp_client_t *self_p, 81 | const char *host_p, 82 | int port) 83 | { 84 | self_p->async_p->runtime_p->tcp_client.connect(self_p, host_p, port); 85 | } 86 | 87 | void async_tcp_client_disconnect(struct async_tcp_client_t *self_p) 88 | { 89 | self_p->async_p->runtime_p->tcp_client.disconnect(self_p); 90 | } 91 | 92 | void async_tcp_client_write(struct async_tcp_client_t *self_p, 93 | const void *buf_p, 94 | size_t size) 95 | { 96 | self_p->async_p->runtime_p->tcp_client.write(self_p, buf_p, size); 97 | } 98 | 99 | size_t async_tcp_client_read(struct async_tcp_client_t *self_p, 100 | void *buf_p, 101 | size_t size) 102 | { 103 | return (self_p->async_p->runtime_p->tcp_client.read(self_p, buf_p, size)); 104 | } 105 | -------------------------------------------------------------------------------- /src/core/async_tcp_server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include 31 | #include "async/core.h" 32 | #include "async/core/runtime.h" 33 | 34 | static void on_connected_default() 35 | { 36 | } 37 | 38 | static void on_disconnected_default() 39 | { 40 | } 41 | 42 | static void on_input_default(struct async_tcp_server_client_t *self_p) 43 | { 44 | char buf[32]; 45 | size_t size; 46 | 47 | do { 48 | size = async_tcp_server_client_read(self_p, &buf[0], sizeof(buf)); 49 | } while (size > 0); 50 | } 51 | 52 | void async_tcp_server_init(struct async_tcp_server_t *self_p, 53 | const char *host_p, 54 | int port, 55 | async_tcp_server_client_connected_t on_connected, 56 | async_tcp_server_client_disconnected_t on_disconnected, 57 | async_tcp_server_client_input_t on_input, 58 | struct async_t *async_p) 59 | { 60 | if (on_connected == NULL) { 61 | on_connected = on_connected_default; 62 | } 63 | 64 | if (on_disconnected == NULL) { 65 | on_disconnected = on_disconnected_default; 66 | } 67 | 68 | if (on_input == NULL) { 69 | on_input = on_input_default; 70 | } 71 | 72 | self_p->async_p = async_p; 73 | async_p->runtime_p->tcp_server.init(self_p, 74 | host_p, 75 | port, 76 | on_connected, 77 | on_disconnected, 78 | on_input); 79 | } 80 | 81 | void async_tcp_server_add_client(struct async_tcp_server_t *self_p, 82 | struct async_tcp_server_client_t *client_p) 83 | { 84 | client_p->server_p = self_p; 85 | self_p->async_p->runtime_p->tcp_server.add_client(self_p, client_p); 86 | } 87 | 88 | int async_tcp_server_start(struct async_tcp_server_t *self_p) 89 | { 90 | return (self_p->async_p->runtime_p->tcp_server.start(self_p)); 91 | } 92 | 93 | void async_tcp_server_stop(struct async_tcp_server_t *self_p) 94 | { 95 | self_p->async_p->runtime_p->tcp_server.stop(self_p); 96 | } 97 | 98 | void async_tcp_server_client_write(struct async_tcp_server_client_t *self_p, 99 | const void *buf_p, 100 | size_t size) 101 | { 102 | self_p->server_p->async_p->runtime_p->tcp_server.client.write(self_p, 103 | buf_p, 104 | size); 105 | } 106 | 107 | size_t async_tcp_server_client_read(struct async_tcp_server_client_t *self_p, 108 | void *buf_p, 109 | size_t size) 110 | { 111 | return (self_p->server_p->async_p->runtime_p->tcp_server.client.read(self_p, 112 | buf_p, 113 | size)); 114 | } 115 | 116 | void async_tcp_server_client_disconnect(struct async_tcp_server_client_t *self_p) 117 | { 118 | self_p->server_p->async_p->runtime_p->tcp_server.client.disconnect(self_p); 119 | } 120 | -------------------------------------------------------------------------------- /src/core/internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef ASYNC_INTERNAL_H 30 | #define ASYNC_INTERNAL_H 31 | 32 | #include "async/core.h" 33 | 34 | #define DIV_CEIL(a, b) (((a) + (b) - 1) / (b)) 35 | 36 | void async_timer_list_init(struct async_timer_list_t *self_p); 37 | 38 | void async_timer_list_tick(struct async_timer_list_t *self_p); 39 | 40 | void async_timer_list_process(struct async_timer_list_t *self_p); 41 | 42 | int async_timer_list_next_timeout(struct async_timer_list_t *self_p); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /src/runtimes/async_runtime.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include "async/runtimes/linux.h" 30 | 31 | struct async_runtime_t *async_runtime_create(void) 32 | { 33 | return (async_runtime_linux_create()); 34 | } 35 | -------------------------------------------------------------------------------- /tst/Makefile: -------------------------------------------------------------------------------- 1 | TESTS += test_core_channel.c 2 | TESTS += test_core_core.c 3 | TESTS += test_core_runtime_null.c 4 | TESTS += test_core_tcp_client.c 5 | TESTS += test_core_tcp_server.c 6 | TESTS += test_core_timer.c 7 | TESTS += test_mqtt_client.c 8 | TESTS += test_shell.c 9 | TESTS += test_runtime.c 10 | 11 | include test.mk 12 | -------------------------------------------------------------------------------- /tst/test_core_channel.c: -------------------------------------------------------------------------------- 1 | #include "nala.h" 2 | #include "async.h" 3 | 4 | TEST(init_use_default_callbacks) 5 | { 6 | struct async_t async; 7 | struct async_channel_t channel; 8 | char ch; 9 | 10 | async_init(&async); 11 | async_channel_init(&channel, NULL, NULL, NULL, NULL, NULL, NULL, &async); 12 | ASSERT_EQ(async_channel_open(&channel), 0); 13 | ASSERT_EQ(async_channel_read(&channel, &ch, 1), 1); 14 | ch = 1; 15 | async_channel_write(&channel, &ch, 1); 16 | async_channel_close(&channel); 17 | async_channel_closed(&channel); 18 | async_channel_input(&channel); 19 | ASSERT_EQ(async_channel_readable_size(&channel), 0); 20 | ASSERT_EQ(async_channel_writable_size(&channel), 0); 21 | async_process(&async); 22 | } 23 | 24 | int init_use_non_default_callbacks_open(struct async_channel_t *self_p) 25 | { 26 | (void)self_p; 27 | 28 | return (0); 29 | } 30 | 31 | void init_use_non_default_callbacks_close(struct async_channel_t *self_p) 32 | { 33 | (void)self_p; 34 | } 35 | 36 | size_t init_use_non_default_callbacks_read(struct async_channel_t *self_p, 37 | void *buf_p, 38 | size_t size) 39 | { 40 | (void)self_p; 41 | (void)buf_p; 42 | 43 | return (size); 44 | } 45 | 46 | void init_use_non_default_callbacks_write(struct async_channel_t *self_p, 47 | const void *buf_p, 48 | size_t size) 49 | { 50 | (void)self_p; 51 | (void)buf_p; 52 | (void)size; 53 | } 54 | 55 | size_t init_use_non_default_callbacks_readable_size(struct async_channel_t *self_p) 56 | { 57 | (void)self_p; 58 | 59 | return (0); 60 | } 61 | 62 | size_t init_use_non_default_callbacks_writable_size(struct async_channel_t *self_p) 63 | { 64 | (void)self_p; 65 | 66 | return (0); 67 | } 68 | 69 | TEST(init_use_non_default_callbacks) 70 | { 71 | struct async_t async; 72 | struct async_channel_t channel; 73 | char ch; 74 | 75 | async_init(&async); 76 | async_channel_init(&channel, 77 | init_use_non_default_callbacks_open, 78 | init_use_non_default_callbacks_close, 79 | init_use_non_default_callbacks_read, 80 | init_use_non_default_callbacks_write, 81 | init_use_non_default_callbacks_readable_size, 82 | init_use_non_default_callbacks_writable_size, 83 | &async); 84 | init_use_non_default_callbacks_open_mock_once(5); 85 | ASSERT_EQ(async_channel_open(&channel), 5); 86 | init_use_non_default_callbacks_read_mock_once(1, 6); 87 | ASSERT_EQ(async_channel_read(&channel, &ch, 1), 6); 88 | ch = 1; 89 | init_use_non_default_callbacks_write_mock_once(1); 90 | async_channel_write(&channel, &ch, 1); 91 | init_use_non_default_callbacks_close_mock_once(); 92 | async_channel_close(&channel); 93 | async_channel_closed(&channel); 94 | init_use_non_default_callbacks_readable_size_mock_once(7); 95 | ASSERT_EQ(async_channel_readable_size(&channel), 7); 96 | init_use_non_default_callbacks_writable_size_mock_once(8); 97 | ASSERT_EQ(async_channel_writable_size(&channel), 8); 98 | async_process(&async); 99 | } 100 | 101 | TEST(set_on_null) 102 | { 103 | struct async_t async; 104 | struct async_channel_t channel; 105 | char ch; 106 | 107 | async_init(&async); 108 | async_channel_init(&channel, NULL, NULL, NULL, NULL, NULL, NULL, &async); 109 | async_channel_set_on(&channel, NULL, NULL, NULL); 110 | ASSERT_EQ(async_channel_open(&channel), 0); 111 | ASSERT_EQ(async_channel_read(&channel, &ch, 1), 1); 112 | ch = 1; 113 | async_channel_write(&channel, &ch, 1); 114 | async_channel_close(&channel); 115 | async_channel_closed(&channel); 116 | async_channel_input(&channel); 117 | ASSERT_EQ(async_channel_readable_size(&channel), 0); 118 | ASSERT_EQ(async_channel_writable_size(&channel), 0); 119 | async_process(&async); 120 | } 121 | -------------------------------------------------------------------------------- /tst/test_core_tcp_client.c: -------------------------------------------------------------------------------- 1 | #include "nala.h" 2 | #include "async.h" 3 | #include "runtime_test.h" 4 | 5 | TEST(call_all_functions) 6 | { 7 | struct async_t async; 8 | struct async_tcp_client_t tcp; 9 | 10 | async_init(&async); 11 | 12 | runtime_test_set_async_mock(); 13 | async_set_runtime(&async, runtime_test_create()); 14 | 15 | runtime_test_tcp_client_init_mock_once(); 16 | async_tcp_client_init(&tcp, NULL, NULL, NULL, &async); 17 | 18 | runtime_test_tcp_client_connect_mock_once("foo", 5); 19 | async_tcp_client_connect(&tcp, "foo", 5); 20 | 21 | runtime_test_tcp_client_write_mock_once(3); 22 | async_tcp_client_write(&tcp, NULL, 3); 23 | 24 | runtime_test_tcp_client_read_mock_once(5, 6); 25 | ASSERT_EQ(async_tcp_client_read(&tcp, NULL, 5), 6u); 26 | 27 | runtime_test_tcp_client_disconnect_mock_once(); 28 | async_tcp_client_disconnect(&tcp); 29 | } 30 | 31 | TEST(call_default_callbacks) 32 | { 33 | struct async_t async; 34 | struct async_tcp_client_t tcp; 35 | int handle; 36 | struct nala_runtime_test_tcp_client_init_params_t *params_p; 37 | 38 | async_init(&async); 39 | 40 | runtime_test_set_async_mock(); 41 | async_set_runtime(&async, runtime_test_create()); 42 | 43 | handle = runtime_test_tcp_client_init_mock_once(); 44 | async_tcp_client_init(&tcp, NULL, NULL, NULL, &async); 45 | 46 | runtime_test_tcp_client_connect_mock_once("bar", 51); 47 | async_tcp_client_connect(&tcp, "bar", 51); 48 | 49 | params_p = runtime_test_tcp_client_init_mock_get_params_in(handle); 50 | 51 | /* Call the callbacks. */ 52 | params_p->on_connected(&tcp, 0); 53 | runtime_test_tcp_client_read_mock_once(32, 30); 54 | runtime_test_tcp_client_read_mock_once(32, 0); 55 | params_p->on_input(&tcp); 56 | params_p->on_disconnected(&tcp); 57 | } 58 | -------------------------------------------------------------------------------- /tst/test_core_tcp_server.c: -------------------------------------------------------------------------------- 1 | #include "nala.h" 2 | #include "async.h" 3 | #include "runtime_test.h" 4 | 5 | TEST(call_all_functions) 6 | { 7 | struct async_t async; 8 | struct async_tcp_server_t server; 9 | struct async_tcp_server_client_t client; 10 | 11 | async_init(&async); 12 | 13 | runtime_test_set_async_mock(); 14 | async_set_runtime(&async, runtime_test_create()); 15 | 16 | runtime_test_tcp_server_init_mock_once("127.0.0.1", 4444); 17 | async_tcp_server_init(&server, "127.0.0.1", 4444, NULL, NULL, NULL, &async); 18 | 19 | runtime_test_tcp_server_add_client_mock_once(); 20 | async_tcp_server_add_client(&server, &client); 21 | 22 | runtime_test_tcp_server_start_mock_once(1); 23 | ASSERT_EQ(async_tcp_server_start(&server), 1); 24 | 25 | runtime_test_tcp_server_client_write_mock_once(5); 26 | async_tcp_server_client_write(&client, NULL, 5); 27 | 28 | runtime_test_tcp_server_client_read_mock_once(5, 6); 29 | ASSERT_EQ(async_tcp_server_client_read(&client, NULL, 5), 6u); 30 | 31 | runtime_test_tcp_server_client_disconnect_mock_once(); 32 | async_tcp_server_client_disconnect(&client); 33 | 34 | runtime_test_tcp_server_stop_mock_once(); 35 | async_tcp_server_stop(&server); 36 | } 37 | 38 | TEST(call_default_callbacks) 39 | { 40 | struct async_t async; 41 | struct async_tcp_server_t server; 42 | struct async_tcp_server_client_t client; 43 | int handle; 44 | struct nala_runtime_test_tcp_server_init_params_t *params_p; 45 | 46 | async_init(&async); 47 | 48 | runtime_test_set_async_mock(); 49 | async_set_runtime(&async, runtime_test_create()); 50 | 51 | handle = runtime_test_tcp_server_init_mock_once("127.0.0.2", 4445); 52 | async_tcp_server_init(&server, "127.0.0.2", 4445, NULL, NULL, NULL, &async); 53 | 54 | runtime_test_tcp_server_add_client_mock_once(); 55 | async_tcp_server_add_client(&server, &client); 56 | 57 | runtime_test_tcp_server_start_mock_once(0); 58 | ASSERT_EQ(async_tcp_server_start(&server), 0); 59 | 60 | params_p = runtime_test_tcp_server_init_mock_get_params_in(handle); 61 | 62 | /* Call the callbacks. */ 63 | params_p->on_connected(&client); 64 | runtime_test_tcp_server_client_read_mock_once(32, 30); 65 | runtime_test_tcp_server_client_read_mock_once(32, 0); 66 | params_p->on_input(&client); 67 | params_p->on_disconnected(&client); 68 | } 69 | -------------------------------------------------------------------------------- /tst/test_shell.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "nala.h" 4 | #include "async.h" 5 | #include "async/modules/shell.h" 6 | #include "utils.h" 7 | 8 | static void mock_prepare_output(const char *output_p) 9 | { 10 | channel_write_mock_once(strlen(output_p)); 11 | channel_write_mock_set_buf_p_in(output_p, strlen(output_p)); 12 | } 13 | 14 | static void mock_prepare_input(char *input_p) 15 | { 16 | channel_read_mock_once(strlen(input_p), strlen(input_p)); 17 | channel_read_mock_set_buf_p_out(input_p, strlen(input_p)); 18 | } 19 | 20 | static void mock_prepare_input_zero(void) 21 | { 22 | channel_read_mock_once(1, 0); 23 | } 24 | 25 | static void mock_prepare_command(char *input_p, 26 | const char *output_p) 27 | { 28 | while (*input_p != '\0') { 29 | channel_read_mock_once(1, 1); 30 | channel_read_mock_set_buf_p_out(input_p, 1); 31 | input_p++; 32 | } 33 | 34 | while (*output_p != '\0') { 35 | channel_write_mock_once(1); 36 | channel_write_mock_set_buf_p_in(output_p, 1); 37 | output_p++; 38 | } 39 | } 40 | 41 | static struct async_t async; 42 | static struct async_shell_t shell; 43 | static struct async_channel_t channel; 44 | 45 | static void setup(void) 46 | { 47 | async_init(&async); 48 | async_channel_init(&channel, 49 | channel_open, 50 | channel_close, 51 | channel_read, 52 | channel_write, 53 | NULL, 54 | NULL, 55 | &async); 56 | 57 | async_shell_init(&shell, &channel, &async); 58 | async_process(&async); 59 | 60 | channel_open_mock_once(0); 61 | channel_write_mock_once(30); 62 | channel_write_mock_set_buf_p_in("\nWelcome to the async shell!\n\n", 30); 63 | channel_write_mock_once(2); 64 | channel_write_mock_set_buf_p_in("$ ", 2); 65 | async_shell_start(&shell); 66 | async_process(&async); 67 | } 68 | 69 | static void teardown(void) 70 | { 71 | channel_close_mock_once(); 72 | async_shell_stop(&shell); 73 | async_process(&async); 74 | } 75 | 76 | TEST(command_help) 77 | { 78 | setup(); 79 | mock_prepare_command("help\n", "help\n"); 80 | mock_prepare_output( 81 | "Cursor movement\n" 82 | "\n" 83 | " LEFT Go left one character.\n" 84 | " RIGHT Go right one character.\n" 85 | " HOME/Ctrl+A Go to the beginning of the line.\n" 86 | " END/Ctrl+E Go to the end of the line.\n" 87 | "\n" 88 | "Edit\n" 89 | "\n" 90 | " Alt+D Delete the word at the cursor.\n" 91 | " Ctrl+D Delete the chracter at the cursor.\n" 92 | " Ctrl+K Cut the line from cursor to end.\n" 93 | " Ctrl+T Swap the last two characters before the cursor " 94 | "(typo).\n" 95 | " TAB Tab completion for command names.\n" 96 | " BACKSPACE Delete the character before the cursor.\n" 97 | "\n" 98 | "History\n" 99 | "\n" 100 | " UP Previous command.\n" 101 | " DOWN Next command.\n" 102 | "\n" 103 | "Commands\n" 104 | "\n"); 105 | mock_prepare_output(" help Print this help.\n"); 106 | mock_prepare_output(" history List command history.\n"); 107 | mock_prepare_output("OK\n"); 108 | mock_prepare_output("$ "); 109 | async_channel_input(&channel); 110 | async_process(&async); 111 | teardown(); 112 | } 113 | 114 | TEST(command_history) 115 | { 116 | setup(); 117 | mock_prepare_command("history\n", "history\n"); 118 | mock_prepare_output("1"); 119 | mock_prepare_output(": "); 120 | mock_prepare_output("history"); 121 | mock_prepare_output("\n"); 122 | mock_prepare_output("OK\n"); 123 | mock_prepare_output("$ "); 124 | async_channel_input(&channel); 125 | async_process(&async); 126 | teardown(); 127 | } 128 | 129 | TEST(autocomplete_no_input) 130 | { 131 | setup(); 132 | mock_prepare_input("\t"); 133 | mock_prepare_input_zero(); 134 | mock_prepare_output("h"); 135 | async_channel_input(&channel); 136 | async_process(&async); 137 | 138 | mock_prepare_input("e"); 139 | mock_prepare_input("\t"); 140 | mock_prepare_input_zero(); 141 | mock_prepare_output("e"); 142 | mock_prepare_output("lp "); 143 | async_channel_input(&channel); 144 | async_process(&async); 145 | teardown(); 146 | } 147 | -------------------------------------------------------------------------------- /tst/utils/runtime_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #include 30 | #include "nala.h" 31 | #include "async/core/runtime.h" 32 | #include "async/runtimes/linux.h" 33 | #include "runtime_test.h" 34 | 35 | static struct async_runtime_t runtime = { 36 | .set_async = runtime_test_set_async, 37 | .run_forever = runtime_test_run_forever, 38 | .tcp_client = { 39 | .init = runtime_test_tcp_client_init, 40 | .connect = runtime_test_tcp_client_connect, 41 | .disconnect = runtime_test_tcp_client_disconnect, 42 | .write = runtime_test_tcp_client_write, 43 | .read = runtime_test_tcp_client_read 44 | }, 45 | .tcp_server = { 46 | .init = runtime_test_tcp_server_init, 47 | .add_client = runtime_test_tcp_server_add_client, 48 | .start = runtime_test_tcp_server_start, 49 | .stop = runtime_test_tcp_server_stop, 50 | .client = { 51 | .write = runtime_test_tcp_server_client_write, 52 | .read = runtime_test_tcp_server_client_read, 53 | .disconnect = runtime_test_tcp_server_client_disconnect 54 | } 55 | } 56 | }; 57 | 58 | struct async_runtime_t *runtime_test_create() 59 | { 60 | return (&runtime); 61 | } 62 | -------------------------------------------------------------------------------- /tst/utils/runtime_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the Async project. 27 | */ 28 | 29 | #ifndef RUNTIME_TEST_H 30 | #define RUNTIME_TEST_H 31 | 32 | #include "async.h" 33 | 34 | void runtime_test_set_async(void *self_p, struct async_t *async_p); 35 | 36 | void runtime_test_run_forever(void *self_p); 37 | 38 | void runtime_test_tcp_client_init(struct async_tcp_client_t *self_p, 39 | async_tcp_client_connected_t on_connected, 40 | async_tcp_client_disconnected_t on_disconnected, 41 | async_tcp_client_input_t on_input); 42 | 43 | void runtime_test_tcp_client_connect(struct async_tcp_client_t *self_p, 44 | const char *host_p, 45 | int port); 46 | 47 | void runtime_test_tcp_client_disconnect(struct async_tcp_client_t *self_p); 48 | 49 | void runtime_test_tcp_client_write(struct async_tcp_client_t *self_p, 50 | const void *buf_p, 51 | size_t size); 52 | 53 | size_t runtime_test_tcp_client_read(struct async_tcp_client_t *self_p, 54 | void *buf_p, 55 | size_t size); 56 | 57 | struct async_runtime_t *runtime_test_create(void); 58 | 59 | void runtime_test_tcp_server_init( 60 | struct async_tcp_server_t *self_p, 61 | const char *host_p, 62 | int port, 63 | async_tcp_server_client_connected_t on_connected, 64 | async_tcp_server_client_disconnected_t on_disconnected, 65 | async_tcp_server_client_input_t on_input); 66 | 67 | void runtime_test_tcp_server_add_client(struct async_tcp_server_t *self_p, 68 | struct async_tcp_server_client_t *client_p); 69 | 70 | int runtime_test_tcp_server_start(struct async_tcp_server_t *self_p); 71 | 72 | void runtime_test_tcp_server_stop(struct async_tcp_server_t *self_p); 73 | 74 | void runtime_test_tcp_server_client_write(struct async_tcp_server_client_t *self_p, 75 | const void *buf_p, 76 | size_t size); 77 | 78 | size_t runtime_test_tcp_server_client_read(struct async_tcp_server_client_t *self_p, 79 | void *buf_p, 80 | size_t size); 81 | 82 | void runtime_test_tcp_server_client_disconnect( 83 | struct async_tcp_server_client_t *self_p); 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /tst/utils/subprocess.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Erik Moqvist 5 | * 6 | * Permission is hereby granted, free of charge, to any person 7 | * obtaining a copy of this software and associated documentation 8 | * files (the "Software"), to deal in the Software without 9 | * restriction, including without limitation the rights to use, copy, 10 | * modify, merge, publish, distribute, sublicense, and/or sell copies 11 | * of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | * 26 | * This file is part of the subprocess project. 27 | */ 28 | 29 | #ifndef SUBPROCESS_H 30 | #define SUBPROCESS_H 31 | 32 | #include 33 | #include 34 | 35 | #define SUBPROCESS_VERSION "0.5.0" 36 | 37 | typedef void (*subprocess_entry_t)(void *arg_p); 38 | 39 | struct subprocess_output_t { 40 | /* Always null-terminated. */ 41 | char *buf_p; 42 | /* Buffer length, not incuding last null-termination. */ 43 | size_t length; 44 | /* Buffer size, including unused bytes. */ 45 | size_t size; 46 | }; 47 | 48 | struct subprocess_result_t { 49 | int exit_code; 50 | int signal_number; 51 | struct subprocess_output_t stdout; 52 | struct subprocess_output_t stderr; 53 | }; 54 | 55 | /** 56 | * Call given function with given argument in a subprocess. Returns 57 | * captured subprocess' exit code, or NULL if the subprocess could not 58 | * be started. 59 | */ 60 | struct subprocess_result_t *subprocess_call(subprocess_entry_t entry, 61 | void *arg_p); 62 | 63 | /** 64 | * Call given function with given argument in a subprocess. Returns 65 | * captured subprocess' stdout, stderr and exit code, or NULL if the 66 | * subprocess could not be started. 67 | */ 68 | struct subprocess_result_t *subprocess_call_output(subprocess_entry_t entry, 69 | void *arg_p); 70 | 71 | /** 72 | * Execute given command in a subprocess. Returns captured subprocess' 73 | * exit code, or NULL if the subprocess could not be started. 74 | */ 75 | struct subprocess_result_t *subprocess_exec(const char *command_p); 76 | 77 | /** 78 | * Execute given command in a subprocess. Returns captured subprocess' 79 | * stdout, stderr and exit code, or NULL if the subprocess could not 80 | * be started. 81 | */ 82 | struct subprocess_result_t *subprocess_exec_output(const char *command_p); 83 | 84 | /** 85 | * Returns true if the subprocess was started and exited with 0, 86 | * otherwise false. 87 | */ 88 | bool subprocess_completed_successfully(struct subprocess_result_t *result_p); 89 | 90 | /** 91 | * Print subprocess exit code, stdout and stderr. 92 | */ 93 | void subprocess_result_print(struct subprocess_result_t *self_p); 94 | 95 | /** 96 | * Free given result. 97 | */ 98 | void subprocess_result_free(struct subprocess_result_t *self_p); 99 | 100 | #endif 101 | -------------------------------------------------------------------------------- /tst/utils/utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "utils.h" 3 | #include "nala.h" 4 | 5 | int channel_open(struct async_channel_t *self_p) 6 | { 7 | (void)self_p; 8 | 9 | FAIL("This function must be mocked."); 10 | 11 | return (-1); 12 | } 13 | 14 | void channel_close(struct async_channel_t *self_p) 15 | { 16 | (void)self_p; 17 | 18 | FAIL("This function must be mocked."); 19 | } 20 | 21 | size_t channel_read(struct async_channel_t *self_p, 22 | void *buf_p, 23 | size_t size) 24 | { 25 | (void)self_p; 26 | (void)buf_p; 27 | 28 | FAIL("This function must be mocked."); 29 | 30 | return (size); 31 | } 32 | 33 | void channel_write(struct async_channel_t *self_p, 34 | const void *buf_p, 35 | size_t size) 36 | { 37 | (void)self_p; 38 | (void)buf_p; 39 | (void)size; 40 | 41 | FAIL("This function must be mocked."); 42 | } 43 | 44 | void mqtt_on_connected(void *obj_p) 45 | { 46 | (void)obj_p; 47 | 48 | FAIL("This function must be mocked."); 49 | } 50 | 51 | void mqtt_on_disconnected(void *obj_p) 52 | { 53 | (void)obj_p; 54 | 55 | FAIL("This function must be mocked."); 56 | } 57 | 58 | void mqtt_on_subscribe_complete(void *obj_p, uint16_t transaction_id) 59 | { 60 | (void)obj_p; 61 | (void)transaction_id; 62 | 63 | FAIL("This function must be mocked."); 64 | } 65 | 66 | void mqtt_on_publish(void *obj_p, 67 | const char *topic_p, 68 | const uint8_t *buf_p, 69 | size_t size) 70 | { 71 | (void)obj_p; 72 | (void)topic_p; 73 | (void)buf_p; 74 | (void)size; 75 | 76 | FAIL("This function must be mocked."); 77 | } 78 | -------------------------------------------------------------------------------- /tst/utils/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | #include "async.h" 5 | 6 | int channel_open(struct async_channel_t *self_p); 7 | 8 | void channel_close(struct async_channel_t *self_p); 9 | 10 | size_t channel_read(struct async_channel_t *self_p, 11 | void *buf_p, 12 | size_t size); 13 | 14 | void channel_write(struct async_channel_t *self_p, 15 | const void *buf_p, 16 | size_t size); 17 | 18 | void mqtt_on_connected(void *obj_p); 19 | 20 | void mqtt_on_disconnected(void *obj_p); 21 | 22 | void mqtt_on_subscribe_complete(void *obj_p, uint16_t transaction_id); 23 | 24 | void mqtt_on_publish(void *obj_p, 25 | const char *topic_p, 26 | const uint8_t *buf_p, 27 | size_t size); 28 | 29 | #endif 30 | --------------------------------------------------------------------------------