Skip to main content

rustorb_giop/
component.rs

1use rustorb_cdr::{ByteOrder, CdrDecode, CdrEncode, CdrVersion, Decoder, Encoder};
2
3use crate::Result;
4
5/// `IOP::TaggedComponent`, with the component data left as an
6/// encapsulation (or raw octets for the few components that are not).
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct TaggedComponent {
9    /// Component tag such as [`component_tag::CODE_SETS`].
10    pub tag: u32,
11    /// Component data.
12    pub component_data: Vec<u8>,
13}
14
15impl CdrEncode for TaggedComponent {
16    fn encode(&self, enc: &mut Encoder) -> rustorb_cdr::Result<()> {
17        enc.write_u32(self.tag);
18        enc.write_octet_seq(&self.component_data)
19    }
20}
21
22impl CdrDecode for TaggedComponent {
23    fn decode(dec: &mut Decoder<'_>) -> rustorb_cdr::Result<Self> {
24        Ok(TaggedComponent {
25            tag: dec.read_u32()?,
26            component_data: dec.read_octet_seq()?.to_vec(),
27        })
28    }
29}
30
31impl TaggedComponent {
32    /// Builds a component whose data is an encapsulation written by `f`.
33    pub fn encapsulated<F>(tag: u32, order: ByteOrder, f: F) -> Result<TaggedComponent>
34    where
35        F: FnOnce(&mut Encoder) -> rustorb_cdr::Result<()>,
36    {
37        let mut enc = Encoder::encapsulation(order, CdrVersion::Giop1_2);
38        f(&mut enc)?;
39        Ok(TaggedComponent {
40            tag,
41            component_data: enc.into_bytes(),
42        })
43    }
44
45    /// Opens the component data as an encapsulation.
46    pub fn decoder(&self) -> Result<Decoder<'_>> {
47        Ok(Decoder::encapsulation(
48            &self.component_data,
49            CdrVersion::Giop1_2,
50        )?)
51    }
52}
53
54/// Standard `IOP::ComponentId` values, and the omniORB specific ones.
55pub mod component_tag {
56    /// `TAG_ORB_TYPE`, an `unsigned long` naming the ORB vendor.
57    pub const ORB_TYPE: u32 = 0;
58    /// `TAG_CODE_SETS`, a [`CodeSetComponentInfo`](crate::CodeSetComponentInfo).
59    pub const CODE_SETS: u32 = 1;
60    /// `TAG_POLICIES`.
61    pub const POLICIES: u32 = 2;
62    /// `TAG_ALTERNATE_IIOP_ADDRESS`, an [`AlternateIiopAddress`](crate::AlternateIiopAddress).
63    pub const ALTERNATE_IIOP_ADDRESS: u32 = 3;
64    /// `TAG_COMPLETE_OBJECT_KEY`.
65    pub const COMPLETE_OBJECT_KEY: u32 = 5;
66    /// `TAG_ENDPOINT_ID_POSITION`.
67    pub const ENDPOINT_ID_POSITION: u32 = 6;
68    /// `TAG_LOCATION_POLICY`.
69    pub const LOCATION_POLICY: u32 = 12;
70    /// `TAG_ASSOCIATION_OPTIONS`.
71    pub const ASSOCIATION_OPTIONS: u32 = 13;
72    /// `TAG_SEC_NAME`.
73    pub const SEC_NAME: u32 = 14;
74    /// `TAG_SSL_SEC_TRANS`, an [`SslSecTrans`](crate::SslSecTrans).
75    pub const SSL_SEC_TRANS: u32 = 20;
76    /// `TAG_JAVA_CODEBASE`.
77    pub const JAVA_CODEBASE: u32 = 25;
78    /// `TAG_CSI_SEC_MECH_LIST`.
79    pub const CSI_SEC_MECH_LIST: u32 = 33;
80    /// `TAG_NULL_TAG`.
81    pub const NULL_TAG: u32 = 34;
82    /// `TAG_TLS_SEC_TRANS`.
83    pub const TLS_SEC_TRANS: u32 = 36;
84    /// omniORB `TAG_OMNIORB_BIDIR`.
85    pub const OMNIORB_BIDIR: u32 = 0x4154_5401;
86    /// omniORB `TAG_OMNIORB_UNIX_TRANS`.
87    pub const OMNIORB_UNIX_TRANS: u32 = 0x4154_5402;
88    /// omniORB `TAG_OMNIORB_PERSISTENT_ID`.
89    pub const OMNIORB_PERSISTENT_ID: u32 = 0x4154_5403;
90    /// omniORB `TAG_OMNIORB_RESTRICTED_CONNECTION`.
91    pub const OMNIORB_RESTRICTED_CONNECTION: u32 = 0x4154_5404;
92    /// omniORB `TAG_OMNIORB_HTTP_TRANS`.
93    pub const OMNIORB_HTTP_TRANS: u32 = 0x4154_5405;
94}
95
96/// Values of the [`component_tag::ORB_TYPE`] component.
97pub mod orb_type {
98    /// omniORB (`"ATT\0"`).
99    pub const OMNIORB: u32 = 0x4154_5400;
100}
101
102/// `CONV_FRAME::CodeSetComponent`: the native code set of one character
103/// type and the code sets it can convert to.
104#[derive(Debug, Clone, PartialEq, Eq, Default)]
105pub struct CodeSetComponent {
106    /// Native code set.
107    pub native_code_set: u32,
108    /// Code sets the server can convert to and from.
109    pub conversion_code_sets: Vec<u32>,
110}
111
112impl CdrEncode for CodeSetComponent {
113    fn encode(&self, enc: &mut Encoder) -> rustorb_cdr::Result<()> {
114        enc.write_u32(self.native_code_set);
115        self.conversion_code_sets.encode(enc)
116    }
117}
118
119impl CdrDecode for CodeSetComponent {
120    fn decode(dec: &mut Decoder<'_>) -> rustorb_cdr::Result<Self> {
121        Ok(CodeSetComponent {
122            native_code_set: dec.read_u32()?,
123            conversion_code_sets: Vec::decode(dec)?,
124        })
125    }
126}
127
128/// `CONV_FRAME::CodeSetComponentInfo`, the data of the
129/// [`component_tag::CODE_SETS`] component.
130#[derive(Debug, Clone, PartialEq, Eq, Default)]
131pub struct CodeSetComponentInfo {
132    /// Code sets for `char` and `string`.
133    pub for_char_data: CodeSetComponent,
134    /// Code sets for `wchar` and `wstring`.
135    pub for_wchar_data: CodeSetComponent,
136}
137
138impl CodeSetComponentInfo {
139    /// Wraps the info in a [`component_tag::CODE_SETS`] component.
140    pub fn to_component(&self, order: ByteOrder) -> Result<TaggedComponent> {
141        TaggedComponent::encapsulated(component_tag::CODE_SETS, order, |enc| {
142            self.for_char_data.encode(enc)?;
143            self.for_wchar_data.encode(enc)
144        })
145    }
146
147    /// Reads the info from a component's data.
148    pub fn from_component(component: &TaggedComponent) -> Result<CodeSetComponentInfo> {
149        let mut dec = component.decoder()?;
150        Ok(CodeSetComponentInfo {
151            for_char_data: CodeSetComponent::decode(&mut dec)?,
152            for_wchar_data: CodeSetComponent::decode(&mut dec)?,
153        })
154    }
155}
156
157/// The data of the [`component_tag::ALTERNATE_IIOP_ADDRESS`] component: one
158/// more address the object can be reached at.
159#[derive(Debug, Clone, PartialEq, Eq)]
160pub struct AlternateIiopAddress {
161    /// Host name or IP address.
162    pub host: String,
163    /// TCP port.
164    pub port: u16,
165}
166
167impl AlternateIiopAddress {
168    /// Wraps the address in a [`component_tag::ALTERNATE_IIOP_ADDRESS`]
169    /// component.
170    pub fn to_component(&self, order: ByteOrder) -> Result<TaggedComponent> {
171        TaggedComponent::encapsulated(component_tag::ALTERNATE_IIOP_ADDRESS, order, |enc| {
172            enc.write_string(&self.host)?;
173            enc.write_u16(self.port);
174            Ok(())
175        })
176    }
177
178    /// Reads the address from a component's data.
179    pub fn from_component(component: &TaggedComponent) -> Result<AlternateIiopAddress> {
180        let mut dec = component.decoder()?;
181        Ok(AlternateIiopAddress {
182            host: dec.read_string()?,
183            port: dec.read_u16()?,
184        })
185    }
186}
187
188/// `Security::AssociationOptions` bits, as in the
189/// [`SslSecTrans`] component.
190pub mod association_options {
191    /// `NoProtection`: unprotected IIOP.
192    pub const NO_PROTECTION: u16 = 1;
193    /// `Integrity`.
194    pub const INTEGRITY: u16 = 2;
195    /// `Confidentiality`.
196    pub const CONFIDENTIALITY: u16 = 4;
197    /// `DetectReplay`.
198    pub const DETECT_REPLAY: u16 = 8;
199    /// `DetectMisordering`.
200    pub const DETECT_MISORDERING: u16 = 16;
201    /// `EstablishTrustInTarget`: the server authenticates itself.
202    pub const ESTABLISH_TRUST_IN_TARGET: u16 = 32;
203    /// `EstablishTrustInClient`: the client authenticates itself.
204    pub const ESTABLISH_TRUST_IN_CLIENT: u16 = 64;
205}
206
207/// `SSLIOP::SSL`, the data of a [`component_tag::SSL_SEC_TRANS`]
208/// component: the port of the TLS endpoint at the host of the IIOP
209/// profile, and the association options of the target.
210#[derive(Debug, Clone, Copy, PartialEq, Eq)]
211pub struct SslSecTrans {
212    /// What the target supports ([`association_options`] bits).
213    pub target_supports: u16,
214    /// What the target requires of clients.
215    pub target_requires: u16,
216    /// TCP port of the TLS endpoint.
217    pub port: u16,
218}
219
220impl SslSecTrans {
221    /// Wraps the data in a [`component_tag::SSL_SEC_TRANS`] component.
222    pub fn to_component(&self, order: ByteOrder) -> Result<TaggedComponent> {
223        TaggedComponent::encapsulated(component_tag::SSL_SEC_TRANS, order, |enc| {
224            enc.write_u16(self.target_supports);
225            enc.write_u16(self.target_requires);
226            enc.write_u16(self.port);
227            Ok(())
228        })
229    }
230
231    /// Reads the data from a component's data.
232    pub fn from_component(component: &TaggedComponent) -> Result<SslSecTrans> {
233        let mut dec = component.decoder()?;
234        Ok(SslSecTrans {
235            target_supports: dec.read_u16()?,
236            target_requires: dec.read_u16()?,
237            port: dec.read_u16()?,
238        })
239    }
240}
241
242/// Builds a [`component_tag::ORB_TYPE`] component.
243pub fn orb_type_component(orb_type: u32, order: ByteOrder) -> Result<TaggedComponent> {
244    TaggedComponent::encapsulated(component_tag::ORB_TYPE, order, |enc| {
245        enc.write_u32(orb_type);
246        Ok(())
247    })
248}