/* forwards a request to dst * parameters: * msg - sip msg * dst - destination name, if non-null it will be resolved and * send_info updated with the ip/port. Even if dst is non * null send_info must contain the protocol and if a non * default port or non srv. lookup is desired, the port must * be !=0 * port - used only if dst!=0 (else the port in send_info->to is used) * send_info - value/result partially filled dest_info structure: * - send_info->proto and comp are used * - send_info->to will be filled (dns) * - send_info->send_flags is filled from the message * - if the send_socket member is null, a send_socket will be * chosen automatically * WARNING: don't forget to zero-fill all the unused members (a non-zero * random id along with proto==PROTO_TCP can have bad consequences, same for * a bogus send_socket value) */ int forward_request(struct sip_msg* msg, str* dst, unsigned short port, struct dest_info* send_info) { unsigned int len; char* buf; char md5[MD5_LEN]; struct socket_info* orig_send_sock; /* initial send_sock */ int ret; struct ip_addr ip; /* debugging only */ char proto; #ifdef USE_DNS_FAILOVER struct socket_info* prev_send_sock; int err; struct dns_srv_handle dns_srv_h; prev_send_sock=0; err=0; #endif buf=0; orig_send_sock=send_info->send_sock; proto=send_info->proto; ret=0; if(dst){ #ifdef USE_DNS_FAILOVER if (cfg_get(core, core_cfg, use_dns_failover)){ dns_srv_handle_init(&dns_srv_h); err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port, &proto, dns_flags); if (err!=0){ LOG(L_ERR, "ERROR: forward_request: resolving \"%.*s\"" " failed: %s [%d]\n", dst->len, ZSW(dst->s), dns_strerror(err), err); ret=E_BAD_ADDRESS; goto error; } }else #endif if (sip_hostport2su(&send_info->to, dst, port, &proto)<0){ LOG(L_ERR, "ERROR: forward_request: bad host name %.*s," " dropping packet\n", dst->len, ZSW(dst->s)); ret=E_BAD_ADDRESS; goto error; } }/* dst */ send_info->send_flags=msg->fwd_send_flags; /* calculate branch for outbound request; if syn_branch is turned off, calculate is from transaction key, i.e., as an md5 of From/To/CallID/ CSeq exactly the same way as TM does; good for reboot -- than messages belonging to transaction lost due to reboot will still be forwarded with the same branch parameter and will be match-able downstream if it is turned on, we don't care about reboot; we simply put a simple value in there; better for performance */ if (syn_branch ) { memcpy(msg->add_to_branch_s, "z9hG4bKcydzigwkX", 16); msg->add_to_branch_len=16; } else { if (!char_msg_val( msg, md5 )) { /* parses transaction key */ LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n"); ret=E_UNSPEC; goto error; } msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number); if (!branch_builder( msg->hash_index, 0, md5, 0 /* 0-th branch */, msg->add_to_branch_s, &msg->add_to_branch_len )) { LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n"); ret=E_UNSPEC; goto error; } } /* try to send the message until success or all the ips are exhausted * (if dns lookup is performed && the dns cache used ) */ #ifdef USE_DNS_FAILOVER do{ #endif if (orig_send_sock==0) /* no forced send_sock => find it **/ send_info->send_sock=get_send_socket(msg, &send_info->to, proto); if (send_info->send_sock==0){ LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d, proto %d " "no corresponding listening socket\n", send_info->to.s.sa_family, proto); ret=ser_error=E_NO_SOCKET; #ifdef USE_DNS_FAILOVER /* continue, maybe we find a socket for some other ip */ continue; #else goto error; #endif } #ifdef USE_DNS_FAILOVER if (prev_send_sock!=send_info->send_sock){ /* rebuild the message only if the send_sock changed */ prev_send_sock=send_info->send_sock; #endif if (buf) pkg_free(buf); send_info->proto=proto; buf = build_req_buf_from_sip_req(msg, &len, send_info, 0); if (!buf){ LOG(L_ERR, "ERROR: forward_request: building failed\n"); ret=E_OUT_OF_MEM; /* most probable */ goto error; } #ifdef USE_DNS_FAILOVER } #endif /* send it! */ DBG("Sending:\n%.*s.\n", (int)len, buf); DBG("orig. len=%d, new_len=%d, proto=%d\n", msg->len, len, send_info->proto ); if (run_onsend(msg, send_info, buf, len)==0){ su2ip_addr(&ip, &send_info->to); LOG(L_INFO, "forward_request: request to %s:%d(%d) dropped" " (onsend_route)\n", ip_addr2a(&ip), su_getport(&send_info->to), send_info->proto); ser_error=E_OK; /* no error */ ret=E_ADM_PROHIBITED; #ifdef USE_DNS_FAILOVER continue; /* try another ip */ #else goto error; /* error ? */ #endif } #ifdef USE_DST_BLACKLIST if (cfg_get(core, core_cfg, use_dst_blacklist)){ if (dst_is_blacklisted(send_info, msg)){ su2ip_addr(&ip, &send_info->to); LOG(L_DBG, "DEBUG: blacklisted destination:%s:%d (%d)\n", ip_addr2a(&ip), su_getport(&send_info->to), send_info->proto); ret=ser_error=E_SEND; #ifdef USE_DNS_FAILOVER continue; /* try another ip */ #else goto error; #endif } } #endif if (msg_send(send_info, buf, len)<0){ ret=ser_error=E_SEND; #ifdef USE_DST_BLACKLIST (void)dst_blacklist_add(BLST_ERR_SEND, send_info, msg); #endif #ifdef USE_DNS_FAILOVER continue; /* try another ip */ #else goto error; #endif }else{ ret=ser_error=E_OK; /* sent requests stats */ STATS_TX_REQUEST( msg->first_line.u.request.method_value ); /* exit succcesfully */ goto end; } #ifdef USE_DNS_FAILOVER }while(dst && cfg_get(core, core_cfg, use_dns_failover) && dns_srv_handle_next(&dns_srv_h, err) && ((err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port, &proto, dns_flags))==0)); if ((err!=0) && (err!=-E_DNS_EOR)){ LOG(L_ERR, "ERROR: resolving %.*s host name in uri" " failed: %s [%d] (dropping packet)\n", dst->len, ZSW(dst->s), dns_strerror(err), err); ret=ser_error=E_BAD_ADDRESS; goto error; } #endif error: STATS_TX_DROPS; end: #ifdef USE_DNS_FAILOVER if (dst && cfg_get(core, core_cfg, use_dns_failover)){ dns_srv_handle_put(&dns_srv_h); } #endif if (buf) pkg_free(buf); /* received_buf & line_buf will be freed in receive_msg by free_lump_list*/ #if defined STATS_REQ_FWD_OK || defined STATS_REQ_FWD_DROP if(ret==0) STATS_REQ_FWD_OK(); else STATS_REQ_FWD_DROP(); #endif /* STATS_REQ_FWD_* */ return ret; }
/** removes first via & sends msg to the second * - mode param controls if modules sip response callbacks are executed */ static int do_forward_reply(struct sip_msg* msg, int mode) { char* new_buf; struct dest_info dst; unsigned int new_len; int r; struct ip_addr ip; #ifdef USE_TCP char* s; int len; #endif init_dest_info(&dst); new_buf=0; /*check if first via host = us */ if (check_via){ if (check_self(&msg->via1->host, msg->via1->port?msg->via1->port:SIP_PORT, msg->via1->proto)!=1){ LM_ERR("host in first via!=me : %.*s:%d\n", msg->via1->host.len, msg->via1->host.s, msg->via1->port); /* send error msg back? */ goto error; } } /* check modules response_f functions */ if(likely(mode==0)) { for (r=0; r<mod_response_cbk_no; r++) if (mod_response_cbks[r](msg)==0) goto skip; } /* we have to forward the reply stateless, so we need second via -bogdan*/ if (parse_headers( msg, HDR_VIA2_F, 0 )==-1 || (msg->via2==0) || (msg->via2->error!=PARSE_OK)) { /* no second via => error */ LM_DBG("reply cannot be forwarded - no 2nd via\n"); goto error; } new_buf = build_res_buf_from_sip_res( msg, &new_len); if (!new_buf){ LM_ERR("building failed\n"); goto error; } dst.proto=msg->via2->proto; SND_FLAGS_OR(&dst.send_flags, &msg->fwd_send_flags, &msg->rpl_send_flags); if (update_sock_struct_from_via( &dst.to, msg, msg->via2 )==-1) goto error; #ifdef USE_COMP dst.comp=msg->via2->comp_no; #endif #if defined USE_TCP || defined USE_SCTP if ( #ifdef USE_TCP dst.proto==PROTO_TCP || dst.proto==PROTO_WS #ifdef USE_TLS || dst.proto==PROTO_TLS || dst.proto==PROTO_WSS #endif #ifdef USE_SCTP || #endif /* USE_SCTP */ #endif /* USE_TCP */ #ifdef USE_SCTP dst.proto==PROTO_SCTP #endif /* USE_SCTP */ ){ /* find id in i param if it exists */ if (msg->via1->i && msg->via1->i->value.s){ s=msg->via1->i->value.s; len=msg->via1->i->value.len; LM_DBG("i=%.*s\n",len, ZSW(s)); if (reverse_hex2int(s, len, (unsigned int*)&dst.id)<0){ LM_ERR("bad via i param \"%.*s\"\n", len, ZSW(s)); dst.id=0; } } } #endif apply_force_send_socket(&dst, msg); /* call onsend_route */ if(dst.send_sock == NULL) { dst.send_sock=get_send_socket(msg, &dst.to, dst.proto); if (dst.send_sock==0){ LM_ERR("cannot forward reply\n"); goto done; } } if (onsend_route_enabled(SIP_REPLY)){ if (run_onsend(msg, &dst, new_buf, new_len)==0){ su2ip_addr(&ip, &(dst.to)); LM_ERR("reply to %s:%d(%d) dropped (onsend_route)\n", ip_addr2a(&ip), su_getport(&(dst.to)), dst.proto); goto error; /* error ? */ } } if (msg_send(&dst, new_buf, new_len)<0) { STATS_RPL_FWD_DROP(); goto error; } done: #ifdef STATS STATS_TX_RESPONSE( (msg->first_line.u.reply.statuscode/100) ); #endif LM_DBG("reply forwarded to %.*s:%d\n", msg->via2->host.len, msg->via2->host.s, (unsigned short) msg->via2->port); STATS_RPL_FWD_OK(); pkg_free(new_buf); skip: return 0; error: if (new_buf) pkg_free(new_buf); return -1; }