libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
disassociation.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 "disassociation.h"
17#include "../../core/misc/byteswap.h"
18
19#include <errno.h>
20#include <stdlib.h>
21#include <string.h>
22
28 return sizeof(struct libwifi_mgmt_unordered_frame_header) +
29 sizeof(struct libwifi_disassoc_fixed_parameters) + disassoc->tags.length;
30}
31
37 const unsigned char receiver[6],
38 const unsigned char transmitter[6],
39 const unsigned char address3[6],
40 uint16_t reason_code) {
41 memset(disassoc, 0, sizeof(struct libwifi_disassoc));
42
45 memcpy(&disassoc->frame_header.addr1, receiver, 6);
46 memcpy(&disassoc->frame_header.addr2, transmitter, 6);
47 memcpy(&disassoc->frame_header.addr3, address3, 6);
48
49 disassoc->frame_header.seq_control.sequence_number = (rand() % 4096);
50
51 memcpy(&disassoc->fixed_parameters.reason_code, &reason_code, sizeof(reason_code));
52
53 return 0;
54}
55
60size_t libwifi_dump_disassoc(struct libwifi_disassoc *disassoc, unsigned char *buf, size_t buf_len) {
61 size_t disassoc_len = libwifi_get_disassoc_length(disassoc);
62 if (disassoc_len > buf_len) {
63 return -EINVAL;
64 }
65
66 size_t offset = 0;
67 memcpy(buf + offset, &disassoc->frame_header, sizeof(struct libwifi_mgmt_unordered_frame_header));
68 offset += sizeof(struct libwifi_mgmt_unordered_frame_header);
69
70 memcpy(buf + offset, &disassoc->fixed_parameters, sizeof(struct libwifi_disassoc_fixed_parameters));
71 offset += sizeof(struct libwifi_disassoc_fixed_parameters);
72
73 memcpy(buf + offset, disassoc->tags.parameters, disassoc->tags.length);
74 offset += disassoc->tags.length;
75
76 return disassoc_len;
77}
78
84 free(disassoc->tags.parameters);
85}
struct libwifi_tagged_parameters tags
Definition: assoc_request.h:2
uint16_t reason_code
@ SUBTYPE_DISASSOC
Definition: frame.h:47
@ TYPE_MANAGEMENT
Definition: frame.h:31
int libwifi_create_disassoc(struct libwifi_disassoc *disassoc, const unsigned char receiver[6], const unsigned char transmitter[6], const unsigned char address3[6], uint16_t reason_code)
The generated disassociation frame contains only the supplied receiver, transmitter and reason_code b...
size_t libwifi_get_disassoc_length(struct libwifi_disassoc *disassoc)
The length of a disassoc frame is the sum of the header length, the fixed parameters length,...
size_t libwifi_dump_disassoc(struct libwifi_disassoc *disassoc, unsigned char *buf, size_t buf_len)
Copy a libwifi_disassoc into a regular unsigned char buffer.
void libwifi_free_disassoc(struct libwifi_disassoc *disassoc)
Because the tagged parameters memory is managed inside of the library, the library must be the one to...
struct libwifi_disassoc_fixed_parameters fixed_parameters
struct libwifi_tagged_parameters tags
struct libwifi_mgmt_unordered_frame_header frame_header
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
unsigned char * parameters
Definition: tag.h:235
size_t length
Definition: tag.h:0