libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
assoc_response.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 "assoc_response.h"
17#include "../../core/frame/tag.h"
18#include "../../core/frame/tag_iterator.h"
19#include "../../core/misc/byteswap.h"
20#include "../../core/misc/epoch.h"
21#include "../../core/misc/types.h"
22#include "common.h"
23
24#include <errno.h>
25#include <stdint.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/types.h>
29
35 return sizeof(struct libwifi_mgmt_unordered_frame_header) +
37 assoc_resp->tags.length;
38}
39
45 int ret = 0;
46
47 if (assoc_resp->tags.length != 0) {
48 ret = libwifi_remove_tag(&assoc_resp->tags, TAG_DS_PARAMETER);
49 if (ret != 0) {
50 return ret;
51 }
52 }
53
54 const unsigned char *chan = (const unsigned char *) &channel;
55
56 ret = libwifi_quick_add_tag(&assoc_resp->tags, TAG_DS_PARAMETER, chan, 1);
57
58 return ret;
59}
60
66 const unsigned char receiver[6],
67 const unsigned char transmitter[6],
68 const unsigned char address3[6],
69 uint8_t channel) {
70 memset(assoc_resp, 0, sizeof(struct libwifi_assoc_resp));
71
74 memcpy(&assoc_resp->frame_header.addr1, receiver, 6);
75 memcpy(&assoc_resp->frame_header.addr2, transmitter, 6);
76 memcpy(&assoc_resp->frame_header.addr3, address3, 6);
77
80 assoc_resp->fixed_parameters.association_id = rand() % 4096;
81
83
84 const unsigned char supported_rates[] = LIBWIFI_DEFAULT_SUPP_RATES;
85 int ret = libwifi_quick_add_tag(&assoc_resp->tags, TAG_SUPP_RATES, supported_rates, sizeof(supported_rates) - 1);
86
87 return ret;
88}
89
94size_t libwifi_dump_assoc_resp(struct libwifi_assoc_resp *assoc_resp, unsigned char *buf, size_t buf_len) {
95 size_t assoc_resp_len = libwifi_get_assoc_resp_length(assoc_resp);
96 if (assoc_resp_len > buf_len) {
97 return -EINVAL;
98 }
99
100 size_t offset = 0;
101 memcpy(buf + offset, &assoc_resp->frame_header, sizeof(struct libwifi_mgmt_unordered_frame_header));
102 offset += sizeof(struct libwifi_mgmt_unordered_frame_header);
103
104 memcpy(buf + offset, &assoc_resp->fixed_parameters, sizeof(struct libwifi_assoc_resp_fixed_parameters));
105 offset += sizeof(struct libwifi_assoc_resp_fixed_parameters);
106
107 memcpy(buf + offset, assoc_resp->tags.parameters, assoc_resp->tags.length);
108 offset += assoc_resp->tags.length;
109
110 return assoc_resp_len;
111}
112
118 free(assoc_resp->tags.parameters);
119}
#define BYTESWAP16(x)
Definition: byteswap.h:22
struct libwifi_tagged_parameters tags
Definition: assoc_request.h:2
struct libwifi_radiotap_channel channel
Definition: radiotap.h:3
@ SUBTYPE_ASSOC_RESP
Definition: frame.h:38
@ TYPE_MANAGEMENT
Definition: frame.h:31
int libwifi_create_assoc_resp(struct libwifi_assoc_resp *assoc_resp, const unsigned char receiver[6], const unsigned char transmitter[6], const unsigned char address3[6], uint8_t channel)
The generated association response frame is made with sane defaults defined in common....
void libwifi_free_assoc_resp(struct libwifi_assoc_resp *assoc_resp)
Because the tagged parameters memory is managed inside of the library, the library must be the one to...
size_t libwifi_dump_assoc_resp(struct libwifi_assoc_resp *assoc_resp, unsigned char *buf, size_t buf_len)
Copy a libwifi_assoc_resp into a regular unsigned char buffer.
size_t libwifi_get_assoc_resp_length(struct libwifi_assoc_resp *assoc_resp)
The length of an association response frame is the sum of the header length, the fixed parameters len...
int libwifi_set_assoc_resp_channel(struct libwifi_assoc_resp *assoc_resp, uint8_t channel)
Simple helper function to set the channel of an association response by removing and re-adding the DS...
#define LIBWIFI_DEFAULT_SUPP_RATES
A sane default for the supported rates frame field.
Definition: common.h:52
#define LIBWIFI_DEFAULT_AP_CAPABS
A sane default for an AP-side capabilities information field.
Definition: common.h:24
struct libwifi_tagged_parameters tags
struct libwifi_assoc_resp_fixed_parameters fixed_parameters
struct libwifi_mgmt_unordered_frame_header frame_header
unsigned int type
Definition: frame.h:123
unsigned int subtype
Definition: frame.h:124
struct libwifi_frame_ctrl frame_control
Definition: frame.h:208
unsigned char * parameters
Definition: tag.h:235
int libwifi_quick_add_tag(struct libwifi_tagged_parameters *tags, int tag_number, const unsigned char *tag_data, size_t tag_length)
Add a tagged parameter via tag number and data to a management frame.
Definition: tag.c:118
int libwifi_remove_tag(struct libwifi_tagged_parameters *tags, int tag_number)
Remove a tagged parameter from a list of frame tagged parameters.
Definition: tag.c:55
@ TAG_SUPP_RATES
Definition: tag.h:25
@ TAG_DS_PARAMETER
Definition: tag.h:27
size_t length
Definition: tag.h:0
@ STATUS_SUCCESS
Definition: types.h:88