├── .gitignore ├── test ├── cases │ ├── case00.sh │ ├── case01.sh │ ├── case10.sh │ ├── case04.sh │ ├── case05.sh │ ├── case02.sh │ ├── case03.sh │ ├── case07.sh │ ├── case09.sh │ ├── case08.sh │ └── case06.sh ├── integration_tests.sh └── test_base.sh ├── .gitmodules ├── src ├── lib │ ├── limiter.h │ ├── common.c │ ├── sender.h │ ├── common.h │ ├── error.c │ ├── limiter.c │ ├── proto.c │ ├── channel.h │ ├── proto.h │ ├── sender.c │ ├── config.c │ ├── hairgap.h │ ├── channel.c │ ├── encoding.h │ ├── hgap_send.c │ ├── hgap_receive.c │ └── encoding.c ├── hairgapr.c ├── test │ ├── channel_test.c │ └── hgap_test.c └── hairgaps.c ├── README.md ├── GNUmakefile └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | hairgapr 2 | hairgaps 3 | *.o 4 | core.* 5 | *~ 6 | .*.swp 7 | 8 | -------------------------------------------------------------------------------- /test/cases/case00.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source "$TEST_BASE" 3 | init_test 50; do_test $* 4 | -------------------------------------------------------------------------------- /test/cases/case01.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source "$TEST_BASE" 3 | init_test 5 1; do_test $* 4 | -------------------------------------------------------------------------------- /test/cases/case10.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source "$TEST_BASE" 3 | init_test 1000; do_test $* 4 | -------------------------------------------------------------------------------- /test/cases/case04.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source "$TEST_BASE" 3 | init_test 50; do_test -r 2 $* 4 | -------------------------------------------------------------------------------- /test/cases/case05.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source "$TEST_BASE" 3 | init_test 50; do_test -r 1 $* 4 | -------------------------------------------------------------------------------- /test/cases/case02.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source "$TEST_BASE" 3 | init_test 50; do_test -N 64000 $* 4 | -------------------------------------------------------------------------------- /test/cases/case03.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source "$TEST_BASE" 3 | init_test 200; do_test -N 64000 $* 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "wirehair"] 2 | path = wirehair 3 | url = https://github.com/catid/wirehair 4 | -------------------------------------------------------------------------------- /test/cases/case07.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source "$TEST_BASE" 3 | edge_test2() { 4 | init_test 100 5 | echo -n "edge case 2, options: $*" 6 | $HAIRGAPR -t 1 127.0.0.1 > $TO & rpid=$! && usleep 10000; 7 | $HAIRGAPS $* 127.0.0.1 < $FROM & spid=$! 8 | kill "$spid" 9 | wait "$rpid" 10 | RET=$? 11 | check_ret_nok $RET 12 | } 13 | edge_test2 $* 14 | -------------------------------------------------------------------------------- /test/cases/case09.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source "$TEST_BASE" 3 | keepalive_test2() { 4 | init_test 10 5 | echo -n "keepalive test 2, options: $*" 6 | $HAIRGAPR 127.0.0.1 > $TO & rpid=$! && usleep 10000! 7 | (cat $FROM && sleep 2 && cat $FROM) | $HAIRGAPS -k 3000 $* 127.0.0.1 8 | wait "$rpid" 9 | RET=$? 10 | # Check no timeout 11 | wait 12 | check_ret_nok $RET 13 | } 14 | keepalive_test2 $* 15 | -------------------------------------------------------------------------------- /test/cases/case08.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source "$TEST_BASE" 3 | keepalive_test1() { 4 | init_test 10 5 | echo -n "keepalive test 1, options: $*" 6 | $HAIRGAPR 127.0.0.1 > $TO & rpid=$! && usleep 10000! 7 | (cat $FROM && sleep 2 && cat $FROM) | $HAIRGAPS $* 127.0.0.1 8 | wait "$rpid" 9 | RET=$? 10 | # Check no timeout 11 | check_ret_ok $RET || return 12 | wait 13 | check_md5 $(cat $FROM $FROM | md5sum) 14 | } 15 | keepalive_test1 $* 16 | -------------------------------------------------------------------------------- /test/cases/case06.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source "$TEST_BASE" 3 | edge_test1() { 4 | init_test 100 5 | echo -n "edge case 1, options: $*" 6 | $HAIRGAPS $* 127.0.0.1 < $FROM & spid=$! && usleep 10000 7 | $HAIRGAPR -t 1 127.0.0.1 > $TO & rpid=$! 8 | wait "$spid" 9 | RET=$? 10 | if [ "$(cat $TO)" ]; then 11 | fail "dest file should be empty" 12 | return 1 13 | fi 14 | check_ret_nok || return 1 15 | $HAIRGAPS $* 127.0.0.1 < $FROM 16 | kill "$rpid" 17 | check_ret_ok $RET 18 | } 19 | edge_test1 20 | -------------------------------------------------------------------------------- /src/lib/limiter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef HGAP_LIMITER_H 20 | #define HGAP_LIMITER_H 21 | 22 | #include 23 | 24 | struct hgap_limiter; 25 | 26 | struct hgap_limiter *hgap_limiter_new(double byterate); 27 | int hgap_limiter_limit(struct hgap_limiter* hlim, size_t len); 28 | void hgap_limiter_free(struct hgap_limiter* hlim); 29 | 30 | #endif // HGAP_LIMITER_H 31 | -------------------------------------------------------------------------------- /test/integration_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ -z "$ERR" ]; then 4 | ERR=/dev/null 5 | fi 6 | exec 2> $ERR 7 | 8 | print_init() { 9 | printf "[ ... ] " 10 | } 11 | 12 | print_ok() { 13 | printf "\r[ \e[32mOK\e[0m ]" 14 | } 15 | 16 | print_err() { 17 | printf "\r[ \e[31mERR\e[0m ]" 18 | } 19 | 20 | do_test() { 21 | total=$(($total+1)) 22 | script=$1 23 | print_init 24 | echo -n "$(basename $script) " 25 | if ! HGAP_PATH=$HGAP_PATH TEST_BASE=$TEST_BASE /bin/bash $script; then 26 | failed=$(($failed+1)) 27 | print_err 28 | else 29 | print_ok 30 | fi 31 | echo 32 | } 33 | 34 | total=0 35 | failed=0 36 | 37 | BASEDIR=$(dirname $(realpath $0)) 38 | if [ -z $HGAP_PATH ]; then 39 | HGAP_PATH=$(realpath $BASEDIR/..) 40 | fi 41 | 42 | if [ -z $TEST_BASE ]; then 43 | TEST_BASE=$(realpath $BASEDIR/test_base.sh) 44 | fi 45 | 46 | if [ $# -lt 1 ]; then 47 | cases=$BASEDIR/cases/*.sh 48 | else 49 | cases="$*" 50 | fi 51 | 52 | for script in $cases; do 53 | do_test $script 54 | done 55 | 56 | 57 | passed=$(($total-$failed)) 58 | echo "Summary: $passed/$total" 59 | 60 | if [ $failed -ne 0 ]; then 61 | exit 1 62 | fi 63 | 64 | -------------------------------------------------------------------------------- /src/lib/common.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | 20 | #include "common.h" 21 | 22 | #include 23 | 24 | void * 25 | xmalloc(size_t size) 26 | { 27 | void *ret = malloc(size); 28 | CHK_PERROR(ret != NULL); 29 | return ret; 30 | } 31 | 32 | void 33 | dbg_hexdump(void *x, size_t len) { 34 | uint8_t *buf = x; 35 | 36 | for (size_t i = 0; i < len; i++) { 37 | if (i % 16 == 0) { 38 | fprintf(stderr, "\n"); 39 | } else if (i % 8 == 0) { 40 | fprintf(stderr, " "); 41 | } 42 | fprintf(stderr, "%02x ", buf[i]); 43 | } 44 | fprintf(stderr, "\n"); 45 | } 46 | -------------------------------------------------------------------------------- /test/test_base.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ -z "$ERR" ]; then 4 | ERR=/dev/null 5 | fi 6 | exec 2> $ERR 7 | 8 | DIR=$(mktemp -d) 9 | FROM=$DIR/from 10 | TO=$DIR/to 11 | 12 | if [ -z $HGAP_PATH ]; then 13 | HGAP_PATH=. 14 | fi 15 | 16 | HAIRGAPR=$HGAP_PATH/hairgapr 17 | HAIRGAPS=$HGAP_PATH/hairgaps 18 | 19 | cleanup() { 20 | pkill -TERM -P $$ 21 | rm -r "$DIR" 2> $ERR 22 | } 23 | 24 | trap cleanup EXIT 25 | 26 | fail() { 27 | if [ -n "$1" ]; then 28 | printf " \e[31m$1\e[0m" 29 | fi 30 | exit 1 31 | } 32 | 33 | init_test() { 34 | count=$1 35 | if [ -z "$2" ]; then 36 | block=1M 37 | else 38 | block=$2 39 | fi 40 | echo -n "Testcase: $count * $block, " 41 | echo -n "" > $TO 42 | dd if=/dev/zero of=$FROM bs=$block count=$count 43 | } 44 | 45 | check_md5() { 46 | if [ -z "$1" ]; then 47 | FROM_MD5=$(md5sum $FROM |cut -d' ' -f1) 48 | else 49 | FROM_MD5=$(echo -n $1 | cut -d' ' -f1) 50 | fi 51 | TO_MD5=$(md5sum $TO |cut -d' ' -f1) 52 | if [ "$FROM_MD5" != "$TO_MD5" ]; then 53 | fail "from != to" 54 | return 1 55 | fi 56 | return 0 57 | } 58 | 59 | check_ret_ok() { 60 | if [ $1 -ne 0 ]; then 61 | fail "Bad ret (should be 0)" 62 | return 1 63 | fi 64 | return 0 65 | } 66 | 67 | check_ret_nok() { 68 | if [ $1 -eq 0 ]; then 69 | fail "Bad ret (should be != 0)." 70 | return 1 71 | fi 72 | return 0 73 | } 74 | 75 | do_test() { 76 | echo -n "options: $*" 77 | $HAIRGAPR 127.0.0.1 > $TO & rpid=$! && usleep 1000000 78 | $HAIRGAPS $* 127.0.0.1 < $FROM & spid=$! 79 | wait 80 | RET=$? 81 | check_md5 && 82 | check_ret_ok $RET 83 | } 84 | 85 | -------------------------------------------------------------------------------- /src/lib/sender.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef HGAP_SENDER_H 20 | #define HGAP_SENDER_H 21 | 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | /** 28 | * The purpose of this structure is to encapsulate rate limiting + keepalive. 29 | * Apart from the encoder handwave and the encoder teardown, it takes pre-built 30 | * packets to send. 31 | */ 32 | struct hgap_sender; 33 | 34 | /** 35 | * Creates an hgap_sender that will send on socket at maximum rate byterate. 36 | * Creation starts the keepalive. If keepalive is 0, no keepalive is started. 37 | */ 38 | struct hgap_sender *hgap_sender_new(char *host, short port, uint64_t byterate, 39 | uint32_t keepalive); 40 | 41 | /** 42 | * Free any memory associated with this hgap_sender 43 | */ 44 | void hgap_sender_free(struct hgap_sender *hs); 45 | 46 | /** 47 | * Send a single packet. 48 | */ 49 | ssize_t hgap_sender_send(struct hgap_sender *hs, void *pkt, size_t size); 50 | 51 | /** 52 | * Send a control salve of a given packet, see encoding.h for control packet 53 | * generation (e.g. hgap_encoder_handwave and hgap_encoder_teardown). 54 | * 55 | * Being unidirectional and redunded, hairgap protocol control packets receive 56 | * a special treatment: they are sent multiple times in the hope of one reaching 57 | * the destination. 58 | * 59 | * @return 0 on success, return value of sendto(2) otherwise. 60 | */ 61 | ssize_t hgap_sender_control(struct hgap_sender *hs, void *pkt, size_t size); 62 | 63 | #endif // HGAP_SENDER_H 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hairgap 2 | 3 | `hairgap` is a set of tools to transfer data over a unidirectional network link 4 | (typically a network diode). It uses catid's `wirehair` 5 | (https://github.com/catid/wirehair) librairy for error correction and is written 6 | to handle high bandwith transfers (> 200 MB/s). 7 | 8 | It is meant to be a mere transport on a dedicated, safe link: no authentication 9 | nor encryption is guaranteed, although there is a work in progress on this 10 | matter. 11 | 12 | This should be considered alpha quality, any bug report will be very welcome. 13 | 14 | ## Usage 15 | 16 | Before anything, to make high bandwith transfers work properly on a linux 17 | machine, you might want to change at least these system options on the receiver 18 | side: 19 | 20 | ``` 21 | net.core.rmem_max=67108864 # at least 4MB, 64MB is fine 22 | net.core.rmem_default=67108864 # equal to net.core.rmem_max (if you dare) 23 | net.core.netdev_max_backlog=10000 # works fine 24 | ``` 25 | and 26 | ``` 27 | net.ipv4.udp_rmem # multiply all three values by 32 28 | ``` 29 | Or, on newer kernels: 30 | ``` 31 | net.ipv4.udp_mem="49314528 65752736 3082158" 32 | ``` 33 | 34 | 35 | To use, on the receiver side first: 36 | 37 | ```sh 38 | $ hairgapr LISTENING_IP > OUTPUT_FILE 39 | ``` 40 | 41 | Then, on the sender side: 42 | ```sh 43 | $ hairgaps RECEIVER_IP < INPUT_FILE 44 | ``` 45 | 46 | see `hairgap[sr]` -h for various options. For very reliable transfers on 47 | machines with a fast CPU, I would suggest `-N 30000 -r 1.5`, which sets a 48 | relatively high redundancy (+50% of redundant data) and big redundancy blocks 49 | for a better resistance to loss bursts (N=30000). 50 | 51 | Note that a static ARP entry for `RECEIVER_IP` must be provided for `hairgaps` 52 | to work properly. One way to achieve this is as follows: 53 | 54 | ```sh 55 | # arp -s 10.0.0.1 aa:bb:cc:00:01:02 56 | ``` 57 | 58 | ## Compiling 59 | 60 | Compilation has only been tested on linux. 61 | 62 | *Note:* you should either `git clone --recursive` or 63 | `git submodules --init --update` to retrieve submodules. 64 | 65 | ```sh 66 | $ make 67 | ``` 68 | 69 | Installing 70 | ---------- 71 | 72 | ```sh 73 | # make install 74 | ``` 75 | 76 | Testing 77 | ------- 78 | 79 | Testing properly will require you to set the aforementioned `sysctl`s. Note 80 | that the tests can currently deadlock, try to restart them. This is another 81 | FIXME. More tests are on their way. 82 | 83 | ```sh 84 | $ make test 85 | ``` 86 | 87 | Protocol 88 | -------- 89 | 90 | Hairgap implements its own, very simple protocol. Its documentation is on its 91 | way too. 92 | 93 | Hacking 94 | ------- 95 | 96 | See `hairgap.h` first, it will give you pointers. 97 | -------------------------------------------------------------------------------- /src/hairgapr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "common.h" 25 | #include "hairgap.h" 26 | 27 | #define USAGE\ 28 | "Usage: hairgapr [-h] [-m MEM_LIMIT] [-p PORT] [-t TIMEOUT] bind_ip\n"\ 29 | "\n"\ 30 | "Hairgap receiver, to reliably receive data over a unidirectional "\ 31 | "network.\n"\ 32 | "\n"\ 33 | "Options:\n"\ 34 | " -h Prints this help and exits.\n"\ 35 | " -m MEM_LIMIT Rough memory limit in megabytes.\n"\ 36 | " -p PORT Bind port port.\n"\ 37 | " -t TIMEOUT Set timeout in seconds. If no packets are received \n"\ 38 | " for seconds, the transfer is interrupted.\n" 39 | 40 | int 41 | main(int argc, char* argv[]) 42 | { 43 | struct hgap_config config; 44 | hgap_defaults(&config); 45 | 46 | int c = 0; 47 | while ((c = getopt(argc, argv, "p:t:m:h")) != -1) { 48 | switch (c) { 49 | case 'p': 50 | // FIXME: atoi 51 | config.port = atoi(optarg); 52 | break; 53 | case 't': 54 | config.timeout = atoi(optarg); 55 | break; 56 | case 'm': 57 | config.mem_limit = atoll(optarg) * 1024 * 1024; 58 | break; 59 | case 'h': 60 | fputs(USAGE, stdout); 61 | exit(EXIT_SUCCESS); 62 | default: 63 | ERROR("Unknown option %c\n", c); 64 | ERROR(USAGE); 65 | exit(EXIT_FAILURE); 66 | } 67 | } 68 | 69 | if (argc < 2 || argc < optind) { 70 | fprintf(stderr, USAGE); 71 | exit(EXIT_FAILURE); 72 | } 73 | 74 | config.addr = argv[optind]; 75 | 76 | INFO("starting with:\n addr: %s port: %hd\n timeout: %"PRIu64"\n", 77 | config.addr, config.port, config.timeout); 78 | 79 | hgap_config_dump(&config, stderr); 80 | int ret = hgap_receive(&config); 81 | 82 | if (ret != HGAP_SUCCESS) { 83 | HGAP_PERROR(ret, "Hairgapr failed"); 84 | return ret; 85 | } 86 | 87 | return ret; 88 | } 89 | -------------------------------------------------------------------------------- /src/lib/common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef HGAP_COMMON_H 20 | #define HGAP_COMMON_H 21 | 22 | #include 23 | 24 | #include "hairgap.h" 25 | 26 | //#define DEBUG 27 | 28 | // To avoid unused compiler warning 29 | #define FAKE_USE(x) ((void) (x)) 30 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 31 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 32 | 33 | #ifdef DEBUG 34 | #define DBG(...) fprintf(stderr, "[DEBUG] - " __VA_ARGS__) 35 | #else 36 | #define DBG(...) 37 | #endif 38 | 39 | #define INFO(...) fprintf(stderr, "[INFO] - " __VA_ARGS__) 40 | #define WARN(...) fprintf(stderr, "[WARN] - " __VA_ARGS__) 41 | #define PWARN(s) perror("[WARN] - " s) 42 | 43 | #define ERROR(...) do {\ 44 | fprintf(stderr, "[ERROR] (at " __FILE__ ":%d) - ", __LINE__);\ 45 | fprintf(stderr, __VA_ARGS__);\ 46 | } while (0) 47 | 48 | #define HGAP_PERROR(err, ...) do {\ 49 | fprintf(stderr, "[ERROR] (at " __FILE__ ":%d) - ", __LINE__);\ 50 | fprintf(stderr, __VA_ARGS__);\ 51 | fprintf(stderr, ": ");\ 52 | fprintf(stderr, hgap_err_str(err));\ 53 | fprintf(stderr, "\n");\ 54 | } while (0) 55 | 56 | #define CHK(x) do {\ 57 | if (!(x)) {\ 58 | ERROR("%s", #x);\ 59 | exit(EXIT_FAILURE);\ 60 | }\ 61 | } while (0) 62 | #define CHK_MSG(x, ...) do {\ 63 | if (!(x)) {\ 64 | ERROR(__VA_ARGS__);\ 65 | exit(EXIT_FAILURE);\ 66 | }\ 67 | } while (0) 68 | #define CHK_PERROR(x) do if (!(x)) { perror(#x); exit(EXIT_FAILURE); } while (0) 69 | 70 | /** 71 | * Helper struct to handle sized bufs 72 | */ 73 | struct sized_buf { 74 | size_t size; 75 | void *data; 76 | /* Optional */ 77 | char content[0]; 78 | }; 79 | 80 | #define SBUF_NULL { .data=NULL, .size=0 } 81 | #define SBUF_RESET(x) do { (x).data = NULL; (x).size = 0; } while (0) 82 | 83 | /** 84 | * Select the best error to report among multiple ones 85 | */ 86 | #define HGAP_SELECT_ERROR(e1, e2) \ 87 | ((e1) == HGAP_SUCCESS ? \ 88 | (e2) : ((e2) == HGAP_SUCCESS ? \ 89 | (e1) : MIN((e1), (e2)))) 90 | 91 | /** 92 | * Malloc that exits on failure. 93 | */ 94 | void *xmalloc(size_t size); 95 | 96 | void dbg_hexdump(void *, size_t); 97 | 98 | #endif // HGAP_COMMON_H 99 | -------------------------------------------------------------------------------- /src/lib/error.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "hairgap.h" 20 | #include "proto.h" 21 | 22 | #define STR_HELPER(x) #x 23 | #define STR(x) STR_HELPER(x) 24 | 25 | const char * 26 | hgap_err_str(int err) 27 | { 28 | switch (err) { 29 | case HGAP_SUCCESS: 30 | return "Success"; 31 | case HGAP_EOT: 32 | return "End of transfer"; 33 | case HGAP_ERR_NO_CONFIG: 34 | return "No configuration passed (logic error)"; 35 | case HGAP_ERR_MTU_TOO_SMALL: 36 | return "MTU too small (should be more than "STR(HGAP_HEADER_LEN)")"; 37 | case HGAP_ERR_MTU_TOO_BIG: 38 | return "MTU too big (> "STR(HGAP_MAX_PKT_SIZE)")"; 39 | case HGAP_ERR_INVALID_ADDR: 40 | return "Invalid address or host"; 41 | case HGAP_ERR_BAD_FD: 42 | return "Bad file descriptor"; 43 | case HGAP_ERR_BAD_IN_FD: 44 | return "Bad input file descriptor"; 45 | case HGAP_ERR_BAD_OUT_FD: 46 | return "Bad output file descriptor"; 47 | case HGAP_ERR_FILE_READ: 48 | return "Error while reading input file"; 49 | case HGAP_ERR_BAD_N_PKT: 50 | return "Bad number of packets per chunk (should be < " 51 | STR(HGAP_MAX_N_PKT)")"; 52 | case HGAP_ERR_BAD_REDUND: 53 | return "Bad redundancy, should be >= 1.0"; 54 | case HGAP_ERR_WIREHAIR_ERROR: 55 | return "Error correction engine (wirehair) error"; 56 | case HGAP_ERR_BUFFER_TOO_SMALL: 57 | return "Buffer too small"; 58 | case HGAP_ERR_INCOMPLETE_CHUNK: 59 | return "Chunk could not be reassembled (probably too many lost " 60 | "packets)"; 61 | case HGAP_ERR_BAD_CHUNK: 62 | return "Invalid chunk (probably too big)"; 63 | case HGAP_ERR_BAD_PKT: 64 | return "Invalid packet (probably too small)"; 65 | case HGAP_ERR_NETWORK: 66 | return "Unspecified network error"; 67 | case HGAP_ERR_TIMEOUT: 68 | return "Receive socket probably timed out"; 69 | case HGAP_ERR_IPC: 70 | return "Internal (IPC) error"; 71 | default: 72 | return "Unknown error"; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/lib/limiter.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "limiter.h" 20 | 21 | #include 22 | #include 23 | 24 | #include "common.h" 25 | #include "proto.h" 26 | 27 | #define HLIM_CHK_PERIOD 1000 28 | #define HLIM_SLEEP_PERIOD 100 // in microseconds 29 | 30 | struct hgap_limiter { 31 | double byterate; 32 | size_t n_pkt_sent; 33 | size_t n_bytes_sent; 34 | struct timeval since; 35 | 36 | size_t total_data_sent; 37 | }; 38 | 39 | static double 40 | time_diff(struct timeval *before , struct timeval *after) 41 | { 42 | double before_us = before->tv_sec + before->tv_usec / 1000000.; 43 | double after_us = after->tv_sec + after->tv_usec / 1000000.; 44 | 45 | return after_us - before_us; 46 | } 47 | 48 | static void 49 | hgap_limiter_reset_rate(struct hgap_limiter *hlim) 50 | { 51 | gettimeofday(&hlim->since , NULL); 52 | hlim->n_pkt_sent = 0; 53 | hlim->n_bytes_sent = 0; 54 | } 55 | 56 | static double 57 | hgap_limiter_get_current_rate(struct hgap_limiter *hlim) 58 | { 59 | struct timeval now; 60 | gettimeofday(&now , NULL); 61 | double diff = time_diff(&hlim->since, &now); 62 | double rate; 63 | if (diff == 0) { 64 | rate = 0; 65 | } else { 66 | rate = hlim->n_bytes_sent / diff; 67 | } 68 | return rate; 69 | } 70 | 71 | struct hgap_limiter * 72 | hgap_limiter_new(double byterate) 73 | { 74 | struct hgap_limiter *hlim = xmalloc(sizeof(struct hgap_limiter)); 75 | hlim->byterate = byterate; 76 | hgap_limiter_reset_rate(hlim); 77 | hlim->total_data_sent = 0; 78 | return hlim; 79 | } 80 | 81 | int 82 | hgap_limiter_limit(struct hgap_limiter *hlim, size_t len) 83 | { 84 | hlim->total_data_sent += len; 85 | hlim->n_pkt_sent++; 86 | hlim->n_bytes_sent += len; 87 | if (hlim->byterate && hlim->n_pkt_sent > HLIM_CHK_PERIOD) { 88 | while (hgap_limiter_get_current_rate(hlim) > hlim->byterate) { 89 | usleep(HLIM_SLEEP_PERIOD); 90 | } 91 | hgap_limiter_reset_rate(hlim); 92 | } 93 | 94 | return HGAP_SUCCESS; 95 | } 96 | 97 | void 98 | hgap_limiter_free(struct hgap_limiter *hlim) 99 | { 100 | DBG("Sent %zu bytes.\n", hlim->total_data_sent); 101 | free(hlim); 102 | } 103 | 104 | // TODO Token bucket filter 105 | -------------------------------------------------------------------------------- /GNUmakefile: -------------------------------------------------------------------------------- 1 | SRCDIR = src 2 | TESTSRCDIR = $(SRCDIR)/test 3 | LIBSRCDIR = $(SRCDIR)/lib 4 | BUILDDIR = build 5 | 6 | LIBH = $(wildcard $(LIBSRCDIR)/*.h) 7 | LIBSRC = $(wildcard $(LIBSRCDIR)/*.c) 8 | MAINSRC := $(wildcard $(SRCDIR)/*.c) 9 | LIBOBJ := $(LIBSRC:$(LIBSRCDIR)/%.c=$(BUILDDIR)/%.o) 10 | MAINOBJ := $(MAINSRC:$(SRCDIR)/%.c=$(BUILDDIR)/%.o) 11 | LIBNAME = $(BUILDDIR)/libhairgap.a 12 | 13 | WIREHAIR = ./wirehair 14 | LIBWIREHAIR = $(WIREHAIR)/bin/libwirehair.a 15 | LIBWIREHAIR_DEBUG = $(WIREHAIR)/bin/libwirehair_debug.a 16 | 17 | CC = clang 18 | OPTFLAGS = -Ofast -D_FORTIFY_SOURCE=1 19 | DBGFLAGS = -g -O0 -DDEBUG 20 | IFLAGS = -I$(WIREHAIR)/include -I$(LIBSRCDIR) 21 | GPRFLAGS = -pg -g 22 | CFLAGS = -Wall -Wextra -fPIE -fstack-protector-strong \ 23 | -Wno-format-security \ 24 | -Werror \ 25 | $(IFLAGS) 26 | LDFLAGS = $(IFLAGS) -L$(WIREHAIR)/bin -lpthread -lstdc++ -Wl,-z,now -Wl,-z,relro 27 | 28 | INSTALLDIR=/usr/local 29 | BIN=${INSTALLDIR}/bin 30 | 31 | all: release 32 | 33 | test: debug hgap_test channel_test 34 | ./hgap_test 35 | ./channel_test 36 | ./test/integration_tests.sh 37 | 38 | doc: Doxyfile ${LIBSRC} ${MAINSRC} ${LIBH} 39 | doxygen 40 | 41 | 42 | # Installation 43 | 44 | install: release 45 | @echo "Installing to '${INSTALLDIR}'..." 46 | install -d $(BIN) 47 | install -m755 hairgaps hairgapr $(BIN) 48 | @echo "Done." 49 | 50 | uninstall: 51 | @echo "Removing from '${INSTALLDIR}'..." 52 | rm ${BIN}/hairgaps ${BIN}/hairgapr 53 | @echo "Done." 54 | 55 | 56 | # Targets 57 | 58 | dirs: build 59 | 60 | build: 61 | mkdir -p build 62 | 63 | debug: CFLAGS += $(DBGFLAGS) 64 | debug: LIBWIREHAIR_CHOSEN := $(LIBWIREHAIR_DEBUG) 65 | debug: dirs $(LIBWIREHAIR_DEBUG) hairgaps hairgapr 66 | 67 | profile: CFLAGS += $(GPRFLAGS) 68 | profile: LDFLAGS += -pg 69 | profile: dirs release 70 | 71 | release: CFLAGS += -D_FORTIFY_SOURCE=1 $(OPTFLAGS) 72 | release: LIBWIREHAIR_CHOSEN := $(LIBWIREHAIR) 73 | release: dirs $(LIBWIREHAIR) hairgaps hairgapr 74 | 75 | clean: 76 | -rm -r build 77 | -cd wirehair && make clean 78 | 79 | dist-clean: clean 80 | -rm -r hairgaps hairgapr channel_test hgap_test doc/* 81 | 82 | 83 | # Compilation 84 | 85 | $(LIBNAME): $(LIBOBJ) 86 | ar rcs $(LIBNAME) $^ 87 | 88 | hairgapr: $(BUILDDIR)/hairgapr.o $(LIBNAME) 89 | $(CC) $^ -o $@ $(LDFLAGS) $(LIBWIREHAIR_CHOSEN) 90 | 91 | hairgaps: $(BUILDDIR)/hairgaps.o $(LIBNAME) 92 | $(CC) $^ -o $@ $(LDFLAGS) $(LIBWIREHAIR_CHOSEN) 93 | 94 | $(BUILDDIR)/hairgapr.o: $(LIBSRCDIR)/proto.h 95 | $(BUILDDIR)/hairgaps.o: $(LIBSRCDIR)/proto.h 96 | $(BUILDDIR)/hairproto.o: $(LIBSRCDIR)/proto.h 97 | $(BUILDDIR)/bufpool.o: $(LIBSRCDIR)/bufpool.h 98 | 99 | $(LIBOBJ): $(BUILDDIR)/%.o:$(LIBSRCDIR)/%.c 100 | $(CC) -c $< -o $@ $(CFLAGS) 101 | 102 | $(MAINOBJ): $(BUILDDIR)/%.o:$(SRCDIR)/%.c 103 | $(CC) -c $< -o $@ $(CFLAGS) 104 | 105 | hgap_test: $(TESTSRCDIR)/hgap_test.c $(LIBNAME) 106 | $(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS) $(LIBWIREHAIR_DEBUG) 107 | 108 | channel_test: CFLAGS += $(OPTFLAGS) 109 | channel_test: $(TESTSRCDIR)/channel_test.c $(LIBSRCDIR)/channel.c \ 110 | $(LIBSRCDIR)/common.c 111 | $(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS) 112 | 113 | $(LIBWIREHAIR): 114 | cd $(WIREHAIR) && make clean && make 115 | 116 | $(LIBWIREHAIR_DEBUG): 117 | cd $(WIREHAIR) && make clean && make debug 118 | -------------------------------------------------------------------------------- /src/test/channel_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "channel.h" 28 | #include "common.h" 29 | 30 | uint32_t send_amount = 0; 31 | struct timeval t1, t2; 32 | 33 | void chan_producer(struct channel *chan) { 34 | uint32_t i = 0; 35 | void *data = NULL; 36 | assert(channel_elt_size(chan) >= sizeof(uint32_t)); 37 | 38 | gettimeofday(&t1, NULL); 39 | for (i = 0; i < send_amount; i++) { 40 | // Reserve data 41 | data = channel_reserve(chan); 42 | assert(data != NULL); 43 | // Fill with counter 44 | *(uint32_t *)data = i; 45 | // Send data 46 | assert(channel_send_reserved(chan, data) == 1); 47 | } 48 | } 49 | 50 | void chan_consumer(struct channel *chan) { 51 | uint32_t cur = 0; 52 | uint32_t next = 0; 53 | void *data = NULL; 54 | 55 | assert(channel_elt_size(chan) >= sizeof(uint32_t)); 56 | while (1) { 57 | data = channel_peek(chan); 58 | if (data == NULL) { 59 | break; 60 | } 61 | cur = *(uint32_t *)data; 62 | assert(cur == next); 63 | if (channel_ack(chan, data) == 0) { 64 | break; 65 | } 66 | next = cur + 1; 67 | 68 | if (next == send_amount) { 69 | break; 70 | } 71 | } 72 | gettimeofday(&t2, NULL); 73 | assert(next == send_amount); 74 | } 75 | 76 | void test_simple_concurrent(size_t elt_size, size_t capacity) { 77 | struct channel *chan = channel_new(elt_size, capacity); 78 | pthread_t send_thread; 79 | CHK_PERROR(pthread_create(&send_thread, NULL, 80 | (void*(*)(void*)) chan_producer, chan) == 0); 81 | chan_consumer(chan); 82 | pthread_join(send_thread, NULL); 83 | channel_free(chan); 84 | 85 | double t1d = ((double) t1.tv_sec) + ((double) t1.tv_usec) / 1000000; 86 | double t2d = ((double) t2.tv_sec) + ((double) t2.tv_usec) / 1000000; 87 | double tdiff = t2d - t1d; 88 | double throughput = ((double) send_amount) / tdiff; 89 | INFO("Throughput: %lf elt/s\n", throughput); 90 | } 91 | 92 | int 93 | main() { 94 | INFO("Test 1\n"); 95 | send_amount = 1 * 1024 * 1024; 96 | test_simple_concurrent(sizeof(uint32_t), 1024); 97 | INFO("Test 2\n"); 98 | test_simple_concurrent(1500, 1024); 99 | INFO("Test 3\n"); 100 | test_simple_concurrent(sizeof(uint32_t), 32); 101 | INFO("Test 4\n"); 102 | send_amount = 1 * 128 * 1024; 103 | test_simple_concurrent(sizeof(uint32_t), 2); 104 | INFO("Test 5\n"); 105 | test_simple_concurrent(1500, 2); 106 | //test_slow_send_recv(); 107 | return EXIT_SUCCESS; 108 | } 109 | -------------------------------------------------------------------------------- /src/lib/proto.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | 20 | #include "proto.h" 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | #include "common.h" 31 | 32 | int 33 | hgap_pkt_parse(struct hgap_pkt *pkt, const void *raw_pkt, size_t size) 34 | { 35 | if (size < HGAP_HEADER_LEN) { 36 | return HGAP_ERR_BAD_PKT; 37 | } 38 | 39 | const struct hgap_header *net_hdr = raw_pkt; 40 | 41 | pkt->hdr.chunk_num = be64toh(net_hdr->chunk_num); 42 | pkt->hdr.chunk_size = be64toh(net_hdr->chunk_size); 43 | pkt->hdr.data_id = be32toh(net_hdr->data_id); 44 | pkt->hdr.data_size = be32toh(net_hdr->data_size); 45 | pkt->data = (char *) raw_pkt + HGAP_HEADER_LEN; 46 | 47 | if (size < pkt->hdr.data_size + HGAP_HEADER_LEN) { 48 | return HGAP_ERR_BAD_PKT; 49 | } 50 | 51 | return HGAP_SUCCESS; 52 | } 53 | 54 | 55 | enum hgap_pkt_t 56 | hgap_pkt_type(void *pkt, size_t len) 57 | { 58 | struct hgap_header *net_hdr = pkt; 59 | 60 | if (len < HGAP_HEADER_LEN) { 61 | return HGAP_PKT_UNKNOWN; 62 | } 63 | 64 | uint64_t type = be64toh(net_hdr->chunk_num); 65 | 66 | switch (type) { 67 | case HGAP_BEGIN_BEACON: 68 | return HGAP_PKT_BEGIN; 69 | case HGAP_NO_MORE_CHUNK: 70 | return HGAP_PKT_END; 71 | case HGAP_KEEP_ALIVE: 72 | return HGAP_PKT_KEEPALIVE; 73 | default: 74 | if (type >= HGAP_FIRST_RESERVED) { 75 | return HGAP_PKT_UNKNOWN; 76 | } 77 | 78 | return HGAP_PKT_DATA; 79 | } 80 | } 81 | 82 | void 83 | hgap_write_header(const struct hgap_header *hdr, void *buf) 84 | { 85 | struct hgap_header *net_hdr = buf; 86 | 87 | net_hdr->chunk_num = htobe64(hdr->chunk_num); 88 | net_hdr->chunk_size = htobe64(hdr->chunk_size); 89 | net_hdr->data_id = htobe32(hdr->data_id); 90 | net_hdr->data_size = htobe32(hdr->data_size); 91 | } 92 | 93 | void 94 | hgap_header_begin(struct hgap_header *hdr) 95 | { 96 | memset(hdr, 0, sizeof *hdr); 97 | hdr->chunk_num = HGAP_BEGIN_BEACON; 98 | } 99 | 100 | void 101 | hgap_header_end(struct hgap_header *hdr) 102 | { 103 | memset(hdr, 0, sizeof *hdr); 104 | hdr->chunk_num = HGAP_NO_MORE_CHUNK; 105 | } 106 | 107 | void 108 | hgap_header_keepalive(struct hgap_header *hdr) 109 | { 110 | memset(hdr, 0, sizeof *hdr); 111 | hdr->chunk_num = HGAP_KEEP_ALIVE; 112 | } 113 | 114 | void 115 | hgap_dump(struct hgap_pkt *pkt) 116 | { 117 | FAKE_USE(pkt); 118 | DBG("Header:\n" 119 | " chunk_num: %"PRIu64"\n" 120 | " chunk_size: %"PRIu64"\n" 121 | " data_id: %"PRIu32"\n" 122 | " data_size: %"PRIu32"\n" 123 | "Meta:\n" 124 | " total_size: %"PRIu64"\n", 125 | pkt->hdr.chunk_num, 126 | pkt->hdr.chunk_size, 127 | pkt->hdr.data_id, 128 | pkt->hdr.data_size, 129 | HGAP_HEADER_LEN + pkt->hdr.data_size); 130 | } 131 | -------------------------------------------------------------------------------- /src/lib/channel.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef HGAP_CHANNEL_H 20 | #define HGAP_CHANNEL_H 21 | 22 | #include 23 | 24 | /** 25 | * An abstraction over a single producer single consumer channel of data. 26 | * 27 | * Implemented as a fixed size ring buffer. Should be lock-free except when 28 | * blocking (empty/full queue). 29 | * 30 | * Please note that all the data is allocated once. 31 | */ 32 | struct channel; 33 | 34 | /** 35 | * Allocates the channel to transfer elements of size elt_size. channel_send 36 | * will block if capacity elements already are in the queue. 37 | * 38 | * Effectivley allocates elt_size * (capacity + 1) bytes. 39 | */ 40 | struct channel *channel_new(size_t elt_size, size_t capacity); 41 | 42 | /** 43 | * Frees resources associated with this channel. 44 | */ 45 | void channel_free(struct channel *chan); 46 | 47 | /** 48 | * Reserves an elt_size-long buffer on the channel and returns a pointer to it. 49 | * Multiple reservations will return the same pointer as long as 50 | * channel_send_reserved has not been called. 51 | * 52 | * If channel_send is called after a reservation, the data passed to it will 53 | * override the buffer returned by the reservation. 54 | */ 55 | void *channel_reserve(struct channel *chan); 56 | 57 | /** 58 | * Send the previously reserved data on the channel. 59 | * 60 | * @return 1 on success, 0 on failure or if data is not a valid reserved 61 | * buffer. 62 | */ 63 | int channel_send_reserved(struct channel *chan, void *data); 64 | 65 | /** 66 | * Send the data pointed by data of size elt_size (see channel_init). 67 | * 68 | * This function copies data in the channel. See channel_reserve and 69 | * channel_send_reserved for a more efficient approach. 70 | * 71 | * @return 1 on success, 0 on failure 72 | */ 73 | int channel_send(struct channel *chan, void *data); 74 | 75 | /** 76 | * Get the next element to be read on the channel. This is meant to retrieve 77 | * an element to use it before calling channel_ack that allows it to be written 78 | * again. 79 | */ 80 | void *channel_peek(struct channel *chan); 81 | 82 | /** 83 | * Signal an element gotten by channel_peek as read and ready to be recycled. 84 | * Further uses of data will result in undefined behaviour. 85 | */ 86 | int channel_ack(struct channel *chan, void *data); 87 | 88 | /** 89 | * Receive data of size elt_size (see channel_init) in the buffer pointed by 90 | * data. 91 | * 92 | * Note that this function copies an internal buffer to data. See channel_peek 93 | * and channel_ack for a more efficient approach. 94 | * 95 | * @return 1 on success, 0 on failure 96 | */ 97 | int channel_recv(struct channel *chan, void *data); 98 | 99 | /** 100 | * Poisons the channel so that any further send/receive will fail. 101 | */ 102 | void channel_poison(struct channel *chan); 103 | 104 | /** 105 | * Returns the size of the elements transfered on this channel 106 | */ 107 | size_t channel_elt_size(struct channel *chan); 108 | 109 | #endif // HGAP_CHANNEL_H 110 | -------------------------------------------------------------------------------- /src/lib/proto.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef HGAP_PROTO_H 20 | #define HGAP_PROTO_H 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | // Important note: the port looks like \m/(-_-)\m/ 29 | #define HGAP_PORT 11011 30 | 31 | #define HGAP_NO_MORE_CHUNK ((uint64_t) 0xffffffffffffffff) 32 | #define HGAP_BEGIN_BEACON ((uint64_t) 0xfffffffffffffffe) 33 | #define HGAP_KEEP_ALIVE ((uint64_t) 0xfffffffffffffffd) 34 | #define HGAP_FIRST_RESERVED ((uint64_t) 0xfffffffffffffff0) 35 | 36 | #define HGAP_HEADER_LEN sizeof(struct hgap_header) 37 | #define HGAP_CONTROL_LEN 0 38 | #define HGAP_SALVE_LEN 32 39 | #define HGAP_LITTLE_CHUNK_RETRIES 128 40 | #define HGAP_MIN_BUF (HGAP_HEADER_LEN + HGAP_CONTROL_LEN) 41 | 42 | // A bit more than standard UDP MTU (FIXME should be adjusted) 43 | #define HGAP_MAX_PKT_SIZE 1500 44 | // A bit more than real max possible size 45 | #define HGAP_MAX_CHUNK_SIZE (HGAP_MAX_PKT_SIZE * HGAP_MAX_N_PKT) 46 | #define HGAP_MAX_DATA_SIZE (HGAP_MAX_PKT_SIZE - HGAP_HEADER_LEN) 47 | 48 | // From wirehair doc 49 | #define HGAP_MAX_N_PKT 64000 50 | 51 | enum hgap_pkt_t { 52 | HGAP_PKT_UNKNOWN = 0, 53 | HGAP_PKT_BEGIN, 54 | HGAP_PKT_END, 55 | HGAP_PKT_KEEPALIVE, 56 | HGAP_PKT_DATA 57 | }; 58 | 59 | 60 | /* 61 | * Hairgap packet format 62 | */ 63 | struct hgap_header { 64 | /// Chunk of encoded data this packet is part of 65 | uint64_t chunk_num; 66 | /// Size of the current chunk 67 | uint64_t chunk_size; 68 | /// Id of the data in this packet 69 | uint32_t data_id; 70 | /// size of payload 71 | uint32_t data_size; 72 | }; 73 | 74 | struct hgap_pkt { 75 | struct hgap_header hdr; 76 | /// Ptr to data in _raw 77 | char *data; 78 | }; 79 | 80 | /** 81 | * Parse raw data to create an hgap_pkt. The raw_pkt buffer shall be at least 82 | * size bytes long and live longer than pkt, as pkt will keep a reference on 83 | * it. 84 | * 85 | * @return HGAP_SUCCESS on success, HGAP_ERR_BAD_PKT if the packet is 86 | * incoherent or too small 87 | */ 88 | int hgap_pkt_parse(struct hgap_pkt *pkt, const void *raw_pkt, size_t size); 89 | 90 | /** 91 | * Returns the type of this packet. 92 | */ 93 | enum hgap_pkt_t hgap_pkt_type(void *pkt, size_t len); 94 | 95 | /** 96 | * Write the header into buf. buf must be at least HGAP_HEADER_LEN long. The 97 | * payload can be written after HGAP_HEADER_LEN bytes. 98 | */ 99 | void hgap_write_header(const struct hgap_header *hdr, void *buf); 100 | 101 | /** 102 | * Special header types 103 | */ 104 | // Announce begining of transfer 105 | void hgap_header_begin(struct hgap_header *hdr); 106 | 107 | // Announce end of transfer 108 | void hgap_header_end(struct hgap_header *hdr); 109 | 110 | // Keepalive 111 | void hgap_header_keepalive(struct hgap_header *hdr); 112 | 113 | void hgap_dump(struct hgap_pkt *pkt); 114 | #ifdef DEBUG 115 | #define HGAP_DUMP(pkt) hgap_dump(pkt); 116 | #else 117 | #define HGAP_DUMP(pkt) 118 | #endif 119 | 120 | #endif // HGAP_PROTO_H 121 | -------------------------------------------------------------------------------- /src/hairgaps.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | /* 20 | * Read block on stdin 21 | * Write block on network 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include "common.h" 29 | #include "hairgap.h" 30 | 31 | #define USAGE\ 32 | "Usage: hairgaps [Options] dest_ip\n"\ 33 | "\n"\ 34 | "Hairgap sender, to reliably send data over a unidirectional network.\n"\ 35 | "\n"\ 36 | "Options:\n"\ 37 | " -h Prints this help and exits.\n"\ 38 | " -p PORT Destination port.\n"\ 39 | " -r REDUND Redundancy ratio (1.2 will send 1.2 times more data\n"\ 40 | " than the original).\n"\ 41 | " -b RATE Rate limit in MB/s\n"\ 42 | " -N NUM Number of UDP packets in an error correction chunk.\n"\ 43 | " Default (and ideal) is 1000, increasing it will\n"\ 44 | " make the transfer more robust to big loss bursts,\n"\ 45 | " but possibly slower. 2 <= NUM <= 64000.\n"\ 46 | " -M MTU Size in bytes of the UDP payloads to send.\n"\ 47 | " -k KEEPALIVE Keepalive period in ms. Default is 500ms. 0\n"\ 48 | " disables keepalives.\n" 49 | 50 | //" -m MEM_LIMIT Rough memory limit in megabytes.\n" 51 | 52 | 53 | int 54 | main(int argc, char *argv[]) 55 | { 56 | struct hgap_config config; 57 | hgap_defaults(&config); 58 | 59 | int c = 0; 60 | // TODO: arg control, no atof, etc... 61 | while ((c = getopt(argc, argv, "p:b:r:N:M:k:h")) != -1) { 62 | switch (c) { 63 | case 'p': 64 | config.port = atoi(optarg); 65 | break; 66 | case 'b': 67 | config.byterate = atof(optarg) * 1024 * 1024; 68 | break; 69 | case 'r': 70 | config.redund = atof(optarg); 71 | break; 72 | case 'N': 73 | config.n_pkt = atoll(optarg); 74 | break; 75 | case 'M': 76 | config.pkt_size = atol(optarg); 77 | break; 78 | /* 79 | case 'm': 80 | config.mem_limit = atoll(optarg) * 1024 * 1024; 81 | break; 82 | */ 83 | case 'k': 84 | config.keepalive = atoi(optarg); 85 | break; 86 | case 'h': 87 | fputs(USAGE, stdout); 88 | exit(EXIT_SUCCESS); 89 | default: 90 | ERROR("Unknown option %c\n", c); 91 | ERROR(USAGE); 92 | exit(EXIT_FAILURE); 93 | } 94 | } 95 | 96 | if (argc < 2 || argc < optind) { 97 | fprintf(stderr, USAGE); 98 | exit(EXIT_FAILURE); 99 | } 100 | 101 | config.addr = argv[optind]; 102 | 103 | INFO("starting with:\n" 104 | " addr: %s\tport: %hd\n" 105 | " redundancy: x%.2lf\tratelimit: %.2lf MB/s\n" 106 | " N: %d\tMTU: %zu\n" 107 | " Memory limit: %zu MB\tkeepalive: %"PRIu64" ms\n", 108 | config.addr, config.port, config.redund, 109 | config.byterate/(1024*1024), config.n_pkt, config.pkt_size, 110 | config.mem_limit/(1024*1024), config.keepalive); 111 | 112 | hgap_config_dump(&config, stderr); 113 | int ret = hgap_send(&config); 114 | 115 | if (ret != HGAP_SUCCESS) { 116 | HGAP_PERROR(ret, "Hairgaps failed"); 117 | return ret; 118 | } 119 | 120 | return ret; 121 | } 122 | -------------------------------------------------------------------------------- /src/lib/sender.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "sender.h" 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "common.h" 31 | #include "limiter.h" 32 | #include "proto.h" 33 | 34 | struct hgap_sender { 35 | struct sockaddr_in dstaddr; 36 | int socket; 37 | 38 | uint32_t keepalive; 39 | 40 | struct hgap_limiter *hlim; 41 | pthread_t keepalive_thread; 42 | int cont; 43 | }; 44 | 45 | static void * 46 | keepalive_loop_thread(void *args) 47 | { 48 | struct hgap_sender *hs = args; 49 | uint32_t keepalive = hs->keepalive; 50 | int *cont = &hs->cont; 51 | struct hgap_header ka_hdr; 52 | char ka_pkt[HGAP_HEADER_LEN]; 53 | 54 | // Generate keepalive packet (header only) 55 | hgap_header_keepalive(&ka_hdr); 56 | hgap_write_header(&ka_hdr, ka_pkt); 57 | 58 | // Stops when the shared cont variable is set to 0 59 | while (*cont) { 60 | // Wait the appropriate time 61 | if (keepalive >= 1000) { 62 | sleep(keepalive / 1000); 63 | } else { 64 | usleep(keepalive * 1000); 65 | } 66 | 67 | // FIXME: CHK_PERROR should disappear 68 | // Send the keepalive 69 | CHK_PERROR(hgap_sender_send(hs, ka_pkt, HGAP_HEADER_LEN) >= 0); 70 | } 71 | 72 | return NULL; 73 | } 74 | 75 | struct hgap_sender * 76 | hgap_sender_new(char *host, short port, uint64_t byterate, uint32_t keepalive) 77 | { 78 | struct hgap_sender *hs = xmalloc(sizeof *hs); 79 | hs->hlim = hgap_limiter_new(byterate); 80 | 81 | // Open socket 82 | hs->socket = socket(AF_INET, SOCK_DGRAM, 0); 83 | if (hs->socket < 0) { 84 | DBG("Socket creation error"); 85 | goto err_sock; 86 | } 87 | 88 | // Create destination address 89 | memset(&hs->dstaddr, 0, sizeof(hs->dstaddr)); 90 | hs->dstaddr.sin_family = AF_INET; 91 | // TODO getaddrinfo 92 | if ((hs->dstaddr.sin_addr.s_addr = inet_addr(host)) == 0) { 93 | DBG("Invalid IP in hgap_sender\n"); 94 | goto err; 95 | } 96 | 97 | hs->dstaddr.sin_port = htons(port); 98 | hs->keepalive = keepalive; 99 | hs->cont = 0; 100 | 101 | // Start keepalive 102 | if (hs->keepalive) { 103 | // FIXME: ensure no weird race condition can be cause by sharing this 104 | // non-locked flag with the keepalive_thread 105 | hs->cont = 1; 106 | int ret = pthread_create(&hs->keepalive_thread, NULL, 107 | keepalive_loop_thread, hs); 108 | if (ret != 0) { 109 | DBG("Keepalive thread creation error\n"); 110 | goto err; 111 | } 112 | } 113 | 114 | return hs; 115 | 116 | // Error handling 117 | err: 118 | close(hs->socket); 119 | err_sock: 120 | free(hs); 121 | return NULL; 122 | } 123 | 124 | ssize_t 125 | hgap_sender_send(struct hgap_sender *hs, void *pkt, size_t size) 126 | { 127 | ssize_t ret = sendto(hs->socket, pkt, size, 0, 128 | (struct sockaddr *)&hs->dstaddr, sizeof(hs->dstaddr)); 129 | 130 | if (ret >= 0) { 131 | hgap_limiter_limit(hs->hlim, ret); 132 | } 133 | 134 | return ret; 135 | } 136 | 137 | ssize_t 138 | hgap_sender_control(struct hgap_sender *hs, void *pkt, size_t size) 139 | { 140 | int i = 0; 141 | ssize_t ret = 0; 142 | 143 | for (i = 0; i < HGAP_SALVE_LEN; i++) { 144 | ret = hgap_sender_send(hs, pkt, size); 145 | 146 | if (ret < 0) { 147 | return ret; 148 | } 149 | } 150 | 151 | return 0; 152 | } 153 | 154 | void 155 | hgap_sender_free(struct hgap_sender *hs) 156 | { 157 | if (hs->cont) { 158 | hs->cont = 0; 159 | pthread_join(hs->keepalive_thread, NULL); 160 | } 161 | hgap_limiter_free(hs->hlim); 162 | free(hs); 163 | } 164 | -------------------------------------------------------------------------------- /src/lib/config.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include "common.h" 31 | #include "hairgap.h" 32 | #include "proto.h" 33 | 34 | int 35 | hgap_defaults(struct hgap_config *config) 36 | { 37 | if (config == NULL) { 38 | return HGAP_ERR_NO_CONFIG; 39 | } 40 | 41 | // Zero fields by default 42 | memset(config, 0, sizeof *config); 43 | 44 | config->in = HGAP_DEF_IN_FILE; 45 | config->out = HGAP_DEF_OUT_FILE; 46 | config->n_pkt = HGAP_DEF_N_PKT; 47 | config->pkt_size = HGAP_DEF_PKT_SIZE; 48 | config->redund = HGAP_DEF_REDUND; 49 | config->addr = HGAP_DEF_ADDR; 50 | config->port = HGAP_DEF_PORT; 51 | config->byterate = HGAP_DEF_BYTERATE; 52 | config->keepalive = HGAP_DEF_KEEPALIVE; 53 | config->timeout = HGAP_DEF_TIMEOUT; 54 | config->mem_limit = HGAP_DEF_MEM_LIMIT; 55 | 56 | return HGAP_SUCCESS; 57 | } 58 | 59 | void 60 | hgap_config_dump(const struct hgap_config *config, FILE *out) 61 | { 62 | char *addr = config->addr != NULL ? config->addr : ""; 63 | 64 | fprintf(out, 65 | "Hairgap config:\n" 66 | " in: %p\n" 67 | " out: %p\n" 68 | " n_pkt: %"PRIu32"\n" 69 | " pkt_size: %zu\n" 70 | " redundancy: %lf\n" 71 | " receiver addr: %s\n" 72 | " receiver port: %hd\n" 73 | " byterate: %lf\n" 74 | " keepalive: %"PRIu64" ms\n" 75 | " timeout: %"PRIu64" us\n" 76 | " memory limit: %.3f MB\n", 77 | config->in, 78 | config->out, 79 | config->n_pkt, 80 | config->pkt_size, 81 | config->redund, 82 | addr, 83 | config->port, 84 | config->byterate, 85 | config->keepalive, 86 | config->timeout, 87 | config->mem_limit / (1024*1024.)); 88 | } 89 | 90 | static int 91 | check_addr(const char *addr) 92 | { 93 | struct addrinfo *res; 94 | if (addr == NULL) { 95 | WARN("NULL address\n"); 96 | return HGAP_ERR_INVALID_ADDR; 97 | } 98 | 99 | int addr_valid = getaddrinfo(addr, NULL, NULL, &res); 100 | if (addr_valid != 0) { 101 | WARN("Invalid network address: %s\n", gai_strerror(addr_valid)); 102 | return HGAP_ERR_INVALID_ADDR; 103 | } else { 104 | freeaddrinfo(res); 105 | } 106 | 107 | return HGAP_SUCCESS; 108 | } 109 | 110 | // FIXME 111 | static int 112 | check_file(FILE *file) 113 | { 114 | if (file == NULL || ferror(file) != 0) { 115 | return HGAP_ERR_BAD_FD; 116 | } 117 | 118 | return HGAP_SUCCESS; 119 | } 120 | 121 | int 122 | hgap_check_config_sender(const struct hgap_config *config) 123 | { 124 | if (config->pkt_size <= HGAP_HEADER_LEN) { 125 | WARN("MTU too small: %zu\n", config->pkt_size); 126 | return HGAP_ERR_MTU_TOO_SMALL; 127 | } 128 | 129 | if (config->pkt_size > HGAP_MAX_PKT_SIZE) { 130 | WARN("MTU too big: %zu\n", config->pkt_size); 131 | return HGAP_ERR_MTU_TOO_BIG; 132 | } 133 | 134 | int ret = check_addr(config->addr); 135 | if (ret != HGAP_SUCCESS) { 136 | return ret; 137 | } 138 | 139 | if (check_file(config->in) != HGAP_SUCCESS) { 140 | PWARN("Invalid input file"); 141 | return HGAP_ERR_BAD_IN_FD; 142 | } 143 | 144 | if (config->n_pkt < 1 || config->n_pkt > HGAP_MAX_N_PKT) { 145 | WARN("Number of pkt must be between 1 and %d\n", HGAP_MAX_N_PKT); 146 | return HGAP_ERR_BAD_N_PKT; 147 | } 148 | 149 | if (config->redund < 1.0) { 150 | WARN("Redundancy must be >= 1\n"); 151 | return HGAP_ERR_BAD_REDUND; 152 | } 153 | 154 | return HGAP_SUCCESS; 155 | } 156 | 157 | int 158 | hgap_check_config_receiver(const struct hgap_config *config) 159 | { 160 | if (check_file(config->out) != HGAP_SUCCESS) { 161 | PWARN("Invalid output file"); 162 | return HGAP_ERR_BAD_OUT_FD; 163 | } 164 | 165 | return check_addr(config->addr); 166 | } 167 | -------------------------------------------------------------------------------- /src/lib/hairgap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef HAIRGAP_H 20 | #define HAIRGAP_H 21 | 22 | /** 23 | * Main hairgap API. This is the high level API, for better control, see: 24 | * - encoding.h: core primitives of hairgap, good starting point. 25 | * - proto.h: some details on the protocol. 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include "proto.h" 33 | 34 | #define HGAP_DEF_IN_FILE stdin 35 | #define HGAP_DEF_OUT_FILE stdout 36 | #define HGAP_DEF_N_PKT 1000 37 | // FIXME: MAX_PDU - header len 38 | #define HGAP_DEF_PKT_SIZE 1400 39 | #define HGAP_DEF_REDUND 1.2 40 | #define HGAP_DEF_ADDR NULL 41 | #define HGAP_DEF_PORT 11011 42 | #define HGAP_DEF_BYTERATE 0 43 | #define HGAP_DEF_KEEPALIVE 500 44 | #define HGAP_DEF_TIMEOUT 1 * 1000 * 1000 45 | #define HGAP_DEF_MEM_LIMIT 100 * 1024 * 1024 46 | 47 | /** 48 | * in: a file object to read from when sending. 49 | * out: a file object to write to when receiving. 50 | * n_pkt: the number of packets in an error correction chunk 51 | * pkt_size size of a packet, hairgap protocol headers included (should 52 | * typically fit in an UDP MTU). 53 | * redund: the desired amount of redundancy (1.2 produces 200 redundant packets 54 | * for a 1000 packet long chunk). 55 | * addr: a string representing the dotted notation of the destination IP (e.g.: 56 | * "10.0.0.2") or a hostname; represents the binding address on the 57 | * receiver side, and the destination address on the sender side. 58 | * port: destination port (binding port on the receiver side, destination port 59 | * on the sender side). 60 | * byterate: the max amount for bytes/second to send 61 | * keepalive: the keepalive period, in ms. Send a keepalive every keepalive ms. 62 | * 0 disables it. Sender side only. 63 | * timeout: the timeout (in us) after which to consider a transfer interrupted 64 | * if no packets are received. 0 disables it (not recommended). Receiver 65 | * side only. 66 | * mem_limit: the approximate maximum amount of memory to use to buffer 67 | * incoming packets and chunk (_very_ approximate). 68 | **/ 69 | struct hgap_config { 70 | FILE *in; 71 | FILE *out; 72 | uint32_t n_pkt; 73 | size_t pkt_size; 74 | double redund; 75 | char *addr; 76 | short port; 77 | double byterate; 78 | uint64_t keepalive; 79 | uint64_t timeout; 80 | size_t mem_limit; 81 | 82 | // FIXME: sockaddr* rather than addr? 83 | }; 84 | 85 | // --------------------------------- Main API ---------------------------------- 86 | 87 | /** 88 | * Sets hgap_config to safe defaults. The addr field is NULL by default and 89 | * should be set by the user. 90 | * 91 | * @return 0 on success, != on failure (if hgap_config* is NULL) 92 | **/ 93 | int hgap_defaults(struct hgap_config *config); 94 | 95 | /** 96 | * Send data as specified by config (from in to addr:port). This will start 97 | * 3 additional pthreads (or 2 if you disabled keepalives). Returns once the 98 | * transfer is complete. 99 | * 100 | * @return HGAP_SUCCESS on success, HGAP_ERR_* on failure 101 | **/ 102 | int hgap_send(const struct hgap_config *config); 103 | 104 | /** 105 | * Similar to hgap_send but receives data from config->addr, config->port and 106 | * writes it to config->out. 107 | * 108 | * @return HGAP_SUCCESS on success, HGAP_ERR_* on failure 109 | */ 110 | int hgap_receive(const struct hgap_config *config); 111 | 112 | 113 | // ------------------------------- Config functions ---------------------------- 114 | 115 | /** 116 | * Print a debug string of hgap_config to out. 117 | **/ 118 | void hgap_config_dump(const struct hgap_config *config, FILE *out); 119 | 120 | /** 121 | * Returns HGAP_SUCCESS if the config is valid for sending data, HGAP_ERR_* 122 | * otherwise. 123 | */ 124 | int hgap_check_config_sender(const struct hgap_config *config); 125 | 126 | /** 127 | * Returns HGAP_SUCCESS if the config is valid for receiving data, HGAP_ERR_* 128 | * otherwise. 129 | */ 130 | int hgap_check_config_receiver(const struct hgap_config *config); 131 | 132 | // ---------------------------- Error functions -------------------------------- 133 | 134 | enum { 135 | HGAP_SUCCESS = 0, 136 | HGAP_EOT, 137 | HGAP_ERR_NO_CONFIG, 138 | HGAP_ERR_MTU_TOO_SMALL, 139 | HGAP_ERR_MTU_TOO_BIG, 140 | HGAP_ERR_INVALID_ADDR, 141 | HGAP_ERR_BAD_FD, 142 | HGAP_ERR_BAD_IN_FD, 143 | HGAP_ERR_BAD_OUT_FD, 144 | HGAP_ERR_FILE_READ, 145 | HGAP_ERR_BAD_N_PKT, 146 | HGAP_ERR_BAD_REDUND, 147 | HGAP_ERR_WIREHAIR_ERROR, 148 | HGAP_ERR_BUFFER_TOO_SMALL, 149 | HGAP_ERR_INCOMPLETE_CHUNK, 150 | HGAP_ERR_BAD_CHUNK, 151 | HGAP_ERR_BAD_PKT, 152 | HGAP_ERR_TIMEOUT, 153 | HGAP_ERR_NETWORK, 154 | HGAP_ERR_IPC, 155 | HGAP_ERR_INTERNAL, 156 | }; 157 | 158 | /** 159 | * Returns a static string describing an HGAP_ERR_*. 160 | */ 161 | const char *hgap_err_str(int err); 162 | 163 | #endif // HAIRGAP_H 164 | -------------------------------------------------------------------------------- /src/test/hgap_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "common.h" 26 | #include "hairgap.h" 27 | #include "proto.h" 28 | #include "string.h" 29 | 30 | void 31 | test_check_config_sender() { 32 | struct hgap_config config; 33 | hgap_defaults(&config); 34 | hgap_config_dump(&config, stdout); 35 | 36 | assert(hgap_check_config_sender(&config) == HGAP_ERR_INVALID_ADDR); 37 | config.addr = "impossibru !"; 38 | assert(hgap_check_config_sender(&config) == HGAP_ERR_INVALID_ADDR); 39 | config.addr = "localhost"; 40 | assert(hgap_check_config_sender(&config) == HGAP_SUCCESS); 41 | config.addr = "127.0.0.1"; 42 | assert(hgap_check_config_sender(&config) == HGAP_SUCCESS); 43 | 44 | config.pkt_size = 1; 45 | assert(hgap_check_config_sender(&config) == HGAP_ERR_MTU_TOO_SMALL); 46 | config.pkt_size = HGAP_MAX_PKT_SIZE + 1; 47 | assert(hgap_check_config_sender(&config) == HGAP_ERR_MTU_TOO_BIG); 48 | config.pkt_size = HGAP_DEF_PKT_SIZE; 49 | 50 | config.in = NULL; 51 | assert(hgap_check_config_sender(&config) == HGAP_ERR_BAD_IN_FD); 52 | config.in = HGAP_DEF_IN_FILE; 53 | 54 | config.n_pkt = 0; 55 | assert(hgap_check_config_sender(&config) == HGAP_ERR_BAD_N_PKT); 56 | config.n_pkt = 0; 57 | assert(hgap_check_config_sender(&config) == HGAP_ERR_BAD_N_PKT); 58 | config.n_pkt = HGAP_DEF_N_PKT; 59 | 60 | config.redund = 0.5; 61 | assert(hgap_check_config_sender(&config) == HGAP_ERR_BAD_REDUND); 62 | config.redund = HGAP_DEF_REDUND; 63 | } 64 | 65 | void 66 | test_check_config_receiver() { 67 | // Code duplication is intentional for now 68 | struct hgap_config config; 69 | hgap_defaults(&config); 70 | 71 | assert(hgap_check_config_receiver(&config) == HGAP_ERR_INVALID_ADDR); 72 | config.addr = "impossibru !"; 73 | assert(hgap_check_config_receiver(&config) == HGAP_ERR_INVALID_ADDR); 74 | config.addr = "localhost"; 75 | assert(hgap_check_config_receiver(&config) == HGAP_SUCCESS); 76 | config.addr = "127.0.0.1"; 77 | assert(hgap_check_config_receiver(&config) == HGAP_SUCCESS); 78 | 79 | config.out = NULL; 80 | assert(hgap_check_config_receiver(&config) == HGAP_ERR_BAD_OUT_FD); 81 | config.out = HGAP_DEF_OUT_FILE; 82 | } 83 | 84 | void 85 | test_check_send_receive(struct hgap_config *config, size_t tr_size) { 86 | INFO("Send/Receive test\n"); 87 | 88 | struct timeval t1, t2; 89 | gettimeofday(&t1, NULL); 90 | 91 | size_t in_buf_sz = tr_size; 92 | char *in_buf = xmalloc(in_buf_sz); 93 | 94 | size_t out_buf_sz = 0; 95 | char *out_buf = NULL; 96 | 97 | memset(in_buf, 0xc, in_buf_sz); 98 | for (size_t i = 0, j = 0; i < in_buf_sz; 99 | i += (config->pkt_size - HGAP_HEADER_LEN) * config->n_pkt, j++) { 100 | in_buf[i] = (char) j; 101 | } 102 | 103 | config->in = fmemopen(in_buf, in_buf_sz, "r"); 104 | assert(config->in); 105 | config->out = open_memstream(&out_buf, &out_buf_sz); 106 | assert(config->out); 107 | 108 | int send_result = HGAP_ERR_INTERNAL; 109 | int receive_result = HGAP_ERR_INTERNAL; 110 | 111 | pthread_t receive_thread; 112 | CHK_PERROR(pthread_create(&receive_thread, NULL, 113 | (void*(*)(void*)) hgap_receive, config) == 0); 114 | usleep(100); 115 | send_result = hgap_send(config); 116 | pthread_join(receive_thread, (void **)&receive_result); 117 | 118 | gettimeofday(&t2, NULL); 119 | double t1d = ((double) t1.tv_sec) + ((double) t1.tv_usec) / 1000000; 120 | double t2d = ((double) t2.tv_sec) + ((double) t2.tv_usec) / 1000000; 121 | double tdiff = t2d - t1d; 122 | double throughput = ((double) out_buf_sz) / (1024*1024*tdiff); 123 | INFO("Throughput: %lf MB/s\n", throughput); 124 | 125 | if (send_result != HGAP_SUCCESS) { 126 | HGAP_PERROR(send_result, "Hgap receive failed"); 127 | exit(-1); 128 | } 129 | 130 | if (receive_result != HGAP_SUCCESS) { 131 | HGAP_PERROR(receive_result, "Hgap receive failed"); 132 | exit(-1); 133 | } 134 | 135 | fclose(config->in); 136 | fclose(config->out); 137 | 138 | DBG("out_buf_sz(%zu), in_buf_sz(%zu)\n", out_buf_sz, in_buf_sz); 139 | if (in_buf_sz != out_buf_sz) { 140 | ERROR("out_buf_sz(%zu) != in_buf_sz(%zu)\n", out_buf_sz, in_buf_sz); 141 | exit(-1); 142 | } 143 | 144 | if (memcmp(in_buf, out_buf, in_buf_sz) != 0) { 145 | DBG("Input:\n"); 146 | dbg_hexdump(in_buf, MIN(in_buf_sz, 0x100)); 147 | DBG("Output:\n"); 148 | dbg_hexdump(out_buf, MIN(in_buf_sz, 0x100)); 149 | ERROR("in_buf != out_buf\n"); 150 | exit(-1); 151 | } 152 | 153 | free(in_buf); 154 | free(out_buf); 155 | fprintf(stderr, "\n"); 156 | } 157 | 158 | int 159 | main() { 160 | test_check_config_sender(); 161 | test_check_config_receiver(); 162 | 163 | struct hgap_config config; 164 | hgap_defaults(&config); 165 | config.addr = "127.0.0.1"; 166 | config.port = 12345; 167 | config.mem_limit = 1 * 1024 * 1024; 168 | //config.byterate = 10 * 1024 * 1024; 169 | 170 | fprintf(stderr, "\n"); 171 | size_t tr_size; 172 | 173 | config.redund = 1.0; 174 | tr_size = 1*(config.pkt_size - HGAP_HEADER_LEN) - 500; 175 | test_check_send_receive(&config, tr_size); 176 | config.redund = 1.2; 177 | 178 | tr_size = 100; 179 | test_check_send_receive(&config, tr_size); 180 | 181 | tr_size = 1; 182 | test_check_send_receive(&config, tr_size); 183 | 184 | tr_size = 300L * 1024L * 1024L; 185 | test_check_send_receive(&config, tr_size); 186 | 187 | return EXIT_SUCCESS; 188 | } 189 | -------------------------------------------------------------------------------- /src/lib/channel.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "channel.h" 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #include "common.h" // Only uses xmalloc 26 | 27 | /** 28 | * r is the next slot to be read, w the next slot to be written. 29 | * _ _ r - - - w _ _ (size = 4) 30 | * - w _ _ _ _ _ r - (size = 3) 31 | * r w _ _ _ _ _ _ _ (size = 1) 32 | * _ rw_ _ _ _ _ _ _ (empty) 33 | * - w r - - - - - - (full) 34 | * 35 | * NOTE/FIXME: the actual implementation loses 1 entry of the channel, i.e when 36 | * the channel is full there is still 1 unused slot. This seems to allow less 37 | * lock contention but is a bit sad. Suggestions are welcome. 38 | * I guess this could be implemented lock-free, but perf is perfectly OK for the 39 | * current application. 40 | */ 41 | struct channel { 42 | size_t elt_size; 43 | size_t capacity; 44 | 45 | int poisoned; 46 | 47 | void *elts; 48 | size_t wr_idx; 49 | size_t rd_idx; 50 | 51 | pthread_mutex_t mutex; 52 | pthread_cond_t send_cond; 53 | pthread_cond_t recv_cond; 54 | }; 55 | 56 | #define POISON_CHECK(chan, ret_val) \ 57 | if ((chan)->poisoned) { return (ret_val); } 58 | 59 | #define channel_next(chan, idx) \ 60 | (((idx) + 1) % (chan)->capacity) 61 | 62 | #define channel_get(chan, idx) \ 63 | ((chan)->elts + (((idx) % (chan)->capacity) * (chan)->elt_size)) 64 | 65 | static int 66 | channel_is_full(struct channel *chan) 67 | { 68 | return (chan->rd_idx == channel_next(chan, chan->wr_idx) && 69 | !chan->poisoned); 70 | } 71 | 72 | static int 73 | channel_is_empty(struct channel *chan) 74 | { 75 | return (chan->rd_idx == chan->wr_idx && !chan->poisoned); 76 | } 77 | 78 | static void 79 | _channel_wait_lock(struct channel *chan, int (*test)(struct channel *), 80 | pthread_cond_t *cond) 81 | { 82 | // First test is without lock: there is only one producer, so if the channel 83 | // isn't full at the moment of this call, there is no race condition 84 | // possible. 85 | if (test(chan)) { 86 | // Take the mutex, test and wait 87 | pthread_mutex_lock(&chan->mutex); 88 | // Re-test, as it could no longer be full between previous test and the 89 | // lock acquisition 90 | while (test(chan)) { 91 | pthread_cond_wait(cond, &chan->mutex); 92 | } 93 | 94 | pthread_mutex_unlock(&chan->mutex); 95 | } 96 | } 97 | 98 | struct channel * 99 | channel_new(size_t elt_size, size_t capacity) 100 | { 101 | struct channel *chan = xmalloc(sizeof(struct channel)); 102 | if (chan == NULL) { 103 | return NULL; 104 | } 105 | chan->elt_size = elt_size; 106 | // + 1 to include the empty slot 107 | chan->capacity = capacity + 1; 108 | 109 | chan->poisoned = 0; 110 | 111 | chan->elts = xmalloc(elt_size * chan->capacity); 112 | chan->rd_idx = 0; 113 | chan->wr_idx = 0; 114 | 115 | pthread_mutex_init(&chan->mutex, NULL); 116 | pthread_cond_init(&chan->send_cond, NULL); 117 | pthread_cond_init(&chan->recv_cond, NULL); 118 | 119 | return chan; 120 | } 121 | 122 | void 123 | channel_free(struct channel *chan) 124 | { 125 | channel_poison(chan); 126 | pthread_cond_destroy(&chan->send_cond); 127 | pthread_cond_destroy(&chan->recv_cond); 128 | pthread_mutex_destroy(&chan->mutex); 129 | free(chan->elts); 130 | free(chan); 131 | } 132 | 133 | void * 134 | channel_reserve(struct channel *chan) 135 | { 136 | POISON_CHECK(chan, NULL); 137 | 138 | _channel_wait_lock(chan, channel_is_full, &chan->send_cond); 139 | 140 | // May have been poisoned while waiting 141 | POISON_CHECK(chan, NULL); 142 | 143 | void *buf = channel_get(chan, chan->wr_idx); 144 | 145 | return buf; 146 | } 147 | 148 | int 149 | channel_send_reserved(struct channel *chan, void *data) 150 | { 151 | POISON_CHECK(chan, 0); 152 | 153 | // Check ptr validity (channel_get handles wrapping) 154 | if (data == NULL || data != channel_get(chan, chan->wr_idx)) { 155 | return 0; 156 | } 157 | 158 | pthread_mutex_lock(&chan->mutex); 159 | int was_empty = channel_is_empty(chan); 160 | chan->wr_idx = channel_next(chan, chan->wr_idx); 161 | if (was_empty) { 162 | // Notify any waiting receiver 163 | pthread_cond_signal(&chan->recv_cond); 164 | } 165 | pthread_mutex_unlock(&chan->mutex); 166 | 167 | return 1; 168 | } 169 | 170 | int 171 | channel_send(struct channel *chan, void *data) 172 | { 173 | void *dst = channel_reserve(chan); 174 | if (dst == NULL) { 175 | return 0; 176 | } 177 | memcpy(dst, data, chan->elt_size); 178 | return channel_send_reserved(chan, dst); 179 | } 180 | 181 | void * 182 | channel_peek(struct channel *chan) 183 | { 184 | POISON_CHECK(chan, NULL); 185 | 186 | _channel_wait_lock(chan, channel_is_empty, &chan->recv_cond); 187 | 188 | // May have been poisoned while waiting 189 | POISON_CHECK(chan, NULL); 190 | 191 | void *data = channel_get(chan, chan->rd_idx); 192 | return data; 193 | } 194 | 195 | int 196 | channel_ack(struct channel *chan, void *data) 197 | { 198 | POISON_CHECK(chan, 0); 199 | 200 | // Invalid data 201 | if (data == NULL || data != channel_get(chan, chan->rd_idx)) { 202 | return 0; 203 | } 204 | 205 | pthread_mutex_lock(&chan->mutex); 206 | int was_full = channel_is_full(chan); 207 | chan->rd_idx = channel_next(chan, chan->rd_idx); 208 | if (was_full) { 209 | // Notify waiting thread 210 | pthread_cond_signal(&chan->send_cond); 211 | } 212 | pthread_mutex_unlock(&chan->mutex); 213 | 214 | 215 | return 1; 216 | } 217 | 218 | int 219 | channel_recv(struct channel *chan, void *data) 220 | { 221 | void *src = channel_peek(chan); 222 | if (src == NULL) { 223 | return 0; 224 | } 225 | memcpy(data, src, chan->elt_size); 226 | return channel_ack(chan, src); 227 | } 228 | 229 | void 230 | channel_poison(struct channel *chan) 231 | { 232 | chan->poisoned = 1; 233 | pthread_cond_signal(&chan->send_cond); 234 | pthread_cond_signal(&chan->recv_cond); 235 | } 236 | 237 | size_t 238 | channel_elt_size(struct channel *chan) 239 | { 240 | return chan->elt_size; 241 | } 242 | -------------------------------------------------------------------------------- /src/lib/encoding.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #ifndef HGAP_ENCODING_H 20 | #define HGAP_ENCODING_H 21 | 22 | #include "proto.h" 23 | 24 | /** 25 | * This file provides the main interfaces to hande a hairgap encoding session. 26 | * An "encoding session" is the operation that transforms a stream of raw data 27 | * chunks into network packets. 28 | * A "decoding session" is the operation that transforms a stream of network 29 | * packets into a stream of raw data to be output. 30 | * 31 | * This module contains: 32 | * 33 | * - hgap_encoder: an encoding session, handles packet generation. The packet 34 | * network sending is delegated to the user for more flexibility (see 35 | * hgap_sender for a helper class). 36 | * - hgap_decoder: a receiver session that eats raw hairgap network packets and 37 | * emits decoded data chunks, performing error correction. 38 | * - helpers to handle the handwave and teardown of the session on the sender 39 | * and receiver side. 40 | */ 41 | 42 | // ========================================================================== 43 | // Encoding 44 | // ========================================================================== 45 | 46 | /** 47 | * Turns chunks of data into initialized hgap_enc_chunk-s. It cannot be used 48 | * for multiple successive transfers. 49 | * It would probably be named HairgapEncodedChunkFactorySession in Java. 50 | * 51 | * In turn, an hgap_enc_chunk will emit a stream of raw hairgap packets ready 52 | * to be sent on the network. 53 | * 54 | * The API exposes the chunking of the datastream because initializing a 55 | * hgap_enc_chunk is relatively costy and may be done in a separate thread from 56 | * the one that actually emits packets. 57 | * 58 | * Typical (simplistic) code would be: 59 | * 60 | * #include "encoding.h" 61 | * #include "sender.h" 62 | * 63 | * char pkt[UDP_MTU]; 64 | * double redund; 65 | * size_t size_to_send = SIZE_TO_SEND; 66 | * size_t pkt_sz = 0; 67 | * void *to_send = get_data(size_to_send); 68 | * 69 | * struct hgap_encoder *enc = hgap_encoder_new(UDP_MTU); 70 | * struct hgap_enc_chunk *chunk = hgap_enc_chunk_new(); 71 | * struct hgap_sender *snd = hgap_sender_new(HOST, PORT, BYTERATE, KEEPALIVE); 72 | * 73 | * // Protocol handwave: generated with the encoder, sent with the sender 74 | * hgap_encoder_handwave(enc, pkt, &pkt_sz); 75 | * hgap_sender_control(snd, pkt, pkt_sz); 76 | * 77 | * // Modulo last chunk size handling 78 | * // (this is valid iff SIZE_TO_SEND % CHUNK_SIZE == 0) 79 | * for (int i = 0; i < SIZE_TO_SEND; i += CHUNK_SIZE) { 80 | * hgap_enc_chunk_init(enc, chunk, to_send + i, CHUNK_SIZE); 81 | * do { 82 | * pkt_sz = UDP_MTU; 83 | * redund = hgap_enc_chunk_emit(chunk, pkt, &pkt_sz); 84 | * if (redund < 0) { 85 | * // Error 86 | * } 87 | * hgap_sender_send(snd, pkt, pkt_sz); 88 | * } while (redund < WANTED_REDUND); 89 | * } 90 | * 91 | * // Protocol teardown 92 | * hgap_encoder_teardown(enc, pkt, &pkt_sz); 93 | * hgap_sender_control(snd, pkt, pkt_sz); 94 | * 95 | * hgap_encoder_free(enc); 96 | * hgap_enc_chunk_free(chunk); 97 | * hgap_sender_free(snd); 98 | */ 99 | struct hgap_encoder; 100 | struct hgap_enc_chunk; 101 | 102 | /** 103 | * Initialize an hgap_encoder that will emit packets of size pkt_size. 104 | */ 105 | struct hgap_encoder *hgap_encoder_new(size_t pkt_size); 106 | void hgap_encoder_free(struct hgap_encoder *enc); 107 | 108 | /** 109 | * Dynamically allocates a safely initialized hgap_enc_chunk. 110 | */ 111 | struct hgap_enc_chunk *hgap_enc_chunk_new(void); 112 | 113 | /** 114 | * Initializes a hgap_enc_chunk that will emit packets encoding the to_enc 115 | * buffer of size size as part of enc's stream. 116 | * 117 | * No reference is kept on to_enc. 118 | * 119 | * @param chunk can be a reused hgap_enc_chunk instance (allowing one 120 | * allocation for multiple uses). 121 | */ 122 | int hgap_enc_chunk_init(struct hgap_encoder *enc, struct hgap_enc_chunk *chunk, 123 | const void *to_enc, size_t size); 124 | 125 | /** 126 | * Free memory allocated in this hgap_enc_chunk. 127 | */ 128 | void hgap_enc_chunk_free(struct hgap_enc_chunk *chunk); 129 | 130 | 131 | /** 132 | * Write a ready-to-send hairgap raw packet to pkt. The number of calls to 133 | * this function is potentially unlimited: emit as many packets as you need to 134 | * meet your redundancy criteria. 135 | * 136 | * @param pkt its length must be at least *size 137 | * @param size is filled with the actual packet length 138 | * @return the redundancy of data after this chunk is emitted or < 0 on error. 139 | */ 140 | double hgap_enc_chunk_emit(struct hgap_enc_chunk *chunk, void *pkt, 141 | size_t *size); 142 | 143 | /** 144 | * Creates a handwave packet in pkt. 145 | * 146 | * @param pkt should be at least of len HGAP_MIN_BUF. 147 | * @param size *size is the available size in the pkt buffer and is filled with 148 | * the actual size of the packet produced. 149 | * @return HGAP_SUCCESS on success or HGAP_ERR_BUFFER_TOO_SMALL if *size is too 150 | * small to contain the produced packet. 151 | */ 152 | int hgap_encoder_handwave(struct hgap_encoder *enc, void *pkt, size_t *size); 153 | 154 | /** 155 | * Same as hgap_encoder_handwave but with the teardown control packet. 156 | */ 157 | int hgap_encoder_teardown(struct hgap_encoder *enc, void *pkt, size_t *size); 158 | 159 | 160 | // ========================================================================== 161 | // Decoding 162 | // ========================================================================== 163 | 164 | /** 165 | * Receive and decode packets from the network. This API is simpler since all 166 | * the parameters are deduced from the packets received on the network. 167 | * 168 | * It is basically an iterator that eats raw hairgap packets and emits decoded 169 | * chunks of data. It is always safe to use a packet buffer of 170 | * HGAP_MAX_PKT_SIZE bytes. 171 | * 172 | * Typical (simplistic) code would be: 173 | * 174 | * #include "encoding.h" 175 | * 176 | * char pkt[UDP_MTU]; 177 | * size_t pkt_sz = 0; 178 | * struct hgap_decoder *dec = hgap_decoder_new(); 179 | * 180 | * while(RECEIVE_PKT(&pkt, &pkt_sz)) { 181 | * int ret = hgap_decoder_read(dec, &pkt, pkt_sz); 182 | * if (ret == -HGAP_EOT) { 183 | * break; // End of transfer 184 | * } else if (ret < 0) { 185 | * // Error 186 | * } else if (ret > 0) { 187 | * void *chunk_buf = malloc(ret); 188 | * assert(hgap_decoder_emit(dec, chunk_buf, ret) == HGAP_SUCCESS); 189 | * OUTPUT_CHUNK(chunk_buf, ret); 190 | * free(chunk_buf); 191 | * } // Else continue to read 192 | * } 193 | * 194 | * hgap_decoder_free(dec); 195 | */ 196 | struct hgap_decoder; 197 | 198 | /** 199 | * Describes at which point in the protocol a hgap_decoder is. 200 | */ 201 | enum hgap_decoder_state { 202 | T_NEW = 0, 203 | T_STARTED = 1, 204 | T_DATA = 2, 205 | T_STOPPED = 3, 206 | }; 207 | 208 | 209 | /** 210 | * Initializes the decoder (not much done yet). 211 | */ 212 | struct hgap_decoder *hgap_decoder_new(); 213 | void hgap_decoder_free(struct hgap_decoder *dec); 214 | 215 | /** 216 | * Reads a raw hairgap packet and update its internal state. 217 | * 218 | * @return size of a chunk ready to be emitted, < 0 if an error occured, 0 219 | * when expecting to read a new packet. 220 | */ 221 | ssize_t hgap_decoder_read(struct hgap_decoder *dec, void *raw_pkt, size_t len); 222 | 223 | /** 224 | * @return HGAP_SUCCESS or an HGAP_ERR_* (auth or lost chunk) 225 | */ 226 | int hgap_decoder_emit(struct hgap_decoder *dec, void *out_buf, size_t len); 227 | 228 | #endif // HGAP_ENCODING_H 229 | -------------------------------------------------------------------------------- /src/lib/hgap_send.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "hairgap.h" 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "channel.h" 28 | #include "common.h" 29 | #include "sender.h" 30 | #include "encoding.h" 31 | 32 | struct read_loop_arg { 33 | const struct hgap_config *config; 34 | struct channel *chan; 35 | }; 36 | 37 | static void 38 | read_chunk(FILE *in, struct sized_buf *buf) 39 | { 40 | size_t to_read = buf->size; 41 | buf->size = 0; 42 | ssize_t read_ret = 0; 43 | while (to_read > 0 && 44 | (read_ret = fread(buf->data + buf->size, 1, to_read, in)) > 0) { 45 | buf->size += read_ret; 46 | to_read -= read_ret; 47 | } 48 | } 49 | 50 | 51 | static void * 52 | read_loop(const struct read_loop_arg *args) 53 | { 54 | struct channel *chan = args->chan; 55 | const struct hgap_config *config = args->config; 56 | FILE *in_file = config->in; 57 | 58 | int more_data = 1; 59 | struct sized_buf *buf = NULL; 60 | size_t buf_size = channel_elt_size(chan) - sizeof(struct sized_buf); 61 | 62 | int retval = HGAP_SUCCESS; 63 | 64 | if (in_file == NULL) { 65 | ERROR("Bad input file descriptor\n"); 66 | retval = HGAP_ERR_BAD_IN_FD; 67 | more_data = 0; 68 | } 69 | 70 | while (more_data) { 71 | buf = channel_reserve(chan); 72 | CHK(buf); 73 | buf->size = buf_size; 74 | buf->data = buf->content; 75 | 76 | read_chunk(in_file, buf); 77 | if (ferror(in_file)) { 78 | PWARN("Error while reading input file"); 79 | retval = HGAP_ERR_FILE_READ; 80 | break; 81 | } 82 | 83 | if (!channel_send_reserved(chan, buf)) { 84 | DBG("chan_in2enc send error\n"); 85 | retval = HGAP_ERR_IPC; 86 | break; 87 | } 88 | 89 | if (feof(in_file)) { 90 | DBG("eof"); 91 | break; 92 | } 93 | } 94 | 95 | // Poison chunk 96 | buf = channel_reserve(chan); 97 | if (buf != NULL) { 98 | SBUF_RESET(*buf); 99 | if (!channel_send_reserved(chan, buf)) { 100 | DBG("chan_in2enc send poison error\n"); 101 | retval = HGAP_ERR_IPC; 102 | } 103 | } 104 | 105 | return (void *) (intptr_t) retval; 106 | } 107 | 108 | struct encode_loop_arg { 109 | struct hgap_encoder *enc; 110 | struct channel *chan_in2enc; 111 | struct channel *chan_enc2net; 112 | }; 113 | 114 | static void * 115 | encode_loop(const struct encode_loop_arg* args) 116 | { 117 | struct hgap_encoder *enc = args->enc; 118 | struct channel *chan_in2enc = args->chan_in2enc; 119 | struct channel *chan_enc2net = args->chan_enc2net; 120 | 121 | struct hgap_enc_chunk *chunk = NULL; 122 | struct sized_buf *to_enc = NULL; 123 | int retval = HGAP_SUCCESS; 124 | int ret; 125 | 126 | // Continue while reading from the channel is possible 127 | while ((to_enc = channel_peek(chan_in2enc)) != NULL) { 128 | // Poison 129 | if (to_enc->data == NULL) { 130 | break; 131 | } 132 | 133 | // New chunk, will be freed by next thread 134 | chunk = hgap_enc_chunk_new(); 135 | if (chunk == NULL) { 136 | DBG("Error while allocating a chunk.\n"); 137 | retval = HGAP_ERR_INTERNAL; 138 | break; 139 | } 140 | 141 | #if 0 142 | DBG("Reading %02hhx %02hhx %02hhx %02hhx\n", to_enc.data[0], to_enc.data[1], to_enc.data[2], to_enc.data[3]); 143 | #endif 144 | 145 | // Pre-encoding 146 | ret = hgap_enc_chunk_init(enc, chunk, to_enc->data, to_enc->size); 147 | if (ret != HGAP_SUCCESS) { 148 | HGAP_PERROR(ret, "Error while encoding chunk"); 149 | retval = ret; 150 | break; 151 | } 152 | 153 | if (!channel_send(chan_enc2net, &chunk)) { 154 | DBG("chan_enc2net send error\n"); 155 | retval = HGAP_ERR_IPC; 156 | break; 157 | } 158 | 159 | chunk = NULL; 160 | 161 | // Encoded, tell the channel that it can reuse the buffer 162 | channel_ack(chan_in2enc, to_enc); 163 | } 164 | 165 | if (chunk != NULL) { 166 | hgap_enc_chunk_free(chunk); 167 | } 168 | 169 | // Propagate poison 170 | chunk = NULL; 171 | if (!channel_send(chan_enc2net, &chunk)) { 172 | DBG("chan_enc2net send poison error\n"); 173 | retval = HGAP_ERR_IPC; 174 | } 175 | 176 | return (void *) (intptr_t) retval; 177 | } 178 | 179 | static int 180 | send_loop(const struct hgap_config *config, struct hgap_encoder *enc, 181 | struct channel *chan_enc2net) 182 | { 183 | size_t data_sent = 0; 184 | double cur_redund = 0; 185 | double redund = config->redund; 186 | size_t pkt_size = config->pkt_size; 187 | // Temporary var to receive actual length of packet 188 | size_t send_size = pkt_size; 189 | void *pkt = xmalloc(pkt_size); 190 | memset(pkt, 0, pkt_size); 191 | 192 | int more_data = 1; 193 | int retval = HGAP_SUCCESS; 194 | 195 | struct hgap_sender *hs = hgap_sender_new(config->addr, config->port, 196 | config->byterate, 197 | config->keepalive); 198 | if (hs == NULL) { 199 | retval = HGAP_ERR_INTERNAL; 200 | goto send_loop_fail; 201 | } 202 | 203 | struct hgap_enc_chunk *chunk = NULL; 204 | 205 | // Handwave (send control salve to announce the transfer) 206 | int ret; 207 | if ((ret = hgap_encoder_handwave(enc, pkt, &send_size)) != HGAP_SUCCESS) { 208 | HGAP_PERROR(ret, "Handwave"); 209 | retval = HGAP_ERR_BUFFER_TOO_SMALL; 210 | goto send_loop_fail; 211 | } 212 | 213 | if (hgap_sender_control(hs, pkt, send_size) != 0) { 214 | perror("Panic: unexpected network error"); 215 | retval = HGAP_ERR_NETWORK; 216 | goto send_loop_fail; 217 | } 218 | send_size = pkt_size; 219 | 220 | while (more_data) { 221 | // Get input chunk 222 | if (!channel_recv(chan_enc2net, (void *)&chunk)) { 223 | DBG("chan_enc2net receive error\n"); 224 | retval = HGAP_ERR_IPC; 225 | goto send_loop_fail; 226 | } 227 | 228 | // Poison (NULL) chunk => end of transfer 229 | if (chunk == NULL) { 230 | more_data = 0; 231 | break; 232 | } 233 | 234 | // Generate and send all packets for this encoding chunk 235 | do { 236 | cur_redund = hgap_enc_chunk_emit(chunk, pkt, &send_size); 237 | if (cur_redund < 0) { 238 | retval = HGAP_ERR_WIREHAIR_ERROR; 239 | more_data = 0; 240 | } else { 241 | size_t sent = hgap_sender_send(hs, pkt, send_size); 242 | data_sent += sent; 243 | } 244 | send_size = pkt_size; 245 | } while (cur_redund < redund); 246 | 247 | hgap_enc_chunk_free(chunk); 248 | chunk = NULL; 249 | } 250 | 251 | INFO("Sent all chunks.\n"); 252 | INFO("%ld bytes sent.\n", data_sent); 253 | send_size = pkt_size; 254 | 255 | // Proper teardown only on proper exit 256 | // TODO: err handling? 257 | if (!more_data) { 258 | hgap_encoder_teardown(enc, pkt, &send_size); 259 | hgap_sender_control(hs, pkt, send_size); 260 | } 261 | 262 | send_loop_fail: 263 | free(pkt); 264 | hgap_sender_free(hs); 265 | 266 | return retval; 267 | } 268 | 269 | int 270 | hgap_send(const struct hgap_config *config) 271 | { 272 | int err; 273 | if ((err = hgap_check_config_sender(config)) != HGAP_SUCCESS) { 274 | return err; 275 | } 276 | 277 | if (wirehair_init() == 0) { 278 | return HGAP_ERR_WIREHAIR_ERROR; 279 | } 280 | DBG("wirehair initialized\n"); 281 | 282 | // Alternatively, read only multiple of pages 283 | // size_t buf_size = PAGE_ROUND_DOWN(config->n_pkt * config->pkt_size); 284 | size_t buf_size = config->n_pkt * (config->pkt_size - HGAP_HEADER_LEN); 285 | 286 | // Shared structure allocation 287 | struct hgap_encoder *enc = hgap_encoder_new(config->pkt_size); 288 | CHK(enc != NULL); 289 | 290 | // FIXME: hardcoded channel size, should depend on config (mem_limit) 291 | struct channel *chan_in2enc = channel_new( 292 | sizeof (struct sized_buf) + buf_size, 16); 293 | struct channel *chan_enc2net = channel_new( 294 | sizeof (struct hgap_enc_chunk *), 16); 295 | CHK(chan_in2enc); 296 | CHK(chan_enc2net); 297 | 298 | pthread_t read_thread; 299 | const struct read_loop_arg rdargs = { 300 | .chan=chan_in2enc, 301 | .config=config, 302 | }; 303 | DBG("Create read_thread\n"); 304 | CHK_PERROR(pthread_create(&read_thread, NULL, 305 | (void*(*)(void*)) read_loop, (void *)&rdargs) == 0); 306 | 307 | pthread_t encode_thread; 308 | const struct encode_loop_arg encargs = { 309 | .enc=enc, 310 | .chan_in2enc=chan_in2enc, 311 | .chan_enc2net=chan_enc2net, 312 | }; 313 | DBG("Create encode_thread\n"); 314 | CHK_PERROR(pthread_create(&encode_thread, NULL, 315 | (void*(*)(void*)) encode_loop, (void *)&encargs) == 0); 316 | 317 | DBG("Start send_loop\n"); 318 | int retval = send_loop(config, enc, chan_enc2net); 319 | void *tmp_ret = (void *) HGAP_SUCCESS; 320 | 321 | pthread_join(read_thread, &tmp_ret); 322 | if (tmp_ret != (void *) HGAP_SUCCESS) { 323 | retval = HGAP_SELECT_ERROR((int) (uintptr_t) tmp_ret, retval); 324 | } 325 | 326 | pthread_join(encode_thread, (void **)&tmp_ret); 327 | if (tmp_ret != (void *) HGAP_SUCCESS) { 328 | retval = HGAP_SELECT_ERROR((int) (uintptr_t) tmp_ret, retval); 329 | } 330 | 331 | channel_free(chan_enc2net); 332 | channel_free(chan_in2enc); 333 | hgap_encoder_free(enc); 334 | 335 | return retval; 336 | } 337 | -------------------------------------------------------------------------------- /src/lib/hgap_receive.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "hairgap.h" 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | #include "channel.h" 36 | #include "common.h" 37 | #include "encoding.h" 38 | 39 | #define HGAPR_WRITE_SYNC_THRESHOLD (100 * 1024 * 1024) 40 | 41 | static int 42 | hgapr_open_udp_socket(char *addr, short port) 43 | { 44 | struct sockaddr_in servaddr; 45 | socklen_t socklen = sizeof(servaddr); 46 | int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 47 | if (sockfd == -1) { 48 | perror("socket"); 49 | goto err; 50 | } 51 | 52 | memset(&servaddr, 0, sizeof(servaddr)); 53 | servaddr.sin_family = AF_INET; 54 | if ((servaddr.sin_addr.s_addr = inet_addr(addr)) == INADDR_NONE) { 55 | goto err; 56 | } 57 | 58 | servaddr.sin_port = htons(port); 59 | 60 | if (bind(sockfd, (struct sockaddr *) &servaddr, socklen) == -1) { 61 | perror("socket"); 62 | goto err; 63 | } 64 | 65 | if (0) { 66 | err: 67 | if (sockfd != -1) { 68 | close(sockfd); 69 | } 70 | sockfd = -1; 71 | } 72 | 73 | return sockfd; 74 | } 75 | 76 | static void 77 | hgapr_set_socket_timeout(int sockfd, uint64_t timeout) { 78 | struct timeval tv; 79 | 80 | tv.tv_sec = timeout / 1000000; 81 | tv.tv_usec = timeout % 1000000; 82 | 83 | CHK_PERROR(setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, 84 | &tv, sizeof(tv)) != -1); 85 | } 86 | 87 | static int 88 | hgapr_net_reader(struct channel *chan, char *addr, short port, uint64_t timeout) 89 | { 90 | struct sized_buf *pkt = NULL; 91 | int retval = HGAP_SUCCESS; 92 | size_t mtu = channel_elt_size(chan) - sizeof (struct sized_buf); 93 | int sockfd; 94 | int started = 0; 95 | 96 | if ((sockfd = hgapr_open_udp_socket(addr, port)) == -1) { 97 | retval = HGAP_ERR_NETWORK; 98 | ERROR("Could not open socket\n"); 99 | goto closing; 100 | } 101 | 102 | for (;;) { 103 | pkt = channel_reserve(chan); 104 | CHK(pkt); 105 | pkt->data = pkt->content; 106 | ssize_t pkt_size = recvfrom(sockfd, pkt->data, mtu, 0, NULL, NULL); 107 | 108 | if (pkt_size == -1) { 109 | if (errno == ETIMEDOUT || errno == EAGAIN) { 110 | ERROR("End of reception, socket timed out\n"); 111 | retval = HGAP_ERR_TIMEOUT; 112 | } else { 113 | perror("recvfrom"); 114 | retval = HGAP_ERR_NETWORK; 115 | } 116 | break; 117 | } 118 | 119 | pkt->size = pkt_size; 120 | enum hgap_pkt_t pkt_type = hgap_pkt_type(pkt->data, pkt->size); 121 | 122 | if (pkt_type == HGAP_PKT_BEGIN && !started) { 123 | started = 1; 124 | hgapr_set_socket_timeout(sockfd, timeout); 125 | } 126 | 127 | // Always send to next thread that really handles the hairgap protocol 128 | if (!channel_send_reserved(chan, pkt)) { 129 | DBG("chan_net2dec send error\n"); 130 | retval = HGAP_ERR_IPC; 131 | break; 132 | } 133 | 134 | if (pkt_type == HGAP_PKT_END) { 135 | break; 136 | } 137 | } 138 | 139 | closing: 140 | if (sockfd != -1) { 141 | close(sockfd); 142 | } 143 | 144 | // Poison pill 145 | pkt = channel_reserve(chan); 146 | if (pkt != NULL) { 147 | pkt->data = NULL; 148 | DBG("Net receiver poison pill\n"); 149 | if (!channel_send_reserved(chan, pkt)) { 150 | DBG("chan_net2dec send poison error\n"); 151 | retval = HGAP_ERR_IPC; 152 | } 153 | } 154 | 155 | return retval; 156 | } 157 | 158 | struct decloop_arg { 159 | struct hgap_decoder *dec; 160 | struct channel *chan_net2dec; 161 | struct channel *chan_dec2out; 162 | }; 163 | 164 | static void * 165 | decloop(struct decloop_arg *args) 166 | { 167 | struct hgap_decoder *dec = args->dec; 168 | struct channel *chan_net2dec = args->chan_net2dec; 169 | struct channel *chan_dec2out = args->chan_dec2out; 170 | 171 | // Received from net thread 172 | struct sized_buf *pkt = NULL; 173 | // Sent to write thread 174 | struct sized_buf chunk = SBUF_NULL; 175 | int retval = HGAP_SUCCESS; 176 | 177 | for (;;) { 178 | if ((pkt = channel_peek(chan_net2dec)) == NULL) { 179 | DBG("chan_net2dec receive error\n"); 180 | retval = HGAP_ERR_IPC; 181 | break; 182 | } 183 | 184 | // Poison pill 185 | if (pkt->data == NULL) { 186 | break; 187 | } 188 | 189 | ssize_t dec_ret = hgap_decoder_read(dec, pkt->data, pkt->size); 190 | if (!channel_ack(chan_net2dec, pkt)) { 191 | DBG("chan_net2dec receive error\n"); 192 | retval = HGAP_ERR_IPC; 193 | break; 194 | } 195 | 196 | // More to read 197 | if (dec_ret == 0) { 198 | continue; 199 | } else if (dec_ret == -HGAP_EOT) { 200 | break; 201 | } else if (dec_ret < 0) { 202 | HGAP_PERROR(-dec_ret, "Error when decoding"); 203 | retval = -dec_ret; 204 | channel_poison(chan_net2dec); 205 | break; 206 | } 207 | 208 | // Chunk ready to be emitted 209 | CHK(dec_ret > 0); 210 | chunk.size = dec_ret; 211 | chunk.data = xmalloc(chunk.size); 212 | 213 | int emit_ret = hgap_decoder_emit(dec, chunk.data, chunk.size); 214 | 215 | if (emit_ret == HGAP_SUCCESS) { 216 | if (!channel_send(chan_dec2out, &chunk)) { 217 | DBG("chan_dec2out send error\n"); 218 | retval = HGAP_ERR_IPC; 219 | break; 220 | } 221 | } else { 222 | free(chunk.data); 223 | SBUF_RESET(chunk); 224 | HGAP_PERROR(emit_ret, "Fatal error when decoding"); 225 | channel_poison(chan_net2dec); 226 | retval = emit_ret; 227 | break; 228 | } 229 | } 230 | 231 | INFO("No more data.\n"); 232 | DBG("Decoder poison pill\n"); 233 | chunk.data = NULL; 234 | if (!channel_send(chan_dec2out, &chunk)) { 235 | DBG("chan_dec2out send poison error\n"); 236 | 237 | if (retval == HGAP_SUCCESS) { 238 | retval = HGAP_ERR_IPC; 239 | } 240 | } 241 | DBG("Poison pill sent\n"); 242 | 243 | return (void *) (intptr_t) retval; 244 | } 245 | 246 | struct writer_arg { 247 | struct channel *chan_dec2out; 248 | FILE *out; 249 | }; 250 | 251 | static void * 252 | writer(struct writer_arg* args) 253 | { 254 | struct channel *chan = args->chan_dec2out; 255 | FILE *out = args->out; 256 | 257 | struct sized_buf chunk; 258 | ssize_t wr_ret = 0; 259 | int retval = HGAP_SUCCESS; 260 | 261 | int fd = fileno(out); 262 | if (fd != -1) { 263 | posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE); 264 | } 265 | 266 | size_t data_written = 0; 267 | size_t data_written_total = 0; 268 | 269 | while (channel_recv(chan, &chunk)) { 270 | if (chunk.data == NULL) { 271 | break; 272 | } 273 | 274 | data_written += chunk.size; 275 | data_written_total += chunk.size; 276 | 277 | #if 0 278 | DBG("Writing %02hhx %02hhx %02hhx %02hhx\n", 279 | chunk.data[0], chunk.data[1], chunk.data[2], chunk.data[3]); 280 | #endif 281 | 282 | if (fd != -1) { 283 | wr_ret = write(fd, chunk.data, chunk.size); 284 | } else { 285 | wr_ret = fwrite(chunk.data, 1, chunk.size, out); 286 | } 287 | 288 | if (wr_ret < (ssize_t) chunk.size) { 289 | DBG("Write error (potentially badly handled :)"); 290 | retval = HGAP_ERR_BAD_OUT_FD; 291 | free(chunk.data); 292 | break; 293 | } 294 | 295 | if (data_written >= HGAPR_WRITE_SYNC_THRESHOLD) { 296 | data_written = 0; 297 | if (fd != -1) { 298 | fsync(fd); 299 | } else { 300 | fflush(out); 301 | } 302 | } 303 | 304 | free(chunk.data); 305 | SBUF_RESET(chunk); 306 | } 307 | 308 | if (fd != -1) { 309 | fsync(fd); 310 | } else { 311 | fflush(out); 312 | } 313 | 314 | INFO("Wrote %ld bytes.\n", data_written_total); 315 | DBG("Output flushed\n"); 316 | 317 | return (void *) (intptr_t) retval; 318 | } 319 | 320 | int 321 | hgap_receive(const struct hgap_config *config) 322 | { 323 | int err = hgap_check_config_receiver(config); 324 | if (err != HGAP_SUCCESS) { 325 | return err; 326 | } 327 | 328 | if (wirehair_init() == 0) { 329 | return HGAP_ERR_WIREHAIR_ERROR; 330 | } 331 | DBG("wirehair initialized\n"); 332 | 333 | struct hgap_decoder *dec = hgap_decoder_new(); 334 | CHK(dec); 335 | 336 | size_t pkt_size = sizeof (struct sized_buf) + config->pkt_size; 337 | size_t pkt_chan_size = (config->mem_limit / 2) / pkt_size; 338 | size_t chunk_chan_size = MAX(256, 339 | (config->mem_limit / 2) / HGAP_MAX_CHUNK_SIZE); 340 | 341 | struct channel *chan_net2dec = channel_new(pkt_size, pkt_chan_size); 342 | struct channel *chan_dec2out = channel_new(sizeof(struct sized_buf), 343 | chunk_chan_size); 344 | CHK(chan_net2dec); 345 | CHK(chan_dec2out); 346 | 347 | // Writer thread 348 | pthread_t wr_thread; 349 | struct writer_arg wr_args = { 350 | .chan_dec2out=chan_dec2out, 351 | .out=config->out 352 | }; 353 | CHK_PERROR(pthread_create(&wr_thread, NULL, 354 | (void*(*)(void*))writer, &wr_args) == 0); 355 | 356 | // Decoder thread 357 | pthread_t dec_thread; 358 | struct decloop_arg dec_args = { 359 | .dec=dec, 360 | .chan_net2dec=chan_net2dec, 361 | .chan_dec2out=chan_dec2out 362 | }; 363 | CHK_PERROR(pthread_create(&dec_thread, NULL, 364 | (void*(*)(void*)) decloop, &dec_args) == 0); 365 | 366 | int retval = hgapr_net_reader(chan_net2dec, config->addr, config->port, 367 | config->timeout); 368 | 369 | void *tmp_ret = (void *) HGAP_SUCCESS; 370 | DBG("net reader ended\n"); 371 | 372 | pthread_join(dec_thread, &tmp_ret); 373 | DBG("decode thread joined\n"); 374 | if (tmp_ret != (void *) HGAP_SUCCESS) { 375 | retval = HGAP_SELECT_ERROR((int) (uintptr_t) tmp_ret, retval); 376 | } 377 | 378 | pthread_join(wr_thread, &tmp_ret); 379 | DBG("Writer joined\n"); 380 | if (tmp_ret != (void *) HGAP_SUCCESS) { 381 | retval = HGAP_SELECT_ERROR((int) (uintptr_t) tmp_ret, retval); 382 | } 383 | 384 | channel_free(chan_dec2out); 385 | channel_free(chan_net2dec); 386 | hgap_decoder_free(dec); 387 | 388 | return retval; 389 | } 390 | -------------------------------------------------------------------------------- /src/lib/encoding.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of hairgap. 3 | * Copyright (C) 2017 Florent MONJALET 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "encoding.h" 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #include "common.h" 26 | #include "hairgap.h" 27 | #include "proto.h" 28 | 29 | struct hgap_enc_chunk { 30 | uint64_t num; 31 | size_t len; 32 | size_t pkt_size; 33 | 34 | uint64_t next_pkt_id; 35 | 36 | // Contains the encoded version of the data to redund 37 | wirehair_state wh_state; 38 | // Copy of the data to redund if it is too small to be wirehair encoded 39 | void *data; 40 | 41 | size_t total_gen; 42 | }; 43 | 44 | struct hgap_encoder { 45 | size_t pkt_size; 46 | uint64_t next_chunk_num; 47 | }; 48 | 49 | struct hgap_decoder { 50 | size_t pkt_size; 51 | struct hgap_enc_chunk *chunk; 52 | int chunk_complete; 53 | int chunk_emitted; 54 | 55 | enum hgap_decoder_state state; 56 | }; 57 | 58 | 59 | // ----------------------------------------------------------------------------- 60 | // Chunk functions 61 | // ----------------------------------------------------------------------------- 62 | 63 | static size_t 64 | hgap_enc_chunk_pkt_payload_size(struct hgap_enc_chunk *chunk) 65 | { 66 | return chunk->pkt_size - HGAP_HEADER_LEN; 67 | } 68 | 69 | static int 70 | hgap_enc_chunk_is_small(struct hgap_enc_chunk *chunk) 71 | { 72 | return chunk->len <= hgap_enc_chunk_pkt_payload_size(chunk); 73 | } 74 | 75 | static void 76 | hgap_enc_chunk_purge(struct hgap_enc_chunk *chunk) 77 | { 78 | if (chunk->data != NULL) { 79 | free(chunk->data); 80 | chunk->data = NULL; 81 | } 82 | } 83 | 84 | struct hgap_enc_chunk * 85 | hgap_enc_chunk_new(void) 86 | { 87 | struct hgap_enc_chunk *chunk = xmalloc(sizeof *chunk); 88 | memset(chunk, 0, sizeof *chunk); 89 | return chunk; 90 | } 91 | 92 | void 93 | hgap_enc_chunk_free(struct hgap_enc_chunk *chunk) 94 | { 95 | hgap_enc_chunk_purge(chunk); 96 | 97 | if (chunk->wh_state != NULL) { 98 | wirehair_free(chunk->wh_state); 99 | } 100 | 101 | free(chunk); 102 | } 103 | 104 | int 105 | hgap_enc_chunk_init(struct hgap_encoder *enc, struct hgap_enc_chunk *chunk, 106 | const void *to_enc, size_t size) 107 | { 108 | chunk->data = NULL; 109 | chunk->num = enc->next_chunk_num++; 110 | chunk->len = size; 111 | chunk->pkt_size = MIN(enc->pkt_size, size + HGAP_HEADER_LEN); 112 | chunk->next_pkt_id = 0; 113 | 114 | // FIXME: Always copy? :( 115 | chunk->data = xmalloc(size); 116 | memcpy(chunk->data, to_enc, size); 117 | 118 | if (!hgap_enc_chunk_is_small(chunk)) { 119 | size_t wh_block_size = hgap_enc_chunk_pkt_payload_size(chunk); 120 | chunk->wh_state = wirehair_encode(chunk->wh_state, chunk->data, size, 121 | wh_block_size); 122 | if (chunk->wh_state == NULL) { 123 | return HGAP_ERR_WIREHAIR_ERROR; 124 | } 125 | } 126 | 127 | return HGAP_SUCCESS; 128 | } 129 | 130 | double 131 | hgap_enc_chunk_emit(struct hgap_enc_chunk *chunk, void *pkt, size_t *size) 132 | { 133 | uint64_t id = chunk->next_pkt_id++; 134 | uint64_t payload_size = 0; 135 | 136 | //DBG("Send chunk %ld pkt %ld\n", chunk->num, id); 137 | 138 | // Handle payload 139 | if (hgap_enc_chunk_is_small(chunk)) { 140 | payload_size = chunk->len; 141 | CHK(payload_size + HGAP_HEADER_LEN == chunk->pkt_size); 142 | 143 | // Zero the buffer 144 | memset(pkt, 0, chunk->pkt_size); 145 | 146 | // Copy the data on the begninng of the payload 147 | memcpy((char *) pkt + HGAP_HEADER_LEN, chunk->data, payload_size); 148 | } else { 149 | payload_size = hgap_enc_chunk_pkt_payload_size(chunk); 150 | if (!wirehair_write(chunk->wh_state, (int) id, 151 | (char *) pkt + HGAP_HEADER_LEN)) { 152 | return -1.0; 153 | } 154 | } 155 | 156 | // Handle header 157 | struct hgap_header hdr = { 158 | .chunk_num=chunk->num, 159 | .chunk_size=chunk->len, 160 | .data_id=id, 161 | .data_size=payload_size, 162 | }; 163 | hgap_write_header(&hdr, pkt); 164 | 165 | #if 0 166 | if (1 || id == 0) { 167 | struct hgap_pkt p2; 168 | hgap_pkt_parse(&p2, pkt, chunk->pkt_size); 169 | DBG("Emit %02hhx %02hhx %02hhx %02hhx Chunk num: %lx\n", 170 | p2.data[0], p2.data[1], p2.data[2], p2.data[3], chunk->num); 171 | HGAP_DUMP(&p2); 172 | } 173 | #endif 174 | 175 | // Update *size 176 | *size = chunk->pkt_size; 177 | 178 | // Compute redundancy 179 | chunk->total_gen += payload_size; 180 | double redund = ((double) chunk->total_gen) / ((double) chunk->len); 181 | 182 | return redund; 183 | } 184 | 185 | 186 | // Decoding part 187 | 188 | static int 189 | hgap_dec_chunk_init(struct hgap_enc_chunk *chunk, struct hgap_pkt *pkt) 190 | { 191 | chunk->data = NULL; 192 | chunk->num = pkt->hdr.chunk_num; 193 | chunk->len = pkt->hdr.chunk_size; 194 | chunk->pkt_size = pkt->hdr.data_size + HGAP_HEADER_LEN; 195 | chunk->next_pkt_id = pkt->hdr.data_id; 196 | 197 | if (!hgap_enc_chunk_is_small(chunk)) { 198 | size_t wh_block_size = hgap_enc_chunk_pkt_payload_size(chunk); 199 | chunk->wh_state = wirehair_decode(chunk->wh_state, chunk->len, 200 | wh_block_size); 201 | if (chunk->wh_state == NULL) { 202 | return HGAP_ERR_WIREHAIR_ERROR; 203 | } 204 | } 205 | 206 | return HGAP_SUCCESS; 207 | } 208 | 209 | static ssize_t 210 | hgap_dec_chunk_read(struct hgap_enc_chunk *chunk, struct hgap_pkt *pkt) 211 | { 212 | // Already ready 213 | if (chunk->data != NULL) { 214 | return chunk->len; 215 | } 216 | 217 | chunk->next_pkt_id = pkt->hdr.data_id; 218 | uint64_t id = chunk->next_pkt_id++; 219 | 220 | if (hgap_enc_chunk_is_small(chunk)) { 221 | chunk->data = xmalloc((size_t) chunk->len); 222 | memcpy(chunk->data, pkt->data, chunk->len); 223 | return chunk->len; 224 | } else { 225 | if (chunk->wh_state == NULL) { 226 | // Error :( 227 | return -1; 228 | } 229 | if (wirehair_read(chunk->wh_state, id, pkt->data)) { 230 | // Ready to reassemble 231 | return chunk->len; 232 | } 233 | } 234 | 235 | // Need more data to reassemble 236 | return 0; 237 | } 238 | 239 | 240 | // ----------------------------------------------------------------------------- 241 | // Encoder part (FIXME: name) 242 | // ----------------------------------------------------------------------------- 243 | 244 | struct 245 | hgap_encoder *hgap_encoder_new(size_t pkt_size) 246 | { 247 | struct hgap_encoder *enc = xmalloc(sizeof *enc); 248 | 249 | enc->pkt_size = pkt_size; 250 | enc->next_chunk_num = 0; 251 | return enc; 252 | } 253 | 254 | void 255 | hgap_encoder_free(struct hgap_encoder *enc) 256 | { 257 | free(enc); 258 | } 259 | 260 | int 261 | hgap_encoder_handwave(struct hgap_encoder *enc, void *pkt, size_t *size) 262 | { 263 | struct hgap_header hdr; 264 | 265 | // Will be used later 266 | FAKE_USE(enc); 267 | 268 | if (*size < HGAP_MIN_BUF) { 269 | return HGAP_ERR_BUFFER_TOO_SMALL; 270 | } 271 | 272 | hgap_header_begin(&hdr); 273 | hgap_write_header(&hdr, pkt); 274 | *size = HGAP_HEADER_LEN; 275 | return HGAP_SUCCESS; 276 | } 277 | 278 | int 279 | hgap_encoder_teardown(struct hgap_encoder *enc, void *pkt, size_t *size) 280 | { 281 | struct hgap_header hdr; 282 | 283 | // Will be used later 284 | FAKE_USE(enc); 285 | 286 | if (*size < HGAP_MIN_BUF) { 287 | return HGAP_ERR_BUFFER_TOO_SMALL; 288 | } 289 | 290 | hgap_header_end(&hdr); 291 | hgap_write_header(&hdr, pkt); 292 | *size = HGAP_HEADER_LEN; 293 | return HGAP_SUCCESS; 294 | } 295 | 296 | 297 | // ----------------------------------------------------------------------------- 298 | // Decoder functions 299 | // ----------------------------------------------------------------------------- 300 | 301 | struct hgap_decoder * 302 | hgap_decoder_new() 303 | { 304 | struct hgap_decoder *dec = xmalloc(sizeof *dec); 305 | 306 | dec->pkt_size = HGAP_MAX_PKT_SIZE; 307 | dec->chunk = hgap_enc_chunk_new(); 308 | dec->chunk->num = -1; 309 | dec->chunk_complete = 1; 310 | dec->chunk_emitted = 1; 311 | dec->state = T_NEW; 312 | return dec; 313 | } 314 | 315 | void 316 | hgap_decoder_free(struct hgap_decoder *dec) 317 | { 318 | hgap_enc_chunk_free(dec->chunk); 319 | free(dec); 320 | } 321 | 322 | // 1 if packet has to be handled, 0 otherwise 323 | static int 324 | hgap_decoder_update_state(struct hgap_decoder *dec, void *raw_pkt, size_t size) 325 | { 326 | switch (hgap_pkt_type(raw_pkt, size)) { 327 | case HGAP_PKT_BEGIN: 328 | if (dec->state == T_NEW) { 329 | INFO("Begin transfer...\n"); 330 | dec->state = T_STARTED; 331 | } 332 | return 0; 333 | 334 | case HGAP_PKT_DATA: 335 | if (dec->state == T_STARTED) { 336 | INFO("Incoming data...\n"); 337 | dec->state = T_DATA; 338 | } 339 | return 1; 340 | 341 | case HGAP_PKT_END: 342 | // End of transfer 343 | if (dec->state >= T_STARTED) { 344 | INFO("End of transfer\n"); 345 | dec->state = T_STOPPED; 346 | } 347 | return 0; 348 | 349 | case HGAP_PKT_UNKNOWN: 350 | INFO("Unknown packet\n"); 351 | /* FALLTHROUGH */ 352 | case HGAP_PKT_KEEPALIVE: 353 | /* FALLTHROUGH */ 354 | default: 355 | return 0; 356 | } 357 | } 358 | 359 | ssize_t 360 | hgap_decoder_read(struct hgap_decoder *dec, void *raw_pkt, size_t len) 361 | { 362 | // TODO: track lost packets 363 | struct hgap_pkt pkt; 364 | hgap_pkt_parse(&pkt, raw_pkt, len); 365 | 366 | int handle_pkt = hgap_decoder_update_state(dec, raw_pkt, len); 367 | 368 | if (dec->state < T_DATA) { 369 | return 0; 370 | } 371 | 372 | if (dec->state == T_STOPPED) { 373 | return -HGAP_EOT; 374 | } 375 | 376 | if (!handle_pkt) { 377 | return 0; 378 | } 379 | 380 | #if 0 381 | if (1 || pkt.hdr.data_id == 0) { 382 | DBG("Packet %02hhx %02hhx %02hhx %02hhx Chunk num: %lx\n", 383 | pkt.data[0], pkt.data[1], pkt.data[2], pkt.data[3], pkt.hdr.chunk_num); 384 | HGAP_DUMP(&pkt); 385 | } 386 | #endif 387 | 388 | // New chunk number 389 | if (pkt.hdr.chunk_num != dec->chunk->num) { 390 | // If incoherent chunk, error (new chunk but previous is incomplete) 391 | if (!dec->chunk_complete) { 392 | ERROR("Error: missed too many packets " 393 | "(cur chunk: %lu, last_chunk: %lu, cur_id: %u\n", 394 | pkt.hdr.chunk_num, dec->chunk->num, pkt.hdr.data_id); 395 | return -HGAP_ERR_INCOMPLETE_CHUNK; 396 | } 397 | 398 | if (pkt.hdr.chunk_size > HGAP_MAX_CHUNK_SIZE) { 399 | return -HGAP_ERR_BAD_CHUNK; 400 | } 401 | 402 | // Purge the chunk (enc/dec is the same struct) 403 | hgap_enc_chunk_purge(dec->chunk); 404 | // Reinit chunk from first packet of the new chunk 405 | hgap_dec_chunk_init(dec->chunk, &pkt); 406 | dec->chunk_complete = 0; 407 | dec->chunk_emitted = 0; 408 | } else if (dec->chunk_complete) { 409 | // Already ready 410 | if (dec->chunk_emitted) { 411 | return 0; 412 | } else { 413 | return dec->chunk->len; 414 | } 415 | } 416 | 417 | // Incorporate this packet in the decoding state of the current chunk 418 | ssize_t ready = hgap_dec_chunk_read(dec->chunk, &pkt); 419 | if (ready > 0) { 420 | dec->chunk_complete = 1; 421 | } 422 | 423 | return ready; 424 | } 425 | 426 | int 427 | hgap_decoder_emit(struct hgap_decoder *dec, void *out_buf, size_t len) 428 | { 429 | #if 0 430 | DBG("Reassemble chunk %lu of size %zu\n", dec->chunk->num, dec->chunk->len); 431 | #endif 432 | if (!dec->chunk_complete) { 433 | return HGAP_ERR_INCOMPLETE_CHUNK; 434 | } 435 | 436 | if (len < dec->chunk->len) { 437 | return HGAP_ERR_BUFFER_TOO_SMALL; 438 | } 439 | 440 | // Actual data emission 441 | if (hgap_enc_chunk_is_small(dec->chunk)) { 442 | CHK(dec->chunk->data); 443 | memcpy(out_buf, dec->chunk->data, dec->chunk->len); 444 | } else if (!wirehair_reconstruct(dec->chunk->wh_state, out_buf)) { 445 | return HGAP_ERR_WIREHAIR_ERROR; 446 | } 447 | 448 | dec->chunk_emitted = 1; 449 | 450 | return HGAP_SUCCESS; 451 | } 452 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------