libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
tag_iterator.c
Go to the documentation of this file.
1/* Copyright 2021 The libwifi Authors
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 */
15
16#include "tag_iterator.h"
17
18#include <errno.h>
19#include <string.h>
20
21int libwifi_tag_iterator_init(struct libwifi_tag_iterator *it, const void *tags_start, size_t data_len) {
22 it->tag_header = (struct libwifi_tag_header *) tags_start;
23
24 it->tag_data = (unsigned char *) tags_start + sizeof(struct libwifi_tag_header);
26 it->_frame_end = (unsigned char *) (tags_start) + data_len - 1;
27
28 return 0;
29}
30
32 unsigned char *next_th = (unsigned char *) it->_next_tag_header;
33 if (next_th >= it->_frame_end) {
34 return -1;
35 }
36
38 if (it->tag_header->tag_len <= 0) {
39 return -1;
40 }
41
42 unsigned long bytes_left = (char *) it->_frame_end - (char *) it->tag_header;
43 if (it->tag_header->tag_len >= bytes_left) {
44 return -1;
45 }
46
47 it->tag_data = ((unsigned char *) (it->tag_header)) + sizeof(struct libwifi_tag_header);
49
50 return it->tag_header->tag_num;
51}
A tagged parameter always consists of a tag number and length.
Definition: tag.h:215
uint8_t tag_num
Definition: tag.h:216
uint8_t tag_len
Definition: tag.h:217
A libwifi_tag_iterator is used to iterate through a list of tagged parameters in a wifi frame.
Definition: tag_iterator.h:28
const unsigned char * _frame_end
Definition: tag_iterator.h:32
struct libwifi_tag_header * _next_tag_header
Definition: tag_iterator.h:31
struct libwifi_tag_header * tag_header
Definition: tag_iterator.h:29
const unsigned char * tag_data
Definition: tag_iterator.h:30
int libwifi_tag_iterator_next(struct libwifi_tag_iterator *it)
Iterate towards the next tagged parameter in a libwifi tag iterator.
Definition: tag_iterator.c:31
int libwifi_tag_iterator_init(struct libwifi_tag_iterator *it, const void *tags_start, size_t data_len)
Initialise a libwifi frame tag iterator.
Definition: tag_iterator.c:21