diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bi_sa.m4 | 208 | ||||
-rw-r--r-- | src/dnsbase.c | 4 |
2 files changed, 174 insertions, 38 deletions
diff --git a/src/bi_sa.m4 b/src/bi_sa.m4 index 0ed7472b..8472bbe9 100644 --- a/src/bi_sa.m4 +++ b/src/bi_sa.m4 | |||
@@ -29,7 +29,127 @@ MF_VAR(clamav_virus_name, STRING); | |||
29 | 29 | ||
30 | |||
31 | struct mf_stream { | ||
32 | eval_environ_t env; | ||
33 | mu_stream_t str; | ||
34 | time_t start; | ||
35 | time_t timeout; | ||
36 | }; | ||
37 | |||
38 | typedef struct mf_stream *mf_stream_t; | ||
39 | |||
40 | /* FIXME: duplicated in engine.c */ | ||
41 | #define UPDATE_TTW(t,start) do { \ | ||
42 | time_t delta = time(NULL) - start; \ | ||
43 | if (t > delta) \ | ||
44 | t -= delta; \ | ||
45 | else \ | ||
46 | t = 0; \ | ||
47 | } while (0) | ||
48 | |||
49 | extern int smtp_stream_wait(mu_stream_t stream, int flags, time_t *timeout); | ||
50 | static void spamd_destroy(mf_stream_t *pstream); | ||
51 | |||
52 | mf_stream_t | ||
53 | mf_stream_new (eval_environ_t env, mu_stream_t stream, time_t start, | ||
54 | time_t timeout) | ||
55 | { | ||
56 | mf_stream_t pmfs = xmalloc (sizeof pmfs[0]); | ||
57 | pmfs->env = env; | ||
58 | pmfs->str = stream; | ||
59 | pmfs->start = start; | ||
60 | pmfs->timeout = timeout; | ||
61 | return pmfs; | ||
62 | } | ||
63 | |||
64 | int | ||
65 | mf_stream_sequential_read(mf_stream_t stream, char *buf, size_t size) | ||
66 | { | ||
67 | int rc; | ||
68 | eval_environ_t env = stream->env; | ||
69 | |||
70 | if (stream->timeout == 0) { | ||
71 | spamd_destroy(&stream); | ||
72 | MF_THROW(mf_temp_failure, | ||
73 | "sa_sequential_read: %s", | ||
74 | mu_strerror(ETIMEDOUT)); | ||
75 | } | ||
76 | |||
77 | rc = smtp_stream_wait(stream->str, MU_STREAM_READY_RD, | ||
78 | &stream->timeout); | ||
79 | if (rc == 0) { | ||
80 | UPDATE_TTW(stream->timeout, stream->start); | ||
81 | rc = mu_stream_sequential_read(stream->str, buf, size, NULL); | ||
82 | } else { | ||
83 | spamd_destroy(&stream); | ||
84 | MF_THROW(mf_temp_failure, | ||
85 | "sa_sequential_read: %s", | ||
86 | mu_strerror(ETIMEDOUT)); | ||
87 | } | ||
88 | |||
89 | return rc; | ||
90 | } | ||
91 | |||
92 | int | ||
93 | mf_stream_sequential_readline(mf_stream_t stream, char *buf, size_t size, | ||
94 | size_t *nbytes) | ||
95 | { | ||
96 | int rc; | ||
97 | eval_environ_t env = stream->env; | ||
98 | |||
99 | if (stream->timeout == 0) { | ||
100 | spamd_destroy(&stream); | ||
101 | MF_THROW(mf_temp_failure, | ||
102 | "sa_sequential_readline: %s", | ||
103 | mu_strerror(ETIMEDOUT)); | ||
104 | } | ||
105 | rc = smtp_stream_wait(stream->str, MU_STREAM_READY_RD, | ||
106 | &stream->timeout); | ||
107 | if (rc == 0) { | ||
108 | UPDATE_TTW(stream->timeout, stream->start); | ||
109 | rc = mu_stream_sequential_readline(stream->str, buf, size, | ||
110 | nbytes); | ||
111 | } else { | ||
112 | spamd_destroy(&stream); | ||
113 | MF_THROW(mf_temp_failure, | ||
114 | "sa_sequential_readline: %s", | ||
115 | mu_strerror(ETIMEDOUT)); | ||
116 | } | ||
117 | return rc; | ||
118 | } | ||
119 | |||
120 | int | ||
121 | mf_stream_sequential_write(mf_stream_t stream, char *buf, size_t size) | ||
122 | { | ||
123 | int rc; | ||
124 | eval_environ_t env = stream->env; | ||
125 | |||
126 | if (stream->timeout == 0) { | ||
127 | spamd_destroy(&stream); | ||
128 | MF_THROW(mf_temp_failure, | ||
129 | "sa_sequential_write: %s", | ||
130 | mu_strerror(ETIMEDOUT)); | ||
131 | } | ||
132 | |||
133 | rc = smtp_stream_wait(stream->str, MU_STREAM_READY_WR, | ||
134 | &stream->timeout); | ||
135 | if (rc == 0) { | ||
136 | UPDATE_TTW(stream->timeout, stream->start); | ||
137 | rc = mu_stream_sequential_write(stream->str, buf, size); | ||
138 | } else { | ||
139 | spamd_destroy(&stream); | ||
140 | MF_THROW(mf_temp_failure, | ||
141 | "sa_sequential_write: %s", | ||
142 | mu_strerror(ETIMEDOUT)); | ||
143 | } | ||
144 | return rc; | ||
145 | } | ||
146 | |||
147 | |||
148 | |||
30 | static int | 149 | static int |
31 | spamd_connect_tcp(eval_environ_t env, mu_stream_t *stream, | 150 | spamd_connect_tcp(eval_environ_t env, mf_stream_t *pmfs, |
32 | char *host, int port) | 151 | char *host, int port, time_t timeout) |
33 | { | 152 | { |
34 | int rc = mu_tcp_stream_create(stream, host, port, 0); | 153 | mu_stream_t stream; |
154 | int rc = mu_tcp_stream_create(&stream, host, port, 0); | ||
35 | MF_ASSERT(rc == 0, mf_failure, | 155 | MF_ASSERT(rc == 0, mf_failure, |
@@ -37,3 +157,3 @@ spamd_connect_tcp(eval_environ_t env, mu_stream_t *stream, | |||
37 | mu_strerror(rc)); | 157 | mu_strerror(rc)); |
38 | rc = mu_stream_open(*stream); | 158 | rc = mu_stream_open(stream); |
39 | MF_ASSERT(rc == 0, mf_failure, | 159 | MF_ASSERT(rc == 0, mf_failure, |
@@ -41,2 +161,3 @@ spamd_connect_tcp(eval_environ_t env, mu_stream_t *stream, | |||
41 | mu_strerror(rc)); | 161 | mu_strerror(rc)); |
162 | *pmfs = mf_stream_new(env, stream, time(NULL), timeout); | ||
42 | return rc; | 163 | return rc; |
@@ -45,3 +166,4 @@ spamd_connect_tcp(eval_environ_t env, mu_stream_t *stream, | |||
45 | static int | 166 | static int |
46 | spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path) | 167 | spamd_connect_socket(eval_environ_t env, mf_stream_t *pmfs, char *path, |
168 | time_t timeout) | ||
47 | { | 169 | { |
@@ -50,2 +172,3 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path) | |||
50 | struct sockaddr_un addr; | 172 | struct sockaddr_un addr; |
173 | mu_stream_t stream; | ||
51 | 174 | ||
@@ -65,3 +188,3 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path) | |||
65 | fp = fdopen(fd, "w+"); | 188 | fp = fdopen(fd, "w+"); |
66 | rc = mu_stdio_stream_create(stream, fp, MU_STREAM_RDWR); | 189 | rc = mu_stdio_stream_create(&stream, fp, MU_STREAM_RDWR); |
67 | if (rc) { | 190 | if (rc) { |
@@ -73,5 +196,5 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path) | |||
73 | 196 | ||
74 | rc = mu_stream_open(*stream); | 197 | rc = mu_stream_open(stream); |
75 | if (rc) { | 198 | if (rc) { |
76 | mu_stream_destroy (stream, mu_stream_get_owner (*stream)); | 199 | mu_stream_destroy(&stream, mu_stream_get_owner(stream)); |
77 | fclose(fp); | 200 | fclose(fp); |
@@ -81,2 +204,5 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path) | |||
81 | } | 204 | } |
205 | |||
206 | *pmfs = mf_stream_new(env, stream, time(NULL), timeout); | ||
207 | |||
82 | return rc; | 208 | return rc; |
@@ -85,9 +211,9 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path) | |||
85 | static void | 211 | static void |
86 | spamd_shutdown(mu_stream_t stream, int isfile, int flag) | 212 | spamd_shutdown(mf_stream_t stream, int isfile, int flag) |
87 | { | 213 | { |
88 | int fd; | 214 | int fd; |
89 | |||
90 | mu_transport_t trans; | 215 | mu_transport_t trans; |
91 | mu_stream_flush(stream); | 216 | |
92 | mu_stream_get_transport(stream, &trans); | 217 | mu_stream_flush(stream->str); |
218 | mu_stream_get_transport(stream->str, &trans); | ||
93 | if (isfile) | 219 | if (isfile) |
@@ -100,6 +226,9 @@ spamd_shutdown(mu_stream_t stream, int isfile, int flag) | |||
100 | static void | 226 | static void |
101 | spamd_destroy(mu_stream_t *stream) | 227 | spamd_destroy(mf_stream_t *pstream) |
102 | { | 228 | { |
103 | mu_stream_close(*stream); | 229 | mf_stream_t stream = *pstream; |
104 | mu_stream_destroy(stream, mu_stream_get_owner(*stream)); | 230 | mu_stream_close(stream->str); |
231 | mu_stream_destroy(&stream->str, mu_stream_get_owner(stream->str)); | ||
232 | free(stream); | ||
233 | *pstream = NULL; | ||
105 | } | 234 | } |
@@ -107,3 +236,3 @@ spamd_destroy(mu_stream_t *stream) | |||
107 | static void | 236 | static void |
108 | spamd_send_command(mu_stream_t stream, const char *fmt, ...) | 237 | spamd_send_command(mf_stream_t stream, const char *fmt, ...) |
109 | { | 238 | { |
@@ -116,4 +245,4 @@ spamd_send_command(mu_stream_t stream, const char *fmt, ...) | |||
116 | va_end (ap); | 245 | va_end (ap); |
117 | mu_stream_sequential_write(stream, buf, n); | 246 | mf_stream_sequential_write(stream, buf, n); |
118 | mu_stream_sequential_write(stream, "\r\n", 2); | 247 | mf_stream_sequential_write(stream, "\r\n", 2); |
119 | } | 248 | } |
@@ -121,3 +250,3 @@ spamd_send_command(mu_stream_t stream, const char *fmt, ...) | |||
121 | static void | 250 | static void |
122 | spamd_send_stream(mu_stream_t stream, mu_stream_t instr) | 251 | spamd_send_stream(mf_stream_t stream, mu_stream_t instr) |
123 | { | 252 | { |
@@ -131,3 +260,3 @@ spamd_send_stream(mu_stream_t stream, mu_stream_t instr) | |||
131 | debug3(80,"<< %*.*s", size, size, buf); | 260 | debug3(80,"<< %*.*s", size, size, buf); |
132 | mu_stream_sequential_write (stream, buf, size); | 261 | mf_stream_sequential_write(stream, buf, size); |
133 | } | 262 | } |
@@ -136,6 +265,6 @@ spamd_send_stream(mu_stream_t stream, mu_stream_t instr) | |||
136 | static int | 265 | static int |
137 | spamd_read_line(mu_stream_t stream, char *buffer, size_t size, size_t *pn) | 266 | spamd_read_line(mf_stream_t stream, char *buffer, size_t size, size_t *pn) |
138 | { | 267 | { |
139 | size_t n = 0; | 268 | size_t n = 0; |
140 | int rc = mu_stream_sequential_readline(stream, buffer, size, &n); | 269 | int rc = mf_stream_sequential_readline(stream, buffer, size, &n); |
141 | if (rc == 0) { | 270 | if (rc == 0) { |
@@ -224,4 +353,5 @@ sigpipe_handler (int sig) | |||
224 |