Skip to main content

rustorb_giop/
service_context.rs

1use rustorb_cdr::{ByteOrder, CdrDecode, CdrEncode, Decoder, Encoder, Encoding};
2
3/// `IOP::ServiceContext`, with the context data left as an encapsulation.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct ServiceContext {
6    /// Context id such as [`service_id::CODE_SETS`].
7    pub context_id: u32,
8    /// Encapsulated context data.
9    pub context_data: Vec<u8>,
10}
11
12impl CdrEncode for ServiceContext {
13    fn encode(&self, enc: &mut Encoder) -> rustorb_cdr::Result<()> {
14        enc.write_u32(self.context_id);
15        enc.write_octet_seq(&self.context_data)
16    }
17}
18
19impl CdrDecode for ServiceContext {
20    fn decode(dec: &mut Decoder<'_>) -> rustorb_cdr::Result<Self> {
21        Ok(ServiceContext {
22            context_id: dec.read_u32()?,
23            context_data: dec.read_octet_seq()?.to_vec(),
24        })
25    }
26}
27
28/// Standard `IOP::ServiceId` values.
29pub mod service_id {
30    /// `TransactionService`.
31    pub const TRANSACTION_SERVICE: u32 = 0;
32    /// `CodeSets`, carrying a [`CodeSetContext`](crate::CodeSetContext).
33    pub const CODE_SETS: u32 = 1;
34    /// `ChainBypassCheck`.
35    pub const CHAIN_BYPASS_CHECK: u32 = 2;
36    /// `ChainBypassInfo`.
37    pub const CHAIN_BYPASS_INFO: u32 = 3;
38    /// `LogicalThreadId`.
39    pub const LOGICAL_THREAD_ID: u32 = 4;
40    /// `BI_DIR_IIOP`.
41    pub const BI_DIR_IIOP: u32 = 5;
42    /// `SendingContextRunTime`.
43    pub const SENDING_CONTEXT_RUN_TIME: u32 = 6;
44    /// `INVOCATION_POLICIES`.
45    pub const INVOCATION_POLICIES: u32 = 7;
46    /// `FORWARDED_IDENTITY`.
47    pub const FORWARDED_IDENTITY: u32 = 8;
48    /// `UnknownExceptionInfo`.
49    pub const UNKNOWN_EXCEPTION_INFO: u32 = 9;
50    /// `RTCorbaPriority`.
51    pub const RT_CORBA_PRIORITY: u32 = 10;
52    /// `RTCorbaPriorityRange`.
53    pub const RT_CORBA_PRIORITY_RANGE: u32 = 11;
54    /// `FT_GROUP_VERSION`.
55    pub const FT_GROUP_VERSION: u32 = 12;
56    /// `FT_REQUEST`.
57    pub const FT_REQUEST: u32 = 13;
58    /// `ExceptionDetailMessage`.
59    pub const EXCEPTION_DETAIL_MESSAGE: u32 = 14;
60    /// `SecurityAttributeService`.
61    pub const SECURITY_ATTRIBUTE_SERVICE: u32 = 15;
62    /// `ActivityService`.
63    pub const ACTIVITY_SERVICE: u32 = 16;
64}
65
66/// OSF registry ids of common code sets (`CONV_FRAME::CodeSetId`).
67pub mod code_set {
68    /// ISO 8859-1 (Latin-1), the default `char` code set.
69    pub const ISO_8859_1: u32 = 0x0001_0001;
70    /// UCS-2 level 1.
71    pub const UCS_2_LEVEL_1: u32 = 0x0001_0100;
72    /// UTF-16.
73    pub const UTF_16: u32 = 0x0001_0109;
74    /// UTF-8.
75    pub const UTF_8: u32 = 0x0501_0001;
76}
77
78/// `CONV_FRAME::CodeSetContext`, the transmission code sets chosen by a
79/// client.
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub struct CodeSetContext {
82    /// Transmission code set for `char` and `string`.
83    pub char_data: u32,
84    /// Transmission code set for `wchar` and `wstring`.
85    pub wchar_data: u32,
86}
87
88impl CodeSetContext {
89    /// Wraps the context in a [`service_id::CODE_SETS`] service context.
90    pub fn to_service_context(&self, order: ByteOrder) -> ServiceContext {
91        let mut enc = Encoder::encapsulation(order, Encoding::Giop1_2);
92        enc.write_u32(self.char_data);
93        enc.write_u32(self.wchar_data);
94        ServiceContext {
95            context_id: service_id::CODE_SETS,
96            context_data: enc.into_bytes(),
97        }
98    }
99
100    /// Reads the context from a service context's data.
101    pub fn from_service_context(context: &ServiceContext) -> rustorb_cdr::Result<CodeSetContext> {
102        let mut dec = Decoder::encapsulation(&context.context_data, Encoding::Giop1_2)?;
103        Ok(CodeSetContext {
104            char_data: dec.read_u32()?,
105            wchar_data: dec.read_u32()?,
106        })
107    }
108
109    /// Finds and reads the code set context in a service context list.
110    pub fn find(contexts: &[ServiceContext]) -> Option<rustorb_cdr::Result<CodeSetContext>> {
111        contexts
112            .iter()
113            .find(|c| c.context_id == service_id::CODE_SETS)
114            .map(CodeSetContext::from_service_context)
115    }
116}
117
118/// `IIOP::ListenPoint`: a host and port a client listens on.
119#[derive(Debug, Clone, PartialEq, Eq, Hash)]
120pub struct ListenPoint {
121    /// Host name or address, as in the client's IORs.
122    pub host: String,
123    /// Port.
124    pub port: u16,
125}
126
127/// `IIOP::BiDirIIOPServiceContext`: the addresses a client listens on,
128/// sent with [`service_id::BI_DIR_IIOP`] to offer the connection for
129/// requests from the server (bidirectional GIOP, GIOP 1.2 and later).
130#[derive(Debug, Clone, PartialEq, Eq, Default)]
131pub struct BiDirIiopServiceContext {
132    /// The client's listen points.
133    pub listen_points: Vec<ListenPoint>,
134}
135
136impl BiDirIiopServiceContext {
137    /// Wraps the context in a [`service_id::BI_DIR_IIOP`] service context.
138    pub fn to_service_context(&self, order: ByteOrder) -> rustorb_cdr::Result<ServiceContext> {
139        let mut enc = Encoder::encapsulation(order, Encoding::Giop1_2);
140        enc.write_len(self.listen_points.len())?;
141        for lp in &self.listen_points {
142            enc.write_string(&lp.host)?;
143            enc.write_u16(lp.port);
144        }
145        Ok(ServiceContext {
146            context_id: service_id::BI_DIR_IIOP,
147            context_data: enc.into_bytes(),
148        })
149    }
150
151    /// Reads the context from a service context's data.
152    pub fn from_service_context(
153        context: &ServiceContext,
154    ) -> rustorb_cdr::Result<BiDirIiopServiceContext> {
155        let mut dec = Decoder::encapsulation(&context.context_data, Encoding::Giop1_2)?;
156        // A string is at least its length and its terminating NUL, a port
157        // two more octets.
158        let n = dec.read_len(7)?;
159        let mut listen_points = Vec::with_capacity(n);
160        for _ in 0..n {
161            listen_points.push(ListenPoint {
162                host: dec.read_string()?,
163                port: dec.read_u16()?,
164            });
165        }
166        Ok(BiDirIiopServiceContext { listen_points })
167    }
168
169    /// Finds and reads the context in a service context list.
170    pub fn find(
171        contexts: &[ServiceContext],
172    ) -> Option<rustorb_cdr::Result<BiDirIiopServiceContext>> {
173        contexts
174            .iter()
175            .find(|c| c.context_id == service_id::BI_DIR_IIOP)
176            .map(BiDirIiopServiceContext::from_service_context)
177    }
178}
179
180#[cfg(test)]
181mod bidir_tests {
182    use super::*;
183
184    #[test]
185    fn bidir_context_round_trips() {
186        let ctx = BiDirIiopServiceContext {
187            listen_points: vec![
188                ListenPoint {
189                    host: "client.example".into(),
190                    port: 2809,
191                },
192                ListenPoint {
193                    host: "10.0.0.1".into(),
194                    port: 65535,
195                },
196            ],
197        };
198        for order in [ByteOrder::Big, ByteOrder::Little] {
199            let sc = ctx.to_service_context(order).unwrap();
200            assert_eq!(sc.context_id, service_id::BI_DIR_IIOP);
201            assert_eq!(BiDirIiopServiceContext::find(&[sc]).unwrap().unwrap(), ctx);
202        }
203    }
204
205    #[test]
206    fn bidir_context_wire_form() {
207        let ctx = BiDirIiopServiceContext {
208            listen_points: vec![ListenPoint {
209                host: "h".into(),
210                port: 0x1234,
211            }],
212        };
213        let sc = ctx.to_service_context(ByteOrder::Big).unwrap();
214        // Byte order, count, string "h", port.
215        assert_eq!(
216            sc.context_data,
217            [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, b'h', 0, 0x12, 0x34]
218        );
219    }
220
221    #[test]
222    fn bidir_context_rejects_a_bad_count() {
223        let sc = ServiceContext {
224            context_id: service_id::BI_DIR_IIOP,
225            context_data: vec![0, 0, 0, 0, 0x7f, 0xff, 0xff, 0xff],
226        };
227        assert!(BiDirIiopServiceContext::find(&[sc]).unwrap().is_err());
228    }
229}