blob: d837a606366cfaba84f21168dbf759f7565949c8 [file] [log] [blame]
James Kuszmaul28cc8252021-01-17 11:32:21 -08001#include <rawrtcc/code.h>
2#include <rawrtcc/utils.h>
3#include <re.h>
4#include <stdarg.h> // va_*
5
6/*
7 * Translate a rawrtc return code to a string.
8 */
9char const* rawrtc_code_to_str(enum rawrtc_code const code) {
10 switch (code) {
11 case RAWRTC_CODE_UNKNOWN_ERROR:
12 return "unknown error";
13 case RAWRTC_CODE_NOT_IMPLEMENTED:
14 return "not implemented";
15 case RAWRTC_CODE_SUCCESS:
16 return "success";
17 case RAWRTC_CODE_INITIALISE_FAIL:
18 return "failed to initialise";
19 case RAWRTC_CODE_INVALID_ARGUMENT:
20 return "invalid argument";
21 case RAWRTC_CODE_NO_MEMORY:
22 return "no memory";
23 case RAWRTC_CODE_INVALID_STATE:
24 return "invalid state";
25 case RAWRTC_CODE_UNSUPPORTED_PROTOCOL:
26 return "unsupported protocol";
27 case RAWRTC_CODE_UNSUPPORTED_ALGORITHM:
28 return "unsupported algorithm";
29 case RAWRTC_CODE_NO_VALUE:
30 return "no value";
31 case RAWRTC_CODE_NO_SOCKET:
32 return "no socket";
33 case RAWRTC_CODE_INVALID_CERTIFICATE:
34 return "invalid certificate";
35 case RAWRTC_CODE_INVALID_FINGERPRINT:
36 return "invalid fingerprint";
37 case RAWRTC_CODE_INSUFFICIENT_SPACE:
38 return "insufficient space";
39 case RAWRTC_CODE_STILL_IN_USE:
40 return "still in use";
41 case RAWRTC_CODE_INVALID_MESSAGE:
42 return "invalid message";
43 case RAWRTC_CODE_MESSAGE_TOO_LONG:
44 return "message too long";
45 case RAWRTC_CODE_TRY_AGAIN_LATER:
46 return "try again later";
47 case RAWRTC_CODE_STOP_ITERATION:
48 return "stop iteration";
49 case RAWRTC_CODE_NOT_PERMITTED:
50 return "not permitted";
51 case RAWRTC_CODE_EXTERNAL_ERROR:
52 return "external callback error-ed";
53 default:
54 return "(no error translation)";
55 }
56}
57
58/*
59 * Translate an re error to a rawrtc code.
60 * TODO: Add codes from trice_lcand_add
61 */
62enum rawrtc_code rawrtc_error_to_code(int const code) {
63 switch (code) {
64 case 0:
65 return RAWRTC_CODE_SUCCESS;
66 case EAGAIN:
67#if (EAGAIN != EWOULDBLOCK)
68 case EWOULDBLOCK:
69#endif
70 return RAWRTC_CODE_TRY_AGAIN_LATER;
71 case EAUTH:
72 return RAWRTC_CODE_INVALID_CERTIFICATE;
73 case EBADMSG:
74 return RAWRTC_CODE_INVALID_MESSAGE;
75 case EINVAL:
76 return RAWRTC_CODE_INVALID_ARGUMENT;
77 case EMSGSIZE:
78 return RAWRTC_CODE_MESSAGE_TOO_LONG;
79 case ENOMEM:
80 return RAWRTC_CODE_NO_MEMORY;
81 case EPERM:
82 return RAWRTC_CODE_NOT_PERMITTED;
83 default:
84 return RAWRTC_CODE_UNKNOWN_ERROR;
85 }
86}
87
88/*
89 * Duplicate a string.
90 * `*destinationp` will be set to a copy of `source` and must be
91 * unreferenced.
92 */
93enum rawrtc_code rawrtc_strdup(char** const destinationp, char const* const source) {
94 int err = str_dup(destinationp, source);
95 return rawrtc_error_to_code(err);
96}
97
98/*
99 * Print a formatted string to a dynamically allocated buffer.
100 * `*destinationp` must be unreferenced.
101 */
102enum rawrtc_code rawrtc_sdprintf(char** const destinationp, char* const formatter, ...) {
103 int err;
104 va_list args;
105 va_start(args, formatter);
106 err = re_vsdprintf(destinationp, formatter, args);
107 va_end(args);
108 return rawrtc_error_to_code(err);
109}