libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
assoc_request.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_request.h"
17#include "common.h"
18
19#include <errno.h>
20#include <stdlib.h>
21#include <string.h>
22
28 return sizeof(assoc_req->frame_header) + sizeof(struct libwifi_assoc_req_fixed_parameters) +
29 assoc_req->tags.length;
30}
31
37 const unsigned char receiver[6],
38 const unsigned char transmitter[6],
39 const unsigned char address3[6],
40 const char *ssid,
41 uint8_t channel) {
42 memset(assoc_req, 0, sizeof(struct libwifi_assoc_req));
43
46 memcpy(&assoc_req->frame_header.addr1, receiver, 6);
47 memcpy(&assoc_req->frame_header.addr2, transmitter, 6);
48 memcpy(&assoc_req->frame_header.addr3, address3, 6);
49 assoc_req->frame_header.seq_control.sequence_number = (rand() % 4096);
50
53
54 int ret = libwifi_quick_add_tag(&assoc_req->tags, TAG_SSID, (const unsigned char *) ssid, strlen(ssid));
55 if (ret != 0) {
56 return ret;
57 }
58
59 ret = libwifi_quick_add_tag(&assoc_req->tags, TAG_DS_PARAMETER, (const unsigned char *) &channel, 1);
60 if (ret != 0) {
61 return ret;
62 }
63
64 return 0;
65}
66
71size_t libwifi_dump_assoc_req(struct libwifi_assoc_req *assoc_req, unsigned char *buf, size_t buf_len) {
72 size_t assoc_req_len = libwifi_get_assoc_req_length(assoc_req);
73 if (assoc_req_len > buf_len) {
74 return -EINVAL;
75 }
76
77 size_t offset = 0;
78 memcpy(buf + offset, &assoc_req->frame_header, sizeof(struct libwifi_mgmt_unordered_frame_header));
79 offset += sizeof(struct libwifi_mgmt_unordered_frame_header);
80
81 memcpy(buf + offset, &assoc_req->fixed_parameters, sizeof(struct libwifi_assoc_req_fixed_parameters));
82 offset += sizeof(struct libwifi_assoc_req_fixed_parameters);
83
84 memcpy(buf + offset, assoc_req->tags.parameters, assoc_req->tags.length);
85 offset += assoc_req->tags.length;
86
87 return assoc_req_len;
88}
89
95 free(assoc_req->tags.parameters);
96}
#define BYTESWAP16(x)
Definition: byteswap.h:22
struct libwifi_radiotap_channel channel
Definition: radiotap.h:3
@ SUBTYPE_ASSOC_REQ
Definition: frame.h:37
@ TYPE_MANAGEMENT
Definition: frame.h:31
size_t libwifi_dump_assoc_req(struct libwifi_assoc_req *assoc_req, unsigned char *buf, size_t buf_len)
Copy a libwifi_assoc_req into a regular unsigned char buffer.
Definition: assoc_request.c:71
void libwifi_free_assoc_req(struct libwifi_assoc_req *assoc_req)
Because the tagged parameters memory is managed inside of the library, the library must be the one to...
Definition: assoc_request.c:94
size_t libwifi_get_assoc_req_length(struct libwifi_assoc_req *assoc_req)
The length of an association request frame is the sum of the header length, the fixed parameters leng...
Definition: assoc_request.c:27
int libwifi_create_assoc_req(struct libwifi_assoc_req *assoc_req, const unsigned char receiver[6], const unsigned char transmitter[6], const unsigned char address3[6], const char *ssid, uint8_t channel)
The generated association request frame is made with sane defaults defined in common....
Definition: assoc_request.c:36
#define LIBWIFI_DEFAULT_AP_CAPABS
A sane default for an AP-side capabilities information field.
Definition: common.h:24
#define LIBWIFI_DEFAULT_LISTEN_INTERVAL
A sane default for the listen_interval field.
Definition: common.h:38
struct libwifi_tagged_parameters tags
Definition: assoc_request.h:48
struct libwifi_mgmt_unordered_frame_header frame_header
Definition: assoc_request.h:46
struct libwifi_assoc_req_fixed_parameters fixed_parameters
Definition: assoc_request.h:47
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
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
@ TAG_SSID
Definition: tag.h:24
@ TAG_DS_PARAMETER
Definition: tag.h:27