Browse code

new select functions:

Source ip/port based on message's src ip_addr struct:
- src.(ip | port | ip_port)

Destination ip/port based on message's dst ip_addr struct:
- dst.(ip | port | ip_port)

Received info (based on socket used while received this message):
- received.(proto | ip | port | ip_port | proto_ip_port)

Michal Matyska authored on 20/10/2006 11:59:47
Showing 2 changed files
... ...
@@ -51,6 +51,7 @@
51 51
 #include "parser/digest/digest.h"
52 52
 #include "mem/mem.h"
53 53
 #include "parser/parse_hname2.h"
54
+#include "ip_addr.h"
54 55
 
55 56
 #define RETURN0_res(x) {*res=x;return 0;}
56 57
 #define TRIM_RET0_res(x) {*res=x;trim(res);return 0;} 
... ...
@@ -1019,3 +1020,98 @@ int select_nameaddr_params(str* res, select_t* s, struct sip_msg* msg)
1019 1019
 	return select_any_params(res, s, msg);
1020 1020
 }
1021 1021
 
1022
+ABSTRACT_F(select_src)
1023
+ABSTRACT_F(select_dst)
1024
+ABSTRACT_F(select_rcv)
1025
+int select_ip_port(str* res, select_t* s, struct sip_msg* msg)
1026
+{
1027
+	str ip_str=STR_NULL, port_str=STR_NULL, proto_str=STR_NULL;
1028
+	int param, pos;
1029
+	
1030
+
1031
+	if ((s->n != 2) || (s->params[1].type != SEL_PARAM_DIV)) return -1;
1032
+	param=s->params[1].v.i;
1033
+
1034
+	if (param & SEL_SRC) {
1035
+		if (param & SEL_IP) {
1036
+			ip_str.s = ip_addr2a(&msg->rcv.src_ip);
1037
+			ip_str.len = strlen(ip_str.s);
1038
+		}
1039
+		if (param & SEL_PORT) {
1040
+			port_str.s = int2str(msg->rcv.src_port, &port_str.len);
1041
+		}
1042
+	} else if (param & SEL_DST) {
1043
+		if (param & SEL_IP) {
1044
+			ip_str.s = ip_addr2a(&msg->rcv.dst_ip);
1045
+			ip_str.len = strlen(ip_str.s);
1046
+		}
1047
+		if (param & SEL_PORT) {
1048
+			port_str.s = int2str(msg->rcv.dst_port, &port_str.len);
1049
+		}
1050
+	} else if (param & SEL_RCV) {
1051
+		if (param & SEL_IP) {
1052
+			ip_str = msg->rcv.bind_address->address_str;
1053
+		}
1054
+		if (param & SEL_PORT) {
1055
+			port_str = msg->rcv.bind_address->port_no_str;
1056
+		}
1057
+		if (param & SEL_PROTO) {
1058
+			switch (msg->rcv.proto) {
1059
+			case PROTO_NONE:
1060
+				proto_str.s = 0;
1061
+				proto_str.len = 0;
1062
+				break;
1063
+
1064
+			case PROTO_UDP:
1065
+				proto_str.s = "udp";
1066
+				proto_str.len = 3;
1067
+				break;
1068
+
1069
+			case PROTO_TCP:
1070
+				proto_str.s = "tcp";
1071
+				proto_str.len = 3;
1072
+				break;
1073
+
1074
+			case PROTO_TLS:
1075
+				proto_str.s = "tls";
1076
+				proto_str.len = 3;
1077
+				break;
1078
+
1079
+			case PROTO_SCTP:
1080
+				proto_str.s = "sctp";
1081
+				proto_str.len = 4;
1082
+				break;
1083
+
1084
+			default:
1085
+				ERR("BUG: select_ip_port: Unknown transport protocol\n");
1086
+				return -1;
1087
+			}
1088
+		}
1089
+	} else {
1090
+		return -1;
1091
+	}
1092
+
1093
+	res->s = get_static_buffer(ip_str.len+port_str.len+proto_str.len+3);
1094
+	if (!res->s) return -1;
1095
+
1096
+	pos=0;
1097
+	if (param & SEL_PROTO) {
1098
+		memcpy(res->s, proto_str.s, proto_str.len);
1099
+		pos += proto_str.len;
1100
+	}
1101
+	if (param & SEL_IP) {
1102
+		if (pos) res->s[pos++] = ':';
1103
+		memcpy(res->s+pos, ip_str.s, ip_str.len);
1104
+		pos += ip_str.len;
1105
+	}
1106
+	if (param & SEL_PORT) {
1107
+		if (pos) res->s[pos++] = ':';
1108
+		memcpy(res->s+pos, port_str.s, port_str.len);
1109
+		pos += port_str.len;
1110
+	}
1111
+	res->s[pos] = 0;
1112
+	res->len = pos;
1113
+	return (pos==0 ? 1 : 0);
1114
+
1115
+}
1116
+
... ...
@@ -61,6 +61,16 @@ enum {
61 61
 	SEL_AUTH_QOP
62 62
 };
63 63
 
64
+enum {
65
+	SEL_SRC  = 1<<0,
66
+	SEL_DST  = 1<<1,
67
+	SEL_RCV  = 1<<2,
68
+	SEL_PROTO= 1<<5,
69
+	SEL_IP   = 1<<6,
70
+	SEL_PORT = 1<<7,
71
+	SEL_IP_PORT = SEL_IP | SEL_PORT,
72
+};
73
+
64 74
 SELECT_F(select_method)
65 75
 SELECT_F(select_ruri)
66 76
 SELECT_F(select_from)
... ...
@@ -123,6 +133,11 @@ SELECT_F(select_auth_param)
123 123
 SELECT_F(select_auth_username)
124 124
 SELECT_F(select_auth_username_comp)
125 125
 
126
+SELECT_F(select_src)
127
+SELECT_F(select_dst)
128
+SELECT_F(select_rcv)
129
+SELECT_F(select_ip_port)
130
+
126 131
 static select_row_t select_core[] = {
127 132
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("method"), select_method, 0},
128 133
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("ruri"), select_ruri, 0},
... ...
@@ -214,6 +229,21 @@ static select_row_t select_core[] = {
214 214
 	{ select_auth_username, SEL_PARAM_STR, STR_STATIC_INIT("user"), select_auth_username_comp, DIVERSION | SEL_AUTH_USER},
215 215
 	{ select_auth_username, SEL_PARAM_STR, STR_STATIC_INIT("domain"), select_auth_username_comp, DIVERSION | SEL_AUTH_DOMAIN},
216 216
 
217
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("src"), select_src, SEL_PARAM_EXPECTED},
218
+	{ select_src, SEL_PARAM_STR, STR_STATIC_INIT("ip"), select_ip_port, DIVERSION | SEL_SRC | SEL_IP},
219
+	{ select_src, SEL_PARAM_STR, STR_STATIC_INIT("port"), select_ip_port, DIVERSION | SEL_SRC | SEL_PORT},
220
+	{ select_src, SEL_PARAM_STR, STR_STATIC_INIT("ip_port"), select_ip_port, DIVERSION | SEL_SRC | SEL_IP_PORT},
221
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("dst"), select_dst, SEL_PARAM_EXPECTED},
222
+	{ select_dst, SEL_PARAM_STR, STR_STATIC_INIT("ip"), select_ip_port, DIVERSION | SEL_DST | SEL_IP},
223
+	{ select_dst, SEL_PARAM_STR, STR_STATIC_INIT("port"), select_ip_port, DIVERSION | SEL_DST | SEL_PORT},
224
+	{ select_dst, SEL_PARAM_STR, STR_STATIC_INIT("ip_port"), select_ip_port, DIVERSION | SEL_DST | SEL_IP_PORT},
225
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("received"), select_rcv, SEL_PARAM_EXPECTED},
226
+	{ select_rcv, SEL_PARAM_STR, STR_STATIC_INIT("proto"), select_ip_port, DIVERSION | SEL_RCV | SEL_PROTO},
227
+	{ select_rcv, SEL_PARAM_STR, STR_STATIC_INIT("ip"), select_ip_port, DIVERSION | SEL_RCV | SEL_IP},
228
+	{ select_rcv, SEL_PARAM_STR, STR_STATIC_INIT("port"), select_ip_port, DIVERSION | SEL_RCV | SEL_PORT},
229
+	{ select_rcv, SEL_PARAM_STR, STR_STATIC_INIT("ip_port"), select_ip_port, DIVERSION | SEL_RCV | SEL_IP_PORT},
230
+	{ select_rcv, SEL_PARAM_STR, STR_STATIC_INIT("proto_ip_port"), select_ip_port, DIVERSION | SEL_RCV | SEL_PROTO | SEL_IP_PORT},
231
+
217 232
 	{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
218 233
 };
219 234