summaryrefslogtreecommitdiff
path: root/src/lbtest_ttl.cc
blob: 843bc57b016842936041ec5953a607f8da79b3a5 (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
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
/*
 * Module: lbpathtest.cc
 *
 * 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.
 */
#include <syslog.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/udp.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <errno.h>
#include <string.h>
#include <memory>
#include <time.h>
#include <sys/timeb.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <algorithm>

#include "lbdata.hh"
#include "lbtest_ttl.hh"

TTLEngine LBTestTTL::_engine;

using namespace std;

/**
 *
 *
 **/
void
TTLEngine::init() 
{
  if (_debug) {
    cout << "LBTestICMP::init(): initializing test system" << endl;
  }
  _results.erase(_results.begin(),_results.end());
}

/**
 *
 *
 **/
int
TTLEngine::process(LBHealth &health,LBTestTTL *data)
{
  if (_debug) {
    cout << "TTLEngine::process()" << endl;
  }
  //iterate over packets and send
  string target = data->_target;
  if (target.empty()) {
    if (health._nexthop == "dhcp") {
      target = health._dhcp_nexthop;
    }
    else {
      target = health._nexthop;
    }
  }
  
  //don't have target yet...
  if (target.empty()) {
    return -1;
  }
  if (_debug) {
    cout << "TTLEngine::process(): sending ttl test for: " << health._interface << " for " << target << endl;
  }
  _packet_id = get_new_packet_id();
    
  send(data->_send_raw_sock, health._interface,target,_packet_id,health._address,data->get_ttl(),data->get_port());
  _results.insert(pair<int,PktData>(_packet_id,PktData(health._interface,-1)));
}

/**
 *
 *
 **/
int
TTLEngine::recv(LBHealth &health,LBTestTTL *data) 
{
  if (_results.empty() == true) {
    return -1;
  }
  
  if (_debug) {
    cout << "TTLEngine::recv()" << endl;
  }
  struct timeval send_time;
  gettimeofday(&send_time,NULL);
  
  //use gettimeofday to calculate time to millisecond
  //then iterate over recv socket and receive and record
  //use sysinfo to make sure we don't get stuck in a loop with timechange
  struct sysinfo si;
  sysinfo(&si);
  //for now hardcode to 5 second overall timeout
  unsigned long timeout = si.uptime + 5; //seconds
  unsigned long cur_time = si.uptime;
  while (cur_time < timeout) {
    int id = receive(data->_recv_icmp_sock);
    if (_debug) {
      cout << "TTLEngine::recv(): " << id << endl;
    }
    //update current time for comparison
    struct sysinfo si;
    sysinfo(&si);
    timeval recv_time;
    gettimeofday(&recv_time,NULL);
    cur_time = si.uptime;
    map<int,PktData>::iterator r_iter = _results.find(id);
    if (r_iter != _results.end()) {
      
      //calculate time in milliseconds
      int secs = 0;
      int msecs = recv_time.tv_usec - send_time.tv_usec;
      if (msecs < 0) {
	secs = recv_time.tv_sec - send_time.tv_sec - 1;
      }
      else {
	secs = recv_time.tv_sec - send_time.tv_sec;
      }
      //time in milliseconds below
      int rtt = abs(msecs) / 1000 + 1000 * secs;
      if (rtt < data->_resp_time) {
	data->_state = LBTest::K_SUCCESS;
	return rtt;
      }
      else {
	data->_state = LBTest::K_FAILURE;
	return -1;
      }
      _results.erase(r_iter);
    }
  }
  
  if (_debug) {
    cout << "TTLEngine::recv(): finished heath test" << endl;
  }
  data->_state = LBTest::K_FAILURE;
  return -1;
}






/**
 *
 *
 **/
void
TTLEngine::send(int send_sock, const string &iface, const string &target_addr, unsigned short packet_id, string saddress, int ttl, unsigned short port)
{
  if (_debug) {
    cout << "TTLEngine::send(): packet_id: " << packet_id << ", ttl: " <<  ttl << ", port: " << port << endl;
  }

  if (saddress.empty()) {
    if (_debug) {
      cout << "TTLEngine::send() source address is empty" << endl;
    }
    return;
  }
  
  if (_debug) {
    cout << "TTLEngine::send() source address is: " << saddress << endl;
  }

  int err;
  sockaddr_in taddr;
  timeval send_time;
  char buffer[42];
  struct iphdr *ip;
  struct udphdr *udp;

  if (iface.empty() || target_addr.empty()) {
    return;
  }

  //convert target_addr to ip addr
  struct hostent *h = gethostbyname(target_addr.c_str());
  if (h == NULL) {
    if (_debug) {
      cerr << "TTLEngine::send() Error in resolving hostname" << endl;
    }
    syslog(LOG_ERR, "wan_lb: error in resolving configured hostname: %s", target_addr.c_str());
    return;
  }

  // bind a socket to a device name (might not work on all systems):
  if (setsockopt(send_sock, SOL_SOCKET, SO_BINDTODEVICE, iface.c_str(), iface.size()) != 0) {
    if (_debug) {
      cerr << "TTLEngine::send() failure to bind to interface: " << iface << endl;
    }
    syslog(LOG_ERR, "wan_lb: failure to bind to interface: %s", iface.c_str());
    return; //will allow the test to time out then
  }

  ip = (iphdr*)buffer;
   
  ip->ihl = 5;
  ip->version = 4;
  ip->tos = 0;
  ip->tot_len = sizeof(iphdr) + sizeof(udphdr);// + 8;
  ip->id = htons(getpid());
  ip->frag_off = 0;
  ip->ttl = ttl;
  ip->protocol = IPPROTO_UDP;
  ip->check = 0;
  in_addr_t addr = inet_addr(saddress.c_str()); //m_ulSourceAddr;
  ip->saddr =  addr;

  struct in_addr ia;
  memcpy(&ia, h->h_addr_list[0], sizeof(ia));
  ip->daddr = ia.s_addr;
  ip->check = in_checksum((unsigned short*)ip, sizeof(iphdr));

  udp = (udphdr*)(buffer + sizeof(iphdr));
  //now udp stuff
  udp->source = htons(packet_id);//was usid--not sure about this...
  udp->dest = htons(packet_id);
  udp->len = sizeof(udphdr);
  udp->check = 0;

  unsigned long data_length = 8; //bytes
   
  //copy in headers
  udp->check = udp_checksum(IPPROTO_UDP,(char*)udp, sizeof(struct udphdr)+data_length, ip->saddr, ip->daddr);

  //set destination port and address
  bzero(&(taddr), sizeof(taddr));
  taddr.sin_addr.s_addr = ia.s_addr;
  taddr.sin_family = AF_INET;
  taddr.sin_port = packet_id;
  err = sendto(send_sock, buffer, sizeof(iphdr)+sizeof(udphdr)+data_length, 0, (struct sockaddr*)&taddr, sizeof(taddr));
  if (_debug) {
    cout << "TTLEngine::send(): send " << err << " bytes" << endl;
  }
  if (_debug) {
    if(err < 0) {
	if (errno == EBADF)
	  cout << "EBADF" << endl;
	else if (errno == EBADF)
	  cout << "EBADF" << endl;
	else if (errno == ENOTSOCK)
	  cout << "ENOTSOCK" << endl;
	else if (errno == EFAULT)
	  cout << "EFAULT" << endl;
	else if (errno == EMSGSIZE)
	  cout << "EMSGSIZE" << endl;
	else if (errno == EWOULDBLOCK)
	  cout << "EWOULDBLOCK" << endl;
	else if (errno == ENOBUFS)
	  cout << "ENOBUFS" << endl;
	else if (errno == EINTR)
	  cout << "EINTR" << endl;
	else if (errno == ENOMEM)
	  cout << "ENOMEM" << endl;
	else if (errno == EINVAL)
	  cout << "EINVAL" << endl;
	else if (errno == EPIPE)
	  cout << "EPIPE" << endl;
	else
	  cout << "unknown error: " << errno << endl;
      }
  }
}

/**
 *
 *
 **/
int
TTLEngine::receive(int recv_sock)
{
  timeval wait_time;
  int ret;
  int icmp_pktsize = 40;
  char resp_buf[icmp_pktsize];
  struct sockaddr_in dest_addr;
  unsigned int addr_len;
  fd_set readfs;
  unsigned short packet_id = 0;

  FD_ZERO(&readfs);
  FD_SET(recv_sock, &readfs);

  wait_time.tv_usec = 0;
  wait_time.tv_sec = 3; //3 second timeout

  if (_debug) {
    cout << "TTLEngine::receive(): start" << endl;
  }

  //NEW-OLD STUFF HERE
   
  while (select(recv_sock+1, &readfs, NULL, NULL, &wait_time) != 0) {
    int bytes_recv = recvfrom(recv_sock, &resp_buf, 56, 0, (struct sockaddr*)&dest_addr, &addr_len);
    if (_debug) {
      cout << "TTLEngine::receive() received: " << bytes_recv << endl;
    }
    if (bytes_recv != -1) {
      //process packet data
      char* datap;
      datap = (char*)(&resp_buf) + 49;
      memcpy(&packet_id, datap, sizeof(unsigned short));
      return packet_id;
    }
    else {
      cerr << "TTLEngine::receive(): error from recvfrom" << endl;
    }
  }
  return -1;
}




/**
 * LatencyTool::inChecksum()
 * Performs ip checksum computation
 *
 **/
unsigned short
TTLEngine::in_checksum(unsigned short *pAddr, int iLen)
{
  int iSum = 0;
  unsigned short usAnswer = 0;
  unsigned short *pW = pAddr;
  int iRemain = iLen;

  while(iRemain > 1)
    {
      iSum += *pW++;
      iRemain -= sizeof(unsigned short);
    }
  if(iRemain==1)
    {
      *(u_char *)(&usAnswer)=*(u_char*)pW;
      iSum += usAnswer;
    }
  iSum = (iSum>>16) + (iSum&0xffff);
  iSum += (iSum>>16);
  usAnswer = ~iSum;
  return(usAnswer);
}


/**
 * LatencyTool::UDPChecksum()
 * UDP check computation
 *
 **/
unsigned short
TTLEngine::udp_checksum(unsigned char ucProto, char *pPacket, int iLength, unsigned long ulSourceAddress, unsigned long ulDestAddress)
{
   struct PsuedoHdr
   {
     struct in_addr sourceAddr;
     struct in_addr destAddr;
     unsigned char ucPlaceHolder;
     unsigned char ucProtocol;
     unsigned short usLength;
   } PsuedoHdr;
   
   struct PsuedoHdr psuedoHdr;
   char *pTempPacket;
   unsigned short usAnswer;
   psuedoHdr.ucProtocol = ucProto;
   psuedoHdr.usLength = htons(iLength);
   psuedoHdr.ucPlaceHolder = 0;
   psuedoHdr.sourceAddr.s_addr = ulSourceAddress;
   psuedoHdr.destAddr.s_addr = ulDestAddress;
   
   if((pTempPacket = (char*)malloc(sizeof(PsuedoHdr) + iLength)) == NULL)
     {
       cerr << "ActionDropConn::UDPChecksum(), error in malloc" << endl;
       //throw an exception
       return 0;
     }
   
   memcpy(pTempPacket, &psuedoHdr, sizeof(PsuedoHdr));
   memcpy((pTempPacket + sizeof(PsuedoHdr)), pPacket, iLength);
   
   usAnswer = (unsigned short)in_checksum((unsigned short*)pTempPacket,
					 (iLength + sizeof(PsuedoHdr)));
   
   free(pTempPacket);
   
   return usAnswer;
}

/**
 *
 *
 **/
unsigned short
TTLEngine::get_new_packet_id()
{
  if (_packet_id >= _max_port_id)
    _packet_id = _min_port_id;
  return ++_packet_id;
}

/**
 *
 *
 **/
string
LBTestTTL::dump()
{
  char buf[20];
  sprintf(buf,"%u",_resp_time);
  string foo = string("target: ") + _target + ", resp_time: " + buf;
  sprintf(buf,"%u",_ttl);
  foo += string(", ttl: ") + buf;
  sprintf(buf,"%u",_port);
  foo += string(", port: ") + buf;
  return foo;
}