Skip to content

Commit

Permalink
Refactor. Unfortunately there's a bug somewhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
jedisct1 committed Jun 19, 2011
1 parent 9cc51f4 commit e79a733
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 216 deletions.
27 changes: 12 additions & 15 deletions src/filter.c
Expand Up @@ -48,10 +48,8 @@ msgpack_unpacked *filter_receive_message(Filter * const filter)
return message;
}

FilterReplyResult filter_parse_common_reply_map(const msgpack_object_map * const map,
int * const ret,
int * const ret_errno,
const int fd)
FilterReplyResult filter_parse_common_reply_map(FilterReplyResultBase * const rb,
const msgpack_object_map * const map)
{
const msgpack_object * const obj_version =
msgpack_get_map_value_for_key(map, "version");
Expand All @@ -66,7 +64,7 @@ FilterReplyResult filter_parse_common_reply_map(const msgpack_object_map * const
obj_return_code->type == MSGPACK_OBJECT_NEGATIVE_INTEGER);
const int64_t new_return_code = obj_return_code->via.i64;
assert(new_return_code >= INT_MIN && new_return_code <= INT_MAX);
*ret = new_return_code;
*rb->ret = new_return_code;
}

const msgpack_object * const obj_ret_errno =
Expand All @@ -76,7 +74,7 @@ FilterReplyResult filter_parse_common_reply_map(const msgpack_object_map * const
obj_ret_errno->type == MSGPACK_OBJECT_NEGATIVE_INTEGER);
const int64_t new_ret_errno = obj_ret_errno->via.i64;
assert(new_ret_errno >= INT_MIN && new_ret_errno <= INT_MAX);
*ret_errno = new_ret_errno;
*rb->ret_errno = new_ret_errno;
}

const msgpack_object * const obj_force_close =
Expand All @@ -85,7 +83,7 @@ FilterReplyResult filter_parse_common_reply_map(const msgpack_object_map * const
assert(obj_force_close->type == MSGPACK_OBJECT_BOOLEAN);
const bool force_close = obj_force_close->via.boolean;
if (force_close != false) {
close(fd);
close(rb->fd);
}
}

Expand All @@ -101,8 +99,7 @@ FilterReplyResult filter_parse_common_reply_map(const msgpack_object_map * const
return FILTER_REPLY_PASS;
}

int filter_before_apply(const bool pre,
const int ret, const int ret_errno, const int fd,
int filter_before_apply(FilterReplyResultBase * const rb,
const unsigned int nongeneric_items,
const char * const function,
const struct sockaddr_storage * const sa_local,
Expand All @@ -117,7 +114,7 @@ int filter_before_apply(const bool pre,
msgpack_packer_init(msgpack_packer, filter->msgpack_sbuffer,
msgpack_sbuffer_write);
unsigned int items_count = nongeneric_items + 5U;
if (pre == false) {
if (rb->pre == false) {
items_count += 2U;
}
if (sa_local != NULL) {
Expand All @@ -132,7 +129,7 @@ int filter_before_apply(const bool pre,
msgpack_pack_unsigned_short(msgpack_packer, VERSION_MAJOR);

msgpack_pack_mstring(msgpack_packer, "filter_type");
msgpack_pack_cstring(msgpack_packer, pre ? "PRE" : "POST");
msgpack_pack_cstring(msgpack_packer, rb->pre ? "PRE" : "POST");

msgpack_pack_mstring(msgpack_packer, "pid");
msgpack_pack_unsigned_int(msgpack_packer, context->pid);
Expand All @@ -141,14 +138,14 @@ int filter_before_apply(const bool pre,
msgpack_pack_cstring(msgpack_packer, function);

msgpack_pack_mstring(msgpack_packer, "fd");
msgpack_pack_int(msgpack_packer, fd);
msgpack_pack_int(msgpack_packer, rb->fd);

if (pre == false) {
if (rb->pre == false) {
msgpack_pack_mstring(msgpack_packer, "return_code");
msgpack_pack_int(msgpack_packer, ret);
msgpack_pack_int(msgpack_packer, *rb->ret);

msgpack_pack_mstring(msgpack_packer, "errno");
msgpack_pack_int(msgpack_packer, ret_errno);
msgpack_pack_int(msgpack_packer, *rb->ret_errno);
}

char host[NI_MAXHOST];
Expand Down
17 changes: 11 additions & 6 deletions src/filter.h
Expand Up @@ -25,10 +25,17 @@ typedef enum FilterReplyResult_ {
FILTER_REPLY_BYPASS = 1
} FilterReplyResult;

typedef struct FilterReplyResultBase_ {
bool pre;
Filter * const filter;
int * const ret;
int * const ret_errno;
int fd;
} FilterReplyResultBase;

Filter *filter_get(void);

int filter_before_apply(const bool pre,
const int ret, const int ret_errno, const int fd,
int filter_before_apply(FilterReplyResultBase * const rb,
const unsigned int nongeneric_items,
const char * const function,
const struct sockaddr_storage * const sa_local,
Expand All @@ -40,10 +47,8 @@ int filter_send_message(Filter * const filter);

msgpack_unpacked *filter_receive_message(Filter * const filter);

FilterReplyResult filter_parse_common_reply_map(const msgpack_object_map * const map,
int * const ret,
int * const ret_errno,
const int fd);
FilterReplyResult filter_parse_common_reply_map(FilterReplyResultBase * const rb,
const msgpack_object_map * const map);

int filter_overwrite_sa_with_reply_map(const msgpack_object_map * const map,
const char * const key_host,
Expand Down
31 changes: 14 additions & 17 deletions src/hook-bind.c
Expand Up @@ -8,17 +8,13 @@

int (* __real_bind)(int fd, const struct sockaddr *sa, socklen_t sa_len);

static FilterReplyResult filter_parse_reply(Filter * const filter,
int * const ret,
int * const ret_errno,
const int fd,
static FilterReplyResult filter_parse_reply(FilterReplyResultBase * const rb,
struct sockaddr_storage * const sa,
socklen_t * const sa_len)
{
msgpack_unpacked * const message = filter_receive_message(filter);
msgpack_unpacked * const message = filter_receive_message(rb->filter);
const msgpack_object_map * const map = &message->data.via.map;
FilterReplyResult reply_result =
filter_parse_common_reply_map(map, ret, ret_errno, fd);
FilterReplyResult reply_result = filter_parse_common_reply_map(rb, map);

const msgpack_object * const obj_local_host =
msgpack_get_map_value_for_key(map, "local_host");
Expand Down Expand Up @@ -77,18 +73,15 @@ static FilterReplyResult filter_parse_reply(Filter * const filter,
return reply_result;
}

static FilterReplyResult filter_apply(const bool pre, int * const ret,
int * const ret_errno, const int fd,
static FilterReplyResult filter_apply(FilterReplyResultBase * const rb,
struct sockaddr_storage * const sa,
socklen_t * const sa_len)
{
Filter * const filter = filter_get();
filter_before_apply(pre, *ret, *ret_errno, fd, 0U, "bind",
sa, *sa_len, NULL, (socklen_t) 0U);
if (filter_send_message(filter) != 0) {
filter_before_apply(rb, 0U, "bind", sa, *sa_len, NULL, (socklen_t) 0U);
if (filter_send_message(rb->filter) != 0) {
return FILTER_REPLY_RESULT_ERROR;
}
return filter_parse_reply(filter, ret, ret_errno, fd, sa, sa_len);
return filter_parse_reply(rb, sa, sa_len);
}

int __real_bind_init(void)
Expand All @@ -115,17 +108,21 @@ int INTERPOSE(bind)(int fd, const struct sockaddr *sa, socklen_t sa_len)
socklen_t sa_len_ = sa_len;
assert(sa_len <= sizeof sa_);
memcpy(&sa_, sa, sa_len);
FilterReplyResultBase rb = {
.pre = true,
.filter = filter_get(), .ret = &ret, .ret_errno = &ret_errno, .fd = fd,
};
if (bypass_filter == false &&
filter_apply(true, &ret, &ret_errno, fd, &sa_, &sa_len_)
== FILTER_REPLY_BYPASS) {
filter_apply(&rb, &sa_, &sa_len_) == FILTER_REPLY_BYPASS) {
bypass_call = true;
}
if (bypass_call == false) {
ret = __real_bind(fd, (struct sockaddr *) &sa_, sa_len_);
ret_errno = errno;
}
if (bypass_filter == false) {
filter_apply(false, &ret, &ret_errno, fd, &sa_, &sa_len_);
rb.pre = false;
filter_apply(&rb, &sa_, &sa_len_);
}
errno = ret_errno;

Expand Down
35 changes: 15 additions & 20 deletions src/hook-close.c
Expand Up @@ -8,35 +8,27 @@

int (* __real_close)(int fd);

static FilterReplyResult filter_parse_reply(Filter * const filter,
int * const ret,
int * const ret_errno,
const int fd)
static FilterReplyResult filter_parse_reply(FilterReplyResultBase * const rb)
{
msgpack_unpacked * const message = filter_receive_message(filter);
msgpack_unpacked * const message = filter_receive_message(rb->filter);
const msgpack_object_map * const map = &message->data.via.map;
FilterReplyResult reply_result =
filter_parse_common_reply_map(map, ret, ret_errno, fd);
FilterReplyResult reply_result = filter_parse_common_reply_map(rb, map);

return reply_result;
}

static FilterReplyResult filter_apply(const bool pre,
int * const ret, int * const ret_errno,
const int fd,
static FilterReplyResult filter_apply(FilterReplyResultBase * const rb,
const struct sockaddr_storage * const sa_local,
const socklen_t sa_local_len,
const struct sockaddr_storage * const sa_remote,
const socklen_t sa_remote_len)
{
Filter * const filter = filter_get();
filter_before_apply(pre, *ret, *ret_errno, fd, 0U, "close",
sa_local, sa_local_len, sa_remote, sa_remote_len);

if (filter_send_message(filter) != 0) {
filter_before_apply(rb, 0U, "close",
sa_local, sa_local_len, sa_remote, sa_remote_len);
if (filter_send_message(rb->filter) != 0) {
return FILTER_REPLY_RESULT_ERROR;
}
return filter_parse_reply(filter, ret, ret_errno, fd);
return filter_parse_reply(rb);
}

int __real_close_init(void)
Expand Down Expand Up @@ -64,9 +56,12 @@ int INTERPOSE(close)(int fd)
int ret = 0;
int ret_errno = 0;
bool bypass_call = false;
FilterReplyResultBase rb = {
.pre = true,
.filter = filter_get(), .ret = &ret, .ret_errno = &ret_errno, .fd = fd,
};
if (bypass_filter == false &&
filter_apply(true, &ret, &ret_errno, fd,
sa_local_, sa_local_len, sa_remote_, sa_remote_len)
filter_apply(&rb, sa_local_, sa_local_len, sa_remote_, sa_remote_len)
== FILTER_REPLY_BYPASS) {
bypass_call = true;
}
Expand All @@ -75,8 +70,8 @@ int INTERPOSE(close)(int fd)
ret_errno = errno;
}
if (bypass_filter == false) {
filter_apply(false, &ret, &ret_errno, fd,
sa_local_, sa_local_len, sa_remote_, sa_remote_len);
rb.pre = false;
filter_apply(&rb, sa_local_, sa_local_len, sa_remote_, sa_remote_len);
}
errno = ret_errno;

Expand Down
31 changes: 15 additions & 16 deletions src/hook-connect.c
Expand Up @@ -8,37 +8,31 @@

int (* __real_connect)(int fd, const struct sockaddr *sa, socklen_t sa_len);

static FilterReplyResult filter_parse_reply(Filter * const filter,
int * const ret,
int * const ret_errno,
const int fd,
static FilterReplyResult filter_parse_reply(FilterReplyResultBase * const rb,
struct sockaddr_storage * const sa,
socklen_t * const sa_len)
{
msgpack_unpacked * const message = filter_receive_message(filter);
msgpack_unpacked * const message = filter_receive_message(rb->filter);
const msgpack_object_map * const map = &message->data.via.map;
FilterReplyResult reply_result =
filter_parse_common_reply_map(map, ret, ret_errno, fd);
FilterReplyResult reply_result = filter_parse_common_reply_map(rb, map);
filter_overwrite_sa_with_reply_map(map, "remote_host", "remote_port",
sa, sa_len);
return reply_result;
}

static FilterReplyResult filter_apply(const bool pre, int * const ret,
int * const ret_errno, const int fd,
static FilterReplyResult filter_apply(FilterReplyResultBase * const rb,
struct sockaddr_storage * const sa,
socklen_t * const sa_len)
{
Filter * const filter = filter_get();
struct sockaddr_storage sa_local, *sa_local_ = &sa_local;
socklen_t sa_local_len;
get_sock_info(fd, &sa_local_, &sa_local_len, NULL, (socklen_t) 0U);
filter_before_apply(pre, *ret, *ret_errno, fd, 0U, "connect",
get_sock_info(rb->fd, &sa_local_, &sa_local_len, NULL, (socklen_t) 0U);
filter_before_apply(rb, 0U, "connect",
sa_local_, sa_local_len, sa, *sa_len);
if (filter_send_message(filter) != 0) {
if (filter_send_message(rb->filter) != 0) {
return FILTER_REPLY_RESULT_ERROR;
}
return filter_parse_reply(filter, ret, ret_errno, fd, sa, sa_len);
return filter_parse_reply(rb, sa, sa_len);
}

int __real_connect_init(void)
Expand All @@ -65,8 +59,12 @@ int INTERPOSE(connect)(int fd, const struct sockaddr *sa, socklen_t sa_len)
socklen_t sa_len_ = sa_len;
assert(sa_len <= sizeof sa_);
memcpy(&sa_, sa, sa_len);
FilterReplyResultBase rb = {
.pre = true,
.filter = filter_get(), .ret = &ret, .ret_errno = &ret_errno, .fd = fd,
};
if (bypass_filter == false &&
filter_apply(true, &ret, &ret_errno, fd, &sa_, &sa_len_)
filter_apply(&rb, &sa_, &sa_len_)
== FILTER_REPLY_BYPASS) {
bypass_call = true;
}
Expand All @@ -75,7 +73,8 @@ int INTERPOSE(connect)(int fd, const struct sockaddr *sa, socklen_t sa_len)
ret_errno = errno;
}
if (bypass_filter == false) {
filter_apply(false, &ret, &ret_errno, fd, &sa_, &sa_len_);
rb.pre = false;
filter_apply(&rb, &sa_, &sa_len_);
}
errno = ret_errno;

Expand Down

0 comments on commit e79a733

Please sign in to comment.