AWS IoT Device Defender  v1.1.1
AWS IoT Device Defender Client Library
defender.h
Go to the documentation of this file.
1 /*
2  * AWS IoT Device Defender Client v1.1.1
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
28 #ifndef DEFENDER_H_
29 #define DEFENDER_H_
30 
31 /* Standard includes. */
32 #include <stdint.h>
33 
34 /* *INDENT-OFF* */
35 #ifdef __cplusplus
36  extern "C" {
37 #endif
38 /* *INDENT-ON* */
39 
40 /* DEFENDER_DO_NOT_USE_CUSTOM_CONFIG allows building the Device Defender library
41  * without a config file. If a config file is provided, DEFENDER_DO_NOT_USE_CUSTOM_CONFIG
42  * macro must not be defined.
43  */
44 #ifndef DEFENDER_DO_NOT_USE_CUSTOM_CONFIG
45  #include "defender_config.h"
46 #endif
47 
48 /* Default config values. */
50 
55 typedef enum
56 {
63 
68 typedef enum
69 {
77  DefenderMaxTopic
79 
80 /*-----------------------------------------------------------*/
81 
85 #define STRING_LITERAL_LENGTH( literal ) ( ( uint16_t ) ( sizeof( literal ) - 1U ) )
86 
87 /*-----------------------------------------------------------*/
88 
93 #define DEFENDER_THINGNAME_MAX_LENGTH 128U
94 
102 #define DEFENDER_REPORT_MIN_PERIOD_SECONDS 300
103 
104 /*-----------------------------------------------------------*/
105 
106 /*
107  * A Defender topic has the following format:
108  *
109  * <Prefix><Thing Name><Bridge><Report Format><Suffix>
110  *
111  * Where:
112  * <Prefix> = $aws/things/
113  * <Thing Name> = Name of the thing.
114  * <Bridge> = /defender/metrics/
115  * <Report Format> = json or cbor
116  * <Suffix> = /accepted or /rejected or empty
117  *
118  * Examples:
119  * $aws/things/THING_NAME/defender/metrics/json
120  * $aws/things/THING_NAME/defender/metrics/json/accepted
121  * $aws/things/THING_NAME/defender/metrics/json/rejected
122  * $aws/things/THING_NAME/defender/metrics/cbor
123  * $aws/things/THING_NAME/defender/metrics/cbor/accepted
124  * $aws/things/THING_NAME/defender/metrics/json/rejected
125  */
126 
132 #define DEFENDER_API_PREFIX "$aws/things/"
133 #define DEFENDER_API_LENGTH_PREFIX STRING_LITERAL_LENGTH( DEFENDER_API_PREFIX )
134 
135 #define DEFENDER_API_BRIDGE "/defender/metrics/"
136 #define DEFENDER_API_LENGTH_BRIDGE STRING_LITERAL_LENGTH( DEFENDER_API_BRIDGE )
137 
138 #define DEFENDER_API_JSON_FORMAT "json"
139 #define DEFENDER_API_LENGTH_JSON_FORMAT STRING_LITERAL_LENGTH( DEFENDER_API_JSON_FORMAT )
140 
141 #define DEFENDER_API_CBOR_FORMAT "cbor"
142 #define DEFENDER_API_LENGTH_CBOR_FORMAT STRING_LITERAL_LENGTH( DEFENDER_API_CBOR_FORMAT )
143 
144 #define DEFENDER_API_ACCEPTED_SUFFIX "/accepted"
145 #define DEFENDER_API_LENGTH_ACCEPTED_SUFFIX STRING_LITERAL_LENGTH( DEFENDER_API_ACCEPTED_SUFFIX )
146 
147 #define DEFENDER_API_REJECTED_SUFFIX "/rejected"
148 #define DEFENDER_API_LENGTH_REJECTED_SUFFIX STRING_LITERAL_LENGTH( DEFENDER_API_REJECTED_SUFFIX )
149 
150 #define DEFENDER_API_NULL_SUFFIX ""
151 #define DEFENDER_API_LENGTH_NULL_SUFFIX ( 0U )
152 
155 /*-----------------------------------------------------------*/
156 
162 /* Defender API topic lengths. */
163 #define DEFENDER_API_COMMON_LENGTH( thingNameLength, reportFormatLength, suffixLength ) \
164  ( DEFENDER_API_LENGTH_PREFIX + \
165  ( thingNameLength ) + \
166  DEFENDER_API_LENGTH_BRIDGE + \
167  ( reportFormatLength ) + \
168  ( suffixLength ) )
169 
177 #define DEFENDER_API_LENGTH_JSON_PUBLISH( thingNameLength ) \
178  DEFENDER_API_COMMON_LENGTH( thingNameLength, \
179  DEFENDER_API_LENGTH_JSON_FORMAT, \
180  DEFENDER_API_LENGTH_NULL_SUFFIX )
181 
187 #define DEFENDER_API_LENGTH_JSON_ACCEPTED( thingNameLength ) \
188  DEFENDER_API_COMMON_LENGTH( thingNameLength, \
189  DEFENDER_API_LENGTH_JSON_FORMAT, \
190  DEFENDER_API_LENGTH_ACCEPTED_SUFFIX )
191 
197 #define DEFENDER_API_LENGTH_JSON_REJECTED( thingNameLength ) \
198  DEFENDER_API_COMMON_LENGTH( thingNameLength, \
199  DEFENDER_API_LENGTH_JSON_FORMAT, \
200  DEFENDER_API_LENGTH_REJECTED_SUFFIX )
201 
207 #define DEFENDER_API_LENGTH_CBOR_PUBLISH( thingNameLength ) \
208  DEFENDER_API_COMMON_LENGTH( thingNameLength, \
209  DEFENDER_API_LENGTH_CBOR_FORMAT, \
210  DEFENDER_API_LENGTH_NULL_SUFFIX )
211 
217 #define DEFENDER_API_LENGTH_CBOR_ACCEPTED( thingNameLength ) \
218  DEFENDER_API_COMMON_LENGTH( thingNameLength, \
219  DEFENDER_API_LENGTH_CBOR_FORMAT, \
220  DEFENDER_API_LENGTH_ACCEPTED_SUFFIX )
221 
227 #define DEFENDER_API_LENGTH_CBOR_REJECTED( thingNameLength ) \
228  DEFENDER_API_COMMON_LENGTH( thingNameLength, \
229  DEFENDER_API_LENGTH_CBOR_FORMAT, \
230  DEFENDER_API_LENGTH_REJECTED_SUFFIX )
231 
237 #define DEFENDER_API_MAX_LENGTH( thingNameLength ) \
238  DEFENDER_API_LENGTH_CBOR_ACCEPTED( thingNameLength )
239 
240 /*-----------------------------------------------------------*/
241 
247 /* Defender API topics. */
248 #define DEFENDER_API_COMMON( thingName, reportFormat, suffix ) \
249  ( DEFENDER_API_PREFIX \
250  thingName \
251  DEFENDER_API_BRIDGE \
252  reportFormat \
253  suffix )
254 
266 #define DEFENDER_API_JSON_PUBLISH( thingName ) \
267  DEFENDER_API_COMMON( thingName, \
268  DEFENDER_API_JSON_FORMAT, \
269  DEFENDER_API_NULL_SUFFIX )
270 
280 #define DEFENDER_API_JSON_ACCEPTED( thingName ) \
281  DEFENDER_API_COMMON( thingName, \
282  DEFENDER_API_JSON_FORMAT, \
283  DEFENDER_API_ACCEPTED_SUFFIX )
284 
294 #define DEFENDER_API_JSON_REJECTED( thingName ) \
295  DEFENDER_API_COMMON( thingName, \
296  DEFENDER_API_JSON_FORMAT, \
297  DEFENDER_API_REJECTED_SUFFIX )
298 
308 #define DEFENDER_API_CBOR_PUBLISH( thingName ) \
309  DEFENDER_API_COMMON( thingName, \
310  DEFENDER_API_CBOR_FORMAT, \
311  DEFENDER_API_NULL_SUFFIX )
312 
322 #define DEFENDER_API_CBOR_ACCEPTED( thingName ) \
323  DEFENDER_API_COMMON( thingName, \
324  DEFENDER_API_CBOR_FORMAT, \
325  DEFENDER_API_ACCEPTED_SUFFIX )
326 
336 #define DEFENDER_API_CBOR_REJECTED( thingName ) \
337  DEFENDER_API_COMMON( thingName, \
338  DEFENDER_API_CBOR_FORMAT, \
339  DEFENDER_API_REJECTED_SUFFIX )
340 
341 /*-----------------------------------------------------------*/
342 
348 /* Keys used in defender report. */
349 #if ( defined( DEFENDER_USE_LONG_KEYS ) && ( DEFENDER_USE_LONG_KEYS == 1 ) )
350  #define DEFENDER_REPORT_SELECT_KEY( longKey, shortKey ) longKey
351 #else
352  #define DEFENDER_REPORT_SELECT_KEY( longKey, shortKey ) shortKey
353 #endif
354 
361 #define DEFENDER_REPORT_HEADER_KEY DEFENDER_REPORT_SELECT_KEY( "header", "hed" )
362 
367 #define DEFENDER_REPORT_LENGTH_HEADER_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_HEADER_KEY )
368 
373 #define DEFENDER_REPORT_METRICS_KEY DEFENDER_REPORT_SELECT_KEY( "metrics", "met" )
374 
379 #define DEFENDER_REPORT_LENGTH_METRICS_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_METRICS_KEY )
380 
385 #define DEFENDER_REPORT_ID_KEY DEFENDER_REPORT_SELECT_KEY( "report_id", "rid" )
386 
391 #define DEFENDER_REPORT_LENGTH_ID_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_ID_KEY )
392 
397 #define DEFENDER_REPORT_VERSION_KEY DEFENDER_REPORT_SELECT_KEY( "version", "v" )
398 
403 #define DEFENDER_REPORT_LENGTH_VERSION_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_VERSION_KEY )
404 
409 #define DEFENDER_REPORT_TCP_CONNECTIONS_KEY DEFENDER_REPORT_SELECT_KEY( "tcp_connections", "tc" )
410 
415 #define DEFENDER_REPORT_LENGTH_TCP_CONNECTIONS_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_TCP_CONNECTIONS_KEY )
416 
421 #define DEFENDER_REPORT_ESTABLISHED_CONNECTIONS_KEY DEFENDER_REPORT_SELECT_KEY( "established_connections", "ec" )
422 
427 #define DEFENDER_REPORT_LENGTH_ESTABLISHED_CONNECTIONS_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_ESTABLISHED_CONNECTIONS_KEY )
428 
433 #define DEFENDER_REPORT_CONNECTIONS_KEY DEFENDER_REPORT_SELECT_KEY( "connections", "cs" )
434 
439 #define DEFENDER_REPORT_LENGTH_CONNECTIONS_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_CONNECTIONS_KEY )
440 
445 #define DEFENDER_REPORT_REMOTE_ADDR_KEY DEFENDER_REPORT_SELECT_KEY( "remote_addr", "rad" )
446 
451 #define DEFENDER_REPORT_LENGTH_REMOTE_ADDR_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_REMOTE_ADDR_KEY )
452 
457 #define DEFENDER_REPORT_LOCAL_PORT_KEY DEFENDER_REPORT_SELECT_KEY( "local_port", "lp" )
458 
463 #define DEFENDER_REPORT_LENGTH_LOCAL_PORT_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_LOCAL_PORT_KEY )
464 
469 #define DEFENDER_REPORT_LOCAL_INTERFACE_KEY DEFENDER_REPORT_SELECT_KEY( "local_interface", "li" )
470 
475 #define DEFENDER_REPORT_LENGTH_LOCAL_INTERFACE_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_LOCAL_INTERFACE_KEY )
476 
481 #define DEFENDER_REPORT_TOTAL_KEY DEFENDER_REPORT_SELECT_KEY( "total", "t" )
482 
487 #define DEFENDER_REPORT_LENGTH_TOTAL_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_TOTAL_KEY )
488 
493 #define DEFENDER_REPORT_TCP_LISTENING_PORTS_KEY DEFENDER_REPORT_SELECT_KEY( "listening_tcp_ports", "tp" )
494 
499 #define DEFENDER_REPORT_LENGTH_TCP_LISTENING_PORTS_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_TCP_LISTENING_PORTS_KEY )
500 
505 #define DEFENDER_REPORT_PORTS_KEY DEFENDER_REPORT_SELECT_KEY( "ports", "pts" )
506 
511 #define DEFENDER_REPORT_LENGTH_PORTS_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_PORTS_KEY )
512 
517 #define DEFENDER_REPORT_PORT_KEY DEFENDER_REPORT_SELECT_KEY( "port", "pt" )
518 
523 #define DEFENDER_REPORT_LENGTH_PORT_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_PORT_KEY )
524 
529 #define DEFENDER_REPORT_INTERFACE_KEY DEFENDER_REPORT_SELECT_KEY( "interface", "if" )
530 
535 #define DEFENDER_REPORT_LENGTH_INTERFACE_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_INTERFACE_KEY )
536 
541 #define DEFENDER_REPORT_UDP_LISTENING_PORTS_KEY DEFENDER_REPORT_SELECT_KEY( "listening_udp_ports", "up" )
542 
547 #define DEFENDER_REPORT_LENGTH_UDP_LISTENING_PORTS_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_UDP_LISTENING_PORTS_KEY )
548 
553 #define DEFENDER_REPORT_NETWORK_STATS_KEY DEFENDER_REPORT_SELECT_KEY( "network_stats", "ns" )
554 
559 #define DEFENDER_REPORT_LENGTH_NETWORK_STATS_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_NETWORK_STATS_KEY )
560 
565 #define DEFENDER_REPORT_BYTES_IN_KEY DEFENDER_REPORT_SELECT_KEY( "bytes_in", "bi" )
566 
571 #define DEFENDER_REPORT_LENGTH_BYTES_IN_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_BYTES_IN_KEY )
572 
577 #define DEFENDER_REPORT_BYTES_OUT_KEY DEFENDER_REPORT_SELECT_KEY( "bytes_out", "bo" )
578 
583 #define DEFENDER_REPORT_LENGTH_BYTES_OUT_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_BYTES_OUT_KEY )
584 
589 #define DEFENDER_REPORT_PKTS_IN_KEY DEFENDER_REPORT_SELECT_KEY( "packets_in", "pi" )
590 
595 #define DEFENDER_REPORT_LENGTH_PKTS_IN_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_PKTS_IN_KEY )
596 
601 #define DEFENDER_REPORT_PKTS_OUT_KEY DEFENDER_REPORT_SELECT_KEY( "packets_out", "po" )
602 
607 #define DEFENDER_REPORT_LENGTH_PKS_OUT_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_LENGTH_PKS_OUT_KEY )
608 
613 #define DEFENDER_REPORT_CUSTOM_METRICS_KEY DEFENDER_REPORT_SELECT_KEY( "custom_metrics", "cmet" )
614 
619 #define DEFENDER_REPORT_LENGTH_CUSTOM_METRICS_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_LENGTH_CUSTOM_METRICS_KEY )
620 
625 #define DEFENDER_REPORT_NUMBER_KEY "number"
626 
631 #define DEFENDER_REPORT_LENGTH_NUMBER_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_LENGTH_NUMBER_KEY )
632 
637 #define DEFENDER_REPORT_NUMBER_LIST_KEY "number_list"
638 
643 #define DEFENDER_REPORT_LENGTH_NUMBER_LIST_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_LENGTH_NUMBER_LIST_KEY )
644 
649 #define DEFENDER_REPORT_STRING_LIST_KEY "string_list"
650 
655 #define DEFENDER_REPORT_LENGTH_STRING_LIST_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_LENGTH_STRING_LIST_KEY )
656 
661 #define DEFENDER_REPORT_IP_LIST_KEY "ip_list"
662 
667 #define DEFENDER_REPORT_LENGTH_IP_LIST_KEY STRING_LITERAL_LENGTH( DEFENDER_REPORT_LENGTH_IP_LIST_KEY )
668 
669 /*-----------------------------------------------------------*/
670 
717 /* @[declare_defender_gettopic] */
718 DefenderStatus_t Defender_GetTopic( char * pBuffer,
719  uint16_t bufferLength,
720  const char * pThingName,
721  uint16_t thingNameLength,
722  DefenderTopic_t api,
723  uint16_t * pOutLength );
724 /* @[declare_defender_gettopic] */
725 
726 /*-----------------------------------------------------------*/
727 
787 /* @[declare_defender_matchtopic] */
788 DefenderStatus_t Defender_MatchTopic( const char * pTopic,
789  uint16_t topicLength,
790  DefenderTopic_t * pOutApi,
791  const char ** ppOutThingName,
792  uint16_t * pOutThingNameLength );
793 /* @[declare_defender_matchtopic] */
794 
795 /*-----------------------------------------------------------*/
796 
797 /* *INDENT-OFF* */
798 #ifdef __cplusplus
799  }
800 #endif
801 /* *INDENT-ON* */
802 
803 #endif /* DEFENDER_H_ */
DefenderError
@ DefenderError
Definition: defender.h:57
DefenderSuccess
@ DefenderSuccess
Definition: defender.h:58
DefenderJsonReportRejected
@ DefenderJsonReportRejected
Definition: defender.h:73
DefenderJsonReportPublish
@ DefenderJsonReportPublish
Definition: defender.h:71
DefenderJsonReportAccepted
@ DefenderJsonReportAccepted
Definition: defender.h:72
defender_config_defaults.h
Default config values for the AWS IoT Device Defender Client Library.
DefenderInvalidTopic
@ DefenderInvalidTopic
Definition: defender.h:70
Defender_GetTopic
DefenderStatus_t Defender_GetTopic(char *pBuffer, uint16_t bufferLength, const char *pThingName, uint16_t thingNameLength, DefenderTopic_t api, uint16_t *pOutLength)
Populate the topic string for a Device Defender operation.
Definition: defender.c:381
DefenderStatus_t
DefenderStatus_t
Return codes from defender APIs.
Definition: defender.h:56
DefenderBufferTooSmall
@ DefenderBufferTooSmall
Definition: defender.h:61
DefenderCborReportPublish
@ DefenderCborReportPublish
Definition: defender.h:74
DefenderCborReportAccepted
@ DefenderCborReportAccepted
Definition: defender.h:75
DefenderTopic_t
DefenderTopic_t
Topic values for subscription requests.
Definition: defender.h:69
DefenderBadParameter
@ DefenderBadParameter
Definition: defender.h:60
DefenderCborReportRejected
@ DefenderCborReportRejected
Definition: defender.h:76
DefenderNoMatch
@ DefenderNoMatch
Definition: defender.h:59
Defender_MatchTopic
DefenderStatus_t Defender_MatchTopic(const char *pTopic, uint16_t topicLength, DefenderTopic_t *pOutApi, const char **ppOutThingName, uint16_t *pOutThingNameLength)
Check if the given topic is one of the Device Defender topics.
Definition: defender.c:462