882a592947744c4ecc7c046af6a86d63f9544859
[sip-router] / action.c
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2001-2003 Fhg Fokus
5  *
6  * This file is part of ser, a free SIP server.
7  *
8  * ser is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * For a license to use the ser software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * ser is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License 
24  * along with this program; if not, write to the Free Software 
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  *
27  * History:
28  * ---------
29  *  2003-02-28  scratchpad compatibility abandoned (jiri)
30  *  2003-01-29  removed scratchpad (jiri)
31  *  2003-03-19  fixed set* len calculation bug & simplified a little the code
32  *              (should be a little faster now) (andrei)
33  *              replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
34  *  2003-04-01  Added support for loose routing in forward (janakj)
35  *  2003-04-12  FORCE_RPORT_T added (andrei)
36  *  2003-04-22  strip_tail added (jiri)
37  *  2003-10-02  added SET_ADV_ADDR_T & SET_ADV_PORT_T (andrei)
38  *  2003-10-29  added FORCE_TCP_ALIAS_T (andrei)
39  */
40
41
42 #include "comp_defs.h"
43
44 #include "action.h"
45 #include "config.h"
46 #include "error.h"
47 #include "dprint.h"
48 #include "proxy.h"
49 #include "forward.h"
50 #include "udp_server.h"
51 #include "route.h"
52 #include "parser/msg_parser.h"
53 #include "parser/parse_uri.h"
54 #include "ut.h"
55 #include "sr_module.h"
56 #include "mem/mem.h"
57 #include "globals.h"
58 #include "dset.h"
59 #ifdef USE_TCP
60 #include "tcp_server.h"
61 #endif
62
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #include <netdb.h>
66 #include <stdlib.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <string.h>
70
71 #ifdef DEBUG_DMALLOC
72 #include <dmalloc.h>
73 #endif
74
75
76 /* ret= 0! if action -> end of list(e.g DROP), 
77       > 0 to continue processing next actions
78    and <0 on error */
79 int do_action(struct action* a, struct sip_msg* msg)
80 {
81         int ret;
82         int v;
83         union sockaddr_union* to;
84         struct socket_info* send_sock;
85         struct proxy_l* p;
86         char* tmp;
87         char *new_uri, *end, *crt;
88         int len;
89         int user;
90         struct sip_uri uri, next_hop;
91         struct sip_uri *u;
92         unsigned short port;
93         int proto;
94
95         /* reset the value of error to E_UNSPEC so avoid unknowledgable
96            functions to return with errror (status<0) and not setting it
97            leaving there previous error; cache the previous value though
98            for functions which want to process it */
99         prev_ser_error=ser_error;
100         ser_error=E_UNSPEC;
101
102         ret=E_BUG;
103         switch ((unsigned char)a->type){
104                 case DROP_T:
105                                 ret=0;
106                         break;
107                 case FORWARD_T:
108 #ifdef USE_TCP
109                 case FORWARD_TCP_T:
110 #endif
111 #ifdef USE_TLS
112                 case FORWARD_TLS_T:
113 #endif
114                 case FORWARD_UDP_T:
115
116                         if (a->type==FORWARD_UDP_T) proto=PROTO_UDP;
117 #ifdef USE_TCP
118                         else if (a->type==FORWARD_TCP_T) proto= PROTO_TCP;
119 #endif
120 #ifdef USE_TLS
121                         else if (a->type==FORWARD_TLS_T) proto= PROTO_TLS;
122 #endif
123                         else proto=msg->rcv.proto;
124                         if (a->p1_type==URIHOST_ST){
125                                 /*parse uri*/
126
127                                 if (msg->dst_uri.len) {
128                                         ret = parse_uri(msg->dst_uri.s, msg->dst_uri.len, &next_hop);
129                                         u = &next_hop;
130                                 } else {
131                                         ret = parse_sip_msg_uri(msg);
132                                         u = &msg->parsed_uri;
133                                 }
134
135                                 if (ret<0) {
136                                         LOG(L_ERR, "ERROR: do_action: forward: bad_uri "
137                                                                 " dropping packet\n");
138                                         break;
139                                 }
140                                 
141                                 switch (a->p2_type){
142                                         case URIPORT_ST:
143                                                                         port=u->port_no;
144                                                                         break;
145                                         case NUMBER_ST:
146                                                                         port=a->p2.number;
147                                                                         break;
148                                         default:
149                                                         LOG(L_CRIT, "BUG: do_action bad forward 2nd"
150                                                                                 " param type (%d)\n", a->p2_type);
151                                                         ret=E_UNSPEC;
152                                                         goto error_fwd_uri;
153                                 }
154                                 switch(u->proto){
155                                         case PROTO_NONE:
156                                                 proto=PROTO_UDP;
157                                                 break;
158                                         case PROTO_UDP:
159 #ifdef USE_TCP
160                                         case PROTO_TCP:
161 #endif
162 #ifdef USE_TLS
163                                         case PROTO_TLS:
164 #endif
165                                                 proto=u->proto;
166                                                 break;
167                                         default:
168                                                 LOG(L_ERR,"ERROR: do action: forward: bad uri"
169                                                                 " transport %d\n", u->proto);
170                                                 ret=E_BAD_PROTO;
171                                                 goto error_fwd_uri;
172                                 }
173 #ifdef USE_TLS
174                                 if (u->secure){
175                                         if (u->proto==PROTO_UDP){
176                                                 LOG(L_ERR, "ERROR: do_action: forward: secure uri"
177                                                                 " incompatible with transport %d\n", u->proto);
178                                                 ret=E_BAD_PROTO;
179                                                 goto error_fwd_uri;
180                                         }
181                                         proto=PROTO_TLS;
182                                 }
183 #endif
184                                 /* create a temporary proxy*/
185                                 p=mk_proxy(&u->host, port, proto);
186                                 if (p==0){
187                                         LOG(L_ERR, "ERROR:  bad host name in uri,"
188                                                         " dropping packet\n");
189                                         ret=E_BAD_ADDRESS;
190                                         goto error_fwd_uri;
191                                 }
192                                 ret=forward_request(msg, p, proto);
193                                 /*free_uri(&uri); -- no longer needed, in sip_msg*/
194                                 free_proxy(p); /* frees only p content, not p itself */
195                                 pkg_free(p);
196                                 if (ret>=0) ret=1;
197                         }else if ((a->p1_type==PROXY_ST) && (a->p2_type==NUMBER_ST)){
198                                 ret=forward_request(msg,(struct proxy_l*)a->p1.data, proto);
199                                 if (ret>=0) ret=1;
200                         }else{
201                                 LOG(L_CRIT, "BUG: do_action: bad forward() types %d, %d\n",
202                                                 a->p1_type, a->p2_type);
203                                 ret=E_BUG;
204                         }
205                         break;
206                 case SEND_T:
207                 case SEND_TCP_T:
208                         if ((a->p1_type!= PROXY_ST)|(a->p2_type!=NUMBER_ST)){
209                                 LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n",
210                                                 a->p1_type, a->p2_type);
211                                 ret=E_BUG;
212                                 break;
213                         }
214                         to=(union sockaddr_union*)
215                                         pkg_malloc(sizeof(union sockaddr_union));
216                         if (to==0){
217                                 LOG(L_ERR, "ERROR: do_action: "
218                                                         "memory allocation failure\n");
219                                 ret=E_OUT_OF_MEM;
220                                 break;
221                         }
222                         
223                         p=(struct proxy_l*)a->p1.data;
224                         
225                         if (p->ok==0){
226                                 if (p->host.h_addr_list[p->addr_idx+1])
227                                         p->addr_idx++;
228                                 else 
229                                         p->addr_idx=0;
230                                 p->ok=1;
231                         }
232                         ret=hostent2su( to, &p->host, p->addr_idx,
233                                                 (p->port)?p->port:SIP_PORT );
234                         if (ret==0){
235                                 p->tx++;
236                                 p->tx_bytes+=msg->len;
237                                 if (a->type==SEND_T){
238                                         /*udp*/
239                                         send_sock=get_send_socket(to, PROTO_UDP);
240                                         if (send_sock!=0){
241                                                 ret=udp_send(send_sock, msg->buf, msg->len, to);
242                                         }else{
243                                                 ret=-1;
244                                         }
245                                 }
246 #ifdef USE_TCP
247                                         else{
248                                         /*tcp*/
249                                         ret=tcp_send(PROTO_TCP, msg->buf, msg->len, to, 0);
250                                 }
251 #endif
252                         }
253                         pkg_free(to);
254                         if (ret<0){
255                                 p->errors++;
256                                 p->ok=0;
257                         }else ret=1;
258                         
259                         break;
260                 case LOG_T:
261                         if ((a->p1_type!=NUMBER_ST)|(a->p2_type!=STRING_ST)){
262                                 LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n",
263                                                 a->p1_type, a->p2_type);
264                                 ret=E_BUG;
265                                 break;
266                         }
267                         LOG(a->p1.number, a->p2.string);
268                         ret=1;
269                         break;
270
271                 /* jku -- introduce a new branch */
272                 case APPEND_BRANCH_T:
273                         if ((a->p1_type!=STRING_ST)) {
274                                 LOG(L_CRIT, "BUG: do_action: bad append_branch_t %d\n",
275                                         a->p1_type );
276                                 ret=E_BUG;
277                                 break;
278                         }
279                         ret=append_branch( msg, a->p1.string, 
280                                            a->p1.string ? strlen(a->p1.string):0,
281                                            a->p2.number);
282                         break;
283
284                 /* jku begin: is_length_greater_than */
285                 case LEN_GT_T:
286                         if (a->p1_type!=NUMBER_ST) {
287                                 LOG(L_CRIT, "BUG: do_action: bad len_gt type %d\n",
288                                         a->p1_type );
289                                 ret=E_BUG;
290                                 break;
291                         }
292                         /* DBG("XXX: message length %d, max %d\n", 
293                                 msg->len, a->p1.number ); */
294                         ret = msg->len >= a->p1.number ? 1 : -1;
295                         break;
296                 /* jku end: is_length_greater_than */
297                         
298                 /* jku - begin : flag processing */
299
300                 case SETFLAG_T:
301                         if (a->p1_type!=NUMBER_ST) {
302                                 LOG(L_CRIT, "BUG: do_action: bad setflag() type %d\n",
303                                         a->p1_type );
304                                 ret=E_BUG;
305                                 break;
306                         }
307                         if (!flag_in_range( a->p1.number )) {
308                                 ret=E_CFG;
309                                 break;
310                         }
311                         setflag( msg, a->p1.number );
312                         ret=1;
313                         break;
314
315                 case RESETFLAG_T:
316                         if (a->p1_type!=NUMBER_ST) {
317                                 LOG(L_CRIT, "BUG: do_action: bad resetflag() type %d\n",
318                                         a->p1_type );
319                                 ret=E_BUG;
320                                 break;
321                         }
322                         if (!flag_in_range( a->p1.number )) {
323                                 ret=E_CFG;
324                                 break;
325                         }
326                         resetflag( msg, a->p1.number );
327                         ret=1;
328                         break;
329                         
330                 case ISFLAGSET_T:
331                         if (a->p1_type!=NUMBER_ST) {
332                                 LOG(L_CRIT, "BUG: do_action: bad isflagset() type %d\n",
333                                         a->p1_type );
334                                 ret=E_BUG;
335                                 break;
336                         }
337                         if (!flag_in_range( a->p1.number )) {
338                                 ret=E_CFG;
339                                 break;
340                         }
341                         ret=isflagset( msg, a->p1.number );
342                         break;
343                 /* jku - end : flag processing */
344
345                 case ERROR_T:
346                         if ((a->p1_type!=STRING_ST)|(a->p2_type!=STRING_ST)){
347                                 LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n",
348                                                 a->p1_type, a->p2_type);
349                                 ret=E_BUG;
350                                 break;
351                         }
352                         LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
353                                         "not implemented yet\n", a->p1.string, a->p2.string);
354                         ret=1;
355                         break;
356                 case ROUTE_T:
357                         if (a->p1_type!=NUMBER_ST){
358                                 LOG(L_CRIT, "BUG: do_action: bad route() type %d\n",
359                                                 a->p1_type);
360                                 ret=E_BUG;
361                                 break;
362                         }
363                         if ((a->p1.number>RT_NO)||(a->p1.number<0)){
364                                 LOG(L_ERR, "ERROR: invalid routing table number in"
365                                                         "route(%lu)\n", a->p1.number);
366                                 ret=E_CFG;
367                                 break;
368                         }
369                         ret=((ret=run_actions(rlist[a->p1.number], msg))<0)?ret:1;
370                         break;
371                 case EXEC_T:
372                         if (a->p1_type!=STRING_ST){
373                                 LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n",
374                                                 a->p1_type);
375                                 ret=E_BUG;
376                                 break;
377                         }
378                         LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,"
379                                                 " using dumb version...\n", a->p1.string);
380                         ret=system(a->p1.string);
381                         if (ret!=0){
382                                 LOG(L_NOTICE, "WARNING: exec() returned %d\n", ret);
383                         }
384                         ret=1;
385                         break;
386                 case REVERT_URI_T:
387                         if (msg->new_uri.s) {
388                                 pkg_free(msg->new_uri.s);
389                                 msg->new_uri.len=0;
390                                 msg->new_uri.s=0;
391                                 msg->parsed_uri_ok=0; /* invalidate current parsed uri*/
392                         };
393                         ret=1;
394                         break;
395                 case SET_HOST_T:
396                 case SET_HOSTPORT_T:
397                 case SET_USER_T:
398                 case SET_USERPASS_T:
399                 case SET_PORT_T:
400                 case SET_URI_T:
401                 case PREFIX_T:
402                 case STRIP_T:
403                 case STRIP_TAIL_T:
404                                 user=0;
405                                 if (a->type==STRIP_T || a->type==STRIP_TAIL_T) {
406                                         if (a->p1_type!=NUMBER_ST) {
407                                                 LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
408                                                         a->p1_type);
409                                                 break;
410                                         }
411                                 } else if (a->p1_type!=STRING_ST){
412                                         LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
413                                                         a->p1_type);
414                                         ret=E_BUG;
415                                         break;
416                                 }
417                                 if (a->type==SET_URI_T){
418                                         if (msg->new_uri.s) {
419                                                         pkg_free(msg->new_uri.s);
420                                                         msg->new_uri.len=0;
421                                         }
422                                         msg->parsed_uri_ok=0;
423                                         len=strlen(a->p1.string);
424                                         msg->new_uri.s=pkg_malloc(len+1);
425                                         if (msg->new_uri.s==0){
426                                                 LOG(L_ERR, "ERROR: do_action: memory allocation"
427                                                                 " failure\n");
428                                                 ret=E_OUT_OF_MEM;
429                                                 break;
430                                         }
431                                         memcpy(msg->new_uri.s, a->p1.string, len);
432                                         msg->new_uri.s[len]=0;
433                                         msg->new_uri.len=len;
434                                         
435                                         ret=1;
436                                         break;
437                                 }
438                                 if (msg->new_uri.s) {
439                                         tmp=msg->new_uri.s;
440                                         len=msg->new_uri.len;
441                                 }else{
442                                         tmp=msg->first_line.u.request.uri.s;
443                                         len=msg->first_line.u.request.uri.len;
444                                 }
445                                 if (parse_uri(tmp, len, &uri)<0){
446                                         LOG(L_ERR, "ERROR: do_action: bad uri <%s>, dropping"
447                                                                 " packet\n", tmp);
448                                         ret=E_UNSPEC;
449                                         break;
450                                 }
451                                 
452                                 new_uri=pkg_malloc(MAX_URI_SIZE);
453                                 if (new_uri==0){
454                                         LOG(L_ERR, "ERROR: do_action: memory allocation "
455                                                                 " failure\n");
456                                         ret=E_OUT_OF_MEM;
457                                         break;
458                                 }
459                                 end=new_uri+MAX_URI_SIZE;
460                                 crt=new_uri;
461                                 /* begin copying */
462                                 len=strlen("sip:"); if(crt+len>end) goto error_uri;
463                                 memcpy(crt,"sip:",len);crt+=len;
464
465                                 /* user */
466
467                                 /* prefix (-jiri) */
468                                 if (a->type==PREFIX_T) {
469                                         tmp=a->p1.string;
470                                         len=strlen(tmp); if(crt+len>end) goto error_uri;
471                                         memcpy(crt,tmp,len);crt+=len;
472                                         /* whatever we had before, with prefix we have username 
473                                            now */
474                                         user=1;
475                                 }
476
477                                 if ((a->type==SET_USER_T)||(a->type==SET_USERPASS_T)) {
478                                         tmp=a->p1.string;
479                                         len=strlen(tmp);
480                                 } else if (a->type==STRIP_T) {
481                                         if (a->p1.number>uri.user.len) {
482                                                 LOG(L_WARN, "Error: too long strip asked; "
483                                                                         " deleting username: %lu of <%.*s>\n",
484                                                                         a->p1.number, uri.user.len, uri.user.s );
485                                                 len=0;
486                                         } else if (a->p1.number==uri.user.len) {
487                                                 len=0;
488                                         } else {
489                                                 tmp=uri.user.s + a->p1.number;
490                                                 len=uri.user.len - a->p1.number;
491                                         }
492                                 } else if (a->type==STRIP_TAIL_T) {
493                                         if (a->p1.number>uri.user.len) {
494                                                 LOG(L_WARN, "WARNING: too long strip_tail asked; "
495                                                                         " deleting username: %lu of <%.*s>\n",
496                                                                         a->p1.number, uri.user.len, uri.user.s );
497                                                 len=0;
498                                         } else if (a->p1.number==uri.user.len) {
499                                                 len=0;
500                                         } else {
501                                                 tmp=uri.user.s;
502                                                 len=uri.user.len - a->p1.number;
503                                         }
504                                 } else {
505                                         tmp=uri.user.s;
506                                         len=uri.user.len;
507                                 }
508
509                                 if (len){
510                                         if(crt+len>end) goto error_uri;
511                                         memcpy(crt,tmp,len);crt+=len;
512                                         user=1; /* we have an user field so mark it */
513                                 }
514
515                                 if (a->type==SET_USERPASS_T) tmp=0;
516                                 else tmp=uri.passwd.s;
517                                 /* passwd */
518                                 if (tmp){
519                                         len=uri.passwd.len; if(crt+len+1>end) goto error_uri;
520                                         *crt=':'; crt++;
521                                         memcpy(crt,tmp,len);crt+=len;
522                                 }
523                                 /* host */
524                                 if (user || tmp){ /* add @ */
525                                         if(crt+1>end) goto error_uri;
526                                         *crt='@'; crt++;
527                                 }
528                                 if ((a->type==SET_HOST_T) ||(a->type==SET_HOSTPORT_T)) {
529                                         tmp=a->p1.string;
530                                         if (tmp) len = strlen(tmp);
531                                         else len=0;
532                                 } else {
533                                         tmp=uri.host.s;
534                                         len = uri.host.len;
535                                 }
536                                 if (tmp){
537                                         if(crt+len>end) goto error_uri;
538                                         memcpy(crt,tmp,len);crt+=len;
539                                 }
540                                 /* port */
541                                 if (a->type==SET_HOSTPORT_T) tmp=0;
542                                 else if (a->type==SET_PORT_T) {
543                                         tmp=a->p1.string;
544                                         if (tmp) len = strlen(tmp);
545                                         else len = 0;
546                                 } else {
547                                         tmp=uri.port.s;
548                                         len = uri.port.len;
549                                 }
550                                 if (tmp){
551                                         if(crt+len+1>end) goto error_uri;
552                                         *crt=':'; crt++;
553                                         memcpy(crt,tmp,len);crt+=len;
554                                 }
555                                 /* params */
556                                 tmp=uri.params.s;
557                                 if (tmp){
558                                         len=uri.params.len; if(crt+len+1>end) goto error_uri;
559                                         *crt=';'; crt++;
560                                         memcpy(crt,tmp,len);crt+=len;
561                                 }
562                                 /* headers */
563                                 tmp=uri.headers.s;
564                                 if (tmp){
565                                         len=uri.headers.len; if(crt+len+1>end) goto error_uri;
566                                         *crt='?'; crt++;
567                                         memcpy(crt,tmp,len);crt+=len;
568                                 }
569                                 *crt=0; /* null terminate the thing */
570                                 /* copy it to the msg */
571                                 if (msg->new_uri.s) pkg_free(msg->new_uri.s);
572                                 msg->new_uri.s=new_uri;
573                                 msg->new_uri.len=crt-new_uri;
574                                 msg->parsed_uri_ok=0;
575                                 ret=1;
576                                 break;
577                 case IF_T:
578                                 /* if null expr => ignore if? */
579                                 if ((a->p1_type==EXPR_ST)&&a->p1.data){
580                                         v=eval_expr((struct expr*)a->p1.data, msg);
581                                         if (v<0){
582                                                 if (v==EXPR_DROP){ /* hack to quit on DROP*/
583                                                         ret=0;
584                                                         break;
585                                                 }else{
586                                                         LOG(L_WARN,"WARNING: do_action:"
587                                                                                 "error in expression\n");
588                                                 }
589                                         }
590                                         
591                                         ret=1;  /*default is continue */
592                                         if (v>0) {
593                                                 if ((a->p2_type==ACTIONS_ST)&&a->p2.data){
594                                                         ret=run_actions((struct action*)a->p2.data, msg);
595                                                 }
596                                         }else if ((a->p3_type==ACTIONS_ST)&&a->p3.data){
597                                                         ret=run_actions((struct action*)a->p3.data, msg);
598                                         }
599                                 }
600                         break;
601                 case MODULE_T:
602                         if ( ((a->p1_type==CMDF_ST)&&a->p1.data)/*&&
603                                         ((a->p2_type==STRING_ST)&&a->p2.data)*/ ){
604                                 ret=((cmd_function)(a->p1.data))(msg, (char*)a->p2.data,
605                                                                                                           (char*)a->p3.data);
606                         }else{
607                                 LOG(L_CRIT,"BUG: do_action: bad module call\n");
608                         }
609                         break;
610                 case FORCE_RPORT_T:
611                         msg->msg_flags|=FL_FORCE_RPORT;
612                         ret=1; /* continue processing */
613                         break;
614                 case SET_ADV_ADDR_T:
615                         if (a->p1_type!=STR_ST){
616                                 LOG(L_CRIT, "BUG: do_action: bad set_advertised_address() "
617                                                 "type %d\n", a->p1_type);
618                                 ret=E_BUG;
619                                 break;
620                         }
621                         msg->set_global_address=*((str*)a->p1.data);
622                         ret=1; /* continue processing */
623                         break;
624                 case SET_ADV_PORT_T:
625                         if (a->p1_type!=STR_ST){
626                                 LOG(L_CRIT, "BUG: do_action: bad set_advertised_port() "
627                                                 "type %d\n", a->p1_type);
628                                 ret=E_BUG;
629                                 break;
630                         }
631                         msg->set_global_port=*((str*)a->p1.data);
632                         ret=1; /* continue processing */
633                         break;
634 #ifdef USE_TCP
635                 case FORCE_TCP_ALIAS_T:
636                         if ( msg->rcv.proto==PROTO_TCP
637 #ifdef USE_TLS
638                                         || msg->rcv.proto==PROTO_TLS
639 #endif
640                            ){
641                                 
642                                 if (a->p1_type==NOSUBTYPE)      port=msg->via1->port;
643                                 else if (a->p1_type==NUMBER_ST) port=(int)a->p1.number;
644                                 else{
645                                         LOG(L_CRIT, "BUG: do_action: bad force_tcp_alias"
646                                                         " port type %d\n", a->p1_type);
647                                         ret=E_BUG;
648                                         break;
649                                 }
650                                                 
651                                 if (tcpconn_add_alias(msg->rcv.proto_reserved1, port,
652                                                                         msg->rcv.proto)!=0){
653                                         LOG(L_ERR, " ERROR: receive_msg: tcp alias failed\n");
654                                         ret=E_UNSPEC;
655                                         break;
656                                 }
657                         }
658 #endif
659                         ret=1; /* continue processing */
660                         break;
661                 default:
662                         LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
663         }
664 /*skip:*/
665         return ret;
666         
667 error_uri:
668         LOG(L_ERR, "ERROR: do_action: set*: uri too long\n");
669         if (new_uri) pkg_free(new_uri);
670         return E_UNSPEC;
671 error_fwd_uri:
672         /*free_uri(&uri); -- not needed anymore, using msg->parsed_uri*/
673         return ret;
674 }
675
676
677
678 /* returns: 0, or 1 on success, <0 on error */
679 /* (0 if drop or break encountered, 1 if not ) */
680 int run_actions(struct action* a, struct sip_msg* msg)
681 {
682         struct action* t;
683         int ret=E_UNSPEC;
684         static int rec_lev=0;
685         struct sr_module *mod;
686
687         rec_lev++;
688         if (rec_lev>ROUTE_MAX_REC_LEV){
689                 LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)"
690                                         " giving up!\n", rec_lev);
691                 ret=E_UNSPEC;
692                 goto error;
693         }
694                 
695         if (a==0){
696                 LOG(L_ERR, "WARNING: run_actions: null action list (rec_level=%d)\n", 
697                         rec_lev);
698                 ret=0;
699         }
700
701         for (t=a; t!=0; t=t->next){
702                 ret=do_action(t, msg);
703                 if(ret==0) break;
704                 /* ignore errors */
705                 /*else if (ret<0){ ret=-1; goto error; }*/
706         }
707         
708         rec_lev--;
709         /* process module onbreak handlers if present */
710         if (rec_lev==0 && ret==0) 
711                 for (mod=modules;mod;mod=mod->next) 
712                         if (mod->exports && mod->exports->onbreak_f) {
713                                 mod->exports->onbreak_f( msg );
714                                 DBG("DEBUG: %s onbreak handler called\n", mod->exports->name);
715                         }
716         return ret;
717         
718
719 error:
720         rec_lev--;
721         return ret;
722 }
723
724
725