On Mar 18, 2009 at 16:57, Henning Westerholt <henning.westerholt(a)1und1.de> wrote:
Hi,
attached patch converts the utils module to use the sr msg_send functionality.
Please review if the API is used correct.
Look ok.
In kamailio we use this "while( get_next_su( proxy, to, 0) == 0 );" construct
to loop over all destinations (e.g. for failover), how this is handled in sr?
int err;
struct dns_srv_handle dns_srv_h;
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){ /* ERROR */ }
do{
/* ... get_send_socket a.s.o */
if (msg_send(send_info, buf, len)<0) continue;
}while( dns_srv_handle_next(&dns_srv_h, err) &&
((err=dns_sip_resolve2su(&dns_srv_h, &send_info->to, dst, port,
&proto, dns_flags)) == 0) );
dns_srv_handle_put(&dns_srv_h); /* very important, don't forget or the
the corresp. dns cache entry won't ever
be freed */
(lots of stuff omitted, like error checks, blacklist check/add,
dns failover on/off checks, #ifdefs for USE_DNS_FAILOVER a.s.o.)
Basically dns_srv_handle_next() selects the next ip and also tells if there are
no more ips availale. It needs a struct dns_srv_handle which was used before in
a call to dns_sip_resolve*() and the return value (err) of the last
dns_sip_resolve*() call. You could use 0 instead of err, if you check
dns_sip_resolve*() return in some other place and stop the loop on error.
For a more complete example, see forward_request() in forward.c.
You might be able to use forward_request() directly and get rid of all the send
and failover code (I haven't checked if it's possible or utils_forward() needs
something more special). forward_request() will also use the blacklist (if
configured).
Andrei
diff --git a/modules/utils/utils.c
b/modules/utils/utils.c
index 0042f87..cc5dd3b 100644
--- a/modules/utils/utils.c
+++ b/modules/utils/utils.c
@@ -275,14 +275,10 @@ static int fixup_free_http_query(void** param, int param_no)
int utils_forward(struct sip_msg *msg, int id, int proto)
{
int ret = -1;
- union sockaddr_union* to = NULL;
- struct socket_info *send_sock = NULL;
+ struct dest_info dst;
- to = (union sockaddr_union*)pkg_malloc(sizeof(union sockaddr_union));
- if (to == NULL) {
- PKG_MEM_ERROR;
- return -1;
- }
+ init_dest_info(&dst);
+ dst.proto = proto;
// critical section start:
// avoids dirty reads when updating configuration.
@@ -291,29 +287,23 @@ int utils_forward(struct sip_msg *msg, int id, int proto)
struct proxy_l *proxy = conf_needs_forward(msg, id);
if (proxy != NULL) {
- hostent2su(to, &proxy->host, proxy->addr_idx,
(proxy->port)?proxy->port:SIP_PORT);
- do {
- send_sock=get_send_socket(msg, to, proto);
-
- if (send_sock==0) {
- LM_ERR("cannot forward to af %d, "
- "proto %d no corresponding listening socket\n", to->s.sa_family,
proto);
- continue;
- }
- LM_DBG("Sending:\n%.*s.\n", (int)msg->len,msg->buf);
-
- if (msg_send(send_sock, proto, to, 0, msg->buf,msg->len)<0){
- LM_ERR("Error sending message!\n");
- continue;
- }
- ret = 0;
- break;
- } while( get_next_su( proxy, to, 0) == 0 );
+ proxy2su(&dst.to, proxy);
+
+ dst.send_sock = get_send_socket(msg, &dst.to, dst.proto);
+ if (dst.send_sock==0) {
+ LM_ERR("cannot forward to af %d, proto %d no corresponding"
+ "listening socket\n", dst.to.s.sa_family, proto);
+ }
+ LM_DBG("Sending:\n%.*s.\n", (int)msg->len,msg->buf);
+
+ if (msg_send(&dst, msg->buf, msg->len)<0){
+ LM_ERR("Error sending message!\n");
+ }
+ ret = 0;
}
// critical section end
lock_release(conf_lock);
- pkg_free(to);
return ret;
}
_______________________________________________
sr-dev mailing list
sr-dev(a)lists.sip-router.org
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev