summaryrefslogtreecommitdiff
path: root/pjlib-util/src/pjlib-util/errno.c
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2006-02-22 22:10:42 +0000
committerBenny Prijono <bennylp@teluu.com>2006-02-22 22:10:42 +0000
commit0cdc9f5a924c0f741372b7403ea10eae0a4cec24 (patch)
tree2a032cc17094287837587728b4076b49af7a0262 /pjlib-util/src/pjlib-util/errno.c
parent37a3c53aee2fa10f0d9317322fc4eccd18283aab (diff)
Started errno framework in pjlib-util
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@217 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib-util/src/pjlib-util/errno.c')
-rw-r--r--pjlib-util/src/pjlib-util/errno.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/pjlib-util/src/pjlib-util/errno.c b/pjlib-util/src/pjlib-util/errno.c
new file mode 100644
index 00000000..5384aab2
--- /dev/null
+++ b/pjlib-util/src/pjlib-util/errno.c
@@ -0,0 +1,112 @@
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#include <pjlib-util/errno.h>
+#include <pj/string.h>
+
+
+
+/* PJLIB_UTIL's own error codes/messages
+ * MUST KEEP THIS ARRAY SORTED!!
+ * Message must be limited to 64 chars!
+ */
+static const struct
+{
+ int code;
+ const char *msg;
+} err_str[] =
+{
+ /* STUN errors */
+ { PJLIB_UTIL_ESTUNRESOLVE, "Unable to resolve STUN server" },
+ { PJLIB_UTIL_ESTUNINMSGTYPE, "Unknown STUN message type" },
+ { PJLIB_UTIL_ESTUNINMSGLEN, "Invalid STUN message length" },
+ { PJLIB_UTIL_ESTUNINATTRLEN, "STUN attribute length error" },
+ { PJLIB_UTIL_ESTUNINATTRTYPE, "Invalid STUN attribute type" },
+ { PJLIB_UTIL_ESTUNININDEX, "Invalid STUN server/socket index" },
+ { PJLIB_UTIL_ESTUNNOBINDRES, "No STUN binding response in the message" },
+ { PJLIB_UTIL_ESTUNRECVERRATTR, "Received STUN error attribute" },
+ { PJLIB_UTIL_ESTUNNOMAP, "No STUN mapped address attribute" },
+ { PJLIB_UTIL_ESTUNNOTRESPOND, "Received no response from STUN server" },
+ { PJLIB_UTIL_ESTUNSYMMETRIC, "Symetric NAT detected by STUN" },
+
+};
+
+
+
+/*
+ * pjlib_util_strerror()
+ */
+PJ_DEF(pj_str_t) pjlib_util_strerror( pj_status_t statcode,
+ char *buf, pj_size_t bufsize )
+{
+ pj_str_t errstr;
+
+ if (statcode >= PJLIB_UTIL_ERRNO_START &&
+ statcode < PJLIB_UTIL_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
+ {
+ /* Find the error in the table.
+ * Use binary search!
+ */
+ int first = 0;
+ int n = PJ_ARRAY_SIZE(err_str);
+
+ while (n > 0) {
+ int half = n/2;
+ int mid = first + half;
+
+ if (err_str[mid].code < statcode) {
+ first = mid+1;
+ n -= (half+1);
+ } else if (err_str[mid].code > statcode) {
+ n = half;
+ } else {
+ first = mid;
+ break;
+ }
+ }
+
+
+ if (PJ_ARRAY_SIZE(err_str) && err_str[first].code == statcode) {
+ pj_str_t msg;
+
+ msg.ptr = (char*)err_str[first].msg;
+ msg.slen = pj_ansi_strlen(err_str[first].msg);
+
+ errstr.ptr = buf;
+ pj_strncpy_with_null(&errstr, &msg, bufsize);
+ return errstr;
+
+ }
+ }
+
+ /* Error not found. */
+ errstr.ptr = buf;
+ errstr.slen = pj_snprintf(buf, bufsize,
+ "Unknown error %d",
+ statcode);
+
+ return errstr;
+}
+
+
+PJ_DEF(pj_status_t) pjlib_util_init(void)
+{
+ return pj_register_strerror(PJLIB_UTIL_ERRNO_START,
+ PJ_ERRNO_SPACE_SIZE,
+ &pjlib_util_strerror);
+}