blob: df4bdcb742dc4be0c43c3ddd48f31e3302912a09 [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 <pjnath/errno.h>
21#include <pjnath/stun_msg.h>
22#include <pj/assert.h>
23#include <pj/log.h>
24#include <pj/string.h>
25
26
27
28/* PJNATH's own error codes/messages
29 * MUST KEEP THIS ARRAY SORTED!!
30 * Message must be limited to 64 chars!
31 */
32#if defined(PJ_HAS_ERROR_STRING) && PJ_HAS_ERROR_STRING!=0
33static const struct
34{
35 int code;
36 const char *msg;
37} err_str[] =
38{
39 /* STUN related error codes */
40 PJ_BUILD_ERR( PJNATH_EINSTUNMSG, "Invalid STUN message"),
41 PJ_BUILD_ERR( PJNATH_EINSTUNMSGLEN, "Invalid STUN message length"),
42 PJ_BUILD_ERR( PJNATH_EINSTUNMSGTYPE, "Invalid or unexpected STUN message type"),
43 PJ_BUILD_ERR( PJNATH_ESTUNTIMEDOUT, "STUN transaction has timed out"),
44
45 PJ_BUILD_ERR( PJNATH_ESTUNTOOMANYATTR, "Too many STUN attributes"),
46 PJ_BUILD_ERR( PJNATH_ESTUNINATTRLEN, "Invalid STUN attribute length"),
47 PJ_BUILD_ERR( PJNATH_ESTUNDUPATTR, "Found duplicate STUN attribute"),
48
49 PJ_BUILD_ERR( PJNATH_ESTUNFINGERPRINT, "STUN FINGERPRINT verification failed"),
50 PJ_BUILD_ERR( PJNATH_ESTUNMSGINTPOS, "Invalid STUN attribute after MESSAGE-INTEGRITY"),
51 PJ_BUILD_ERR( PJNATH_ESTUNFINGERPOS, "Invalid STUN attribute after FINGERPRINT"),
52
53 PJ_BUILD_ERR( PJNATH_ESTUNNOMAPPEDADDR, "STUN (XOR-)MAPPED-ADDRESS attribute not found"),
54 PJ_BUILD_ERR( PJNATH_ESTUNIPV6NOTSUPP, "STUN IPv6 attribute not supported"),
55 PJ_BUILD_ERR( PJNATH_EINVAF, "Invalid STUN address family value"),
56 PJ_BUILD_ERR( PJNATH_ESTUNINSERVER, "Invalid STUN server or server not configured"),
57
58 PJ_BUILD_ERR( PJNATH_ESTUNDESTROYED, "STUN object has been destoyed"),
59
60 /* ICE related errors */
61 PJ_BUILD_ERR( PJNATH_ENOICE, "ICE session not available"),
62 PJ_BUILD_ERR( PJNATH_EICEINPROGRESS, "ICE check is in progress"),
63 PJ_BUILD_ERR( PJNATH_EICEFAILED, "All ICE checklists failed"),
64 PJ_BUILD_ERR( PJNATH_EICEMISMATCH, "Default target doesn't match any ICE candidates"),
65 PJ_BUILD_ERR( PJNATH_EICEINCOMPID, "Invalid ICE component ID"),
66 PJ_BUILD_ERR( PJNATH_EICEINCANDID, "Invalid ICE candidate ID"),
67 PJ_BUILD_ERR( PJNATH_EICEINSRCADDR, "Source address mismatch"),
68 PJ_BUILD_ERR( PJNATH_EICEMISSINGSDP, "Missing ICE SDP attribute"),
69 PJ_BUILD_ERR( PJNATH_EICEINCANDSDP, "Invalid SDP \"candidate\" attribute"),
70 PJ_BUILD_ERR( PJNATH_EICENOHOSTCAND, "No host candidate associated with srflx"),
71 PJ_BUILD_ERR( PJNATH_EICENOMTIMEOUT, "Controlled agent timed out waiting for nomination"),
72
73 /* TURN related errors */
74 PJ_BUILD_ERR( PJNATH_ETURNINTP, "Invalid/unsupported transport"),
75
76};
77#endif /* PJ_HAS_ERROR_STRING */
78
79
80/*
81 * pjnath_strerror()
82 */
83static pj_str_t pjnath_strerror(pj_status_t statcode,
84 char *buf, pj_size_t bufsize )
85{
86 pj_str_t errstr;
87
88#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0)
89
90 if (statcode >= PJNATH_ERRNO_START &&
91 statcode < PJNATH_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
92 {
93 /* Find the error in the table.
94 * Use binary search!
95 */
96 int first = 0;
97 int n = PJ_ARRAY_SIZE(err_str);
98
99 while (n > 0) {
100 int half = n/2;
101 int mid = first + half;
102
103 if (err_str[mid].code < statcode) {
104 first = mid+1;
105 n -= (half+1);
106 } else if (err_str[mid].code > statcode) {
107 n = half;
108 } else {
109 first = mid;
110 break;
111 }
112 }
113
114
115 if (PJ_ARRAY_SIZE(err_str) && err_str[first].code == statcode) {
116 pj_str_t msg;
117
118 msg.ptr = (char*)err_str[first].msg;
119 msg.slen = pj_ansi_strlen(err_str[first].msg);
120
121 errstr.ptr = buf;
122 pj_strncpy_with_null(&errstr, &msg, bufsize);
123 return errstr;
124
125 }
126 }
127
128#endif /* PJ_HAS_ERROR_STRING */
129
130
131 /* Error not found. */
132 errstr.ptr = buf;
133 errstr.slen = pj_ansi_snprintf(buf, bufsize,
134 "Unknown pjnath error %d",
135 statcode);
136 if (errstr.slen < 0) errstr.slen = 0;
137 else if (errstr.slen > (int)bufsize) errstr.slen = bufsize;
138
139 return errstr;
140}
141
142
143static pj_str_t pjnath_strerror2(pj_status_t statcode,
144 char *buf, pj_size_t bufsize )
145{
146 int stun_code = statcode - PJ_STATUS_FROM_STUN_CODE(0);
147 const pj_str_t cmsg = pj_stun_get_err_reason(stun_code);
148 pj_str_t errstr;
149
150 buf[bufsize-1] = '\0';
151
152 if (cmsg.slen == 0) {
153 /* Not found */
154 errstr.ptr = buf;
155 errstr.slen = pj_ansi_snprintf(buf, bufsize,
156 "Unknown STUN err-code %d",
157 stun_code);
158 } else {
159 errstr.ptr = buf;
160 pj_strncpy(&errstr, &cmsg, bufsize);
161 if (errstr.slen < (int)bufsize)
162 buf[errstr.slen] = '\0';
163 else
164 buf[bufsize-1] = '\0';
165 }
166
167 if (errstr.slen < 0) errstr.slen = 0;
168 else if (errstr.slen > (int)bufsize) errstr.slen = bufsize;
169
170 return errstr;
171}
172
173
174PJ_DEF(pj_status_t) pjnath_init(void)
175{
176 pj_status_t status;
177
178 status = pj_register_strerror(PJNATH_ERRNO_START, 299,
179 &pjnath_strerror);
180 pj_assert(status == PJ_SUCCESS);
181
182 status = pj_register_strerror(PJ_STATUS_FROM_STUN_CODE(300),
183 699 - 300,
184 &pjnath_strerror2);
185 pj_assert(status == PJ_SUCCESS);
186
187 return PJ_SUCCESS;
188}
189
190
191#if PJNATH_ERROR_LEVEL <= PJ_LOG_MAX_LEVEL
192
193PJ_DEF(void) pjnath_perror(const char *sender, const char *title,
194 pj_status_t status)
195{
196 char errmsg[PJ_ERR_MSG_SIZE];
197
198 pj_strerror(status, errmsg, sizeof(errmsg));
199
200#if PJNATH_ERROR_LEVEL==1
201 PJ_LOG(1,(sender, "%s: %s", title, errmsg));
202#elif PJNATH_ERROR_LEVEL==2
203 PJ_LOG(2,(sender, "%s: %s", title, errmsg));
204#elif PJNATH_ERROR_LEVEL==3
205 PJ_LOG(3,(sender, "%s: %s", title, errmsg));
206#elif PJNATH_ERROR_LEVEL==4
207 PJ_LOG(4,(sender, "%s: %s", title, errmsg));
208#elif PJNATH_ERROR_LEVEL==5
209 PJ_LOG(5,(sender, "%s: %s", title, errmsg));
210#else
211# error Invalid PJNATH_ERROR_LEVEL value
212#endif
213}
214
215#endif /* PJNATH_ERROR_LEVEL <= PJ_LOG_MAX_LEVEL */
216