1c87c5fbaSopenharmony_ci/*
2c87c5fbaSopenharmony_ci * coap_uri.h -- helper functions for URI treatment
3c87c5fbaSopenharmony_ci *
4c87c5fbaSopenharmony_ci * Copyright (C) 2010-2023 Olaf Bergmann <bergmann@tzi.org>
5c87c5fbaSopenharmony_ci *
6c87c5fbaSopenharmony_ci * SPDX-License-Identifier: BSD-2-Clause
7c87c5fbaSopenharmony_ci *
8c87c5fbaSopenharmony_ci * This file is part of the CoAP library libcoap. Please see README for terms
9c87c5fbaSopenharmony_ci * of use.
10c87c5fbaSopenharmony_ci */
11c87c5fbaSopenharmony_ci
12c87c5fbaSopenharmony_ci/**
13c87c5fbaSopenharmony_ci * @file coap_uri.h
14c87c5fbaSopenharmony_ci * @brief Helper functions for URI treatment
15c87c5fbaSopenharmony_ci */
16c87c5fbaSopenharmony_ci
17c87c5fbaSopenharmony_ci#ifndef COAP_URI_H_
18c87c5fbaSopenharmony_ci#define COAP_URI_H_
19c87c5fbaSopenharmony_ci
20c87c5fbaSopenharmony_ci#include <stdint.h>
21c87c5fbaSopenharmony_ci
22c87c5fbaSopenharmony_ci#include "coap_str.h"
23c87c5fbaSopenharmony_ci
24c87c5fbaSopenharmony_ci/**
25c87c5fbaSopenharmony_ci * The scheme specifiers. Secure schemes have an odd numeric value,
26c87c5fbaSopenharmony_ci * others are even.
27c87c5fbaSopenharmony_ci */
28c87c5fbaSopenharmony_citypedef enum coap_uri_scheme_t {
29c87c5fbaSopenharmony_ci  COAP_URI_SCHEME_COAP = 0,
30c87c5fbaSopenharmony_ci  COAP_URI_SCHEME_COAPS,     /* 1 */
31c87c5fbaSopenharmony_ci  COAP_URI_SCHEME_COAP_TCP,  /* 2 */
32c87c5fbaSopenharmony_ci  COAP_URI_SCHEME_COAPS_TCP, /* 3 */
33c87c5fbaSopenharmony_ci  COAP_URI_SCHEME_HTTP,      /* 4 Proxy-Uri only */
34c87c5fbaSopenharmony_ci  COAP_URI_SCHEME_HTTPS,     /* 5 Proxy-Uri only */
35c87c5fbaSopenharmony_ci  COAP_URI_SCHEME_COAP_WS,   /* 6 */
36c87c5fbaSopenharmony_ci  COAP_URI_SCHEME_COAPS_WS,  /* 7 */
37c87c5fbaSopenharmony_ci  COAP_URI_SCHEME_LAST       /* 8 Size of scheme */
38c87c5fbaSopenharmony_ci} coap_uri_scheme_t;
39c87c5fbaSopenharmony_ci
40c87c5fbaSopenharmony_ci/** This mask can be used to check if a parsed URI scheme is secure. */
41c87c5fbaSopenharmony_ci#define COAP_URI_SCHEME_SECURE_MASK 0x01
42c87c5fbaSopenharmony_ci
43c87c5fbaSopenharmony_ci#define COAP_URI_SCHEME_COAP_BIT       (1 << COAP_URI_SCHEME_COAP)
44c87c5fbaSopenharmony_ci#define COAP_URI_SCHEME_COAPS_BIT      (1 << COAP_URI_SCHEME_COAPS)
45c87c5fbaSopenharmony_ci#define COAP_URI_SCHEME_COAP_TCP_BIT   (1 << COAP_URI_SCHEME_COAP_TCP)
46c87c5fbaSopenharmony_ci#define COAP_URI_SCHEME_COAPS_TCP_BIT  (1 << COAP_URI_SCHEME_COAPS_TCP)
47c87c5fbaSopenharmony_ci#define COAP_URI_SCHEME_HTTP_BIT       (1 << COAP_URI_SCHEME_HTTP)
48c87c5fbaSopenharmony_ci#define COAP_URI_SCHEME_HTTPS_BIT      (1 << COAP_URI_SCHEME_HTTPS)
49c87c5fbaSopenharmony_ci#define COAP_URI_SCHEME_COAP_WS_BIT    (1 << COAP_URI_SCHEME_COAP_WS)
50c87c5fbaSopenharmony_ci#define COAP_URI_SCHEME_COAPS_WS_BIT   (1 << COAP_URI_SCHEME_COAPS_WS)
51c87c5fbaSopenharmony_ci
52c87c5fbaSopenharmony_ci#define COAP_URI_SCHEME_ALL_COAP_BITS (COAP_URI_SCHEME_COAP_BIT | \
53c87c5fbaSopenharmony_ci                                       COAP_URI_SCHEME_COAPS_BIT | \
54c87c5fbaSopenharmony_ci                                       COAP_URI_SCHEME_COAP_TCP_BIT | \
55c87c5fbaSopenharmony_ci                                       COAP_URI_SCHEME_COAPS_TCP_BIT | \
56c87c5fbaSopenharmony_ci                                       COAP_URI_SCHEME_COAP_WS_BIT | \
57c87c5fbaSopenharmony_ci                                       COAP_URI_SCHEME_COAPS_WS_BIT)
58c87c5fbaSopenharmony_ci
59c87c5fbaSopenharmony_ci/**
60c87c5fbaSopenharmony_ci * Representation of parsed URI. Components may be filled from a string with
61c87c5fbaSopenharmony_ci * coap_split_uri() or coap_split_proxy_uri() and can be used as input for
62c87c5fbaSopenharmony_ci * option-creation functions. Alternatively, coap_uri_into_options() can
63c87c5fbaSopenharmony_ci * be used to convert coap_uri_t into CoAP options.
64c87c5fbaSopenharmony_ci */
65c87c5fbaSopenharmony_citypedef struct {
66c87c5fbaSopenharmony_ci  coap_str_const_t host;  /**< The host part of the URI */
67c87c5fbaSopenharmony_ci  uint16_t port;          /**< The port in host byte order */
68c87c5fbaSopenharmony_ci  coap_str_const_t path;  /**< The complete path if present or {0, NULL}.
69c87c5fbaSopenharmony_ci                               Needs to be split using coap_split_path()
70c87c5fbaSopenharmony_ci                               or coap_uri_into_options(). */
71c87c5fbaSopenharmony_ci  coap_str_const_t query; /**< The complete query if present or {0, NULL}.
72c87c5fbaSopenharmony_ci                               Needs to be split using coap_split_query()
73c87c5fbaSopenharmony_ci                               or coap_uri_into_options(). */
74c87c5fbaSopenharmony_ci  /** The parsed scheme specifier. */
75c87c5fbaSopenharmony_ci  enum coap_uri_scheme_t scheme;
76c87c5fbaSopenharmony_ci} coap_uri_t;
77c87c5fbaSopenharmony_ci
78c87c5fbaSopenharmony_cistatic inline int
79c87c5fbaSopenharmony_cicoap_uri_scheme_is_secure(const coap_uri_t *uri) {
80c87c5fbaSopenharmony_ci  return uri && ((uri->scheme & COAP_URI_SCHEME_SECURE_MASK) != 0);
81c87c5fbaSopenharmony_ci}
82c87c5fbaSopenharmony_ci
83c87c5fbaSopenharmony_ci/**
84c87c5fbaSopenharmony_ci * Determines from the @p host whether this is a Unix Domain socket
85c87c5fbaSopenharmony_ci * request.
86c87c5fbaSopenharmony_ci *
87c87c5fbaSopenharmony_ci * @param host    The host object.
88c87c5fbaSopenharmony_ci *
89c87c5fbaSopenharmony_ci * @return        @c 0 on failure, or @c 1 on success.
90c87c5fbaSopenharmony_ci *
91c87c5fbaSopenharmony_ci */
92c87c5fbaSopenharmony_ciint coap_host_is_unix_domain(const coap_str_const_t *host);
93c87c5fbaSopenharmony_ci
94c87c5fbaSopenharmony_ci/**
95c87c5fbaSopenharmony_ci * Creates a new coap_uri_t object from the specified URI. Returns the new
96c87c5fbaSopenharmony_ci * object or NULL on error. The memory allocated by the new coap_uri_t
97c87c5fbaSopenharmony_ci * should be released using coap_delete_uri().
98c87c5fbaSopenharmony_ci *
99c87c5fbaSopenharmony_ci * @param uri The URI path to copy.
100c87c5fbaSopenharmony_ci * @param length The length of uri.
101c87c5fbaSopenharmony_ci *
102c87c5fbaSopenharmony_ci * @return New URI object or NULL on error.
103c87c5fbaSopenharmony_ci */
104c87c5fbaSopenharmony_cicoap_uri_t *coap_new_uri(const uint8_t *uri, unsigned int length);
105c87c5fbaSopenharmony_ci
106c87c5fbaSopenharmony_ci/**
107c87c5fbaSopenharmony_ci * Clones the specified coap_uri_t object. This function allocates sufficient
108c87c5fbaSopenharmony_ci * memory to hold the coap_uri_t structure and its contents. The object should
109c87c5fbaSopenharmony_ci * be released with delete_uri().
110c87c5fbaSopenharmony_ci *
111c87c5fbaSopenharmony_ci * @param uri The coap_uri_t structure to copy.
112c87c5fbaSopenharmony_ci *
113c87c5fbaSopenharmony_ci * @return New URI object or NULL on error.
114c87c5fbaSopenharmony_ci */
115c87c5fbaSopenharmony_cicoap_uri_t *coap_clone_uri(const coap_uri_t *uri);
116c87c5fbaSopenharmony_ci
117c87c5fbaSopenharmony_ci/**
118c87c5fbaSopenharmony_ci * Removes the specified coap_uri_t object.
119c87c5fbaSopenharmony_ci *
120c87c5fbaSopenharmony_ci * @param uri The coap_uri_t structure to remove.
121c87c5fbaSopenharmony_ci */
122c87c5fbaSopenharmony_civoid coap_delete_uri(coap_uri_t *uri);
123c87c5fbaSopenharmony_ci
124c87c5fbaSopenharmony_ci/**
125c87c5fbaSopenharmony_ci * @ingroup application_api
126c87c5fbaSopenharmony_ci * @defgroup uri_parse URI Parsing Functions
127c87c5fbaSopenharmony_ci * API for parsing URIs.
128c87c5fbaSopenharmony_ci * CoAP PDUs contain normalized URIs with their path and query split into
129c87c5fbaSopenharmony_ci * multiple segments. The functions in this module help splitting strings.
130c87c5fbaSopenharmony_ci * @{
131c87c5fbaSopenharmony_ci */
132c87c5fbaSopenharmony_ci
133c87c5fbaSopenharmony_ci/**
134c87c5fbaSopenharmony_ci * Parses a given string into URI components. The identified syntactic
135c87c5fbaSopenharmony_ci * components are stored in the result parameter @p uri. Optional URI
136c87c5fbaSopenharmony_ci * components that are not specified will be set to { 0, 0 }, except for the
137c87c5fbaSopenharmony_ci * port which is set to the default port for the protocol. This function
138c87c5fbaSopenharmony_ci * returns @p 0 if parsing succeeded, a value less than zero otherwise.
139c87c5fbaSopenharmony_ci *
140c87c5fbaSopenharmony_ci * @param str_var The string to split up.
141c87c5fbaSopenharmony_ci * @param len     The actual length of @p str_var
142c87c5fbaSopenharmony_ci * @param uri     The coap_uri_t object to store the result.
143c87c5fbaSopenharmony_ci *
144c87c5fbaSopenharmony_ci * @return        @c 0 on success, or < 0 on error.
145c87c5fbaSopenharmony_ci *
146c87c5fbaSopenharmony_ci */
147c87c5fbaSopenharmony_ciint coap_split_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri);
148c87c5fbaSopenharmony_ci
149c87c5fbaSopenharmony_ci/**
150c87c5fbaSopenharmony_ci * Parses a given string into URI components. The identified syntactic
151c87c5fbaSopenharmony_ci * components are stored in the result parameter @p uri. Optional URI
152c87c5fbaSopenharmony_ci * components that are not specified will be set to { 0, 0 }, except for the
153c87c5fbaSopenharmony_ci * port which is set to default port for the protocol. This function returns
154c87c5fbaSopenharmony_ci * @p 0 if parsing succeeded, a value less than zero otherwise.
155c87c5fbaSopenharmony_ci * Note: This function enforces that the given string is in Proxy-Uri format
156c87c5fbaSopenharmony_ci *       as well as supports different schema such as http and https.
157c87c5fbaSopenharmony_ci *
158c87c5fbaSopenharmony_ci * @param str_var The string to split up.
159c87c5fbaSopenharmony_ci * @param len     The actual length of @p str_var
160c87c5fbaSopenharmony_ci * @param uri     The coap_uri_t object to store the result.
161c87c5fbaSopenharmony_ci *
162c87c5fbaSopenharmony_ci * @return        @c 0 on success, or < 0 on error.
163c87c5fbaSopenharmony_ci *
164c87c5fbaSopenharmony_ci */
165c87c5fbaSopenharmony_ciint coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri);
166c87c5fbaSopenharmony_ci
167c87c5fbaSopenharmony_ci/**
168c87c5fbaSopenharmony_ci * Takes a coap_uri_t and then adds CoAP options into the @p optlist_chain.
169c87c5fbaSopenharmony_ci * If the port is not the default port and create_port_host_opt is not 0, then
170c87c5fbaSopenharmony_ci * the Port option is added to the @p optlist_chain.
171c87c5fbaSopenharmony_ci * If the dst defines an address that does not match the host in uri->host and
172c87c5fbaSopenharmony_ci * is not 0, then the Host option is added to the @p optlist_chain.
173c87c5fbaSopenharmony_ci * Any path or query are broken down into the individual segment Path or Query
174c87c5fbaSopenharmony_ci * options and added to the @p optlist_chain.
175c87c5fbaSopenharmony_ci *
176c87c5fbaSopenharmony_ci * @param uri     The coap_uri_t object.
177c87c5fbaSopenharmony_ci * @param dst     The destination, or NULL if URI_HOST not to be added.
178c87c5fbaSopenharmony_ci * @param optlist_chain Where to store the chain of options.
179c87c5fbaSopenharmony_ci * @param buf     Scratch buffer area (needs to be bigger than
180c87c5fbaSopenharmony_ci *                uri->path.length and uri->query.length)
181c87c5fbaSopenharmony_ci * @param buflen  Size of scratch buffer.
182c87c5fbaSopenharmony_ci * @param create_port_host_opt @c 1 if port/host option to be added
183c87c5fbaSopenharmony_ci *                             (if non-default) else @c 0
184c87c5fbaSopenharmony_ci *
185c87c5fbaSopenharmony_ci * @return        @c 0 on success, or < 0 on error.
186c87c5fbaSopenharmony_ci *
187c87c5fbaSopenharmony_ci */
188c87c5fbaSopenharmony_ciint coap_uri_into_options(const coap_uri_t *uri, const coap_address_t *dst,
189c87c5fbaSopenharmony_ci                          coap_optlist_t **optlist_chain,
190c87c5fbaSopenharmony_ci                          int create_port_host_opt,
191c87c5fbaSopenharmony_ci                          uint8_t *buf, size_t buflen);
192c87c5fbaSopenharmony_ci
193c87c5fbaSopenharmony_ci/**
194c87c5fbaSopenharmony_ci * Splits the given URI path into segments. Each segment is preceded
195c87c5fbaSopenharmony_ci * by an option pseudo-header with delta-value 0 and the actual length
196c87c5fbaSopenharmony_ci * of the respective segment after percent-decoding.
197c87c5fbaSopenharmony_ci *
198c87c5fbaSopenharmony_ci * @param s      The path string to split.
199c87c5fbaSopenharmony_ci * @param length The actual length of @p s.
200c87c5fbaSopenharmony_ci * @param buf    Result buffer for parsed segments.
201c87c5fbaSopenharmony_ci * @param buflen Maximum length of @p buf. Will be set to the actual number
202c87c5fbaSopenharmony_ci *               of bytes written into buf on success.
203c87c5fbaSopenharmony_ci *
204c87c5fbaSopenharmony_ci * @return       The number of segments created or @c -1 on error.
205c87c5fbaSopenharmony_ci */
206c87c5fbaSopenharmony_ciint coap_split_path(const uint8_t *s,
207c87c5fbaSopenharmony_ci                    size_t length,
208c87c5fbaSopenharmony_ci                    unsigned char *buf,
209c87c5fbaSopenharmony_ci                    size_t *buflen);
210c87c5fbaSopenharmony_ci
211c87c5fbaSopenharmony_ci/**
212c87c5fbaSopenharmony_ci * Splits the given URI query into segments. Each segment is preceded
213c87c5fbaSopenharmony_ci * by an option pseudo-header with delta-value 0 and the actual length
214c87c5fbaSopenharmony_ci * of the respective query term.
215c87c5fbaSopenharmony_ci *
216c87c5fbaSopenharmony_ci * @param s      The query string to split.
217c87c5fbaSopenharmony_ci * @param length The actual length of @p s.
218c87c5fbaSopenharmony_ci * @param buf    Result buffer for parsed segments.
219c87c5fbaSopenharmony_ci * @param buflen Maximum length of @p buf. Will be set to the actual number
220c87c5fbaSopenharmony_ci *               of bytes written into buf on success.
221c87c5fbaSopenharmony_ci *
222c87c5fbaSopenharmony_ci * @return       The number of segments created or @c -1 on error.
223c87c5fbaSopenharmony_ci *
224c87c5fbaSopenharmony_ci * @bug This function does not reserve additional space for delta > 12.
225c87c5fbaSopenharmony_ci */
226c87c5fbaSopenharmony_ciint coap_split_query(const uint8_t *s,
227c87c5fbaSopenharmony_ci                     size_t length,
228c87c5fbaSopenharmony_ci                     unsigned char *buf,
229c87c5fbaSopenharmony_ci                     size_t *buflen);
230c87c5fbaSopenharmony_ci
231c87c5fbaSopenharmony_ci/**
232c87c5fbaSopenharmony_ci * Extract query string from request PDU according to escape rules in 6.5.8.
233c87c5fbaSopenharmony_ci * @param request Request PDU.
234c87c5fbaSopenharmony_ci * @return        Reconstructed and escaped query string part or @c NULL if
235c87c5fbaSopenharmony_ci *                no query was contained in @p request. The coap_string_t
236c87c5fbaSopenharmony_ci *                object returned by this function must be released with
237c87c5fbaSopenharmony_ci *                coap_delete_string.
238c87c5fbaSopenharmony_ci */
239c87c5fbaSopenharmony_cicoap_string_t *coap_get_query(const coap_pdu_t *request);
240c87c5fbaSopenharmony_ci
241c87c5fbaSopenharmony_ci/**
242c87c5fbaSopenharmony_ci * Extract uri_path string from request PDU
243c87c5fbaSopenharmony_ci * @param request Request PDU.
244c87c5fbaSopenharmony_ci * @return        Reconstructed and escaped uri path string part or @c NULL
245c87c5fbaSopenharmony_ci *                if no URI-Path was contained in @p request. The
246c87c5fbaSopenharmony_ci *                coap_string_t object returned by this function must be
247c87c5fbaSopenharmony_ci *                released with coap_delete_string.
248c87c5fbaSopenharmony_ci */
249c87c5fbaSopenharmony_cicoap_string_t *coap_get_uri_path(const coap_pdu_t *request);
250c87c5fbaSopenharmony_ci
251c87c5fbaSopenharmony_ci/** @} */
252c87c5fbaSopenharmony_ci
253c87c5fbaSopenharmony_ci#endif /* COAP_URI_H_ */
254