~ubuntu-branches/ubuntu/raring/dansguardian/raring

« back to all changes in this revision

Viewing changes to src/BaseSocket.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt, Thomas Viehmann
  • Date: 2008-10-13 09:29:35 UTC
  • mfrom: (1.1.7 upstream) (7.1.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20081013092935-g8uz3kgfjs37ikgv
Tags: 2.9.9.7-2
[ Thomas Viehmann ]
OptionContainer.cpp: If you need to iterate through all lines in the
config file to find a field, at least don't abuse the configfile deque
by accessing it as if it was an array with signed index.
Works way better on arm, too, i.e. closes: #493047.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
// INCLUDES
21
21
 
22
 
#include "BaseSocket.hpp"
 
22
#ifdef HAVE_CONFIG_H
 
23
        #include "dgconfig.h"
 
24
#endif
23
25
 
24
26
#include <csignal>
25
27
#include <fcntl.h>
29
31
#include <unistd.h>
30
32
#include <stdexcept>
31
33
#include <syslog.h>
 
34
#include <sys/select.h>
32
35
 
33
36
#ifdef DGDEBUG
34
37
#include <iostream>
35
38
#endif
36
39
 
 
40
#include "BaseSocket.hpp"
 
41
 
37
42
// GLOBALS
38
43
extern bool reloadconfig;
39
44
 
214
219
}
215
220
 
216
221
// blocking check for waiting data - blocks for up to given timeout, can be told to break on signal-triggered config reloads
217
 
void BaseSocket::checkForInput(int timeout, bool honour_reloadconfig) throw(exception)
 
222
void BaseSocket::checkForInput(int timeout, bool honour_reloadconfig) throw(std::exception)
218
223
{
219
224
        if ((bufflen - buffstart) > 0)
220
225
                return;
227
232
        t.tv_sec = timeout;
228
233
        t.tv_usec = 0;
229
234
        if (selectEINTR(sck + 1, &fdSet, NULL, NULL, &t, honour_reloadconfig) < 1) {
230
 
                string err("select() on input: ");
231
 
                throw runtime_error(err + (errno ? strerror(errno) : "timeout"));
 
235
                std::string err("select() on input: ");
 
236
                throw std::runtime_error(err + (errno ? strerror(errno) : "timeout"));
232
237
        }
233
238
}
234
239
 
248
253
}
249
254
 
250
255
// blocking equivalent of above, can be told to break on signal-triggered reloads
251
 
void BaseSocket::readyForOutput(int timeout, bool honour_reloadconfig) throw(exception)
 
256
void BaseSocket::readyForOutput(int timeout, bool honour_reloadconfig) throw(std::exception)
252
257
{
253
258
        // blocks if socket blocking
254
259
        // until timeout
259
264
        t.tv_sec = timeout;
260
265
        t.tv_usec = 0;
261
266
        if (selectEINTR(sck + 1, NULL, &fdSet, NULL, &t, honour_reloadconfig) < 1) {
262
 
                string err("select() on output: ");
263
 
                throw runtime_error(err + (errno ? strerror(errno) : "timeout"));
 
267
                std::string err("select() on output: ");
 
268
                throw std::runtime_error(err + (errno ? strerror(errno) : "timeout"));
264
269
        }
265
270
}
266
271
 
267
272
// read a line from the socket, can be told to break on config reloads
268
 
int BaseSocket::getLine(char *buff, int size, int timeout, bool honour_reloadconfig, bool *chopped) throw(exception)
 
273
int BaseSocket::getLine(char *buff, int size, int timeout, bool honour_reloadconfig, bool *chopped) throw(std::exception)
269
274
{
270
275
        // first, return what's left from the previous buffer read, if anything
271
276
        int i = 0;
293
298
                bufflen = 0;
294
299
                try {
295
300
                        checkForInput(timeout, honour_reloadconfig);
296
 
                } catch(exception & e) {
297
 
                        throw runtime_error(string("Can't read from socket: ") + strerror(errno));  // on error
 
301
                } catch(std::exception & e) {
 
302
                        throw std::runtime_error(std::string("Can't read from socket: ") + strerror(errno));  // on error
298
303
                }
299
304
                bufflen = recv(sck, buffer, 1024, 0);
300
305
#ifdef DGDEBUG
304
309
                        if (errno == EINTR && (honour_reloadconfig ? !reloadconfig : true)) {
305
310
                                continue;
306
311
                        }
307
 
                        throw runtime_error(string("Can't read from socket: ") + strerror(errno));  // on error
 
312
                        throw std::runtime_error(std::string("Can't read from socket: ") + strerror(errno));  // on error
308
313
                }
309
314
                //if socket closed or newline received...
310
315
                if (bufflen == 0) {
331
336
}
332
337
 
333
338
// write line to socket
334
 
void BaseSocket::writeString(const char *line) throw(exception)
 
339
void BaseSocket::writeString(const char *line) throw(std::exception)
335
340
{
336
341
        int l = strlen(line);
337
342
        if (!writeToSocket(line, l, 0, timeout)) {
338
 
                throw runtime_error(string("Can't write to socket: ") + strerror(errno));
 
343
                throw std::runtime_error(std::string("Can't write to socket: ") + strerror(errno));
339
344
        }
340
345
}
341
346
 
342
347
// write data to socket - throws exception on failure, can be told to break on config reloads
343
 
void BaseSocket::writeToSockete(const char *buff, int len, unsigned int flags, int timeout, bool honour_reloadconfig) throw(exception)
 
348
void BaseSocket::writeToSockete(const char *buff, int len, unsigned int flags, int timeout, bool honour_reloadconfig) throw(std::exception)
344
349
{
345
350
        if (!writeToSocket(buff, len, flags, timeout, honour_reloadconfig)) {
346
 
                throw runtime_error(string("Can't write to socket: ") + strerror(errno));
 
351
                throw std::runtime_error(std::string("Can't write to socket: ") + strerror(errno));
347
352
        }
348
353
}
349
354
 
357
362
                        try {
358
363
                                readyForOutput(timeout, honour_reloadconfig);  // throws exception on error or timeout
359
364
                        }
360
 
                        catch(exception & e) {
 
365
                        catch(std::exception & e) {
361
366
                                return false;
362
367
                        }
363
368
                }
402
407
                try {
403
408
                        checkForInput(timeout);  // throws exception on error or timeout
404
409
                }
405
 
                catch(exception & e) {
 
410
                catch(std::exception & e) {
406
411
                        return -1;
407
412
                }
408
413
                rc = recv(sck, buff, cnt, flags);
446
451
        if (check_first) {
447
452
                try {
448
453
                        checkForInput(timeout, honour_reloadconfig);
449
 
                } catch(exception & e) {
 
454
                } catch(std::exception & e) {
450
455
                        return -1;
451
456
                }
452
457
        }