blob: 1949d05e666dd62f51d0d97de1784ed8a32ac5c6 [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjlib-util/errno.h>
21#include <pjlib-util/types.h>
22#include <pj/assert.h>
23#include <pj/string.h>
24
25
26
27/* PJLIB_UTIL's own error codes/messages
28 * MUST KEEP THIS ARRAY SORTED!!
29 * Message must be limited to 64 chars!
30 */
31#if defined(PJ_HAS_ERROR_STRING) && PJ_HAS_ERROR_STRING!=0
32static const struct
33{
34 int code;
35 const char *msg;
36} err_str[] =
37{
38 /* STUN errors */
39 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNRESOLVE, "Unable to resolve STUN server" ),
40 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINMSGTYPE, "Unknown STUN message type" ),
41 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINMSGLEN, "Invalid STUN message length" ),
42 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINATTRLEN, "STUN attribute length error" ),
43 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINATTRTYPE, "Invalid STUN attribute type" ),
44 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNININDEX, "Invalid STUN server/socket index" ),
45 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOBINDRES, "No STUN binding response in the message" ),
46 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNRECVERRATTR, "Received STUN error attribute" ),
47 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOMAP, "No STUN mapped address attribute" ),
48 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOTRESPOND, "Received no response from STUN server" ),
49 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNSYMMETRIC, "Symetric NAT detected by STUN" ),
50
51 /* XML errors */
52 PJ_BUILD_ERR( PJLIB_UTIL_EINXML, "Invalid XML message" ),
53
54 /* DNS errors */
55 PJ_BUILD_ERR( PJLIB_UTIL_EDNSQRYTOOSMALL, "DNS query packet buffer is too small"),
56 PJ_BUILD_ERR( PJLIB_UTIL_EDNSINSIZE, "Invalid DNS packet length"),
57 PJ_BUILD_ERR( PJLIB_UTIL_EDNSINCLASS, "Invalid DNS class"),
58 PJ_BUILD_ERR( PJLIB_UTIL_EDNSINNAMEPTR, "Invalid DNS name pointer"),
59 PJ_BUILD_ERR( PJLIB_UTIL_EDNSINNSADDR, "Invalid DNS nameserver address"),
60 PJ_BUILD_ERR( PJLIB_UTIL_EDNSNONS, "No nameserver is in DNS resolver"),
61 PJ_BUILD_ERR( PJLIB_UTIL_EDNSNOWORKINGNS, "No working DNS nameserver"),
62 PJ_BUILD_ERR( PJLIB_UTIL_EDNSNOANSWERREC, "No answer record in the DNS response"),
63 PJ_BUILD_ERR( PJLIB_UTIL_EDNSINANSWER, "Invalid DNS answer"),
64
65 PJ_BUILD_ERR( PJLIB_UTIL_EDNS_FORMERR, "DNS \"Format error\""),
66 PJ_BUILD_ERR( PJLIB_UTIL_EDNS_SERVFAIL, "DNS \"Server failure\""),
67 PJ_BUILD_ERR( PJLIB_UTIL_EDNS_NXDOMAIN, "DNS \"Name Error\""),
68 PJ_BUILD_ERR( PJLIB_UTIL_EDNS_NOTIMPL, "DNS \"Not Implemented\""),
69 PJ_BUILD_ERR( PJLIB_UTIL_EDNS_REFUSED, "DNS \"Refused\""),
70 PJ_BUILD_ERR( PJLIB_UTIL_EDNS_YXDOMAIN, "DNS \"The name exists\""),
71 PJ_BUILD_ERR( PJLIB_UTIL_EDNS_YXRRSET, "DNS \"The RRset (name, type) exists\""),
72 PJ_BUILD_ERR( PJLIB_UTIL_EDNS_NXRRSET, "DNS \"The RRset (name, type) does not exist\""),
73 PJ_BUILD_ERR( PJLIB_UTIL_EDNS_NOTAUTH, "DNS \"Not authorized\""),
74 PJ_BUILD_ERR( PJLIB_UTIL_EDNS_NOTZONE, "DNS \"The zone specified is not a zone\""),
75
76 /* STUN */
77 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNTOOMANYATTR, "Too many STUN attributes"),
78 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNUNKNOWNATTR, "Unknown STUN attribute"),
79 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINADDRLEN, "Invalid STUN socket address length"),
80 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNIPV6NOTSUPP, "STUN IPv6 attribute not supported"),
81 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOTRESPONSE, "Expecting STUN response message"),
82 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINVALIDID, "STUN transaction ID mismatch"),
83 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOHANDLER, "Unable to find STUN handler for the request"),
84 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNMSGINTPOS, "Found non-FINGERPRINT attr. after MESSAGE-INTEGRITY"),
85 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNFINGERPOS, "Found STUN attribute after FINGERPRINT"),
86 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOUSERNAME, "Missing STUN USERNAME attribute"),
87 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNMSGINT, "Missing/invalid STUN MESSAGE-INTEGRITY attribute"),
88 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNDUPATTR, "Found duplicate STUN attribute"),
89 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOREALM, "Missing STUN REALM attribute"),
90 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNONCE, "Missing/stale STUN NONCE attribute value"),
91 PJ_BUILD_ERR( PJLIB_UTIL_ESTUNTSXFAILED, "STUN transaction terminates with failure"),
92
93 /* HTTP Client */
94 PJ_BUILD_ERR( PJLIB_UTIL_EHTTPINURL, "Invalid URL format"),
95 PJ_BUILD_ERR( PJLIB_UTIL_EHTTPINPORT, "Invalid URL port number"),
96 PJ_BUILD_ERR( PJLIB_UTIL_EHTTPINCHDR, "Incomplete response header received"),
97 PJ_BUILD_ERR( PJLIB_UTIL_EHTTPINSBUF, "Insufficient buffer"),
98 PJ_BUILD_ERR( PJLIB_UTIL_EHTTPLOST, "Connection lost"),
99
100 /* CLI */
101 PJ_BUILD_ERR( PJ_CLI_EEXIT, "Exit current session"),
102 PJ_BUILD_ERR( PJ_CLI_EMISSINGARG, "Missing argument"),
103 PJ_BUILD_ERR( PJ_CLI_ETOOMANYARGS, "Too many arguments"),
104 PJ_BUILD_ERR( PJ_CLI_EINVARG, "Invalid argument"),
105 PJ_BUILD_ERR( PJ_CLI_EBADNAME, "Command name already exists"),
106 PJ_BUILD_ERR( PJ_CLI_EBADID, "Command id already exists"),
107 PJ_BUILD_ERR( PJ_CLI_EBADXML, "Invalid XML format"),
108 PJ_BUILD_ERR( PJ_CLI_ETELNETLOST, "Connection lost"),
109};
110#endif /* PJ_HAS_ERROR_STRING */
111
112
113/*
114 * pjlib_util_strerror()
115 */
116pj_str_t pjlib_util_strerror(pj_status_t statcode,
117 char *buf, pj_size_t bufsize )
118{
119 pj_str_t errstr;
120
121#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0)
122
123 if (statcode >= PJLIB_UTIL_ERRNO_START &&
124 statcode < PJLIB_UTIL_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
125 {
126 /* Find the error in the table.
127 * Use binary search!
128 */
129 int first = 0;
130 int n = PJ_ARRAY_SIZE(err_str);
131
132 while (n > 0) {
133 int half = n/2;
134 int mid = first + half;
135
136 if (err_str[mid].code < statcode) {
137 first = mid+1;
138 n -= (half+1);
139 } else if (err_str[mid].code > statcode) {
140 n = half;
141 } else {
142 first = mid;
143 break;
144 }
145 }
146
147
148 if (PJ_ARRAY_SIZE(err_str) && err_str[first].code == statcode) {
149 pj_str_t msg;
150
151 msg.ptr = (char*)err_str[first].msg;
152 msg.slen = pj_ansi_strlen(err_str[first].msg);
153
154 errstr.ptr = buf;
155 pj_strncpy_with_null(&errstr, &msg, bufsize);
156 return errstr;
157
158 }
159 }
160
161#endif /* PJ_HAS_ERROR_STRING */
162
163
164 /* Error not found. */
165 errstr.ptr = buf;
166 errstr.slen = pj_ansi_snprintf(buf, bufsize,
167 "Unknown pjlib-util error %d",
168 statcode);
169
170 return errstr;
171}
172
173
174PJ_DEF(pj_status_t) pjlib_util_init(void)
175{
176 pj_status_t status;
177
178 status = pj_register_strerror(PJLIB_UTIL_ERRNO_START,
179 PJ_ERRNO_SPACE_SIZE,
180 &pjlib_util_strerror);
181 pj_assert(status == PJ_SUCCESS);
182
183 return PJ_SUCCESS;
184}