Browse code

- @next_hop.src_ip: source ip of outgoing message

Tomas Mandys authored on 02/06/2009 20:46:05
Showing 2 changed files
... ...
@@ -61,6 +61,8 @@
61 61
 #include "parser/parse_body.h"
62 62
 #include "dset.h"
63 63
 #include "sr_module.h"
64
+#include "resolve.h"
65
+#include "forward.h"
64 66
 
65 67
 #define RETURN0_res(x) {*res=(x);return 0;}
66 68
 #define TRIM_RET0_res(x) {*res=(x);trim(res);return 0;} 
... ...
@@ -103,6 +105,55 @@ int select_next_hop(str* res, select_t* s, struct sip_msg* msg)
103 103
 	return -1;
104 104
 }
105 105
 
106
+int select_next_hop_src_ip(str* res, select_t* s, struct sip_msg* msg) {
107
+	struct socket_info* socket_info;
108
+	union sockaddr_union to;
109
+	char proto;
110
+	struct sip_uri *u, next_hop;
111
+	str *dst_host;
112
+
113
+	if (msg->first_line.type!=SIP_REQUEST) 
114
+		return -1;
115
+
116
+	if (msg->force_send_socket) {
117
+		*res = msg->force_send_socket->address_str;
118
+		return 0;
119
+	}
120
+
121
+	if (msg->dst_uri.len) {
122
+		if (parse_uri(msg->dst_uri.s, msg->dst_uri.len, &next_hop) < 0)
123
+			return -1;
124
+		u = &next_hop;
125
+	}
126
+	else {
127
+		if (parse_sip_msg_uri(msg) < 0)
128
+			return -1;
129
+		u = &msg->parsed_uri;
130
+	}
131
+#ifdef USE_TLS
132
+	if (u->type==SIPS_URI_T)
133
+		proto = PROTO_TLS;
134
+	else
135
+#endif
136
+		proto = u->proto;
137
+
138
+#ifdef HONOR_MADDR
139
+	if (u->maddr_val.s && u->maddr_val.len)
140
+		dst_host = &u->maddr_val;
141
+	else
142
+#endif
143
+		dst_host = &u->host;
144
+
145
+	if (sip_hostport2su(&to, dst_host, u->port_no, &proto) < 0)
146
+		return -1;
147
+	socket_info = get_send_socket(msg, &to, proto);
148
+	if (!socket_info)
149
+		return -1;
150
+
151
+	*res = socket_info->address_str;
152
+	return 0;
153
+}
154
+
106 155
 #define SELECT_uri_header(_name_) \
107 156
 int select_##_name_(str* res, select_t* s, struct sip_msg* msg) \
108 157
 { \
... ...
@@ -86,6 +86,7 @@ enum {
86 86
 SELECT_F(select_ruri)
87 87
 SELECT_F(select_dst_uri)
88 88
 SELECT_F(select_next_hop)
89
+SELECT_F(select_next_hop_src_ip)
89 90
 SELECT_F(select_from)
90 91
 SELECT_F(select_from_uri)
91 92
 SELECT_F(select_from_tag)
... ...
@@ -221,6 +222,7 @@ static select_row_t select_core[] = {
221 221
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("dst_uri"), select_dst_uri, 0},
222 222
 	{ select_dst_uri, SEL_PARAM_STR, STR_NULL, select_any_uri, NESTED},
223 223
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("next_hop"), select_next_hop, 0},
224
+	{ select_next_hop, SEL_PARAM_STR, STR_STATIC_INIT("src_ip"), select_next_hop_src_ip, 0},
224 225
 	{ select_next_hop, SEL_PARAM_STR, STR_NULL, select_any_uri, NESTED},
225 226
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("from"), select_from, 0},
226 227
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("f"), select_from, 0},