Browse code

dialog: api function get pkg-allocated duplicate of dlg var value

(cherry picked from commit 13512f48e03a2ef99847edf639582aaf3b4dfeb3)

Daniel-Constantin Mierla authored on 26/09/2022 09:57:02
Showing 4 changed files
... ...
@@ -42,7 +42,7 @@ struct dlg_cb_params {
42 42
 };
43 43
 
44 44
 /* callback function prototype */
45
-typedef void (dialog_cb) (struct dlg_cell* dlg, int type, 
45
+typedef void (dialog_cb) (struct dlg_cell* dlg, int type,
46 46
 		struct dlg_cb_params * params);
47 47
 /* function to free the callback param */
48 48
 typedef void (param_free_cb) (void *param);
... ...
@@ -60,6 +60,9 @@ typedef str* (*get_dlg_varref_f)( struct dlg_cell* dlg,
60 60
 /* method to get a variable from a dialog */
61 61
 typedef int (*get_dlg_varval_f)( struct dlg_cell* dlg,
62 62
                                     str* key, str* val);
63
+/* method to get a variable value duplicate from a dialog */
64
+typedef int (*get_dlg_vardup_f)( struct dlg_cell* dlg,
65
+                                    str* key, str* val);
63 66
 
64 67
 #define CONFIRMED_DIALOG_STATE 1
65 68
 
... ...
@@ -48,6 +48,7 @@ struct dlg_binds {
48 48
     set_dlg_variable_f set_dlg_var;
49 49
 	get_dlg_varref_f   get_dlg_varref;
50 50
 	get_dlg_varval_f   get_dlg_varval;
51
+	get_dlg_vardup_f   get_dlg_vardup;
51 52
 	get_dlg_f          get_dlg;
52 53
 	release_dlg_f      release_dlg;
53 54
 };
... ...
@@ -342,6 +342,40 @@ int get_dlg_varval(struct dlg_cell *dlg, str *key, str *val)
342 342
 	return -2;
343 343
 }
344 344
 
345
+/**
346
+ * set *val to a pkg-allocated buffer where the dlg value is cloned
347
+ * - val->s has to be pkg-freed after use
348
+ */
349
+int get_dlg_vardup(struct dlg_cell *dlg, str *key, str *val)
350
+{
351
+	str *var = NULL;
352
+
353
+	val->s = NULL;
354
+	val->len = 0;
355
+
356
+	if( !dlg || !key || key->len<=0) {
357
+		LM_ERR("BUG - bad parameters\n");
358
+		return -1;
359
+	}
360
+
361
+	dlg_lock(d_table, &(d_table->entries[dlg->h_entry]));
362
+	var = get_dlg_variable_unsafe(dlg, key);
363
+	if(var && var->s) {
364
+		val->s = (char*)pkg_malloc(var->len + 1);
365
+		if(val->s!=NULL) {
366
+			memcpy(val->s, var->s, var->len);
367
+			val->len = var->len;
368
+			val->s[val->len] = '\0';
369
+		}
370
+	}
371
+	dlg_unlock(d_table, &(d_table->entries[dlg->h_entry]));
372
+
373
+	if(val->s) {
374
+		return 0;
375
+	}
376
+	return -2;
377
+}
378
+
345 379
 int get_dlg_variable_uintval(struct dlg_cell *dlg, str *key, unsigned int *uval)
346 380
 {
347 381
 	str* var = NULL;
... ...
@@ -61,6 +61,7 @@ typedef struct dlg_var {
61 61
 
62 62
 str* get_dlg_varref(dlg_cell_t *dlg, str *key);
63 63
 int get_dlg_varval(dlg_cell_t *dlg, str *key, str *val);
64
+int get_dlg_vardup(dlg_cell_t *dlg, str *key, str *val);
64 65
 int set_dlg_variable(dlg_cell_t *dlg, str *key, str *val);
65 66
 
66 67
 int get_dlg_variable_uintval(struct dlg_cell *dlg, str *key, unsigned int *uval);