~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to sflphone-common/libs/pjproject/pjlib-util/src/pjlib-util/errno.c

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2011-11-25 13:24:12 UTC
  • mfrom: (4.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20111125132412-dc4qvhyosk74cd42
Tags: 1.0.1-4
Don't assume that arch:all packages will get built (closes: #649726)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: errno.c 2394 2008-12-23 17:27:53Z bennylp $ */
2
 
/* 
3
 
 * Copyright (C) 2008-2009 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
 
 *  Additional permission under GNU GPL version 3 section 7:
21
 
 *
22
 
 *  If you modify this program, or any covered work, by linking or
23
 
 *  combining it with the OpenSSL project's OpenSSL library (or a
24
 
 *  modified version of that library), containing parts covered by the
25
 
 *  terms of the OpenSSL or SSLeay licenses, Teluu Inc. (http://www.teluu.com)
26
 
 *  grants you additional permission to convey the resulting work.
27
 
 *  Corresponding Source for a non-source form of such a combination
28
 
 *  shall include the source code for the parts of OpenSSL used as well
29
 
 *  as that of the covered work.
30
 
 */
31
 
#include <pjlib-util/errno.h>
32
 
#include <pjlib-util/types.h>
33
 
#include <pj/string.h>
34
 
 
35
 
 
36
 
 
37
 
/* PJLIB_UTIL's own error codes/messages 
38
 
 * MUST KEEP THIS ARRAY SORTED!!
39
 
 * Message must be limited to 64 chars!
40
 
 */
41
 
#if defined(PJ_HAS_ERROR_STRING) && PJ_HAS_ERROR_STRING!=0
42
 
static const struct 
43
 
{
44
 
    int code;
45
 
    const char *msg;
46
 
} err_str[] = 
47
 
{
48
 
    /* STUN errors */
49
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNRESOLVE,      "Unable to resolve STUN server" ),
50
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINMSGTYPE,    "Unknown STUN message type" ),
51
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINMSGLEN,     "Invalid STUN message length" ),
52
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINATTRLEN,    "STUN attribute length error" ),
53
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINATTRTYPE,   "Invalid STUN attribute type" ),
54
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNININDEX,      "Invalid STUN server/socket index" ),
55
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOBINDRES,    "No STUN binding response in the message" ),
56
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNRECVERRATTR,  "Received STUN error attribute" ),
57
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOMAP,        "No STUN mapped address attribute" ),
58
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOTRESPOND,   "Received no response from STUN server" ),
59
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNSYMMETRIC,    "Symetric NAT detected by STUN" ),
60
 
 
61
 
    /* XML errors */
62
 
    PJ_BUILD_ERR( PJLIB_UTIL_EINXML,            "Invalid XML message" ),
63
 
 
64
 
    /* DNS errors */
65
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNSQRYTOOSMALL,   "DNS query packet buffer is too small"),
66
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNSINSIZE,        "Invalid DNS packet length"),
67
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNSINCLASS,       "Invalid DNS class"),
68
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNSINNAMEPTR,     "Invalid DNS name pointer"),
69
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNSINNSADDR,      "Invalid DNS nameserver address"),
70
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNSNONS,          "No nameserver is in DNS resolver"),
71
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNSNOWORKINGNS,   "No working DNS nameserver"),
72
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNSNOANSWERREC,   "No answer record in the DNS response"),
73
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNSINANSWER,      "Invalid DNS answer"),
74
 
 
75
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNS_FORMERR,      "DNS \"Format error\""),
76
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNS_SERVFAIL,     "DNS \"Server failure\""),
77
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNS_NXDOMAIN,     "DNS \"Name Error\""),
78
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNS_NOTIMPL,      "DNS \"Not Implemented\""),
79
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNS_REFUSED,      "DNS \"Refused\""),
80
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNS_YXDOMAIN,     "DNS \"The name exists\""),
81
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNS_YXRRSET,      "DNS \"The RRset (name, type) exists\""),
82
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNS_NXRRSET,      "DNS \"The RRset (name, type) does not exist\""),
83
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNS_NOTAUTH,      "DNS \"Not authorized\""),
84
 
    PJ_BUILD_ERR( PJLIB_UTIL_EDNS_NOTZONE,      "DNS \"The zone specified is not a zone\""),
85
 
 
86
 
    /* STUN */
87
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNTOOMANYATTR,  "Too many STUN attributes"),
88
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNUNKNOWNATTR,  "Unknown STUN attribute"),
89
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINADDRLEN,    "Invalid STUN socket address length"),
90
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNIPV6NOTSUPP,  "STUN IPv6 attribute not supported"),
91
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOTRESPONSE,  "Expecting STUN response message"),
92
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNINVALIDID,    "STUN transaction ID mismatch"),
93
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOHANDLER,    "Unable to find STUN handler for the request"),
94
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNMSGINTPOS,    "Found non-FINGERPRINT attr. after MESSAGE-INTEGRITY"),
95
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNFINGERPOS,    "Found STUN attribute after FINGERPRINT"),
96
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOUSERNAME,   "Missing STUN USERNAME attribute"),
97
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNMSGINT,       "Missing/invalid STUN MESSAGE-INTEGRITY attribute"),
98
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNDUPATTR,      "Found duplicate STUN attribute"),
99
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNOREALM,      "Missing STUN REALM attribute"),
100
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNNONCE,        "Missing/stale STUN NONCE attribute value"),
101
 
    PJ_BUILD_ERR( PJLIB_UTIL_ESTUNTSXFAILED,    "STUN transaction terminates with failure"),
102
 
};
103
 
#endif  /* PJ_HAS_ERROR_STRING */
104
 
 
105
 
 
106
 
/*
107
 
 * pjlib_util_strerror()
108
 
 */
109
 
pj_str_t pjlib_util_strerror(pj_status_t statcode, 
110
 
                             char *buf, pj_size_t bufsize )
111
 
{
112
 
    pj_str_t errstr;
113
 
 
114
 
#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0)
115
 
 
116
 
    if (statcode >= PJLIB_UTIL_ERRNO_START && 
117
 
        statcode < PJLIB_UTIL_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
118
 
    {
119
 
        /* Find the error in the table.
120
 
         * Use binary search!
121
 
         */
122
 
        int first = 0;
123
 
        int n = PJ_ARRAY_SIZE(err_str);
124
 
 
125
 
        while (n > 0) {
126
 
            int half = n/2;
127
 
            int mid = first + half;
128
 
 
129
 
            if (err_str[mid].code < statcode) {
130
 
                first = mid+1;
131
 
                n -= (half+1);
132
 
            } else if (err_str[mid].code > statcode) {
133
 
                n = half;
134
 
            } else {
135
 
                first = mid;
136
 
                break;
137
 
            }
138
 
        }
139
 
 
140
 
 
141
 
        if (PJ_ARRAY_SIZE(err_str) && err_str[first].code == statcode) {
142
 
            pj_str_t msg;
143
 
            
144
 
            msg.ptr = (char*)err_str[first].msg;
145
 
            msg.slen = pj_ansi_strlen(err_str[first].msg);
146
 
 
147
 
            errstr.ptr = buf;
148
 
            pj_strncpy_with_null(&errstr, &msg, bufsize);
149
 
            return errstr;
150
 
 
151
 
        } 
152
 
    }
153
 
 
154
 
#endif  /* PJ_HAS_ERROR_STRING */
155
 
 
156
 
 
157
 
    /* Error not found. */
158
 
    errstr.ptr = buf;
159
 
    errstr.slen = pj_ansi_snprintf(buf, bufsize, 
160
 
                                   "Unknown pjlib-util error %d",
161
 
                                   statcode);
162
 
 
163
 
    return errstr;
164
 
}
165
 
 
166
 
 
167
 
PJ_DEF(pj_status_t) pjlib_util_init(void)
168
 
{
169
 
    return pj_register_strerror(PJLIB_UTIL_ERRNO_START, 
170
 
                                PJ_ERRNO_SPACE_SIZE, 
171
 
                                &pjlib_util_strerror);
172
 
}