diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-04-24 13:46:07 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-04-24 13:46:07 +0000 |
commit | 8d9525a642c6bd74a2903ccb6358da8ebb081864 (patch) | |
tree | 1e26db482e5ab9869bbc5cc687e1cd27c592eda6 /src/block | |
parent | 32892c4aa7736eb8039f28d79b5cdb53cdc0061c (diff) | |
download | gnunet-8d9525a642c6bd74a2903ccb6358da8ebb081864.tar.gz gnunet-8d9525a642c6bd74a2903ccb6358da8ebb081864.zip |
blocklib
Diffstat (limited to 'src/block')
-rw-r--r-- | src/block/Makefile.am | 28 | ||||
-rw-r--r-- | src/block/block.c | 230 | ||||
-rwxr-xr-x | src/block/test_block | 148 | ||||
-rw-r--r-- | src/block/test_block.c | 36 |
4 files changed, 442 insertions, 0 deletions
diff --git a/src/block/Makefile.am b/src/block/Makefile.am new file mode 100644 index 000000000..b9585c260 --- /dev/null +++ b/src/block/Makefile.am | |||
@@ -0,0 +1,28 @@ | |||
1 | INCLUDES = -I$(top_srcdir)/src/include | ||
2 | |||
3 | if MINGW | ||
4 | WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols | ||
5 | endif | ||
6 | |||
7 | if USE_COVERAGE | ||
8 | AM_CFLAGS = --coverage | ||
9 | endif | ||
10 | |||
11 | lib_LTLIBRARIES = libgnunetblock.la | ||
12 | |||
13 | libgnunetblock_la_SOURCES = \ | ||
14 | block.c | ||
15 | libgnunetblock_la_LIBADD = \ | ||
16 | $(top_builddir)/src/util/libgnunetutil.la | ||
17 | |||
18 | check_PROGRAMS = \ | ||
19 | test_block | ||
20 | |||
21 | #TESTS = $(check_PROGRAMS) | ||
22 | |||
23 | test_block_SOURCES = \ | ||
24 | test_block.c | ||
25 | test_block_LDADD = \ | ||
26 | $(top_builddir)/src/block/libgnunetblock.la \ | ||
27 | $(top_builddir)/src/util/libgnunetutil.la | ||
28 | |||
diff --git a/src/block/block.c b/src/block/block.c new file mode 100644 index 000000000..24dab7907 --- /dev/null +++ b/src/block/block.c | |||
@@ -0,0 +1,230 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2010 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file block/block.c | ||
23 | * @brief library for data block manipulation | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_signatures.h" | ||
29 | #include "gnunet_block_lib.h" | ||
30 | |||
31 | /** | ||
32 | * Check if the given KBlock is well-formed. | ||
33 | * | ||
34 | * @param kb the kblock data (or at least "dsize" bytes claiming to be one) | ||
35 | * @param dsize size of "kb" in bytes; check for < sizeof(struct KBlock)! | ||
36 | * @param query where to store the query that this block answers | ||
37 | * @return GNUNET_OK if this is actually a well-formed KBlock | ||
38 | */ | ||
39 | static int | ||
40 | check_kblock (const struct KBlock *kb, | ||
41 | size_t dsize, | ||
42 | GNUNET_HashCode *query) | ||
43 | { | ||
44 | if (dsize < sizeof (struct KBlock)) | ||
45 | { | ||
46 | GNUNET_break_op (0); | ||
47 | return GNUNET_SYSERR; | ||
48 | } | ||
49 | if (dsize - sizeof (struct KBlock) != | ||
50 | ntohl (kb->purpose.size) | ||
51 | - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) | ||
52 | - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) ) | ||
53 | { | ||
54 | GNUNET_break_op (0); | ||
55 | return GNUNET_SYSERR; | ||
56 | } | ||
57 | if (GNUNET_OK != | ||
58 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK, | ||
59 | &kb->purpose, | ||
60 | &kb->signature, | ||
61 | &kb->keyspace)) | ||
62 | { | ||
63 | GNUNET_break_op (0); | ||
64 | return GNUNET_SYSERR; | ||
65 | } | ||
66 | if (query != NULL) | ||
67 | GNUNET_CRYPTO_hash (&kb->keyspace, | ||
68 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | ||
69 | query); | ||
70 | return GNUNET_OK; | ||
71 | } | ||
72 | |||
73 | |||
74 | /** | ||
75 | * Check if the given NBlock is well-formed. | ||
76 | * | ||
77 | * @param nb the nblock data (or at least "dsize" bytes claiming to be one) | ||
78 | * @param dsize size of "nb" in bytes; check for < sizeof(struct NBlock)! | ||
79 | * @param query where to store the query that this block answers | ||
80 | * @return GNUNET_OK if this is actually a well-formed NBlock | ||
81 | */ | ||
82 | static int | ||
83 | check_nblock (const struct NBlock *nb, | ||
84 | size_t dsize, | ||
85 | GNUNET_HashCode *query) | ||
86 | { | ||
87 | if (dsize < sizeof (struct NBlock)) | ||
88 | { | ||
89 | GNUNET_break_op (0); | ||
90 | return GNUNET_SYSERR; | ||
91 | } | ||
92 | if (dsize - sizeof (struct NBlock) != | ||
93 | ntohl (nb->ns_purpose.size) | ||
94 | - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) | ||
95 | - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) ) | ||
96 | { | ||
97 | GNUNET_break_op (0); | ||
98 | return GNUNET_SYSERR; | ||
99 | } | ||
100 | if (dsize != | ||
101 | ntohl (nb->ksk_purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature)) | ||
102 | { | ||
103 | GNUNET_break_op (0); | ||
104 | return GNUNET_SYSERR; | ||
105 | } | ||
106 | if (GNUNET_OK != | ||
107 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG, | ||
108 | &nb->ksk_purpose, | ||
109 | &nb->ksk_signature, | ||
110 | &nb->keyspace)) | ||
111 | { | ||
112 | GNUNET_break_op (0); | ||
113 | return GNUNET_SYSERR; | ||
114 | } | ||
115 | if (GNUNET_OK != | ||
116 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK, | ||
117 | &nb->ns_purpose, | ||
118 | &nb->ns_signature, | ||
119 | &nb->subspace)) | ||
120 | { | ||
121 | GNUNET_break_op (0); | ||
122 | return GNUNET_SYSERR; | ||
123 | } | ||
124 | if (query != NULL) | ||
125 | GNUNET_CRYPTO_hash (&nb->keyspace, | ||
126 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | ||
127 | query); | ||
128 | return GNUNET_OK; | ||
129 | } | ||
130 | |||
131 | |||
132 | /** | ||
133 | * Check if the given SBlock is well-formed. | ||
134 | * | ||
135 | * @param sb the sblock data (or at least "dsize" bytes claiming to be one) | ||
136 | * @param dsize size of "kb" in bytes; check for < sizeof(struct SBlock)! | ||
137 | * @param query where to store the query that this block answers | ||
138 | * @return GNUNET_OK if this is actually a well-formed SBlock | ||
139 | */ | ||
140 | static int | ||
141 | check_sblock (const struct SBlock *sb, | ||
142 | size_t dsize, | ||
143 | GNUNET_HashCode *query) | ||
144 | { | ||
145 | if (dsize < sizeof (struct SBlock)) | ||
146 | { | ||
147 | GNUNET_break_op (0); | ||
148 | return GNUNET_SYSERR; | ||
149 | } | ||
150 | if (dsize != | ||
151 | ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature)) | ||
152 | { | ||
153 | GNUNET_break_op (0); | ||
154 | return GNUNET_SYSERR; | ||
155 | } | ||
156 | if (GNUNET_OK != | ||
157 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK, | ||
158 | &sb->purpose, | ||
159 | &sb->signature, | ||
160 | &sb->subspace)) | ||
161 | { | ||
162 | GNUNET_break_op (0); | ||
163 | return GNUNET_SYSERR; | ||
164 | } | ||
165 | if (query != NULL) | ||
166 | *query = sb->identifier; | ||
167 | return GNUNET_OK; | ||
168 | } | ||
169 | |||
170 | |||
171 | /** | ||
172 | * Check if the given block is well-formed (and of the given type). | ||
173 | * | ||
174 | * @param type type of the block | ||
175 | * @param block the block data (or at least "size" bytes claiming to be one) | ||
176 | * @param size size of "kb" in bytes; check that it is large enough | ||
177 | * @param query where to store the query that this block answers | ||
178 | * @return GNUNET_OK if this is actually a well-formed block, | ||
179 | * GNUNET_NO if we could not determine the query, | ||
180 | * GNUNET_SYSERR if the block is malformed | ||
181 | */ | ||
182 | int | ||
183 | GNUNET_BLOCK_check_block (enum GNUNET_BLOCK_Type type, | ||
184 | const void *block, | ||
185 | size_t size, | ||
186 | GNUNET_HashCode *query) | ||
187 | { | ||
188 | /* first, validate! */ | ||
189 | switch (type) | ||
190 | { | ||
191 | case GNUNET_BLOCK_TYPE_DBLOCK: | ||
192 | case GNUNET_BLOCK_TYPE_IBLOCK: | ||
193 | GNUNET_CRYPTO_hash (block, size, query); | ||
194 | break; | ||
195 | case GNUNET_BLOCK_TYPE_KBLOCK: | ||
196 | if (GNUNET_OK != | ||
197 | check_kblock (block, | ||
198 | size, | ||
199 | query)) | ||
200 | return GNUNET_SYSERR; | ||
201 | break; | ||
202 | case GNUNET_BLOCK_TYPE_SBLOCK: | ||
203 | if (GNUNET_OK != | ||
204 | check_sblock (block, | ||
205 | size, | ||
206 | query)) | ||
207 | return GNUNET_SYSERR; | ||
208 | break; | ||
209 | case GNUNET_BLOCK_TYPE_NBLOCK: | ||
210 | if (GNUNET_OK != | ||
211 | check_nblock (block, | ||
212 | size, | ||
213 | query)) | ||
214 | return GNUNET_SYSERR; | ||
215 | return GNUNET_OK; | ||
216 | case GNUNET_BLOCK_TYPE_ONDEMAND: | ||
217 | if (size != sizeof (struct OnDemandBlock)) | ||
218 | return GNUNET_SYSERR; | ||
219 | memset (query, 0, sizeof (GNUNET_HashCode)); | ||
220 | return GNUNET_NO; | ||
221 | default: | ||
222 | /* unknown block type */ | ||
223 | GNUNET_break_op (0); | ||
224 | return GNUNET_SYSERR; | ||
225 | } | ||
226 | return GNUNET_OK; | ||
227 | } | ||
228 | |||
229 | |||
230 | /* end of block.c */ | ||
diff --git a/src/block/test_block b/src/block/test_block new file mode 100755 index 000000000..2b533cc6b --- /dev/null +++ b/src/block/test_block | |||
@@ -0,0 +1,148 @@ | |||
1 | #! /bin/sh | ||
2 | |||
3 | # test_block - temporary wrapper script for .libs/test_block | ||
4 | # Generated by ltmain.sh (GNU libtool) 2.2.6b Debian-2.2.6b-2 | ||
5 | # | ||
6 | # The test_block program cannot be directly executed until all the libtool | ||
7 | # libraries that it depends on are installed. | ||
8 | # | ||
9 | # This wrapper script should never be moved out of the build directory. | ||
10 | # If it is, it will not operate correctly. | ||
11 | |||
12 | # Sed substitution that helps us do robust quoting. It backslashifies | ||
13 | # metacharacters that are still active within double-quoted strings. | ||
14 | Xsed='/bin/sed -e 1s/^X//' | ||
15 | sed_quote_subst='s/\([`"$\\]\)/\\\1/g' | ||
16 | |||
17 | # Be Bourne compatible | ||
18 | if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then | ||
19 | emulate sh | ||
20 | NULLCMD=: | ||
21 | # Zsh 3.x and 4.x performs word splitting on ${1+"$@"}, which | ||
22 | # is contrary to our usage. Disable this feature. | ||
23 | alias -g '${1+"$@"}'='"$@"' | ||
24 | setopt NO_GLOB_SUBST | ||
25 | else | ||
26 | case `(set -o) 2>/dev/null` in *posix*) set -o posix;; esac | ||
27 | fi | ||
28 | BIN_SH=xpg4; export BIN_SH # for Tru64 | ||
29 | DUALCASE=1; export DUALCASE # for MKS sh | ||
30 | |||
31 | # The HP-UX ksh and POSIX shell print the target directory to stdout | ||
32 | # if CDPATH is set. | ||
33 | (unset CDPATH) >/dev/null 2>&1 && unset CDPATH | ||
34 | |||
35 | relink_command="(cd /home/grothoff/svn/gnunet/src/block; { test -z \"\${LIBRARY_PATH+set}\" || unset LIBRARY_PATH || { LIBRARY_PATH=; export LIBRARY_PATH; }; }; { test -z \"\${COMPILER_PATH+set}\" || unset COMPILER_PATH || { COMPILER_PATH=; export COMPILER_PATH; }; }; { test -z \"\${GCC_EXEC_PREFIX+set}\" || unset GCC_EXEC_PREFIX || { GCC_EXEC_PREFIX=; export GCC_EXEC_PREFIX; }; }; { test -z \"\${LD_RUN_PATH+set}\" || unset LD_RUN_PATH || { LD_RUN_PATH=; export LD_RUN_PATH; }; }; { test -z \"\${LD_LIBRARY_PATH+set}\" || unset LD_LIBRARY_PATH || { LD_LIBRARY_PATH=; export LD_LIBRARY_PATH; }; }; PATH=/home/grothoff/bin:/home/grothoff/private/software/prevent-linux-4.3.0/bin/:/home/grothoff/bin:/usr/local/bin:/usr/bin:/bin:/usr/games; export PATH; gcc -fno-strict-aliasing -Wall -g -O0 -o \$progdir/\$file test_block.o -L/home/grothoff//lib ../../src/block/.libs/libgnunetblock.so ../../src/util/.libs/libgnunetutil.so -lm -ldl -Wl,-rpath -Wl,/home/grothoff/svn/gnunet/src/block/.libs -Wl,-rpath -Wl,/home/grothoff/svn/gnunet/src/util/.libs -Wl,-rpath -Wl,/home/grothoff/lib)" | ||
36 | |||
37 | # This environment variable determines our operation mode. | ||
38 | if test "$libtool_install_magic" = "%%%MAGIC variable%%%"; then | ||
39 | # install mode needs the following variables: | ||
40 | generated_by_libtool_version='2.2.6b' | ||
41 | notinst_deplibs=' ../../src/block/libgnunetblock.la ../../src/util/libgnunetutil.la' | ||
42 | else | ||
43 | # When we are sourced in execute mode, $file and $ECHO are already set. | ||
44 | if test "$libtool_execute_magic" != "%%%MAGIC variable%%%"; then | ||
45 | ECHO="echo" | ||
46 | file="$0" | ||
47 | # Make sure echo works. | ||
48 | if test "X$1" = X--no-reexec; then | ||
49 | # Discard the --no-reexec flag, and continue. | ||
50 | shift | ||
51 | elif test "X`{ $ECHO '\t'; } 2>/dev/null`" = 'X\t'; then | ||
52 | # Yippee, $ECHO works! | ||
53 | : | ||
54 | else | ||
55 | # Restart under the correct shell, and then maybe $ECHO will work. | ||
56 | exec /bin/sh "$0" --no-reexec ${1+"$@"} | ||
57 | fi | ||
58 | fi | ||
59 | |||
60 | # Find the directory that this script lives in. | ||
61 | thisdir=`$ECHO "X$file" | $Xsed -e 's%/[^/]*$%%'` | ||
62 | test "x$thisdir" = "x$file" && thisdir=. | ||
63 | |||
64 | # Follow symbolic links until we get to the real thisdir. | ||
65 | file=`ls -ld "$file" | /bin/sed -n 's/.*-> //p'` | ||
66 | while test -n "$file"; do | ||
67 | destdir=`$ECHO "X$file" | $Xsed -e 's%/[^/]*$%%'` | ||
68 | |||
69 | # If there was a directory component, then change thisdir. | ||
70 | if test "x$destdir" != "x$file"; then | ||
71 | case "$destdir" in | ||
72 | [\\/]* | [A-Za-z]:[\\/]*) thisdir="$destdir" ;; | ||
73 | *) thisdir="$thisdir/$destdir" ;; | ||
74 | esac | ||
75 | fi | ||
76 | |||
77 | file=`$ECHO "X$file" | $Xsed -e 's%^.*/%%'` | ||
78 | file=`ls -ld "$thisdir/$file" | /bin/sed -n 's/.*-> //p'` | ||
79 | done | ||
80 | |||
81 | |||
82 | # Usually 'no', except on cygwin/mingw when embedded into | ||
83 | # the cwrapper. | ||
84 | WRAPPER_SCRIPT_BELONGS_IN_OBJDIR=no | ||
85 | if test "$WRAPPER_SCRIPT_BELONGS_IN_OBJDIR" = "yes"; then | ||
86 | # special case for '.' | ||
87 | if test "$thisdir" = "."; then | ||
88 | thisdir=`pwd` | ||
89 | fi | ||
90 | # remove .libs from thisdir | ||
91 | case "$thisdir" in | ||
92 | *[\\/].libs ) thisdir=`$ECHO "X$thisdir" | $Xsed -e 's%[\\/][^\\/]*$%%'` ;; | ||
93 | .libs ) thisdir=. ;; | ||
94 | esac | ||
95 | fi | ||
96 | |||
97 | # Try to get the absolute directory name. | ||
98 | absdir=`cd "$thisdir" && pwd` | ||
99 | test -n "$absdir" && thisdir="$absdir" | ||
100 | |||
101 | program=lt-'test_block' | ||
102 | progdir="$thisdir/.libs" | ||
103 | |||
104 | if test ! -f "$progdir/$program" || | ||
105 | { file=`ls -1dt "$progdir/$program" "$progdir/../$program" 2>/dev/null | /bin/sed 1q`; \ | ||
106 | test "X$file" != "X$progdir/$program"; }; then | ||
107 | |||
108 | file="$$-$program" | ||
109 | |||
110 | if test ! -d "$progdir"; then | ||
111 | mkdir "$progdir" | ||
112 | else | ||
113 | rm -f "$progdir/$file" | ||
114 | fi | ||
115 | |||
116 | # relink executable if necessary | ||
117 | if test -n "$relink_command"; then | ||
118 | if relink_command_output=`eval $relink_command 2>&1`; then : | ||
119 | else | ||
120 | echo "$relink_command_output" >&2 | ||
121 | rm -f "$progdir/$file" | ||
122 | exit 1 | ||
123 | fi | ||
124 | fi | ||
125 | |||
126 | mv -f "$progdir/$file" "$progdir/$program" 2>/dev/null || | ||
127 | { rm -f "$progdir/$program"; | ||
128 | mv -f "$progdir/$file" "$progdir/$program"; } | ||
129 | rm -f "$progdir/$file" | ||
130 | fi | ||
131 | |||
132 | if test -f "$progdir/$program"; then | ||
133 | if test "$libtool_execute_magic" != "%%%MAGIC variable%%%"; then | ||
134 | # Run the actual program with our arguments. | ||
135 | |||
136 | exec "$progdir/$program" ${1+"$@"} | ||
137 | |||
138 | $ECHO "$0: cannot exec $program $*" 1>&2 | ||
139 | exit 1 | ||
140 | fi | ||
141 | else | ||
142 | # The program doesn't exist. | ||
143 | $ECHO "$0: error: \`$progdir/$program' does not exist" 1>&2 | ||
144 | $ECHO "This script is just a wrapper for $program." 1>&2 | ||
145 | echo "See the libtool documentation for more information." 1>&2 | ||
146 | exit 1 | ||
147 | fi | ||
148 | fi | ||
diff --git a/src/block/test_block.c b/src/block/test_block.c new file mode 100644 index 000000000..b8586982a --- /dev/null +++ b/src/block/test_block.c | |||
@@ -0,0 +1,36 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2010 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file block/test_block.c | ||
22 | * @brief test for block.c | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_block_lib.h" | ||
27 | |||
28 | #define DEBUG GNUNET_NO | ||
29 | |||
30 | #define VERBOSE GNUNET_NO | ||
31 | |||
32 | int | ||
33 | main (int argc, char *argv[]) | ||
34 | { | ||
35 | return 0; /* testcase passed */ | ||
36 | } | ||