1 /*
2  * ngtcp2
3  *
4  * Copyright (c) 2017 ngtcp2 contributors
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "ngtcp2_err.h"
26 
ngtcp2_strerror(int liberr)27 const char *ngtcp2_strerror(int liberr) {
28   switch (liberr) {
29   case 0:
30     return "NO_ERROR";
31   case NGTCP2_ERR_INVALID_ARGUMENT:
32     return "ERR_INVALID_ARGUMENT";
33   case NGTCP2_ERR_NOBUF:
34     return "ERR_NOBUF";
35   case NGTCP2_ERR_PROTO:
36     return "ERR_PROTO";
37   case NGTCP2_ERR_INVALID_STATE:
38     return "ERR_INVALID_STATE";
39   case NGTCP2_ERR_ACK_FRAME:
40     return "ERR_ACK_FRAME";
41   case NGTCP2_ERR_STREAM_ID_BLOCKED:
42     return "ERR_STREAM_ID_BLOCKED";
43   case NGTCP2_ERR_STREAM_IN_USE:
44     return "ERR_STREAM_IN_USE";
45   case NGTCP2_ERR_STREAM_DATA_BLOCKED:
46     return "ERR_STREAM_DATA_BLOCKED";
47   case NGTCP2_ERR_FLOW_CONTROL:
48     return "ERR_FLOW_CONTROL";
49   case NGTCP2_ERR_CONNECTION_ID_LIMIT:
50     return "ERR_CONNECTION_ID_LIMIT";
51   case NGTCP2_ERR_STREAM_LIMIT:
52     return "ERR_STREAM_LIMIT";
53   case NGTCP2_ERR_FINAL_SIZE:
54     return "ERR_FINAL_SIZE";
55   case NGTCP2_ERR_CRYPTO:
56     return "ERR_CRYPTO";
57   case NGTCP2_ERR_PKT_NUM_EXHAUSTED:
58     return "ERR_PKT_NUM_EXHAUSTED";
59   case NGTCP2_ERR_NOMEM:
60     return "ERR_NOMEM";
61   case NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM:
62     return "ERR_REQUIRED_TRANSPORT_PARAM";
63   case NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM:
64     return "ERR_MALFORMED_TRANSPORT_PARAM";
65   case NGTCP2_ERR_FRAME_ENCODING:
66     return "ERR_FRAME_ENCODING";
67   case NGTCP2_ERR_DECRYPT:
68     return "ERR_DECRYPT";
69   case NGTCP2_ERR_STREAM_SHUT_WR:
70     return "ERR_STREAM_SHUT_WR";
71   case NGTCP2_ERR_STREAM_NOT_FOUND:
72     return "ERR_STREAM_NOT_FOUND";
73   case NGTCP2_ERR_STREAM_STATE:
74     return "ERR_STREAM_STATE";
75   case NGTCP2_ERR_RECV_VERSION_NEGOTIATION:
76     return "ERR_RECV_VERSION_NEGOTIATION";
77   case NGTCP2_ERR_CLOSING:
78     return "ERR_CLOSING";
79   case NGTCP2_ERR_DRAINING:
80     return "ERR_DRAINING";
81   case NGTCP2_ERR_TRANSPORT_PARAM:
82     return "ERR_TRANSPORT_PARAM";
83   case NGTCP2_ERR_DISCARD_PKT:
84     return "ERR_DISCARD_PKT";
85   case NGTCP2_ERR_CONN_ID_BLOCKED:
86     return "ERR_CONN_ID_BLOCKED";
87   case NGTCP2_ERR_CALLBACK_FAILURE:
88     return "ERR_CALLBACK_FAILURE";
89   case NGTCP2_ERR_INTERNAL:
90     return "ERR_INTERNAL";
91   case NGTCP2_ERR_CRYPTO_BUFFER_EXCEEDED:
92     return "ERR_CRYPTO_BUFFER_EXCEEDED";
93   case NGTCP2_ERR_WRITE_MORE:
94     return "ERR_WRITE_MORE";
95   case NGTCP2_ERR_RETRY:
96     return "ERR_RETRY";
97   case NGTCP2_ERR_DROP_CONN:
98     return "ERR_DROP_CONN";
99   case NGTCP2_ERR_AEAD_LIMIT_REACHED:
100     return "ERR_AEAD_LIMIT_REACHED";
101   case NGTCP2_ERR_NO_VIABLE_PATH:
102     return "ERR_NO_VIABLE_PATH";
103   case NGTCP2_ERR_VERSION_NEGOTIATION:
104     return "ERR_VERSION_NEGOTIATION";
105   default:
106     return "(unknown)";
107   }
108 }
109 
ngtcp2_err_is_fatal(int liberr)110 int ngtcp2_err_is_fatal(int liberr) { return liberr < NGTCP2_ERR_FATAL; }
111 
ngtcp2_err_infer_quic_transport_error_code(int liberr)112 uint64_t ngtcp2_err_infer_quic_transport_error_code(int liberr) {
113   switch (liberr) {
114   case 0:
115     return NGTCP2_NO_ERROR;
116   case NGTCP2_ERR_ACK_FRAME:
117   case NGTCP2_ERR_FRAME_ENCODING:
118     return NGTCP2_FRAME_ENCODING_ERROR;
119   case NGTCP2_ERR_FLOW_CONTROL:
120     return NGTCP2_FLOW_CONTROL_ERROR;
121   case NGTCP2_ERR_CONNECTION_ID_LIMIT:
122     return NGTCP2_CONNECTION_ID_LIMIT_ERROR;
123   case NGTCP2_ERR_STREAM_LIMIT:
124     return NGTCP2_STREAM_LIMIT_ERROR;
125   case NGTCP2_ERR_FINAL_SIZE:
126     return NGTCP2_FINAL_SIZE_ERROR;
127   case NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM:
128   case NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM:
129   case NGTCP2_ERR_TRANSPORT_PARAM:
130     return NGTCP2_TRANSPORT_PARAMETER_ERROR;
131   case NGTCP2_ERR_INVALID_ARGUMENT:
132     return NGTCP2_INTERNAL_ERROR;
133   case NGTCP2_ERR_STREAM_STATE:
134     return NGTCP2_STREAM_STATE_ERROR;
135   case NGTCP2_ERR_CRYPTO_BUFFER_EXCEEDED:
136     return NGTCP2_CRYPTO_BUFFER_EXCEEDED;
137   case NGTCP2_ERR_AEAD_LIMIT_REACHED:
138     return NGTCP2_AEAD_LIMIT_REACHED;
139   case NGTCP2_ERR_NO_VIABLE_PATH:
140     return NGTCP2_NO_VIABLE_PATH;
141   default:
142     return NGTCP2_PROTOCOL_VIOLATION;
143   }
144 }
145