Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 81 lines (73 sloc) 2.645 kb
a2572951 »
2011-06-13 Add a hook to connect()
1
2 #define DEFINE_HOOK_GLOBALS 1
3 #define DONT_BYPASS_HOOKS 1
4
5 #include "common.h"
6 #include "filter.h"
2556a909 »
2011-06-14 Actually, connect() and bind() are not that similar. In particular, w…
7 #include "hook-connect.h"
a2572951 »
2011-06-13 Add a hook to connect()
8
9 int (* __real_connect)(int fd, const struct sockaddr *sa, socklen_t sa_len);
10
e79a7333 »
2011-06-20 Refactor. Unfortunately there's a bug somewhere.
11 static FilterReplyResult filter_parse_reply(FilterReplyResultBase * const rb,
fe476962 »
2011-06-14 Add the ability to alter the remote host and port on connect().
12 struct sockaddr_storage * const sa,
2556a909 »
2011-06-14 Actually, connect() and bind() are not that similar. In particular, w…
13 socklen_t * const sa_len)
a2572951 »
2011-06-13 Add a hook to connect()
14 {
e79a7333 »
2011-06-20 Refactor. Unfortunately there's a bug somewhere.
15 msgpack_unpacked * const message = filter_receive_message(rb->filter);
a2572951 »
2011-06-13 Add a hook to connect()
16 const msgpack_object_map * const map = &message->data.via.map;
e79a7333 »
2011-06-20 Refactor. Unfortunately there's a bug somewhere.
17 FilterReplyResult reply_result = filter_parse_common_reply_map(rb, map);
02855100 »
2011-06-17 Add recvfrom
18 filter_overwrite_sa_with_reply_map(map, "remote_host", "remote_port",
19 sa, sa_len);
578ab5cb »
2011-06-14 bypass=true in a filter reply can return a result without actually ma…
20 return reply_result;
a2572951 »
2011-06-13 Add a hook to connect()
21 }
22
e79a7333 »
2011-06-20 Refactor. Unfortunately there's a bug somewhere.
23 static FilterReplyResult filter_apply(FilterReplyResultBase * const rb,
fe93bb13 »
2011-06-14 Filter replies can now either return an error, a "pass" or a "bypass"…
24 struct sockaddr_storage * const sa,
2556a909 »
2011-06-14 Actually, connect() and bind() are not that similar. In particular, w…
25 socklen_t * const sa_len)
a2572951 »
2011-06-13 Add a hook to connect()
26 {
3254cd24 »
2011-06-16 Introduce get_sock_info() to get local/remote addresses in hooks.
27 struct sockaddr_storage sa_local, *sa_local_ = &sa_local;
28 socklen_t sa_local_len;
e3e69091 »
2011-06-21 Fix 0 vs NULL.
29 get_sock_info(rb->fd, &sa_local_, &sa_local_len, NULL, NULL);
e79a7333 »
2011-06-20 Refactor. Unfortunately there's a bug somewhere.
30 filter_before_apply(rb, 0U, "connect",
3254cd24 »
2011-06-16 Introduce get_sock_info() to get local/remote addresses in hooks.
31 sa_local_, sa_local_len, sa, *sa_len);
e79a7333 »
2011-06-20 Refactor. Unfortunately there's a bug somewhere.
32 if (filter_send_message(rb->filter) != 0) {
242acc52 »
2011-06-19 Make all properties from filters optional.
33 return FILTER_REPLY_RESULT_ERROR;
a2572951 »
2011-06-13 Add a hook to connect()
34 }
e79a7333 »
2011-06-20 Refactor. Unfortunately there's a bug somewhere.
35 return filter_parse_reply(rb, sa, sa_len);
a2572951 »
2011-06-13 Add a hook to connect()
36 }
37
38 int __real_connect_init(void)
39 {
40 #ifdef USE_INTERPOSERS
41 __real_connect = connect;
42 #else
43 if (__real_connect == NULL) {
44 __real_connect = dlsym(RTLD_NEXT, "connect");
45 assert(__real_connect != NULL);
46 }
47 #endif
48 return 0;
49 }
50
51 int INTERPOSE(connect)(int fd, const struct sockaddr *sa, socklen_t sa_len)
52 {
53 __real_connect_init();
54 const bool bypass_filter = getenv("SIXJACK_BYPASS") != NULL;
69430084 »
2011-06-14 Prepare for PRE and POST filters.
55 int ret = 0;
56 int ret_errno = 0;
57 bool bypass_call = false;
58 struct sockaddr_storage sa_;
59 socklen_t sa_len_ = sa_len;
60 assert(sa_len <= sizeof sa_);
61 memcpy(&sa_, sa, sa_len);
e79a7333 »
2011-06-20 Refactor. Unfortunately there's a bug somewhere.
62 FilterReplyResultBase rb = {
e12719c1 »
2011-06-20 Only call get_filter() if we don't bypass the filter.
63 .pre = true, .ret = &ret, .ret_errno = &ret_errno, .fd = fd
e79a7333 »
2011-06-20 Refactor. Unfortunately there's a bug somewhere.
64 };
e12719c1 »
2011-06-20 Only call get_filter() if we don't bypass the filter.
65 if (bypass_filter == false && (rb.filter = filter_get()) &&
e79a7333 »
2011-06-20 Refactor. Unfortunately there's a bug somewhere.
66 filter_apply(&rb, &sa_, &sa_len_)
578ab5cb »
2011-06-14 bypass=true in a filter reply can return a result without actually ma…
67 == FILTER_REPLY_BYPASS) {
68 bypass_call = true;
69430084 »
2011-06-14 Prepare for PRE and POST filters.
69 }
70 if (bypass_call == false) {
71 ret = __real_connect(fd, (struct sockaddr *) &sa_, sa_len_);
72 ret_errno = errno;
73 }
74 if (bypass_filter == false) {
e79a7333 »
2011-06-20 Refactor. Unfortunately there's a bug somewhere.
75 rb.pre = false;
76 filter_apply(&rb, &sa_, &sa_len_);
2d4e52d4 »
2011-06-14 bind() and connect() are pretty similar, so implement a bind() hook.
77 }
78 errno = ret_errno;
79
80 return ret;
81 }
Something went wrong with that request. Please try again.