~ubuntu-branches/ubuntu/raring/sip-tester/raring

« back to all changes in this revision

Viewing changes to call.cpp

  • Committer: Bazaar Package Importer
  • Author(s): ARAKI Yasuhiro
  • Date: 2005-04-11 11:53:53 UTC
  • Revision ID: james.westby@ubuntu.com-20050411115353-o33qn3noyjwjgnpd
Tags: upstream-1.1rc1
ImportĀ upstreamĀ versionĀ 1.1rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This program is free software; you can redistribute it and/or modify
 
3
 *  it under the terms of the GNU General Public License as published by
 
4
 *  the Free Software Foundation; either version 2 of the License, or
 
5
 *  (at your option) any later version.
 
6
 *
 
7
 *  This program is distributed in the hope that it will be useful,
 
8
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *  GNU General Public License for more details.
 
11
 *
 
12
 *  You should have received a copy of the GNU General Public License
 
13
 *  along with this program; if not, write to the Free Software
 
14
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
 *
 
16
 *  Author : Richard GAYRAUD - 04 Nov 2003
 
17
 *           Olivier Jacques
 
18
 *           From Hewlett Packard Company.
 
19
 *           Shriram Natarajan
 
20
 *           Peter Higginson
 
21
 *           Eric Miller
 
22
 */
 
23
 
 
24
#include <iterator>
 
25
#include <algorithm>
 
26
#include <fstream>
 
27
#include <iostream>
 
28
#include "sipp.hpp"
 
29
 
 
30
#define KEYWORD_SIZE 64
 
31
 
 
32
call_map calls;
 
33
 
 
34
/************** Call map and management routines **************/
 
35
 
 
36
call_map * get_calls()
 
37
{
 
38
  return & calls;
 
39
}
 
40
 
 
41
static unsigned int next_number = 1;
 
42
 
 
43
call * add_call(char * call_id, bool ipv6)
 
44
{
 
45
  call * new_call;
 
46
 
 
47
  new_call = new call(call_id, ipv6);
 
48
 
 
49
  if(!new_call) {
 
50
    ERROR("Memory Overflow");
 
51
  }
 
52
 
 
53
  calls[std::string(call_id)] = new_call;
 
54
 
 
55
  if(!next_number) { next_number ++; }
 
56
  new_call -> number = next_number;
 
57
 
 
58
  /* Vital counters update */
 
59
  next_number++;
 
60
  open_calls++;
 
61
 
 
62
  /* Statistics update */
 
63
  calls_since_last_rate_change++;
 
64
  total_calls ++;
 
65
 
 
66
  if(open_calls > open_calls_peak) { 
 
67
    open_calls_peak = open_calls;
 
68
    open_calls_peak_time = clock_tick / 1000;
 
69
  }
 
70
  return new_call;
 
71
}
 
72
 
 
73
call * add_call(bool ipv6)
 
74
{
 
75
  static char call_id[MAX_HEADER_LEN];
 
76
  
 
77
  call * new_call;
 
78
  
 
79
  if(!next_number) { next_number ++; }
 
80
  
 
81
  sprintf(call_id, 
 
82
          "%u-%u@%s",
 
83
          next_number, pid, local_ip);
 
84
  
 
85
  return add_call(call_id, ipv6);
 
86
}
 
87
 
 
88
call * get_call(char * call_id)
 
89
{
 
90
  call * call_ptr;
 
91
  
 
92
  call_ptr = calls[std::string(call_id)];
 
93
 
 
94
  if(!call_ptr) {
 
95
    calls.erase(std::string(call_id));
 
96
  }
 
97
  
 
98
  return call_ptr;
 
99
}
 
100
 
 
101
void delete_call(char * call_id)
 
102
{
 
103
  call * call_ptr = calls[std::string(call_id)];
 
104
 
 
105
  if(!calls.erase(std::string(call_id))) {
 
106
    ERROR("Call not erased from map");
 
107
  } 
 
108
 
 
109
  if(call_ptr) {
 
110
    delete call_ptr;
 
111
    open_calls--;
 
112
  } else {
 
113
    ERROR("Call not found");
 
114
  }
 
115
}
 
116
 
 
117
/******* Very simple hash for retransmission detection  *******/
 
118
 
 
119
unsigned long hash(char * msg) {
 
120
  unsigned long rv = 0;
 
121
  int len = strlen(msg);
 
122
  int index = 0;
 
123
  
 
124
  if (len > 4) {
 
125
    rv  = msg[0] + msg[1] + msg[2] + msg[3];
 
126
  }
 
127
  
 
128
  if (len > 12) {
 
129
    rv += msg[9] + msg[10] + msg[11] + msg[12];
 
130
  }
 
131
 
 
132
  rv <<= 8;
 
133
  rv += len;
 
134
  rv <<= 16;
 
135
  
 
136
  for (index = 0; index < len; index ++) {
 
137
    rv += + msg[index] * index;
 
138
  }
 
139
  
 
140
  return rv;
 
141
}
 
142
 
 
143
/******************* Call class implementation ****************/
 
144
 
 
145
call::InputFileUsage call::m_usage   = call::InputFileSequentialOrder;
 
146
int                  call::m_counter = 0;
 
147
 
 
148
call::call(char * p_id, bool ipv6) : use_ipv6(ipv6)
 
149
{
 
150
  memset(this, 0, sizeof(call));
 
151
  id = strdup(p_id);
 
152
  start_time = clock_tick;
 
153
  call_established=false ;
 
154
  count_in_stats=true ;
 
155
  ack_is_pending=false ;
 
156
  last_recv_msg = NULL;
 
157
  
 
158
  call_remote_socket = 0;
 
159
  
 
160
  // initialising the CallVariable with the Scenario variable
 
161
  for(int i=0; i<SCEN_VARIABLE_SIZE; i++) 
 
162
    {
 
163
      if(scenVariableTable[i] != NULL) {
 
164
        M_callVariableTable[i] = new CCallVariable();
 
165
        if (M_callVariableTable[i] == NULL) {
 
166
          ERROR ("call variable allocation failed");
 
167
        }
 
168
      } else {
 
169
        M_callVariableTable[i] = NULL;
 
170
      }
 
171
    }
 
172
 
 
173
  // If not updated by a message we use the start time 
 
174
  // information to compute rtd information
 
175
  start_time_rtd  = clock_tick; 
 
176
 
 
177
  // by default, last action result is NO_ERROR
 
178
  last_action_result = call::E_AR_NO_ERROR;
 
179
 
 
180
  if (InputFileRandomOrder == m_usage) {
 
181
      m_localLineNumber = rand() % numLinesInFile;
 
182
  } else {
 
183
      m_localLineNumber = m_counter++;
 
184
      if (m_counter >= numLinesInFile) {
 
185
          m_counter = 0;
 
186
      }
 
187
 
 
188
  }
 
189
 
 
190
  peer_tag[0] = '\0';
 
191
}
 
192
 
 
193
call::~call()
 
194
{
 
195
  deleted += 1;
 
196
  
 
197
  if(comp_state) { comp_free(&comp_state); }
 
198
 
 
199
  if(count_in_stats) {
 
200
    CStat::instance()->computeStat(CStat::E_ADD_CALL_DURATION, 
 
201
                                   clock_tick - start_time);
 
202
  }
 
203
 
 
204
  call_duration_sum += clock_tick - start_time;
 
205
  call_duration_nb++;
 
206
  
 
207
  if(pollset_index) {
 
208
    pollset_remove(pollset_index);
 
209
    shutdown(call_socket, SHUT_RDWR);
 
210
    close(call_socket);
 
211
  }
 
212
 
 
213
  /* Deletion of the call variable */ 
 
214
  for(int i=0; i<SCEN_VARIABLE_SIZE; i++) {
 
215
    if(M_callVariableTable[i] != NULL) {
 
216
      delete M_callVariableTable[i] ;
 
217
      M_callVariableTable[i] = NULL;
 
218
    }
 
219
  }
 
220
 
 
221
  if(id) { free(id); }
 
222
  if(last_recv_msg) { free(last_recv_msg); }
 
223
  if(last_send_msg) { free(last_send_msg); }
 
224
 
 
225
  if(dialog_route_set) {
 
226
       free(dialog_route_set);
 
227
  }
 
228
 
 
229
#ifdef _USE_OPENSSL
 
230
  if(dialog_authentication) {
 
231
       free(dialog_authentication);
 
232
  }
 
233
#endif
 
234
  call_established= false ;
 
235
}
 
236
 
 
237
void call::connect_socket_if_needed()
 
238
{
 
239
         
 
240
  if(call_socket) return;
 
241
  if(!multisocket) return;
 
242
 
 
243
  if(transport == T_UDP) {
 
244
    struct sockaddr_storage saddr;
 
245
    sipp_socklen_t len;
 
246
    
 
247
    if(toolMode != MODE_CLIENT) return;
 
248
 
 
249
    if((call_socket = socket(use_ipv6 ? AF_INET6 : AF_INET,
 
250
                             SOCK_DGRAM,
 
251
                             0)) == -1) {
 
252
      ERROR_NO("Unable to get an UDP socket");
 
253
    }
 
254
    
 
255
    memset(&saddr, 0, sizeof(struct sockaddr_storage));
 
256
 
 
257
    saddr.ss_family       = AF_INET;
 
258
    
 
259
    if(bind(call_socket,
 
260
            (sockaddr *)(void *)&saddr,
 
261
            use_ipv6 ? sizeof(struct sockaddr_in6) :
 
262
                       sizeof(struct sockaddr_in))) {
 
263
      ERROR_NO("Unable to bind UDP socket");
 
264
    }
 
265
    
 
266
    if (use_ipv6) {
 
267
      len = sizeof(struct sockaddr_in6);
 
268
    } else {
 
269
      len = sizeof(struct sockaddr_in);
 
270
    }
 
271
 
 
272
    getsockname(call_socket, 
 
273
                (sockaddr *)(void *)&saddr,
 
274
                &len);
 
275
 
 
276
    if (use_ipv6) {
 
277
      call_port =
 
278
        ntohs((short)((_RCAST(struct sockaddr_in6 *, &saddr))->sin6_port));
 
279
    } else {
 
280
      call_port
 
281
        = ntohs((short)((_RCAST(struct sockaddr_in *, &saddr))->sin_port));
 
282
    }
 
283
 
 
284
  } else { /* TCP */
 
285
 
 
286
    if((call_socket= socket(use_ipv6 ? AF_INET6 : AF_INET,
 
287
                            SOCK_STREAM,
 
288
                            0))== -1) {
 
289
      ERROR_NO("Unable to get a TCP socket");
 
290
    }
 
291
    
 
292
    sipp_customize_socket(call_socket);
 
293
 
 
294
    /*
 
295
    struct sockaddr_storage *L_dest = &remote_sockaddr;
 
296
 
 
297
    if (use_remote_sending_addr) {
 
298
        L_dest = &remote_sending_sockaddr ;
 
299
     }
 
300
    */
 
301
              //  (struct sockaddr *)(void *)&remote_sockaddr,
 
302
              //  (struct sockaddr *)(void *)L_dest,
 
303
               // use_ipv6 ? sizeof(struct sockaddr_in6) :
 
304
                //           sizeof(struct sockaddr_in))) {
 
305
                
 
306
    if(connect(call_socket,
 
307
               (struct sockaddr *)(void *)&remote_sockaddr,
 
308
                SOCK_ADDR_SIZE(&remote_sockaddr))) {
 
309
      
 
310
      if(errno == EINVAL){
 
311
        /* This occurs sometime on HPUX but is not a true INVAL */
 
312
        ERROR("Unable to connect a TCP socket, remote peer error");
 
313
      } else {
 
314
        ERROR_NO("Unable to connect a TCP socket");
 
315
      }
 
316
    }
 
317
  }
 
318
 
 
319
  /* Asks to receive incoming messages */
 
320
  pollset_index = pollset_add(this, call_socket);
 
321
}
 
322
 
 
323
bool lost(int percent)
 
324
{
 
325
  static int inited = 0;
 
326
 
 
327
  if(!lose_packets) return false;
 
328
  if(!percent) return false;
 
329
 
 
330
  if(!inited) {
 
331
    srand((unsigned int) time(NULL));
 
332
    inited = 1;
 
333
  }
 
334
 
 
335
  if((rand() % 100) < percent) {
 
336
    return true;
 
337
  } else {
 
338
    return false;
 
339
  }
 
340
}
 
341
 
 
342
int call::send_raw(char * msg, int index) 
 
343
{
 
344
  void ** state;
 
345
  int sock;
 
346
  int rc;
 
347
#ifdef _USE_OPENSSL
 
348
  SSL *ssl;
 
349
  extern SSL *ssl_list[];
 
350
#endif
 
351
  struct timeval currentTime;
 
352
  GET_TIME (&currentTime);
 
353
  TRACE_MSG((s, "----------------------------------------------- %s\n"
 
354
             "%s message sent:\n\n%s\n",
 
355
             CStat::instance()->formatTime(&currentTime),
 
356
             TRANSPORT_TO_STRING(transport),
 
357
             msg));
 
358
  
 
359
  if(lost(scenario[index] -> lost)) {
 
360
    TRACE_MSG((s, "%s message voluntary lost (while sending).", TRANSPORT_TO_STRING(transport)));
 
361
    
 
362
    if(comp_state) { comp_free(&comp_state); }
 
363
    scenario[index] -> nb_lost++;
 
364
    return 0;
 
365
  }
 
366
  
 
367
  if(call_socket) {
 
368
    state = &comp_state;
 
369
    sock = call_socket;
 
370
 
 
371
    if (use_remote_sending_addr) {
 
372
      if (!call_remote_socket) {
 
373
        struct sockaddr_storage *L_dest = &remote_sending_sockaddr;
 
374
        if((call_remote_socket= socket(use_ipv6 ? AF_INET6 : AF_INET,
 
375
                            SOCK_STREAM,
 
376
                            0))== -1) {
 
377
          ERROR_NO("Unable to get a TCP socket");
 
378
        }
 
379
    
 
380
        sipp_customize_socket(call_remote_socket);
 
381
 
 
382
        if(connect(call_remote_socket,
 
383
               (struct sockaddr *)(void *)L_dest,
 
384
                SOCK_ADDR_SIZE(&remote_sockaddr))) {
 
385
          if(errno == EINVAL){
 
386
            /* This occurs sometime on HPUX but is not a true INVAL */
 
387
            ERROR("Unable to connect a TCP socket, remote peer error");
 
388
          } else {
 
389
            ERROR_NO("Unable to connect a TCP socket");
 
390
          }
 
391
        }
 
392
      }
 
393
      sock=call_remote_socket ;
 
394
    }
 
395
 
 
396
#ifdef _USE_OPENSSL
 
397
    ssl  = ssl_list[sock];
 
398
#endif
 
399
  } else {
 
400
    state = &monosocket_comp_state;
 
401
    if(transport == T_UDP) {
 
402
      sock = main_socket;
 
403
    } else {
 
404
      sock = tcp_multiplex;
 
405
#ifdef _USE_OPENSSL
 
406
      ssl = ssl_tcp_multiplex;
 
407
#endif
 
408
    }
 
409
  }
 
410
 
 
411
#ifdef _USE_OPENSSL
 
412
  if ( transport == T_TLS ) {
 
413
    rc = send_message_tls(ssl, state, msg);
 
414
  } else {
 
415
#endif
 
416
  rc = send_message(sock, state, msg);
 
417
#ifdef _USE_OPENSSL
 
418
  }
 
419
#endif
 
420
 
 
421
  if(rc == -1) return -1;
 
422
 
 
423
  if(rc < -1) {
 
424
    CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
425
    CStat::instance()->computeStat(CStat::E_FAILED_CANNOT_SEND_MSG);
 
426
    delete_call(id);
 
427
  }
 
428
  
 
429
  return rc; /* OK */
 
430
}
 
431
 
 
432
int call::sendBuffer(char * msg) 
 
433
{
 
434
  void ** state;
 
435
  int sock;
 
436
  int rc;
 
437
 
 
438
  TRACE_MSG((s, "-----------------------------------------------\n"
 
439
             "%s message send:\n\n%s\n",
 
440
             TRANSPORT_TO_STRING(transport),
 
441
             msg));
 
442
  
 
443
  if(call_socket) {
 
444
    state = &comp_state;
 
445
    sock = call_socket;
 
446
  } else {
 
447
    state = &monosocket_comp_state;
 
448
    if(transport == T_UDP) {
 
449
      sock = main_socket;
 
450
    } else {
 
451
      sock = tcp_multiplex;
 
452
    }
 
453
  }
 
454
 
 
455
  rc = send_message(sock, state, msg);
 
456
 
 
457
  if(rc == -1) return -1;
 
458
 
 
459
  if(rc < -1) {
 
460
    CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
461
    CStat::instance()->computeStat(CStat::E_FAILED_CANNOT_SEND_MSG);
 
462
    delete_call(id);
 
463
  }
 
464
  
 
465
  return rc; /* OK */
 
466
}
 
467
 
 
468
 
 
469
char * call::compute_cseq(char * src)
 
470
{
 
471
  char *dest;
 
472
  static char cseq[MAX_HEADER_LEN * 10];
 
473
 
 
474
  if(dest = strstr(src, "CSeq")) {
 
475
    /* If we find a CSeq in incoming msg */
 
476
    char * last_header = get_last_header(dest+4);
 
477
    if(last_header) {
 
478
      int i;
 
479
      /* Extract the integer value of the last CSeq */
 
480
      last_header = strstr(last_header, ":");
 
481
      last_header++;
 
482
      while(isspace(*last_header)) last_header++;
 
483
      sscanf(last_header,"%d", &i);
 
484
      /* Add 1 to the last CSeq value */
 
485
      sprintf(cseq, "%s%d",  "CSeq: ", (i+1));
 
486
    } else {
 
487
      sprintf(cseq, "%s",  "CSeq: 2");
 
488
    }
 
489
    return cseq;
 
490
  } else {
 
491
    return NULL;
 
492
  }
 
493
}
 
494
 
 
495
char * call::get_last_header(char * name)
 
496
{
 
497
  static char last_header[MAX_HEADER_LEN * 10];
 
498
  char * src, *dest, *ptr;
 
499
  char src_tmp[1024] = "\n";
 
500
 
 
501
  if((!last_recv_msg) || (!strlen(last_recv_msg))) {
 
502
    return NULL;
 
503
  }
 
504
 
 
505
  src = last_recv_msg;
 
506
  dest = last_header;
 
507
 
 
508
  strcpy(src_tmp+1,name);
 
509
  while(src = strcasestr2(src, src_tmp)) {
 
510
    src++;
 
511
    ptr = strchr(src, '\n');
 
512
    
 
513
    /* Multiline headers always begin with a tab or a space
 
514
     * on the subsequent lines */
 
515
    while((ptr) &&
 
516
          ((*(ptr+1) == ' ' ) ||
 
517
           (*(ptr+1) == '\t')    )) {
 
518
      ptr = strchr(ptr + 1, '\n'); 
 
519
    }
 
520
 
 
521
    if(ptr) { *ptr = 0; }
 
522
    // Add \r\n when several Via header are present (AgM) 
 
523
    if (dest != last_header) {
 
524
      dest += sprintf(dest, "\r\n");
 
525
    }
 
526
    dest += sprintf(dest, "%s", src);
 
527
    if(ptr) { *ptr = '\n'; }
 
528
    
 
529
    src++;
 
530
  }
 
531
  
 
532
  if(dest == last_header) {
 
533
    return NULL;
 
534
  }
 
535
 
 
536
  *(dest--) = 0;
 
537
 
 
538
  /* Remove trailing whitespaces, tabs, and CRs */
 
539
  while ((dest > last_header) && 
 
540
         ((*dest == ' ') || (*dest == '\r')|| (*dest == '\t'))) {
 
541
    *(dest--) = 0;
 
542
  }
 
543
 
 
544
  /* remove enclosed CRs in multilines */
 
545
  /* don't remove enclosed CRs for multiple headers (e.g. Via) (Rhys) */
 
546
  while((ptr = strstr(last_header, "\r\n")) != NULL
 
547
        && (   *(ptr + 2) == ' ' 
 
548
            || *(ptr + 2) == '\r' 
 
549
            || *(ptr + 2) == '\t') ) {
 
550
    /* Use strlen(ptr) to include trailing zero */
 
551
    memmove(ptr, ptr+1, strlen(ptr));
 
552
  }
 
553
  /* Remove illegal double CR characters */
 
554
  while((ptr = strstr(last_header, "\r\r")) != NULL) {
 
555
    memmove(ptr, ptr+1, strlen(ptr));
 
556
  }
 
557
  /* Remove illegal double Newline characters */  
 
558
  while((ptr = strstr(last_header, "\n\n")) != NULL) {
 
559
    memmove(ptr, ptr+1, strlen(ptr));
 
560
  }
 
561
 
 
562
  return last_header;
 
563
}
 
564
 
 
565
char * call::get_header_content(char* message, char * name)
 
566
{
 
567
  /* non reentrant. consider accepting char buffer as param */
 
568
  static char last_header[MAX_HEADER_LEN * 10];
 
569
  char * src, *dest, *ptr;
 
570
 
 
571
  /* returns empty string in case of error */
 
572
  memset(last_header, 0, sizeof(last_header));
 
573
 
 
574
  if((!message) || (!strlen(message))) {
 
575
    return last_header;
 
576
  }
 
577
 
 
578
  src = message;
 
579
  dest = last_header;
 
580
  
 
581
  /* for safety's sake */
 
582
  if (NULL == name || NULL == strrchr(name, ':')) {
 
583
      return last_header;
 
584
  }
 
585
 
 
586
  while(src = strstr(src, name)) {
 
587
 
 
588
      /* just want the header's content */
 
589
      src += strlen(name);
 
590
 
 
591
    ptr = strchr(src, '\n');
 
592
    
 
593
    /* Multiline headers always begin with a tab or a space
 
594
     * on the subsequent lines */
 
595
    while((ptr) &&
 
596
          ((*(ptr+1) == ' ' ) ||
 
597
           (*(ptr+1) == '\t')    )) {
 
598
      ptr = strchr(ptr + 1, '\n'); 
 
599
    }
 
600
 
 
601
    if(ptr) { *ptr = 0; }
 
602
    // Add "," when several headers are present
 
603
    if (dest != last_header) {
 
604
      dest += sprintf(dest, ",");
 
605
    }
 
606
    dest += sprintf(dest, "%s", src);
 
607
    if(ptr) { *ptr = '\n'; }
 
608
    
 
609
    src++;
 
610
  }
 
611
  
 
612
  if(dest == last_header) {
 
613
    return last_header;
 
614
  }
 
615
 
 
616
  *(dest--) = 0;
 
617
 
 
618
  /* Remove trailing whitespaces, tabs, and CRs */
 
619
  while ((dest > last_header) && 
 
620
         ((*dest == ' ') || (*dest == '\r')|| (*dest == '\t'))) {
 
621
    *(dest--) = 0;
 
622
  }
 
623
 
 
624
  /* remove enclosed CRs in multilines */
 
625
  while(ptr = strchr(last_header, '\r')) {
 
626
    /* Use strlen(ptr) to include trailing zero */
 
627
    memmove(ptr, ptr+1, strlen(ptr));
 
628
  }
 
629
 
 
630
  return last_header;
 
631
}
 
632
 
 
633
char * call::send_scene(int index, int *send_status)
 
634
{
 
635
  static char msg_buffer[SIPP_MAX_MSG_SIZE];
 
636
 
 
637
#define MAX_MSG_NAME_SIZE 30
 
638
  static char msg_name[MAX_MSG_NAME_SIZE];
 
639
  char *L_ptr1 ;
 
640
  char *L_ptr2 ;
 
641
 
 
642
  /* Socket port must be known before string substitution */
 
643
  connect_socket_if_needed();
 
644
  
 
645
  if(scenario[index] -> send_scheme) {
 
646
    char * dest;
 
647
    dest = createSendingMessage(scenario[index] -> send_scheme, index);
 
648
    strcpy(msg_buffer, dest);
 
649
 
 
650
    if (dest) {
 
651
      L_ptr1=msg_name ;
 
652
      L_ptr2=msg_buffer ;
 
653
      while ((*L_ptr2 != ' ') && (*L_ptr2 != '\n') && (*L_ptr2 != '\t'))  {
 
654
        *L_ptr1 = *L_ptr2;
 
655
        L_ptr1 ++;
 
656
        L_ptr2 ++;
 
657
      }
 
658
      *L_ptr1 = '\0' ;
 
659
    }
 
660
 
 
661
    if (strcmp(msg_name,"ACK") == 0) {
 
662
      call_established = true ;
 
663
      ack_is_pending = false ;
 
664
    }
 
665
 
 
666
    if(send_status) {
 
667
      *send_status = 
 
668
        send_raw(msg_buffer, index);
 
669
    } else {
 
670
      send_raw(msg_buffer, index);
 
671
    }
 
672
  } else {
 
673
    ERROR("Unsupported 'send' message in scenario");
 
674
  }
 
675
 
 
676
  return msg_buffer;
 
677
}
 
678
 
 
679
bool call::next()
 
680
{
 
681
  int test = scenario[msg_index]->test;
 
682
  /* What is the next message index? */
 
683
  if ( scenario[msg_index]->next && 
 
684
       ((test == -1) ||
 
685
        (test < SCEN_VARIABLE_SIZE && M_callVariableTable[test] != NULL && M_callVariableTable[test]->isSet()))
 
686
     ) {
 
687
    /* For branching, use the 'next' attribute value */
 
688
         msg_index = labelArray[scenario[msg_index]->next];
 
689
  } else {
 
690
    /* Without branching, use the next message */
 
691
  msg_index++;
 
692
  }
 
693
  if(msg_index >= scenario_len) {
 
694
    // Call end -> was it successful?
 
695
    if(call::last_action_result != call::E_AR_NO_ERROR) {
 
696
      switch(call::last_action_result) {
 
697
        case call::E_AR_REGEXP_DOESNT_MATCH:
 
698
          CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
699
          CStat::instance()->computeStat(CStat::E_FAILED_REGEXP_DOESNT_MATCH);
 
700
          break;
 
701
        case call::E_AR_HDR_NOT_FOUND:
 
702
          CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
703
          CStat::instance()->computeStat(CStat::E_FAILED_REGEXP_HDR_NOT_FOUND);
 
704
          break;
 
705
      }
 
706
    } else {
 
707
      CStat::instance()->computeStat(CStat::E_CALL_SUCCESSFULLY_ENDED);
 
708
    }
 
709
    delete_call(id);
 
710
    return false;
 
711
  }
 
712
 
 
713
  return run();
 
714
}
 
715
 
 
716
bool call::run()
 
717
{
 
718
  if(msg_index >= scenario_len) {
 
719
    ERROR_P3("Scenario overrun for call %s (%08x) (index = %d)\n", 
 
720
             id, this, msg_index);
 
721
  }
 
722
 
 
723
  /* Manages retransmissions or delete if max retrans reached */
 
724
  if(next_retrans && (next_retrans < clock_tick)) {
 
725
    nb_retrans++;
 
726
    
 
727
    if(nb_retrans > UDP_MAX_RETRANS) {
 
728
      scenario[last_send_index] -> nb_timeout ++;
 
729
      CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
730
      CStat::instance()->computeStat(CStat::E_FAILED_MAX_UDP_RETRANS);
 
731
      if (default_behavior) {
 
732
        // Abort the call by sending proper SIP message
 
733
        WARNING_P1("Aborting call on UDP retransmission timeout for Call-ID '%s'", id);
 
734
        return(abortCall());
 
735
      } else {
 
736
        // Just delete existing call
 
737
      delete_call(id);
 
738
      return false;
 
739
      }
 
740
    } else {
 
741
      unsigned int delay = scenario[last_send_index] -> retrans_delay;
 
742
      unsigned int pow = nb_retrans;
 
743
      while(pow--) {
 
744
        delay *= 2;
 
745
      }
 
746
      if(send_raw(last_send_msg, last_send_index) < -1) {
 
747
        return false;
 
748
      }
 
749
      scenario[last_send_index] -> nb_sent_retrans++;
 
750
      next_retrans = clock_tick + delay;
 
751
    }
 
752
  }
 
753
 
 
754
  if(paused_until) {
 
755
    /* Process a pending pause instruction until delay expiration */
 
756
    if(paused_until > clock_tick) {
 
757
      return true;
 
758
    } else {
 
759
      paused_until = 0;
 
760
      return next();
 
761
    }
 
762
  } else if(scenario[msg_index] -> pause) {
 
763
    /* Starts a pause instruction */
 
764
    if((scenario[msg_index] -> pause) &&
 
765
       ((scenario[msg_index] -> pause) == -1)) {
 
766
      paused_until = clock_tick + duration;
 
767
    } else {
 
768
      paused_until = clock_tick + scenario[msg_index] -> pause;
 
769
    }
 
770
    return run(); /* In case delay is 0 */
 
771
    
 
772
  } 
 
773
#ifdef __3PCC__
 
774
  else if(scenario[msg_index] -> M_type == MSG_TYPE_SENDCMD) {
 
775
    int send_status;
 
776
 
 
777
    if(next_retrans) {
 
778
      return true;
 
779
    }
 
780
 
 
781
    send_status = sendCmdMessage(msg_index);
 
782
 
 
783
    if(send_status != 0) { /* Send error */
 
784
      return false; /* call deleted */
 
785
    }
 
786
    scenario[msg_index] -> M_nbCmdSent++;
 
787
    next_retrans = 0;
 
788
    return(next());
 
789
  }
 
790
#endif
 
791
  else if(scenario[msg_index] -> send_scheme) {
 
792
 
 
793
    char * msg_snd;
 
794
    int send_status;
 
795
 
 
796
    /* Do not send a new message until the previous one which had
 
797
     * retransmission enabled is acknowledged */
 
798
 
 
799
    if(next_retrans) {
 
800
      return true;
 
801
    }
 
802
 
 
803
    /* If this message can be used to compute RTD, do it now */
 
804
    if(!rtd_done) {
 
805
      if(scenario[msg_index] -> start_rtd) {
 
806
        start_time_rtd = clock_tick;
 
807
      }
 
808
  
 
809
      if(scenario[msg_index] -> stop_rtd) {
 
810
        rtd_sum += clock_tick - start_time_rtd;
 
811
        CStat::instance()->computeStat(CStat::E_ADD_RESPONSE_TIME_DURATION,
 
812
                                           clock_tick - start_time_rtd);
 
813
        rtd_nb ++;
 
814
        rtd_done = true;
 
815
      }
 
816
    }
 
817
  
 
818
    msg_snd = send_scene(msg_index, &send_status);
 
819
 
 
820
    if(send_status == -1) { /* Would Block on TCP */
 
821
      return true; /* No step, nothing done, retry later */
 
822
    }
 
823
 
 
824
    if(send_status <-1) { /* Send error */
 
825
      return false; /* call deleted */
 
826
    }
 
827
    
 
828
    last_send_index = msg_index;
 
829
    last_send_msg = (char *) realloc(last_send_msg, strlen(msg_snd) + 1);
 
830
    strcpy(last_send_msg, msg_snd);
 
831
 
 
832
    if(last_recv_hash) {
 
833
      /* We are sending just after msg reception. There is a great
 
834
       * chance that we will be asked to retransmit this message */
 
835
      recv_retrans_hash       = last_recv_hash;
 
836
      recv_retrans_recv_index = last_recv_index;
 
837
      recv_retrans_send_index = msg_index;
 
838
    
 
839
      /* Prevent from detecting the cause relation between send and recv 
 
840
       * in the next valid send */
 
841
      last_recv_hash = 0;
 
842
    }
 
843
 
 
844
    /* Update retransmission information */
 
845
    if(scenario[msg_index] -> retrans_delay) {
 
846
      if((transport == T_UDP) && (retrans_enabled)) {
 
847
        next_retrans = clock_tick + scenario[msg_index] -> retrans_delay;
 
848
        nb_retrans = 0;
 
849
      }
 
850
    } else {
 
851
      next_retrans = 0;
 
852
    }
 
853
    
 
854
    /* Update scenario statistics */
 
855
    scenario[msg_index] -> nb_sent++;
 
856
 
 
857
    return next();
 
858
  }
 
859
  return true;
 
860
}
 
861
 
 
862
bool call::process_unexpected(char * msg)
 
863
{
 
864
  int search_index;
 
865
  static int first = 1;
 
866
  int res ;
 
867
  
 
868
  scenario[msg_index] -> nb_unexp++;
 
869
  
 
870
  if (scenario[msg_index] -> recv_request) {
 
871
    if (default_behavior) {
 
872
      WARNING_P3("Aborting call on unexpected message for Call-ID '%s': while expecting '%s', received '%s' ",
 
873
                id, scenario[msg_index] -> recv_request, msg);
 
874
  } else {
 
875
      WARNING_P3("Continuing call on unexpected message for Call-ID '%s': while expecting '%s', received '%s' ",
 
876
                  id, scenario[msg_index] -> recv_request, msg);
 
877
    }
 
878
  } else {
 
879
    if (default_behavior) {
 
880
      WARNING_P3("Aborting call on unexpected message for Call-ID '%s': while expecting '%d' response, received '%s' ", 
 
881
                  id, scenario[msg_index] -> recv_response, msg);
 
882
    } else {
 
883
      WARNING_P3("Continuing call on unexpected message for Call-ID '%s': while expecting '%d' response, received '%s' ", 
 
884
                id, scenario[msg_index] -> recv_response, msg);
 
885
  }
 
886
  }
 
887
  
 
888
  TRACE_MSG((s, "-----------------------------------------------\n"
 
889
             "Unexpected %s message received:\n\n%s\n",
 
890
             TRANSPORT_TO_STRING(transport),
 
891
             msg));
 
892
  
 
893
  if (default_behavior) {
 
894
#ifdef __3PCC__
 
895
  // if twin socket call => reset the other part here 
 
896
  if (twinSippSocket && (msg_index > 0)) {
 
897
    //WARNING_P2("call-ID '%s', internal-cmd: abort_call %s",id, "");
 
898
    res = sendCmdBuffer
 
899
      (createSendingMessage((char*)"call-id: [call_id]\ninternal-cmd: abort_call\n", -1));
 
900
  }
 
901
#endif /* __3PCC__ */
 
902
 
 
903
  // usage of last_ keywords => for call aborting
 
904
  last_recv_msg = (char *) realloc(last_recv_msg, strlen(msg) + 1);
 
905
  strcpy(last_recv_msg, msg);
 
906
 
 
907
    CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
908
    CStat::instance()->computeStat(CStat::E_FAILED_UNEXPECTED_MSG);
 
909
  return (abortCall());
 
910
  } else {
 
911
    // Do not abort call nor send anything in reply if default behavior is disabled
 
912
    return true;
 
913
  }
 
914
}
 
915
 
 
916
bool call::abortCall()
 
917
{
 
918
  int res ;
 
919
 
 
920
  if ((toolMode != MODE_SERVER) && (msg_index > 0)) {
 
921
    if (call_established == false) {
 
922
      char * src = last_recv_msg ;
 
923
      char * dest   ;
 
924
      char   L_msg_buffer[SIPP_MAX_MSG_SIZE];
 
925
      L_msg_buffer[0] = '\0';
 
926
      char * L_param = L_msg_buffer;
 
927
 
 
928
      // Answer unexpected errors (4XX, 5XX and beyond) with an ACK 
 
929
      // Contributed by F. Tarek Rogers
 
930
      if((src) && (get_reply_code(src) > 400)) {
 
931
       sendBuffer(createSendingMessage(
 
932
         (char*)"ACK sip:[service]@[remote_ip]:[remote_port] SIP/2.0\n"
 
933
           "Via: SIP/2.0/[transport] [local_ip]:[local_port]\n"
 
934
           "From: sipp <sip:sipp@[local_ip]:[local_port]>;tag=[call_number]\n"
 
935
           "To: sut <sip:[service]@[remote_ip]:[remote_port]>[peer_tag_param]\n"
 
936
           "Call-ID: [call_id]\n"
 
937
           "CSeq: 1 ACK\n"
 
938
           "Contact: sip:sipp@[local_ip]:[local_port]\n"
 
939
           "Max-Forwards: 70\n"
 
940
           "Subject: Performance Test\n"
 
941
           "Content-Length: 0\n\n"
 
942
         , -1));
 
943
      } else if (src) {
 
944
        /* Call is not established and the reply is not a 4XX, 5XX */
 
945
        /* And we already received a message. */
 
946
        if (ack_is_pending == true) {
 
947
          /* If an ACK is expected from the other side, send it
 
948
           * and send a BYE afterwards                           */
 
949
          ack_is_pending = false;
 
950
          /* Send an ACK */
 
951
          strcpy(L_param, "ACK sip:[service]@[remote_ip]:[remote_port] SIP/2.0\n");
 
952
          sprintf(L_param, "%s%s", L_param, "[last_Via:]\n");
 
953
          sprintf(L_param, "%s%s", L_param, "[last_From:]\n");
 
954
          sprintf(L_param, "%s%s", L_param, "[last_To:]\n");
 
955
          sprintf(L_param, "%s%s",  L_param, "[last_Call-ID:]\n");
 
956
          /* The CSeq of an ACK relating to an INVITE must be the same as  */
 
957
          /* the one from the INVITE.                                      */
 
958
          /* Let's simplify this by putting 1 (no support for re-invite in */
 
959
          /* 3PCC?)                                                        */
 
960
          /* FIXME: store CSeq from last INVITE and re-use it              */
 
961
          sprintf(L_param, "%sCSeq: 1 ACK\n", L_param);
 
962
          sprintf(L_param, "%s%s", L_param, "Contact: <sip:[local_ip]:[local_port];transport=[transport]>\n");
 
963
          sprintf(L_param, "%s%s", L_param,  "Content-Length: 0\n");
 
964
          res = sendBuffer(createSendingMessage((char*)(L_param),-1));
 
965
          
 
966
          /* Send the BYE */
 
967
          strcpy(L_param, "BYE sip:[service]@[remote_ip]:[remote_port] SIP/2.0\n");
 
968
          sprintf(L_param, "%s%s", L_param, "[last_Via:]\n");
 
969
          sprintf(L_param, "%s%s", L_param, "[last_From:]\n");
 
970
          sprintf(L_param, "%s%s", L_param, "[last_To:]\n");
 
971
          sprintf(L_param, "%s%s",  L_param, "[last_Call-ID:]\n");
 
972
          char * cseq;
 
973
          cseq = compute_cseq(src);
 
974
          if (cseq != NULL) {
 
975
            sprintf(L_param, "%s%s BYE\n", L_param, compute_cseq(src));
 
976
          }
 
977
          sprintf(L_param, "%s%s", L_param, "Contact: <sip:[local_ip]:[local_port];transport=[transport]>\n");
 
978
          sprintf(L_param, "%s%s", L_param,  "Content-Length: 0\n");
 
979
          res = sendBuffer(createSendingMessage((char*)(L_param),-1));
 
980
        } else {
 
981
          /* Send a CANCEL */
 
982
          strcpy(L_param, "CANCEL sip:[service]@[remote_ip]:[remote_port] SIP/2.0\n");
 
983
          sprintf(L_param, "%s%s", L_param, "[last_Via:]\n");
 
984
          sprintf(L_param, "%s%s", L_param, "[last_From:]\n");
 
985
          sprintf(L_param, "%s%s", L_param, "[last_To:]\n");
 
986
          sprintf(L_param, "%s%s",  L_param, "[last_Call-ID:]\n");
 
987
          char * cseq;
 
988
          cseq = compute_cseq(src);
 
989
          if (cseq != NULL) {
 
990
            sprintf(L_param, "%s%s CANCEL\n", L_param, compute_cseq(src));
 
991
          }
 
992
          sprintf(L_param, "%s%s", L_param, "Contact: <sip:[local_ip]:[local_port];transport=[transport]>\n");
 
993
          sprintf(L_param, "%s%s", L_param,  "Content-Length: 0\n");
 
994
          res = sendBuffer(createSendingMessage((char*)(L_param),-1));
 
995
        }
 
996
      } else {
 
997
        /* Call is not established and the reply is not a 4XX, 5XX */
 
998
        /* and we didn't received any message. This is the case when */
 
999
        /* we are aborting after having send an INVITE and not received */
 
1000
        /* any answer. */
 
1001
        /* Do nothing ! */
 
1002
      }
 
1003
    } else {
 
1004
      /* Call is established */
 
1005
      char * src = last_recv_msg ;
 
1006
      char   L_msg_buffer[SIPP_MAX_MSG_SIZE];
 
1007
      L_msg_buffer[0] = '\0';
 
1008
      char * L_param = L_msg_buffer;
 
1009
      strcpy(L_param, "BYE sip:[service]@[remote_ip]:[remote_port] SIP/2.0\n");
 
1010
      sprintf(L_param, "%s%s", L_param, "[last_Via:]\n");
 
1011
      sprintf(L_param, "%s%s", L_param, "[last_From:]\n");
 
1012
      sprintf(L_param, "%s%s", L_param, "[last_To:]\n");
 
1013
      sprintf(L_param, "%s%s",  L_param, "[last_Call-ID:]\n");
 
1014
      char * cseq;
 
1015
      cseq = compute_cseq(src);
 
1016
      if (cseq != NULL) {
 
1017
        sprintf(L_param, "%s%s BYE\n", L_param, compute_cseq(src));
 
1018
      }
 
1019
      sprintf(L_param, "%s%s", L_param, "Contact: <sip:[local_ip]:[local_port];transport=[transport]>\n");
 
1020
      sprintf(L_param, "%s%s", L_param,  "Content-Length: 0\n");
 
1021
      res = sendBuffer(createSendingMessage((char*)(L_param),-1));
 
1022
    }
 
1023
  }
 
1024
 
 
1025
  delete_call(id);
 
1026
  return false;
 
1027
}
 
1028
 
 
1029
bool call::rejectCall()
 
1030
{
 
1031
  CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
1032
  CStat::instance()->computeStat(CStat::E_FAILED_CALL_REJECTED);
 
1033
  delete_call(id);
 
1034
  return false;
 
1035
}
 
1036
 
 
1037
 
 
1038
#ifdef __3PCC__
 
1039
int call::sendCmdMessage(int index)
 
1040
{
 
1041
  char * dest;
 
1042
  char delimitor[2];
 
1043
  delimitor[0]=27;
 
1044
  delimitor[1]=0;
 
1045
 
 
1046
  if(scenario[index] -> M_sendCmdData) {
 
1047
    // WARNING_P1("---PREPARING_TWIN_CMD---%s---", scenario[index] -> M_sendCmdData); 
 
1048
    dest = createSendingMessage(scenario[index] -> M_sendCmdData, -2);
 
1049
    strcat(dest, delimitor);
 
1050
    //WARNING_P1("---SEND_TWIN_CMD---%s---", dest); 
 
1051
 
 
1052
    int rc;
 
1053
 
 
1054
    rc = send(twinSippSocket, 
 
1055
              dest, 
 
1056
              strlen(dest), 
 
1057
              0);
 
1058
    if(rc <  0) {
 
1059
      CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
1060
      CStat::instance()->computeStat(CStat::E_FAILED_CMD_NOT_SENT);
 
1061
      delete_call(id);
 
1062
      return(-1);
 
1063
    }
 
1064
 
 
1065
    return(0);
 
1066
  }
 
1067
  else
 
1068
    return(-1);
 
1069
}
 
1070
 
 
1071
 
 
1072
int call::sendCmdBuffer(char* cmd)
 
1073
{
 
1074
  char * dest;
 
1075
  char delimitor[2];
 
1076
  int  rc;
 
1077
 
 
1078
  delimitor[0]=27;
 
1079
  delimitor[1]=0;
 
1080
 
 
1081
  dest = cmd ;
 
1082
 
 
1083
  strcat(dest, delimitor);
 
1084
 
 
1085
 
 
1086
  rc = send(twinSippSocket, 
 
1087
            dest, 
 
1088
            strlen(dest), 
 
1089
            0);
 
1090
  if(rc <  0) {
 
1091
    CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
1092
    CStat::instance()->computeStat(CStat::E_FAILED_CMD_NOT_SENT);
 
1093
    delete_call(id);
 
1094
    return(-1);
 
1095
  }
 
1096
 
 
1097
  return(0);
 
1098
}
 
1099
 
 
1100
#endif
 
1101
 
 
1102
char* call::createSendingMessage(char * src, int P_index)
 
1103
{
 
1104
  static char msg_buffer[SIPP_MAX_MSG_SIZE+2];
 
1105
 
 
1106
  if(src != NULL) {
 
1107
    char * dest = msg_buffer;
 
1108
    char * key;
 
1109
    char * length_marker = NULL;
 
1110
    int    offset = 0;
 
1111
 
 
1112
 
 
1113
    while(*src) {
 
1114
      if(*src == '[') {
 
1115
        char keyword [KEYWORD_SIZE+1];
 
1116
        src++;
 
1117
        key = strchr(src, ']');
 
1118
        if((!key) || ((key - src) > KEYWORD_SIZE) || (!(key - src))){
 
1119
          ERROR("Syntax error or invalid [keyword] in scenario");
 
1120
        }
 
1121
        memcpy(keyword, src,  key - src);
 
1122
 
 
1123
        keyword[key - src] = 0;
 
1124
        src = key + 1;
 
1125
        // allow +n for numeric variables
 
1126
        if ((key = strchr(keyword,'+'))) {
 
1127
           offset = atoi(key);
 
1128
           *key = 0;
 
1129
        }
 
1130
 
 
1131
        if(!strcmp(keyword, "remote_ip")) {
 
1132
          dest += sprintf(dest, "%s", remote_ip_escaped);
 
1133
        } else if(!strcmp(keyword, "remote_port")) {
 
1134
          dest += sprintf(dest, "%u", remote_port + offset);
 
1135
        } else if(!strcmp(keyword, "transport")) {
 
1136
          dest += sprintf(dest, "%s", TRANSPORT_TO_STRING(transport));
 
1137
        } else if(!strcmp(keyword, "local_ip")) {
 
1138
          dest += sprintf(dest, "%s", local_ip_escaped);
 
1139
        } else if(!strcmp(keyword, "local_port")) {
 
1140
          if((transport == T_UDP) && (multisocket)) {
 
1141
            dest += sprintf(dest, "%u", call_port + offset);
 
1142
          } else {
 
1143
            dest += sprintf(dest, "%u", local_port + offset);
 
1144
          }
 
1145
        } else if(!strcmp(keyword, "media_ip")) {
 
1146
          dest += sprintf(dest, "%s", media_ip_escaped);
 
1147
        } else if(!strcmp(keyword, "media_port")) {
 
1148
          dest += sprintf(dest, "%u", media_port + offset);
 
1149
        } else if(!strcmp(keyword, "call_number")) {
 
1150
          dest += sprintf(dest, "%lu", number);
 
1151
        } else if(!strcmp(keyword, "call_id")) {
 
1152
          dest += sprintf(dest, "%s", id);
 
1153
        } else if(!strcmp(keyword, "pid")) {
 
1154
          dest += sprintf(dest, "%u", pid);
 
1155
        } else if(!strcmp(keyword, "service")) {
 
1156
          dest += sprintf(dest, "%s", service);
 
1157
        } else if(!strncmp(keyword, "field", 5)) {
 
1158
            char* local_dest = dest;
 
1159
            getFieldFromInputFile(keyword, m_localLineNumber, dest);
 
1160
            if (dest == local_dest && ('\r' == *(local_dest-1) || '\n' == *(local_dest-1))) {
 
1161
                /* If the line begins with a field value and there
 
1162
                 * is nothing to add for this field, 
 
1163
                 * Jump to the end of line in scenario. SN 
 
1164
                 */
 
1165
                while((*src) && (*src != '\n')) {
 
1166
                    src++;
 
1167
                }
 
1168
                if(*src == '\n') {
 
1169
                    src++;
 
1170
                }
 
1171
            }
 
1172
        } else if(!strcmp(keyword, "peer_tag_param")) {
 
1173
          if(peer_tag && strlen(peer_tag)) {
 
1174
            dest += sprintf(dest, ";tag=%s", peer_tag);
 
1175
          }
 
1176
        } else if(strstr(keyword, "$")) {
 
1177
          int varId = atoi(keyword+1);
 
1178
          if(varId < SCEN_VARIABLE_SIZE) {
 
1179
            if(M_callVariableTable[varId] != NULL) {
 
1180
              if(M_callVariableTable[varId]->isSet()) {
 
1181
                dest += sprintf(dest, "%s",
 
1182
                                M_callVariableTable[varId]->
 
1183
                                getMatchingValue());
 
1184
                // WARNING_P1("VARIABLE --%s--", M_callVariableTable[varId]->getMatchingValue());
 
1185
              } else {
 
1186
                dest += sprintf(dest, "%s", "");
 
1187
              }
 
1188
            }
 
1189
          }
 
1190
        } else if(strstr(keyword, "last_")) {
 
1191
          char * last_header = get_last_header(keyword+5);
 
1192
          if(last_header) {
 
1193
            dest += sprintf(dest, "%s", last_header);
 
1194
          } else {
 
1195
            /* Jump to the end of line in scenario if nothing
 
1196
             * to insert in place of this header. */
 
1197
            while((*src) && (*src != '\n')) {
 
1198
              src++;
 
1199
            }
 
1200
            if(*src == '\n') {
 
1201
              src++;
 
1202
            }
 
1203
          }
 
1204
        } else if(strstr(keyword, "routes")) {
 
1205
            if (dialog_route_set) {
 
1206
                dest += sprintf(dest, "Route: %s", dialog_route_set);
 
1207
            }
 
1208
#ifdef _USE_OPENSSL
 
1209
        } else if(strstr(keyword, "authentication")) {
 
1210
            /* This keyword is substituted below */
 
1211
            dest += sprintf(dest, "[%s]", keyword);
 
1212
#endif
 
1213
        } else if(strstr(keyword, "len")) {
 
1214
            length_marker = dest;
 
1215
            // dest += sprintf(dest, "   0");
 
1216
            dest += sprintf(dest, "    ");
 
1217
        } else {
 
1218
          ERROR_P1("Unsupported keyword '%s' in xml scenario file",
 
1219
                   keyword);
 
1220
        }
 
1221
      } else if (*src == '\n') {
 
1222
        *dest++ = '\r';
 
1223
        *dest++ = *src++;
 
1224
      } else {
 
1225
        *dest++ = *src++;
 
1226
      }
 
1227
    }
 
1228
    *dest = 0;
 
1229
 
 
1230
#ifdef _USE_OPENSSL
 
1231
    /* 
 
1232
     * The authentication substitution must be done outside the above
 
1233
     * loop because auth-int will use the body (which must have already
 
1234
     * been keyword substituted) to build the md5 hash
 
1235
     */
 
1236
 
 
1237
    if((src = strstr(msg_buffer, "[authentication")) && dialog_authentication) {
 
1238
 
 
1239
        char my_auth_user[KEYWORD_SIZE];
 
1240
        char my_auth_pass[KEYWORD_SIZE];
 
1241
        char * tmp;
 
1242
 
 
1243
        strcpy(my_auth_user, service);
 
1244
        strcpy(my_auth_pass, auth_password);
 
1245
 
 
1246
        /* Look for optional username and password paramaters */
 
1247
        if(tmp = strstr(src, "username=")) {
 
1248
            tmp += strlen("username=");
 
1249
            key = tmp;
 
1250
            while (*key) {
 
1251
                if (((key - src) > KEYWORD_SIZE) || (!(key - src))) {
 
1252
                    ERROR("Syntax error parsing authentication paramaters");
 
1253
                } else if (*key == ']' || *key < 33 || *key > 126) {
 
1254
                    memset(my_auth_user, 0, sizeof(my_auth_user));
 
1255
                    strncpy(my_auth_user, tmp, key-tmp);
 
1256
                    break;
 
1257
                }
 
1258
                key++;
 
1259
            }
 
1260
        }
 
1261
 
 
1262
        if(tmp = strstr(src, "password=")) {
 
1263
            tmp += strlen("password=");
 
1264
            key = tmp;
 
1265
            while (*key) {
 
1266
                if (((key - src) > KEYWORD_SIZE) || (!(key - src))) {
 
1267
                    ERROR("Syntax error parsing authentication paramaters");
 
1268
                } else if (*key == ']' || *key < 33 || *key > 126) {
 
1269
                    memset(my_auth_pass, 0, sizeof(my_auth_pass));
 
1270
                    strncpy(my_auth_pass, tmp, key-tmp);
 
1271
                    break;
 
1272
                }
 
1273
                key++;
 
1274
            }
 
1275
        }
 
1276
 
 
1277
        /* Need the Method name from the CSeq of the Challenge */
 
1278
        char method[MAX_HEADER_LEN];
 
1279
        tmp = get_last_header("CSeq") + 5;
 
1280
        if(!tmp) {
 
1281
            ERROR("Could not extract method from cseq of challenge");
 
1282
        }
 
1283
        while(isspace(*tmp) || isdigit(*tmp)) tmp++;
 
1284
        sscanf(tmp,"%s", &method);
 
1285
 
 
1286
        /* Need the body for auth-int calculation */
 
1287
        char body[SIPP_MAX_MSG_SIZE];
 
1288
        memset(body, 0, sizeof(body));
 
1289
        tmp = msg_buffer;
 
1290
        while(*(tmp+4)) {
 
1291
            if (*tmp == '\r' && *(tmp + 1) == '\n' &&
 
1292
                    *(tmp + 2) == '\r' && *(tmp + 3) == '\n') {
 
1293
                sprintf(body, "%s", tmp+4);
 
1294
                break;
 
1295
            }
 
1296
            tmp++;                      
 
1297
        }
 
1298
 
 
1299
        /* Build the auth credenticals */
 
1300
        char result[MAX_HEADER_LEN];
 
1301
        if (createAuthHeader(my_auth_user, my_auth_pass, method, remote_ip,
 
1302
                body, dialog_authentication, result) == 0) {
 
1303
            ERROR_P1("%s", result);
 
1304
        }
 
1305
   
 
1306
        char tmp_buffer[SIPP_MAX_MSG_SIZE];
 
1307
        dest = strncpy(tmp_buffer, msg_buffer, src - msg_buffer);
 
1308
        dest += src - msg_buffer;
 
1309
        key = strchr(src, ']');
 
1310
        src += key - src + 1;
 
1311
 
 
1312
        if (dialog_challenge_type == 401) {
 
1313
            /* Registrars use Authorization */
 
1314
            dest += sprintf(dest, "Authorization: %s", result);
 
1315
        } else {
 
1316
            /* Proxies use Proxy-Authorization */
 
1317
            dest += sprintf(dest, "Proxy-Authorization: %s", result);
 
1318
        }
 
1319
        dest += sprintf(dest, "%s", src);
 
1320
        strcpy(msg_buffer, tmp_buffer);
 
1321
    }
 
1322
#endif
 
1323
 
 
1324
    // Remove all \r, \n but 1 at the end of a message to send 
 
1325
    int len = strlen(msg_buffer);
 
1326
    while ( (msg_buffer[len-1] == '\n') &&
 
1327
            (msg_buffer[len-2] == '\r') &&
 
1328
            (msg_buffer[len-3] == '\n') &&
 
1329
            (msg_buffer[len-4] == '\r')) {
 
1330
      msg_buffer[len-2] = 0;
 
1331
      len -= 2;
 
1332
    }
 
1333
 
 
1334
    int    L_flag_crlf = 0 ; // don't need add crlf
 
1335
    int    L_content_length = 0;
 
1336
 
 
1337
    if(P_index == -1 ) {
 
1338
      L_flag_crlf = 1 ; // Add crlf
 
1339
    } else if(P_index >= 0 ) {
 
1340
      message::ContentLengthFlag L_flag_content = scenario[P_index] -> content_length_flag ; 
 
1341
      switch (L_flag_content) {
 
1342
        case  message::ContentLengthValueZero :
 
1343
          L_flag_crlf = 1;
 
1344
          break ;
 
1345
        case  message::ContentLengthValueNoZero :
 
1346
          // the msg contains content-length field and his value is greater than 0
 
1347
          break ;
 
1348
        default :
 
1349
          // the msg does not contain content-length field
 
1350
          // control the crlf
 
1351
          L_content_length = xp_get_content_length(msg_buffer) ;
 
1352
          if( L_content_length == 0) {
 
1353
            L_flag_crlf = 1;
 
1354
          } else if (L_content_length == -1 ) {
 
1355
            // Error to treat?
 
1356
          } 
 
1357
          break;
 
1358
      }
 
1359
    } 
 
1360
 
 
1361
    if(L_flag_crlf) {
 
1362
      // Add crlf 
 
1363
      msg_buffer[len] ='\r';
 
1364
      msg_buffer[len+1] ='\n';
 
1365
      msg_buffer[len+2] =0;
 
1366
    }
 
1367
 
 
1368
    if (length_marker) {
 
1369
      key = strstr(length_marker,"\r\n\r\n");
 
1370
      if (key && dest - key > 4 && dest - key < 10004) {
 
1371
        char tmp = length_marker[4];
 
1372
        sprintf(length_marker, "%4u", dest - key - 4 + offset);
 
1373
        length_marker[4] = tmp;
 
1374
      }
 
1375
    }
 
1376
  } else {
 
1377
    ERROR("Unsupported 'send' message in scenario");
 
1378
  }
 
1379
  return(msg_buffer);
 
1380
}
 
1381
 
 
1382
 
 
1383
#ifdef __3PCC__
 
1384
bool call::process_twinSippCom(char * msg)
 
1385
{
 
1386
  char          * ptr;
 
1387
  int             search_index;
 
1388
  bool            found = false;
 
1389
  T_ActionResult  actionResult;
 
1390
 
 
1391
  if (checkInternalCmd(msg) == false) {
 
1392
 
 
1393
    for(search_index = msg_index;
 
1394
      search_index < scenario_len;
 
1395
      search_index++) {
 
1396
      if(scenario[search_index] -> M_type != MSG_TYPE_RECVCMD) {
 
1397
        if(scenario[search_index] -> optional) {
 
1398
          continue;
 
1399
        }
 
1400
        /* The received message is different from the expected one */
 
1401
        return rejectCall();
 
1402
      } else {
 
1403
        found = true;
 
1404
        break;
 
1405
      }
 
1406
    }
 
1407
    
 
1408
    if (found) {
 
1409
      scenario[search_index]->M_nbCmdRecv ++;
 
1410
      
 
1411
      // variable treatment
 
1412
      // WARNING_P1("---RECVD_TWIN_CMD---%s---", msg); 
 
1413
      // Remove \r, \n at the end of a received command
 
1414
      // (necessary for transport, to be removed for usage)
 
1415
      while ( (msg[strlen(msg)-1] == '\n') &&
 
1416
      (msg[strlen(msg)-2] == '\r') ) {
 
1417
        msg[strlen(msg)-2] = 0;
 
1418
      }
 
1419
      // WARNING_P1("---RECVD_TWIN_CMD AFTER---%s---", msg);
 
1420
      actionResult = executeAction(msg, search_index);
 
1421
      
 
1422
      if(actionResult != call::E_AR_NO_ERROR) {
 
1423
        // Store last action result if it is an error
 
1424
        // and go on with the scenario
 
1425
        call::last_action_result = actionResult;
 
1426
        if (actionResult == E_AR_STOP_CALL) {
 
1427
            return rejectCall();
 
1428
        }
 
1429
      }
 
1430
    } else {
 
1431
      return rejectCall();
 
1432
    }
 
1433
    return(next());
 
1434
    
 
1435
  } else {
 
1436
    return (false);
 
1437
  }
 
1438
}
 
1439
 
 
1440
bool call::checkInternalCmd(char * cmd)
 
1441
{
 
1442
 
 
1443
  char * L_ptr1, * L_ptr2, L_backup;
 
1444
  char *cmd_name       ;
 
1445
 
 
1446
  L_ptr1 = strstr(cmd, "internal-cmd:");
 
1447
  if (!L_ptr1) {return (false);}
 
1448
  L_ptr1 += 13 ;
 
1449
  while((*L_ptr1 == ' ') || (*L_ptr1 == '\t')) { L_ptr1++; }
 
1450
  if (!(*L_ptr1)) {return (false);}
 
1451
  L_ptr2 = L_ptr1;
 
1452
  while((*L_ptr2) && 
 
1453
        (*L_ptr2 != ' ') && 
 
1454
        (*L_ptr2 != '\t') && 
 
1455
        (*L_ptr2 != '\r') && 
 
1456
        (*L_ptr2 != '\n')) { 
 
1457
    L_ptr2 ++;
 
1458
  } 
 
1459
  if(!*L_ptr2) { return (false); }
 
1460
  L_backup = *L_ptr2;
 
1461
  *L_ptr2 = 0;
 
1462
 
 
1463
  if (strcmp(L_ptr1, "abort_call") == 0) {
 
1464
    *L_ptr2 = L_backup;
 
1465
    abortCall();
 
1466
    CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
1467
    return (true);
 
1468
  }
 
1469
 
 
1470
  *L_ptr2 = L_backup;
 
1471
  return (false);
 
1472
}
 
1473
#endif
 
1474
 
 
1475
 
 
1476
bool call::process_incomming(char * msg)
 
1477
{
 
1478
  int             reply_code;
 
1479
  static char     request[65];
 
1480
  unsigned long   cookie;
 
1481
  char          * ptr;
 
1482
  int             search_index;
 
1483
  bool            found = false;
 
1484
  T_ActionResult  actionResult;
 
1485
 
 
1486
  int             L_case = 0 ;
 
1487
 
 
1488
#define MATCHES_SCENARIO(index)                                \
 
1489
      (((reply_code) &&                                        \
 
1490
        ((scenario[index] -> recv_response) == reply_code)) || \
 
1491
       ((scenario[index] -> recv_request) &&                   \
 
1492
        (!strcmp(scenario[index] -> recv_request,              \
 
1493
                 request))))
 
1494
 
 
1495
  if((transport == T_UDP) && (retrans_enabled)) {
 
1496
 
 
1497
  /* Detects retransmissions from peer and retransmit the 
 
1498
   * message which was sent just after this one was received */
 
1499
  cookie = hash(msg);
 
1500
  if(recv_retrans_hash == cookie) {
 
1501
 
 
1502
    int status;
 
1503
 
 
1504
    if(lost(scenario[recv_retrans_recv_index] -> lost)) {
 
1505
      TRACE_MSG((s, "%s message (retrans) lost (recv).", 
 
1506
                 TRANSPORT_TO_STRING(transport)));
 
1507
 
 
1508
      if(comp_state) { comp_free(&comp_state); }
 
1509
      scenario[recv_retrans_recv_index] -> nb_lost++;
 
1510
      return true;
 
1511
    }
 
1512
    
 
1513
    send_scene(recv_retrans_send_index, &status);
 
1514
 
 
1515
    if(status == 0) {
 
1516
      scenario[recv_retrans_recv_index] -> nb_recv_retrans++;
 
1517
      scenario[recv_retrans_send_index] -> nb_sent_retrans++;
 
1518
    } else if(status < -1) { 
 
1519
      return false; 
 
1520
    }
 
1521
 
 
1522
    return true;
 
1523
  }
 
1524
 
 
1525
  if(last_recv_hash == cookie) {
 
1526
    /* This one has already been received, but not processed
 
1527
     * yet => (has not triggered something yet) so we can discard.
 
1528
     *
 
1529
     * This case appears when the UAS has send a 200 but not received
 
1530
     * a ACK yet. Thus, the UAS retransmit the 200 (invite transaction)
 
1531
     * until it receives a ACK. In this case, it nevers sends the 200
 
1532
     * from the  BYE, until it has reveiced the previous 200. Thus, 
 
1533
     * the UAC retransmit the BYE, and this BYE is considered as an
 
1534
     * unexpected.
 
1535
     *
 
1536
     * This case can also appear in case of message duplication by
 
1537
     * the network. This should not be considered as an unexpected.
 
1538
     */
 
1539
    return true;
 
1540
    }
 
1541
  }
 
1542
  
 
1543
  
 
1544
  /* Catch peer tag if necessary */
 
1545
  if((!strlen(peer_tag)) &&
 
1546
     (ptr = get_peer_tag(msg))) {
 
1547
    if(strlen(ptr) > (MAX_HEADER_LEN - 1)) {
 
1548
      ERROR("Peer tag too long. Change MAX_TAG_LEN and recompile sipp");
 
1549
    }
 
1550
    strcpy(peer_tag, ptr);
 
1551
  }
 
1552
  
 
1553
  /* Is it a response ? */
 
1554
  if((msg[0] == 'S') && 
 
1555
     (msg[1] == 'I') &&
 
1556
     (msg[2] == 'P') &&
 
1557
     (msg[3] == '/') &&
 
1558
     (msg[4] == '2') &&
 
1559
     (msg[5] == '.') &&
 
1560
     (msg[6] == '0')    ) {    
 
1561
 
 
1562
    reply_code = get_reply_code(msg);
 
1563
    if(!reply_code) {
 
1564
      if (!process_unexpected(msg)) {
 
1565
        return false; // Call aborted by unexpected message handling
 
1566
      }
 
1567
    }
 
1568
    request[0]=0;
 
1569
  } else if(ptr = strchr(msg, ' ')) {
 
1570
    if((ptr - msg) < 64) {
 
1571
      memcpy(request, msg, ptr - msg);
 
1572
      request[ptr - msg] = 0;
 
1573
      // Check if we received an ACK => call established
 
1574
      if (strcmp(request,"ACK")==0) {
 
1575
        call_established=true;
 
1576
      }
 
1577
 
 
1578
      reply_code = 0;
 
1579
    } else {
 
1580
      ERROR_P1("SIP method too long in received message '%s'",
 
1581
               msg);
 
1582
    }
 
1583
  } else {
 
1584
    ERROR_P1("Invalid sip message received '%s'",
 
1585
             msg);
 
1586
  }
 
1587
    
 
1588
  /* Try to find it in the expected non mandatory responses
 
1589
   * until the first mandatory response  in the scenario */
 
1590
  for(search_index = msg_index;
 
1591
      search_index < scenario_len;
 
1592
      search_index++) {
 
1593
    
 
1594
    if(!MATCHES_SCENARIO(search_index)) {
 
1595
      if(scenario[search_index] -> optional) {
 
1596
        continue;
 
1597
      }
 
1598
      /* The received message is different for the expected one */
 
1599
      break;
 
1600
    }
 
1601
 
 
1602
    found = true;
 
1603
    /* TODO : this is a little buggy: If a 100 trying from an INVITE
 
1604
     * is delayed by the network until the BYE is sent, it may
 
1605
     * stop BYE transmission erroneously, if the BYE also expects
 
1606
     * a 100 trying. */    
 
1607
    break;
 
1608
  }
 
1609
  
 
1610
  /* Try to find it in the old non-mandatory receptions */
 
1611
  if(!found) {
 
1612
    for(search_index = msg_index - 1;
 
1613
        search_index >= 0;
 
1614
        search_index--) {
 
1615
      if(MATCHES_SCENARIO(search_index) && 
 
1616
         ((scenario[search_index] -> optional))) {
 
1617
        found = true;
 
1618
        break;
 
1619
      }
 
1620
    }
 
1621
  }
 
1622
 
 
1623
  int test = (!found) ? -1 : scenario[search_index]->test;
 
1624
  /* test==0: No branching"
 
1625
   * test==-1 branching without testing"
 
1626
   * test>0   branching with testing
 
1627
   */
 
1628
 
 
1629
  // Action treatment
 
1630
  if (found) {
 
1631
    //WARNING_P1("---EXECUTE_ACTION_ON_MSG---%s---", msg); 
 
1632
    
 
1633
    actionResult = executeAction(msg, search_index);
 
1634
 
 
1635
    if(actionResult != call::E_AR_NO_ERROR) {
 
1636
      // Store last action result if it is an error
 
1637
      // and go on with the scenario
 
1638
      call::last_action_result = actionResult;
 
1639
      if (actionResult == E_AR_STOP_CALL) {
 
1640
          return rejectCall();
 
1641
      }
 
1642
      // handle the error
 
1643
      return next();
 
1644
    }
 
1645
  }
 
1646
  
 
1647
  /* Not found */
 
1648
  if(!found) {
 
1649
    if ((L_case = checkAutomaticResponseMode(request)) == 0) {
 
1650
      if (!process_unexpected(msg)) {
 
1651
        return false; // Call aborted by unexpected message handling
 
1652
      }
 
1653
    } else {
 
1654
      automaticResponseMode(L_case, msg);
 
1655
      return false ; // call aborted by automatic response mode
 
1656
    }
 
1657
  }
 
1658
 
 
1659
  /* Simulate loss of messages */
 
1660
  if(lost(scenario[search_index] -> lost)) {
 
1661
    TRACE_MSG((s, "%s message lost (recv).", 
 
1662
               TRANSPORT_TO_STRING(transport)));
 
1663
    if(comp_state) { comp_free(&comp_state); }
 
1664
    scenario[search_index] -> nb_lost++;
 
1665
    return true;
 
1666
  }
 
1667
  
 
1668
  /* This is an ACK or a response, and its index is greater than the 
 
1669
   * current active retransmission message, so we stop the retrans timer. */
 
1670
  if(((reply_code) ||
 
1671
      (!strcmp(request, "ACK")))  && 
 
1672
     (search_index > last_send_index)) {
 
1673
    next_retrans = 0;
 
1674
  }
 
1675
  
 
1676
  /* This is a response with 200 so set the flag indicating that an
 
1677
   * ACK is pending (used to prevent from release a call with CANCEL
 
1678
   * when an ACK+BYE should be sent instead)                         */
 
1679
  if (reply_code == 200) {
 
1680
    ack_is_pending = true;
 
1681
  }
 
1682
  
 
1683
  /* If this message can be used to compute RTD, do it now */
 
1684
  if(!rtd_done) {
 
1685
    if(scenario[search_index] -> start_rtd) {
 
1686
      start_time_rtd = clock_tick;
 
1687
    }
 
1688
 
 
1689
    if(scenario[search_index] -> stop_rtd) {
 
1690
      rtd_sum += clock_tick - start_time_rtd; 
 
1691
      CStat::instance()->
 
1692
        computeStat(CStat::E_ADD_RESPONSE_TIME_DURATION, 
 
1693
                    clock_tick - start_time_rtd);
 
1694
      rtd_nb ++;
 
1695
      rtd_done = true;
 
1696
    }
 
1697
  }
 
1698
 
 
1699
  /* Increment the recv counter */
 
1700
  scenario[search_index] -> nb_recv++;
 
1701
 
 
1702
  /* store the route set only once. TODO: does not support target refreshes!! */
 
1703
  if (scenario[search_index] -> bShouldRecordRoutes &&
 
1704
          NULL == dialog_route_set ) {
 
1705
 
 
1706
      /* should cache the route set */
 
1707
      if (reply_code) {
 
1708
 
 
1709
          /* is a reply. */
 
1710
          char rr[MAX_HEADER_LEN];
 
1711
          strcpy(rr, get_header_content(msg, (char*)"Record-Route:"));
 
1712
 
 
1713
          char actual_rr[MAX_HEADER_LEN];
 
1714
          memset(actual_rr, 0, sizeof(actual_rr));
 
1715
 
 
1716
          bool isFirst(true);
 
1717
          while (1)
 
1718
          {
 
1719
              char* pointer = strrchr(rr, ',');
 
1720
              if (pointer) {
 
1721
                if (!isFirst) {
 
1722
                  strcat(actual_rr, pointer + 1);
 
1723
                } else {
 
1724
                  isFirst = false;
 
1725
                }
 
1726
              } else {
 
1727
                if (!isFirst) {
 
1728
                  strcat(actual_rr, rr);
 
1729
                }
 
1730
                break;
 
1731
              }
 
1732
              *pointer = '\0';
 
1733
          }
 
1734
          /* lose the bottom most record route -- that is the SUT */
 
1735
 
 
1736
          char ch[MAX_HEADER_LEN];
 
1737
          strcpy(ch, get_header_content(msg, (char*)"Contact:"));
 
1738
 
 
1739
          if (strlen(actual_rr)) {
 
1740
              dialog_route_set = (char *)
 
1741
                  calloc(1, strlen(actual_rr) + strlen(ch) + 2);
 
1742
              sprintf(dialog_route_set, "%s,%s", actual_rr, ch);
 
1743
          } else {
 
1744
              dialog_route_set = (char *)
 
1745
                  calloc(1, strlen(ch) + 2);
 
1746
              sprintf(dialog_route_set, "%s", ch);
 
1747
          }
 
1748
      }
 
1749
      else {
 
1750
 
 
1751
          /* is a request. */
 
1752
          char rr[MAX_HEADER_LEN];
 
1753
          strcpy(rr, get_header_content(msg, (char*)"Record-Route:"));
 
1754
 
 
1755
          /* toss the first RR, it is going to be the SUT */
 
1756
          char* actual_rr = strchr(rr, ',');
 
1757
 
 
1758
          if (actual_rr) {
 
1759
              actual_rr += 1;
 
1760
          }
 
1761
 
 
1762
          char ch[MAX_HEADER_LEN];
 
1763
          strcpy(ch, get_header_content(msg, (char*)"Contact:"));
 
1764
 
 
1765
          if (actual_rr) {
 
1766
              dialog_route_set = (char *)
 
1767
                  calloc(1, strlen(actual_rr) + strlen(ch) + 2);
 
1768
              sprintf(dialog_route_set, "%s,%s", actual_rr, ch);
 
1769
          } else {
 
1770
              dialog_route_set = (char *)
 
1771
                  calloc(1, strlen(ch) + 2);
 
1772
              sprintf(dialog_route_set, "%s", ch);
 
1773
          }
 
1774
      }
 
1775
  }
 
1776
 
 
1777
#ifdef _USE_OPENSSL
 
1778
  /* store the authentication info */
 
1779
  if ((scenario[search_index] -> bShouldAuthenticate) && 
 
1780
          (reply_code == 401 || reply_code == 407)) {
 
1781
 
 
1782
      /* is a challenge */
 
1783
      char auth[MAX_HEADER_LEN];
 
1784
      memset(auth, 0, sizeof(auth));
 
1785
      strcpy(auth, get_header_content(msg, (char*)"Proxy-Authenticate:"));
 
1786
      if (auth[0] == 0) {
 
1787
        strcpy(auth, get_header_content(msg, (char*)"WWW-Authenticate:"));
 
1788
      }
 
1789
 
 
1790
 
 
1791
      dialog_authentication = (char *) calloc(1, strlen(auth) + 2);
 
1792
      sprintf(dialog_authentication, "%s", auth);
 
1793
 
 
1794
      /* Store the code of the challenge for building the proper header */
 
1795
      dialog_challenge_type = reply_code;
 
1796
  }
 
1797
#endif
 
1798
 
 
1799
  /* If this was a mandatory message, and keeps its cookie for
 
1800
   * future retransmissions, and its body for fields inclusion
 
1801
   * in our messages. Similarly if there is an explicit next label set 
 
1802
   */
 
1803
  if (!(scenario[search_index] -> optional) ||
 
1804
       scenario[search_index]->next && 
 
1805
      ((test == -1) ||
 
1806
       (test < SCEN_VARIABLE_SIZE && M_callVariableTable[test] != NULL && M_callVariableTable[test]->isSet()))
 
1807
     ) {
 
1808
    msg_index = search_index;
 
1809
 
 
1810
    /* Store last recv msg information */
 
1811
    last_recv_index = search_index;
 
1812
    last_recv_hash = cookie;
 
1813
 
 
1814
    last_recv_msg = (char *) realloc(last_recv_msg, strlen(msg) + 1);
 
1815
    strcpy(last_recv_msg, msg);
 
1816
    return next();
 
1817
  }
 
1818
  return true;
 
1819
}
 
1820
  
 
1821
call::T_ActionResult call::executeAction(char * msg, int scenarioIndex)
 
1822
{
 
1823
  CActions*  actions;
 
1824
  CAction*   currentAction;
 
1825
  CVariable* scenVariable;
 
1826
  char       msgPart[MAX_SUB_MESSAGE_LENGTH];
 
1827
  int        currentId;
 
1828
 
 
1829
  actions = scenario[scenarioIndex]->M_actions;
 
1830
  // looking for action to do on this message
 
1831
  if(actions != NULL) {
 
1832
    for(int i=0; i<actions->getUsedAction(); i++) {
 
1833
      currentAction = actions->getAction(i);
 
1834
      if(currentAction != NULL) {
 
1835
        if(currentAction->getActionType() == CAction::E_AT_ASSIGN_FROM_REGEXP) {
 
1836
          currentId = currentAction->getVarId();
 
1837
          scenVariable = scenVariableTable[currentId];
 
1838
          if(scenVariable != NULL) {
 
1839
            if(currentAction->getLookingPlace() == CAction::E_LP_HDR) {
 
1840
              extractSubMessage
 
1841
                                (msg, 
 
1842
                                currentAction->getLookingChar(), 
 
1843
                                msgPart);
 
1844
        
 
1845
              if(strlen(msgPart) > 0) {
 
1846
          
 
1847
                scenVariable->executeRegExp
 
1848
                                  (msgPart, 
 
1849
                                  M_callVariableTable,
 
1850
                                  currentId,
 
1851
                                  currentAction->getNbSubVarId(),
 
1852
                                  currentAction->getSubVarId()
 
1853
                                  );
 
1854
          
 
1855
                if( (!(M_callVariableTable[currentId]->isSet())) 
 
1856
                && (currentAction->getCheckIt() == true) ) {
 
1857
                  // the message doesn't match and the checkit 
 
1858
                  // action say it MUST match
 
1859
                  // Allow easier regexp debugging
 
1860
                  WARNING_P2("Failed regexp match: looking "
 
1861
                  "in '%s', with regexp '%s'", 
 
1862
                  msgPart, 
 
1863
                  scenVariable->
 
1864
                  getRegularExpression());
 
1865
                  // --> Call will be marked as failed
 
1866
                  return(call::E_AR_REGEXP_DOESNT_MATCH);
 
1867
                }
 
1868
              } else {// sub part of message not found
 
1869
                if( currentAction->getCheckIt() == true ) {
 
1870
                  // the sub message is not found and the
 
1871
                  // checking action say it MUST match
 
1872
                  // --> Call will be marked as failed but 
 
1873
                  // will go on
 
1874
                  return(call::E_AR_HDR_NOT_FOUND);
 
1875
                } 
 
1876
              }
 
1877
            } else {// we must look in the entire message
 
1878
              // WARNING_P1("LOOKING IN MSG -%s-", msg);
 
1879
                scenVariable->executeRegExp
 
1880
                              (msg, 
 
1881
                                  M_callVariableTable,
 
1882
                                  currentId,
 
1883
                                  currentAction->getNbSubVarId(),
 
1884
                                  currentAction->getSubVarId()
 
1885
                                  );
 
1886
 
 
1887
                   
 
1888
              if((!(M_callVariableTable[currentId]->isSet())) 
 
1889
              && (currentAction->getCheckIt() == true) ) {
 
1890
                // the message doesn't match and the checkit 
 
1891
                // action say it MUST match
 
1892
                // Allow easier regexp debugging
 
1893
                WARNING_P2("Failed regexp match: looking in '%s'"
 
1894
                ", with regexp '%s'", 
 
1895
                msg, 
 
1896
                scenVariable->getRegularExpression());
 
1897
                // --> rejecting the call
 
1898
                return(call::E_AR_REGEXP_DOESNT_MATCH);
 
1899
              }
 
1900
            }
 
1901
          } // end if scen variable != null
 
1902
        } else /* end action == E_AT_ASSIGN_FROM_REGEXP */ 
 
1903
            if (currentAction->getActionType() == CAction::E_AT_LOG_TO_FILE) {
 
1904
            char* x = createSendingMessage(currentAction->getMessage(), -2 /* do not add crlf*/);
 
1905
            LOG_MSG((s, "%s\n", x));
 
1906
        } else /* end action == E_AT_LOG_TO_FILE */ 
 
1907
            if (currentAction->getActionType() == CAction::E_AT_EXECUTE_CMD) {
 
1908
 
 
1909
            if (currentAction->getCmdLine()) {
 
1910
                char* x = createSendingMessage(currentAction->getCmdLine(), -2 /* do not add crlf*/);
 
1911
                // TRACE_MSG((s, "Trying to execute [%s]", x)); 
 
1912
                pid_t l_pid;
 
1913
                switch(l_pid = fork())
 
1914
                {
 
1915
                    case -1:
 
1916
                        // error when forking !
 
1917
                        ERROR("Forking error");
 
1918
                        break;
 
1919
 
 
1920
                    case 0:
 
1921
                        // child process - execute the command
 
1922
                        system(x);
 
1923
                        exit(EXIT_OTHER);
 
1924
 
 
1925
                    default:
 
1926
                        // parent process continue
 
1927
                        break;
 
1928
                }
 
1929
            }
 
1930
        } else /* end action == E_AT_LOG_TO_FILE */ 
 
1931
            if (currentAction->getActionType() == CAction::E_AT_EXEC_INTCMD) {
 
1932
                switch (currentAction->getIntCmd())
 
1933
                {
 
1934
                    case CAction::E_INTCMD_STOP_ALL:
 
1935
                        quitting = 1;
 
1936
                        break;
 
1937
                    case CAction::E_INTCMD_STOP_NOW:
 
1938
                        screen_exit(EXIT_TEST_RES_INTERNAL);
 
1939
                        break;
 
1940
                    case CAction::E_INTCMD_STOPCALL:
 
1941
                    default:
 
1942
                        return(call::E_AR_STOP_CALL);
 
1943
                        break;
 
1944
                }
 
1945
        } else {// end action == E_AT_EXECUTE_CMD
 
1946
          ERROR("call::executeAction unknown action");
 
1947
        }
 
1948
      } // end if current action != null
 
1949
    } // end for
 
1950
  }
 
1951
  return(call::E_AR_NO_ERROR);
 
1952
}
 
1953
 
 
1954
void call::extractSubMessage(char * msg, char * matchingString, char* result)
 
1955
{
 
1956
  char * ptr;
 
1957
  int sizeOf;
 
1958
  int i = 0;
 
1959
  int len;
 
1960
 
 
1961
  ptr = strstr(msg, matchingString); 
 
1962
  if(ptr != NULL) {
 
1963
    len = strlen(matchingString);
 
1964
    strcpy(result, ptr+len);
 
1965
    sizeOf = strlen(result);
 
1966
    if(sizeOf >= MAX_SUB_MESSAGE_LENGTH)  
 
1967
      sizeOf = MAX_SUB_MESSAGE_LENGTH-1;
 
1968
    while((i<sizeOf) && (result[i] != '\n') && (result[i] != '\r'))
 
1969
      i++;
 
1970
    result[i] = '\0';
 
1971
  } else {
 
1972
    result[0] = '\0';
 
1973
  }
 
1974
}
 
1975
 
 
1976
void call::dumpFileContents(void)
 
1977
{
 
1978
    WARNING_P3("Line choosing strategy is [%s]. m_counter [%d] numLinesInFile [%d]",
 
1979
               m_usage == InputFileSequentialOrder ? "SEQUENTIAL" : "RANDOM",
 
1980
               m_counter, numLinesInFile);
 
1981
 
 
1982
    for (int i(0); i < numLinesInFile && fileContents[i][0]; ++i) {
 
1983
        WARNING_P2("%dth line reads [%s]", i, fileContents[i].c_str());
 
1984
    }
 
1985
}
 
1986
 
 
1987
/* Read MAX_CHAR_BUFFER_SIZE size lines from the
 
1988
 * "fileName" and populate it in the fileContents
 
1989
 * vector. The file should not be more than
 
1990
 * MAX_LINES_IN_FILE lines long and each line
 
1991
 * should be terminated with a '\n'
 
1992
 */
 
1993
 
 
1994
void call::readInputFileContents(const char* fileName)
 
1995
{
 
1996
  ifstream *inFile    = new ifstream(fileName);
 
1997
  ifstream &inFileObj = *inFile;
 
1998
  char      line[MAX_CHAR_BUFFER_SIZE];
 
1999
  
 
2000
  if (!inFile->good()) {
 
2001
    ERROR_P1("Unable to open file %s", fileName);
 
2002
    return ;
 
2003
  }
 
2004
 
 
2005
  numLinesInFile = 0;
 
2006
  call::m_counter = 0;
 
2007
  line[0] = '\0';
 
2008
  inFileObj.getline(line, MAX_CHAR_BUFFER_SIZE);
 
2009
 
 
2010
  if (NULL != strstr(line, "RANDOM")) {
 
2011
      call::m_usage = InputFileRandomOrder;
 
2012
  } else if (NULL != strstr(line, "SEQUENTIAL")) {
 
2013
      call::m_usage = InputFileSequentialOrder;
 
2014
  } else {
 
2015
      // default
 
2016
      call::m_usage = InputFileSequentialOrder;
 
2017
  }
 
2018
 
 
2019
  while (!inFileObj.eof()) {
 
2020
    line[0] = '\0';
 
2021
    inFileObj.getline(line, MAX_CHAR_BUFFER_SIZE);
 
2022
    if (line[0]) {
 
2023
      if ('#' != line[0]) {
 
2024
        fileContents.push_back(line);
 
2025
        numLinesInFile++; /* this counts number of valid data lines */
 
2026
      }
 
2027
    } else {
 
2028
      break;
 
2029
    }
 
2030
  }
 
2031
  // call::dumpFileContents();
 
2032
  delete inFile;
 
2033
}
 
2034
 
 
2035
void call::getFieldFromInputFile(const char* keyword, int lineNum, char*& dest)
 
2036
{
 
2037
  int nthField    = atoi(keyword+5 /*strlen("field")*/);
 
2038
  int origNth     = nthField;
 
2039
  
 
2040
  if (fileContents.size() > lineNum) {
 
2041
    const string& line = fileContents[lineNum];
 
2042
    
 
2043
    // WARNING_P3("lineNum [%d] nthField [%d] line [%s]",
 
2044
    //         lineNum, nthField, line.c_str());
 
2045
    
 
2046
    int pos(0), oldpos(0);
 
2047
    do {
 
2048
      oldpos = pos;
 
2049
      int localint = line.find(';', oldpos);
 
2050
      
 
2051
      if (localint != string::npos) {
 
2052
        pos = localint + 1;
 
2053
      } else {
 
2054
        pos = localint;
 
2055
        break;
 
2056
      }
 
2057
      
 
2058
      //string x = line.substr(oldpos, pos - oldpos);
 
2059
      // WARNING_P3("pos [%d] oldpos [%d] is [%s]", pos, oldpos, x.c_str());
 
2060
      
 
2061
      if (nthField) {
 
2062
        --nthField;
 
2063
      } else {
 
2064
        break;
 
2065
      }
 
2066
      
 
2067
    } while (oldpos != string::npos);
 
2068
    
 
2069
    if (nthField) {
 
2070
      WARNING_P1("Field %d not found in the file", origNth);
 
2071
      // field not found in line
 
2072
    } else {
 
2073
      if (string::npos != oldpos) {
 
2074
        if (string::npos != pos) {
 
2075
          // should not be decremented for fieldN
 
2076
          pos -= (oldpos + 1);
 
2077
        }
 
2078
    
 
2079
        string x = line.substr(oldpos, pos);
 
2080
        if (x.length()) {
 
2081
        dest += sprintf(dest, "%s", x.c_str());
 
2082
        }
 
2083
        
 
2084
        // WARNING_P2("nthField [%d] is [%s]", origNth, x.c_str());
 
2085
      }
 
2086
    }
 
2087
  } else {
 
2088
    // WARNING_P1("Field %d definition not found", nthField);
 
2089
  }
 
2090
}
 
2091
 
 
2092
int  call::checkAutomaticResponseMode(char * P_recv) {
 
2093
 
 
2094
  int L_res = 0 ;
 
2095
 
 
2096
  if (strcmp(P_recv, "BYE")==0) {
 
2097
    L_res = 1 ;
 
2098
  } else if (strcmp(P_recv, "CANCEL") == 0) {
 
2099
    L_res = 2 ;
 
2100
  } else if (strcmp(P_recv, "PING") == 0) {
 
2101
    L_res = 3 ;
 
2102
  }
 
2103
 
 
2104
  return (L_res) ;
 
2105
  
 
2106
}
 
2107
 
 
2108
 
 
2109
void call::automaticResponseMode(int P_case, char * P_recv)
 
2110
{
 
2111
 
 
2112
  int res ;
 
2113
 
 
2114
  // usage of last_ keywords
 
2115
  last_recv_msg = (char *) realloc(last_recv_msg, strlen(P_recv) + 1);
 
2116
  strcpy(last_recv_msg, P_recv);
 
2117
 
 
2118
  switch (P_case) {
 
2119
  case 1: // response for an unexpected BYE
 
2120
    if (default_behavior) {
 
2121
      WARNING_P1("Aborting call on an unexpected BYE for call: %s", (id==NULL)?"none":id);
 
2122
    res = sendBuffer(createSendingMessage(
 
2123
                    (char*)"SIP/2.0 200 OK\n"
 
2124
                    "[last_Via:]\n"
 
2125
                    "[last_From:]\n"
 
2126
                    "[last_To:]\n"
 
2127
                    "[last_Call-ID:]\n"
 
2128
                    "[last_CSeq:]\n"
 
2129
                    "Contact: <sip:[local_ip]:[local_port];transport=[transport]>\n"
 
2130
                    "Content-Length: 0\n"
 
2131
                    , -1)) ;
 
2132
 
 
2133
#ifdef __3PCC__
 
2134
    // if twin socket call => reset the other part here 
 
2135
    if (twinSippSocket && (msg_index > 0)) {
 
2136
      res = sendCmdBuffer
 
2137
      (createSendingMessage((char*)"call-id: [call_id]\ninternal-cmd: abort_call\n", -1));
 
2138
    }
 
2139
#endif /* __3PCC__ */
 
2140
      CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
2141
      CStat::instance()->computeStat(CStat::E_FAILED_UNEXPECTED_MSG);
 
2142
      delete_call(id);
 
2143
    } else {
 
2144
      WARNING_P1("Continuing call on an unexpected BYE for call: %s", (id==NULL)?"none":id);
 
2145
    }
 
2146
      break ;
 
2147
      
 
2148
  case 2: // response for an unexpected cancel
 
2149
    if (default_behavior) {
 
2150
      WARNING_P1("Aborting call on an unexpected CANCEL for call: %s", (id==NULL)?"none":id);
 
2151
    res = sendBuffer(createSendingMessage(
 
2152
                      (char*)"SIP/2.0 200 OK\n"
 
2153
                      "[last_Via:]\n"
 
2154
                      "[last_From:]\n"
 
2155
                      "[last_To:]\n"
 
2156
                      "[last_Call-ID:]\n"
 
2157
                      "[last_CSeq:]\n"
 
2158
                      "Contact: sip:sipp@[local_ip]:[local_port]\n"
 
2159
                      "Content-Length: 0"
 
2160
                      , -1)) ;
 
2161
    
 
2162
#ifdef __3PCC__
 
2163
    // if twin socket call => reset the other part here 
 
2164
    if (twinSippSocket && (msg_index > 0)) {
 
2165
      res = sendCmdBuffer
 
2166
      (createSendingMessage((char*)"call-id: [call_id]\ninternal-cmd: abort_call\n", -1));
 
2167
    }
 
2168
#endif /* __3PCC__ */
 
2169
    
 
2170
    CStat::instance()->computeStat(CStat::E_CALL_FAILED);
 
2171
    CStat::instance()->computeStat(CStat::E_FAILED_UNEXPECTED_MSG);
 
2172
    delete_call(id);
 
2173
    } else {
 
2174
      WARNING_P1("Continuing call on unexpected CANCEL for call: %s", (id==NULL)?"none":id);
 
2175
    }
 
2176
    break ;
 
2177
      
 
2178
  case 3: // response for a random ping
 
2179
    if (default_behavior) {
 
2180
    WARNING_P1("Automatic response mode for an unexpected PING for call: %s", (id==NULL)?"none":id);
 
2181
    count_in_stats = false; // Call must not be counted in statistics
 
2182
    res = sendBuffer(createSendingMessage(
 
2183
                    (char*)"SIP/2.0 200 OK\n"
 
2184
                    "[last_Via:]\n"
 
2185
                    "[last_Call-ID:]\n"
 
2186
                    "[last_To:]\n"
 
2187
                    "[last_From:]\n"
 
2188
                    "[last_CSeq:]\n"
 
2189
                    "Contact: sip:sipp@[local_ip]:[local_port]\n"
 
2190
                    "Content-Length: 0"
 
2191
                    , -1)) ;
 
2192
    // Note: the call ends here but it is not marked as bad. PING is a 
 
2193
    //       normal message.
 
2194
#ifdef __3PCC__
 
2195
    // if twin socket call => reset the other part here 
 
2196
    if (twinSippSocket && (msg_index > 0)) {
 
2197
      res = sendCmdBuffer
 
2198
      (createSendingMessage((char*)"call-id: [call_id]\ninternal-cmd: abort_call\n",-1));
 
2199
    }
 
2200
#endif /* __3PCC__ */
 
2201
    
 
2202
    CStat::instance()->computeStat(CStat::E_AUTO_ANSWERED);
 
2203
    delete_call(id);
 
2204
    } else {
 
2205
      WARNING_P1("Do not answer on an unexpected PING for call: %s", (id==NULL)?"none":id);
 
2206
    }
 
2207
    break ;
 
2208
 
 
2209
    default:
 
2210
    ERROR_P1("Internal error for automaticResponseMode %d", P_case);
 
2211
    break ;
 
2212
  }
 
2213
  
 
2214
}
 
2215
 
 
2216