forked from PowerDNS/powermail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.cc
461 lines (360 loc) · 9.71 KB
/
session.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
PowerMail versatile mail receiver
Copyright (C) 2002 PowerDNS.COM BV
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "session.hh"
#include "ahuexception.hh"
#include "misc.hh"
#include <strings.h>
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sstream>
#include <signal.h>
#ifdef __FreeBSD__
# include <sys/uio.h>
#else
# include <sys/sendfile.h>
#endif
void Session::init()
{
d_bufsize=15049;
d_verbose=false;
rdoffset=0;
wroffset=0;
}
void Session::beVerbose()
{
d_verbose=true;
}
Session::Session(int s, struct sockaddr_in r)
{
init();
remote=r;
clisock=s;
}
int Session::close()
{
int rc=0;
if(clisock>=0) {
shutdown(clisock,SHUT_RDWR); // helps against CLOSE_WAIT problems
rc=::close(clisock);
}
clisock=-1;
return rc;
}
Session::~Session()
{
/* NOT CLOSING AUTOMATICALLY ANYMORE!
if(clisock>=0)
::close(clisock);
*/
}
#if 0
//! This function makes a deep copy of Session
Session::Session(const Session &s)
{
d_bufsize=s.d_bufsize;
init(); // needs d_bufsize, but will reset rdoffset & wroffset
rdoffset=s.rdoffset;
wroffset=s.wroffset;
clisock=s.clisock;
remote=s.remote;
}
#endif
Session::Session(const string &dest, int port, int timeout)
{
struct hostent h, *ret;
int herrno;
char buf[256];
gethostbyname_r(dest.c_str(), &h, buf, sizeof(buf), &ret, &herrno);
if(!ret)
throw SessionException("Unable to resolve target name");
if(timeout)
d_timeout=timeout;
doConnect(*(int*)h.h_addr, port);
}
Session::Session(u_int32_t ip, int port, int timeout)
{
if(timeout)
d_timeout=timeout;
doConnect(ip, port);
}
void Session::setTimeout(unsigned int seconds)
{
d_timeout=seconds;
}
// gets called from constructor context, should clean up *everything*
void Session::doConnect(u_int32_t ip, int port)
{
init();
if((clisock=socket(AF_INET,SOCK_STREAM,0))<0) {
throw SessionException("Unable to get a socket: "+stringerror());
}
bzero(&remote,sizeof(remote));
remote.sin_family=AF_INET;
remote.sin_port=htons(port);
remote.sin_addr.s_addr=ip;
int flags=fcntl(clisock,F_GETFL,0);
fcntl(clisock, F_SETFL,flags|O_NONBLOCK);
int err;
if((err=connect(clisock,(struct sockaddr*)&remote,sizeof(remote)))<0 && errno!=EINPROGRESS)
{
::close(clisock);
throw SessionException("connect: "+stringerror());
}
if(!err)
goto done;
fd_set rset,wset;
struct timeval tval;
FD_ZERO(&rset);
FD_SET(clisock, &rset);
wset=rset;
tval.tv_sec=d_timeout;
tval.tv_usec=0;
if(!select(clisock+1,&rset,&wset,0,tval.tv_sec ? &tval : 0))
{
::close(clisock); // timeout
clisock=-1;
errno=ETIMEDOUT;
throw SessionTimeoutException("Timeout connecting to server");
}
if(FD_ISSET(clisock, &rset) || FD_ISSET(clisock, &wset))
{
socklen_t len=sizeof(err);
if(getsockopt(clisock, SOL_SOCKET,SO_ERROR,(char *)&err,&len)<0) {
::close(clisock);
throw SessionException("Error connecting: "+stringerror()); // Solaris
}
if(err) {
::close(clisock);
throw SessionException("Error connecting: "+string(strerror(err)));
}
}
else {
::close(clisock);
throw SessionException("nonblocking connect failed");
}
done:
fcntl(clisock,F_SETFL,flags);
}
bool Session::putLine(const string &s)
{
int length=s.length();
int written=0;
int err;
while(written<length)
{
fd_set wset;
FD_ZERO(&wset);
FD_SET(clisock, &wset);
struct timeval tval;
tval.tv_sec=d_timeout;
tval.tv_usec=0;
if(!select(clisock+1,0,&wset,0,tval.tv_sec ? &tval : 0))
throw SessionTimeoutException("timeout writing line");
if(FD_ISSET(clisock, &wset))
{
socklen_t len=sizeof(err);
if(getsockopt(clisock, SOL_SOCKET,SO_ERROR,(char *) &err,&len)<0)
throw SessionException(+strerror(err)); // Solaris..
if(err)
throw SessionException(strerror(err));
}
else
throw SessionException("nonblocking write failed: "+string(strerror(errno)));
err=write(clisock,s.c_str()+written,length-written);
if(err<0)
throw SessionException("Error writing to remote: "+stringerror());
written+=err;
}
return true;
}
char *strnchr(char *p, char c, int len)
{
int n;
for(n=0;n<len;n++)
if(p[n]==c)
return p+n;
return 0;
}
int Session::timeoutRead(int s, char *buf, size_t len)
{
fd_set rset;
FD_ZERO(&rset);
FD_SET(clisock, &rset);
struct timeval tval;
tval.tv_sec=d_timeout;
tval.tv_usec=0;
int err;
if(!select(clisock+1,&rset,0,0,tval.tv_sec ? &tval : 0))
throw SessionTimeoutException("timeout reading");
if(FD_ISSET(clisock, &rset)) {
socklen_t len=sizeof(err);
if(getsockopt(clisock, SOL_SOCKET,SO_ERROR,(char *)&err,&len)<0)
throw SessionException(strerror(errno)); // Solaris..
if(err)
throw SessionException(strerror(err));
}
else
throw SessionException("nonblocking read failed"+string(strerror(errno)));
return read(s,buf,len);
}
bool
Session::haveLine()
{
return (wroffset!=rdoffset && (strnchr(rdbuf+rdoffset,'\n',wroffset-rdoffset)!=NULL));
}
void Session::getLine(string &line)
{
int bytes;
char *p;
int linelength;
// read data into a buffer
// find first \n, and return that as string, store how far we were
for(;;) {
if(wroffset==rdoffset)
wroffset=rdoffset=0;
if(wroffset!=rdoffset && (p=strnchr(rdbuf+rdoffset,'\n',wroffset-rdoffset))) { // we have a full line in store, return that
// from rdbuf+rdoffset to p should become the new line
linelength=p-(rdbuf+rdoffset);
*p=0; // terminate
line=rdbuf+rdoffset;
line+="\n";
rdoffset+=linelength+1;
return;
}
// we need more data before we can return a line
if(wroffset==d_bufsize) { // buffer is full, flush to left
if(!rdoffset) { // line too long!
throw SessionException("Line too long");
}
memmove(rdbuf,rdbuf+rdoffset,wroffset-rdoffset);
wroffset-=rdoffset;
rdoffset=0;
}
bytes=timeoutRead(clisock,rdbuf+wroffset,d_bufsize-wroffset);
if(bytes<0)
throw SessionException("error on read from socket: "+string(strerror(errno)));
if(bytes==0)
throw SessionException("Connection closed by foreign host");
wroffset+=bytes;
}
// we never get here, but if we do, it's bad
throw SessionException("Unexpected error");
}
int Session::getSocket()
{
return clisock;
}
string Session::getRemote ()
{
ostringstream o;
u_int32_t rint=htonl(remote.sin_addr.s_addr);
o<< (rint>>24 & 0xff)<<".";
o<< (rint>>16 & 0xff)<<".";
o<< (rint>>8 & 0xff)<<".";
o<< (rint & 0xff);
o<<":"<<htons(remote.sin_port);
return o.str();
}
u_int32_t Session::getRemoteAddr()
{
return htonl(remote.sin_addr.s_addr);
}
string Session::getRemoteIP()
{
ostringstream o;
u_int32_t rint=htonl(remote.sin_addr.s_addr);
o<< (rint>>24 & 0xff)<<".";
o<< (rint>>16 & 0xff)<<".";
o<< (rint>>8 & 0xff)<<".";
o<< (rint & 0xff);
return o.str();
}
Session *Server::accept()
{
struct sockaddr_in remote;
socklen_t len=sizeof(remote);
int clisock=-1;
while((clisock=::accept(s,(struct sockaddr *)(&remote),&len))==-1) // repeat until we have a succesful connect
{
// L<<Logger::Error<<"accept() returned: "<<strerror(errno)<<endl;
if(errno==EMFILE) {
throw SessionException("Out of file descriptors - won't recover from that");
}
}
return new Session(clisock, remote);
}
Server::Server(int p, const string &p_localaddress)
{
d_localaddress="0.0.0.0";
string localaddress=p_localaddress;
port=p;
struct sockaddr_in local;
s=socket(AF_INET,SOCK_STREAM,0);
if(s<0)
{
throw Exception(string("socket: ")+strerror(errno));
}
bzero(&local,sizeof(local));
local.sin_family=AF_INET;
struct hostent *h;
if(localaddress=="")
localaddress=d_localaddress;
h=gethostbyname(localaddress.c_str());
if(!h)
throw Exception();
local.sin_addr.s_addr=*(int*)h->h_addr;
local.sin_port=htons(port);
int tmp=1;
if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0)
throw SessionException(string("Setsockopt failed: ")+strerror(errno));
if(bind(s, (sockaddr*)&local,sizeof(local))<0)
throw SessionException("binding to port "+itoa(port)+string(": ")+strerror(errno));
if(listen(s,128)<0)
throw SessionException("listen: "+stringerror());
}
bool Session::sendFile(int fd)
{
#ifndef __FreeBSD__
off_t offset=0;
#endif
struct stat buf;
fstat(fd, &buf);
#ifdef __FreeBSD__
sendfile(fd, clisock, 0, buf.st_size, NULL, NULL, 0);
#else
sendfile(clisock,fd, &offset, buf.st_size);
#endif
return 0;
}
int writen(int fd, const void* ptr, size_t size)
{
size_t done=0;
int ret;
while(size!=done) {
ret=write(fd,(const char*)ptr+done,size-done);
if(ret==0) {
return 0;
}
if(ret<0)
throw SessionException("Error writing line to socket: "+string(strerror(errno)));
done+=ret;
}
return size;
}