ROS 2 rclcpp + rcl - rolling  rolling-e7363f10
ROS 2 C++ Client Library with ROS Client Library
validate_enclave_name.c
1 // Copyright 2020 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <stddef.h>
18 #include <string.h>
19 
20 #include <rcutils/macros.h>
21 #include <rcutils/snprintf.h>
22 
23 #include "rmw/validate_namespace.h"
24 
25 #include "rcl/error_handling.h"
26 #include "rcl/types.h"
27 
28 #include "rmw/ret_types.h"
29 
30 #include "./common.h"
31 
34  const char * enclave,
35  int * validation_result,
36  size_t * invalid_index)
37 {
38  RCL_CHECK_ARGUMENT_FOR_NULL(enclave, RCL_RET_INVALID_ARGUMENT);
40  enclave, strlen(enclave), validation_result, invalid_index);
41 }
42 
45  const char * enclave,
46  size_t enclave_length,
47  int * validation_result,
48  size_t * invalid_index)
49 {
50  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_INVALID_ARGUMENT);
51  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_ERROR);
52 
53  RCL_CHECK_ARGUMENT_FOR_NULL(enclave, RCL_RET_INVALID_ARGUMENT);
54  RCL_CHECK_ARGUMENT_FOR_NULL(validation_result, RCL_RET_INVALID_ARGUMENT);
55 
56  int tmp_validation_result;
57  size_t tmp_invalid_index;
58  rmw_ret_t ret = rmw_validate_namespace_with_size(
59  enclave, enclave_length, &tmp_validation_result, &tmp_invalid_index);
60  if (ret != RMW_RET_OK) {
61  // rmw_validate_namespace_with_size already set the error
62  return rcl_convert_rmw_ret_to_rcl_ret(ret);
63  }
64 
65  if (tmp_validation_result != RMW_NAMESPACE_VALID &&
66  tmp_validation_result != RMW_NAMESPACE_INVALID_TOO_LONG)
67  {
68  switch (tmp_validation_result) {
69  case RMW_NAMESPACE_INVALID_IS_EMPTY_STRING:
70  *validation_result = RCL_ENCLAVE_NAME_INVALID_IS_EMPTY_STRING;
71  break;
72  case RMW_NAMESPACE_INVALID_NOT_ABSOLUTE:
73  *validation_result = RCL_ENCLAVE_NAME_INVALID_NOT_ABSOLUTE;
74  break;
75  case RMW_NAMESPACE_INVALID_ENDS_WITH_FORWARD_SLASH:
77  break;
78  case RMW_NAMESPACE_INVALID_CONTAINS_UNALLOWED_CHARACTERS:
80  break;
81  case RMW_NAMESPACE_INVALID_CONTAINS_REPEATED_FORWARD_SLASH:
83  break;
84  case RMW_NAMESPACE_INVALID_NAME_TOKEN_STARTS_WITH_NUMBER:
86  break;
87  default:
88  {
89  char default_err_msg[256];
90  // explicitly not taking return value which is number of bytes written
91  int ret = rcutils_snprintf(
92  default_err_msg, sizeof(default_err_msg),
93  "rcl_validate_enclave_name_with_size(): "
94  "unknown rmw_validate_namespace_with_size() result '%d'",
95  tmp_validation_result);
96  if (ret < 0) {
97  RCL_SET_ERROR_MSG(
98  "rcl_validate_enclave_name_with_size(): "
99  "rcutils_snprintf() failed while reporting an unknown validation result");
100  } else {
101  RCL_SET_ERROR_MSG(default_err_msg);
102  }
103  }
104  return RCL_RET_ERROR;
105  }
106  if (invalid_index) {
107  *invalid_index = tmp_invalid_index;
108  }
109  return RCL_RET_OK;
110  }
111 
112  // enclave might be longer that namespace length, check false positives and correct
113  if (RMW_NAMESPACE_INVALID_TOO_LONG == tmp_validation_result) {
114  if (RCL_ENCLAVE_NAME_MAX_LENGTH >= enclave_length) {
115  *validation_result = RCL_ENCLAVE_NAME_VALID;
116  } else {
117  *validation_result = RCL_ENCLAVE_NAME_INVALID_TOO_LONG;
118  if (invalid_index) {
119  *invalid_index = RCL_ENCLAVE_NAME_MAX_LENGTH - 1;
120  }
121  }
122  return RCL_RET_OK;
123  }
124 
125  // everything was ok, set result to valid namespace, avoid setting invalid_index, and return
126  *validation_result = RCL_ENCLAVE_NAME_VALID;
127  return RCL_RET_OK;
128 }
129 
130 const char *
132 {
133  switch (validation_result) {
135  return NULL;
137  return "context name must not be empty";
139  return "context name must be absolute, it must lead with a '/'";
141  return "context name must not end with a '/', unless only a '/'";
143  return "context name must not contain characters other than alphanumerics, '_', or '/'";
145  return "context name must not contain repeated '/'";
147  return "context name must not have a token that starts with a number";
149  return "context name should not exceed '"
150  RCUTILS_STRINGIFY(RCL_ENCLAVE_NAME_MAX_NAME_LENGTH) "'";
151  default:
152  return "unknown result code for rcl context name validation";
153  }
154 }
#define RCL_RET_OK
Success return code.
Definition: types.h:28
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:36
#define RCL_RET_ERROR
Unspecified error return code.
Definition: types.h:30
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:25
#define RCL_ENCLAVE_NAME_INVALID_NOT_ABSOLUTE
The enclave name is invalid because it is not absolute.
#define RCL_ENCLAVE_NAME_VALID
The enclave name is valid.
#define RCL_ENCLAVE_NAME_INVALID_NAME_TOKEN_STARTS_WITH_NUMBER
The enclave name is invalid because one of the tokens starts with a number.
#define RCL_ENCLAVE_NAME_INVALID_TOO_LONG
The enclave name is invalid because the name is too long.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_validate_enclave_name_with_size(const char *enclave, size_t enclave_length, int *validation_result, size_t *invalid_index)
Determine if a given enclave name is valid.
#define RCL_ENCLAVE_NAME_MAX_LENGTH
The maximum length of an enclave name.
#define RCL_ENCLAVE_NAME_INVALID_ENDS_WITH_FORWARD_SLASH
The enclave name is invalid because it ends with a forward slash.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_validate_enclave_name(const char *enclave, int *validation_result, size_t *invalid_index)
Determine if a given enclave name is valid.
#define RCL_ENCLAVE_NAME_INVALID_CONTAINS_UNALLOWED_CHARACTERS
The enclave name is invalid because it has characters that are not allowed.
#define RCL_ENCLAVE_NAME_INVALID_IS_EMPTY_STRING
The enclave name is invalid because it is an empty string.
#define RCL_ENCLAVE_NAME_INVALID_CONTAINS_REPEATED_FORWARD_SLASH
The enclave name is invalid because it contains repeated forward slashes.
RCL_PUBLIC RCL_WARN_UNUSED const char * rcl_enclave_name_validation_result_string(int validation_result)
Return a validation result description, or NULL if unknown or RCL_ENCLAVE_NAME_VALID.