4 * @brief Implementation of parser_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 #include <arpa/inet.h>
30 #include <definitions.h>
32 #include <utils/logger.h>
33 #include <utils/linked_list.h>
34 #include <encoding/payloads/encodings.h>
35 #include <encoding/payloads/payload.h>
36 #include <encoding/payloads/sa_payload.h>
37 #include <encoding/payloads/proposal_substructure.h>
38 #include <encoding/payloads/transform_substructure.h>
39 #include <encoding/payloads/transform_attribute.h>
40 #include <encoding/payloads/ke_payload.h>
41 #include <encoding/payloads/nonce_payload.h>
42 #include <encoding/payloads/id_payload.h>
43 #include <encoding/payloads/notify_payload.h>
44 #include <encoding/payloads/encryption_payload.h>
45 #include <encoding/payloads/auth_payload.h>
46 #include <encoding/payloads/cert_payload.h>
47 #include <encoding/payloads/certreq_payload.h>
48 #include <encoding/payloads/ts_payload.h>
49 #include <encoding/payloads/delete_payload.h>
50 #include <encoding/payloads/vendor_id_payload.h>
51 #include <encoding/payloads/cp_payload.h>
52 #include <encoding/payloads/configuration_attribute.h>
53 #include <encoding/payloads/eap_payload.h>
54 #include <encoding/payloads/unknown_payload.h>
57 typedef struct private_parser_t private_parser_t
;
60 * Private data stored in a context.
62 * Contains pointers and counters to store current state.
64 struct private_parser_t
{
66 * Public members, see parser_t.
71 * @brief Parse a 4-Bit unsigned integer from the current parsing position.
73 * @param this parser_t object
74 * @param rule_number number of current rule
75 * @param[out] output_pos pointer where to write the parsed result
78 * - PARSE_ERROR when not successful
80 status_t (*parse_uint4
) (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
);
83 * @brief Parse a 8-Bit unsigned integer from the current parsing position.
85 * @param this parser_t object
86 * @param rule_number number of current rule
87 * @param[out] output_pos pointer where to write the parsed result
90 * - PARSE_ERROR when not successful
92 status_t (*parse_uint8
) (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
);
95 * @brief Parse a 15-Bit unsigned integer from the current parsing position.
97 * This is a special case used for ATTRIBUTE_TYPE.
98 * Big-/Little-endian conversion is done here.
100 * @param this parser_t object
101 * @param rule_number number of current rule
102 * @param[out] output_pos pointer where to write the parsed result
105 * - PARSE_ERROR when not successful
107 status_t (*parse_uint15
) (private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
);
110 * @brief Parse a 16-Bit unsigned integer from the current parsing position.
112 * Big-/Little-endian conversion is done here.
114 * @param this parser_t object
115 * @param rule_number number of current rule
116 * @param[out] output_pos pointer where to write the parsed result
119 * - PARSE_ERROR when not successful
121 status_t (*parse_uint16
) (private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
);
124 * @brief Parse a 32-Bit unsigned integer from the current parsing position.
126 * Big-/Little-endian conversion is done here.
128 * @param this parser_t object
129 * @param rule_number number of current rule
130 * @param[out] output_pos pointer where to write the parsed result
133 * - PARSE_ERROR when not successful
135 status_t (*parse_uint32
) (private_parser_t
*this, int rule_number
, u_int32_t
*output_pos
);
138 * @brief Parse a 64-Bit unsigned integer from the current parsing position.
140 * @todo add support for big-endian machines.
142 * @param this parser_t object
143 * @param rule_number number of current rule
144 * @param[out] output_pos pointer where to write the parsed result
147 * - PARSE_ERROR when not successful
149 status_t (*parse_uint64
) (private_parser_t
*this, int rule_number
, u_int64_t
*output_pos
);
152 * @brief Parse a given amount of bytes and writes them to a specific location
154 * @param this parser_t object
155 * @param rule_number number of current rule
156 * @param[out] output_pos pointer where to write the parsed result
157 * @param bytes number of bytes to parse
160 * - PARSE_ERROR when not successful
162 status_t (*parse_bytes
) (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
,size_t bytes
);
165 * @brief Parse a single Bit from the current parsing position
167 * @param this parser_t object
168 * @param rule_number number of current rule
169 * @param[out] output_pos pointer where to write the parsed result
172 * - PARSE_ERROR when not successful
174 status_t (*parse_bit
) (private_parser_t
*this, int rule_number
, bool *output_pos
);
177 * @brief Parse substructures in a list
179 * This function calls the parser recursivly to parse contained substructures
180 * in a linked_list_t. The list must already be created. Payload defines
181 * the type of the substructures. parsing is continued until the specified length
182 * is completely parsed.
184 * @param this parser_t object
185 * @param rule_number number of current rule
186 * @param[out] output_pos pointer of a linked_list where substructures are added
187 * @param payload_type type of the contained substructures to parse
188 * @param length number of bytes to parse in this list
191 * - PARSE_ERROR when not successful
193 status_t (*parse_list
) (private_parser_t
*this, int rule_number
, linked_list_t
**output_pos
, payload_type_t payload_ype
, size_t length
);
196 * @brief Parse data from current parsing position in a chunk.
198 * This function clones length number of bytes to output_pos, without
199 * modifiyng them. Space will be allocated and must be freed by caller.
201 * @param this parser_t object
202 * @param rule_number number of current rule
203 * @param[out] output_pos pointer of a chunk which will point to the allocated data
204 * @param length number of bytes to clone
207 * - PARSE_ERROR when not successful
209 status_t (*parse_chunk
) (private_parser_t
*this, int rule_number
, chunk_t
*output_pos
, size_t length
);
212 * Current bit for reading in input data.
217 * Current byte for reading in input data.
222 * Input data to parse.
227 * Roof of input, used for length-checking.
229 u_int8_t
*input_roof
;
232 * Set of encoding rules for this parsing session.
234 encoding_rule_t
*rules
;
237 * Assigned logger_t object.
243 * Implementation of private_parser_t.parse_uint4.
245 static status_t
parse_uint4(private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
)
247 if (this->byte_pos
+ sizeof(u_int8_t
) > this->input_roof
)
249 this->logger
->log(this->logger
, ERROR
|LEVEL1
, " not enough input to parse rule %d %s",
250 rule_number
, mapping_find(encoding_type_m
,
251 this->rules
[rule_number
].type
));
254 switch (this->bit_pos
)
257 /* caller interested in result ? */
258 if (output_pos
!= NULL
)
260 *output_pos
= *(this->byte_pos
) >> 4;
265 /* caller interested in result ? */
266 if (output_pos
!= NULL
)
268 *output_pos
= *(this->byte_pos
) & 0x0F;
274 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
275 rule_number
, mapping_find(encoding_type_m
,
276 this->rules
[rule_number
].type
), this->bit_pos
);
280 if (output_pos
!= NULL
)
282 this->logger
->log(this->logger
, RAW
|LEVEL2
, " => %d", *output_pos
);
289 * Implementation of private_parser_t.parse_uint8.
291 static status_t
parse_uint8(private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
)
293 if (this->byte_pos
+ sizeof(u_int8_t
) > this->input_roof
)
295 this->logger
->log(this->logger
, ERROR
|LEVEL1
, " not enough input to parse rule %d %s",
296 rule_number
, mapping_find(encoding_type_m
,
297 this->rules
[rule_number
].type
));
302 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
303 rule_number
, mapping_find(encoding_type_m
,
304 this->rules
[rule_number
].type
), this->bit_pos
);
308 /* caller interested in result ? */
309 if (output_pos
!= NULL
)
311 *output_pos
= *(this->byte_pos
);
312 this->logger
->log(this->logger
, RAW
|LEVEL2
, " => %d", *output_pos
);
320 * Implementation of private_parser_t.parse_uint15.
322 static status_t
parse_uint15(private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
)
324 if (this->byte_pos
+ sizeof(u_int16_t
) > this->input_roof
)
326 this->logger
->log(this->logger
, ERROR
|LEVEL1
, " not enough input to parse rule %d %s",
327 rule_number
, mapping_find(encoding_type_m
,
328 this->rules
[rule_number
].type
));
331 if (this->bit_pos
!= 1)
333 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
334 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
338 /* caller interested in result ? */
339 if (output_pos
!= NULL
)
341 *output_pos
= ntohs(*((u_int16_t
*)this->byte_pos
)) & ~0x8000;
342 this->logger
->log(this->logger
, RAW
|LEVEL2
, " => %d", *output_pos
);
351 * Implementation of private_parser_t.parse_uint16.
353 static status_t
parse_uint16(private_parser_t
*this, int rule_number
, u_int16_t
*output_pos
)
355 if (this->byte_pos
+ sizeof(u_int16_t
) > this->input_roof
)
357 this->logger
->log(this->logger
, ERROR
|LEVEL1
, " not enough input to parse rule %d %s",
358 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
363 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
364 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
368 /* caller interested in result ? */
369 if (output_pos
!= NULL
)
371 *output_pos
= ntohs(*((u_int16_t
*)this->byte_pos
));
373 this->logger
->log(this->logger
, RAW
|LEVEL2
, " => %d", *output_pos
);
380 * Implementation of private_parser_t.parse_uint32.
382 static status_t
parse_uint32(private_parser_t
*this, int rule_number
, u_int32_t
*output_pos
)
384 if (this->byte_pos
+ sizeof(u_int32_t
) > this->input_roof
)
386 this->logger
->log(this->logger
, ERROR
|LEVEL1
, " not enough input to parse rule %d %s",
387 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
392 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
393 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
397 /* caller interested in result ? */
398 if (output_pos
!= NULL
)
400 *output_pos
= ntohl(*((u_int32_t
*)this->byte_pos
));
402 this->logger
->log(this->logger
, RAW
|LEVEL2
, " => %d", *output_pos
);
410 * Implementation of private_parser_t.parse_uint64.
412 static status_t
parse_uint64(private_parser_t
*this, int rule_number
, u_int64_t
*output_pos
)
414 if (this->byte_pos
+ sizeof(u_int64_t
) > this->input_roof
)
416 this->logger
->log(this->logger
, ERROR
|LEVEL1
, " not enough input to parse rule %d %s",
417 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
422 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
423 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
427 /* caller interested in result ? */
428 if (output_pos
!= NULL
)
430 /* assuming little endian host order */
431 *(output_pos
+ 1) = ntohl(*((u_int32_t
*)this->byte_pos
));
432 *output_pos
= ntohl(*(((u_int32_t
*)this->byte_pos
) + 1));
434 this->logger
->log_bytes(this->logger
, RAW
|LEVEL2
, " =>", (void*)output_pos
, 8);
442 * Implementation of private_parser_t.parse_bytes.
444 static status_t
parse_bytes (private_parser_t
*this, int rule_number
, u_int8_t
*output_pos
,size_t bytes
)
446 if (this->byte_pos
+ bytes
> this->input_roof
)
448 this->logger
->log(this->logger
, ERROR
|LEVEL1
, " not enough input to parse rule %d %s",
449 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
454 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
455 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
),
460 /* caller interested in result ? */
461 if (output_pos
!= NULL
)
463 memcpy(output_pos
,this->byte_pos
,bytes
);
465 this->logger
->log_bytes(this->logger
, RAW
|LEVEL2
, " =>", (void*)output_pos
, bytes
);
467 this->byte_pos
+= bytes
;
473 * Implementation of private_parser_t.parse_bit.
475 static status_t
parse_bit(private_parser_t
*this, int rule_number
, bool *output_pos
)
477 if (this->byte_pos
+ sizeof(u_int8_t
) > this->input_roof
)
479 this->logger
->log(this->logger
, ERROR
|LEVEL1
, " not enough input to parse rule %d %s",
480 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
483 /* caller interested in result ? */
484 if (output_pos
!= NULL
)
487 mask
= 0x01 << (7 - this->bit_pos
);
488 *output_pos
= *this->byte_pos
& mask
;
492 /* set to a "clean", comparable true */
496 this->logger
->log(this->logger
, RAW
|LEVEL2
, " => %d", *output_pos
);
498 this->bit_pos
= (this->bit_pos
+ 1) % 8;
499 if (this->bit_pos
== 0)
508 * Implementation of private_parser_t.parse_list.
510 static status_t
parse_list(private_parser_t
*this, int rule_number
, linked_list_t
**output_pos
, payload_type_t payload_type
, size_t length
)
512 linked_list_t
* list
= *output_pos
;
516 this->logger
->log(this->logger
, ERROR
|LEVEL1
, " invalid length for rule %d %s",
517 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
523 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
524 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
), this->bit_pos
);
530 u_int8_t
*pos_before
= this->byte_pos
;
533 this->logger
->log(this->logger
, CONTROL
|LEVEL1
, " %d bytes left, parsing recursivly %s",
534 length
, mapping_find(payload_type_m
, payload_type
));
535 status
= this->public.parse_payload((parser_t
*)this, payload_type
, &payload
);
536 if (status
!= SUCCESS
)
538 this->logger
->log(this->logger
, ERROR
, " parsing of a %s substructure failed",
539 mapping_find(payload_type_m
, payload_type
));
542 list
->insert_last(list
, payload
);
543 length
-= this->byte_pos
- pos_before
;
550 * Implementation of private_parser_t.parse_chunk.
552 static status_t
parse_chunk(private_parser_t
*this, int rule_number
, chunk_t
*output_pos
, size_t length
)
554 if (this->byte_pos
+ length
> this->input_roof
)
556 this->logger
->log(this->logger
, ERROR
, " not enough input (%d bytes) to parse rule %d %s",
557 length
, rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
));
562 this->logger
->log(this->logger
, ERROR
, " found rule %d %s on bitpos %d",
563 rule_number
, mapping_find(encoding_type_m
, this->rules
[rule_number
].type
), this->bit_pos
);
566 if (output_pos
!= NULL
)
568 output_pos
->len
= length
;
569 output_pos
->ptr
= malloc(length
);
570 memcpy(output_pos
->ptr
, this->byte_pos
, length
);
572 this->byte_pos
+= length
;
573 this->logger
->log_bytes(this->logger
, RAW
|LEVEL2
, " =>", (void*)output_pos
->ptr
, length
);
579 * Implementation of parser_t.parse_payload.
581 static status_t
parse_payload(private_parser_t
*this, payload_type_t payload_type
, payload_t
**payload
)
585 size_t rule_count
, payload_length
, spi_size
, attribute_length
;
587 bool attribute_format
;
589 encoding_rule_t
*rule
;
591 /* create instance of the payload to parse */
592 pld
= payload_create(payload_type
);
594 this->logger
->log(this->logger
, CONTROL
|LEVEL1
, "parsing %s payload, %d bytes left",
595 mapping_find(payload_type_m
, payload_type
),
596 this->input_roof
-this->byte_pos
);
598 this->logger
->log_bytes(this->logger
, RAW
|LEVEL3
, "parsing payload from", this->byte_pos
,
599 this->input_roof
-this->byte_pos
);
601 if (pld
->get_type(pld
) == UNKNOWN_PAYLOAD
)
603 this->logger
->log(this->logger
, ERROR
|LEVEL1
, " payload type %d is unknown, handling as %s",
604 payload_type
, mapping_find(payload_type_m
, UNKNOWN_PAYLOAD
));
607 /* base pointer for output, avoids casting in every rule */
610 /* parse the payload with its own rulse */
611 pld
->get_encoding_rules(pld
, &(this->rules
), &rule_count
);
612 for (rule_number
= 0; rule_number
< rule_count
; rule_number
++)
614 rule
= &(this->rules
[rule_number
]);
615 this->logger
->log(this->logger
, CONTROL
|LEVEL2
, " parsing rule %d %s",
616 rule_number
, mapping_find(encoding_type_m
, rule
->type
));
621 if (this->parse_uint4(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
630 if (this->parse_uint8(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
639 if (this->parse_uint16(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
648 if (this->parse_uint32(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
657 if (this->parse_uint64(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
666 if (this->parse_bytes(this, rule_number
, output
+ rule
->offset
,8) != SUCCESS
)
675 if (this->parse_bit(this, rule_number
, NULL
) != SUCCESS
)
684 if (this->parse_uint8(this, rule_number
, NULL
) != SUCCESS
)
693 if (this->parse_bit(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
702 if (this->parse_uint16(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
707 payload_length
= *(u_int16_t
*)(output
+ rule
->offset
);
712 if (this->parse_uint32(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
721 if (this->parse_uint8(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
726 spi_size
= *(u_int8_t
*)(output
+ rule
->offset
);
731 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, spi_size
) != SUCCESS
)
740 size_t proposals_length
= payload_length
- SA_PAYLOAD_HEADER_LENGTH
;
741 if (this->parse_list(this, rule_number
, output
+ rule
->offset
, PROPOSAL_SUBSTRUCTURE
, proposals_length
) != SUCCESS
)
750 size_t transforms_length
= payload_length
- spi_size
- PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH
;
751 if (this->parse_list(this, rule_number
, output
+ rule
->offset
, TRANSFORM_SUBSTRUCTURE
, transforms_length
) != SUCCESS
)
758 case TRANSFORM_ATTRIBUTES
:
760 size_t transform_a_length
= payload_length
- TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH
;
761 if (this->parse_list(this, rule_number
, output
+ rule
->offset
, TRANSFORM_ATTRIBUTE
, transform_a_length
) != SUCCESS
)
768 case CONFIGURATION_ATTRIBUTES
:
770 size_t configuration_attributes_length
= payload_length
- CP_PAYLOAD_HEADER_LENGTH
;
771 if (this->parse_list(this, rule_number
, output
+ rule
->offset
, CONFIGURATION_ATTRIBUTE
, configuration_attributes_length
) != SUCCESS
)
778 case ATTRIBUTE_FORMAT
:
780 if (this->parse_bit(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
785 attribute_format
= *(bool*)(output
+ rule
->offset
);
790 if (this->parse_uint15(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
795 attribute_format
= *(bool*)(output
+ rule
->offset
);
798 case CONFIGURATION_ATTRIBUTE_LENGTH
:
800 if (this->parse_uint16(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
805 attribute_length
= *(u_int16_t
*)(output
+ rule
->offset
);
808 case ATTRIBUTE_LENGTH_OR_VALUE
:
810 if (this->parse_uint16(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
815 attribute_length
= *(u_int16_t
*)(output
+ rule
->offset
);
818 case ATTRIBUTE_VALUE
:
820 if (attribute_format
== FALSE
)
822 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, attribute_length
) != SUCCESS
)
832 size_t nonce_length
= payload_length
- NONCE_PAYLOAD_HEADER_LENGTH
;
833 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, nonce_length
) != SUCCESS
)
842 size_t data_length
= payload_length
- ID_PAYLOAD_HEADER_LENGTH
;
843 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, data_length
) != SUCCESS
)
852 size_t data_length
= payload_length
- AUTH_PAYLOAD_HEADER_LENGTH
;
853 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, data_length
) != SUCCESS
)
862 size_t data_length
= payload_length
- CERT_PAYLOAD_HEADER_LENGTH
;
863 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, data_length
) != SUCCESS
)
872 size_t data_length
= payload_length
- CERTREQ_PAYLOAD_HEADER_LENGTH
;
873 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, data_length
) != SUCCESS
)
882 size_t data_length
= payload_length
- EAP_PAYLOAD_HEADER_LENGTH
;
883 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, data_length
) != SUCCESS
)
892 size_t data_length
= payload_length
- DELETE_PAYLOAD_HEADER_LENGTH
;
893 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, data_length
) != SUCCESS
)
902 size_t data_length
= payload_length
- VENDOR_ID_PAYLOAD_HEADER_LENGTH
;
903 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, data_length
) != SUCCESS
)
910 case CONFIGURATION_ATTRIBUTE_VALUE
:
912 size_t data_length
= attribute_length
;
913 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, data_length
) != SUCCESS
)
920 case KEY_EXCHANGE_DATA
:
922 size_t keydata_length
= payload_length
- KE_PAYLOAD_HEADER_LENGTH
;
923 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, keydata_length
) != SUCCESS
)
930 case NOTIFICATION_DATA
:
932 size_t notify_length
= payload_length
- NOTIFY_PAYLOAD_HEADER_LENGTH
- spi_size
;
933 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, notify_length
) != SUCCESS
)
942 size_t data_length
= payload_length
- ENCRYPTION_PAYLOAD_HEADER_LENGTH
;
943 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, data_length
) != SUCCESS
)
952 if (this->parse_uint8(this, rule_number
, output
+ rule
->offset
) != SUCCESS
)
957 ts_type
= *(u_int8_t
*)(output
+ rule
->offset
);
962 size_t address_length
= (ts_type
== TS_IPV4_ADDR_RANGE
) ? 4 : 16;
963 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
,address_length
) != SUCCESS
)
970 case TRAFFIC_SELECTORS
:
972 size_t traffic_selectors_length
= payload_length
- TS_PAYLOAD_HEADER_LENGTH
;
973 if (this->parse_list(this, rule_number
, output
+ rule
->offset
, TRAFFIC_SELECTOR_SUBSTRUCTURE
, traffic_selectors_length
) != SUCCESS
)
980 case UNKNOWN_PAYLOAD
:
982 size_t unknown_payload_data_length
= payload_length
- UNKNOWN_PAYLOAD_HEADER_LENGTH
;
983 if (this->parse_chunk(this, rule_number
, output
+ rule
->offset
, unknown_payload_data_length
) != SUCCESS
)
992 this->logger
->log(this->logger
, ERROR
, " no rule to parse rule %d %s (%d)", rule_number
, mapping_find(encoding_type_m
, rule
->type
), rule
->type
);
997 /* process next rulue */
1002 this->logger
->log(this->logger
, CONTROL
|LEVEL2
, "parsing %s payload finished.",
1003 mapping_find(payload_type_m
, payload_type
));
1008 * Implementation of parser_t.get_remaining_byte_count.
1010 static int get_remaining_byte_count (private_parser_t
*this)
1012 int count
= (this->input_roof
- this->byte_pos
);
1017 * Implementation of parser_t.reset_context.
1019 static void reset_context (private_parser_t
*this)
1021 this->byte_pos
= this->input
;
1026 * Implementation of parser_t.destroy.
1028 static void destroy(private_parser_t
*this)
1034 * Described in header.
1036 parser_t
*parser_create(chunk_t data
)
1038 private_parser_t
*this = malloc_thing(private_parser_t
);
1040 this->logger
= logger_manager
->get_logger(logger_manager
, PARSER
);
1042 this->public.parse_payload
= (status_t(*)(parser_t
*,payload_type_t
,payload_t
**)) parse_payload
;
1043 this->public.reset_context
= (void(*)(parser_t
*)) reset_context
;
1044 this->public.get_remaining_byte_count
= (int (*) (parser_t
*))get_remaining_byte_count
;
1045 this->public.destroy
= (void(*)(parser_t
*)) destroy
;
1047 this->parse_uint4
= parse_uint4
;
1048 this->parse_uint8
= parse_uint8
;
1049 this->parse_uint15
= parse_uint15
;
1050 this->parse_uint16
= parse_uint16
;
1051 this->parse_uint32
= parse_uint32
;
1052 this->parse_uint64
= parse_uint64
;
1053 this->parse_bytes
= parse_bytes
;
1054 this->parse_bit
= parse_bit
;
1055 this->parse_list
= parse_list
;
1056 this->parse_chunk
= parse_chunk
;
1058 this->input
= data
.ptr
;
1059 this->byte_pos
= data
.ptr
;
1061 this->input_roof
= data
.ptr
+ data
.len
;
1063 return (parser_t
*)this;