aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/c
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2009-12-31 11:41:57 -0500
committerGravatar Adam Chlipala <adamc@hcoop.net>2009-12-31 11:41:57 -0500
commit21678b3f280cd85961e3354faecc29aab4819de4 (patch)
treebd23d8cf5bd50193307b43173436dee92553e4cd /src/c
parentc0b98201e7415eeada11e08c69264cf165bba50f (diff)
Basis.serialize; separate file for mhash; run transactional finishers in reverse order; set needs_sig properly
Diffstat (limited to 'src/c')
-rw-r--r--src/c/mhash.c41
-rw-r--r--src/c/request.c41
-rw-r--r--src/c/urweb.c20
3 files changed, 53 insertions, 49 deletions
diff --git a/src/c/mhash.c b/src/c/mhash.c
new file mode 100644
index 00000000..becb9d97
--- /dev/null
+++ b/src/c/mhash.c
@@ -0,0 +1,41 @@
+#include <mhash.h>
+
+#define KEYSIZE 16
+#define PASSSIZE 4
+
+#define HASH_ALGORITHM MHASH_SHA256
+#define HASH_BLOCKSIZE 32
+#define KEYGEN_ALGORITHM KEYGEN_MCRYPT
+
+int uw_hash_blocksize = HASH_BLOCKSIZE;
+
+static int password[PASSSIZE];
+static unsigned char private_key[KEYSIZE];
+
+void uw_init_crypto() {
+ KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}};
+ int i;
+
+ assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE);
+
+ for (i = 0; i < PASSSIZE; ++i)
+ password[i] = rand();
+
+ if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg,
+ private_key, sizeof(private_key),
+ (unsigned char*)password, sizeof(password)) < 0) {
+ fprintf(stderr, "Key generation failed\n");
+ exit(1);
+ }
+}
+
+void uw_sign(const char *in, char *out) {
+ MHASH td;
+
+ td = mhash_hmac_init(HASH_ALGORITHM, private_key, sizeof(private_key),
+ mhash_get_hash_pblock(HASH_ALGORITHM));
+
+ mhash(td, in, strlen(in));
+ if (mhash_hmac_deinit(td, out) < 0)
+ fprintf(stderr, "Signing failed\n");
+}
diff --git a/src/c/request.c b/src/c/request.c
index 5c8159cc..f190ec98 100644
--- a/src/c/request.c
+++ b/src/c/request.c
@@ -67,35 +67,6 @@ uw_context uw_request_new_context(uw_app *app, void *logger_data, uw_logger log_
return ctx;
}
-#define KEYSIZE 16
-#define PASSSIZE 4
-
-#define HASH_ALGORITHM MHASH_SHA256
-#define HASH_BLOCKSIZE 32
-#define KEYGEN_ALGORITHM KEYGEN_MCRYPT
-
-int uw_hash_blocksize = HASH_BLOCKSIZE;
-
-static int password[PASSSIZE];
-static unsigned char private_key[KEYSIZE];
-
-static void init_crypto(void *logger_data, uw_logger log_error) {
- KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}};
- int i;
-
- assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE);
-
- for (i = 0; i < PASSSIZE; ++i)
- password[i] = rand();
-
- if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg,
- private_key, sizeof(private_key),
- (unsigned char*)password, sizeof(password)) < 0) {
- log_error(logger_data, "Key generation failed\n");
- exit(1);
- }
-}
-
void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
uw_context ctx;
failure_kind fk;
@@ -121,20 +92,8 @@ void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_log
}
uw_free(ctx);
-
- init_crypto(logger_data, log_error);
}
-void uw_sign(const char *in, char *out) {
- MHASH td;
-
- td = mhash_hmac_init(HASH_ALGORITHM, private_key, sizeof(private_key),
- mhash_get_hash_pblock(HASH_ALGORITHM));
-
- mhash(td, in, strlen(in));
- if (mhash_hmac_deinit(td, out) < 0)
- fprintf(stderr, "Signing failed\n");
-}
typedef struct uw_rc {
size_t path_copy_size;
diff --git a/src/c/urweb.c b/src/c/urweb.c
index 455b3e1e..3773e1fb 100644
--- a/src/c/urweb.c
+++ b/src/c/urweb.c
@@ -289,10 +289,14 @@ static void client_send(client *c, buf *msg) {
// Global entry points
+extern void uw_init_crypto();
+
void uw_global_init() {
srand(time(NULL) ^ getpid());
clients = malloc(0);
+
+ uw_init_crypto();
}
void uw_app_init(uw_app *app) {
@@ -420,7 +424,7 @@ uw_context uw_init() {
ctx->script_header = "";
ctx->needs_push = 0;
ctx->needs_sig = 0;
-
+
ctx->error_message[0] = 0;
ctx->source_count = 0;
@@ -2766,14 +2770,14 @@ uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg)
}
void uw_commit(uw_context ctx) {
- unsigned i;
+ int i;
- for (i = 0; i < ctx->used_transactionals; ++i)
+ for (i = ctx->used_transactionals-1; i >= 0; --i)
if (ctx->transactionals[i].rollback != NULL)
if (ctx->transactionals[i].commit)
ctx->transactionals[i].commit(ctx->transactionals[i].data);
- for (i = 0; i < ctx->used_transactionals; ++i)
+ for (i = ctx->used_transactionals-1; i >= 0; --i)
if (ctx->transactionals[i].rollback == NULL)
if (ctx->transactionals[i].commit)
ctx->transactionals[i].commit(ctx->transactionals[i].data);
@@ -2793,7 +2797,7 @@ void uw_commit(uw_context ctx) {
if (ctx->client)
release_client(ctx->client);
- for (i = 0; i < ctx->used_transactionals; ++i)
+ for (i = ctx->used_transactionals-1; i >= 0; --i)
if (ctx->transactionals[i].free)
ctx->transactionals[i].free(ctx->transactionals[i].data);
@@ -2832,7 +2836,7 @@ void uw_commit(uw_context ctx) {
}
int uw_rollback(uw_context ctx) {
- size_t i;
+ int i;
cleanup *cl;
if (ctx->client)
@@ -2843,11 +2847,11 @@ int uw_rollback(uw_context ctx) {
ctx->cleanup_front = ctx->cleanup;
- for (i = 0; i < ctx->used_transactionals; ++i)
+ for (i = ctx->used_transactionals-1; i >= 0; --i)
if (ctx->transactionals[i].rollback != NULL)
ctx->transactionals[i].rollback(ctx->transactionals[i].data);
- for (i = 0; i < ctx->used_transactionals; ++i)
+ for (i = ctx->used_transactionals-1; i >= 0; --i)
if (ctx->transactionals[i].free)
ctx->transactionals[i].free(ctx->transactionals[i].data);