Browse code

dispatcher: reworked management of ping reply codes and classes

- do not generate all the 100 replies code for a class, but compare to be in
in the range

Daniel-Constantin Mierla authored on 13/01/2022 11:16:14
Showing 1 changed files
... ...
@@ -1123,22 +1123,21 @@ static int ds_parse_reply_codes()
1123 1123
 	int *ds_ping_reply_codes_new = NULL;
1124 1124
 	int *ds_ping_reply_codes_old = NULL;
1125 1125
 
1126
-	/* Validate String: */
1126
+	/* validate input string */
1127 1127
 	if(cfg_get(dispatcher, dispatcher_cfg, ds_ping_reply_codes_str).s == 0
1128 1128
 			|| cfg_get(dispatcher, dispatcher_cfg, ds_ping_reply_codes_str).len
1129 1129
 					   <= 0)
1130 1130
 		return 0;
1131 1131
 
1132
-	/* parse_params will modify the string pointer of .s, so we need to make a copy. */
1132
+	/* parse_params() updates the string pointer of .s -- make a copy */
1133 1133
 	input.s = cfg_get(dispatcher, dispatcher_cfg, ds_ping_reply_codes_str).s;
1134 1134
 	input.len =
1135 1135
 			cfg_get(dispatcher, dispatcher_cfg, ds_ping_reply_codes_str).len;
1136 1136
 
1137
-	/* Parse the parameters: */
1138 1137
 	if(parse_params(&input, CLASS_ANY, 0, &params_list) < 0)
1139 1138
 		return -1;
1140 1139
 
1141
-	/* Get the number of entries in the list */
1140
+	/* get the number of entries in the list */
1142 1141
 	for(pit = params_list; pit; pit = pit->next) {
1143 1142
 		if(pit->name.len == 4 && strncasecmp(pit->name.s, "code", 4) == 0) {
1144 1143
 			str2sint(&pit->body, &code);
... ...
@@ -1148,10 +1147,10 @@ static int ds_parse_reply_codes()
1148 1147
 				  && strncasecmp(pit->name.s, "class", 5) == 0) {
1149 1148
 			str2sint(&pit->body, &code);
1150 1149
 			if((code >= 1) && (code < 7))
1151
-				list_size += 100;
1150
+				list_size += 1;
1152 1151
 		}
1153 1152
 	}
1154
-	LM_DBG("expecting %d reply codes\n", list_size);
1153
+	LM_DBG("expecting %d reply codes and classes\n", list_size);
1155 1154
 
1156 1155
 	if(list_size > 0) {
1157 1156
 		/* Allocate Memory for the new list: */
... ...
@@ -1166,15 +1165,14 @@ static int ds_parse_reply_codes()
1166 1165
 		for(pit = params_list; pit; pit = pit->next) {
1167 1166
 			if(pit->name.len == 4 && strncasecmp(pit->name.s, "code", 4) == 0) {
1168 1167
 				str2sint(&pit->body, &code);
1169
-				if((code >= 100) && (code < 700))
1168
+				if((code >= 100) && (code < 700)) {
1170 1169
 					ds_ping_reply_codes_new[pos++] = code;
1170
+				}
1171 1171
 			} else if(pit->name.len == 5
1172 1172
 					  && strncasecmp(pit->name.s, "class", 5) == 0) {
1173 1173
 				str2sint(&pit->body, &code);
1174 1174
 				if((code >= 1) && (code < 7)) {
1175
-					/* Add every code from this class, e.g. 100 to 199 */
1176
-					for(i = (code * 100); i <= ((code * 100) + 99); i++)
1177
-						ds_ping_reply_codes_new[pos++] = i;
1175
+					ds_ping_reply_codes_new[pos++] = code;
1178 1176
 				}
1179 1177
 			}
1180 1178
 		}
... ...
@@ -1183,30 +1181,25 @@ static int ds_parse_reply_codes()
1183 1181
 	}
1184 1182
 	free_params(params_list);
1185 1183
 
1186
-	/* More reply-codes? Change Pointer and then set number of codes. */
1187 1184
 	if(list_size > *ds_ping_reply_codes_cnt) {
1188
-		// Copy Pointer
1185
+		/* if more reply-codes -- change pointer and then set number of codes */
1189 1186
 		ds_ping_reply_codes_old = *ds_ping_reply_codes;
1190 1187
 		*ds_ping_reply_codes = ds_ping_reply_codes_new;
1191
-		// Done: Set new Number of entries:
1192 1188
 		*ds_ping_reply_codes_cnt = list_size;
1193
-		// Free the old memory area:
1194 1189
 		if(ds_ping_reply_codes_old)
1195 1190
 			shm_free(ds_ping_reply_codes_old);
1196
-		/* Less or equal? Set the number of codes first. */
1197 1191
 	} else {
1198
-		// Done:
1192
+		/* less or equal reply codea -- set the number of codes first */
1199 1193
 		*ds_ping_reply_codes_cnt = list_size;
1200
-		// Copy Pointer
1201 1194
 		ds_ping_reply_codes_old = *ds_ping_reply_codes;
1202 1195
 		*ds_ping_reply_codes = ds_ping_reply_codes_new;
1203
-		// Free the old memory area:
1204 1196
 		if(ds_ping_reply_codes_old)
1205 1197
 			shm_free(ds_ping_reply_codes_old);
1206 1198
 	}
1207 1199
 	/* Print the list as INFO: */
1208 1200
 	for(i = 0; i < *ds_ping_reply_codes_cnt; i++) {
1209
-		LM_DBG("now accepting reply code %d (%d/%d) as valid\n",
1201
+		LM_DBG("accepting reply %s %d (%d/%d) as valid\n",
1202
+				((*ds_ping_reply_codes)[i]/10)?"code":"class",
1210 1203
 				(*ds_ping_reply_codes)[i], (i + 1), *ds_ping_reply_codes_cnt);
1211 1204
 	}
1212 1205
 	return 0;
... ...
@@ -1217,8 +1210,17 @@ int ds_ping_check_rplcode(int code)
1217 1210
 	int i;
1218 1211
 
1219 1212
 	for(i = 0; i < *ds_ping_reply_codes_cnt; i++) {
1220
-		if((*ds_ping_reply_codes)[i] == code)
1221
-			return 1;
1213
+		if((*ds_ping_reply_codes)[i] / 10) {
1214
+			/* reply code */
1215
+			if((*ds_ping_reply_codes)[i] == code) {
1216
+				return 1;
1217
+			}
1218
+		} else {
1219
+			/* reply class */
1220
+			if(((*ds_ping_reply_codes)[i] / 100) == code) {
1221
+				return 1;
1222
+			}
1223
+		}
1222 1224
 	}
1223 1225
 
1224 1226
 	return 0;