Explorar o código

corex: added sendx(uri, socket, data)

- send data to destination uri by using local socket
Daniel-Constantin Mierla %!s(int64=7) %!d(string=hai) anos
pai
achega
cf11f16c60
Modificáronse 3 ficheiros con 64 adicións e 7 borrados
  1. 33 5
      src/modules/corex/corex_lib.c
  2. 1 1
      src/modules/corex/corex_lib.h
  3. 30 1
      src/modules/corex/corex_mod.c

+ 33 - 5
src/modules/corex/corex_lib.c

@@ -315,15 +315,17 @@ error:
 /**
  *
  */
-int corex_send_data(str *puri, str *pdata)
+int corex_send_data(str *puri, str *psock, str *pdata)
 {
 	struct dest_info dst;
 	sip_uri_t next_hop;
 	int ret = 0;
 	char proto;
+	socket_info_t *si = NULL;
+	int sport, sproto;
+	str shost;
 
-	if(parse_uri(puri->s, puri->len, &next_hop)<0)
-	{
+	if(parse_uri(puri->s, puri->len, &next_hop)<0) {
 		LM_ERR("bad dst sip uri <%.*s>\n", puri->len, puri->s);
 		return -1;
 	}
@@ -337,12 +339,28 @@ int corex_send_data(str *puri, str *pdata)
 			ZSW(next_hop.host.s));
 		return -1;
 	}
+
+	if(psock && psock->s && psock->len>0) {
+		if (parse_phostport(psock->s, &shost.s, &shost.len, &sport, &sproto) < 0) {
+			LM_ERR("invalid socket specification\n");
+			return -1;
+		}
+		si = grep_sock_info(&shost, (unsigned short)sport, (unsigned short)sproto);
+		if (si==NULL) {
+			LM_WARN("local socket not found: %.*s\n", psock->len, psock->s);
+		}
+	}
+
 	dst.proto = proto;
 	if(dst.proto==PROTO_NONE) dst.proto = PROTO_UDP;
 
 	if (dst.proto == PROTO_UDP)
 	{
-		dst.send_sock=get_send_socket(0, &dst.to, PROTO_UDP);
+		if(si!=NULL) {
+			dst.send_sock=si;
+		} else {
+			dst.send_sock=get_send_socket(0, &dst.to, PROTO_UDP);
+		}
 		if (dst.send_sock!=0) {
 			ret=udp_send(&dst, pdata->s, pdata->len);
 		} else {
@@ -352,6 +370,9 @@ int corex_send_data(str *puri, str *pdata)
 	}
 #ifdef USE_TCP
 	else if(dst.proto == PROTO_TCP) {
+		if(si!=NULL) {
+			dst.send_sock=si;
+		}
 		/*tcp*/
 		dst.id=0;
 		ret=tcp_send(&dst, 0, pdata->s, pdata->len);
@@ -359,6 +380,9 @@ int corex_send_data(str *puri, str *pdata)
 #endif
 #ifdef USE_TLS
 	else if(dst.proto == PROTO_TLS) {
+		if(si!=NULL) {
+			dst.send_sock=si;
+		}
 		/*tls*/
 		dst.id=0;
 		ret=tcp_send(&dst, 0, pdata->s, pdata->len);
@@ -367,7 +391,11 @@ int corex_send_data(str *puri, str *pdata)
 #ifdef USE_SCTP
 	else if(dst.proto == PROTO_SCTP) {
 		/*sctp*/
-		dst.send_sock=get_send_socket(0, &dst.to, PROTO_SCTP);
+		if(si!=NULL) {
+			dst.send_sock=si;
+		} else {
+			dst.send_sock=get_send_socket(0, &dst.to, PROTO_SCTP);
+		}
 		if (dst.send_sock!=0) {
 			ret=sctp_core_msg_send(&dst, pdata->s, pdata->len);
 		} else {

+ 1 - 1
src/modules/corex/corex_lib.h

@@ -27,7 +27,7 @@
 int w_corex_append_branch(sip_msg_t *msg, gparam_t *pu, gparam_t *pq);
 int corex_append_branch(sip_msg_t *msg, str *uri, str *qv);
 int corex_send(sip_msg_t *msg, gparam_t *pu, enum sip_protos proto);
-int corex_send_data(str *puri, str *pdata);
+int corex_send_data(str *puri, str *psock, str *pdata);
 
 int corex_add_alias_subdomains(char* aliasval);
 

+ 30 - 1
src/modules/corex/corex_mod.c

@@ -44,6 +44,7 @@ static int w_append_branch(sip_msg_t *msg, char *su, char *sq);
 static int w_send(sip_msg_t *msg, char *su, char *sq);
 static int w_send_tcp(sip_msg_t *msg, char *su, char *sq);
 static int w_send_data(sip_msg_t *msg, char *suri, char *sdata);
+static int w_sendx(sip_msg_t *msg, char *suri, char *ssock, char *sdata);
 static int w_msg_iflag_set(sip_msg_t *msg, char *pflag, char *p2);
 static int w_msg_iflag_reset(sip_msg_t *msg, char *pflag, char *p2);
 static int w_msg_iflag_is_set(sip_msg_t *msg, char *pflag, char *p2);
@@ -90,6 +91,8 @@ static cmd_export_t cmds[]={
 		0, REQUEST_ROUTE | FAILURE_ROUTE },
 	{"send_data", (cmd_function)w_send_data, 2, fixup_spve_spve,
 		0, ANY_ROUTE },
+	{"sendx", (cmd_function)w_sendx, 3, fixup_spve_all,
+		0, ANY_ROUTE },
 	{"is_incoming",    (cmd_function)nio_check_incoming, 0, 0,
 		0, ANY_ROUTE },
 	{"msg_iflag_set", (cmd_function)w_msg_iflag_set,       1, fixup_spve_null,
@@ -233,7 +236,33 @@ static int w_send_data(sip_msg_t *msg, char *suri, char *sdata)
 		LM_ERR("cannot get the destination parameter\n");
 		return -1;
 	}
-	if(corex_send_data(&uri, &data) < 0)
+	if(corex_send_data(&uri, NULL, &data) < 0)
+		return -1;
+	return 1;
+}
+
+static int w_sendx(sip_msg_t *msg, char *suri, char *ssock, char *sdata)
+{
+	str uri;
+	str sock;
+	str data;
+
+	if (fixup_get_svalue(msg, (gparam_t*)suri, &uri))
+	{
+		LM_ERR("cannot get the destination parameter\n");
+		return -1;
+	}
+	if (fixup_get_svalue(msg, (gparam_t*)ssock, &sock))
+	{
+		LM_ERR("cannot get the socket parameter\n");
+		return -1;
+	}
+	if (fixup_get_svalue(msg, (gparam_t*)sdata, &data))
+	{
+		LM_ERR("cannot get the destination parameter\n");
+		return -1;
+	}
+	if(corex_send_data(&uri, &sock, &data) < 0)
 		return -1;
 	return 1;
 }