X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=net.c;h=28bc777d4dde6f56be5414d4d8d92aeb0d47ab6e;hp=850a9f3996990bfd76a0e3586fc44e9d82c4b271;hb=4b16f9ed84133d5c475eaeb887df6475ab0091bc;hpb=bc3ab35d42d4bb321f984806c43b7b991e973716;ds=sidebyside diff --git a/net.c b/net.c index 850a9f39..28bc777d 100644 --- a/net.c +++ b/net.c @@ -23,25 +23,38 @@ #include "string.h" #include "error.h" -static crypt_function **crypt_functions; -static unsigned max_crypt_fd; +struct crypt_data { + crypt_function *recv; + crypt_function *send; + void *private_data; +}; + +static struct crypt_data *crypt_data_array; +static unsigned cda_size = 0; -void enable_crypt(int fd, crypt_function *recv, crypt_function *send) +void enable_crypt(int fd, crypt_function *recv, crypt_function *send, + void *private_data) { - if (max_crypt_fd < fd) { - crypt_functions = para_realloc(crypt_functions, - 2 * (fd + 1) * sizeof(crypt_function*)); - max_crypt_fd = fd; + if (fd + 1 > cda_size) { + crypt_data_array = para_realloc(crypt_data_array, + (fd + 1) * sizeof(struct crypt_data)); + memset(crypt_data_array + cda_size * sizeof(struct crypt_data), 0, + (fd + 1 - cda_size) * sizeof(struct crypt_data)); + cda_size = fd + 1; } - crypt_functions[2 * fd] = recv; - crypt_functions[2 * fd + 1] = send; + crypt_data_array[fd].recv = recv; + crypt_data_array[fd].send = send; + crypt_data_array[fd].private_data = private_data; PARA_INFO_LOG("rc4 encryption activated for fd %d\n", fd); } void disable_crypt(int fd) { - crypt_functions[2 * fd] = NULL; - crypt_functions[2 * fd + 1] = NULL; + if (cda_size < fd + 1) + return; + crypt_data_array[fd].recv = NULL; + crypt_data_array[fd].send = NULL; + crypt_data_array[fd].private_data = NULL; } @@ -115,14 +128,14 @@ int send_bin_buffer(int fd, const char *buf, size_t len) int ret; crypt_function *cf = NULL; - if (fd <= max_crypt_fd) - cf = crypt_functions[2 * fd + 1]; - if (!len) PARA_CRIT_LOG("%s", "len == 0\n"); + if (fd + 1 <= cda_size) + cf = crypt_data_array[fd].send; if (cf) { + void *private = crypt_data_array[fd].private_data; unsigned char *outbuf = para_malloc(len); - (*cf)(len, (unsigned char *)buf, outbuf); + (*cf)(len, (unsigned char *)buf, outbuf, private); ret = sendall(fd, (char *)outbuf, &len); free(outbuf); } else @@ -183,13 +196,14 @@ __must_check int recv_bin_buffer(int fd, char *buf, ssize_t size) int n; crypt_function *cf = NULL; - if (fd <= max_crypt_fd) - cf = crypt_functions[2 * fd]; + if (fd + 1 <= cda_size) + cf = crypt_data_array[fd].recv; if (cf) { unsigned char *tmp = para_malloc(size); + void *private = crypt_data_array[fd].private_data; n = recv(fd, tmp, size, 0); if (n > 0) - (*cf)(n, tmp, (unsigned char *)buf); + (*cf)(n, tmp, (unsigned char *)buf, private); free(tmp); } else n = recv(fd, buf, size, 0);