Browse code

- for user not found in db, or found with an empty script, no error will be generated anymore, but ser script will be mormally resumed. - before starting script interpretation, 100 is send back.

Bogdan-Andrei Iancu authored on 21/08/2003 11:43:21
Showing 5 changed files
... ...
@@ -75,7 +75,7 @@ MODULE_VERSION
75 75
 
76 76
 
77 77
 
78
-static int cpl_run_script(struct sip_msg* msg, char* str, char* str2);
78
+static int cpl_invoke_script(struct sip_msg* msg, char* str, char* str2);
79 79
 static int cpl_process_register(struct sip_msg* msg, char* str, char* str2);
80 80
 static int fixup_cpl_run_script(void** param, int param_no);
81 81
 static int cpl_init(void);
... ...
@@ -87,7 +87,7 @@ static int cpl_exit(void);
87 87
  * Exported functions
88 88
  */
89 89
 static cmd_export_t cmds[] = {
90
-	{"cpl_run_script", cpl_run_script, 1, fixup_cpl_run_script, REQUEST_ROUTE},
90
+	{"cpl_run_script", cpl_invoke_script,1,fixup_cpl_run_script,REQUEST_ROUTE},
91 91
 	{"cpl_process_register", cpl_process_register, 0, 0, REQUEST_ROUTE},
92 92
 	{0, 0, 0, 0, 0}
93 93
 };
... ...
@@ -333,7 +333,9 @@ static int cpl_exit(void)
333 333
 
334 334
 
335 335
 
336
-static int cpl_run_script(struct sip_msg* msg, char* str1, char* str2)
336
+/* Params: str1 - as unsigned int - can be CPL_RUN_INCOMING
337
+ * or CPL_RUN_OUTGOING */
338
+static int cpl_invoke_script(struct sip_msg* msg, char* str1, char* str2)
337 339
 {
338 340
 	struct cpl_interpreter  *cpl_intr;
339 341
 	struct to_body          *from;
... ...
@@ -346,20 +348,20 @@ static int cpl_run_script(struct sip_msg* msg, char* str1, char* str2)
346 348
 	/* get the user_name */
347 349
 	if ( ((unsigned int)str1)&CPL_RUN_INCOMING ) {
348 350
 		/* if it's incoming -> get the user_name from new_uri/RURI/To */
349
-		DBG("DEBUG:cpl_run_script: tring to get user from new_uri\n");
351
+		DBG("DEBUG:cpl_invoke_script: tring to get user from new_uri\n");
350 352
 		if ( !msg->new_uri.s||parse_uri( msg->new_uri.s,msg->new_uri.len,&uri)
351 353
 		|| !uri.user.len )
352 354
 		{
353
-			DBG("DEBUG:cpl_run_script: tring to get user from R_uri\n");
355
+			DBG("DEBUG:cpl_invoke_script: tring to get user from R_uri\n");
354 356
 			if ( parse_uri( msg->first_line.u.request.uri.s,
355 357
 			msg->first_line.u.request.uri.len ,&uri)||!uri.user.len )
356 358
 			{
357
-				DBG("DEBUG:cpl_run_script: tring to get user from To\n");
359
+				DBG("DEBUG:cpl_invoke_script: tring to get user from To\n");
358 360
 				if (!msg->to || !get_to(msg) ||
359 361
 				parse_uri( get_to(msg)->uri.s, get_to(msg)->uri.len, &uri)
360 362
 				||!uri.user.len)
361 363
 				{
362
-					LOG(L_ERR,"ERROR:cpl_run_script: unable to extract user"
364
+					LOG(L_ERR,"ERROR:cpl_invoke_script: unable to extract user"
363 365
 					" name from RURI or To header!\n");
364 366
 					goto error;
365 367
 				}
... ...
@@ -368,15 +370,16 @@ static int cpl_run_script(struct sip_msg* msg, char* str1, char* str2)
368 370
 	} else {
369 371
 		/* if it's outgoing -> get the user_name from From */
370 372
 		/* parsing from header */
373
+		DBG("DEBUG:cpl_invoke_script: tring to get user from From\n");
371 374
 		if ( parse_from_header( msg )==-1 ) {
372
-			LOG(L_ERR,"ERROR:cpl_run_script: unable to extract URI "
375
+			LOG(L_ERR,"ERROR:cpl_invoke_script: unable to extract URI "
373 376
 				"from FROM header\n");
374 377
 			goto error;
375 378
 		}
376 379
 		from = (struct to_body*)msg->from->parsed;
377 380
 		/* parse the extracted uri from From */
378 381
 		if (parse_uri( from->uri.s, from->uri.len, &uri)||!uri.user.len) {
379
-			LOG(L_ERR,"ERROR:cpl_run_script: unable to extract user name "
382
+			LOG(L_ERR,"ERROR:cpl_invoke_script: unable to extract user name "
380 383
 				"from URI (From header)\n");
381 384
 			goto error;
382 385
 		}
... ...
@@ -386,6 +389,11 @@ static int cpl_run_script(struct sip_msg* msg, char* str1, char* str2)
386 389
 	if (get_user_script( db_hdl, &uri.user, &script)==-1)
387 390
 		goto error;
388 391
 
392
+	/* has the user a non-empty script? if not, return normaly, allowing ser to
393
+	 * continue its script */
394
+	if ( !script.s || !script.len )
395
+		return 1;
396
+
389 397
 	/* build a new script interpreter */
390 398
 	if ( (cpl_intr=new_cpl_interpreter(msg,&script))==0 )
391 399
 		goto error;
... ...
@@ -394,8 +402,15 @@ static int cpl_run_script(struct sip_msg* msg, char* str1, char* str2)
394 402
 	/* attache the user */
395 403
 	cpl_intr->user = uri.user;
396 404
 
405
+	/* since the script interpretation can take some time, it will be better to
406
+	 * send a 100 back to prevent the UAC to retransmit */
407
+	if ( cpl_tmb.t_reply( msg, (int)100, "Running cpl script" )!=1 ) {
408
+		LOG(L_ERR,"ERROR:cpl_invoke_script: unable to send 100 reply!\n");
409
+		goto error;
410
+	}
411
+
397 412
 	/* run the script */
398
-	switch (run_cpl_script( cpl_intr )) {
413
+	switch (cpl_run_script( cpl_intr )) {
399 414
 		case SCRIPT_DEFAULT:
400 415
 			free_cpl_interpreter( cpl_intr );
401 416
 			return 1; /* execution of ser's script will continue */
... ...
@@ -32,6 +32,12 @@
32 32
 
33 33
 
34 34
 
35
+
36
+/* gets from database the cpl script in binary format; the returned script is
37
+ * allocated in shared memory
38
+ * Returns:  1 - success
39
+ *          -1 - error
40
+ */
35 41
 int get_user_script( db_con_t *db_hdl, str *user, str *script)
36 42
 {
37 43
 	db_key_t   keys_cmp[] = {"user"};
... ...
@@ -40,7 +46,7 @@ int get_user_script( db_con_t *db_hdl, str *user, str *script)
40 46
 	db_res_t   *res = 0 ;
41 47
 	char       tmp;
42 48
 
43
-	/* it shouldn't be aproblem verwriting a \0 at the end of user name; the
49
+	/* it shouldn't be aproblem overwriting a \0 at the end of user name; the
44 50
 	 * user name is part of the sip_msg struct received -  that is allocated in
45 51
 	 * process private mem - no sync problems; also there is all the time some
46 52
 	 * extra characters after the user name - at least the EOH */
... ...
@@ -57,26 +63,27 @@ int get_user_script( db_con_t *db_hdl, str *user, str *script)
57 63
 	user->s[user->len] = tmp;
58 64
 
59 65
 	if (res->n==0) {
60
-		LOG(L_ERR,"ERROR:get_user_script: user <%.*s> not found in databse\n",
61
-			user->len, user->s);
62
-		goto error;
63
-	}
64
-
65
-	if (res->rows[0].values[0].nul) {
66
-		DBG("DEBUG:get_user_script: user <%.*s> has a NULL script\n",
67
-			user->len, user->s);
66
+		DBG("DEBUG:get_user_script: user <%.*s> not found in db -> probably "
67
+			"he has no script\n",user->len, user->s);
68 68
 		script->s = 0;
69 69
 		script->len = 0;
70 70
 	} else {
71
-		DBG("DEBUG:get_user_script: we got the script len=%d\n",
72
-			res->rows[0].values[0].val.blob_val.len);
73
-		script->len = res->rows[0].values[0].val.blob_val.len;
74
-		script->s = shm_malloc( script->len );
75
-		if (!script->s) {
76
-			LOG(L_ERR,"ERROR:cpl-c:get_user_script: no more free memory\n");
77
-			goto error;
71
+		if (res->rows[0].values[0].nul) {
72
+			DBG("DEBUG:get_user_script: user <%.*s> has a NULL script\n",
73
+				user->len, user->s);
74
+			script->s = 0;
75
+			script->len = 0;
76
+		} else {
77
+			DBG("DEBUG:get_user_script: we got the script len=%d\n",
78
+				res->rows[0].values[0].val.blob_val.len);
79
+			script->len = res->rows[0].values[0].val.blob_val.len;
80
+			script->s = shm_malloc( script->len );
81
+			if (!script->s) {
82
+				LOG(L_ERR,"ERROR:cpl-c:get_user_script: no more free sh_mem\n");
83
+				goto error;
84
+			}
85
+			memcpy(script->s,res->rows[0].values[0].val.blob_val.s,script->len);
78 86
 		}
79
-		memcpy(script->s,res->rows[0].values[0].val.blob_val.s,script->len);
80 87
 	}
81 88
 
82 89
 	db_free_query( db_hdl, res);
... ...
@@ -152,6 +159,7 @@ error:
152 159
 }
153 160
 
154 161
 
162
+
155 163
 /* delete from database the entiry record for a given user - if a user has no
156 164
  * script, he will be removed complitly from db; users without script are not
157 165
  * allowed into db ;-)
... ...
@@ -176,3 +184,4 @@ int rmv_from_db(db_con_t *db_con, char *usr)
176 184
 
177 185
 	return 1;
178 186
 }
187
+
... ...
@@ -136,7 +136,7 @@ static void failed_reply( struct cell* t, struct sip_msg* msg, int code,
136 136
 		if( intr->ip==DEFAULT_ACTION)
137 137
 			rez = run_default(intr);
138 138
 		else
139
-			rez = run_cpl_script(intr);
139
+			rez = cpl_run_script(intr);
140 140
 		switch ( rez ) {
141 141
 			case SCRIPT_END:
142 142
 			case SCRIPT_TO_BE_CONTINUED:
... ...
@@ -823,91 +823,91 @@ static inline int run_default( struct cpl_interpreter *intr )
823 823
 
824 824
 
825 825
 
826
-int run_cpl_script( struct cpl_interpreter *intr )
826
+int cpl_run_script( struct cpl_interpreter *intr )
827 827
 {
828 828
 	do {
829 829
 		check_overflow_by_offset( SIMPLE_NODE_SIZE(intr->ip), intr, error);
830 830
 		switch ( NODE_TYPE(intr->ip) ) {
831 831
 			case CPL_NODE:
832
-				DBG("DEBUG:run_cpl_script: processing CPL node \n");
832
+				DBG("DEBUG:cpl_run_script: processing CPL node \n");
833 833
 				intr->ip = run_cpl_node( intr );
834 834
 				break;
835 835
 			case ADDRESS_SWITCH_NODE:
836
-				DBG("DEBUG:run_cpl_script: processing address-switch node\n");
836
+				DBG("DEBUG:cpl_run_script: processing address-switch node\n");
837 837
 				intr->ip = run_address_switch( intr );
838 838
 				break;
839 839
 			case STRING_SWITCH_NODE:
840
-				DBG("DEBUG:run_cpl_script: processing string-switch node\n");
840
+				DBG("DEBUG:cpl_run_script: processing string-switch node\n");
841 841
 				intr->ip = run_string_switch( intr );
842 842
 				break;
843 843
 			case PRIORITY_SWITCH_NODE:
844
-				DBG("DEBUG:run_cpl_script: processing priority-switch node\n");
844
+				DBG("DEBUG:cpl_run_script: processing priority-switch node\n");
845 845
 				intr->ip = run_priority_switch( intr );
846 846
 				break;
847 847
 			case TIME_SWITCH_NODE:
848
-				DBG("DEBUG:run_cpl_script: processing time-switch node\n");
848
+				DBG("DEBUG:cpl_run_script: processing time-switch node\n");
849 849
 				intr->ip = run_time_switch( intr );
850 850
 				break;
851 851
 			case LANGUAGE_SWITCH_NODE:
852
-				DBG("DEBUG:run_cpl_script: processing language-switch node\n");
852
+				DBG("DEBUG:cpl_run_script: processing language-switch node\n");
853 853
 				intr->ip = run_language_switch( intr );
854 854
 				break;
855 855
 			case LOOKUP_NODE:
856
-				DBG("DEBUG:run_cpl_script: processing lookup node\n");
856
+				DBG("DEBUG:cpl_run_script: processing lookup node\n");
857 857
 				intr->ip = run_lookup( intr );
858 858
 				break;
859 859
 			case LOCATION_NODE:
860
-				DBG("DEBUG:run_cpl_script: processing location node\n");
860
+				DBG("DEBUG:cpl_run_script: processing location node\n");
861 861
 				intr->ip = run_location( intr );
862 862
 				break;
863 863
 			case REMOVE_LOCATION_NODE:
864
-				DBG("DEBUG:run_cpl_script: processing remove_location node\n");
864
+				DBG("DEBUG:cpl_run_script: processing remove_location node\n");
865 865
 				intr->ip = run_remove_location( intr );
866 866
 				break;
867 867
 			case PROXY_NODE:
868
-				DBG("DEBUG:run_cpl_script: processing proxy node\n");
868
+				DBG("DEBUG:cpl_run_script: processing proxy node\n");
869 869
 				intr->ip = run_proxy( intr );
870 870
 				break;
871 871
 			case REJECT_NODE:
872
-				DBG("DEBUG:run_cpl_script: processing reject node\n");
872
+				DBG("DEBUG:cpl_run_script: processing reject node\n");
873 873
 				intr->ip = run_reject( intr );
874 874
 				break;
875 875
 			case REDIRECT_NODE:
876
-				DBG("DEBUG:run_cpl_script: processing redirect node\n");
876
+				DBG("DEBUG:cpl_run_script: processing redirect node\n");
877 877
 				intr->ip = run_redirect( intr );
878 878
 				break;
879 879
 			case LOG_NODE:
880
-				DBG("DEBUG:run_cpl_script: processing log node\n");
880
+				DBG("DEBUG:cpl_run_script: processing log node\n");
881 881
 				intr->ip = run_log( intr );
882 882
 				break;
883 883
 			case MAIL_NODE:
884
-				DBG("DEBUG:run_cpl_script: processing mail node\n");
884
+				DBG("DEBUG:cpl_run_script: processing mail node\n");
885 885
 				intr->ip = run_mail( intr );
886 886
 				break;
887 887
 			case SUB_NODE:
888
-				DBG("DEBUG:run_cpl_script: processing sub node\n");
888
+				DBG("DEBUG:cpl_run_script: processing sub node\n");
889 889
 				intr->ip = run_sub( intr );
890 890
 				break;
891 891
 			default:
892
-				LOG(L_ERR,"ERROR:run_cpl_script: unknown type node (%d)\n",
892
+				LOG(L_ERR,"ERROR:cpl_run_script: unknown type node (%d)\n",
893 893
 					NODE_TYPE(intr->ip));
894 894
 				goto error;
895 895
 		}
896 896
 
897 897
 		if (intr->ip==CPL_RUNTIME_ERROR) {
898
-			LOG(L_ERR,"ERROR:cpl_c:run_cpl_script: runtime error\n");
898
+			LOG(L_ERR,"ERROR:cpl_c:cpl_run_script: runtime error\n");
899 899
 			return SCRIPT_RUN_ERROR;
900 900
 		} else if (intr->ip==CPL_SCRIPT_ERROR) {
901
-			LOG(L_ERR,"ERROR:cpl_c:run_cpl_script: script error\n");
901
+			LOG(L_ERR,"ERROR:cpl_c:cpl_run_script: script error\n");
902 902
 			return SCRIPT_FORMAT_ERROR;
903 903
 		} else if (intr->ip==DEFAULT_ACTION) {
904
-			DBG("DEBUG:cpl_c:run_cpl_script: running default action\n");
904
+			DBG("DEBUG:cpl_c:cpl_run_script: running default action\n");
905 905
 			return run_default(intr);
906 906
 		} else if (intr->ip==EO_SCRIPT) {
907
-			DBG("DEBUG:cpl_c:run_cpl_script: script interpretation done!\n");
907
+			DBG("DEBUG:cpl_c:cpl_run_script: script interpretation done!\n");
908 908
 			return SCRIPT_END;
909 909
 		} else if (intr->ip==CPL_TO_CONTINUE) {
910
-			DBG("DEBUG:cpl_c:run_cpl_script: done for the moment; waiting "
910
+			DBG("DEBUG:cpl_c:cpl_run_script: done for the moment; waiting "
911 911
 				"after signaling!\n");
912 912
 			return SCRIPT_TO_BE_CONTINUED;
913 913
 		}
... ...
@@ -93,7 +93,7 @@ struct cpl_interpreter* new_cpl_interpreter( struct sip_msg *msg, str *script);
93 93
 
94 94
 void free_cpl_interpreter(struct cpl_interpreter *intr);
95 95
 
96
-int run_cpl_script( struct cpl_interpreter *cpl_intr );
96
+int cpl_run_script( struct cpl_interpreter *cpl_intr );
97 97
 
98 98
 #endif
99 99