summaryrefslogtreecommitdiff
path: root/src/lbtest_ttl.hh
blob: 8f2035f1352ebbf6c99d65a6761e2f9f42c7fdc5 (plain)
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
/*
 * Module: lbtest_ttl.hh
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation.
 */
#ifndef __LBTEST_TTL_HH__
#define __LBTEST_TTL_HH__

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <iostream>
#include "lbdata.hh"

using namespace std;

class LBTestTTL;

/**
 *
 *
 **/
class TTLEngine
{
  class PktData
  {
  public:
    PktData(string iface, int rtt) : _iface(iface),_rtt(rtt) {}
    string _iface;
    int _rtt;
  };

public:
  TTLEngine() : 
    _debug(true),
    _initialized(false),
    _packet_id(32767),
    _min_port_id(32767),
    _max_port_id(55000)
  {}

  void
  init();

  int
  process(LBHealth &health,LBTestTTL *data);

  int
  recv(LBHealth &health,LBTestTTL *data);

private:
  void
  send(int sock, const string &iface, const string &target_addr, unsigned short packet_id, string address, int ttl, unsigned short port);

  int
  receive(int sock);

  unsigned short
  udp_checksum(unsigned char ucProto, char *pPacket, int iLength, unsigned long ulSourceAddress, unsigned long ulDestAddress);

  unsigned short
  in_checksum(unsigned short *pAddr, int iLen);

  unsigned short
  get_new_packet_id();

private:
  bool _debug;
  bool _initialized;
  unsigned short _packet_id;
  unsigned short _min_port_id;
  unsigned long _max_port_id;

  map<int,PktData> _results;
};

/**
 *
 *
 **/
class LBTestTTL : public LBTest 
{
public:
  LBTestTTL(bool debug) : 
    LBTest(debug),
    _ttl(0),
    _port(0)
  {}
  LBTestTTL(bool debug, unsigned short ttl, unsigned short port) : 
    LBTest(debug),
    _ttl(ttl),
    _port(port)
  {}
  ~LBTestTTL() {}

  void
  init() {_engine.init();this->LBTest::init();}

  void
  send(LBHealth &health) {_engine.process(health,this);}

  int
  recv(LBHealth &health) {return _engine.recv(health,this);}

  unsigned short
  get_ttl() const {return _ttl;}

  unsigned short
  get_port() const {return _port;}

  void
  set_ttl(unsigned short ttl) {_ttl = ttl;}

  void
  set_port(unsigned short port) {_port = port;}

  string
  dump();

private:
  static TTLEngine _engine; //singleton
  bool _debug;
  unsigned short _ttl;
  unsigned short _port;
};

#endif //__LBTEST_TTL_HH__