322ac2671398a7bb428c5404fbd39605dd6fba83
[sip-router] / modules / tm / t_funcs.c
1 /*
2  * $Id$
3  *
4  */
5
6 #include "hash_func.h"
7 #include "t_funcs.h"
8 #include "../../dprint.h"
9 #include "../../config.h"
10 #include "../../parser/parser_f.h"
11 #include "../../ut.h"
12 //#include "../../timer.h"
13
14
15
16 #define  append_mem_block(_d,_s,_len) \
17                 do{\
18                         memcpy((_d),(_s),(_len));\
19                         (_d) += (_len);\
20                 }while(0);
21 #define  req_line(_msg) \
22                 ((_msg)->first_line.u.request)
23
24
25
26 struct cell      *T;
27 unsigned int     global_msg_id;
28 struct s_table*  hash_table;
29 unsigned int     nr_forks;
30 struct fork      t_forks[ NR_OF_CLIENTS ];
31
32
33
34 void timer_routine(unsigned int, void*);
35
36
37
38 int tm_startup()
39 {
40         /* building the hash table*/
41         hash_table = init_hash_table();
42         if (!hash_table)
43                 return -1;
44
45         /* init. timer lists */
46         hash_table->timers[RT_T1_TO_1].id = RT_T1_TO_1;
47         hash_table->timers[RT_T1_TO_2].id = RT_T1_TO_2;
48         hash_table->timers[RT_T1_TO_3].id = RT_T1_TO_3;
49         hash_table->timers[RT_T2].id      = RT_T2;
50         hash_table->timers[FR_TIMER_LIST].id     = FR_TIMER_LIST;
51         hash_table->timers[FR_INV_TIMER_LIST].id = FR_INV_TIMER_LIST;
52         hash_table->timers[WT_TIMER_LIST].id     = WT_TIMER_LIST;
53         hash_table->timers[DELETE_LIST].id       = DELETE_LIST;
54
55         /* register the timer function */
56         register_timer( timer_routine , hash_table , 1 );
57
58         /* fork table */
59         nr_forks = 0;
60
61         /*first msg id*/
62         global_msg_id = 0;
63         T = T_UNDEFINED;
64
65         return 0;
66 }
67
68
69
70
71 void tm_shutdown()
72 {
73         struct timer_link  *tl, *end, *tmp;
74         int i;
75
76         DBG("DEBUG: tm_shutdown : start\n");
77         /* remember the DELETE LIST */
78         tl = hash_table->timers[DELETE_LIST].first_tl.next_tl;
79         end = & hash_table->timers[DELETE_LIST].last_tl;
80         /* unlink the timer lists */
81         for( i=0; i<NR_OF_TIMER_LISTS ; i++ )
82                 reset_timer_list( hash_table, i );
83
84         DBG("DEBUG: tm_shutdown : empting DELETE list\n");
85         /* deletes all cells from DELETE_LIST list
86         (they are no more accessible from enrys) */
87         while (tl!=end) {
88                 tmp=tl->next_tl;
89                 free_cell((struct cell*)tl->payload);
90                 tl=tmp;
91         }
92
93         /* destroy the hash table */
94         DBG("DEBUG: tm_shutdown : empting hash table\n");
95         free_hash_table( hash_table );
96         DBG("DEBUG: tm_shutdown : removing semaphores\n");
97         lock_cleanup();
98         DBG("DEBUG: tm_shutdown : done\n");
99 }
100
101
102
103
104 /* function returns:
105  *       1 - a new transaction was created
106  *      -1 - error, including retransmission
107  */
108 int t_add_transaction( struct sip_msg* p_msg )
109 {
110         struct cell*    new_cell;
111
112         DBG("DEBUG: t_add_transaction: adding......\n");
113         /* sanity check: ACKs can never establish a transaction */
114         if ( p_msg->REQ_METHOD==METHOD_ACK )
115         {
116                 LOG(L_ERR, "ERROR: add_transaction: ACK can't be used to add"
117                         " transaction\n");
118                 return -1;
119         }
120
121         /* creates a new transaction */
122         new_cell = build_cell( p_msg ) ;
123         DBG("DEBUG: t_add_transaction: new transaction created %p\n", new_cell);
124         if  ( !new_cell ){
125                 LOG(L_ERR, "ERROR: add_transaction: out of mem:\n");
126                 sh_status();
127                 return -1;
128         }
129         /*insert the transaction into hash table*/
130         insert_into_hash_table( hash_table , new_cell );
131         DBG("DEBUG: t_add_transaction: new transaction inserted, hash: %d\n",
132                 new_cell->hash_index );
133
134         T = new_cell;
135         T_REF(T);
136         return 1;
137 }
138
139
140
141
142 /*   returns 1 if everything was OK or -1 for error
143 */
144 int t_release_transaction( struct sip_msg* p_msg)
145 {
146       return t_put_on_wait( T );
147 }
148
149
150
151 int t_unref( /* struct sip_msg* p_msg */ )
152 {
153         if (T==T_UNDEFINED || T==T_NULL)
154                 return -1;
155         T_UNREF( T );
156         T=T_UNDEFINED;
157         return 1;
158 }
159
160
161
162
163
164 /* ----------------------------HELPER FUNCTIONS-------------------------------- */
165
166
167 int t_update_timers_after_sending_reply( struct retr_buf *rb )
168 {
169         struct cell *Trans = rb->my_T;
170
171         /* make sure that if we send something final upstream, everything else
172            will be cancelled */
173         if (Trans->uas.status>=300&&Trans->uas.request->REQ_METHOD==METHOD_INVITE)
174         {
175                 rb->retr_list = RT_T1_TO_1;
176                 set_timer( hash_table, &(rb->retr_timer), RT_T1_TO_1 );
177                 set_timer( hash_table, &(rb->fr_timer), FR_TIMER_LIST );
178         } else if ( Trans->uas.request->REQ_METHOD==METHOD_CANCEL ) {
179                 if ( Trans->T_canceled==T_UNDEFINED )
180                         Trans->T_canceled = t_lookupOriginalT( hash_table ,
181                                 Trans->uas.request );
182                 if ( Trans->T_canceled==T_NULL )
183                         return 1;
184                 /* put CANCEL transaction on wait only if canceled transaction already
185                     is in final status and there is nothing to cancel; */
186                 if ( Trans->T_canceled->uas.status>=200)
187                         t_put_on_wait( Trans );
188         } else if (Trans->uas.status>=200)
189                 t_put_on_wait( Trans );
190    return 1;
191 }
192
193
194
195
196 /* Checks if the new reply (with new_code status) should be sent or not
197  *  based on the current
198  * transactin status.
199  * Returns      - branch number (0,1,...) which should be relayed
200  *         -1 if nothing to be relayed
201  */
202 int t_should_relay_response( struct cell *Trans , int new_code,
203                                                                         int branch , int *should_store )
204 {
205         //int T_code;
206         int b, lowest_b, lowest_s;
207
208         //if (Trans->uas.request->REQ_METHOD==METHOD_INVITE)
209         //      T_code = Trans->uac[branch].status;
210         //else
211         //T_code = Trans->uas.status;
212
213         /* note: this code never lets replies to CANCEL go through;
214            we generate always a local 200 for CANCEL; 200s are
215            not relayed because it's not an INVITE transaction;
216            >= 300 are not relayed because 200 was already sent
217            out
218         */
219         DBG("->>>>>>>>> T_code=%d, new_code=%d\n",Trans->uas.status,new_code);
220         /* if final response sent out, allow only INVITE 2xx  */
221         if ( Trans->uas.status >= 200 ) {
222                 if (new_code>=200 && new_code < 300  && 
223                         Trans->uas.request->REQ_METHOD==METHOD_INVITE) {
224                         DBG("DBG: t_should_relay: 200 INV after final sent\n");
225                         *should_store=1;
226                         return branch;
227                 } else {
228                         *should_store=0;
229                         return -1;
230                 }
231         } else { /* no final response sent yet */
232                 /* negative replies subject to fork picking */
233                 if (new_code >=300 ) {
234                         *should_store=1;
235                         /* if all_final return lowest */
236                         lowest_b=-1; lowest_s=999;
237                         for ( b=0; b<Trans->nr_of_outgoings ; b++ ) {
238                                 /* "fake" for the currently processed branch */
239                                 if (b==branch) {
240                                         if (new_code<lowest_s) {
241                                                 lowest_b=b;
242                                                 lowest_s=new_code;
243                                         }
244                                         continue;
245                                 }
246                                 /* there is still an unfinished UAC transaction; wait now! */
247                                 if ( Trans->uac[b].status<200 )
248                                         return -1;
249                                 if ( Trans->uac[b].status<lowest_s )
250                                 {
251                                         lowest_b =b;
252                                         lowest_s = T->uac[b].status;
253                                 }
254                         }
255                         return lowest_b;
256                 /* 1xx except 100 and 2xx will be relayed */
257                 } else if (new_code>100) {
258                         *should_store=1;
259                         return branch;
260                 }
261                 /* 100 won't be relayed */
262                 else {
263                         if (!T->uac[branch].rpl_received) *should_store=1;
264                                 else *should_store=0;
265                         if (Trans->uas.status==0) return branch;
266                                 else return -1;
267                 }
268         }
269
270         LOG(L_CRIT, "ERROR: Oh my gooosh! We don't know whether to relay\n");
271         abort();
272 }
273
274
275 /*
276   */
277 int t_put_on_wait(  struct cell  *Trans  )
278 {
279         unsigned int i;
280         //struct retrans_buff* rb;
281
282 #ifndef WAIT
283         if (is_in_timer_list2( &(Trans->wait_tl)))
284         {
285                 DBG("DEBUG: t_put_on_wait: already on wait\n");
286                 return 1;
287         }
288 #else
289         /* have some race conditons occured and we already
290           entered/passed the wait status previously?
291           if so, exit now
292         */
293
294         LOCK_WAIT(Trans);
295         if (Trans->on_wait)
296         {
297                 DBG("DEBUG: t_put_on_wait: already on wait\n");
298                 UNLOCK_WAIT(Trans);
299                 return 1;
300         } else {
301                 Trans->on_wait=1;
302                 UNLOCK_WAIT(Trans);
303         }
304 #endif
305
306         /* remove from  retranssmision  and  final response   list */
307         DBG("DEBUG: t_put_on_wait: stopping timers (FR and RETR)\n");
308         reset_retr_timers(hash_table,Trans) ;
309
310 #ifdef SILENT_FR
311         if (Trans->nr_of_outgoings>1)
312 #endif
313         {
314         /* cancel pending client transactions, if any */
315         for( i=0 ; i<Trans->nr_of_outgoings ; i++ )
316                 if ( Trans->uac[i].rpl_received && Trans->uac[i].status<200 )
317                         t_build_and_send_CANCEL(Trans , i);
318         }
319
320         /* adds to Wait list*/
321         set_timer( hash_table, &(Trans->wait_tl), WT_TIMER_LIST );
322         return 1;
323 }
324
325
326
327
328 /* Builds a CANCEL request based on an INVITE request. CANCEL is send
329  * to same address as the INVITE */
330 int t_build_and_send_CANCEL(struct cell *Trans,unsigned int branch)
331 {
332         struct sip_msg      *p_msg;
333         struct hdr_field    *hdr;
334         char                *cancel_buf, *p, *via;
335         unsigned int         len, via_len;
336
337         if ( !Trans->uac[branch].rpl_received )
338         {
339                 DBG("DEBUG: t_build_and_send_CANCEL: no response ever received"
340                         " : dropping local cancel! \n");
341                 return 1;
342         }
343
344         if (Trans->uac[branch].request.cancel!=NO_CANCEL)
345         {
346                 DBG("DEBUG: t_build_and_send_CANCEL: branch (%d)was already canceled"
347                         " : dropping local cancel! \n",branch);
348                 return 1;
349         }
350
351         cancel_buf = 0;
352         via = 0;
353         p_msg = Trans->uas.request;
354
355         len = 0;
356         /*first line's len - CANCEL and INVITE has the same lenght */
357         len += ( req_line(p_msg).version.s+req_line(p_msg).version.len)-
358                 req_line(p_msg).method.s+CRLF_LEN;
359         /*check if the REQ URI was override */
360         if (Trans->uac[branch].uri.s)
361                 len += Trans->uac[branch].uri.len - req_line(p_msg).uri.len;
362         /*via*/
363         if ( add_branch_label(Trans,p_msg,branch)==-1 )
364                 goto error;
365         via = via_builder( p_msg , &via_len );
366         if (!via)
367         {
368                 LOG(L_ERR, "ERROR: t_build_and_send_CANCEL: "
369                         "no via header got from builder\n");
370                 goto error;
371         }
372         len+= via_len;
373         /*headers*/
374         for ( hdr=p_msg->headers ; hdr ; hdr=hdr->next )
375                 if (hdr->type==HDR_FROM || hdr->type==HDR_CALLID || 
376                         hdr->type==HDR_CSEQ || hdr->type==HDR_TO )
377                         len += ((hdr->body.s+hdr->body.len ) - hdr->name.s ) + CRLF_LEN ;
378         /* User Agent header*/
379         len += USER_AGENT_LEN + CRLF_LEN;
380         /* Content Lenght heder*/
381         len += CONTENT_LEN_LEN + CRLF_LEN;
382         /* end of message */
383         len += CRLF_LEN;
384
385         cancel_buf=sh_malloc( len+1 );
386         if (!cancel_buf)
387         {
388                 LOG(L_ERR, "ERROR: t_build_and_send_CANCEL: cannot allocate memory\n");
389                 goto error;
390         }
391         p = cancel_buf;
392
393         /* first line -> do we have a new URI? */
394         if (Trans->uac[branch].uri.s)
395         {
396                 append_mem_block(p,req_line(p_msg).method.s,
397                         req_line(p_msg).uri.s-req_line(p_msg).method.s);
398                 append_mem_block(p,Trans->uac[branch].uri.s,
399                         Trans->uac[branch].uri.len);
400                 append_mem_block(p,req_line(p_msg).uri.s+req_line(p_msg).uri.len,
401                         req_line(p_msg).version.s+req_line(p_msg).version.len-
402                         (req_line(p_msg).uri.s+req_line(p_msg).uri.len))
403         }else{
404                 append_mem_block(p,req_line(p_msg).method.s,
405                         req_line(p_msg).version.s+req_line(p_msg).version.len-
406                         req_line(p_msg).method.s);
407         }
408         /* changhing method name*/
409         memcpy(cancel_buf, CANCEL , CANCEL_LEN );
410         append_mem_block(p,CRLF,CRLF_LEN);
411         /* insert our via */
412         append_mem_block(p,via,via_len);
413
414         /*other headers*/
415         for ( hdr=p_msg->headers ; hdr ; hdr=hdr->next )
416         {
417                 if(hdr->type==HDR_FROM||hdr->type==HDR_CALLID||hdr->type==HDR_TO)
418                 {
419                         append_mem_block(p,hdr->name.s,
420                                 ((hdr->body.s+hdr->body.len)-hdr->name.s) );
421                         append_mem_block(p, CRLF, CRLF_LEN );
422                 }else if ( hdr->type==HDR_CSEQ )
423                 {
424                         append_mem_block(p,hdr->name.s,
425                                 ((((struct cseq_body*)hdr->parsed)->method.s)-hdr->name.s));
426                         append_mem_block(p, CANCEL CRLF, CANCEL_LEN +CRLF_LEN );
427                 }
428 }
429
430         /* User Agent header */
431         append_mem_block(p,USER_AGENT,USER_AGENT_LEN);
432         append_mem_block(p,CRLF,CRLF_LEN);
433         /* Content Lenght header*/
434         append_mem_block(p,CONTENT_LEN,CONTENT_LEN_LEN);
435         append_mem_block(p,CRLF,CRLF_LEN);
436         /* end of message */
437         append_mem_block(p,CRLF,CRLF_LEN);
438         *p=0;
439
440         if (Trans->uac[branch].request.cancel) {
441                 shm_free( cancel_buf );
442                 LOG(L_WARN, "send_cancel: Warning: CANCEL already sent out\n");
443                 goto error;
444         }
445
446         Trans->uac[branch].request.activ_type = TYPE_LOCAL_CANCEL;
447         Trans->uac[branch].request.cancel = cancel_buf;
448         Trans->uac[branch].request.cancel_len = len;
449
450         /*sets and starts the FINAL RESPONSE timer */
451         set_timer(hash_table,&(Trans->uac[branch].request.fr_timer),FR_TIMER_LIST);
452         /* sets and starts the RETRANS timer */
453         Trans->uac[branch].request.retr_list = RT_T1_TO_1;
454         set_timer(hash_table,&(Trans->uac[branch].request.retr_timer),RT_T1_TO_1);
455         DBG("DEBUG: T_build_and_send_CANCEL : sending cancel...\n");
456         SEND_CANCEL_BUFFER( &(Trans->uac[branch].request) );
457
458         pkg_free(via);
459         return 1;
460 error:
461         if (via) pkg_free(via);
462         return -1;
463 }
464
465
466
467
468
469
470 void delete_cell( struct cell *p_cell )
471 {
472 #ifdef EXTRA_DEBUG
473         int i;
474
475         if (is_in_timer_list2(& p_cell->wait_tl )) {
476                 LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"
477                         " still on WAIT\n", p_cell);
478                 abort();
479         }
480         /*
481         if (is_in_timer_list2(& p_cell->outbound_response.retr_timer )) {
482                 LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"
483                         " still on RETR (rep)\n",
484                         p_cell);
485                 abort();
486         }
487         if (is_in_timer_list2(& p_cell->outbound_response.fr_timer )) {
488                 LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"
489                         " still on FR (rep)\n", p_cell);
490                 abort();
491         }
492         for (i=0; i<p_cell->nr_of_outgoings; i++) {
493                 if (is_in_timer_list2(& p_cell->outbound_request[i]->retr_timer)) {
494                         LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"
495                                 " still on RETR (req %d)\n", p_cell, i);
496                         abort();
497                 }
498                 if (is_in_timer_list2(& p_cell->outbound_request[i]->fr_timer)) {
499                         LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"
500                                 " still on FR (req %d)\n", p_cell, i);
501                         abort();
502                 }
503         }
504         */
505         reset_retr_timers( hash_table, p_cell );
506 #endif
507         /* still in use ... don't delete */
508         if ( T_IS_REFED(p_cell) ) {
509 #ifdef  EXTRA_DEBUG
510                 if (T_REFCOUNTER(p_cell)>1) {
511                         DBG("DEBUG: while debugging with a single process, ref_count>1\n");
512                         DBG("DEBUG: transaction =%p\n", p_cell );
513                         abort();
514                 }
515 #endif
516                 DBG("DEBUG: delete_cell: t=%p post for delete (refbitmap %x,"
517                         " refcount %d)\n",p_cell,p_cell->ref_bitmap,T_REFCOUNTER(p_cell));
518                 /* it's added to del list for future del */
519                 set_timer( hash_table, &(p_cell->dele_tl), DELETE_LIST );
520         } else {
521                 DBG("DEBUG: delete transaction %p\n", p_cell );
522                 free_cell( p_cell );
523         }
524 }
525
526
527
528
529 /* Returns  -1 = error
530                     0 = OK
531 */
532 int get_ip_and_port_from_uri( str *uri , unsigned int *param_ip, unsigned int *param_port)
533 {
534         struct hostent  *nhost;
535         unsigned int    ip, port;
536         struct sip_uri  parsed_uri;
537         int             err;
538 #ifdef DNS_IP_HACK
539         int             len;
540 #endif
541
542         /* parsing the request uri in order to get host and port */
543         if (parse_uri( uri->s , uri->len , &parsed_uri )<0)
544         {
545                 LOG(L_ERR, "ERROR: get_ip_and_port_from_uri: "
546                    "unable to parse destination uri: %.*s\n", uri->len, uri->s );
547                 goto error;
548         }
549
550         /* getting the port */
551         if ( parsed_uri.port.s==0 || parsed_uri.port.len==0 )
552                 port = SIP_PORT;
553         else{
554                 port = str2s( (unsigned char*) parsed_uri.port.s, parsed_uri.port.len,
555                                                 &err );
556                 if ( err<0 ){
557                         LOG(L_ERR, "ERROR: get_ip_and_port_from_uri: converting port "
558                                 "from str to int failed; using default SIP port\n\turi:%.*s\n",
559                                 uri->len, uri->s );
560                         port = SIP_PORT;
561                 }
562         }
563         port = htons(port);
564
565         /* getting host address*/
566 #ifdef DNS_IP_HACK
567         len=strlen( parsed_uri.host.s );
568         ip=str2ip( (unsigned char*)parsed_uri.host.s, len, &err);
569         if (err==0)
570                 goto success;
571 #endif
572         /* fail over to normal lookup */
573         nhost = gethostbyname( parsed_uri.host.s );
574         if ( !nhost )
575         {
576                 LOG(L_ERR, "ERROR: get_ip_and_port_from_uri: "
577                   "cannot resolve host in uri: %.*s\n", uri->len, uri->s );
578                 free_uri(&parsed_uri);
579                 goto error;
580         }
581         memcpy(&ip, nhost->h_addr_list[0], sizeof(unsigned int));
582
583
584 success:
585         free_uri(&parsed_uri);
586         *param_ip = ip;
587         *param_port = port;
588         return 0;
589
590 error:
591         *param_ip = 0;
592         *param_port = 0;
593         return -1;
594 }
595
596
597
598
599 int t_add_fork( unsigned int ip, unsigned int port, char* uri_s,
600                         unsigned int uri_len, enum fork_type type, unsigned char free_flag)
601 {
602         unsigned int pos=0;
603         char         *foo=0;
604
605         switch (type)
606         {
607                 case DEFAULT:
608                         if (nr_forks+1>=MAX_FORK)
609                         {
610                                 LOG(L_ERR,"ERROR:t_add_fork: trying to add new fork ->"
611                                         " MAX_FORK exceded\n");
612                                 return -1;
613                         }
614                         pos = ++nr_forks;
615                         break;
616                 case NO_RESPONSE:
617                         if (t_forks[NO_RPL_BRANCH].ip)
618                                 LOG(L_WARN,"WARNING:t_add_fork: trying to add NO_RPL fork ->"
619                                         " it was set before -> overriding\n");
620                         if (uri_s && uri_len)
621                         {
622                                 foo = (char*)shm_malloc(uri_len);
623                                 if (!foo)
624                                 {
625                                         LOG(L_ERR,"ERROR:t_add_fork: cannot get free memory\n");
626                                         return -1;
627                                 }
628                                 memcpy(foo,uri_s,uri_len);
629                         }
630                         if (free_flag && uri_s)
631                                 pkg_free(uri_s);
632                         uri_s = foo;
633                         free_flag = 0;
634                         pos = NO_RPL_BRANCH;
635         }
636         t_forks[pos].ip = ip;
637         t_forks[pos].port = port;
638         if (uri_s && uri_len)
639         {
640                 t_forks[pos].free_flag = free_flag;
641                 t_forks[pos].uri.len = uri_len;
642                 t_forks[pos].uri.s = uri_s;
643         }
644
645         return 1;
646 }
647
648
649
650
651 int t_clear_forks( )
652 {
653         int i;
654
655         DBG("DEBUG: t_clear_forks: clearing tabel...\n");
656         for(i=1;i<nr_forks;i++)
657                 if (t_forks[i].free_flag && t_forks[i].uri.s)
658                         pkg_free(t_forks[i].uri.s);
659         memset( t_forks, 0, sizeof(t_forks));
660         nr_forks = 0;
661         return 1;
662 }
663
664
665
666
667
668 /*---------------------------TIMERS FUNCTIONS-------------------------------*/
669
670
671
672
673
674 inline void retransmission_handler( void *attr)
675 {
676         struct retr_buf* r_buf ;
677         enum lists id;
678
679         r_buf = (struct retr_buf*)attr;
680 #ifdef EXTRA_DEBUG
681         if (r_buf->my_T->damocles) {
682                 LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"
683                         " called from RETR timer\n",r_buf->my_T);
684                 abort();
685         }       
686 #endif
687
688         /*the transaction is already removed from RETRANSMISSION_LIST by timer*/
689         /* retransmision */
690         DBG("DEBUG: retransmission_handler : resending (t=%p)\n", r_buf->my_T);
691         switch ( r_buf->activ_type )
692         {
693                 case (TYPE_REQUEST):
694                         SEND_BUFFER( r_buf );
695                         break;
696                 case (TYPE_LOCAL_CANCEL):
697                         SEND_CANCEL_BUFFER( r_buf );
698                         break;
699                 default:
700                         T=r_buf->my_T;
701                         t_retransmit_reply();
702         }
703
704         id = r_buf->retr_list;
705         r_buf->retr_list = id < RT_T2 ? id + 1 : RT_T2;
706
707         set_timer(hash_table,&(r_buf->retr_timer),id < RT_T2 ? id + 1 : RT_T2 );
708
709         DBG("DEBUG: retransmission_handler : done\n");
710 }
711
712
713
714
715 inline void final_response_handler( void *attr)
716 {
717         struct retr_buf* r_buf = (struct retr_buf*)attr;
718
719 #ifdef EXTRA_DEBUG
720         if (r_buf->my_T->damocles) 
721         {
722                 LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"
723                         " called from FR timer\n",r_buf->my_T);
724                 abort();
725         }
726 #endif
727
728         /* the transaction is already removed from FR_LIST by the timer */
729         if (r_buf->activ_type==TYPE_LOCAL_CANCEL)
730         {
731                 DBG("DEBUG: FR_handler: stop retransmission for Local Cancel\n");
732                 reset_timer( hash_table , &(r_buf->retr_timer) );
733                 return;
734         }
735         /* send a 408 */
736         if ( r_buf->my_T->uac[r_buf->branch].status<200
737 #ifdef SILENT_FR
738         && (r_buf->my_T->nr_of_outgoings>1     /*if we have forked*/
739                 || r_buf->my_T->uas.request->first_line.u.request.method_value!=
740                         METHOD_INVITE                  /*if is not an INVITE */
741                 || r_buf->my_T->uac[r_buf->my_T->nr_of_outgoings].uri.s
742                                                    /*if "no on no response" was set*/
743                 || r_buf->my_T->uac[r_buf->branch].rpl_received==0
744                                                                                         /*if no reply was received*/
745         )
746 #endif
747         )
748         {
749                 DBG("DEBUG: FR_handler:stop retr. and send CANCEL (%p)\n",r_buf->my_T);
750                 reset_timer( hash_table, &(r_buf->retr_timer) );
751                 t_build_and_send_CANCEL( r_buf->my_T ,r_buf->branch);
752                 /* dirty hack:t_send_reply would increase ref_count which would indeed
753                 result in refcount++ which would not -- until timer processe's
754                 T changes again; currently only on next call to t_send_reply from
755                 FR timer; thus I fake the values now to avoid recalculating T
756                 and refcount++ JKU */
757                 T=r_buf->my_T;
758                 global_msg_id=T->uas.request->id;
759                 DBG("DEBUG: FR_handler: send 408 (%p)\n", r_buf->my_T);
760                 t_send_reply( r_buf->my_T->uas.request, 408, "Request Timeout",
761                         r_buf->branch);
762         }else{
763                 /* put it on WT_LIST - transaction is over */
764                 DBG("DEBUG: final_response_handler:-> put on wait"
765                         " (t=%p)\n", r_buf->my_T);
766                 t_put_on_wait(  r_buf->my_T );
767         }
768         DBG("DEBUG: final_response_handler : done\n");
769 }
770
771
772
773
774 inline void wait_handler( void *attr)
775 {
776         struct cell *p_cell = (struct cell*)attr;
777
778 #ifdef EXTRA_DEBUG
779         if (p_cell->damocles) {
780                 LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"
781                         " called from WAIT timer\n",p_cell);
782                 abort();
783         }       
784 #endif
785
786         /* the transaction is already removed from WT_LIST by the timer */
787         /* the cell is removed from the hash table */
788         DBG("DEBUG: wait_handler : removing %p from table \n", p_cell );
789         remove_from_hash_table( hash_table, p_cell );
790         /* jku: no more here -- we do it when we put a transaction on wait */
791         DBG("DEBUG: wait_handler : stopping all timers\n");
792         reset_retr_timers(hash_table,p_cell) ; 
793         /* put it on DEL_LIST - sch for del */
794 #ifdef EXTRA_DEBUG
795         p_cell->damocles = 1;
796 #endif
797         delete_cell( p_cell );
798         DBG("DEBUG: wait_handler : done\n");
799 }
800
801
802
803
804 inline void delete_handler( void *attr)
805 {
806         struct cell *p_cell = (struct cell*)attr;
807
808         DBG("DEBUG: delete_handler : removing %p \n", p_cell );
809 #ifdef EXTRA_DEBUG
810         if (p_cell->damocles==0) {
811                 LOG( L_ERR, "ERROR: transaction %p not scheduled for deletion"
812                         " and called from DELETE timer\n",p_cell);
813                 abort();
814         }       
815 #endif
816         delete_cell( p_cell );
817     DBG("DEBUG: delete_handler : done\n");
818 }
819
820
821
822
823 #define run_handler_for_each( _tl , _handler ) \
824         while ((_tl))\
825         {\
826                 /* reset the timer list linkage */\
827                 tmp_tl = (_tl)->next_tl;\
828                 (_tl)->next_tl = (_tl)->prev_tl = 0;\
829                 DBG("DEBUG: timer routine:%d,tl=%p next=%p\n",\
830                         id,(_tl),tmp_tl);\
831                 (_handler)( (_tl)->payload );\
832                 (_tl) = tmp_tl;\
833         }
834
835
836
837
838 void timer_routine(unsigned int ticks , void * attr)
839 {
840         struct s_table    *hash_table = (struct s_table *)attr;
841         struct timer_link *tl, *tmp_tl;
842         int                id;
843
844 #ifdef BOGDAN_TRIFLE
845         DBG(" %d \n",ticks);
846 #endif
847
848         for( id=0 ; id<NR_OF_TIMER_LISTS ; id++ )
849         {
850                 /* to waste as little time in lock as possible, detach list
851                    with expired items and process them after leaving the lock */
852                 tl=check_and_split_time_list( &(hash_table->timers[ id ]), ticks);
853                 /* process items now */
854                 switch (id)
855                 {
856                         case FR_TIMER_LIST:
857                         case FR_INV_TIMER_LIST:
858                                 run_handler_for_each(tl,final_response_handler);
859                                 break;
860                         case RT_T1_TO_1:
861                         case RT_T1_TO_2:
862                         case RT_T1_TO_3:
863                         case RT_T2:
864                                 run_handler_for_each(tl,retransmission_handler);
865                                 break;
866                         case WT_TIMER_LIST:
867                                 run_handler_for_each(tl,wait_handler);
868                                 break;
869                         case DELETE_LIST:
870                                 run_handler_for_each(tl,delete_handler);
871                                 break;
872                 }
873         }
874 }
875
876
877
878
879
880
881
882 /* Builds an ACK request based on an INVITE request. ACK is send
883  * to same address */
884 char *build_ack(struct sip_msg* rpl,struct cell *trans,int branch,int *ret_len)
885 {
886         struct sip_msg      *p_msg , *r_msg;
887         struct hdr_field    *hdr;
888         char                *ack_buf, *p, *via;
889         unsigned int         len, via_len;
890
891         ack_buf = 0;
892         via =0;
893         p_msg = trans->uas.request;
894         r_msg = rpl;
895
896         if ( parse_headers(rpl,HDR_TO)==-1 || !rpl->to )
897         {
898                 LOG(L_ERR, "ERROR: t_build_ACK: "
899                         "cannot generate a HBH ACK if key HFs in reply missing\n");
900                 goto error;
901         }
902
903         len = 0;
904         /*first line's len */
905         len += 4/*reply code and one space*/+
906                 p_msg->first_line.u.request.version.len+CRLF_LEN;
907         /*uri's len*/
908         if (trans->uac[branch].uri.s)
909                 len += trans->uac[branch].uri.len +1;
910         else
911                 len += p_msg->first_line.u.request.uri.len +1;
912         /*adding branch param*/
913         if ( add_branch_label( trans , trans->uas.request , branch)==-1 )
914                 goto error;
915         /*via*/
916         via = via_builder( p_msg , &via_len );
917         if (!via)
918         {
919                 LOG(L_ERR, "ERROR: t_build_ACK: "
920                         "no via header got from builder\n");
921                 goto error;
922         }
923         len+= via_len;
924         /*headers*/
925         for ( hdr=p_msg->headers ; hdr ; hdr=hdr->next )
926                 if (hdr->type==HDR_FROM||hdr->type==HDR_CALLID||hdr->type==HDR_CSEQ)
927                         len += ((hdr->body.s+hdr->body.len ) - hdr->name.s ) + CRLF_LEN ;
928                 else if ( hdr->type==HDR_TO )
929                         len += ((r_msg->to->body.s+r_msg->to->body.len ) -
930                                 r_msg->to->name.s ) + CRLF_LEN ;
931         /* CSEQ method : from INVITE-> ACK */
932         len -= 3  ;
933         /* end of message */
934         len += CRLF_LEN; /*new line*/
935
936         ack_buf = sh_malloc(len+1);
937         if (!ack_buf)
938         {
939                 LOG(L_ERR, "ERROR: t_build_and_ACK: cannot allocate memory\n");
940                 goto error1;
941         }
942         p = ack_buf;
943
944         /* first line */
945         memcpy( p , "ACK " , 4);
946         p += 4;
947         /* uri */
948         if ( trans->uac[branch].uri.s )
949         {
950                 memcpy(p,trans->uac[branch].uri.s,trans->uac[branch].uri.len);
951                 p +=trans->uac[branch].uri.len;
952         }else{
953                 memcpy(p,p_msg->orig+(p_msg->first_line.u.request.uri.s-p_msg->buf),
954                         p_msg->first_line.u.request.uri.len );
955                 p += p_msg->first_line.u.request.uri.len;
956         }
957         /* SIP version */
958         *(p++) = ' ';
959         memcpy(p,p_msg->orig+(p_msg->first_line.u.request.version.s-p_msg->buf),
960                 p_msg->first_line.u.request.version.len );
961         p += p_msg->first_line.u.request.version.len;
962         memcpy( p, CRLF, CRLF_LEN );
963         p+=CRLF_LEN;
964
965         /* insert our via */
966         memcpy( p , via , via_len );
967         p += via_len;
968
969         /*other headers*/
970         for ( hdr=p_msg->headers ; hdr ; hdr=hdr->next )
971         {
972                 if ( hdr->type==HDR_FROM || hdr->type==HDR_CALLID  )
973                 {
974                         memcpy( p , p_msg->orig+(hdr->name.s-p_msg->buf) ,
975                                 ((hdr->body.s+hdr->body.len ) - hdr->name.s ) );
976                         p += ((hdr->body.s+hdr->body.len ) - hdr->name.s );
977                         memcpy( p, CRLF, CRLF_LEN );
978                         p+=CRLF_LEN;
979                 }
980                 else if ( hdr->type==HDR_TO )
981                 {
982                         memcpy( p , r_msg->orig+(r_msg->to->name.s-r_msg->buf) ,
983                                 ((r_msg->to->body.s+r_msg->to->body.len)-r_msg->to->name.s));
984                         p+=((r_msg->to->body.s+r_msg->to->body.len)-r_msg->to->name.s);
985                         memcpy( p, CRLF, CRLF_LEN );
986                         p+=CRLF_LEN;
987                 }
988                 else if ( hdr->type==HDR_CSEQ )
989                 {
990                         memcpy( p , p_msg->orig+(hdr->name.s-p_msg->buf) ,
991                                 ((((struct cseq_body*)hdr->parsed)->method.s)-hdr->name.s));
992                         p+=((((struct cseq_body*)hdr->parsed)->method.s)-hdr->name.s);
993                         memcpy( p , "ACK" CRLF, 3+CRLF_LEN );
994                         p += 3+CRLF_LEN;
995                 }
996         }
997
998         /* end of message */
999         memcpy( p , CRLF , CRLF_LEN );
1000         p += CRLF_LEN;
1001
1002         pkg_free( via );
1003         DBG("DEBUG: t_build_ACK: ACK generated\n");
1004
1005         *(ret_len) = p-ack_buf;
1006         return ack_buf;
1007
1008 error1:
1009         pkg_free(via );
1010 error:
1011         return 0;
1012 }
1013
1014
1015