-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.cc
313 lines (260 loc) · 6.83 KB
/
connect.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (c) 2008-2011, Adrian Thurston <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "dsnp.h"
#include "error.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
void Parser::parse( const String &data )
{
/* FIXME: deal with results? */
this->data( data.data, data.length );
}
void Parser::parse( const char *data, int length )
{
this->data( data, length );
}
BioWrap::BioWrap( BioWrap::Type type )
:
type(type),
sockFd(-1),
sockBio(0)
{
if ( type == BioWrap::Null )
sockBio = BIO_new( BIO_f_null() );
}
BioWrapParse::BioWrapParse( Config *c, BioWrap::Type type, TlsContext &tlsContext )
:
ConfigCtx(c),
BioWrap(type),
tlsContext(tlsContext),
linelen(4096)
{
input = new char[linelen];
}
void BioWrapParse::setSockFd( int sfd )
{
sockFd = sfd;
sockBio = BIO_new_fd( sfd, BIO_NOCLOSE );
}
void BioWrapParse::makeNonBlocking()
{
makeNonBlocking( sockFd );
}
BioWrapParse::~BioWrapParse()
{
delete[] input;
BIO_free_all( sockBio );
close( sockFd );
}
void BioWrap::write( const char *data, long dlen )
{
if ( sockBio == 0 || sockFd < 0 )
throw WriteOnNullSocketBio();
fd_set readSet, writeSet;
int result, nbytes;
int max = sockFd;
bool needsRead = true;
bool needsWrite = true;
/* FIXME: we assume linux, with TV modified. The harm in assuming this on
* other systems is that timeout can get reset on select interruptions and
* partial reads. Must be fixed. */
timeval tv;
tv.tv_usec = 0;
tv.tv_sec = DAEMON_DAEMON_TIMEOUT;
while ( true ) {
FD_ZERO( &readSet );
FD_ZERO( &writeSet );
if ( needsRead )
FD_SET( sockFd, &readSet );
if ( needsWrite )
FD_SET( sockFd, &writeSet );
result = select( max+1, &readSet, &writeSet, 0, &tv );
if ( result == 0 )
throw DaemonToDaemonTimeout( __FILE__, __LINE__ );
if ( result == EBADFD )
break;
if ( FD_ISSET( sockFd, &readSet ) || FD_ISSET( sockFd, &writeSet ) ) {
while ( true ) {
nbytes = BIO_write( sockBio, data, dlen );
/* break when client closes the connection. */
if ( nbytes <= 0 ) {
/* If the BIO is saying it we should retry later, go back into
* select. */
if ( BIO_should_retry( sockBio ) ) {
needsRead = BIO_should_read(sockBio);
needsWrite = BIO_should_write(sockBio);
break;
}
goto done;
}
goto done;
}
}
}
done:
return;
}
void BioWrap::writeBody( const String &msg )
{
write( msg.data, msg.length );
write( "\r\n", 2 );
}
void BioWrap::returnOkLocal()
{
printf( "OK\r\n" );
}
void BioWrap::returnOkLocal( const String &r1 )
{
printf( "OK %s\r\n", r1() );
}
void BioWrap::returnOkLocal( const String &r1, const String &r2 )
{
printf( "OK %s %s\r\n", r1(), r2() );
}
void BioWrap::returnOkLocal( const String &r1, const String &r2, long ld3 )
{
printf( "OK %s %s %ld\r\n", r1(), r2(), ld3 );
}
void BioWrap::returnOkLocal( const String &r1, const String &r2, const String &r3 )
{
printf( "OK %s %s %s\r\n", r1(), r2(), r3() );
}
void BioWrap::returnOkServer()
{
String ret0 = consRet0();
write( ret0 );
}
void BioWrap::returnOkServer( const String &r1 )
{
String ret1 = consRet1( r1 );
write( ret1 );
}
/* For the error classes that are thrown. These will get written to the content
* manager, and to other DNSP servers processes. */
void BioWrap::returnError( int errorCode )
{
if ( type == Local ) {
printf( "ERROR %d\r\n", errorCode );
}
else if ( type == Server ) {
printf( "ERROR %d\r\n", errorCode );
}
}
void BioWrap::returnError( int errorCode, int arg1 )
{
if ( type == Local ) {
printf( "ERROR %d %d\r\n", errorCode, arg1 );
}
else if ( type == Server ) {
printf( "ERROR %d %d\r\n", errorCode, arg1 );
}
}
void BioWrap::returnError( int errorCode, int arg1, int arg2 )
{
if ( type == Local ) {
printf( "ERROR %d %d %d\r\n", errorCode, arg1, arg2 );
}
else if ( type == Server ) {
printf( "ERROR %d %s\r\n", errorCode, arg1 );
}
}
void BioWrap::returnError( int errorCode, const char *arg1 )
{
if ( type == Local ) {
printf( "ERROR %d %s\r\n", errorCode, arg1 );
}
else if ( type == Server ) {
printf( "ERROR %d %s\r\n", errorCode, arg1 );
}
}
void BioWrap::returnError( int errorCode, const char *arg1, const char *arg2 )
{
if ( type == Local ) {
printf( "ERROR %d %s %s\r\n", errorCode, arg1, arg2 );
}
else if ( type == Server ) {
printf( "ERROR %d %s %s\r\n", errorCode, arg1, arg2 );
}
}
int BioWrap::printf( const char *fmt, ... )
{
va_list args;
char buf[1];
va_start( args, fmt );
long length = vsnprintf( buf, 0, fmt, args );
va_end( args );
if ( length < 0 )
throw WriteError();
char *data = new char[ length+1 ];
va_start( args, fmt );
vsnprintf( data, length+1, fmt, args );
va_end( args );
write( data, length );
delete[] data;
return length;
}
int BioWrapParse::readParse( Parser &parser )
{
fd_set readSet, writeSet;
int result, nbytes;
int max = sockFd;
bool needsRead = true;
bool needsWrite = true;
/* FIXME: we assume linux, with TV modified. The harm in assuming this on
* other systems is that timeout can get reset on select interruptions (eg
* signals). */
timeval tv;
tv.tv_usec = 0;
tv.tv_sec = DAEMON_DAEMON_TIMEOUT;
while ( true ) {
/* Over SSL, there can be reads and writes just to accomplish read. */
FD_ZERO( &readSet );
FD_ZERO( &writeSet );
if ( needsRead )
FD_SET( sockFd, &readSet );
if ( needsWrite )
FD_SET( sockFd, &writeSet );
result = select( max+1, &readSet, &writeSet, 0, &tv );
if ( result == 0 )
throw DaemonToDaemonTimeout( __FILE__, __LINE__ );
if ( result == EBADFD )
break;
if ( FD_ISSET( sockFd, &readSet ) || FD_ISSET( sockFd, &writeSet ) ) {
while ( true ) {
nbytes = BIO_read( sockBio, input, linelen );
/* break when client closes the connection. */
if ( nbytes <= 0 ) {
/* If the BIO is saying it we should retry later, go back into
* select. */
if ( BIO_should_retry( sockBio ) ) {
needsRead = BIO_should_read(sockBio);
needsWrite = BIO_should_write(sockBio);
break;
}
goto done;
}
Parser::Control control = parser.data( input, nbytes );
if ( control == Parser::Stop )
goto done;
}
}
}
done:
return 0;
}