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

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.1.0/pjlib-util/src/pjlib-util/errno.c

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: errno.c 3553 2011-05-05 06:14:19Z nanang $ */
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
32
 
static 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
 
#endif  /* PJ_HAS_ERROR_STRING */
101
 
 
102
 
 
103
 
/*
104
 
 * pjlib_util_strerror()
105
 
 */
106
 
pj_str_t pjlib_util_strerror(pj_status_t statcode, 
107
 
                             char *buf, pj_size_t bufsize )
108
 
{
109
 
    pj_str_t errstr;
110
 
 
111
 
#if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0)
112
 
 
113
 
    if (statcode >= PJLIB_UTIL_ERRNO_START && 
114
 
        statcode < PJLIB_UTIL_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
115
 
    {
116
 
        /* Find the error in the table.
117
 
         * Use binary search!
118
 
         */
119
 
        int first = 0;
120
 
        int n = PJ_ARRAY_SIZE(err_str);
121
 
 
122
 
        while (n > 0) {
123
 
            int half = n/2;
124
 
            int mid = first + half;
125
 
 
126
 
            if (err_str[mid].code < statcode) {
127
 
                first = mid+1;
128
 
                n -= (half+1);
129
 
            } else if (err_str[mid].code > statcode) {
130
 
                n = half;
131
 
            } else {
132
 
                first = mid;
133
 
                break;
134
 
            }
135
 
        }
136
 
 
137
 
 
138
 
        if (PJ_ARRAY_SIZE(err_str) && err_str[first].code == statcode) {
139
 
            pj_str_t msg;
140
 
            
141
 
            msg.ptr = (char*)err_str[first].msg;
142
 
            msg.slen = pj_ansi_strlen(err_str[first].msg);
143
 
 
144
 
            errstr.ptr = buf;
145
 
            pj_strncpy_with_null(&errstr, &msg, bufsize);
146
 
            return errstr;
147
 
 
148
 
        } 
149
 
    }
150
 
 
151
 
#endif  /* PJ_HAS_ERROR_STRING */
152
 
 
153
 
 
154
 
    /* Error not found. */
155
 
    errstr.ptr = buf;
156
 
    errstr.slen = pj_ansi_snprintf(buf, bufsize, 
157
 
                                   "Unknown pjlib-util error %d",
158
 
                                   statcode);
159
 
 
160
 
    return errstr;
161
 
}
162
 
 
163
 
 
164
 
PJ_DEF(pj_status_t) pjlib_util_init(void)
165
 
{
166
 
    pj_status_t status;
167
 
    
168
 
    status = pj_register_strerror(PJLIB_UTIL_ERRNO_START, 
169
 
                                  PJ_ERRNO_SPACE_SIZE, 
170
 
                                  &pjlib_util_strerror);
171
 
    pj_assert(status == PJ_SUCCESS);
172
 
 
173
 
    return PJ_SUCCESS;
174
 
}