libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
action.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 "action.h"
17
18#include <errno.h>
19#include <stdlib.h>
20#include <string.h>
21
22size_t libwifi_add_action_detail(struct libwifi_action_detail *detail, const unsigned char *data,
23 size_t data_len) {
24 if (detail->detail_length != 0) {
25 detail->detail = realloc(detail->detail, data_len);
26 } else {
27 detail->detail = malloc(data_len);
28 }
29
30 if (detail->detail == NULL) {
31 return -EINVAL;
32 }
33
34 detail->detail_length = data_len;
35
36 memcpy(detail->detail, data, data_len);
37 detail->detail_length = data_len;
38
39 return detail->detail_length;
40}
41
43 if (detail->detail_length != 0) {
44 free(detail->detail);
45 detail->detail_length = 0;
46 }
47}
48
50 const unsigned char receiver[6],
51 const unsigned char transmitter[6],
52 const unsigned char address3[6],
53 uint8_t category) {
54 memset(action, 0, sizeof(struct libwifi_action));
55
58 memcpy(&action->frame_header.addr1, receiver, 6);
59 memcpy(&action->frame_header.addr2, transmitter, 6);
60 memcpy(&action->frame_header.addr3, address3, 6);
61
62 action->frame_header.seq_control.sequence_number = (rand() % 4096);
63
65
66 return 0;
67}
68
70 const unsigned char receiver[6],
71 const unsigned char transmitter[6],
72 const unsigned char address3[6],
73 uint8_t category) {
74 memset(action, 0, sizeof(struct libwifi_action));
75
78 memcpy(&action->frame_header.addr1, receiver, 6);
79 memcpy(&action->frame_header.addr2, transmitter, 6);
80 memcpy(&action->frame_header.addr3, address3, 6);
81
82 action->frame_header.seq_control.sequence_number = (rand() % 4096);
83
85
86 return 0;
87}
88
90 return sizeof(struct libwifi_mgmt_unordered_frame_header) + sizeof(action->fixed_parameters.category) +
92}
93
94size_t libwifi_dump_action(struct libwifi_action *action, unsigned char *buf, size_t buf_len) {
95 size_t action_len = libwifi_get_action_length(action);
96 if (action_len > buf_len) {
97 return -EINVAL;
98 }
99
100 size_t offset = 0;
101
102 memcpy(buf + offset, &action->frame_header, sizeof(struct libwifi_mgmt_unordered_frame_header));
103 offset += sizeof(struct libwifi_mgmt_unordered_frame_header);
104
105 memcpy(buf + offset, &action->fixed_parameters.category, sizeof(action->fixed_parameters.category));
106 offset += sizeof(action->fixed_parameters.category);
107 memcpy(buf + offset, action->fixed_parameters.details.detail,
109 offset += action->fixed_parameters.details.detail_length;
110
111 return action_len;
112}
113
115 free(action->fixed_parameters.details.detail);
116}
int libwifi_create_action_no_ack(struct libwifi_action *action, const unsigned char receiver[6], const unsigned char transmitter[6], const unsigned char address3[6], uint8_t category)
Definition: action.c:69
void libwifi_free_action(struct libwifi_action *action)
Free data associated to a given libwifi_action.
Definition: action.c:114
size_t libwifi_add_action_detail(struct libwifi_action_detail *detail, const unsigned char *data, size_t data_len)
Create a detail for an action frame by supplying raw data and it's length.
Definition: action.c:22
size_t libwifi_dump_action(struct libwifi_action *action, unsigned char *buf, size_t buf_len)
Dump a given libwifi_action to a raw buffer.
Definition: action.c:94
void libwifi_free_action_detail(struct libwifi_action_detail *detail)
Free all memory in a given libwifi_action_detail.
Definition: action.c:42
size_t libwifi_get_action_length(struct libwifi_action *action)
Get the length of a given libwifi_action.
Definition: action.c:89
int libwifi_create_action(struct libwifi_action *action, const unsigned char receiver[6], const unsigned char transmitter[6], const unsigned char address3[6], uint8_t category)
Create a new action frame with a specified action and category.
Definition: action.c:49
uint8_t detail_length
Definition: action.h:0
struct libwifi_action_fixed_parameters fixed_parameters
Definition: action.h:1
char * detail
Definition: action.h:1
struct libwifi_action_detail details
Definition: action.h:1
uint8_t category
Definition: action.h:0
@ SUBTYPE_ACTION_NOACK
Definition: frame.h:51
@ SUBTYPE_ACTION
Definition: frame.h:50
@ TYPE_MANAGEMENT
Definition: frame.h:31
uint8_t detail_length
Definition: action.h:79
struct libwifi_action_detail details
Definition: action.h:85
struct libwifi_action_fixed_parameters fixed_parameters
Definition: action.h:90
struct libwifi_mgmt_unordered_frame_header frame_header
Definition: action.h:89
unsigned int type
Definition: frame.h:123
unsigned int subtype
Definition: frame.h:124
struct libwifi_seq_control seq_control
Definition: frame.h:213
struct libwifi_frame_ctrl frame_control
Definition: frame.h:208
unsigned int sequence_number
Definition: frame.h:133