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 1020
 	return select_any_params(res, s, msg);
1020 1021
 }
1021 1022
 
1023
+ABSTRACT_F(select_src)
1024
+ABSTRACT_F(select_dst)
1025
+ABSTRACT_F(select_rcv)
1026
+int select_ip_port(str* res, select_t* s, struct sip_msg* msg)
1027
+{
1028
+	str ip_str=STR_NULL, port_str=STR_NULL, proto_str=STR_NULL;
1029
+	int param, pos;
1030
+	
1031
+
1032
+	if ((s->n != 2) || (s->params[1].type != SEL_PARAM_DIV)) return -1;
1033
+	param=s->params[1].v.i;
1034
+
1035
+	if (param & SEL_SRC) {
1036
+		if (param & SEL_IP) {
1037
+			ip_str.s = ip_addr2a(&msg->rcv.src_ip);
1038
+			ip_str.len = strlen(ip_str.s);
1039
+		}
1040
+		if (param & SEL_PORT) {
1041
+			port_str.s = int2str(msg->rcv.src_port, &port_str.len);
1042
+		}
1043
+	} else if (param & SEL_DST) {
1044
+		if (param & SEL_IP) {
1045
+			ip_str.s = ip_addr2a(&msg->rcv.dst_ip);
1046
+			ip_str.len = strlen(ip_str.s);
1047
+		}
1048
+		if (param & SEL_PORT) {
1049
+			port_str.s = int2str(msg->rcv.dst_port, &port_str.len);
1050
+		}
1051
+	} else if (param & SEL_RCV) {
1052
+		if (param & SEL_IP) {
1053
+			ip_str = msg->rcv.bind_address->address_str;
1054
+		}
1055
+		if (param & SEL_PORT) {
1056
+			port_str = msg->rcv.bind_address->port_no_str;
1057
+		}
1058
+		if (param & SEL_PROTO) {
1059
+			switch (msg->rcv.proto) {
1060
+			case PROTO_NONE:
1061
+				proto_str.s = 0;
1062
+				proto_str.len = 0;
1063
+				break;
1064
+
1065
+			case PROTO_UDP:
1066
+				proto_str.s = "udp";
1067
+				proto_str.len = 3;
1068
+				break;
1069
+
1070
+			case PROTO_TCP:
1071
+				proto_str.s = "tcp";
1072
+				proto_str.len = 3;
1073
+				break;
1074
+
1075
+			case PROTO_TLS:
1076
+				proto_str.s = "tls";
1077
+				proto_str.len = 3;
1078
+				break;
1079
+
1080
+			case PROTO_SCTP:
1081
+				proto_str.s = "sctp";
1082
+				proto_str.len = 4;
1083
+				break;
1084
+
1085
+			default:
1086
+				ERR("BUG: select_ip_port: Unknown transport protocol\n");
1087
+				return -1;
1088
+			}
1089
+		}
1090
+	} else {
1091
+		return -1;
1092
+	}
1093
+
1094
+	res->s = get_static_buffer(ip_str.len+port_str.len+proto_str.len+3);
1095
+	if (!res->s) return -1;
1096
+
1097
+	pos=0;
1098
+	if (param & SEL_PROTO) {
1099
+		memcpy(res->s, proto_str.s, proto_str.len);
1100
+		pos += proto_str.len;
1101
+	}
1102
+	if (param & SEL_IP) {
1103
+		if (pos) res->s[pos++] = ':';
1104
+		memcpy(res->s+pos, ip_str.s, ip_str.len);
1105
+		pos += ip_str.len;
1106
+	}
1107
+	if (param & SEL_PORT) {
1108
+		if (pos) res->s[pos++] = ':';
1109
+		memcpy(res->s+pos, port_str.s, port_str.len);
1110
+		pos += port_str.len;
1111
+	}
1112
+	res->s[pos] = 0;
1113
+	res->len = pos;
1114
+	return (pos==0 ? 1 : 0);
1115
+
1116
+}
1117
+
... ...
@@ -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 133
 SELECT_F(select_auth_username)
124 134
 SELECT_F(select_auth_username_comp)
125 135
 
136
+SELECT_F(select_src)
137
+SELECT_F(select_dst)
138
+SELECT_F(select_rcv)
139
+SELECT_F(select_ip_port)
140
+
126 141
 static select_row_t select_core[] = {
127 142
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("method"), select_method, 0},
128 143
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("ruri"), select_ruri, 0},
... ...
@@ -214,6 +229,21 @@ static select_row_t select_core[] = {
214 229
 	{ select_auth_username, SEL_PARAM_STR, STR_STATIC_INIT("user"), select_auth_username_comp, DIVERSION | SEL_AUTH_USER},
215 230
 	{ select_auth_username, SEL_PARAM_STR, STR_STATIC_INIT("domain"), select_auth_username_comp, DIVERSION | SEL_AUTH_DOMAIN},
216 231
 
232
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("src"), select_src, SEL_PARAM_EXPECTED},
233
+	{ select_src, SEL_PARAM_STR, STR_STATIC_INIT("ip"), select_ip_port, DIVERSION | SEL_SRC | SEL_IP},
234
+	{ select_src, SEL_PARAM_STR, STR_STATIC_INIT("port"), select_ip_port, DIVERSION | SEL_SRC | SEL_PORT},
235
+	{ select_src, SEL_PARAM_STR, STR_STATIC_INIT("ip_port"), select_ip_port, DIVERSION | SEL_SRC | SEL_IP_PORT},
236
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("dst"), select_dst, SEL_PARAM_EXPECTED},
237
+	{ select_dst, SEL_PARAM_STR, STR_STATIC_INIT("ip"), select_ip_port, DIVERSION | SEL_DST | SEL_IP},
238
+	{ select_dst, SEL_PARAM_STR, STR_STATIC_INIT("port"), select_ip_port, DIVERSION | SEL_DST | SEL_PORT},
239
+	{ select_dst, SEL_PARAM_STR, STR_STATIC_INIT("ip_port"), select_ip_port, DIVERSION | SEL_DST | SEL_IP_PORT},
240
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("received"), select_rcv, SEL_PARAM_EXPECTED},
241
+	{ select_rcv, SEL_PARAM_STR, STR_STATIC_INIT("proto"), select_ip_port, DIVERSION | SEL_RCV | SEL_PROTO},
242
+	{ select_rcv, SEL_PARAM_STR, STR_STATIC_INIT("ip"), select_ip_port, DIVERSION | SEL_RCV | SEL_IP},
243
+	{ select_rcv, SEL_PARAM_STR, STR_STATIC_INIT("port"), select_ip_port, DIVERSION | SEL_RCV | SEL_PORT},
244
+	{ select_rcv, SEL_PARAM_STR, STR_STATIC_INIT("ip_port"), select_ip_port, DIVERSION | SEL_RCV | SEL_IP_PORT},
245
+	{ select_rcv, SEL_PARAM_STR, STR_STATIC_INIT("proto_ip_port"), select_ip_port, DIVERSION | SEL_RCV | SEL_PROTO | SEL_IP_PORT},
246
+
217 247
 	{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
218 248
 };
219 249