libcoap_rs/message/
mod.rs

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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// SPDX-License-Identifier: BSD-2-Clause
/*
 * message.rs - Types related to CoAP messages.
 * This file is part of the libcoap-rs crate, see the README and LICENSE files for
 * more information and terms of use.
 * Copyright © 2021-2023 The NAMIB Project Developers, all rights reserved.
 * See the README as well as the LICENSE file for more information.
 */

//! Types related to message handling, parsing and creation.
//!
//! The base unit that is transmitted between a CoAP client and a CoAP server is called a CoAP
//! message (in libcoap also referred to as a "pdu"). This module contains both the basic
//! representation for messages in libcoap-rs ([CoapMessage]) as well as builders that simplify the
//! process of creating requests and responses and setting the appropriate options ([CoapRequest]
//! and [CoapResponse]).

use std::{ffi::c_void, mem::MaybeUninit, slice::Iter};
use std::fmt::Write;

use num_traits::FromPrimitive;

use libcoap_sys::{
    coap_add_data, coap_add_data_large_request, coap_add_optlist_pdu, coap_add_token, coap_delete_optlist,
    coap_delete_pdu, coap_get_data, coap_insert_optlist, coap_new_optlist, coap_opt_length, coap_opt_t, coap_opt_value,
    coap_option_iterator_init, coap_option_next, coap_option_num_t, coap_optlist_t, coap_pdu_get_code,
    coap_pdu_get_mid, coap_pdu_get_token, coap_pdu_get_type, coap_pdu_init, coap_pdu_set_code, coap_pdu_set_type,
    coap_pdu_t, coap_session_t,
};
pub use request::CoapRequest;
pub use response::CoapResponse;

use crate::{
    error::{MessageConversionError, OptionValueError},
    protocol::{
        Block, CoapMatch, CoapMessageCode, CoapMessageType, CoapOptionNum, CoapOptionType, ContentFormat, ETag,
        HopLimit, MaxAge, NoResponse, Observe, ProxyScheme, ProxyUri, Size, UriHost, UriPath, UriPort, UriQuery,
    },
    session::CoapSessionCommon,
    types::CoapMessageId,
};
use crate::context::ensure_coap_started;
use crate::protocol::{Echo, Oscore, RequestTag};
use crate::types::{
    decode_var_len_u16, decode_var_len_u32, decode_var_len_u8, encode_var_len_u16, encode_var_len_u32,
    encode_var_len_u8,
};

pub mod request;
pub mod response;

/// Representation of a CoAP option including its value.
///
/// For an enum describing the possible option types (and their associated option numbers), see
/// [CoapOptionType], for the data type representing option numbers, see [CoapOptionNum]
#[derive(Debug, Hash, Eq, PartialEq, Clone)]
pub enum CoapOption {
    IfMatch(CoapMatch),
    IfNoneMatch,
    UriHost(UriHost),
    UriPort(UriPort),
    UriPath(UriPath),
    UriQuery(UriQuery),
    LocationPath(UriPath),
    LocationQuery(UriQuery),
    ProxyUri(ProxyUri),
    ProxyScheme(ProxyScheme),
    ContentFormat(ContentFormat),
    Accept(ContentFormat),
    Size1(Size),
    Size2(Size),
    Block1(Block),
    Block2(Block),
    HopLimit(HopLimit),
    NoResponse(NoResponse),
    ETag(ETag),
    MaxAge(MaxAge),
    Observe(Observe),
    Oscore(Oscore),
    Echo(Echo),
    RTag(RequestTag),
    QBlock1(Block),
    QBlock2(Block),
    Other(CoapOptionNum, Box<[u8]>),
}

impl CoapOption {
    /// Create a CoAP option from its raw representation in the C library.
    ///
    /// # Safety
    /// `opt` must be a valid pointer to a well-formed coap_opt_t value as returned by
    /// [coap_option_next()].
    pub(crate) unsafe fn from_raw_opt(
        number: coap_option_num_t,
        opt: *const coap_opt_t,
    ) -> Result<CoapOption, OptionValueError> {
        let value = Vec::from(std::slice::from_raw_parts(
            coap_opt_value(opt),
            coap_opt_length(opt) as usize,
        ));
        Self::from_type_value(number, value)
    }

    /// Returns the option number associated with this option.
    pub fn number(&self) -> CoapOptionNum {
        match self {
            CoapOption::IfMatch(_) => CoapOptionType::IfMatch as u16,
            CoapOption::IfNoneMatch => CoapOptionType::IfNoneMatch as u16,
            CoapOption::UriHost(_) => CoapOptionType::UriHost as u16,
            CoapOption::UriPort(_) => CoapOptionType::UriPort as u16,
            CoapOption::UriPath(_) => CoapOptionType::UriPath as u16,
            CoapOption::UriQuery(_) => CoapOptionType::UriQuery as u16,
            CoapOption::LocationPath(_) => CoapOptionType::LocationPath as u16,
            CoapOption::LocationQuery(_) => CoapOptionType::LocationQuery as u16,
            CoapOption::ProxyUri(_) => CoapOptionType::ProxyUri as u16,
            CoapOption::ProxyScheme(_) => CoapOptionType::ProxyScheme as u16,
            CoapOption::ContentFormat(_) => CoapOptionType::ContentFormat as u16,
            CoapOption::Accept(_) => CoapOptionType::Accept as u16,
            CoapOption::Size1(_) => CoapOptionType::Size1 as u16,
            CoapOption::Size2(_) => CoapOptionType::Size2 as u16,
            CoapOption::Block1(_) => CoapOptionType::Block1 as u16,
            CoapOption::Block2(_) => CoapOptionType::Block2 as u16,
            CoapOption::HopLimit(_) => CoapOptionType::HopLimit as u16,
            CoapOption::NoResponse(_) => CoapOptionType::NoResponse as u16,
            CoapOption::ETag(_) => CoapOptionType::ETag as u16,
            CoapOption::MaxAge(_) => CoapOptionType::MaxAge as u16,
            CoapOption::Observe(_) => CoapOptionType::Observe as u16,
            CoapOption::Oscore(_) => CoapOptionType::Oscore as u16,
            CoapOption::Echo(_) => CoapOptionType::Echo as u16,
            CoapOption::RTag(_) => CoapOptionType::RTag as u16,
            CoapOption::QBlock1(_) => CoapOptionType::QBlock1 as u16,
            CoapOption::QBlock2(_) => CoapOptionType::QBlock2 as u16,
            CoapOption::Other(num, _) => *num,
        }
    }

    /// Converts the option into a `Box<[u8]>` containing the value bytes.
    pub fn into_value_bytes(self) -> Result<Box<[u8]>, OptionValueError> {
        let num = self.number();
        let bytes = match self {
            CoapOption::IfMatch(val) => match val {
                CoapMatch::ETag(tag) => tag,
                CoapMatch::Empty => Box::new([]),
            },
            CoapOption::IfNoneMatch => Box::new([]),
            CoapOption::UriHost(value) => value.into_boxed_str().into_boxed_bytes(),
            CoapOption::UriPort(value) => encode_var_len_u16(value),
            CoapOption::UriPath(value) => value.into_boxed_str().into_boxed_bytes(),
            CoapOption::UriQuery(value) => value.into_boxed_str().into_boxed_bytes(),
            CoapOption::LocationPath(value) => value.into_boxed_str().into_boxed_bytes(),
            CoapOption::LocationQuery(value) => value.into_boxed_str().into_boxed_bytes(),
            CoapOption::ProxyUri(value) => value.into_boxed_str().into_boxed_bytes(),
            CoapOption::ProxyScheme(value) => value.into_boxed_str().into_boxed_bytes(),
            CoapOption::ContentFormat(value) => encode_var_len_u16(value),
            CoapOption::Accept(value) => encode_var_len_u16(value),
            CoapOption::Size1(value) => encode_var_len_u32(value),
            CoapOption::Size2(value) => encode_var_len_u32(value),
            CoapOption::Block1(value) => encode_var_len_u32(value),
            CoapOption::Block2(value) => encode_var_len_u32(value),
            CoapOption::HopLimit(value) => encode_var_len_u16(value),
            CoapOption::NoResponse(value) => encode_var_len_u8(value),
            CoapOption::ETag(value) => value,
            CoapOption::MaxAge(value) => encode_var_len_u32(value),
            CoapOption::Observe(value) => encode_var_len_u32(value),
            CoapOption::Oscore(value) => value,
            CoapOption::Echo(value) => value,
            CoapOption::RTag(value) => value,
            CoapOption::QBlock1(value) => encode_var_len_u32(value),
            CoapOption::QBlock2(value) => encode_var_len_u32(value),
            CoapOption::Other(_num, data) => data,
        };
        if let Some(opt_type) = <CoapOptionType as FromPrimitive>::from_u16(num) {
            if bytes.len() < opt_type.min_len() {
                return Err(OptionValueError::TooShort);
            } else if bytes.len() > opt_type.max_len() {
                return Err(OptionValueError::TooLong);
            }
        }
        Ok(bytes)
    }

    /// Converts this option into a raw coap_optlist_t instance, suitable for addition to a raw
    /// coap_pdu_t.
    pub(crate) fn into_optlist_entry(self) -> Result<*mut coap_optlist_t, OptionValueError> {
        let num = self.number();
        let value = self.into_value_bytes()?;
        Ok(unsafe { coap_new_optlist(num, value.len(), value.as_ptr()) })
    }

    /// Attempts to convert a raw coap_optlist_t instance into a [CoapOption].
    ///
    /// # Errors
    ///
    /// Returns an [OptionValueError] if the provided `optlist_entry` has an invalid value.
    ///
    /// # Safety
    ///
    /// optlist_entry must be a valid coap_optlist_t instance whose `number`, `data` and `length`
    /// fields describe a CoAP option number and the option value respectively.
    pub(crate) unsafe fn from_optlist_entry(optlist_entry: &coap_optlist_t) -> Result<Self, OptionValueError> {
        let value = Vec::from(std::slice::from_raw_parts(optlist_entry.data, optlist_entry.length));
        Self::from_type_value(optlist_entry.number, value)
    }

    /// Convert coap_option_num_t and option value into a parsed [CoapOption] if possible.
    fn from_type_value(type_: coap_option_num_t, value: Vec<u8>) -> Result<Self, OptionValueError> {
        match CoapOptionType::try_from(type_) {
            Ok(opt_type) => {
                if opt_type.min_len() > value.len() {
                    return Err(OptionValueError::TooShort);
                } else if opt_type.max_len() < value.len() {
                    return Err(OptionValueError::TooLong);
                }
                match opt_type {
                    CoapOptionType::IfMatch => Ok(CoapOption::IfMatch(if value.is_empty() {
                        CoapMatch::Empty
                    } else {
                        CoapMatch::ETag(value.into_boxed_slice())
                    })),
                    CoapOptionType::UriHost => Ok(CoapOption::UriHost(String::from_utf8(value)?)),
                    CoapOptionType::ETag => Ok(CoapOption::ETag(value.into_boxed_slice())),
                    CoapOptionType::IfNoneMatch => Ok(CoapOption::IfNoneMatch),
                    CoapOptionType::UriPort => Ok(CoapOption::UriPort(decode_var_len_u16(value.as_slice()))),
                    CoapOptionType::LocationPath => Ok(CoapOption::LocationPath(String::from_utf8(value)?)),
                    CoapOptionType::UriPath => Ok(CoapOption::UriPath(String::from_utf8(value)?)),
                    CoapOptionType::ContentFormat => {
                        Ok(CoapOption::ContentFormat(decode_var_len_u16(value.as_slice())))
                    },
                    CoapOptionType::MaxAge => Ok(CoapOption::MaxAge(decode_var_len_u32(value.as_slice()))),
                    CoapOptionType::UriQuery => Ok(CoapOption::UriQuery(String::from_utf8(value)?)),
                    CoapOptionType::Accept => Ok(CoapOption::Accept(decode_var_len_u16(value.as_slice()))),
                    CoapOptionType::LocationQuery => Ok(CoapOption::LocationQuery(String::from_utf8(value)?)),
                    CoapOptionType::ProxyUri => Ok(CoapOption::ProxyUri(String::from_utf8(value)?)),
                    CoapOptionType::ProxyScheme => Ok(CoapOption::ProxyScheme(String::from_utf8(value)?)),
                    CoapOptionType::Size1 => Ok(CoapOption::Size1(decode_var_len_u32(value.as_slice()))),
                    CoapOptionType::Size2 => Ok(CoapOption::Size2(decode_var_len_u32(value.as_slice()))),
                    CoapOptionType::Block1 => Ok(CoapOption::Block1(decode_var_len_u32(value.as_slice()))),
                    CoapOptionType::Block2 => Ok(CoapOption::Block2(decode_var_len_u32(value.as_slice()))),
                    CoapOptionType::HopLimit => Ok(CoapOption::HopLimit(decode_var_len_u16(value.as_slice()))),
                    CoapOptionType::NoResponse => {
                        Ok(CoapOption::NoResponse(decode_var_len_u8(value.as_slice()) as NoResponse))
                    },
                    CoapOptionType::Observe => Ok(CoapOption::Observe(decode_var_len_u32(value.as_slice()))),
                    CoapOptionType::Oscore => Ok(CoapOption::Oscore(value.into_boxed_slice())),
                    CoapOptionType::Echo => Ok(CoapOption::Echo(value.into_boxed_slice())),
                    CoapOptionType::RTag => Ok(CoapOption::RTag(value.into_boxed_slice())),
                    CoapOptionType::QBlock1 => Ok(CoapOption::QBlock1(decode_var_len_u32(value.as_slice()))),
                    CoapOptionType::QBlock2 => Ok(CoapOption::QBlock2(decode_var_len_u32(value.as_slice()))),
                }
            },
            _ => Ok(CoapOption::Other(type_, value.into_boxed_slice())),
        }
    }
}

/// Constructs a path string from a [Vec] of strings containing the separate path components.
pub(crate) fn construct_path_string(path_components: Vec<String>) -> String {
    path_components.into_iter().fold(String::new(), |mut a: String, v| {
        // Writing to a String _shouldn't_ cause an error.
        // If it does, something is terribly wrong and we should panic.
        write!(&mut a, "/{}", v).expect("unable to create path string");
        a
    })
}

/// Constructs a query string from a [Vec] of strings containing the separate query components.
pub(crate) fn construct_query_string(query_components: Vec<String>) -> String {
    let mut iter = query_components.iter();
    let mut out_str = String::from("?");
    if let Some(q) = iter.next() {
        out_str = out_str + q
    }
    for q in iter {
        out_str += format!("&{}", q).as_ref();
    }
    out_str
}

/// Interface for CoAP messages common between requests, responses and other messages.
pub trait CoapMessageCommon {
    /// Add the supplied CoAP option to this message.
    fn add_option(&mut self, option: CoapOption) {
        self.as_message_mut().options.push(option);
    }

    /// Clear the list of options that were added to this message using [add_option()](CoapMessageCommon::add_option()).
    fn clear_options(&mut self) {
        self.as_message_mut().options.clear();
    }

    /// Returns an iterator over the options contained in this message.
    fn options_iter(&self) -> Iter<CoapOption> {
        self.as_message().options.iter()
    }

    /// Returns the CoAP message type (confirmable, non-confirmable, acknowledgement, rst) of this message.
    fn type_(&self) -> CoapMessageType {
        self.as_message().type_
    }

    /// Sets the CoAP message type (confirmable, non-confirmable, acknowledgement, rst) of this message.
    fn set_type_(&mut self, type_: CoapMessageType) {
        self.as_message_mut().type_ = type_;
    }

    /// Returns the message code of this message.
    /// To determine whether the message is a request or response, use [CoapMessageCode::try_from()]
    /// and match for the enum variants.
    fn code(&self) -> CoapMessageCode {
        self.as_message().code
    }

    /// Sets the message code of this message.
    fn set_code<C: Into<CoapMessageCode>>(&mut self, code: C) {
        self.as_message_mut().code = code.into();
    }

    /// Returns the CoAP message ID for this message.
    fn mid(&self) -> Option<CoapMessageId> {
        self.as_message().mid
    }

    /// Sets the CoAP message ID for this message.
    fn set_mid(&mut self, mid: Option<CoapMessageId>) {
        self.as_message_mut().mid = mid;
    }

    /// Returns a reference to the data/body of this message.
    fn data(&self) -> Option<&[u8]> {
        self.as_message().data.as_ref().map(|v| v.as_ref())
    }

    /// Sets the data/body of this message.
    fn set_data<D: Into<Box<[u8]>>>(&mut self, data: Option<D>) {
        self.as_message_mut().data = data.map(Into::into);
    }

    /// Returns the message token.
    fn token(&self) -> Option<&[u8]> {
        self.as_message().token.as_ref().map(|v| v.as_ref())
    }

    /// Sets the message token.
    ///
    /// Note that [CoapSessionCommon::send_request()] will automatically set the token to a random
    /// value if you don't.
    fn set_token<D: Into<Box<[u8]>>>(&mut self, token: Option<D>) {
        self.as_message_mut().token = token.map(Into::into);
    }

    /// Returns a reference to this message.
    fn as_message(&self) -> &CoapMessage;
    /// Returns a mutable reference to this message.
    fn as_message_mut(&mut self) -> &mut CoapMessage;
}

/// Representation of a CoAP message.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CoapMessage {
    /// CoAP message type (CON, NON, ACK, RST).
    type_: CoapMessageType,
    /// CoAP message code (e.g. 2.00 Content or 4.04 Not Found)
    code: CoapMessageCode,
    /// ID of this message – unique to each message sent over this session.
    mid: Option<CoapMessageId>,
    /// List of CoAP options associated with this message.
    options: Vec<CoapOption>,
    /// CoAP message token – used for request-response-matching.
    token: Option<Box<[u8]>>,
    /// Message body of this message.
    data: Option<Box<[u8]>>,
}

impl CoapMessage {
    /// Creates a new CoAP message with the given type and code.
    pub fn new(type_: CoapMessageType, code: CoapMessageCode) -> CoapMessage {
        ensure_coap_started();
        CoapMessage {
            type_,
            code,
            mid: None,
            options: Vec::new(),
            token: None,
            data: None,
        }
    }

    /// Parses the given raw coap_pdu_t into a CoapMessage.
    ///
    /// # Safety
    /// raw_pdu must point to a valid instance of coap_pdu_t.
    pub unsafe fn from_raw_pdu(raw_pdu: *const coap_pdu_t) -> Result<CoapMessage, MessageConversionError> {
        ensure_coap_started();
        let mut option_iter = MaybeUninit::zeroed();
        coap_option_iterator_init(raw_pdu, option_iter.as_mut_ptr(), std::ptr::null());
        let mut option_iter = option_iter.assume_init();
        let mut options = Vec::new();
        while let Some(read_option) = coap_option_next(&mut option_iter).as_ref() {
            options.push(CoapOption::from_raw_opt(option_iter.number, read_option).map_err(|e| {
                MessageConversionError::InvalidOptionValue(CoapOptionType::try_from(option_iter.number).ok(), e)
            })?);
        }
        let mut len: usize = 0;
        let mut data = std::ptr::null();
        coap_get_data(raw_pdu, &mut len, &mut data);
        let data = match len {
            0 => None,
            len => Some(Vec::from(std::slice::from_raw_parts(data, len)).into_boxed_slice()),
        };
        let raw_token = coap_pdu_get_token(raw_pdu);
        let token = Vec::from(std::slice::from_raw_parts(raw_token.s, raw_token.length));
        Ok(CoapMessage {
            type_: coap_pdu_get_type(raw_pdu).into(),
            code: coap_pdu_get_code(raw_pdu).try_into().unwrap(),
            mid: Some(coap_pdu_get_mid(raw_pdu)),
            options,
            token: Some(token.into_boxed_slice()),
            data,
        })
    }

    /// Converts this message into a raw PDU suitable for sending using the raw [coap_send()](libcoap_sys::coap_send())
    /// function.
    ///
    /// The caller is responsible for freeing the returned PDU, either by calling [coap_send()](libcoap_sys::coap_send()) or
    /// [coap_delete_pdu()].
    pub fn into_raw_pdu<'a, S: CoapSessionCommon<'a> + ?Sized>(
        mut self,
        session: &S,
    ) -> Result<*mut coap_pdu_t, MessageConversionError> {
        let message = self.as_message_mut();

        // SAFETY: all values are valid, cannot cause UB.
        let pdu = unsafe {
            coap_pdu_init(
                message.type_.to_raw_pdu_type(),
                message.code.to_raw_pdu_code(),
                message.mid.ok_or(MessageConversionError::MissingMessageId)?,
                session.max_pdu_size(),
            )
        };
        if pdu.is_null() {
            return Err(MessageConversionError::Unknown);
        }
        // SAFETY: We just checked that pdu is a valid pointer.
        unsafe {
            let result = self.apply_to_raw_pdu(pdu, session);
            if result.is_err() {
                coap_delete_pdu(pdu);
            }
            result
        }
    }

    /// Applies this message to the given raw PDU.
    ///
    /// Note that existing parameters of the PDU are not explicitly removed. However, because this
    /// function calls [coap_add_token()](libcoap_sys::coap_add_token()), any pre-added options and
    /// data will probably be destroyed.
    ///
    /// If you don't have a raw pdu instance, use [CoapMessage::into_raw_pdu()] instead of this
    /// function.
    ///
    /// # Panics
    /// Panics if the provided `raw_pdu` is a null pointer.
    ///
    /// # Safety
    /// raw_pdu must point to a valid mutable instance of coap_pdu_t.
    pub(crate) unsafe fn apply_to_raw_pdu<'a, S: CoapSessionCommon<'a> + ?Sized>(
        mut self,
        raw_pdu: *mut coap_pdu_t,
        session: &S,
    ) -> Result<*mut coap_pdu_t, MessageConversionError> {
        assert!(!raw_pdu.is_null(), "attempted to apply CoapMessage to null pointer");
        coap_pdu_set_type(raw_pdu, self.type_.to_raw_pdu_type());
        coap_pdu_set_code(raw_pdu, self.code.to_raw_pdu_code());
        let message = self.as_message_mut();
        let token: &[u8] = message.token.as_ref().ok_or(MessageConversionError::MissingToken)?;
        if coap_add_token(raw_pdu, token.len(), token.as_ptr()) == 0 {
            return Err(MessageConversionError::Unknown);
        }
        let mut optlist = None;
        let option_iter = std::mem::take(&mut message.options).into_iter();
        for option in option_iter {
            let optnum = option.number();
            let entry = option
                .into_optlist_entry()
                .map_err(|e| MessageConversionError::InvalidOptionValue(CoapOptionType::try_from(optnum).ok(), e))?;
            if entry.is_null() {
                if let Some(optlist) = optlist {
                    coap_delete_optlist(optlist);
                    return Err(MessageConversionError::Unknown);
                }
            }
            match optlist {
                None => {
                    optlist = Some(entry);
                },
                Some(mut optlist) => {
                    coap_insert_optlist(&mut optlist, entry);
                },
            }
        }
        if let Some(mut optlist) = optlist {
            let optlist_add_success = coap_add_optlist_pdu(raw_pdu, &mut optlist);
            coap_delete_optlist(optlist);
            if optlist_add_success == 0 {
                return Err(MessageConversionError::Unknown);
            }
        }
        if let Some(data) = message.data.take() {
            match message.code {
                CoapMessageCode::Empty => return Err(MessageConversionError::DataInEmptyMessage),
                CoapMessageCode::Request(_) => {
                    let len = data.len();
                    let box_ptr = Box::into_raw(data);
                    coap_add_data_large_request(
                        session.raw_session_mut(),
                        raw_pdu,
                        len,
                        box_ptr as *mut u8,
                        Some(large_data_cleanup_handler),
                        box_ptr as *mut c_void,
                    );
                },
                CoapMessageCode::Response(_) => {
                    // TODO blockwise transfer here as well.
                    // (for some reason libcoap needs the request PDU here?)
                    let data: &[u8] = data.as_ref();
                    if coap_add_data(raw_pdu, data.len(), data.as_ptr()) == 0 {
                        return Err(MessageConversionError::Unknown);
                    }
                },
            }
        }
        Ok(raw_pdu)
    }
}

impl CoapMessageCommon for CoapMessage {
    fn as_message(&self) -> &CoapMessage {
        self
    }

    fn as_message_mut(&mut self) -> &mut CoapMessage {
        self
    }
}

impl From<CoapRequest> for CoapMessage {
    fn from(val: CoapRequest) -> Self {
        val.into_message()
    }
}

impl From<CoapResponse> for CoapMessage {
    fn from(val: CoapResponse) -> Self {
        val.into_message()
    }
}

/// Handler provided to libcoap to cleanup large message bodies.
unsafe extern "C" fn large_data_cleanup_handler(_session: *mut coap_session_t, app_ptr: *mut c_void) {
    std::mem::drop(Box::from_raw(app_ptr as *mut u8));
}