[svn-commits] rizzo: branch rizzo/astobj2 r47714 - /team/rizzo/astobj2/channels/chan_sip.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Wed Nov 15 17:44:02 MST 2006


Author: rizzo
Date: Wed Nov 15 18:44:01 2006
New Revision: 47714

URL: http://svn.digium.com/view/asterisk?view=rev&rev=47714
Log:
more cleanup of config parsing code.
Introduce a 3 new macros (to set integer > min and a default)
and set flags.
In the process, spot some inconsistencies (as expected, this
was the main motivation of this work) and
mark them XXX, and correct the obvious ones (not checking
atoi() return in some places).

We still need to introduce a function to match multiple keywords
so we can replace things like

	if (!strcasecmp("foo", x) || !strcasecmp("bar", x))

and perhaps some to match common mapping from strings to
integer values.


Modified:
    team/rizzo/astobj2/channels/chan_sip.c

Modified: team/rizzo/astobj2/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/channels/chan_sip.c?view=diff&rev=47714&r1=47713&r2=47714
==============================================================================
--- team/rizzo/astobj2/channels/chan_sip.c (original)
+++ team/rizzo/astobj2/channels/chan_sip.c Wed Nov 15 18:44:01 2006
@@ -15654,11 +15654,35 @@
 #define M_START(var, val) \
         char *__s = var; char *__val = val;
 #define M_END(x)   x;
+
+/*! \brief generic block */
 #define M_F(tag, f)                     if (!strcasecmp((__s), tag)) { f; } else
+/* set a boolean */
 #define M_BOOL(tag, dst)        M_F(tag, (dst) = ast_true(__val) )
+/* set a given flag if true */
+#define	M_FLAG(tag, dst, flag)	M_F(tag, { if (ast_true(__val)) ast_set_flag(&dst, flag); } )
+/* set unsigned integer */
 #define M_UINT(tag, dst)        M_F(tag, (dst) = strtoul(__val, NULL, 0) )
+/* set a static string */
 #define M_STR(tag, dst)         M_F(tag, ast_copy_string(dst, __val, sizeof(dst)))
-
+/* set integer if >= min, otherwise use dflt */
+#define M_INT_GE(tag, dst, min, dflt)	M_F(tag, dst = __def_int(__s, __val, v->lineno, min, dflt))
+/* set integer if >= 0, otherwise use dflt */
+#define M_INT_GE0(tag, dst, dflt)	M_INT_GE(tag, dst, 0, dflt)
+
+static int __def_int(const char *var, const char *val, int line, int min, int dflt)
+{
+	int res;
+	if (sscanf(val, "%d", &res) != 1 || res < min) {
+		ast_log(LOG_WARNING,
+		    "Invalid '%s' '%s' at line %d. Using default %d.\n",
+			var, val, line, dflt);
+		return dflt;
+	}
+	return res;
+}
+
+	
 /*!
   \brief Handle flag-type options common to configuration of devices - users and peers
   \param flags array of two struct ast_flags
@@ -16014,10 +16038,7 @@
 		M_STR("musiconhold", user->mohinterpret)
 		M_STR("mohsuggest", user->mohsuggest)
 		M_STR("accountcode", user->accountcode)
-		M_F("call-limit", {
-			user->call_limit = atoi(v->value);
-			if (user->call_limit < 0)
-				user->call_limit = 0;} )
+		M_INT_GE0("call-limit", user->call_limit, 0)
 		M_F("amaflags", {
 			format = ast_cdr_amaflags2int(v->value);
 			if (format < 0) {
@@ -16032,10 +16053,7 @@
 			user->callingpres = ast_parse_caller_presentation(v->value);
 			if (user->callingpres == -1)
 				user->callingpres = atoi(v->value);} )
-		M_F("maxcallbitrate", {
-			user->maxcallbitrate = atoi(v->value);
-			if (user->maxcallbitrate < 0)
-				user->maxcallbitrate = default_maxcallbitrate;} )
+		M_INT_GE0("maxcallbitrate", user->maxcallbitrate, default_maxcallbitrate)
  		M_F("t38pt_udptl", ast_set2_flag(&user->flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_UDPTL))
 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
 		M_F("t38pt_rtp", ast_set2_flag(&user->flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_RTP))
@@ -16255,10 +16273,7 @@
 		M_STR("language", peer->language)
 		M_STR("regexten", peer->regexten)
 		M_STR("callbackextension", callback)
-		M_F("call-limit", {
-			peer->call_limit = atoi(v->value);
-			if (peer->call_limit < 0)
-				peer->call_limit = 0; } )
+		M_INT_GE0("call-limit", peer->call_limit, 0)
 		M_F("amaflags", {
 			format = ast_cdr_amaflags2int(v->value);
 			if (format < 0) {
@@ -16280,21 +16295,9 @@
 		M_F("allow", ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 1);)
 		M_F("disallow", ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 0);)
 		M_BOOL("autoframing", peer->autoframing)
-		M_F("rtptimeout", {
-			if ((sscanf(v->value, "%d", &peer->rtptimeout) != 1) || (peer->rtptimeout < 0)) {
-				ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d.  Using default.\n", v->value, v->lineno);
-				peer->rtptimeout = global_rtptimeout;
-			} } )
-		M_F("rtpholdtimeout", {
-			if ((sscanf(v->value, "%d", &peer->rtpholdtimeout) != 1) || (peer->rtpholdtimeout < 0)) {
-				ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d.  Using default.\n", v->value, v->lineno);
-				peer->rtpholdtimeout = global_rtpholdtimeout;
-			} } )
-		M_F("rtpkeepalive", {
-			if ((sscanf(v->value, "%d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
-				ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d.  Using default.\n", v->value, v->lineno);
-				peer->rtpkeepalive = global_rtpkeepalive;
-			} } )
+		M_INT_GE0("rtptimeout", peer->rtptimeout, global_rtptimeout)
+		M_INT_GE0("rtpholdtimeout", peer->rtpholdtimeout, global_rtpholdtimeout)
+		M_INT_GE0("rtpkeepalive", peer->rtpkeepalive, global_rtpkeepalive)
 		M_F("setvar", peer->chanvars = add_var(v->value, peer->chanvars);)
 		M_F("qualify", {
 			if (!strcasecmp(v->value, "no")) {
@@ -16305,12 +16308,10 @@
 				ast_log(LOG_WARNING, "Qualification of peer '%s' should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", peer->name, v->lineno);
 				peer->maxms = 0;
 			} } )
-		M_F("maxcallbitrate", {
-			peer->maxcallbitrate = atoi(v->value);
-			if (peer->maxcallbitrate < 0)
-				peer->maxcallbitrate = default_maxcallbitrate; } )
+		M_INT_GE0("maxcallbitrate", peer->maxcallbitrate, default_maxcallbitrate)
 		M_F("t38pt_udptl", ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_UDPTL);)
 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
+		/* XXX this is different from the handling in the global section */
 		M_F("t38pt_rtp", ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_RTP);)
 		M_F("t38pt_tcp", ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_T38SUPPORT_TCP);)
 #endif
@@ -16495,36 +16496,19 @@
 			ast_set2_flag(&global_flags[1], i || ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR); } )
 		M_F("usereqphone", ast_set2_flag(&global_flags[0], ast_true(v->value), SIP_USEREQPHONE);)
 		M_BOOL("relaxdtmf", global_relaxdtmf)
-		M_F("checkmwi", {
-			if ((sscanf(v->value, "%d", &global_mwitime) != 1) || (global_mwitime < 0)) {
-				ast_log(LOG_WARNING, "'%s' is not a valid MWI time setting at line %d.  Using default (10).\n", v->value, v->lineno);
-				global_mwitime = DEFAULT_MWITIME;
-			} } )
+		M_INT_GE0("checkmwi", global_mwitime, DEFAULT_MWITIME)
 		M_STR("vmexten", default_vmexten)
-		M_F("rtptimeout", {
-			if ((sscanf(v->value, "%d", &global_rtptimeout) != 1) || (global_rtptimeout < 0)) {
-				ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d.  Using default.\n", v->value, v->lineno);
-				global_rtptimeout = 0;
-			} } )
-		M_F("rtpholdtimeout", {
-			if ((sscanf(v->value, "%d", &global_rtpholdtimeout) != 1) || (global_rtpholdtimeout < 0)) {
-				ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d.  Using default.\n", v->value, v->lineno);
-				global_rtpholdtimeout = 0;
-			} } )
-		M_F("rtpkeepalive", {
-			if ((sscanf(v->value, "%d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
-				ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d.  Using default.\n", v->value, v->lineno);
-				global_rtpkeepalive = 0;
-			} } )
+		M_INT_GE0("rtptimeout", global_rtptimeout, 0)
+		M_INT_GE0("rtpholdtimeout", global_rtpholdtimeout, 0)
+		M_INT_GE0("rtpkeepalive", global_rtpkeepalive, 0)
 		M_BOOL("compactheaders", compactheaders)
 		M_STR("notifymimetype", default_notifymime)
 		M_BOOL("notifyringing", global_notifyringing)
 		M_BOOL("limitpeersonly", global_limitonpeers)
 		M_BOOL("alwaysauthreject", global_alwaysauthreject)
-		if (!strcasecmp(v->name, "mohinterpret") 
-			|| !strcasecmp(v->name, "musicclass") || !strcasecmp(v->name, "musiconhold")) {
-			ast_copy_string(default_mohinterpret, v->value, sizeof(default_mohinterpret));
-		} else
+		M_STR("mohinterpret", default_mohinterpret)
+		M_STR("musicclass", default_mohinterpret)
+		M_STR("musiconhold", default_mohinterpret)
 		M_STR("mohsuggest", default_mohsuggest)
 		M_STR("language", default_language)
 		M_F("regcontext", {
@@ -16551,29 +16535,17 @@
 		M_BOOL("match_auth_username", global_match_auth_username)
 		M_BOOL("global_srvlookup", global_srvlookup)
 		M_BOOL("pedantic", pedanticsipchecking)
-		if (!strcasecmp(v->name, "maxexpirey") || !strcasecmp(v->name, "maxexpiry")) {
-			max_expiry = atoi(v->value);
-			if (max_expiry < 1)
-				max_expiry = DEFAULT_MAX_EXPIRY;
-		} else if (!strcasecmp(v->name, "minexpirey") || !strcasecmp(v->name, "minexpiry")) {
-			min_expiry = atoi(v->value);
-			if (min_expiry < 1)
-				min_expiry = DEFAULT_MIN_EXPIRY;
-		} else if (!strcasecmp(v->name, "defaultexpiry") || !strcasecmp(v->name, "defaultexpirey")) {
-			default_expiry = atoi(v->value);
-			if (default_expiry < 1)
-				default_expiry = DEFAULT_DEFAULT_EXPIRY;
-		} else if (!strcasecmp(v->name, "sipdebug")) {	/* XXX maybe ast_set2_flags ? */
-			if (ast_true(v->value))
-				ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONFIG);
-		} else
+		M_INT_GE("maxexpirey", max_expiry, 1, DEFAULT_MAX_EXPIRY)
+		M_INT_GE("maxexpiry", max_expiry, 1, DEFAULT_MAX_EXPIRY)
+		M_INT_GE("minexpirey", min_expiry, 1, DEFAULT_MIN_EXPIRY)
+		M_INT_GE("minexpiry", min_expiry, 1, DEFAULT_MIN_EXPIRY)
+		M_INT_GE("defaultexpirey", default_expiry, 1, DEFAULT_DEFAULT_EXPIRY)
+		M_INT_GE("defaultexpiry", default_expiry, 1, DEFAULT_DEFAULT_EXPIRY)
+		M_FLAG("sipdebug", global_flags[1], SIP_PAGE2_DEBUG_CONFIG)
 		M_BOOL("dumphistory", dumphistory)
 		M_BOOL("recordhistory", recordhistory)
-		M_F("registertimeout", {
-			global_reg_timeout = atoi(v->value);
-			if (global_reg_timeout < 1)
-				global_reg_timeout = DEFAULT_REGISTRATION_TIMEOUT; } )
-		M_F("registerattempts", global_regattempts_max = atoi(v->value);)
+		M_INT_GE("registertimeout", global_reg_timeout, 1, DEFAULT_REGISTRATION_TIMEOUT)
+		M_F("registerattempts", global_regattempts_max = atoi(v->value);)	/* XXX */
 		M_F("bindaddr", {
 			if (!(hp = ast_gethostbyname(v->value, &ahp))) {
 				ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
@@ -16600,11 +16572,7 @@
 			else
 				memcpy(&externip.sin_addr, hp->h_addr, sizeof(externip.sin_addr));
 			externexpire = time(NULL); } )
-		M_F("externrefresh", {
-			if (sscanf(v->value, "%d", &externrefresh) != 1) {
-				ast_log(LOG_WARNING, "Invalid externrefresh value '%s', must be an integer >0 at line %d\n", v->value, v->lineno);
-				externrefresh = 10;
-			} } )
+		M_INT_GE("externrefresh", externrefresh, 1, 10)
 		M_F("allow", ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 1);)
 		M_F("disallow", ast_parse_allow_disallow(&default_prefs, &global_capability, v->value, 0);)
 		M_BOOL("autoframing", global_autoframing)
@@ -16652,28 +16620,13 @@
 				default_qualify = 0;
 			} } )
 		M_BOOL("callevents", global_callevents)
-		M_F("maxcallbitrate", {
-			default_maxcallbitrate = atoi(v->value);
-			if (default_maxcallbitrate < 0)
-				default_maxcallbitrate = DEFAULT_MAX_CALL_BITRATE; } )
-		M_F("t38pt_udptl", {	/* XXX maybe ast_set2_flags ? */
-			if (ast_true(v->value)) {
-				ast_set_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_UDPTL);
-			}} )
+		M_INT_GE0("maxcallbitrate", default_maxcallbitrate, DEFAULT_MAX_CALL_BITRATE)
+		M_FLAG("t38pt_udptl", global_flags[1], SIP_PAGE2_T38SUPPORT_UDPTL)
 #ifdef WHEN_WE_HAVE_T38_FOR_OTHER_TRANSPORTS
-		M_F("t38pt_rtp", {	/* XXX maybe ast_set2_flags ? */
-			if (ast_true(v->value)) {
-				ast_set_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_RTP);
-			} } )
-		M_F("t38pt_tcp", {	/* XXX maybe ast_set2_flags ? */
-			if (ast_true(v->value)) {
-				ast_set_flag(&global_flags[1], SIP_PAGE2_T38SUPPORT_TCP);
-			} } )
+		M_FLAG("t38pt_rtp", global_flags[1], SIP_PAGE2_T38SUPPORT_RTP)
+		M_FLAG("t38pt_tcp", global_flags[1], SIP_PAGE2_T38SUPPORT_TCP)
 #endif
-		M_F("rfc2833compensate", {	/* XXX maybe ast_set2_flags ? */
-			if (ast_true(v->value)) {
-				ast_set_flag(&global_flags[1], SIP_PAGE2_RFC2833_COMPENSATE);
-			} } )
+		M_FLAG("rfc2833compensate", global_flags[1], SIP_PAGE2_RFC2833_COMPENSATE)
 		M_END(/* */)
 	}
 



More information about the svn-commits mailing list