[asterisk-commits] rizzo: branch rizzo/astobj2 r48545 - /team/rizzo/astobj2/channels/chan_sip.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sun Dec 17 07:51:55 MST 2006


Author: rizzo
Date: Sun Dec 17 08:51:55 2006
New Revision: 48545

URL: http://svn.digium.com/view/asterisk?view=rev&rev=48545
Log:
more generalization of the string <-> int mapping code.


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=48545&r1=48544&r2=48545
==============================================================================
--- team/rizzo/astobj2/channels/chan_sip.c (original)
+++ team/rizzo/astobj2/channels/chan_sip.c Sun Dec 17 08:51:55 2006
@@ -760,7 +760,7 @@
 #define SIP_CAN_REINVITE_NAT	(2 << 20)	/*!< allow media reinvite when new peer is behind NAT */
 #define SIP_REINVITE_UPDATE	(4 << 20)	/*!< use UPDATE (RFC3311) when reinviting this peer */
 
-/* "insecure" settings */
+/* "insecure" settings, see insecure2str() */
 #define SIP_INSECURE		(3 << 23)	/*!< two bits used */
 #define SIP_INSECURE_PORT	(1 << 23)	/*!< don't require matching port for incoming requests */
 #define SIP_INSECURE_INVITE	(1 << 24)	/*!< don't require authentication for incoming INVITEs */
@@ -873,6 +873,15 @@
         REFER_200OK,                   /*!< Answered by transfer target */
         REFER_FAILED,                  /*!< REFER declined - go on */
         REFER_NOAUTH                   /*!< We had no auth for REFER */
+};
+
+/*!
+ * generic struct to map between strings and integers.
+ * Must be terminated by s = NULL;
+ */
+struct _map_x_s {
+	int x;
+	const char *s;
 };
 
 static const struct c_referstatusstring {
@@ -1692,6 +1701,28 @@
 	.send_text = sip_sendtext,	/* called with chan locked */
 };
 
+/*! \begin map from an integer value to a string */
+static const char *map_x_s(const struct _map_x_s *table, int x, const char *errorstring)
+{
+	const struct _map_x_s *cur;
+
+	for (cur = table; cur->s; cur++)
+		if (cur->x == x)
+			return cur->s;
+	return errorstring;
+}
+
+/*! \begin map from a string to an integer value */
+static int map_s_x(const struct _map_x_s *table, const char *s, int errorvalue)
+{
+	const struct _map_x_s *cur;
+
+	for (cur = table; cur->s; cur++)
+		if (!strcasecmp(cur->s, s))
+			return cur->x;
+	return errorvalue;
+}
+
 /**--- some list management macros. **/
  
 #define UNLINK(element, head, prev) do {	\
@@ -10176,57 +10207,39 @@
 	ast_cli(fd, crlf ? "%s\r\n" : "%s\n", ast_print_group(buf, sizeof(buf), group) );
 }
 
-/*!
- * generic struct to map between strings and integers.
- * Must be terminated by s = NULL;
- */
-struct _map_x_s {
-	int x;
-	const char *s;
-};
-
 /*! \brief mapping between dtmf flags and strings */
 static struct _map_x_s dtmfstr[] = {
 	{ SIP_DTMF_RFC2833,	"rfc2833" },
 	{ SIP_DTMF_INFO,	"info" },
 	{ SIP_DTMF_INBAND,	"inband" },
 	{ SIP_DTMF_AUTO,	"auto" },
-	{ 0,			NULL }, /* terminator */
+	{ -1,			NULL }, /* terminator */
 };
 
 /*! \brief Convert DTMF mode to printable string */
 static const char *dtmfmode2str(int mode)
 {
-	struct _map_x_s *cur;
-	for (cur = dtmfstr; cur->s; cur++)
-		if (mode == cur->x)
-			return cur->s;
-	return "<error>";
+	return map_x_s(dtmfstr, mode, "<error>");
 }
 
 /*! \brief maps a string to dtmfmode, returns -1 on error */
 static int str2dtmfmode(const char *str)
 {
-	struct _map_x_s *cur;
-	for (cur = dtmfstr; cur->s; cur++)
-		if (!strcasecmp(str, cur->s))
-			return cur->x;
-	return -1;
-}
+	return map_s_x(dtmfstr, str, -1);
+}
+
+static struct _map_x_s insecurestr[] = {
+	{ SIP_INSECURE_PORT,	"port" },
+	{ SIP_INSECURE_INVITE,	"invite" },
+	{ SIP_INSECURE_PORT | SIP_INSECURE_INVITE, "port,invite" },
+	{ 0,			"no" },
+	{ -1,			NULL }, /* terminator */
+};
 
 /*! \brief Convert Insecure setting to printable string */
 static const char *insecure2str(int mode)
 {
-	switch (mode) {
-	case SIP_INSECURE_PORT:
-		return "port";
-	case SIP_INSECURE_INVITE:
-		return "invite";
-	case SIP_INSECURE_PORT | SIP_INSECURE_INVITE:
-		return "port,invite";
-	default:
-		return "no";
-	}
+	return map_x_s(insecurestr, mode, "<error>");
 }
 
 /*! \brief Destroy disused contexts between reloads



More information about the asterisk-commits mailing list