Browse code

uac: new rpc command uac.reg_unregister

- send REGISTER with expires 0 for matching record, turning it in
disable state
- GH #2549

Daniel-Constantin Mierla authored on 18/11/2020 08:54:01
Showing 1 changed files
... ...
@@ -1063,7 +1063,11 @@ done:
1063 1063
 	counter_inc(regactive);
1064 1064
 }
1065 1065
 
1066
-int uac_reg_update(reg_uac_t *reg, time_t tn)
1066
+
1067
+/**
1068
+ *
1069
+ */
1070
+int uac_reg_send(reg_uac_t *reg, time_t tn)
1067 1071
 {
1068 1072
 	char *uuid;
1069 1073
 	uac_req_t uac_r;
... ...
@@ -1077,36 +1081,8 @@ int uac_reg_update(reg_uac_t *reg, time_t tn)
1077 1081
 	str   s_hdrs;
1078 1082
 	dlg_t tmdlg;
1079 1083
 
1080
-	if(uac_tmb.t_request==NULL)
1081
-		return -1;
1082
-	if(reg->expires==0)
1083
-		return 1;
1084
-	if(reg->flags&UAC_REG_ONGOING) {
1085
-		if (reg->timer_expires > tn - reg_retry_interval)
1086
-			return 2;
1087
-		LM_DBG("record marked as ongoing registration (%d) - resetting\n",
1088
-				(int)reg->flags);
1089
-		reg->flags &= ~(UAC_REG_ONLINE|UAC_REG_AUTHSENT);
1090
-	}
1091
-	if(reg_active && *reg_active == 0)
1092
-		return 4;
1093
-	if(reg->flags&UAC_REG_DISABLED)
1094
-		return 4;
1095
-
1096
-	if(!(reg->flags & UAC_REG_INIT)) {
1097
-		if(reg->reg_delay>0) {
1098
-			if(tn < reg->reg_init+reg->reg_delay) {
1099
-				return 2;
1100
-			}
1101
-		}
1102
-		reg->flags |= UAC_REG_INIT;
1103
-	}
1104
-
1105
-	if(reg->timer_expires > tn + reg_timer_interval + 3)
1106
-		return 3;
1107 1084
 	uuid = (char*)shm_malloc(reg->l_uuid.len+1);
1108
-	if(uuid==NULL)
1109
-	{
1085
+	if(uuid==NULL) {
1110 1086
 		SHM_MEM_ERROR;
1111 1087
 		return -1;
1112 1088
 	}
... ...
@@ -1157,8 +1133,7 @@ int uac_reg_update(reg_uac_t *reg, time_t tn)
1157 1133
 
1158 1134
 	if (reg_keep_callid && reg->flags & UAC_REG_ONLINE
1159 1135
 				&& reg->cseq > 0 && reg->cseq < 2147483638
1160
-				&& reg->callid.len > 0)
1161
-	{
1136
+				&& reg->callid.len > 0) {
1162 1137
 		/* reregister, reuse callid and cseq */
1163 1138
 		memset(&tmdlg, 0, sizeof(dlg_t));
1164 1139
 		tmdlg.id.call_id = reg->callid;
... ...
@@ -1187,9 +1162,9 @@ int uac_reg_update(reg_uac_t *reg, time_t tn)
1187 1162
 	{
1188 1163
 		LM_ERR("failed to send request for [%.*s]", reg->l_uuid.len, reg->l_uuid.s);
1189 1164
 		shm_free(uuid);
1190
-		if (reg_retry_interval)
1165
+		if (reg_retry_interval) {
1191 1166
 			reg->timer_expires = (tn ? tn : time(NULL)) + reg_retry_interval;
1192
-		else {
1167
+		} else {
1193 1168
 			reg->flags |= UAC_REG_DISABLED;
1194 1169
 			counter_inc(regdisabled);
1195 1170
 		}
... ...
@@ -1199,6 +1174,43 @@ int uac_reg_update(reg_uac_t *reg, time_t tn)
1199 1174
 	return 0;
1200 1175
 }
1201 1176
 
1177
+
1178
+/**
1179
+ *
1180
+ */
1181
+int uac_reg_update(reg_uac_t *reg, time_t tn)
1182
+{
1183
+	if(uac_tmb.t_request==NULL)
1184
+		return -1;
1185
+	if(reg->expires==0)
1186
+		return 1;
1187
+	if(reg->flags&UAC_REG_ONGOING) {
1188
+		if (reg->timer_expires > tn - reg_retry_interval)
1189
+			return 2;
1190
+		LM_DBG("record marked as ongoing registration (%d) - resetting\n",
1191
+				(int)reg->flags);
1192
+		reg->flags &= ~(UAC_REG_ONLINE|UAC_REG_AUTHSENT);
1193
+	}
1194
+	if(reg_active && *reg_active == 0)
1195
+		return 4;
1196
+	if(reg->flags&UAC_REG_DISABLED)
1197
+		return 4;
1198
+
1199
+	if(!(reg->flags & UAC_REG_INIT)) {
1200
+		if(reg->reg_delay>0) {
1201
+			if(tn < reg->reg_init+reg->reg_delay) {
1202
+				return 2;
1203
+			}
1204
+		}
1205
+		reg->flags |= UAC_REG_INIT;
1206
+	}
1207
+
1208
+	if(reg->timer_expires > tn + reg_timer_interval + 3)
1209
+		return 3;
1210
+
1211
+	return uac_reg_send(reg, tn);
1212
+}
1213
+
1202 1214
 /**
1203 1215
  *
1204 1216
  */
... ...
@@ -2212,6 +2224,50 @@ static void rpc_uac_reg_active(rpc_t* rpc, void* ctx)
2212 2224
 	}
2213 2225
 }
2214 2226
 
2227
+static const char* rpc_uac_reg_unregister_doc[2] = {
2228
+	"Send a register request with expires 0.",
2229
+	0
2230
+};
2231
+
2232
+
2233
+static void rpc_uac_reg_unregister(rpc_t* rpc, void* ctx)
2234
+{
2235
+	reg_uac_t *reg = NULL;
2236
+	str attr = {0};
2237
+	str val = {0};
2238
+	int ret;
2239
+
2240
+	if(_reg_htable==NULL) {
2241
+		rpc->fault(ctx, 500, "Not enabled");
2242
+		return;
2243
+	}
2244
+
2245
+	if(rpc->scan(ctx, "S.S", &attr, &val)<2) {
2246
+		rpc->fault(ctx, 400, "Invalid Parameters");
2247
+		return;
2248
+	}
2249
+	if(attr.len<=0 || attr.s==NULL || val.len<=0 || val.s==NULL) {
2250
+		LM_ERR("bad parameter values\n");
2251
+		rpc->fault(ctx, 400, "Invalid Parameter Values");
2252
+		return;
2253
+	}
2254
+
2255
+	ret = reg_ht_get_byfilter(&reg, &attr, &val);
2256
+	if (ret == 0) {
2257
+		rpc->fault(ctx, 404, "Record not found");
2258
+		return;
2259
+	} else if (ret < 0) {
2260
+		rpc->fault(ctx, 400, "Unsupported filter attribute");
2261
+		return;
2262
+	}
2263
+
2264
+	reg->expires = 0;
2265
+	uac_reg_send(reg, time(NULL));
2266
+
2267
+	lock_release(reg->lock);
2268
+	return;
2269
+}
2270
+
2215 2271
 rpc_export_t uac_reg_rpc[] = {
2216 2272
 	{"uac.reg_dump", rpc_uac_reg_dump, rpc_uac_reg_dump_doc, RET_ARRAY},
2217 2273
 	{"uac.reg_info", rpc_uac_reg_info, rpc_uac_reg_info_doc, 0},
... ...
@@ -2222,6 +2278,7 @@ rpc_export_t uac_reg_rpc[] = {
2222 2278
 	{"uac.reg_remove", rpc_uac_reg_remove, rpc_uac_reg_remove_doc, 0},
2223 2279
 	{"uac.reg_add", rpc_uac_reg_add, rpc_uac_reg_add_doc, 0},
2224 2280
 	{"uac.reg_active", rpc_uac_reg_active, rpc_uac_reg_active_doc, 0},
2281
+	{"uac.reg_unregister", rpc_uac_reg_unregister, rpc_uac_reg_unregister_doc, 0},
2225 2282
 	{0, 0, 0, 0}
2226 2283
 };
2227 2284