1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
#
# SPDX-FileCopyrightText: 2016 - 2026 Dimitri Staessens
# SPDX-License-Identifier: LGPL-2.1-only
#
"""
Exception hierarchy and errno translation for pyouroboros.
The ``E*`` constants mirror the Ouroboros-specific errno values in
``include/ouroboros/errno.h``.
"""
from __future__ import annotations
import errno
import os
from importlib.metadata import PackageNotFoundError, version
ENOTALLOC = 1000 # Flow is not allocated
EIPCPTYPE = 1001 # Unknown IPCP type
EIRMD = 1002 # Failed to communicate with IRMD
EIPCP = 1003 # Failed to communicate with IPCP
EIPCPSTATE = 1004 # Target in wrong state
EFLOWDOWN = 1005 # Flow is down
EFLOWPEER = 1006 # Flow is down (peer timed out)
ENAME = 1007 # Naming error
ECRYPT = 1008 # Encryption error
EAUTH = 1009 # Authentication error
EREPLAY = 1010 # OAP replay detected
_O7S_ERRNO_STR: dict[int, str] = {
ENOTALLOC: "flow is not allocated",
EIPCPTYPE: "unknown IPCP type",
EIRMD: "failed to communicate with IRMd",
EIPCP: "failed to communicate with IPCP",
EIPCPSTATE: "target in wrong state",
EFLOWDOWN: "flow is down",
EFLOWPEER: "flow peer timed out",
ENAME: "naming error",
ECRYPT: "encryption error",
EAUTH: "authentication error",
EREPLAY: "OAP replay detected",
}
class OuroborosError(Exception):
"""
Base class for all pyouroboros exceptions.
"""
class IrmError(OuroborosError):
"""
Base class for IRM (control plane) errors.
"""
class IpcpCreateError(IrmError):
"""
Raised when IPCP creation fails.
"""
class IpcpBootstrapError(IrmError):
"""
Raised when IPCP bootstrapping fails.
"""
class IpcpEnrollError(IrmError):
"""
Raised when IPCP enrollment fails.
"""
class IpcpConnectError(IrmError):
"""
Raised when IPCP connection or disconnection fails.
"""
class IpcpTypeError(IrmError, ValueError):
"""
Raised when an unknown IPCP type is encountered.
"""
class IpcpStateError(IrmError):
"""
Raised when an IPCP/IRMd is in the wrong state for the request.
"""
class IrmdError(IrmError):
"""
Raised when the IRM daemon is unreachable.
"""
class IpcpdError(IrmError):
"""
Raised when the IPCP daemon is unreachable.
"""
class BindError(IrmError):
"""
Raised when binding a program/process/IPCP to a name fails.
"""
class NameNotFoundError(IrmError):
"""
Raised when a name lookup or unregister target is missing.
"""
class NameExistsError(IrmError):
"""
Raised when a name already exists.
"""
class InvalidNameError(IrmError, ValueError):
"""
Raised when a name is malformed or otherwise invalid.
"""
class FlowError(OuroborosError):
"""
Base class for flow-plane errors.
"""
class FlowAlreadyAllocatedError(FlowError):
"""
Raised when allocating on a Flow that already holds an fd.
"""
class FlowNotAllocatedError(FlowError):
"""
Raised when operating on a Flow that has no fd.
"""
class FlowDownError(FlowError, ConnectionError):
"""
Raised when the flow has gone down.
"""
class FlowPeerError(FlowDownError):
"""
Raised when the flow's peer has timed out.
"""
class FlowPermissionError(FlowError, PermissionError):
"""
Raised when an operation is not permitted on the flow.
"""
class FlowTimeout(FlowError, TimeoutError):
"""
Raised when a flow operation times out.
"""
class FlowCryptError(FlowError):
"""
Raised on flow encryption errors.
"""
class FlowAuthError(FlowError):
"""
Raised on flow authentication errors.
"""
class FlowReplayError(FlowError):
"""
Raised when OAP replay is detected.
"""
class FlowEventError(FlowError):
"""
Raised on errors from the flow event subsystem.
"""
class FlowDeallocWarning(Warning):
"""
Warning issued when a deallocation reports a non-fatal problem.
"""
_ERRNO_MAP: dict[int, type[BaseException]] = {
errno.ETIMEDOUT: TimeoutError,
errno.EAGAIN: BlockingIOError,
errno.EWOULDBLOCK: BlockingIOError,
errno.ENOMEM: MemoryError,
errno.EACCES: PermissionError,
errno.ENOTCONN: ConnectionError,
errno.ECONNRESET: ConnectionResetError,
ENOTALLOC: FlowNotAllocatedError,
EIPCPTYPE: IpcpTypeError,
EIRMD: IrmdError,
EIPCP: IpcpdError,
EIPCPSTATE: IpcpStateError,
EFLOWDOWN: FlowDownError,
EFLOWPEER: FlowPeerError,
ENAME: NameNotFoundError,
ECRYPT: FlowCryptError,
EAUTH: FlowAuthError,
EREPLAY: FlowReplayError,
}
def _strerror(err: int) -> str:
if err >= 1000:
return _O7S_ERRNO_STR.get(err, f"ouroboros errno {err}")
return os.strerror(err)
def raise_errno(rc: int, default: type[OuroborosError] = FlowError) -> int:
"""
Translate a non-negative-or-negative-errno return code.
``_ERRNO_MAP`` holds errnos in their canonical positive form.
:param rc: The return code to translate; a negative
value is a negated errno.
:param default: Exception class to raise when the errno
has no mapping; a mapped errno overrides
it.
:return: *rc* unchanged when it is non-negative.
:raises OuroborosError: (or the builtin mapped for ``-rc``) when
*rc* is negative.
"""
if rc >= 0:
return rc
err = -rc
exc_cls = _ERRNO_MAP.get(err, default)
raise exc_cls(_strerror(err))
def check_ouroboros_version(o7s_major: int, o7s_minor: int) -> None:
"""
Verify that the installed pyouroboros matches the linked library.
Compares the linked library version against the installed
pyouroboros distribution metadata. Silently returns when running
from a source tree without distribution metadata.
:param o7s_major: Major version of the linked library, from
``OUROBOROS_VERSION_MAJOR`` in the CFFI
module.
:param o7s_minor: Minor version of the linked library, from
``OUROBOROS_VERSION_MINOR`` in the CFFI
module.
:raises RuntimeError: If the library and pyouroboros ``major.minor``
differ.
"""
try:
pyo7s_parts = version("PyOuroboros").split(".")
except PackageNotFoundError:
return
if o7s_major != int(pyo7s_parts[0]) or \
o7s_minor != int(pyo7s_parts[1]):
raise RuntimeError(
f"Ouroboros version mismatch: library is "
f"{o7s_major}.{o7s_minor}, pyouroboros is "
f"{pyo7s_parts[0]}.{pyo7s_parts[1]}"
)
|