├── .gitignore ├── COPYING ├── Makefile.am ├── NEWS ├── README.md ├── TODO ├── autogen.sh ├── build ├── Makefile.am ├── autotools │ └── as-compiler-flag.m4 └── binary-installer │ ├── installer-header.sh.in │ └── make-binary-installer.sh ├── configure.ac ├── doc ├── Makefile.am └── ipc-protocol.txt └── src ├── Makefile.am ├── dump-store.c ├── main.c ├── ntb-address.c ├── ntb-address.h ├── ntb-base58.c ├── ntb-base58.h ├── ntb-base64.c ├── ntb-base64.h ├── ntb-blob.c ├── ntb-blob.h ├── ntb-buffer.c ├── ntb-buffer.h ├── ntb-connection.c ├── ntb-connection.h ├── ntb-cpus.c ├── ntb-cpus.h ├── ntb-crypto.c ├── ntb-crypto.h ├── ntb-daemon.c ├── ntb-daemon.h ├── ntb-dns-bootstrap.c ├── ntb-dns-bootstrap.h ├── ntb-ecc.c ├── ntb-ecc.h ├── ntb-encoded-words.c ├── ntb-encoded-words.h ├── ntb-error.c ├── ntb-error.h ├── ntb-file-error.c ├── ntb-file-error.h ├── ntb-hash-table.c ├── ntb-hash-table.h ├── ntb-ipc-client.c ├── ntb-ipc-client.h ├── ntb-ipc-proto.c ├── ntb-ipc-proto.h ├── ntb-ipc-sockaddr.c ├── ntb-ipc-sockaddr.h ├── ntb-ipc.c ├── ntb-ipc.h ├── ntb-key-value.c ├── ntb-key-value.h ├── ntb-key.c ├── ntb-key.h ├── ntb-keygen.c ├── ntb-keygen.h ├── ntb-keyring.c ├── ntb-keyring.h ├── ntb-list.c ├── ntb-list.h ├── ntb-load-keys.c ├── ntb-load-keys.h ├── ntb-load-outgoings.c ├── ntb-load-outgoings.h ├── ntb-log.c ├── ntb-log.h ├── ntb-mail-parser.c ├── ntb-mail-parser.h ├── ntb-main-context.c ├── ntb-main-context.h ├── ntb-mkdir.c ├── ntb-mkdir.h ├── ntb-netaddress.c ├── ntb-netaddress.h ├── ntb-network.c ├── ntb-network.h ├── ntb-parse-addresses.c ├── ntb-parse-addresses.h ├── ntb-parse-content-type.c ├── ntb-parse-content-type.h ├── ntb-pointer-array.h ├── ntb-pow.c ├── ntb-pow.h ├── ntb-proto.c ├── ntb-proto.h ├── ntb-proxy.c ├── ntb-proxy.h ├── ntb-quoted-printable.c ├── ntb-quoted-printable.h ├── ntb-ref-count.h ├── ntb-save-message.c ├── ntb-save-message.h ├── ntb-sendmail.c ├── ntb-sendmail.h ├── ntb-signal.h ├── ntb-slab.c ├── ntb-slab.h ├── ntb-slice.c ├── ntb-slice.h ├── ntb-socket.c ├── ntb-socket.h ├── ntb-store.c ├── ntb-store.h ├── ntb-util.c └── ntb-util.h /.gitignore: -------------------------------------------------------------------------------- 1 | /Makefile 2 | /Makefile.in 3 | /aclocal.m4 4 | /autom4te.cache 5 | /build/depcomp 6 | /build/install-sh 7 | /build/missing 8 | /config.h 9 | /config.h.in 10 | /config.log 11 | /config.status 12 | /configure 13 | /depcomp 14 | /install-sh 15 | /missing 16 | /stamp-h1 17 | 18 | /build/Makefile 19 | /build/Makefile.in 20 | /build/compile 21 | 22 | /doc/Makefile 23 | /doc/Makefile.in 24 | 25 | /src/Makefile 26 | /src/Makefile.in 27 | /src/notbit 28 | /src/notbit-sendmail 29 | /src/notbit-keygen 30 | /src/dump-store 31 | 32 | *.o 33 | .deps 34 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Copyright © 2011, 2012, 2013, 2014 Neil Roberts 2 | Copyright © 2008-2011 Kristian Høgsberg 3 | Copyright © 2011, 2012, 2013 Intel Corporation 4 | 5 | Permission to use, copy, modify, distribute, and sell this software and its 6 | documentation for any purpose is hereby granted without fee, provided that 7 | the above copyright notice appear in all copies and that both that copyright 8 | notice and this permission notice appear in supporting documentation, and 9 | that the name of the copyright holders not be used in advertising or 10 | publicity pertaining to distribution of the software without specific, 11 | written prior permission. The copyright holders make no representations 12 | about the suitability of this software for any purpose. It is provided "as 13 | is" without express or implied warranty. 14 | 15 | THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | OF THIS SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | # XXX - this is a massive hack to make autoreconf honour the ACLOCAL_FLAGS 2 | # that jhbuild sets while still retaining build/autotools as the authoritative 3 | # source for m4 macros 4 | ACLOCAL_AMFLAGS = -I build/autotools ${ACLOCAL_FLAGS} 5 | 6 | SUBDIRS = \ 7 | doc \ 8 | src \ 9 | build \ 10 | $(NULL) 11 | 12 | EXTRA_DIST = \ 13 | autogen.sh \ 14 | NEWS \ 15 | README.md \ 16 | $(NULL) 17 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | ┏━━━━━━━━━━━━━━━━━━━━━━━┓ 2 | ┃Notbit 0.6 - 2014-03-26┃ 3 | ┗━━━━━━━━━━━━━━━━━━━━━━━┛ 4 | 5 | This is a brown paper bag release fixing an embarrassing crash on 6 | 32-bit builds. 7 | 8 | ┏━━━━━━━━━━━━━━━━━━━━━━━┓ 9 | ┃Notbit 0.4 - 2014-03-25┃ 10 | ┗━━━━━━━━━━━━━━━━━━━━━━━┛ 11 | 12 | • The license has been changed to a liberal MIT license instead of 13 | GPLv3. 14 | 15 | • There is now a command-line option to disable the peer address 16 | bootstrapping. Along with a new option to make it accept advertised 17 | addresses in the local address range this can be used to create a 18 | private internal network on a LAN. Many thanks to William A. 19 | Kennington III for this feature. 20 | 21 | • The proof-of-work difficulty for a new address can now be specified 22 | when running notbit-keygen. The difficulty will be used to avoid 23 | wasting time trying to decrypt messages with insufficient 24 | proof-of-work. The default value is now 2 instead of 1 which matches 25 | PyBitmessage. 26 | 27 | • Some bugs have been fixed where Notbit may have been using an 28 | insufficient proof-of-work depending on chance. It was also 29 | incorrectly storing the proof-of-work settings for public keys that 30 | are requested from peers. 31 | 32 | • Some Linuxisms have been removed to make it more portable to other 33 | Unices. It has been tested on OpenBSD. 34 | 35 | • When receiving a message it will use the label of the keys as the 36 | name in the email to make it easier to identify who sent the 37 | message. 38 | 39 | • It now supports searching for the bootstrap servers via DNS. 40 | Previously it was only using a hard-coded list in the source code. 41 | 42 | • Keys that are marked as disabled by manually editing the keys.dat 43 | file and restarting notbit will now be ignored. 44 | 45 | • If Notbit creates the maildir, it will now also create the cur 46 | directory. This was apparently causing problems when using Notbit 47 | with Mutt. 48 | 49 | ┏━━━━━━━━━━━━━━━━━━━━━━━┓ 50 | ┃Notbit 0.2 - 2014-02-15┃ 51 | ┗━━━━━━━━━━━━━━━━━━━━━━━┛ 52 | 53 | Initial release 54 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | In rough order of priority 2 | 3 | • Configuration file to set the maildir 4 | • Add an alternative to clock_gettime for OS X 5 | • Support renaming keys via notbit-keygen 6 | • Deterministic keys / channels 7 | • Broadcasts 8 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | test -n "$srcdir" || srcdir=`dirname "$0"` 4 | test -n "$srcdir" || srcdir=. 5 | 6 | olddir=`pwd` 7 | 8 | cd "$srcdir" 9 | 10 | test -f src/main.c || { 11 | echo "You must run this script in the top-level source directory" 12 | exit 1 13 | } 14 | 15 | AUTORECONF=`which autoreconf` 16 | if test -z $AUTORECONF; then 17 | echo "*** No autoreconf found, please install it ***" 18 | exit 1 19 | fi 20 | 21 | # NOCONFIGURE is used by gnome-common 22 | if test -z "$NOCONFIGURE"; then 23 | if test -z "$*"; then 24 | echo "I am going to run ./configure with no arguments -" \ 25 | "if you wish " 26 | echo "to pass any to it, please specify them on the $0" \ 27 | "command line." 28 | fi 29 | fi 30 | 31 | rm -rf autom4te.cache 32 | 33 | autoreconf --force --install --verbose || exit $? 34 | 35 | cd "$olddir" 36 | test -n "$NOCONFIGURE" || "$srcdir/configure" "$@" 37 | -------------------------------------------------------------------------------- /build/Makefile.am: -------------------------------------------------------------------------------- 1 | EXTRA_DIST = \ 2 | autotools/as-compiler-flag.m4 \ 3 | binary-installer/installer-header.sh.in \ 4 | binary-installer/make-binary-installer.sh \ 5 | $(NULL) 6 | -------------------------------------------------------------------------------- /build/autotools/as-compiler-flag.m4: -------------------------------------------------------------------------------- 1 | dnl as-compiler-flag.m4 0.1.0 2 | 3 | dnl autostars m4 macro for detection of compiler flags 4 | 5 | dnl David Schleef 6 | 7 | dnl $Id: as-compiler-flag.m4,v 1.1 2005/12/15 23:35:19 ds Exp $ 8 | 9 | dnl AS_COMPILER_FLAG(CFLAGS, ACTION-IF-ACCEPTED, [ACTION-IF-NOT-ACCEPTED]) 10 | dnl Tries to compile with the given CFLAGS. 11 | dnl Runs ACTION-IF-ACCEPTED if the compiler can compile with the flags, 12 | dnl and ACTION-IF-NOT-ACCEPTED otherwise. 13 | 14 | AC_DEFUN([AS_COMPILER_FLAG], 15 | [ 16 | AC_MSG_CHECKING([to see if compiler understands $1]) 17 | 18 | save_CFLAGS="$CFLAGS" 19 | CFLAGS="$CFLAGS $1" 20 | 21 | AC_TRY_COMPILE([ ], [], [flag_ok=yes], [flag_ok=no]) 22 | CFLAGS="$save_CFLAGS" 23 | 24 | if test "X$flag_ok" = Xyes ; then 25 | m4_ifvaln([$2],[$2]) 26 | true 27 | else 28 | m4_ifvaln([$3],[$3]) 29 | true 30 | fi 31 | AC_MSG_RESULT([$flag_ok]) 32 | ]) 33 | 34 | dnl AS_COMPILER_FLAGS(VAR, FLAGS) 35 | dnl Tries to compile with the given CFLAGS. 36 | 37 | AC_DEFUN([AS_COMPILER_FLAGS], 38 | [ 39 | list=$2 40 | flags_supported="" 41 | flags_unsupported="" 42 | AC_MSG_CHECKING([for supported compiler flags]) 43 | for each in $list 44 | do 45 | save_CFLAGS="$CFLAGS" 46 | CFLAGS="$CFLAGS $each" 47 | AC_TRY_COMPILE([ ], [], [flag_ok=yes], [flag_ok=no]) 48 | CFLAGS="$save_CFLAGS" 49 | 50 | if test "X$flag_ok" = Xyes ; then 51 | flags_supported="$flags_supported $each" 52 | else 53 | flags_unsupported="$flags_unsupported $each" 54 | fi 55 | done 56 | AC_MSG_RESULT([$flags_supported]) 57 | if test "X$flags_unsupported" != X ; then 58 | AC_MSG_WARN([unsupported compiler flags: $flags_unsupported]) 59 | fi 60 | $1="$$1 $flags_supported" 61 | ]) 62 | 63 | -------------------------------------------------------------------------------- /build/binary-installer/installer-header.sh.in: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eu 4 | 5 | cat < "$PKGCONFIG_WRAPPER" < "$OPENSSL_TARBALL_FILE" 27 | curl -L "${OPENSSL_TARBALL_URL}.asc" > "${OPENSSL_TARBALL_FILE}.asc" 28 | fi 29 | 30 | if ! gpg --list-keys "$OPENSSL_KEYID" > /dev/null; then 31 | set +x 32 | echo 33 | echo "The public key used to sign OpenSSL releases is not in your keyring " 34 | echo "so the tarball can not be verified. You can retrieve by typing: " 35 | echo 36 | echo " gpg --recv-keys 0x$OPENSSL_KEYID" 37 | exit 1 38 | fi 39 | 40 | gpg -r "$OPENSSL_KEYID" --verify "${OPENSSL_TARBALL_FILE}"{.asc,} 41 | 42 | if test -e "$OPENSSL_BUILDDIR"; then 43 | rm -rf "$OPENSSL_BUILDDIR" 44 | fi 45 | 46 | if test -e "$OPENSSL_PREFIX"; then 47 | rm -rf "$OPENSSL_PREFIX" 48 | fi 49 | mkdir -p "${OPENSSL_PREFIX}/lib/pkgconfig" 50 | 51 | tar -C "$TOPDIR" -zxf "$OPENSSL_TARBALL_FILE" 52 | 53 | cd "$OPENSSL_BUILDDIR" 54 | 55 | # Bodge in the -m32 flag in the compilation flags supplied by the 56 | # configure script in case we are building on a 64-bit machine 57 | sed -i 's/^"linux-generic32","gcc:/\0-m32 /' Configure 58 | 59 | # enabling sha ripemd ec ecdsa ecdh hw cms aes md5 60 | ./Configure \ 61 | linux-generic32 \ 62 | no-threads \ 63 | no-zlib \ 64 | no-shared \ 65 | no-idea \ 66 | no-camellia \ 67 | no-seed \ 68 | no-bf \ 69 | no-cast \ 70 | no-des \ 71 | no-rc2 \ 72 | no-rc4 \ 73 | no-rc5 \ 74 | no-md2 \ 75 | no-md4 \ 76 | no-mdc2 \ 77 | no-rsa \ 78 | no-dsa \ 79 | no-dh \ 80 | no-sock \ 81 | no-ssl2 \ 82 | no-ssl3 \ 83 | no-err \ 84 | no-krb5 \ 85 | no-engine \ 86 | no-tlsext \ 87 | no-jpake \ 88 | no-capieng \ 89 | no-dso \ 90 | --prefix="$OPENSSL_PREFIX" 91 | 92 | make -j4 build_crypto 93 | make libcrypto.pc 94 | 95 | cp -L libcrypto.pc "$OPENSSL_PREFIX/lib/pkgconfig/" 96 | cp -L libcrypto.a "$OPENSSL_PREFIX/lib/" 97 | cp -L -R include "$OPENSSL_PREFIX/" 98 | 99 | cd "$TOPDIR" 100 | ./configure \ 101 | PKG_CONFIG="$PKGCONFIG_WRAPPER" \ 102 | CFLAGS="-O3 -fstack-protector-strong -m32" 103 | make -j4 104 | 105 | strip --strip-all "${TOPDIR}/src/notbit" 106 | 107 | installer_header_length=`wc -l "$INSTALLER_HEADER".in | 108 | sed -r "s/^ *([0-9]+).*/\\1/"` 109 | copyright_length=`wc -l "${TOPDIR}/COPYING" | 110 | sed -r "s/^ *([0-9]+).*/\\1/"` 111 | installer_header_offset=`expr $installer_header_length + $copyright_length` 112 | 113 | sed -e s/@INSTALLER_HEADER_OFFSET@/"$installer_header_offset"/g \ 114 | -e /@NOTBIT_COPYRIGHT@/r" ${TOPDIR}/COPYING" \ 115 | -e /@NOTBIT_COPYRIGHT@/d \ 116 | < "$INSTALLER_HEADER".in > "$INSTALLER_HEADER" 117 | 118 | tar -C "${TOPDIR}/src" -zcf - notbit{-sendmail,-keygen,} | \ 119 | cat "$INSTALLER_HEADER" - > \ 120 | "$INSTALLER_SCRIPT" 121 | 122 | chmod a+x "$INSTALLER_SCRIPT" 123 | 124 | rm -f "$INSTALLER_SCRIPT".asc 125 | gpg -a --detach-sign "$INSTALLER_SCRIPT" 126 | 127 | echo 128 | echo "The installer is now available at " 129 | echo "$INSTALLER_SCRIPT" 130 | echo "with a signature at" 131 | echo "$INSTALLER_SCRIPT".asc 132 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_INIT([notbit], 0.7) 2 | 3 | AC_CONFIG_AUX_DIR([build]) 4 | AC_CONFIG_MACRO_DIR([build/autotools]) 5 | AC_CONFIG_SRCDIR([src/main.c]) 6 | AM_CONFIG_HEADER([config.h]) 7 | AC_GNU_SOURCE 8 | 9 | AC_PROG_CC 10 | AC_PROG_LN_S 11 | 12 | AM_INIT_AUTOMAKE([1.9 foreign no-dist-gzip dist-xz tar-ustar]) 13 | 14 | m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) 15 | 16 | NOTBIT_EXTRA_CFLAGS="" 17 | NOTBIT_EXTRA_LIBS="" 18 | 19 | AC_C_BIGENDIAN([AC_DEFINE([HAVE_BIG_ENDIAN], [1], [System is big-endian])], 20 | [AC_DEFINE([HAVE_LITTLE_ENDIAN], [1], 21 | [System is little-endian])]) 22 | 23 | alignof_name="" 24 | AC_MSG_CHECKING([for alignof]) 25 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([const int thing = alignof (int);], 26 | [(void) 0])], 27 | [alignof_name="alignof" 28 | AC_MSG_RESULT([yes])], 29 | [AC_MSG_RESULT([no])]) 30 | AC_MSG_CHECKING([for __alignof__]) 31 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([const int thing = __alignof__ (int);], 32 | [(void) 0])], 33 | [alignof_name="__alignof__" 34 | AC_MSG_RESULT([yes])], 35 | [AC_MSG_RESULT([no])]) 36 | AS_IF([test "x$alignof_name" = "x"], 37 | [AC_MSG_ERROR([No operator for alignof found])]) 38 | AC_DEFINE_UNQUOTED([ALIGNOF_NAME], [$alignof_name], 39 | [The name of the alignof operator]) 40 | 41 | have_sync_ref_count=yes 42 | AC_MSG_CHECKING([for __sync_fetch_and_add]) 43 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([int x;], 44 | [__sync_fetch_and_add(&x, 1);])], 45 | [AC_MSG_RESULT([yes])], 46 | [AC_MSG_RESULT([no]) 47 | have_sync_ref_count=no]) 48 | AC_MSG_CHECKING([for __sync_fetch_and_sub]) 49 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([int x;], 50 | [__sync_fetch_and_sub(&x, 1);])], 51 | [AC_MSG_RESULT([yes])], 52 | [AC_MSG_RESULT([no]) 53 | have_sync_ref_count=no]) 54 | AS_IF([test "x$have_sync_ref_count" = "xyes"], 55 | [AC_DEFINE([HAVE_SYNC_REF_COUNT], [1], 56 | [GCC extensions for atomic ref-counting are available])]) 57 | 58 | AC_MSG_CHECKING([for _Static_assert]) 59 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([_Static_assert (1, "");], 60 | [(void) 0])], 61 | [AC_DEFINE([HAVE_STATIC_ASSERT], [1], 62 | [Whether _Static_assert can be used or not]) 63 | AC_MSG_RESULT([yes])], 64 | [AC_MSG_RESULT([no])]) 65 | 66 | have_clock_gettime=no 67 | AC_CHECK_FUNC([clock_gettime], [have_clock_gettime=yes]) 68 | AS_IF([test "x$have_clock_gettime" = "xno"], 69 | [AC_CHECK_LIB([rt], [clock_gettime], 70 | [NOTBIT_EXTRA_LIBS="$NOTBIT_EXTRA_LIBS -lrt"], 71 | [clock_gettime required but not found])]) 72 | 73 | AC_CHECK_FUNCS([getpeereid]) 74 | 75 | dnl ============================================================ 76 | dnl Enable strict compiler flags 77 | dnl ============================================================ 78 | 79 | # use strict compiler flags only when building from git; the rules for 80 | # distcheck will take care of turning this on when making a release 81 | m4_define([maintainer_default], [yes]) 82 | AC_ARG_ENABLE( 83 | [maintainer-flags], 84 | [AC_HELP_STRING([--enable-maintainer-flags=@<:@no/yes/error@:>@], 85 | [Use strict compiler flags @<:@default=]maintainer_default[@:>@])], 86 | [], 87 | enable_maintainer_flags=maintainer_default 88 | ) 89 | 90 | MAINTAINER_COMPILER_FLAGS="-Wall -Wcast-align -Wuninitialized 91 | -Wno-strict-aliasing -Wempty-body -Wformat 92 | -Wformat-security -Winit-self -Wundef 93 | -Wpointer-arith -Wmissing-declarations" 94 | 95 | AS_CASE( 96 | [$enable_maintainer_flags], 97 | [yes], 98 | [ 99 | AS_COMPILER_FLAGS([MAINTAINER_CFLAGS], [$MAINTAINER_COMPILER_FLAGS]) 100 | ], 101 | [no], 102 | [ 103 | ], 104 | [error], 105 | [ 106 | MAINTAINER_COMPILER_FLAGS="$MAINTAINER_COMPILER_FLAGS -Werror" 107 | AS_COMPILER_FLAGS([MAINTAINER_CFLAGS], [$MAINTAINER_COMPILER_FLAGS]) 108 | ], 109 | [*], 110 | [AC_MSG_ERROR([Invalid option for --enable-maintainer-flags])] 111 | ) 112 | 113 | # strip leading spaces 114 | NOTBIT_EXTRA_CFLAGS="$NOTBIT_EXTRA_CFLAGS ${MAINTAINER_CFLAGS#* }" 115 | 116 | PKG_CHECK_MODULES(LIBCRYPTO, [libcrypto]) 117 | 118 | AC_SUBST(NOTBIT_EXTRA_CFLAGS) 119 | AC_SUBST(NOTBIT_EXTRA_LIBS) 120 | 121 | AC_CONFIG_FILES([ 122 | Makefile 123 | build/Makefile 124 | doc/Makefile 125 | src/Makefile 126 | ]) 127 | 128 | AC_OUTPUT 129 | -------------------------------------------------------------------------------- /doc/Makefile.am: -------------------------------------------------------------------------------- 1 | EXTRA_DIST = \ 2 | ipc-protocol.txt \ 3 | $(NULL) 4 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | bin_PROGRAMS = \ 2 | notbit \ 3 | $(NULL) 4 | 5 | noinst_PROGRAMS = dump-store 6 | 7 | AM_CPPFLAGS = \ 8 | $(LIBCRYPTO_CFLAGS) \ 9 | $(NOTBIT_EXTRA_CFLAGS) \ 10 | $(NULL) 11 | 12 | notbit_SOURCES = \ 13 | ntb-address.c \ 14 | ntb-address.h \ 15 | ntb-base58.c \ 16 | ntb-base58.h \ 17 | ntb-base64.c \ 18 | ntb-base64.h \ 19 | ntb-blob.c \ 20 | ntb-blob.h \ 21 | ntb-buffer.c \ 22 | ntb-buffer.h \ 23 | ntb-connection.c \ 24 | ntb-connection.h \ 25 | ntb-cpus.c \ 26 | ntb-cpus.h \ 27 | ntb-crypto.c \ 28 | ntb-crypto.h \ 29 | ntb-daemon.c \ 30 | ntb-daemon.h \ 31 | ntb-dns-bootstrap.c \ 32 | ntb-dns-bootstrap.h \ 33 | ntb-ecc.c \ 34 | ntb-ecc.h \ 35 | ntb-encoded-words.c \ 36 | ntb-encoded-words.h \ 37 | ntb-error.c \ 38 | ntb-error.h \ 39 | ntb-file-error.c \ 40 | ntb-file-error.h \ 41 | ntb-hash-table.c \ 42 | ntb-hash-table.h \ 43 | ntb-ipc.c \ 44 | ntb-ipc.h \ 45 | ntb-ipc-client.c \ 46 | ntb-ipc-client.h \ 47 | ntb-ipc-proto.c \ 48 | ntb-ipc-proto.h \ 49 | ntb-ipc-sockaddr.c \ 50 | ntb-ipc-sockaddr.h \ 51 | ntb-key.c \ 52 | ntb-key.h \ 53 | ntb-key-value.c \ 54 | ntb-key-value.h \ 55 | ntb-keygen.c \ 56 | ntb-keygen.h \ 57 | ntb-keyring.c \ 58 | ntb-keyring.h \ 59 | ntb-list.c \ 60 | ntb-list.h \ 61 | ntb-load-keys.c \ 62 | ntb-load-keys.h \ 63 | ntb-load-outgoings.c \ 64 | ntb-load-outgoings.h \ 65 | ntb-log.c \ 66 | ntb-log.h \ 67 | ntb-mail-parser.c \ 68 | ntb-mail-parser.h \ 69 | ntb-main-context.c \ 70 | ntb-main-context.h \ 71 | ntb-mkdir.c \ 72 | ntb-mkdir.h \ 73 | ntb-netaddress.c \ 74 | ntb-netaddress.h \ 75 | ntb-network.c \ 76 | ntb-network.h \ 77 | ntb-parse-addresses.c \ 78 | ntb-parse-addresses.h \ 79 | ntb-parse-content-type.c \ 80 | ntb-parse-content-type.h \ 81 | ntb-pointer-array.h \ 82 | ntb-pow.c \ 83 | ntb-pow.h \ 84 | ntb-proto.c \ 85 | ntb-proto.h \ 86 | ntb-proxy.c \ 87 | ntb-proxy.h \ 88 | ntb-quoted-printable.c \ 89 | ntb-quoted-printable.h \ 90 | ntb-ref-count.h \ 91 | ntb-save-message.c \ 92 | ntb-save-message.h \ 93 | ntb-sendmail.c \ 94 | ntb-sendmail.h \ 95 | ntb-signal.h \ 96 | ntb-slab.c \ 97 | ntb-slab.h \ 98 | ntb-slice.c \ 99 | ntb-slice.h \ 100 | ntb-socket.c \ 101 | ntb-socket.h \ 102 | ntb-store.c \ 103 | ntb-store.h \ 104 | ntb-util.c \ 105 | ntb-util.h \ 106 | main.c \ 107 | $(NULL) 108 | 109 | notbit_LDFLAGS = \ 110 | -pthread \ 111 | $(NULL) 112 | 113 | notbit_LDADD = \ 114 | $(LIBCRYPTO_LIBS) \ 115 | $(NOTBIT_EXTRA_LIBS) \ 116 | $(NULL) 117 | 118 | dump_store_SOURCES = \ 119 | ntb-buffer.c \ 120 | ntb-buffer.h \ 121 | ntb-util.c \ 122 | ntb-util.h \ 123 | dump-store.c \ 124 | $(NULL) 125 | 126 | dump_store_LDFLAGS = \ 127 | -pthread \ 128 | $(NULL) 129 | 130 | NOTBIT_LINKS = \ 131 | notbit-sendmail$(EXEEXT) \ 132 | notbit-keygen$(EXEEXT) \ 133 | $(NULL) 134 | 135 | notbit-sendmail$(EXEEXT): notbit 136 | $(AM_V_GEN)$(RM) $@ && $(LN_S) $< $@ 137 | 138 | notbit-keygen$(EXEEXT): notbit 139 | $(AM_V_GEN)$(RM) $@ && $(LN_S) $< $@ 140 | 141 | install-exec-hook: 142 | for x in $(NOTBIT_LINKS); do \ 143 | $(RM) $(DESTDIR)$(bindir)/$$x; \ 144 | $(LN_S) notbit $(DESTDIR)$(bindir)/$$x || exit 1; \ 145 | done 146 | 147 | all-local: $(NOTBIT_LINKS) 148 | 149 | CLEANFILES = \ 150 | $(NOTBIT_LINKS) \ 151 | $(NULL) 152 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include "ntb-daemon.h" 31 | #include "ntb-sendmail.h" 32 | #include "ntb-keygen.h" 33 | 34 | int 35 | main(int argc, char **argv) 36 | { 37 | const char *bn; 38 | 39 | for (bn = argv[0] + strlen(argv[0]); 40 | bn > argv[0] && bn[-1] != '/'; 41 | bn--); 42 | 43 | if (!strcmp(bn, "notbit-sendmail")) { 44 | return ntb_sendmail(argc, argv); 45 | } else if (!strcmp(bn, "notbit-keygen")) { 46 | return ntb_keygen(argc, argv); 47 | } else if (!strcmp(bn, "notbit")) { 48 | return ntb_daemon(argc, argv); 49 | } else { 50 | fprintf(stderr, "Unknown executable name “%s”\n", argv[0]); 51 | return EXIT_FAILURE; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/ntb-address.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_ADDRESS_H 25 | #define NTB_ADDRESS_H 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | struct ntb_address { 32 | uint8_t version; 33 | uint8_t stream; 34 | uint8_t ripe[RIPEMD160_DIGEST_LENGTH]; 35 | }; 36 | 37 | /* Maximum length that an encoded BitMessage address can be */ 38 | /* The largest number in hex is: 39 | * 0xff - The maximum version number 40 | * 0xff - The maximum stream number 41 | * 0xff × 20 - The ripe 42 | * 0xff × 4 - The checksum 43 | * 44 | * In base58 that is 8qfKFDmPNA1uDdWpLJyhogk4u4W7taoE15Pc 45 | * With three characters for the "BM-" prefix that makes 39 46 | */ 47 | #define NTB_ADDRESS_MAX_LENGTH 39 48 | 49 | #define NTB_ADDRESS_TAG_SIZE 32 50 | 51 | void 52 | ntb_address_encode(const struct ntb_address *address, 53 | char *output); 54 | 55 | bool 56 | ntb_address_decode(struct ntb_address *address, 57 | const char *address_string); 58 | 59 | bool 60 | ntb_address_equal(const struct ntb_address *a, 61 | const struct ntb_address *b); 62 | 63 | void 64 | ntb_address_from_network_keys(struct ntb_address *address, 65 | uint8_t version, 66 | uint8_t stream, 67 | const uint8_t *public_signing_key, 68 | const uint8_t *public_encryption_key); 69 | 70 | void 71 | ntb_address_get_tag(const struct ntb_address *address, 72 | uint8_t *tag, 73 | uint8_t *tag_private_key); 74 | 75 | #endif /* NTB_ADDRESS_H */ 76 | -------------------------------------------------------------------------------- /src/ntb-base58.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | 29 | #include "ntb-base58.h" 30 | 31 | static const char 32 | alphabet[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; 33 | 34 | static void 35 | reverse_bytes(char *data, 36 | size_t length) 37 | { 38 | char tmp; 39 | size_t i; 40 | 41 | for (i = 0; i < length / 2; i++) { 42 | tmp = data[i]; 43 | data[i] = data[length - 1 - i]; 44 | data[length - 1 - i] = tmp; 45 | } 46 | } 47 | 48 | size_t 49 | ntb_base58_encode(const uint8_t *input, 50 | size_t length, 51 | char *output) 52 | { 53 | BIGNUM *val; 54 | BN_ULONG part; 55 | char *p = output; 56 | 57 | val = BN_new(); 58 | 59 | if (BN_bin2bn(input, length, val) == NULL) 60 | ntb_fatal("A big number operation failed"); 61 | 62 | while (!BN_is_zero(val)) { 63 | part = BN_div_word(val, 58); 64 | assert(part >= 0 && part < 58); 65 | *(p++) = alphabet[part]; 66 | } 67 | 68 | BN_free(val); 69 | 70 | /* Make it big-endian */ 71 | reverse_bytes(output, p - output); 72 | 73 | return p - output; 74 | } 75 | 76 | static int 77 | get_digit_value(char digit) 78 | { 79 | int min, max, mid; 80 | 81 | min = 0; 82 | max = sizeof alphabet - 1; 83 | 84 | while (max > min) { 85 | mid = (min + max) / 2; 86 | 87 | if (alphabet[mid] < digit) 88 | min = mid + 1; 89 | else 90 | max = mid; 91 | } 92 | 93 | if (alphabet[min] == digit) 94 | return min; 95 | 96 | return -1; 97 | } 98 | 99 | ssize_t 100 | ntb_base58_decode(const char *input, 101 | size_t input_length, 102 | uint8_t *output, 103 | size_t output_length) 104 | { 105 | BIGNUM *val; 106 | int bn_result; 107 | int digit_value; 108 | int n_bytes; 109 | size_t i; 110 | 111 | val = BN_new(); 112 | 113 | for (i = 0; i < input_length; i++) { 114 | digit_value = get_digit_value(input[i]); 115 | if (digit_value == -1) 116 | return -1; 117 | 118 | bn_result = BN_mul_word(val, 58); 119 | assert(bn_result); 120 | 121 | bn_result = BN_add_word(val, digit_value); 122 | assert(bn_result); 123 | } 124 | 125 | n_bytes = BN_num_bytes(val); 126 | 127 | if (n_bytes > output_length) 128 | return -1; 129 | 130 | BN_bn2bin(val, output); 131 | 132 | BN_free(val); 133 | 134 | return n_bytes; 135 | } 136 | -------------------------------------------------------------------------------- /src/ntb-base58.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_BASE58_H 25 | #define NTB_BASE58_H 26 | 27 | #include 28 | #include 29 | 30 | #include "ntb-util.h" 31 | 32 | size_t 33 | ntb_base58_encode(const uint8_t *input, 34 | size_t length, 35 | char *output); 36 | 37 | ssize_t 38 | ntb_base58_decode(const char *input, 39 | size_t input_length, 40 | uint8_t *output, 41 | size_t output_length); 42 | 43 | #endif /* NTB_BASE58_H */ 44 | -------------------------------------------------------------------------------- /src/ntb-base64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_BASE64_H 25 | #define NTB_BASE64_H 26 | 27 | #include 28 | #include 29 | 30 | #include "ntb-error.h" 31 | 32 | extern struct ntb_error_domain 33 | ntb_base64_error; 34 | 35 | enum ntb_base64_error { 36 | NTB_BASE64_ERROR_INVALID_PADDING 37 | }; 38 | 39 | struct ntb_base64_data { 40 | int n_padding; 41 | int n_chars; 42 | int value; 43 | }; 44 | 45 | #define NTB_BASE64_MAX_INPUT_FOR_SIZE(input_size) \ 46 | ((size_t) (input_size) * 4 / 3) 47 | 48 | void 49 | ntb_base64_decode_start(struct ntb_base64_data *data); 50 | 51 | ssize_t 52 | ntb_base64_decode(struct ntb_base64_data *data, 53 | const uint8_t *in_buffer, 54 | size_t length, 55 | uint8_t *out_buffer, 56 | struct ntb_error **error); 57 | 58 | ssize_t 59 | ntb_base64_decode_end(struct ntb_base64_data *data, 60 | uint8_t *buffer, 61 | struct ntb_error **error); 62 | 63 | size_t 64 | ntb_base64_encode(const uint8_t *data_in, 65 | size_t data_in_length, 66 | char *data_out); 67 | 68 | #endif /* NTB_BASE64_H */ 69 | -------------------------------------------------------------------------------- /src/ntb-blob.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | 28 | #include "ntb-blob.h" 29 | #include "ntb-util.h" 30 | 31 | void 32 | ntb_blob_dynamic_init(struct ntb_buffer *buffer) 33 | { 34 | ntb_buffer_init(buffer); 35 | 36 | ntb_buffer_set_length(buffer, 37 | NTB_STRUCT_OFFSET(struct ntb_blob, data)); 38 | } 39 | 40 | struct ntb_blob * 41 | ntb_blob_dynamic_end(struct ntb_buffer *buffer) 42 | { 43 | struct ntb_blob *blob = (struct ntb_blob *) buffer->data; 44 | 45 | blob->size = buffer->length - NTB_STRUCT_OFFSET(struct ntb_blob, data); 46 | ntb_ref_count_init(&blob->ref_count); 47 | 48 | return blob; 49 | } 50 | 51 | struct ntb_blob * 52 | ntb_blob_new(const void *data, 53 | size_t size) 54 | { 55 | struct ntb_blob *blob = 56 | ntb_alloc(NTB_STRUCT_OFFSET(struct ntb_blob, data) + size); 57 | 58 | blob->size = size; 59 | 60 | ntb_ref_count_init(&blob->ref_count); 61 | 62 | if (data) 63 | memcpy(blob->data, data, size); 64 | 65 | return blob; 66 | } 67 | 68 | struct ntb_blob * 69 | ntb_blob_ref(struct ntb_blob *blob) 70 | { 71 | ntb_ref_count_ref(&blob->ref_count); 72 | 73 | return blob; 74 | } 75 | 76 | void 77 | ntb_blob_unref(struct ntb_blob *blob) 78 | { 79 | if (ntb_ref_count_unref(&blob->ref_count) <= 1) { 80 | ntb_ref_count_destroy(&blob->ref_count); 81 | ntb_free(blob); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/ntb-blob.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_BLOB_H 25 | #define NTB_BLOB_H 26 | 27 | #include 28 | #include 29 | 30 | #include "ntb-proto.h" 31 | #include "ntb-ref-count.h" 32 | #include "ntb-buffer.h" 33 | 34 | /* A blob represents a ref-counted immutable chunk of data. This will 35 | * be used to hold all inventory objects from the network such as 36 | * messages and public keys. The ref-count is thread-safe so that the 37 | * blob can be passed off to the store thread to be written to 38 | * disk. */ 39 | 40 | struct ntb_blob { 41 | struct ntb_ref_count ref_count; 42 | 43 | size_t size; 44 | 45 | /* Over-allocated to contain the data */ 46 | uint8_t data[1]; 47 | }; 48 | 49 | void 50 | ntb_blob_dynamic_init(struct ntb_buffer *buffer); 51 | 52 | struct ntb_blob * 53 | ntb_blob_dynamic_end(struct ntb_buffer *buffer); 54 | 55 | struct ntb_blob * 56 | ntb_blob_new(const void *data, 57 | size_t size); 58 | 59 | struct ntb_blob * 60 | ntb_blob_ref(struct ntb_blob *blob); 61 | 62 | void 63 | ntb_blob_unref(struct ntb_blob *blob); 64 | 65 | #endif /* NTB_BLOB_H */ 66 | -------------------------------------------------------------------------------- /src/ntb-buffer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include "ntb-buffer.h" 31 | 32 | void 33 | ntb_buffer_init(struct ntb_buffer *buffer) 34 | { 35 | static const struct ntb_buffer init = NTB_BUFFER_STATIC_INIT; 36 | 37 | *buffer = init; 38 | } 39 | 40 | void 41 | ntb_buffer_ensure_size(struct ntb_buffer *buffer, 42 | size_t size) 43 | { 44 | size_t new_size = MAX(buffer->size, 1); 45 | 46 | while (new_size < size) 47 | new_size *= 2; 48 | 49 | if (new_size != buffer->size) { 50 | buffer->data = ntb_realloc(buffer->data, new_size); 51 | buffer->size = new_size; 52 | } 53 | } 54 | 55 | void 56 | ntb_buffer_set_length(struct ntb_buffer *buffer, 57 | size_t length) 58 | { 59 | ntb_buffer_ensure_size(buffer, length); 60 | buffer->length = length; 61 | } 62 | 63 | void 64 | ntb_buffer_append_vprintf(struct ntb_buffer *buffer, 65 | const char *format, 66 | va_list ap) 67 | { 68 | va_list apcopy; 69 | int length; 70 | 71 | ntb_buffer_ensure_size(buffer, buffer->length + 16); 72 | 73 | va_copy(apcopy, ap); 74 | length = vsnprintf((char *) buffer->data + buffer->length, 75 | buffer->size - buffer->length, 76 | format, 77 | ap); 78 | 79 | if (length >= buffer->size - buffer->length) { 80 | ntb_buffer_ensure_size(buffer, buffer->length + length + 1); 81 | vsnprintf((char *) buffer->data + buffer->length, 82 | buffer->size - buffer->length, 83 | format, 84 | apcopy); 85 | } 86 | 87 | va_end(apcopy); 88 | 89 | buffer->length += length; 90 | } 91 | 92 | NTB_PRINTF_FORMAT(2, 3) void 93 | ntb_buffer_append_printf(struct ntb_buffer *buffer, 94 | const char *format, 95 | ...) 96 | { 97 | va_list ap; 98 | 99 | va_start(ap, format); 100 | ntb_buffer_append_vprintf(buffer, format, ap); 101 | va_end(ap); 102 | } 103 | 104 | void 105 | ntb_buffer_append(struct ntb_buffer *buffer, 106 | const void *data, 107 | size_t length) 108 | { 109 | ntb_buffer_ensure_size(buffer, buffer->length + length); 110 | memcpy(buffer->data + buffer->length, data, length); 111 | buffer->length += length; 112 | } 113 | 114 | void 115 | ntb_buffer_append_string(struct ntb_buffer *buffer, 116 | const char *str) 117 | { 118 | ntb_buffer_append(buffer, str, strlen(str) + 1); 119 | buffer->length--; 120 | } 121 | 122 | void 123 | ntb_buffer_destroy(struct ntb_buffer *buffer) 124 | { 125 | ntb_free(buffer->data); 126 | } 127 | -------------------------------------------------------------------------------- /src/ntb-buffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_BUFFER_H 25 | #define NTB_BUFFER_H 26 | 27 | #include 28 | #include 29 | 30 | #include "ntb-util.h" 31 | 32 | struct ntb_buffer { 33 | uint8_t *data; 34 | size_t length; 35 | size_t size; 36 | }; 37 | 38 | #define NTB_BUFFER_STATIC_INIT { .data = NULL, .length = 0, .size = 0 } 39 | 40 | void 41 | ntb_buffer_init(struct ntb_buffer *buffer); 42 | 43 | void 44 | ntb_buffer_ensure_size(struct ntb_buffer *buffer, 45 | size_t size); 46 | 47 | void 48 | ntb_buffer_set_length(struct ntb_buffer *buffer, 49 | size_t length); 50 | 51 | NTB_PRINTF_FORMAT(2, 3) void 52 | ntb_buffer_append_printf(struct ntb_buffer *buffer, 53 | const char *format, 54 | ...); 55 | 56 | void 57 | ntb_buffer_append_vprintf(struct ntb_buffer *buffer, 58 | const char *format, 59 | va_list ap); 60 | 61 | void 62 | ntb_buffer_append(struct ntb_buffer *buffer, 63 | const void *data, 64 | size_t length); 65 | 66 | static inline void 67 | ntb_buffer_append_c(struct ntb_buffer *buffer, 68 | char c) 69 | { 70 | if (buffer->size > buffer->length) 71 | buffer->data[buffer->length++] = c; 72 | else 73 | ntb_buffer_append(buffer, &c, 1); 74 | } 75 | 76 | void 77 | ntb_buffer_append_string(struct ntb_buffer *buffer, 78 | const char *str); 79 | 80 | void 81 | ntb_buffer_destroy(struct ntb_buffer *buffer); 82 | 83 | #endif /* NTB_BUFFER_H */ 84 | -------------------------------------------------------------------------------- /src/ntb-connection.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_CONNECTION_H 25 | #define NTB_CONNECTION_H 26 | 27 | #include 28 | #include 29 | 30 | #include "ntb-error.h" 31 | #include "ntb-netaddress.h" 32 | #include "ntb-buffer.h" 33 | #include "ntb-main-context.h" 34 | #include "ntb-signal.h" 35 | #include "ntb-proto.h" 36 | #include "ntb-blob.h" 37 | 38 | enum ntb_connection_event_type { 39 | NTB_CONNECTION_EVENT_CONNECT_FAILED, 40 | NTB_CONNECTION_EVENT_ERROR, 41 | 42 | NTB_CONNECTION_EVENT_PROXY_CONNECTED, 43 | NTB_CONNECTION_EVENT_VERSION, 44 | NTB_CONNECTION_EVENT_INV, 45 | NTB_CONNECTION_EVENT_ADDR, 46 | NTB_CONNECTION_EVENT_OBJECT, 47 | NTB_CONNECTION_EVENT_GETDATA, 48 | NTB_CONNECTION_EVENT_VERACK 49 | }; 50 | 51 | struct ntb_connection_event { 52 | enum ntb_connection_event_type type; 53 | struct ntb_connection *connection; 54 | }; 55 | 56 | struct ntb_connection_version_event { 57 | struct ntb_connection_event base; 58 | 59 | uint32_t version; 60 | uint64_t services; 61 | int64_t timestamp; 62 | 63 | struct ntb_netaddress addr_recv; 64 | struct ntb_netaddress addr_from; 65 | 66 | uint64_t nonce; 67 | struct ntb_proto_var_str user_agent; 68 | struct ntb_proto_var_int_list stream_numbers; 69 | }; 70 | 71 | struct ntb_connection_object_event { 72 | struct ntb_connection_event base; 73 | 74 | const uint8_t *object_data; 75 | size_t object_data_length; 76 | }; 77 | 78 | struct ntb_connection_inv_event { 79 | struct ntb_connection_event base; 80 | 81 | uint64_t n_inventories; 82 | const uint8_t *inventories; 83 | }; 84 | 85 | struct ntb_connection_addr_event { 86 | struct ntb_connection_event base; 87 | 88 | int64_t timestamp; 89 | uint32_t stream; 90 | uint64_t services; 91 | struct ntb_netaddress address; 92 | }; 93 | 94 | struct ntb_connection_getdata_event { 95 | struct ntb_connection_event base; 96 | 97 | uint64_t n_hashes; 98 | const uint8_t *hashes; 99 | }; 100 | 101 | struct ntb_connection; 102 | 103 | struct ntb_connection * 104 | ntb_connection_connect(const struct ntb_netaddress *address, 105 | struct ntb_error **error); 106 | 107 | struct ntb_connection * 108 | ntb_connection_connect_proxy(const struct ntb_netaddress *proxy, 109 | const struct ntb_netaddress *address, 110 | struct ntb_error **error); 111 | 112 | struct ntb_connection * 113 | ntb_connection_accept(int server_sock, 114 | struct ntb_error **error); 115 | 116 | void 117 | ntb_connection_free(struct ntb_connection *conn); 118 | 119 | struct ntb_signal * 120 | ntb_connection_get_event_signal(struct ntb_connection *conn); 121 | 122 | const char * 123 | ntb_connection_get_remote_address_string(struct ntb_connection *conn); 124 | 125 | const struct ntb_netaddress * 126 | ntb_connection_get_remote_address(struct ntb_connection *conn); 127 | 128 | void 129 | ntb_connection_send_verack(struct ntb_connection *conn); 130 | 131 | void 132 | ntb_connection_send_version(struct ntb_connection *conn, 133 | uint64_t nonce, 134 | uint16_t local_port); 135 | 136 | void 137 | ntb_connection_send_blob(struct ntb_connection *conn, 138 | const uint8_t *hash, 139 | struct ntb_blob *blob); 140 | 141 | void 142 | ntb_connection_begin_getdata(struct ntb_connection *conn); 143 | 144 | void 145 | ntb_connection_add_getdata_hash(struct ntb_connection *conn, 146 | const uint8_t *hash); 147 | 148 | void 149 | ntb_connection_end_getdata(struct ntb_connection *conn); 150 | 151 | void 152 | ntb_connection_begin_addr(struct ntb_connection *conn); 153 | 154 | void 155 | ntb_connection_add_addr_address(struct ntb_connection *conn, 156 | int64_t timestamp, 157 | uint32_t stream, 158 | uint64_t services, 159 | const struct ntb_netaddress *address); 160 | 161 | void 162 | ntb_connection_end_addr(struct ntb_connection *conn); 163 | 164 | void 165 | ntb_connection_begin_inv(struct ntb_connection *conn); 166 | 167 | void 168 | ntb_connection_add_inv_hash(struct ntb_connection *conn, 169 | const uint8_t *hash); 170 | 171 | void 172 | ntb_connection_end_inv(struct ntb_connection *conn); 173 | 174 | #endif /* NTB_CONNECTION_H */ 175 | -------------------------------------------------------------------------------- /src/ntb-cpus.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | 29 | #include "ntb-cpus.h" 30 | #include "ntb-util.h" 31 | 32 | static int 33 | get_n_cpus_from_file(FILE *file) 34 | { 35 | char search_string[] = "processor"; 36 | const int search_string_length = sizeof search_string - 1; 37 | int n_cpus = 0; 38 | int state = 0; /* state 0 is the beginning of a line */ 39 | int ch; 40 | 41 | while (true) { 42 | ch = fgetc(file); 43 | 44 | if (ch == EOF) 45 | return MAX(n_cpus, 1); 46 | 47 | if (ch == '\n') { 48 | state = 0; /* we're now at the beginning of a line */ 49 | } else if (state == search_string_length) { 50 | if (ch == ':') { 51 | n_cpus++; 52 | state = -1; /* skip the rest of the line */ 53 | } else if (ch != ' ' && ch != '\t') { 54 | state = -1; /* skip the rest of the line */ 55 | } 56 | } else if (state != -1) { 57 | if (ch == search_string[state]) 58 | state++; 59 | else 60 | state = -1; /* skip the rest of the line */ 61 | } 62 | } 63 | } 64 | 65 | int 66 | ntb_cpus_count(void) 67 | { 68 | FILE *file; 69 | static int n_cpus = 0; 70 | 71 | /* Use the cached value if we've already calculated it */ 72 | if (n_cpus) 73 | return n_cpus; 74 | 75 | file = fopen("/proc/cpuinfo", "r"); 76 | if (file == NULL) { 77 | n_cpus = 1; 78 | } else { 79 | n_cpus = get_n_cpus_from_file(file); 80 | fclose(file); 81 | } 82 | 83 | return n_cpus; 84 | } 85 | -------------------------------------------------------------------------------- /src/ntb-cpus.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_CPUS_H 25 | #define NTB_CPUS_H 26 | 27 | int 28 | ntb_cpus_count(void); 29 | 30 | #endif /* NTB_CPUS_H */ 31 | -------------------------------------------------------------------------------- /src/ntb-crypto.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_CRYPTO_H 25 | #define NTB_CRYPTO_H 26 | 27 | #include 28 | #include 29 | 30 | #include "ntb-error.h" 31 | #include "ntb-key.h" 32 | #include "ntb-blob.h" 33 | #include "ntb-address.h" 34 | 35 | struct ntb_crypto; 36 | 37 | struct ntb_crypto_cookie; 38 | 39 | typedef void 40 | (* ntb_crypto_create_key_func)(struct ntb_key *key, 41 | void *user_data); 42 | 43 | typedef void 44 | (* ntb_crypto_create_pubkey_blob_func)(struct ntb_blob *blob, 45 | void *user_data); 46 | 47 | typedef void 48 | (* ntb_crypto_create_msg_blob_func)(struct ntb_blob *blob, 49 | void *user_data); 50 | 51 | /* If the decryption failed, the key and blob will be NULL. The blob 52 | * will have the msg type but it isn't a real msg and instead it 53 | * contains the decrypted data */ 54 | typedef void 55 | (* ntb_crypto_decrypt_msg_func)(struct ntb_key *key, 56 | struct ntb_blob *blob, 57 | void *user_data); 58 | 59 | typedef void 60 | (* ntb_crypto_generate_ackdata_func)(const uint8_t *ackdata, 61 | void *user_data); 62 | 63 | 64 | struct ntb_crypto * 65 | ntb_crypto_new(void); 66 | 67 | /* Creates a new private key. The key parameters must not be given 68 | * because they will be generated */ 69 | struct ntb_crypto_cookie * 70 | ntb_crypto_create_key(struct ntb_crypto *crypto, 71 | const struct ntb_key_params *params, 72 | int leading_zeroes, 73 | ntb_crypto_create_key_func callback, 74 | void *user_data); 75 | 76 | struct ntb_crypto_cookie * 77 | ntb_crypto_create_pubkey_blob(struct ntb_crypto *crypto, 78 | struct ntb_key *key, 79 | ntb_crypto_create_pubkey_blob_func callback, 80 | void *user_data); 81 | 82 | struct ntb_crypto_cookie * 83 | ntb_crypto_create_msg_blob(struct ntb_crypto *crypto, 84 | int64_t timestamp, 85 | struct ntb_key *from_key, 86 | struct ntb_key *to_key, 87 | struct ntb_blob *content, 88 | ntb_crypto_create_msg_blob_func callback, 89 | void *user_data); 90 | 91 | /* The private keys must not be given but the public keys must */ 92 | struct ntb_crypto_cookie * 93 | ntb_crypto_create_public_key(struct ntb_crypto *crypto, 94 | const struct ntb_key_params *params, 95 | ntb_crypto_create_key_func callback, 96 | void *user_data); 97 | 98 | struct ntb_crypto_cookie * 99 | ntb_crypto_check_pubkey(struct ntb_crypto *crypto, 100 | const struct ntb_address *address, 101 | struct ntb_blob *blob, 102 | ntb_crypto_create_key_func callback, 103 | void *user_data); 104 | 105 | struct ntb_crypto_cookie * 106 | ntb_crypto_decrypt_msg(struct ntb_crypto *crypto, 107 | struct ntb_blob *msg, 108 | struct ntb_key * const *keys, 109 | int n_keys, 110 | ntb_crypto_decrypt_msg_func callback, 111 | void *user_data); 112 | 113 | struct ntb_crypto_cookie * 114 | ntb_crypto_generate_ackdata(struct ntb_crypto *crypto, 115 | ntb_crypto_generate_ackdata_func callback, 116 | void *user_data); 117 | 118 | void 119 | ntb_crypto_cancel_task(struct ntb_crypto_cookie *cookie); 120 | 121 | void 122 | ntb_crypto_free(struct ntb_crypto *crypto); 123 | 124 | #endif /* NTB_CRYPTO_H */ 125 | -------------------------------------------------------------------------------- /src/ntb-daemon.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_DAEMON_H 25 | #define NTB_DAEMON_H 26 | 27 | int 28 | ntb_daemon(int argc, char **argv); 29 | 30 | #endif /* NTB_DAEMON_H */ 31 | -------------------------------------------------------------------------------- /src/ntb-dns-bootstrap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | 29 | #include "ntb-dns-bootstrap.h" 30 | #include "ntb-buffer.h" 31 | #include "ntb-netaddress.h" 32 | #include "ntb-main-context.h" 33 | #include "ntb-log.h" 34 | 35 | static void 36 | lookup_address(const char *node, 37 | int port, 38 | ntb_dns_bootstrap_func callback, 39 | void *user_data) 40 | { 41 | struct ntb_netaddress_native native_address; 42 | struct ntb_netaddress address; 43 | struct addrinfo *addrinfo, *a; 44 | int ret; 45 | 46 | ret = getaddrinfo(node, 47 | NULL, /* service */ 48 | NULL, /* hints */ 49 | &addrinfo); 50 | 51 | if (ret) { 52 | ntb_log("Resolving %s failed: %s", 53 | node, 54 | gai_strerror(ret)); 55 | return; 56 | } 57 | 58 | for (a = addrinfo; a; a = a->ai_next) { 59 | switch (a->ai_family) { 60 | case AF_INET: 61 | if (a->ai_addrlen != sizeof (struct sockaddr_in)) 62 | continue; 63 | break; 64 | case AF_INET6: 65 | if (a->ai_addrlen != sizeof (struct sockaddr_in6)) 66 | continue; 67 | break; 68 | default: 69 | continue; 70 | } 71 | 72 | memcpy(&native_address.sockaddr, a->ai_addr, a->ai_addrlen); 73 | native_address.length = a->ai_addrlen; 74 | 75 | ntb_netaddress_from_native(&address, &native_address); 76 | address.port = port; 77 | 78 | callback(&address, user_data); 79 | } 80 | 81 | freeaddrinfo(addrinfo); 82 | } 83 | 84 | void 85 | ntb_dns_bootstrap(ntb_dns_bootstrap_func callback, 86 | void *user_data) 87 | { 88 | ntb_log("Doing DNS bootstrap"); 89 | 90 | lookup_address("bootstrap8080.bitmessage.org", 91 | 8080, 92 | callback, 93 | user_data); 94 | lookup_address("bootstrap8444.bitmessage.org", 95 | 8444, 96 | callback, 97 | user_data); 98 | } 99 | -------------------------------------------------------------------------------- /src/ntb-dns-bootstrap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_DNS_BOOTSTRAP_H 25 | #define NTB_DNS_BOOTSTRAP_H 26 | 27 | #include "ntb-netaddress.h" 28 | 29 | typedef void 30 | (* ntb_dns_bootstrap_func)(const struct ntb_netaddress *address, 31 | void *user_data); 32 | 33 | void 34 | ntb_dns_bootstrap(ntb_dns_bootstrap_func callback, 35 | void *user_data); 36 | 37 | #endif /* NTB_DNS_BOOTSTRAP_H */ 38 | -------------------------------------------------------------------------------- /src/ntb-ecc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_ECC_H 25 | #define NTB_ECC_H 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "ntb-buffer.h" 33 | 34 | #define NTB_ECC_PRIVATE_KEY_SIZE 32 35 | #define NTB_ECC_PUBLIC_KEY_SIZE 65 /* includes the 0x04 prefix */ 36 | 37 | struct ntb_ecc; 38 | 39 | struct ntb_ecc * 40 | ntb_ecc_new(void); 41 | 42 | EC_POINT * 43 | ntb_ecc_make_pub_key_point(struct ntb_ecc *ecc, 44 | const uint8_t *private_key); 45 | 46 | void 47 | ntb_ecc_make_pub_key_bin(struct ntb_ecc *ecc, 48 | const uint8_t *private_key, 49 | uint8_t *public_key); 50 | 51 | EC_KEY * 52 | ntb_ecc_create_key(struct ntb_ecc *ecc, 53 | const uint8_t *private_key); 54 | 55 | 56 | EC_KEY * 57 | ntb_ecc_create_key_with_public(struct ntb_ecc *ecc, 58 | const uint8_t *private_key, 59 | const uint8_t *public_key); 60 | 61 | EC_KEY * 62 | ntb_ecc_create_random_key(struct ntb_ecc *ecc); 63 | 64 | void 65 | ntb_ecc_get_pub_key(struct ntb_ecc *ecc, 66 | EC_KEY *key, 67 | uint8_t *public_key); 68 | 69 | void 70 | ntb_ecc_free(struct ntb_ecc *ecc); 71 | 72 | void 73 | ntb_ecc_encrypt_with_point_begin(struct ntb_ecc *ecc, 74 | const EC_POINT *public_key, 75 | struct ntb_buffer *data_out); 76 | 77 | void 78 | ntb_ecc_encrypt_update(struct ntb_ecc *ecc, 79 | const uint8_t *data_in, 80 | size_t data_in_length, 81 | struct ntb_buffer *data_out); 82 | 83 | void 84 | ntb_ecc_encrypt_end(struct ntb_ecc *ecc, 85 | struct ntb_buffer *data_out); 86 | 87 | void 88 | ntb_ecc_encrypt_with_point(struct ntb_ecc *ecc, 89 | const EC_POINT *public_key, 90 | const uint8_t *data_in, 91 | size_t data_in_length, 92 | struct ntb_buffer *data_out); 93 | 94 | bool 95 | ntb_ecc_decrypt(struct ntb_ecc *ecc, 96 | EC_KEY *key, 97 | const uint8_t *data_in, 98 | size_t data_in_length, 99 | struct ntb_buffer *data_out); 100 | 101 | #endif /* NTB_ECC_H */ 102 | -------------------------------------------------------------------------------- /src/ntb-encoded-words.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_ENCODED_WORDS_H 25 | #define NTB_ENCODED_WORDS_H 26 | 27 | #include 28 | #include 29 | 30 | size_t 31 | ntb_encoded_words_decode(uint8_t *buffer, 32 | size_t length); 33 | 34 | #endif /* NTB_ENCODED_WORDS_H */ 35 | -------------------------------------------------------------------------------- /src/ntb-error.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | 29 | #include "ntb-error.h" 30 | #include "ntb-util.h" 31 | 32 | static struct ntb_error * 33 | ntb_error_new(int buf_size) 34 | { 35 | return ntb_alloc(NTB_STRUCT_OFFSET(struct ntb_error, message) + 36 | buf_size); 37 | } 38 | 39 | void 40 | ntb_set_error_va_list(struct ntb_error **error_out, 41 | struct ntb_error_domain *domain, 42 | int code, 43 | const char *format, 44 | va_list ap) 45 | { 46 | struct ntb_error *error; 47 | va_list apcopy; 48 | size_t buf_size, required_size; 49 | 50 | if (error_out == NULL) 51 | /* Error is being ignored */ 52 | return; 53 | 54 | if (*error_out) { 55 | ntb_warning("Multiple exceptions occured without " 56 | "being handled"); 57 | return; 58 | } 59 | 60 | buf_size = 64; 61 | 62 | error = ntb_error_new(buf_size); 63 | 64 | va_copy(apcopy, ap); 65 | required_size = vsnprintf(error->message, buf_size, format, ap); 66 | 67 | if (required_size >= buf_size) { 68 | ntb_free(error); 69 | buf_size = required_size + 1; 70 | error = ntb_error_new(buf_size); 71 | vsnprintf(error->message, buf_size, format, apcopy); 72 | } 73 | 74 | va_end(apcopy); 75 | 76 | error->domain = domain; 77 | error->code = code; 78 | *error_out = error; 79 | } 80 | 81 | void 82 | ntb_set_error(struct ntb_error **error_out, 83 | struct ntb_error_domain *domain, 84 | int code, 85 | const char *format, 86 | ...) 87 | { 88 | va_list ap; 89 | 90 | va_start(ap, format); 91 | ntb_set_error_va_list(error_out, domain, code, format, ap); 92 | va_end(ap); 93 | } 94 | 95 | void 96 | ntb_error_free(struct ntb_error *error) 97 | { 98 | ntb_free(error); 99 | } 100 | 101 | void 102 | ntb_error_clear(struct ntb_error **error) 103 | { 104 | ntb_error_free(*error); 105 | *error = NULL; 106 | } 107 | 108 | void 109 | ntb_error_propagate(struct ntb_error **error, 110 | struct ntb_error *other) 111 | { 112 | ntb_return_if_fail(other != NULL); 113 | 114 | if (error) 115 | *error = other; 116 | else 117 | ntb_error_free(other); 118 | } 119 | -------------------------------------------------------------------------------- /src/ntb-error.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_ERROR_H 25 | #define NTB_ERROR_H 26 | 27 | #include 28 | 29 | #include "ntb-util.h" 30 | 31 | /* Exception handling mechanism inspired by glib's GError */ 32 | 33 | struct ntb_error_domain { 34 | int stub; 35 | }; 36 | 37 | struct ntb_error { 38 | struct ntb_error_domain *domain; 39 | int code; 40 | char message[1]; 41 | }; 42 | 43 | void 44 | ntb_set_error_va_list(struct ntb_error **error_out, 45 | struct ntb_error_domain *domain, 46 | int code, 47 | const char *format, 48 | va_list ap); 49 | 50 | NTB_PRINTF_FORMAT(4, 5) void 51 | ntb_set_error(struct ntb_error **error, 52 | struct ntb_error_domain *domain, 53 | int code, 54 | const char *format, 55 | ...); 56 | 57 | void 58 | ntb_error_free(struct ntb_error *error); 59 | 60 | void 61 | ntb_error_clear(struct ntb_error **error); 62 | 63 | void 64 | ntb_error_propagate(struct ntb_error **error, 65 | struct ntb_error *other); 66 | 67 | #endif /* NTB_ERROR_H */ 68 | -------------------------------------------------------------------------------- /src/ntb-file-error.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | 28 | #include "ntb-file-error.h" 29 | 30 | struct ntb_error_domain 31 | ntb_file_error; 32 | 33 | enum ntb_file_error 34 | ntb_file_error_from_errno(int errnum) 35 | { 36 | switch (errnum) { 37 | case EEXIST: 38 | return NTB_FILE_ERROR_EXIST; 39 | case EISDIR: 40 | return NTB_FILE_ERROR_ISDIR; 41 | case EACCES: 42 | return NTB_FILE_ERROR_ACCES; 43 | case ENAMETOOLONG: 44 | return NTB_FILE_ERROR_NAMETOOLONG; 45 | case ENOENT: 46 | return NTB_FILE_ERROR_NOENT; 47 | case ENOTDIR: 48 | return NTB_FILE_ERROR_NOTDIR; 49 | case EAGAIN: 50 | #if EAGAIN != EWOULDBLOCK 51 | case EWOULDBLOCK: 52 | #endif 53 | return NTB_FILE_ERROR_AGAIN; 54 | case EINTR: 55 | return NTB_FILE_ERROR_INTR; 56 | case EPERM: 57 | return NTB_FILE_ERROR_PERM; 58 | case EPFNOSUPPORT: 59 | return NTB_FILE_ERROR_PFNOSUPPORT; 60 | case EAFNOSUPPORT: 61 | return NTB_FILE_ERROR_AFNOSUPPORT; 62 | } 63 | 64 | return NTB_FILE_ERROR_OTHER; 65 | } 66 | 67 | NTB_PRINTF_FORMAT(3, 4) void 68 | ntb_file_error_set(struct ntb_error **error, 69 | int errnum, 70 | const char *format, 71 | ...) 72 | { 73 | va_list ap; 74 | 75 | va_start(ap, format); 76 | ntb_set_error_va_list(error, 77 | &ntb_file_error, 78 | ntb_file_error_from_errno(errnum), 79 | format, 80 | ap); 81 | va_end(ap); 82 | } 83 | -------------------------------------------------------------------------------- /src/ntb-file-error.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_FILE_ERROR_H 25 | #define NTB_FILE_ERROR_H 26 | 27 | #include "ntb-error.h" 28 | 29 | extern struct ntb_error_domain 30 | ntb_file_error; 31 | 32 | enum ntb_file_error { 33 | NTB_FILE_ERROR_EXIST, 34 | NTB_FILE_ERROR_ISDIR, 35 | NTB_FILE_ERROR_ACCES, 36 | NTB_FILE_ERROR_NAMETOOLONG, 37 | NTB_FILE_ERROR_NOENT, 38 | NTB_FILE_ERROR_NOTDIR, 39 | NTB_FILE_ERROR_AGAIN, 40 | NTB_FILE_ERROR_INTR, 41 | NTB_FILE_ERROR_PERM, 42 | NTB_FILE_ERROR_PFNOSUPPORT, 43 | NTB_FILE_ERROR_AFNOSUPPORT, 44 | 45 | NTB_FILE_ERROR_OTHER 46 | }; 47 | 48 | enum ntb_file_error 49 | ntb_file_error_from_errno(int errnum); 50 | 51 | NTB_PRINTF_FORMAT(3, 4) void 52 | ntb_file_error_set(struct ntb_error **error, 53 | int errnum, 54 | const char *format, 55 | ...); 56 | 57 | #endif /* NTB_FILE_ERROR_H */ 58 | -------------------------------------------------------------------------------- /src/ntb-hash-table.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_HASH_TABLE_H 25 | #define NTB_HASH_TABLE_H 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | struct ntb_hash_table; 32 | 33 | struct ntb_hash_table * 34 | ntb_hash_table_new(size_t hash_offset); 35 | 36 | void * 37 | ntb_hash_table_get(struct ntb_hash_table *hash_table, 38 | const uint8_t *hash); 39 | 40 | void * 41 | ntb_hash_table_set(struct ntb_hash_table *hash_table, 42 | void *value); 43 | 44 | bool 45 | ntb_hash_table_remove(struct ntb_hash_table *hash_table, 46 | const void *value); 47 | 48 | void 49 | ntb_hash_table_free(struct ntb_hash_table *hash_table); 50 | 51 | #endif /* NTB_HASH_TABLE_H */ 52 | -------------------------------------------------------------------------------- /src/ntb-ipc-client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_IPC_CLIENT_H 25 | #define NTB_IPC_CLIENT_H 26 | 27 | #include 28 | 29 | #include "ntb-error.h" 30 | #include "ntb-buffer.h" 31 | 32 | extern struct ntb_error_domain 33 | ntb_ipc_client_error; 34 | 35 | enum ntb_ipc_client_error { 36 | NTB_IPC_CLIENT_ERROR_INVALID_DATA, 37 | NTB_IPC_CLIENT_ERROR_INVALID_RESPONSE, 38 | NTB_IPC_CLIENT_ERROR_COMMAND_FAILED, 39 | NTB_IPC_CLIENT_ERROR_NO_RESPONSE 40 | }; 41 | 42 | int 43 | ntb_ipc_client_connect(struct ntb_error **error); 44 | 45 | bool 46 | ntb_ipc_client_send_command(int sock, 47 | const uint8_t *data, 48 | size_t data_length, 49 | const int *fds, 50 | size_t n_fds, 51 | struct ntb_error **error); 52 | 53 | bool 54 | ntb_ipc_client_get_response(int sock, 55 | struct ntb_buffer *response_buf, 56 | struct ntb_error **error, 57 | uint32_t request_id); 58 | 59 | #endif /* NTB_IPC_CLIENT_H */ 60 | -------------------------------------------------------------------------------- /src/ntb-ipc-proto.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | 28 | #include "ntb-ipc-proto.h" 29 | #include "ntb-buffer.h" 30 | 31 | void 32 | ntb_ipc_proto_begin_command(struct ntb_buffer *buffer, 33 | const char *name, 34 | uint32_t request_id) 35 | { 36 | int name_length = strlen(name); 37 | int i; 38 | 39 | ntb_buffer_append(buffer, name, name_length); 40 | for (i = name_length; i < 12; i++) 41 | ntb_buffer_append_c(buffer, '\0'); 42 | 43 | request_id = NTB_UINT32_TO_BE(request_id); 44 | ntb_buffer_append(buffer, &request_id, sizeof request_id); 45 | 46 | /* Reserve space for the length */ 47 | ntb_buffer_set_length(buffer, buffer->length + 4); 48 | } 49 | 50 | void 51 | ntb_ipc_proto_end_command(struct ntb_buffer *buffer, 52 | size_t command_start) 53 | { 54 | uint32_t command_length; 55 | 56 | command_length = buffer->length - command_start - 20; 57 | command_length = NTB_UINT32_TO_BE(command_length); 58 | 59 | memcpy(buffer->data + command_start + 16, 60 | &command_length, 61 | sizeof command_length); 62 | } 63 | -------------------------------------------------------------------------------- /src/ntb-ipc-proto.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_IPC_PROTO_H 25 | #define NTB_IPC_PROTO_H 26 | 27 | #include "ntb-buffer.h" 28 | 29 | enum ntb_ipc_proto_status { 30 | NTB_IPC_PROTO_STATUS_SUCCESS = 0, 31 | NTB_IPC_PROTO_STATUS_GENERIC_ERROR, 32 | NTB_IPC_PROTO_STATUS_INVALID_COMMAND, 33 | NTB_IPC_PROTO_STATUS_FD_ERROR, 34 | NTB_IPC_PROTO_STATUS_INVALID_EMAIL, 35 | NTB_IPC_PROTO_STATUS_UNKNOWN_FROM_ADDRESS, 36 | NTB_IPC_PROTO_STATUS_UNSUPPORTED 37 | }; 38 | 39 | void 40 | ntb_ipc_proto_begin_command(struct ntb_buffer *buffer, 41 | const char *name, 42 | uint32_t request_id); 43 | 44 | void 45 | ntb_ipc_proto_end_command(struct ntb_buffer *buffer, 46 | size_t command_start); 47 | 48 | #endif /* NTB_IPC_PROTO_H */ 49 | -------------------------------------------------------------------------------- /src/ntb-ipc-sockaddr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include "ntb-ipc-sockaddr.h" 34 | #include "ntb-util.h" 35 | #include "ntb-buffer.h" 36 | 37 | void 38 | ntb_ipc_sockaddr_create(struct sockaddr **sockaddr_out, 39 | socklen_t *sockaddr_len_out) 40 | { 41 | const char *runtime_dir; 42 | struct ntb_buffer buffer; 43 | struct sockaddr_un *sockaddr; 44 | 45 | ntb_buffer_init(&buffer); 46 | 47 | ntb_buffer_set_length(&buffer, offsetof(struct sockaddr_un, sun_path)); 48 | 49 | runtime_dir = getenv("XDG_RUNTIME_DIR"); 50 | 51 | if (runtime_dir) { 52 | ntb_buffer_append_string(&buffer, runtime_dir); 53 | 54 | while (buffer.length > offsetof(struct sockaddr_un, sun_path) && 55 | buffer.data[buffer.length - 1] == '/') 56 | buffer.length--; 57 | 58 | ntb_buffer_append_string(&buffer, "/notbit/notbit-ipc"); 59 | } else { 60 | ntb_buffer_append_printf(&buffer, 61 | "/tmp/notbit-%i/notbit-ipc", 62 | (int) getuid()); 63 | } 64 | 65 | sockaddr = (struct sockaddr_un *) buffer.data; 66 | 67 | sockaddr->sun_family = AF_LOCAL; 68 | 69 | *sockaddr_out = (struct sockaddr *) sockaddr; 70 | *sockaddr_len_out = buffer.length; 71 | } 72 | -------------------------------------------------------------------------------- /src/ntb-ipc-sockaddr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_IPC_SOCKADDR_H 25 | #define NTB_IPC_SOCKADDR_H 26 | 27 | #include 28 | 29 | void 30 | ntb_ipc_sockaddr_create(struct sockaddr **sockaddr_out, 31 | socklen_t *sockaddr_len_out); 32 | 33 | #endif /* NTB_IPC_SOCKADDR_H */ 34 | -------------------------------------------------------------------------------- /src/ntb-ipc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_IPC_H 25 | #define NTB_IPC_H 26 | 27 | #include "ntb-error.h" 28 | #include "ntb-keyring.h" 29 | 30 | struct ntb_ipc; 31 | 32 | struct ntb_ipc * 33 | ntb_ipc_new(struct ntb_keyring *keyring, 34 | struct ntb_error **error); 35 | 36 | void 37 | ntb_ipc_free(struct ntb_ipc *ipc); 38 | 39 | #endif /* NTB_IPC_H */ 40 | -------------------------------------------------------------------------------- /src/ntb-key-value.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013, 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_KEY_VALUE_H 25 | #define NTB_KEY_VALUE_H 26 | 27 | #include 28 | 29 | #include "ntb-key.h" 30 | 31 | enum ntb_key_value_event { 32 | NTB_KEY_VALUE_EVENT_HEADER, 33 | NTB_KEY_VALUE_EVENT_PROPERTY 34 | }; 35 | 36 | typedef void 37 | (* ntb_key_value_func)(enum ntb_key_value_event event, 38 | int line_number, 39 | const char *key, 40 | const char *value, 41 | void *user_data); 42 | 43 | void 44 | ntb_key_value_load(FILE *file, 45 | ntb_key_value_func func, 46 | void *user_data); 47 | 48 | bool 49 | ntb_key_value_parse_bool_value(int line_number, 50 | const char *value, 51 | bool *result); 52 | 53 | bool 54 | ntb_key_value_parse_int_value(int line_number, 55 | const char *value_string, 56 | int64_t max, 57 | int64_t *result); 58 | 59 | #endif /* NTB_KEY_VALUE_H */ 60 | -------------------------------------------------------------------------------- /src/ntb-key.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_KEY_H 25 | #define NTB_KEY_H 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "ntb-proto.h" 33 | #include "ntb-ref-count.h" 34 | #include "ntb-ecc.h" 35 | #include "ntb-address.h" 36 | 37 | /* Private keys are immutable and reference counted. The ref-count is 38 | * thread-safe so that the key can be passed off to the store thread 39 | * to be written to disk. */ 40 | 41 | struct ntb_key { 42 | struct ntb_ref_count ref_count; 43 | 44 | struct ntb_address address; 45 | uint8_t tag[NTB_ADDRESS_TAG_SIZE]; 46 | uint8_t tag_private_key[NTB_ECC_PRIVATE_KEY_SIZE]; 47 | 48 | char *label; 49 | 50 | int pow_per_byte; 51 | int pow_extra_bytes; 52 | int64_t last_pubkey_send_time; 53 | 54 | EC_KEY *signing_key; 55 | EC_KEY *encryption_key; 56 | 57 | bool enabled; 58 | bool decoy; 59 | }; 60 | 61 | enum ntb_key_param { 62 | NTB_KEY_PARAM_LABEL = (1 << 0), 63 | NTB_KEY_PARAM_VERSION = (1 << 1), 64 | NTB_KEY_PARAM_STREAM = (1 << 2), 65 | NTB_KEY_PARAM_PRIVATE_KEYS = (1 << 3), 66 | NTB_KEY_PARAM_PUBLIC_KEYS = (1 << 4), 67 | NTB_KEY_PARAM_POW_DIFFICULTY = (1 << 5), 68 | NTB_KEY_PARAM_LAST_PUBKEY_SEND_TIME = (1 << 6), 69 | NTB_KEY_PARAM_ENABLED = (1 << 7), 70 | NTB_KEY_PARAM_DECOY = (1 << 8), 71 | NTB_KEY_PARAM_RIPE = (1 << 9) 72 | }; 73 | 74 | /* Optional parameters for ntb_key_new */ 75 | struct ntb_key_params { 76 | /* Flags of parameters that are filled in. Everything is 77 | * optional except that at least one of 78 | * NTB_KEY_PARAM_PRIVATE_KEYS and NTB_KEY_PARAM_PUBLIC_KEYS 79 | * must be provided. Any parameters that don't have the 80 | * corresponding flag set will be set to the default */ 81 | enum ntb_key_param flags; 82 | 83 | /* NTB_KEY_PARAM_LABEL */ 84 | const char *label; 85 | 86 | /* NTB_KEY_PARAM_VERSION */ 87 | uint64_t version; 88 | /* NTB_KEY_PARAM_STREAM */ 89 | uint64_t stream; 90 | 91 | /* NTB_KEY_PARAM_PRIVATE_KEYS */ 92 | const uint8_t *private_signing_key; 93 | const uint8_t *private_encryption_key; 94 | 95 | /* NTB_KEY_PARAM_PUBLIC_KEYS */ 96 | const uint8_t *public_signing_key; 97 | const uint8_t *public_encryption_key; 98 | 99 | /* NTB_KEY_PARAM_POW_DIFFICULTY */ 100 | int pow_per_byte; 101 | int pow_extra_bytes; 102 | 103 | /* NTB_KEY_PARAM_RIPE */ 104 | const uint8_t *ripe; 105 | 106 | /* NTB_KEY_PARAM_LAST_PUBKEY_SEND_TIME */ 107 | int64_t last_pubkey_send_time; 108 | 109 | /* NTB_KEY_PARAM_ENABLED */ 110 | bool enabled; 111 | 112 | /* NTB_KEY_PARAM_DECOY */ 113 | bool decoy; 114 | }; 115 | 116 | struct ntb_key * 117 | ntb_key_new(struct ntb_ecc *ecc, 118 | const struct ntb_key_params *params); 119 | 120 | struct ntb_key * 121 | ntb_key_ref(struct ntb_key *key); 122 | 123 | struct ntb_key * 124 | ntb_key_copy(struct ntb_key *key); 125 | 126 | void 127 | ntb_key_unref(struct ntb_key *key); 128 | 129 | bool 130 | ntb_key_has_private(struct ntb_key *key); 131 | 132 | #endif /* NTB_KEY_H */ 133 | -------------------------------------------------------------------------------- /src/ntb-keygen.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_KEYGEN_H 25 | #define NTB_KEYGEN_H 26 | 27 | int 28 | ntb_keygen(int argc, char **argv); 29 | 30 | #endif /* NTB_KEYGEN_H */ 31 | -------------------------------------------------------------------------------- /src/ntb-keyring.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_KEYRING_H 25 | #define NTB_KEYRING_H 26 | 27 | #include 28 | #include 29 | 30 | #include "ntb-network.h" 31 | #include "ntb-key.h" 32 | #include "ntb-address.h" 33 | #include "ntb-error.h" 34 | 35 | struct ntb_keyring; 36 | 37 | struct ntb_keyring_cookie; 38 | 39 | extern struct ntb_error_domain 40 | ntb_keyring_error; 41 | 42 | enum ntb_keyring_error { 43 | NTB_KEYRING_ERROR_UNKNOWN_FROM_ADDRESS 44 | }; 45 | 46 | typedef void 47 | (* ntb_keyring_create_key_func)(struct ntb_key *key, 48 | void *user_data); 49 | 50 | struct ntb_keyring * 51 | ntb_keyring_new(struct ntb_network *nw); 52 | 53 | void 54 | ntb_keyring_start(struct ntb_keyring *keyring); 55 | 56 | void 57 | ntb_keyring_load_store(struct ntb_keyring *keyring); 58 | 59 | bool 60 | ntb_keyring_send_message(struct ntb_keyring *keyring, 61 | const struct ntb_address *from_address, 62 | const struct ntb_address *to_addresses, 63 | int n_to_addresses, 64 | int content_encoding, 65 | struct ntb_blob *content, 66 | struct ntb_error **error); 67 | 68 | struct ntb_keyring_cookie * 69 | ntb_keyring_create_key(struct ntb_keyring *keyring, 70 | const struct ntb_key_params *params, 71 | int leading_zeroes, 72 | ntb_keyring_create_key_func func, 73 | void *user_data); 74 | 75 | void 76 | ntb_keyring_cancel_task(struct ntb_keyring_cookie *cookie); 77 | 78 | void 79 | ntb_keyring_free(struct ntb_keyring *crypto); 80 | 81 | #endif /* NTB_KEYRING_H */ 82 | -------------------------------------------------------------------------------- /src/ntb-list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2008-2011 Kristian Høgsberg 3 | * Copyright © 2011, 2012, 2013 Intel Corporation 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | /* This list implementation is based on the Wayland source code */ 25 | 26 | #include "config.h" 27 | 28 | #include 29 | #include 30 | 31 | #include "ntb-list.h" 32 | 33 | void 34 | ntb_list_init(struct ntb_list *list) 35 | { 36 | list->prev = list; 37 | list->next = list; 38 | } 39 | 40 | void 41 | ntb_list_insert(struct ntb_list *list, struct ntb_list *elm) 42 | { 43 | elm->prev = list; 44 | elm->next = list->next; 45 | list->next = elm; 46 | elm->next->prev = elm; 47 | } 48 | 49 | void 50 | ntb_list_remove(struct ntb_list *elm) 51 | { 52 | elm->prev->next = elm->next; 53 | elm->next->prev = elm->prev; 54 | elm->next = NULL; 55 | elm->prev = NULL; 56 | } 57 | 58 | int 59 | ntb_list_length(struct ntb_list *list) 60 | { 61 | struct ntb_list *e; 62 | int count; 63 | 64 | count = 0; 65 | e = list->next; 66 | while (e != list) { 67 | e = e->next; 68 | count++; 69 | } 70 | 71 | return count; 72 | } 73 | 74 | int 75 | ntb_list_empty(struct ntb_list *list) 76 | { 77 | return list->next == list; 78 | } 79 | 80 | void 81 | ntb_list_insert_list(struct ntb_list *list, struct ntb_list *other) 82 | { 83 | if (ntb_list_empty(other)) 84 | return; 85 | 86 | other->next->prev = list; 87 | other->prev->next = list->next; 88 | list->next->prev = other->prev; 89 | list->next = other->next; 90 | } 91 | -------------------------------------------------------------------------------- /src/ntb-list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2008 Kristian Høgsberg 3 | * Copyright © 2012, 2013 Intel Corporation 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | /* This list implementation is based on the Wayland source code */ 25 | 26 | #ifndef NTB_LIST_H 27 | #define NTB_LIST_H 28 | 29 | /** 30 | * NtbList - linked list 31 | * 32 | * The list head is of "NtbList" type, and must be initialized 33 | * using ntb_list_init(). All entries in the list must be of the same 34 | * type. The item type must have a "NtbList" member. This 35 | * member will be initialized by ntb_list_insert(). There is no need to 36 | * call ntb_list_init() on the individual item. To query if the list is 37 | * empty in O(1), use ntb_list_empty(). 38 | * 39 | * Let's call the list reference "NtbList foo_list", the item type as 40 | * "item_t", and the item member as "NtbList link". The following code 41 | * 42 | * The following code will initialize a list: 43 | * 44 | * ntb_list_init (foo_list); 45 | * ntb_list_insert (foo_list, item1); Pushes item1 at the head 46 | * ntb_list_insert (foo_list, item2); Pushes item2 at the head 47 | * ntb_list_insert (item2, item3); Pushes item3 after item2 48 | * 49 | * The list now looks like [item2, item3, item1] 50 | * 51 | * Will iterate the list in ascending order: 52 | * 53 | * item_t *item; 54 | * ntb_list_for_each(item, foo_list, link) { 55 | * Do_something_with_item(item); 56 | * } 57 | */ 58 | 59 | struct ntb_list { 60 | struct ntb_list *prev; 61 | struct ntb_list *next; 62 | }; 63 | 64 | void 65 | ntb_list_init(struct ntb_list *list); 66 | 67 | void 68 | ntb_list_insert(struct ntb_list *list, struct ntb_list *elm); 69 | 70 | void 71 | ntb_list_remove(struct ntb_list *elm); 72 | 73 | int 74 | ntb_list_length(struct ntb_list *list); 75 | 76 | int 77 | ntb_list_empty(struct ntb_list *list); 78 | 79 | void 80 | ntb_list_insert_list(struct ntb_list *list, struct ntb_list *other); 81 | 82 | /* This assigns to iterator first so that taking a reference to it 83 | * later in the second step won't be an undefined operation. It 84 | * assigns the value of list_node rather than 0 so that it is possible 85 | * have list_node be based on the previous value of iterator. In that 86 | * respect iterator is just used as a convenient temporary variable. 87 | * The compiler optimises all of this down to a single subtraction by 88 | * a constant */ 89 | #define ntb_list_set_iterator(list_node, iterator, member) \ 90 | ((iterator) = (void *) (list_node), \ 91 | (iterator) = (void *) ((char *) (iterator) - \ 92 | (((char *) &(iterator)->member) - \ 93 | (char *) (iterator)))) 94 | 95 | #define ntb_container_of(ptr, type, member) \ 96 | (type *) ((char *) (ptr) - offsetof (type, member)) 97 | 98 | #define ntb_list_for_each(pos, head, member) \ 99 | for (ntb_list_set_iterator ((head)->next, pos, member); \ 100 | &pos->member != (head); \ 101 | ntb_list_set_iterator (pos->member.next, pos, member)) 102 | 103 | #define ntb_list_for_each_safe(pos, tmp, head, member) \ 104 | for (ntb_list_set_iterator ((head)->next, pos, member), \ 105 | ntb_list_set_iterator ((pos)->member.next, tmp, member); \ 106 | &pos->member != (head); \ 107 | pos = tmp, \ 108 | ntb_list_set_iterator (pos->member.next, tmp, member)) 109 | 110 | #define ntb_list_for_each_reverse(pos, head, member) \ 111 | for (ntb_list_set_iterator ((head)->prev, pos, member); \ 112 | &pos->member != (head); \ 113 | ntb_list_set_iterator (pos->member.prev, pos, member)) 114 | 115 | #endif /* NTB_LIST_H */ 116 | -------------------------------------------------------------------------------- /src/ntb-load-keys.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_LOAD_KEYS_H 25 | #define NTB_LOAD_KEYS_H 26 | 27 | #include 28 | 29 | #include "ntb-key.h" 30 | 31 | /* This is a helper function for ntb_store. It's in its own file just 32 | * because it's a bit big */ 33 | 34 | typedef void (* ntb_load_keys_func)(struct ntb_key *key, 35 | void *user_data); 36 | 37 | void 38 | ntb_load_keys(FILE *file, 39 | ntb_load_keys_func func, 40 | void *user_data); 41 | 42 | #endif /* NTB_LOAD_KEYS_H */ 43 | -------------------------------------------------------------------------------- /src/ntb-load-outgoings.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "ntb-load-outgoings.h" 33 | #include "ntb-key-value.h" 34 | #include "ntb-buffer.h" 35 | #include "ntb-proto.h" 36 | #include "ntb-log.h" 37 | #include "ntb-util.h" 38 | #include "ntb-base58.h" 39 | #include "ntb-address.h" 40 | 41 | struct ntb_load_outgoings_data { 42 | ntb_load_outgoings_func func; 43 | void *user_data; 44 | 45 | struct ntb_store_outgoing outgoing; 46 | bool has_from_address; 47 | bool has_to_address; 48 | bool has_ackdata; 49 | bool has_content_id; 50 | }; 51 | 52 | static void 53 | reset_data(struct ntb_load_outgoings_data *data) 54 | { 55 | data->has_from_address = false; 56 | data->has_to_address = false; 57 | data->has_ackdata = false; 58 | data->has_content_id = false; 59 | data->outgoing.content_encoding = 1; 60 | data->outgoing.last_getpubkey_send_time = 0; 61 | data->outgoing.last_msg_send_time = 0; 62 | } 63 | 64 | static void 65 | flush_outgoing(struct ntb_load_outgoings_data *data) 66 | { 67 | if (data->has_from_address && 68 | data->has_to_address && 69 | data->has_ackdata && 70 | data->has_content_id) 71 | data->func(&data->outgoing, data->user_data); 72 | 73 | reset_data(data); 74 | } 75 | 76 | static bool 77 | parse_ackdata(struct ntb_load_outgoings_data *data, 78 | const char *value, 79 | uint8_t *ackdata) 80 | { 81 | ssize_t got; 82 | 83 | got = ntb_base58_decode(value, strlen(value), 84 | ackdata, 85 | NTB_PROTO_ACKDATA_SIZE); 86 | 87 | if (got == -1) 88 | return false; 89 | 90 | memmove(ackdata + NTB_PROTO_ACKDATA_SIZE - got, ackdata, got); 91 | memset(ackdata, 0, NTB_PROTO_ACKDATA_SIZE - got); 92 | 93 | return true; 94 | } 95 | 96 | static void 97 | process_property(struct ntb_load_outgoings_data *data, 98 | int line_number, 99 | const char *key, 100 | const char *value) 101 | { 102 | int64_t int_value; 103 | 104 | if (!strcmp(key, "fromaddress")) { 105 | if (ntb_address_decode(&data->outgoing.from_address, value)) 106 | data->has_from_address = true; 107 | else 108 | ntb_log("Invalid address on line %i", line_number); 109 | } else if (!strcmp(key, "toaddress")) { 110 | if (ntb_address_decode(&data->outgoing.to_address, value)) 111 | data->has_to_address = true; 112 | else 113 | ntb_log("Invalid address on line %i", line_number); 114 | } else if (!strcmp(key, "ackdata")) { 115 | if (parse_ackdata(data, value, data->outgoing.ackdata)) 116 | data->has_ackdata = true; 117 | else 118 | ntb_log("Invalid ackdata on line %i", line_number); 119 | } else if (!strcmp(key, "contentid")) { 120 | if (ntb_key_value_parse_int_value(line_number, 121 | value, 122 | INT_MAX, 123 | &int_value)) { 124 | data->outgoing.content_id = int_value; 125 | data->has_content_id = true; 126 | } 127 | } else if (!strcmp(key, "contentencoding")) { 128 | if (ntb_key_value_parse_int_value(line_number, 129 | value, 130 | INT_MAX, 131 | &int_value)) 132 | data->outgoing.content_encoding = int_value; 133 | } else if (!strcmp(key, "lastgetpubkeysendtime")) { 134 | if (ntb_key_value_parse_int_value(line_number, 135 | value, 136 | INT64_MAX, 137 | &int_value)) 138 | data->outgoing.last_getpubkey_send_time = int_value; 139 | } else if (!strcmp(key, "lastmsgsendtime")) { 140 | if (ntb_key_value_parse_int_value(line_number, 141 | value, 142 | INT64_MAX, 143 | &int_value)) 144 | data->outgoing.last_msg_send_time = int_value; 145 | } 146 | } 147 | 148 | static void 149 | key_value_event_cb(enum ntb_key_value_event event, 150 | int line_number, 151 | const char *key, 152 | const char *value, 153 | void *user_data) 154 | { 155 | struct ntb_load_outgoings_data *data = user_data; 156 | 157 | switch (event) { 158 | case NTB_KEY_VALUE_EVENT_HEADER: 159 | flush_outgoing(data); 160 | break; 161 | 162 | case NTB_KEY_VALUE_EVENT_PROPERTY: 163 | process_property(data, line_number, key, value); 164 | break; 165 | } 166 | } 167 | 168 | void 169 | ntb_load_outgoings(FILE *file, 170 | ntb_load_outgoings_func func, 171 | void *user_data) 172 | { 173 | struct ntb_load_outgoings_data data; 174 | 175 | ntb_log("Loading outgoing messages"); 176 | 177 | data.func = func; 178 | data.user_data = user_data; 179 | 180 | reset_data(&data); 181 | 182 | ntb_key_value_load(file, key_value_event_cb, &data); 183 | 184 | flush_outgoing(&data); 185 | } 186 | -------------------------------------------------------------------------------- /src/ntb-load-outgoings.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_LOAD_OUTGOINGS_H 25 | #define NTB_LOAD_OUTGOINGS_H 26 | 27 | #include 28 | 29 | #include "ntb-store.h" 30 | 31 | /* This is a helper function for ntb_store. It's in its own file just 32 | * because it's a bit big */ 33 | 34 | typedef void 35 | (* ntb_load_outgoings_func)(const struct ntb_store_outgoing *outgoing, 36 | void *user_data); 37 | 38 | void 39 | ntb_load_outgoings(FILE *file, 40 | ntb_load_outgoings_func func, 41 | void *user_data); 42 | 43 | #endif /* NTB_LOAD_OUTGOINGS_H */ 44 | -------------------------------------------------------------------------------- /src/ntb-log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2011, 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef __NTB_LOG_H__ 25 | #define __NTB_LOG_H__ 26 | 27 | #include 28 | 29 | #include "ntb-util.h" 30 | #include "ntb-log.h" 31 | #include "ntb-error.h" 32 | 33 | bool 34 | ntb_log_available(void); 35 | 36 | NTB_PRINTF_FORMAT(1, 2) void 37 | ntb_log(const char *format, ...); 38 | 39 | bool 40 | ntb_log_set_file(const char *filename, 41 | struct ntb_error **error); 42 | 43 | void 44 | ntb_log_start(void); 45 | 46 | void 47 | ntb_log_close(void); 48 | 49 | #endif /* __NTB_LOG_H__ */ 50 | -------------------------------------------------------------------------------- /src/ntb-mail-parser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_MAIL_PARSER_H 25 | #define NTB_MAIL_PARSER_H 26 | 27 | #include 28 | #include 29 | 30 | #include "ntb-error.h" 31 | #include "ntb-address.h" 32 | 33 | extern struct ntb_error_domain 34 | ntb_mail_parser_error; 35 | 36 | enum ntb_mail_parser_error { 37 | NTB_MAIL_PARSER_ERROR_INVALID_HEADER, 38 | NTB_MAIL_PARSER_ERROR_INVALID_CONTENT_TYPE, 39 | NTB_MAIL_PARSER_ERROR_INVALID_TRANSFER_ENCODING, 40 | NTB_MAIL_PARSER_ERROR_INVALID_ADDRESS, 41 | NTB_MAIL_PARSER_ERROR_MISSING_HEADER 42 | }; 43 | 44 | enum ntb_mail_parser_event { 45 | NTB_MAIL_PARSER_EVENT_SUBJECT, 46 | NTB_MAIL_PARSER_EVENT_SOURCE, 47 | NTB_MAIL_PARSER_EVENT_DESTINATION, 48 | NTB_MAIL_PARSER_EVENT_CONTENT 49 | }; 50 | 51 | struct ntb_mail_parser; 52 | 53 | typedef bool 54 | (* ntb_mail_parser_address_cb)(enum ntb_mail_parser_event event, 55 | const struct ntb_address *address, 56 | void *user_data, 57 | struct ntb_error **error); 58 | 59 | typedef bool 60 | (* ntb_mail_parser_data_cb)(enum ntb_mail_parser_event event, 61 | const uint8_t *data, 62 | size_t length, 63 | void *user_data, 64 | struct ntb_error **error); 65 | 66 | struct ntb_mail_parser * 67 | ntb_mail_parser_new(ntb_mail_parser_address_cb address_cb, 68 | ntb_mail_parser_data_cb data_cb, 69 | void *user_data); 70 | 71 | bool 72 | ntb_mail_parser_parse(struct ntb_mail_parser *parser, 73 | const uint8_t *data, 74 | size_t length, 75 | struct ntb_error **error); 76 | 77 | bool 78 | ntb_mail_parser_end(struct ntb_mail_parser *parser, 79 | struct ntb_error **error); 80 | 81 | void 82 | ntb_mail_parser_free(struct ntb_mail_parser *parser); 83 | 84 | #endif /* NTB_MAIL_PARSER_H */ 85 | -------------------------------------------------------------------------------- /src/ntb-main-context.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2011, 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_MAIN_CONTEXT_H 25 | #define NTB_MAIN_CONTEXT_H 26 | 27 | #include 28 | 29 | #include "ntb-util.h" 30 | #include "ntb-error.h" 31 | 32 | enum ntb_main_context_error { 33 | NTB_MAIN_CONTEXT_ERROR_UNSUPPORTED, 34 | NTB_MAIN_CONTEXT_ERROR_UNKNOWN 35 | }; 36 | 37 | enum ntb_main_context_poll_flags { 38 | NTB_MAIN_CONTEXT_POLL_IN = 1 << 0, 39 | NTB_MAIN_CONTEXT_POLL_OUT = 1 << 1, 40 | NTB_MAIN_CONTEXT_POLL_ERROR = 1 << 2, 41 | }; 42 | 43 | extern struct ntb_error_domain 44 | ntb_main_context_error; 45 | 46 | struct ntb_main_context; 47 | struct ntb_main_context_source; 48 | 49 | typedef void 50 | (* ntb_main_context_poll_callback) (struct ntb_main_context_source *source, 51 | int fd, 52 | enum ntb_main_context_poll_flags flags, 53 | void *user_data); 54 | 55 | typedef void 56 | (* ntb_main_context_timer_callback) (struct ntb_main_context_source *source, 57 | void *user_data); 58 | 59 | typedef void 60 | (* ntb_main_context_idle_callback) (struct ntb_main_context_source *source, 61 | void *user_data); 62 | 63 | typedef void 64 | (* ntb_main_context_quit_callback) (struct ntb_main_context_source *source, 65 | void *user_data); 66 | 67 | struct ntb_main_context * 68 | ntb_main_context_new(void); 69 | 70 | struct ntb_main_context * 71 | ntb_main_context_get_default(void); 72 | 73 | struct ntb_main_context_source * 74 | ntb_main_context_add_poll(struct ntb_main_context *mc, 75 | int fd, 76 | enum ntb_main_context_poll_flags flags, 77 | ntb_main_context_poll_callback callback, 78 | void *user_data); 79 | 80 | void 81 | ntb_main_context_modify_poll(struct ntb_main_context_source *source, 82 | enum ntb_main_context_poll_flags flags); 83 | 84 | struct ntb_main_context_source * 85 | ntb_main_context_add_quit(struct ntb_main_context *mc, 86 | ntb_main_context_quit_callback callback, 87 | void *user_data); 88 | 89 | struct ntb_main_context_source * 90 | ntb_main_context_add_timer(struct ntb_main_context *mc, 91 | int minutes, 92 | ntb_main_context_timer_callback callback, 93 | void *user_data); 94 | 95 | struct ntb_main_context_source * 96 | ntb_main_context_add_idle(struct ntb_main_context *mc, 97 | ntb_main_context_idle_callback callback, 98 | void *user_data); 99 | 100 | void 101 | ntb_main_context_remove_source(struct ntb_main_context_source *source); 102 | 103 | void 104 | ntb_main_context_poll(struct ntb_main_context *mc); 105 | 106 | uint64_t 107 | ntb_main_context_get_monotonic_clock(struct ntb_main_context *mc); 108 | 109 | int64_t 110 | ntb_main_context_get_wall_clock(struct ntb_main_context *mc); 111 | 112 | void 113 | ntb_main_context_free(struct ntb_main_context *mc); 114 | 115 | #endif /* NTB_MAIN_CONTEXT_H */ 116 | -------------------------------------------------------------------------------- /src/ntb-mkdir.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include "ntb-mkdir.h" 31 | #include "ntb-buffer.h" 32 | #include "ntb-file-error.h" 33 | 34 | bool 35 | ntb_mkdir(const char *name, 36 | struct ntb_error **error) 37 | { 38 | if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) == -1 && errno != EEXIST) { 39 | ntb_file_error_set(error, 40 | errno, 41 | "Error creating directory %s: %s", 42 | name, 43 | strerror(errno)); 44 | return false; 45 | } 46 | 47 | return true; 48 | } 49 | 50 | bool 51 | ntb_mkdir_hierarchy(struct ntb_buffer *buf, 52 | struct ntb_error **error) 53 | { 54 | uint8_t *slash; 55 | bool res; 56 | 57 | if (buf->length < 1) 58 | return true; 59 | 60 | slash = buf->data; 61 | 62 | while ((slash = memchr(slash + 1, 63 | '/', 64 | buf->data + buf->length - slash - 1))) { 65 | *slash = '\0'; 66 | 67 | res = ntb_mkdir((const char *) buf->data, error); 68 | 69 | *slash = '/'; 70 | 71 | if (!res) 72 | return false; 73 | } 74 | 75 | if (buf->data[buf->length - 1] != '/') { 76 | ntb_buffer_append_c(buf, '\0'); 77 | buf->length--; 78 | if (!ntb_mkdir((const char *) buf->data, error)) 79 | return false; 80 | } 81 | 82 | return true; 83 | } 84 | -------------------------------------------------------------------------------- /src/ntb-mkdir.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_MKDIR_H 25 | #define NTB_MKDIR_H 26 | 27 | #include 28 | 29 | #include "ntb-error.h" 30 | #include "ntb-buffer.h" 31 | 32 | bool 33 | ntb_mkdir(const char *name, 34 | struct ntb_error **error); 35 | 36 | bool 37 | ntb_mkdir_hierarchy(struct ntb_buffer *buf, 38 | struct ntb_error **error); 39 | 40 | #endif /* NTB_MKDIR_H */ 41 | -------------------------------------------------------------------------------- /src/ntb-netaddress.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_NETADDRESS_H 25 | #define NTB_NETADDRESS_H 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | struct ntb_netaddress { 32 | /* This is in network byte order. It is the same format as in 33 | * the Bitmessage protocol, ie, if it is an IPv4 address then 34 | * it will begin with the 12 bytes 00 00 00 00 00 00 00 00 00 35 | * 00 FF FF followed by the 4 byte address */ 36 | uint8_t host[16]; 37 | /* In native byte order */ 38 | uint16_t port; 39 | }; 40 | 41 | struct ntb_netaddress_native { 42 | union { 43 | struct sockaddr sockaddr; 44 | struct sockaddr_in sockaddr_in; 45 | struct sockaddr_in6 sockaddr_in6; 46 | }; 47 | socklen_t length; 48 | }; 49 | 50 | void 51 | ntb_netaddress_to_native(const struct ntb_netaddress *address, 52 | struct ntb_netaddress_native *native); 53 | 54 | void 55 | ntb_netaddress_from_native(struct ntb_netaddress *address, 56 | const struct ntb_netaddress_native *native); 57 | 58 | char * 59 | ntb_netaddress_to_string(const struct ntb_netaddress *address); 60 | 61 | bool 62 | ntb_netaddress_from_string(struct ntb_netaddress *address, 63 | const char *str, 64 | int default_port); 65 | 66 | bool 67 | ntb_netaddress_is_allowed(const struct ntb_netaddress *address, 68 | bool allow_private_addresses); 69 | 70 | bool 71 | ntb_netaddress_is_ipv6(const struct ntb_netaddress *address); 72 | 73 | #endif /* NTB_NETADDRESS_H */ 74 | -------------------------------------------------------------------------------- /src/ntb-network.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_NETWORK_H 25 | #define NTB_NETWORK_H 26 | 27 | #include 28 | 29 | #include "ntb-error.h" 30 | #include "ntb-signal.h" 31 | #include "ntb-blob.h" 32 | #include "ntb-netaddress.h" 33 | 34 | extern struct ntb_error_domain 35 | ntb_network_error; 36 | 37 | enum ntb_network_error { 38 | NTB_NETWORK_ERROR_INVALID_ADDRESS 39 | }; 40 | 41 | enum ntb_network_add_object_flags { 42 | NTB_NETWORK_SKIP_VALIDATION = (1 << 0), 43 | NTB_NETWORK_DELAY = (1 << 1) 44 | }; 45 | 46 | enum ntb_network_object_location { 47 | NTB_NETWORK_OBJECT_LOCATION_NOWHERE, 48 | NTB_NETWORK_OBJECT_LOCATION_STORE, 49 | NTB_NETWORK_OBJECT_LOCATION_MEMORY 50 | }; 51 | 52 | struct ntb_network; 53 | 54 | struct ntb_network * 55 | ntb_network_new(bool add_default_nodes); 56 | 57 | void 58 | ntb_network_add_object_from_data(struct ntb_network *nw, 59 | const uint8_t *object_data, 60 | size_t object_data_length, 61 | enum ntb_network_add_object_flags flags, 62 | const char *source_note); 63 | 64 | void 65 | ntb_network_add_blob(struct ntb_network *nw, 66 | struct ntb_blob *blob, 67 | enum ntb_network_add_object_flags flags, 68 | const char *source_note); 69 | 70 | void 71 | ntb_network_load_store(struct ntb_network *nw, bool bootstrap); 72 | 73 | bool 74 | ntb_network_add_listen_address(struct ntb_network *nw, 75 | const char *address, 76 | struct ntb_error **error); 77 | 78 | bool 79 | ntb_network_add_peer_address(struct ntb_network *nw, 80 | const char *address, 81 | struct ntb_error **error); 82 | 83 | struct ntb_signal * 84 | ntb_network_get_new_object_signal(struct ntb_network *nw); 85 | 86 | void 87 | ntb_network_set_only_use_explicit_addresses(struct ntb_network *nw, 88 | bool value); 89 | 90 | void 91 | ntb_network_set_allow_private_addresses(struct ntb_network *nw, 92 | bool value); 93 | 94 | void 95 | ntb_network_set_proxy_address(struct ntb_network *nw, 96 | const struct ntb_netaddress *addr); 97 | 98 | enum ntb_network_object_location 99 | ntb_network_get_object(struct ntb_network *nw, 100 | const uint8_t *hash, 101 | struct ntb_blob **blob); 102 | 103 | void 104 | ntb_network_free(struct ntb_network *nw); 105 | 106 | #endif /* NTB_NETWORK_H */ 107 | -------------------------------------------------------------------------------- /src/ntb-parse-addresses.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_PARSE_ADDRESSES_H 25 | #define NTB_PARSE_ADDRESSES_H 26 | 27 | #include 28 | #include 29 | 30 | #include "ntb-error.h" 31 | #include "ntb-buffer.h" 32 | #include "ntb-address.h" 33 | 34 | extern struct ntb_error_domain 35 | ntb_parse_addresses_error; 36 | 37 | enum ntb_parse_addresses_error { 38 | NTB_PARSE_ADDRESSES_ERROR_INVALID 39 | }; 40 | 41 | typedef bool 42 | (* ntb_parse_addresses_cb)(const struct ntb_address *address, 43 | void *user_data, 44 | struct ntb_error **error); 45 | 46 | bool 47 | ntb_parse_addresses(struct ntb_buffer *buffer, 48 | ntb_parse_addresses_cb cb, 49 | void *user_data, 50 | struct ntb_error **error); 51 | 52 | #endif /* NTB_PARSE_ADDRESSES_H */ 53 | -------------------------------------------------------------------------------- /src/ntb-parse-content-type.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2011, 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_PARSE_CONTENT_TYPE_H 25 | #define NTB_PARSE_CONTENT_TYPE_H 26 | 27 | #include 28 | 29 | typedef bool 30 | (* ntb_parse_content_type_type_cb)(const char *type, 31 | void *user_data); 32 | 33 | typedef bool 34 | (* ntb_parse_content_type_attribute_cb)(const char *attribute, 35 | const char *value, 36 | void *user_data); 37 | 38 | bool 39 | ntb_parse_content_type(const char *header_value, 40 | ntb_parse_content_type_type_cb type_cb, 41 | ntb_parse_content_type_attribute_cb attribute_cb, 42 | void *user_data); 43 | 44 | #endif /* NTB_PARSE_CONTENT_TYPE_H */ 45 | -------------------------------------------------------------------------------- /src/ntb-pointer-array.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_POINTER_ARRAY_H 25 | #define NTB_POINTER_ARRAY_H 26 | 27 | #include 28 | 29 | #include "ntb-buffer.h" 30 | 31 | static inline void 32 | ntb_pointer_array_append(struct ntb_buffer *buf, 33 | void *pointer) 34 | { 35 | ntb_buffer_append(buf, &pointer, sizeof (pointer)); 36 | } 37 | 38 | #define ntb_pointer_array_length(buf) \ 39 | ((buf)->length / sizeof (void *)) 40 | 41 | #define ntb_pointer_array_get(buf, index) \ 42 | ((((void **) (buf)->data))[index]) 43 | 44 | #define ntb_pointer_array_set(buf, index, pointer) \ 45 | ((((void **) (buf)->data))[index] = (pointer)) 46 | 47 | #endif /* NTB_POINTER_ARRAY_H */ 48 | -------------------------------------------------------------------------------- /src/ntb-pow.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013, 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_POW_H 25 | #define NTB_POW_H 26 | 27 | #include 28 | #include 29 | 30 | #include "ntb-error.h" 31 | 32 | struct ntb_pow; 33 | 34 | struct ntb_pow_cookie; 35 | 36 | typedef void (* ntb_pow_calculate_func)(uint64_t nonce, 37 | void *user_data); 38 | 39 | struct ntb_pow * 40 | ntb_pow_new(void); 41 | 42 | struct ntb_pow_cookie * 43 | ntb_pow_calculate(struct ntb_pow *pow, 44 | const uint8_t *payload, 45 | size_t length, 46 | int pow_per_byte, 47 | int pow_extra_bytes, 48 | ntb_pow_calculate_func func, 49 | void *user_data); 50 | 51 | void 52 | ntb_pow_cancel(struct ntb_pow_cookie *cookie); 53 | 54 | void 55 | ntb_pow_free(struct ntb_pow *pow); 56 | 57 | uint64_t 58 | ntb_pow_calculate_target(size_t length, 59 | int pow_per_byte, 60 | int pow_extra_bytes, 61 | int remaining_time); 62 | 63 | uint64_t 64 | ntb_pow_calculate_value(const uint8_t *payload, 65 | size_t length); 66 | 67 | bool 68 | ntb_pow_check(const uint8_t *payload, 69 | size_t length, 70 | int pow_per_byte, 71 | int pow_extra_bytes, 72 | int remaining_time); 73 | 74 | #endif /* NTB_POW_H */ 75 | -------------------------------------------------------------------------------- /src/ntb-proxy.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_PROXY_H 25 | #define NTB_PROXY_H 26 | 27 | #include "ntb-error.h" 28 | #include "ntb-netaddress.h" 29 | #include "ntb-buffer.h" 30 | 31 | struct ntb_proxy; 32 | 33 | extern struct ntb_error_domain 34 | ntb_proxy_error; 35 | 36 | enum ntb_proxy_error { 37 | NTB_PROXY_ERROR_BAD_PROTOCOL, 38 | NTB_PROXY_ERROR_NO_AUTHENTICATION_UNSUPPORTED, 39 | NTB_PROXY_ERROR_GENERAL_SOCKS_SERVER_FAILURE, 40 | NTB_PROXY_ERROR_CONNECTION_NOT_ALLOWED_BY_RULESET, 41 | NTB_PROXY_ERROR_NETWORK_UNREACHABLE, 42 | NTB_PROXY_ERROR_HOST_UNREACHABLE, 43 | NTB_PROXY_ERROR_CONNECTION_REFUSED, 44 | NTB_PROXY_ERROR_TTL_EXPIRED, 45 | NTB_PROXY_ERROR_COMMAND_NOT_SUPPORTED, 46 | NTB_PROXY_ERROR_ADDRESS_TYPE_NOT_SUPPORTED, 47 | NTB_PROXY_ERROR_UNKNOWN 48 | }; 49 | 50 | struct ntb_proxy * 51 | ntb_proxy_new(const struct ntb_netaddress *dst_addr, 52 | struct ntb_buffer *in_buf, 53 | struct ntb_buffer *out_buf); 54 | 55 | bool 56 | ntb_proxy_process_commands(struct ntb_proxy *proxy, 57 | struct ntb_error **error); 58 | 59 | bool 60 | ntb_proxy_is_connected(struct ntb_proxy *proxy); 61 | 62 | void 63 | ntb_proxy_free(struct ntb_proxy *proxy); 64 | 65 | #endif /* NTB_PROXY_H */ 66 | -------------------------------------------------------------------------------- /src/ntb-quoted-printable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_QUOTED_PRINTABLE_H 25 | #define NTB_QUOTED_PRINTABLE_H 26 | 27 | #include 28 | #include 29 | 30 | #include "ntb-error.h" 31 | 32 | extern struct ntb_error_domain 33 | ntb_quoted_printable_error; 34 | 35 | enum ntb_quoted_printable_error { 36 | NTB_QUOTED_PRINTABLE_ERROR_INVALID_ESCAPE 37 | }; 38 | 39 | enum ntb_quoted_printable_state { 40 | /* The default state where if we see a character we will 41 | * directly add it to the output or if we see an equals sign 42 | * we'll start a quote */ 43 | NTB_QUOTED_PRINTABLE_STATE_OCTET, 44 | /* We've encountered an equals sign and the next character 45 | * will determine how to handle it */ 46 | NTB_QUOTED_PRINTABLE_STATE_QUOTE_START, 47 | /* We've encountered a space or tab character afer an equals 48 | * sign and we're ignoring the rest of the whitespace until 49 | * the end of the line */ 50 | NTB_QUOTED_PRINTABLE_STATE_SKIP_SPACES, 51 | /* We've encountered the CR (0xd) of a soft line break */ 52 | NTB_QUOTED_PRINTABLE_STATE_SOFT_CR, 53 | /* We've encountered the first hex digit of an escaped octet */ 54 | NTB_QUOTED_PRINTABLE_STATE_ESCAPED_OCTET 55 | }; 56 | 57 | struct ntb_quoted_printable_data { 58 | enum ntb_quoted_printable_state state; 59 | int nibble; 60 | uint8_t *out; 61 | bool underscore_is_space; 62 | }; 63 | 64 | void 65 | ntb_quoted_printable_decode_start(struct ntb_quoted_printable_data *state, 66 | bool underscore_is_space); 67 | 68 | ssize_t 69 | ntb_quoted_printable_decode(struct ntb_quoted_printable_data *state, 70 | const uint8_t *in_buffer, 71 | size_t length, 72 | uint8_t *out_buffer, 73 | struct ntb_error **error); 74 | 75 | bool 76 | ntb_quoted_printable_decode_end(struct ntb_quoted_printable_data *state, 77 | struct ntb_error **error); 78 | 79 | #endif /* NTB_QUOTED_PRINTABLE_H */ 80 | -------------------------------------------------------------------------------- /src/ntb-ref-count.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_REF_COUNT_H 25 | #define NTB_REF_COUNT_H 26 | 27 | #ifndef HAVE_SYNC_REF_COUNT 28 | #include 29 | #endif /* HAVE_SYNC_REF_COUNT */ 30 | 31 | #include 32 | #include 33 | 34 | /* This is used for thread-safe atomic reference counting */ 35 | 36 | struct ntb_ref_count { 37 | #ifndef HAVE_SYNC_REF_COUNT 38 | pthread_mutex_t mutex; 39 | #endif /* HAVE_SYNC_REF_COUNT */ 40 | 41 | int count; 42 | }; 43 | 44 | static inline void 45 | ntb_ref_count_init(struct ntb_ref_count *ref_count) 46 | { 47 | #ifndef HAVE_SYNC_REF_COUNT 48 | pthread_mutex_init(&blob->mutex, NULL); 49 | #endif 50 | ref_count->count = 1; 51 | } 52 | 53 | static inline void 54 | ntb_ref_count_ref(struct ntb_ref_count *ref_count) 55 | { 56 | #ifdef HAVE_SYNC_REF_COUNT 57 | __sync_fetch_and_add(&ref_count->count, 1); 58 | #else 59 | pthread_mutex_lock(&ref_count->mutex); 60 | blob->count++; 61 | pthread_mutex_unlock(&ref_count->mutex); 62 | #endif 63 | } 64 | 65 | /* Returns the old ref count */ 66 | 67 | static inline int 68 | ntb_ref_count_unref(struct ntb_ref_count *ref_count) 69 | { 70 | int old_value; 71 | 72 | #ifdef HAVE_SYNC_REF_COUNT 73 | old_value = __sync_fetch_and_sub(&ref_count->count, 1); 74 | #else 75 | pthread_mutex_lock(&ref_count->mutex); 76 | old_value = ref_count->count--; 77 | pthread_mutex_unlock(&ref_count->mutex); 78 | #endif 79 | 80 | return old_value; 81 | } 82 | 83 | static inline void 84 | ntb_ref_count_destroy(struct ntb_ref_count *ref_count) 85 | { 86 | #ifndef HAVE_SYNC_REF_COUNT 87 | pthread_mutex_destroy(&ref_count->mutex); 88 | #endif 89 | } 90 | 91 | #endif /* NTB_REF_COUNT_H */ 92 | -------------------------------------------------------------------------------- /src/ntb-save-message.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_SAVE_MESSAGE_H 25 | #define NTB_SAVE_MESSAGE_H 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include "ntb-blob.h" 32 | #include "ntb-key.h" 33 | 34 | void 35 | ntb_save_message(time_t timestamp, 36 | struct ntb_key *from_key, 37 | const char *from_address, 38 | struct ntb_key *to_key, 39 | struct ntb_blob *blob, 40 | FILE *out); 41 | 42 | #endif /* NTB_SAVE_MESSAGE_H */ 43 | -------------------------------------------------------------------------------- /src/ntb-sendmail.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include "ntb-sendmail.h" 35 | #include "ntb-util.h" 36 | #include "ntb-ipc-client.h" 37 | #include "ntb-buffer.h" 38 | #include "ntb-proto.h" 39 | 40 | static const uint8_t 41 | email_command[] = 42 | "email\0\0\0\0\0\0\0" /* command name */ 43 | "\0\0\0\0" /* request id */ 44 | "\0\0\0\0"; /* payload length */ 45 | 46 | static bool 47 | send_email_command(int sock, 48 | struct ntb_error **error) 49 | { 50 | int fd = STDIN_FILENO; 51 | 52 | return ntb_ipc_client_send_command(sock, 53 | email_command, 54 | sizeof email_command - 1, 55 | &fd, 56 | 1, /* n_fds */ 57 | error); 58 | } 59 | 60 | int 61 | ntb_sendmail(int argc, char **argv) 62 | { 63 | struct ntb_error *error = NULL; 64 | struct ntb_buffer response_buf; 65 | int ret = EXIT_SUCCESS; 66 | int sock; 67 | 68 | sock = ntb_ipc_client_connect(&error); 69 | 70 | if (sock == -1) { 71 | fprintf(stderr, "%s\n", error->message); 72 | ntb_error_free(error); 73 | return EXIT_FAILURE; 74 | } 75 | 76 | if (send_email_command(sock, &error)) { 77 | ntb_buffer_init(&response_buf); 78 | if (!ntb_ipc_client_get_response(sock, 79 | &response_buf, 80 | &error, 81 | 0 /* request_id */)) { 82 | fprintf(stderr, "%s\n", error->message); 83 | ntb_error_free(error); 84 | ret = EXIT_FAILURE; 85 | } 86 | ntb_buffer_destroy(&response_buf); 87 | } else { 88 | fprintf(stderr, "%s\n", error->message); 89 | ntb_error_free(error); 90 | ret = EXIT_FAILURE; 91 | } 92 | 93 | close(sock); 94 | 95 | return ret; 96 | } 97 | -------------------------------------------------------------------------------- /src/ntb-sendmail.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2014 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_SENDMAIL_H 25 | #define NTB_SENDMAIL_H 26 | 27 | int 28 | ntb_sendmail(int argc, char **argv); 29 | 30 | #endif /* NTB_SENDMAIL_H */ 31 | -------------------------------------------------------------------------------- /src/ntb-signal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2008 Kristian Høgsberg 3 | * Copyright © 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | /* This file was originally borrowed from the Wayland source code */ 25 | 26 | #ifndef NTB_SIGNAL_H 27 | #define NTB_SIGNAL_H 28 | 29 | #include 30 | 31 | #include "ntb-list.h" 32 | 33 | struct ntb_listener; 34 | 35 | typedef bool 36 | (* ntb_notify_func)(struct ntb_listener *listener, void *data); 37 | 38 | struct ntb_signal { 39 | struct ntb_list listener_list; 40 | }; 41 | 42 | struct ntb_listener { 43 | struct ntb_list link; 44 | ntb_notify_func notify; 45 | }; 46 | 47 | static inline void 48 | ntb_signal_init(struct ntb_signal *signal) 49 | { 50 | ntb_list_init(&signal->listener_list); 51 | } 52 | 53 | static inline void 54 | ntb_signal_add(struct ntb_signal *signal, 55 | struct ntb_listener *listener) 56 | { 57 | ntb_list_insert(signal->listener_list.prev, &listener->link); 58 | } 59 | 60 | static inline bool 61 | ntb_signal_emit(struct ntb_signal *signal, void *data) 62 | { 63 | struct ntb_listener *l, *next; 64 | 65 | ntb_list_for_each_safe(l, next, &signal->listener_list, link) 66 | if (!l->notify(l, data)) 67 | return false; 68 | 69 | return true; 70 | } 71 | 72 | #endif /* NTB_SIGNAL_H */ 73 | -------------------------------------------------------------------------------- /src/ntb-slab.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | 28 | #include "ntb-slab.h" 29 | #include "ntb-util.h" 30 | 31 | /* All of the allocations are made out of slabs of 2kb. That way all 32 | * of the memory can be freed by just freeing the few slabs */ 33 | struct ntb_slab { 34 | struct ntb_slab *next; 35 | }; 36 | 37 | void 38 | ntb_slab_init(struct ntb_slab_allocator *allocator) 39 | { 40 | static const struct ntb_slab_allocator init = NTB_SLAB_STATIC_INIT; 41 | 42 | *allocator = init; 43 | } 44 | 45 | static size_t 46 | ntb_slab_align(size_t base, int alignment) 47 | { 48 | return (base + alignment - 1) & ~(alignment - 1); 49 | } 50 | 51 | void * 52 | ntb_slab_allocate(struct ntb_slab_allocator *allocator, 53 | size_t size, int alignment) 54 | { 55 | struct ntb_slab *slab; 56 | size_t offset; 57 | 58 | offset = ntb_slab_align(allocator->slab_used, alignment); 59 | 60 | if (size + offset > NTB_SLAB_SIZE) { 61 | /* Start a new slab */ 62 | slab = ntb_alloc(NTB_SLAB_SIZE); 63 | slab->next = allocator->slabs; 64 | allocator->slabs = slab; 65 | 66 | offset = ntb_slab_align(sizeof(struct ntb_slab), alignment); 67 | } else { 68 | slab = allocator->slabs; 69 | } 70 | 71 | allocator->slab_used = offset + size; 72 | 73 | return (uint8_t *) slab + offset; 74 | } 75 | 76 | void 77 | ntb_slab_destroy(struct ntb_slab_allocator *allocator) 78 | { 79 | struct ntb_slab *slab, *next; 80 | 81 | for (slab = allocator->slabs; slab; slab = next) { 82 | next = slab->next; 83 | ntb_free(slab); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/ntb-slab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_SLAB_H 25 | #define NTB_SLAB_H 26 | 27 | #include 28 | 29 | struct ntb_slab; 30 | 31 | #define NTB_SLAB_SIZE 2048 32 | 33 | struct ntb_slab_allocator { 34 | struct ntb_slab *slabs; 35 | size_t slab_used; 36 | }; 37 | 38 | #define NTB_SLAB_STATIC_INIT \ 39 | { .slabs = NULL, .slab_used = NTB_SLAB_SIZE } 40 | 41 | void 42 | ntb_slab_init(struct ntb_slab_allocator *allocator); 43 | 44 | void * 45 | ntb_slab_allocate(struct ntb_slab_allocator *allocator, 46 | size_t size, 47 | int alignment); 48 | 49 | void 50 | ntb_slab_destroy(struct ntb_slab_allocator *allocator); 51 | 52 | #endif /* NTB_SLAB_H */ 53 | -------------------------------------------------------------------------------- /src/ntb-slice.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include "ntb-slice.h" 27 | 28 | void 29 | ntb_slice_allocator_init(struct ntb_slice_allocator *allocator, 30 | size_t size, 31 | size_t alignment) 32 | { 33 | allocator->element_size = MAX(size, sizeof (struct ntb_slice)); 34 | allocator->element_alignment = alignment; 35 | allocator->magazine = NULL; 36 | ntb_slab_init(&allocator->slab); 37 | } 38 | 39 | void 40 | ntb_slice_allocator_destroy(struct ntb_slice_allocator *allocator) 41 | { 42 | ntb_slab_destroy(&allocator->slab); 43 | } 44 | 45 | void * 46 | ntb_slice_alloc(struct ntb_slice_allocator *allocator) 47 | { 48 | void *ret; 49 | 50 | if (allocator->magazine) { 51 | ret = allocator->magazine; 52 | allocator->magazine = allocator->magazine->next; 53 | } else { 54 | ret = ntb_slab_allocate(&allocator->slab, 55 | allocator->element_size, 56 | allocator->element_alignment); 57 | } 58 | 59 | return ret; 60 | } 61 | 62 | void 63 | ntb_slice_free(struct ntb_slice_allocator *allocator, 64 | void *ptr) 65 | { 66 | struct ntb_slice *slice = ptr; 67 | 68 | slice->next = allocator->magazine; 69 | allocator->magazine = slice; 70 | } 71 | -------------------------------------------------------------------------------- /src/ntb-slice.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_SLICE_H 25 | #define NTB_SLICE_H 26 | 27 | #include "ntb-util.h" 28 | #include "ntb-slab.h" 29 | 30 | struct ntb_slice { 31 | struct ntb_slice *next; 32 | }; 33 | 34 | struct ntb_slice_allocator { 35 | size_t element_size; 36 | size_t element_alignment; 37 | struct ntb_slice *magazine; 38 | struct ntb_slab_allocator slab; 39 | }; 40 | 41 | #define NTB_SLICE_ALLOCATOR(type, name) \ 42 | static struct ntb_slice_allocator \ 43 | name = { \ 44 | .element_size = MAX(sizeof(type), sizeof (struct ntb_slice)), \ 45 | .element_alignment = NTB_ALIGNOF(type), \ 46 | .magazine = NULL, \ 47 | .slab = NTB_SLAB_STATIC_INIT \ 48 | } 49 | 50 | void 51 | ntb_slice_allocator_init(struct ntb_slice_allocator *allocator, 52 | size_t size, 53 | size_t alignment); 54 | 55 | void 56 | ntb_slice_allocator_destroy(struct ntb_slice_allocator *allocator); 57 | 58 | void * 59 | ntb_slice_alloc(struct ntb_slice_allocator *allocator); 60 | 61 | void 62 | ntb_slice_free(struct ntb_slice_allocator *allocator, 63 | void *ptr); 64 | 65 | #endif /* NTB_SLICE_H */ 66 | -------------------------------------------------------------------------------- /src/ntb-socket.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include "ntb-socket.h" 31 | #include "ntb-file-error.h" 32 | 33 | bool 34 | ntb_socket_set_nonblock(int sock, 35 | struct ntb_error **error) 36 | { 37 | int flags; 38 | 39 | flags = fcntl(sock, F_GETFL, 0); 40 | 41 | if (flags == -1 || fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) { 42 | ntb_file_error_set(error, 43 | errno, 44 | "Error setting non-blocking mode: %s", 45 | strerror(errno)); 46 | return false; 47 | } 48 | 49 | return true; 50 | } 51 | -------------------------------------------------------------------------------- /src/ntb-socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_SOCKET_H 25 | #define NTB_SOCKET_H 26 | 27 | #include 28 | 29 | #include "ntb-error.h" 30 | 31 | bool 32 | ntb_socket_set_nonblock(int sock, 33 | struct ntb_error **error); 34 | 35 | #endif /* NTB_SOCKET_H */ 36 | -------------------------------------------------------------------------------- /src/ntb-store.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_STORE_H 25 | #define NTB_STORE_H 26 | 27 | #include 28 | 29 | #include "ntb-blob.h" 30 | #include "ntb-error.h" 31 | #include "ntb-netaddress.h" 32 | #include "ntb-key.h" 33 | #include "ntb-proto.h" 34 | 35 | /* The store is used to do all of the disk I/O. The actions are stored 36 | * in a queue and then executed in a separate thread */ 37 | 38 | struct ntb_store; 39 | 40 | extern struct ntb_error_domain 41 | ntb_store_error; 42 | 43 | enum ntb_store_error { 44 | NTB_STORE_ERROR_INVALID_STORE_DIRECTORY, 45 | NTB_STORE_ERROR_INVALID_MAILDIR 46 | }; 47 | 48 | struct ntb_store_cookie; 49 | 50 | struct ntb_store_addr { 51 | int64_t timestamp; 52 | uint32_t stream; 53 | uint64_t services; 54 | struct ntb_netaddress address; 55 | }; 56 | 57 | struct ntb_store_outgoing { 58 | struct ntb_address from_address; 59 | struct ntb_address to_address; 60 | uint8_t ackdata[NTB_PROTO_ACKDATA_SIZE]; 61 | uint64_t content_id; 62 | int content_encoding; 63 | int64_t last_getpubkey_send_time; 64 | int64_t last_msg_send_time; 65 | }; 66 | 67 | typedef void 68 | (* ntb_store_for_each_blob_func)(const uint8_t *hash, 69 | int64_t expires_time, 70 | void *user_data); 71 | 72 | typedef void 73 | (* ntb_store_for_each_pubkey_blob_func)(const uint8_t *hash, 74 | int64_t expires_time, 75 | struct ntb_blob *blob, 76 | void *user_data); 77 | 78 | typedef void 79 | (* ntb_store_for_each_addr_func)(const struct ntb_store_addr *addr, 80 | void *user_data); 81 | 82 | typedef void 83 | (* ntb_store_for_each_key_func)(struct ntb_key *key, 84 | void *user_data); 85 | 86 | typedef void 87 | (* ntb_store_for_each_outgoing_func)(const struct ntb_store_outgoing *outgoing, 88 | void *user_data); 89 | 90 | /* This is called when a load is complete. If the load succeeded then 91 | * blob will point to the contents. If it failed the callback will 92 | * still be called but blob will be NULL. The callback won't be called 93 | * at all if the task is cancelled. The callback will always be 94 | * invoked from an idle handler in the main thread */ 95 | typedef void (* ntb_store_load_callback)(struct ntb_blob *blob, 96 | void *user_data); 97 | 98 | struct ntb_store * 99 | ntb_store_new(const char *store_directory, 100 | const char *maildir, 101 | struct ntb_error **error); 102 | 103 | const char * 104 | ntb_store_get_directory(struct ntb_store *store); 105 | 106 | void 107 | ntb_store_start(struct ntb_store *store); 108 | 109 | struct ntb_store * 110 | ntb_store_get_default(void); 111 | 112 | void 113 | ntb_store_set_default(struct ntb_store *store); 114 | 115 | void 116 | ntb_store_save_blob(struct ntb_store *store, 117 | const uint8_t *hash, 118 | struct ntb_blob *blob); 119 | 120 | void 121 | ntb_store_delete_object(struct ntb_store *store, 122 | const uint8_t *hash); 123 | 124 | void 125 | ntb_store_save_addr_list(struct ntb_store *store, 126 | struct ntb_store_addr *addrs, 127 | int n_addrs); 128 | 129 | void 130 | ntb_store_save_keys(struct ntb_store *store, 131 | struct ntb_key * const *keys, 132 | int n_keys); 133 | 134 | void 135 | ntb_store_save_outgoings(struct ntb_store *store, 136 | struct ntb_blob *blob); 137 | 138 | void 139 | ntb_store_save_message(struct ntb_store *store, 140 | int64_t timestamp, 141 | struct ntb_key *from_key, 142 | const char *from_address, 143 | struct ntb_key *to_key, 144 | struct ntb_blob *blob); 145 | 146 | void 147 | ntb_store_save_message_content(struct ntb_store *store, 148 | uint64_t content_id, 149 | struct ntb_blob *blob); 150 | 151 | struct ntb_store_cookie * 152 | ntb_store_load_message_content(struct ntb_store *store, 153 | uint64_t content_id, 154 | ntb_store_load_callback func, 155 | void *user_data); 156 | 157 | void 158 | ntb_store_delete_message_content(struct ntb_store *store, 159 | uint64_t content_id); 160 | 161 | void 162 | ntb_store_for_each_blob(struct ntb_store *store, 163 | ntb_store_for_each_blob_func func, 164 | void *user_data); 165 | 166 | void 167 | ntb_store_for_each_pubkey_blob(struct ntb_store *store, 168 | ntb_store_for_each_pubkey_blob_func func, 169 | void *user_data); 170 | 171 | void 172 | ntb_store_for_each_addr(struct ntb_store *store, 173 | ntb_store_for_each_addr_func func, 174 | void *user_data); 175 | 176 | void 177 | ntb_store_for_each_key(struct ntb_store *store, 178 | ntb_store_for_each_key_func func, 179 | void *user_data); 180 | 181 | void 182 | ntb_store_for_each_outgoing(struct ntb_store *store, 183 | ntb_store_for_each_outgoing_func func, 184 | void *user_data); 185 | 186 | struct ntb_store_cookie * 187 | ntb_store_load_blob(struct ntb_store *store, 188 | const uint8_t *hash, 189 | ntb_store_load_callback func, 190 | void *user_data); 191 | 192 | void 193 | ntb_store_cancel_task(struct ntb_store_cookie *cookie); 194 | 195 | void 196 | ntb_store_free(struct ntb_store *store); 197 | 198 | #endif /* NTB_STORE_H */ 199 | -------------------------------------------------------------------------------- /src/ntb-util.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #include "config.h" 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "ntb-util.h" 33 | 34 | void 35 | ntb_fatal(const char *format, ...) 36 | { 37 | va_list ap; 38 | 39 | va_start(ap, format); 40 | vfprintf(stderr, format, ap); 41 | va_end(ap); 42 | 43 | fputc('\n', stderr); 44 | 45 | fflush(stderr); 46 | 47 | abort(); 48 | } 49 | 50 | void 51 | ntb_warning(const char *format, ...) 52 | { 53 | va_list ap; 54 | 55 | va_start(ap, format); 56 | vfprintf(stderr, format, ap); 57 | va_end(ap); 58 | 59 | fputc('\n', stderr); 60 | } 61 | 62 | void * 63 | ntb_alloc(size_t size) 64 | { 65 | void *result = malloc(size); 66 | 67 | if (result == NULL) 68 | ntb_fatal("Memory exhausted"); 69 | 70 | return result; 71 | } 72 | 73 | void * 74 | ntb_realloc(void *ptr, size_t size) 75 | { 76 | if (ptr == NULL) 77 | return ntb_alloc(size); 78 | 79 | ptr = realloc(ptr, size); 80 | 81 | if (ptr == NULL) 82 | ntb_fatal("Memory exhausted"); 83 | 84 | return ptr; 85 | } 86 | 87 | void 88 | ntb_free(void *ptr) 89 | { 90 | if (ptr) 91 | free(ptr); 92 | } 93 | 94 | char * 95 | ntb_strdup(const char *str) 96 | { 97 | return ntb_memdup(str, strlen(str) + 1); 98 | } 99 | 100 | void * 101 | ntb_memdup(const void *data, size_t size) 102 | { 103 | void *ret; 104 | 105 | ret = ntb_alloc(size); 106 | memcpy(ret, data, size); 107 | 108 | return ret; 109 | } 110 | 111 | char * 112 | ntb_strconcat(const char *string1, ...) 113 | { 114 | size_t string1_length; 115 | size_t total_length; 116 | size_t str_length; 117 | va_list ap, apcopy; 118 | const char *str; 119 | char *result, *p; 120 | 121 | if (string1 == NULL) 122 | return ntb_strdup(""); 123 | 124 | total_length = string1_length = strlen(string1); 125 | 126 | va_start(ap, string1); 127 | va_copy(apcopy, ap); 128 | 129 | while ((str = va_arg(ap, const char *))) 130 | total_length += strlen(str); 131 | 132 | va_end(ap); 133 | 134 | result = ntb_alloc(total_length + 1); 135 | memcpy(result, string1, string1_length); 136 | p = result + string1_length; 137 | 138 | while ((str = va_arg(apcopy, const char *))) { 139 | str_length = strlen(str); 140 | memcpy(p, str, str_length); 141 | p += str_length; 142 | } 143 | *p = '\0'; 144 | 145 | va_end(apcopy); 146 | 147 | return result; 148 | } 149 | 150 | int 151 | ntb_close(int fd) 152 | { 153 | int ret; 154 | 155 | do { 156 | ret = close(fd); 157 | } while (ret == -1 && errno == EINTR); 158 | 159 | return ret; 160 | } 161 | 162 | pthread_t 163 | ntb_create_thread(void *(* thread_func)(void *), 164 | void *user_data) 165 | { 166 | pthread_t thread; 167 | int result; 168 | 169 | result = pthread_create(&thread, 170 | NULL, /* attr */ 171 | thread_func, 172 | user_data); 173 | 174 | if (result) 175 | ntb_fatal("Error creating thread: %s", 176 | strerror(result)); 177 | 178 | return thread; 179 | } 180 | -------------------------------------------------------------------------------- /src/ntb-util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Notbit - A Bitmessage client 3 | * Copyright (C) 2013 Neil Roberts 4 | * 5 | * Permission to use, copy, modify, distribute, and sell this software and its 6 | * documentation for any purpose is hereby granted without fee, provided that 7 | * the above copyright notice appear in all copies and that both that copyright 8 | * notice and this permission notice appear in supporting documentation, and 9 | * that the name of the copyright holders not be used in advertising or 10 | * publicity pertaining to distribution of the software without specific, 11 | * written prior permission. The copyright holders make no representations 12 | * about the suitability of this software for any purpose. It is provided "as 13 | * is" without express or implied warranty. 14 | * 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 | * OF THIS SOFTWARE. 22 | */ 23 | 24 | #ifndef NTB_UTIL_H 25 | #define NTB_UTIL_H 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #ifdef __GNUC__ 32 | #define NTB_NO_RETURN __attribute__((noreturn)) 33 | #define NTB_PRINTF_FORMAT(string_index, first_to_check) \ 34 | __attribute__((format(printf, string_index, first_to_check))) 35 | #define NTB_NULL_TERMINATED __attribute__((sentinel)) 36 | #else 37 | #define NTB_NO_RETURN 38 | #define NTB_PRINTF_FORMAT(string_index, first_to_check) 39 | #define NTB_NULL_TERMINATED 40 | #endif 41 | 42 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) 43 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) 44 | 45 | #define NTB_ALIGNOF(x) ALIGNOF_NAME(x) 46 | 47 | #define NTB_STRUCT_OFFSET(container, member) \ 48 | ((size_t) &((container *) 0)->member) 49 | 50 | #define NTB_SWAP_UINT16(x) \ 51 | ((uint16_t) \ 52 | (((uint16_t) (x) >> 8) | \ 53 | ((uint16_t) (x) << 8))) 54 | #define NTB_SWAP_UINT32(x) \ 55 | ((uint32_t) \ 56 | ((((uint32_t) (x) & UINT32_C(0x000000ff)) << 24) | \ 57 | (((uint32_t) (x) & UINT32_C(0x0000ff00)) << 8) | \ 58 | (((uint32_t) (x) & UINT32_C(0x00ff0000)) >> 8) | \ 59 | (((uint32_t) (x) & UINT32_C(0xff000000)) >> 24))) 60 | #define NTB_SWAP_UINT64(x) \ 61 | ((uint64_t) \ 62 | ((((uint64_t) (x) & (uint64_t) UINT64_C(0x00000000000000ff)) << 56) | \ 63 | (((uint64_t) (x) & (uint64_t) UINT64_C(0x000000000000ff00)) << 40) | \ 64 | (((uint64_t) (x) & (uint64_t) UINT64_C(0x0000000000ff0000)) << 24) | \ 65 | (((uint64_t) (x) & (uint64_t) UINT64_C(0x00000000ff000000)) << 8) | \ 66 | (((uint64_t) (x) & (uint64_t) UINT64_C(0x000000ff00000000)) >> 8) | \ 67 | (((uint64_t) (x) & (uint64_t) UINT64_C(0x0000ff0000000000)) >> 24) | \ 68 | (((uint64_t) (x) & (uint64_t) UINT64_C(0x00ff000000000000)) >> 40) | \ 69 | (((uint64_t) (x) & (uint64_t) UINT64_C(0xff00000000000000)) >> 56))) 70 | 71 | #if defined(HAVE_BIG_ENDIAN) 72 | #define NTB_UINT16_FROM_BE(x) (x) 73 | #define NTB_UINT32_FROM_BE(x) (x) 74 | #define NTB_UINT64_FROM_BE(x) (x) 75 | #define NTB_UINT16_FROM_LE(x) NTB_SWAP_UINT16(x) 76 | #define NTB_UINT32_FROM_LE(x) NTB_SWAP_UINT32(x) 77 | #define NTB_UINT64_FROM_LE(x) NTB_SWAP_UINT64(x) 78 | #elif defined(HAVE_LITTLE_ENDIAN) 79 | #define NTB_UINT16_FROM_LE(x) (x) 80 | #define NTB_UINT32_FROM_LE(x) (x) 81 | #define NTB_UINT64_FROM_LE(x) (x) 82 | #define NTB_UINT16_FROM_BE(x) NTB_SWAP_UINT16(x) 83 | #define NTB_UINT32_FROM_BE(x) NTB_SWAP_UINT32(x) 84 | #define NTB_UINT64_FROM_BE(x) NTB_SWAP_UINT64(x) 85 | #else 86 | #error Platform is neither little-endian nor big-endian 87 | #endif 88 | 89 | #define NTB_UINT16_TO_LE(x) NTB_UINT16_FROM_LE(x) 90 | #define NTB_UINT16_TO_BE(x) NTB_UINT16_FROM_BE(x) 91 | #define NTB_UINT32_TO_LE(x) NTB_UINT32_FROM_LE(x) 92 | #define NTB_UINT32_TO_BE(x) NTB_UINT32_FROM_BE(x) 93 | #define NTB_UINT64_TO_LE(x) NTB_UINT64_FROM_LE(x) 94 | #define NTB_UINT64_TO_BE(x) NTB_UINT64_FROM_BE(x) 95 | 96 | #define NTB_STMT_START do 97 | #define NTB_STMT_END while (0) 98 | 99 | #define NTB_N_ELEMENTS(array) \ 100 | (sizeof (array) / sizeof ((array)[0])) 101 | 102 | #define NTB_STRINGIFY(macro_or_string) NTB_STRINGIFY_ARG(macro_or_string) 103 | #define NTB_STRINGIFY_ARG(contents) #contents 104 | 105 | void * 106 | ntb_alloc(size_t size); 107 | 108 | void * 109 | ntb_realloc(void *ptr, size_t size); 110 | 111 | void 112 | ntb_free(void *ptr); 113 | 114 | char * 115 | ntb_strdup(const char *str); 116 | 117 | void * 118 | ntb_memdup(const void *data, size_t size); 119 | 120 | NTB_NULL_TERMINATED char * 121 | ntb_strconcat(const char *string1, ...); 122 | 123 | NTB_NO_RETURN NTB_PRINTF_FORMAT(1, 2) void 124 | ntb_fatal(const char *format, ...); 125 | 126 | NTB_PRINTF_FORMAT(1, 2) void 127 | ntb_warning(const char *format, ...); 128 | 129 | int 130 | ntb_close(int fd); 131 | 132 | static inline char 133 | ntb_ascii_tolower(char ch) 134 | { 135 | if (ch >= 'A' && ch <= 'Z') 136 | return ch - 'A' + 'a'; 137 | else 138 | return ch; 139 | } 140 | 141 | pthread_t 142 | ntb_create_thread(void *(* thread_func)(void *), 143 | void *user_data); 144 | 145 | #ifdef HAVE_STATIC_ASSERT 146 | #define NTB_STATIC_ASSERT(EXPRESSION, MESSAGE) \ 147 | _Static_assert(EXPRESSION, MESSAGE); 148 | #else 149 | #define NTB_STATIC_ASSERT(EXPRESSION, MESSAGE) 150 | #endif 151 | 152 | #define ntb_return_if_fail(condition) \ 153 | NTB_STMT_START { \ 154 | if (!(condition)) { \ 155 | ntb_warning("assertion '%s' failed", \ 156 | #condition); \ 157 | return; \ 158 | } \ 159 | } NTB_STMT_END 160 | 161 | #define ntb_return_val_if_fail(condition, val) \ 162 | NTB_STMT_START { \ 163 | if (!(condition)) { \ 164 | ntb_warning("assertion '%s' failed", \ 165 | #condition); \ 166 | return (val); \ 167 | } \ 168 | } NTB_STMT_END 169 | 170 | #define ntb_warn_if_reached() \ 171 | NTB_STMT_START { \ 172 | ntb_warning("Line %i in %s should not be reached", \ 173 | __LINE__, \ 174 | __FILE__); \ 175 | } NTB_STMT_END 176 | 177 | #endif /* NTB_UTIL_H */ 178 | --------------------------------------------------------------------------------