From 27e0133d40f7b328482b501a18e22b12a55564ea Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sun, 13 Jul 2025 10:07:59 -0400 Subject: Associate --- src/transmit.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/transmit.c (limited to 'src/transmit.c') diff --git a/src/transmit.c b/src/transmit.c new file mode 100644 index 0000000..1978ff2 --- /dev/null +++ b/src/transmit.c @@ -0,0 +1,55 @@ +#include "transmit.h" + +#include +#include +#include +#include +#include +#include + +static pthread_mutex_t socket_mutex = PTHREAD_MUTEX_INITIALIZER; + +int send_packet(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size) +{ + struct sockaddr_ll socket_address; + socket_address.sll_ifindex = device_index; + socket_address.sll_halen = ETH_ALEN; + memcpy(socket_address.sll_addr, dst_addr, 6); + + pthread_mutex_lock(&socket_mutex); + + int ret = 0; + if (sendto(sock, packet, packet_size, 0, (struct sockaddr *)&socket_address, sizeof(struct sockaddr_ll)) < 0) + { + printf("Could not send packet: %d\n", errno); + ret = errno; + } + + pthread_mutex_unlock(&socket_mutex); + + return ret; +} + +int send_packet_with_radiotap(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char* packet, int packet_size) +{ + struct libwifi_radiotap_info radiotap_info = {0}; + radiotap_info.present = (1 << IEEE80211_RADIOTAP_FLAGS) | (1 << IEEE80211_RADIOTAP_RATE); + radiotap_info.flags = IEEE80211_RADIOTAP_F_FCS | IEEE80211_RADIOTAP_F_SHORTPRE; + radiotap_info.rate_raw = 4; + + unsigned char radiotap_buffer[256]; + int radiotap_size = libwifi_create_radiotap(&radiotap_info, radiotap_buffer); + + uint32_t fcs = libwifi_calculate_fcs(packet, packet_size); + + int buffer_size = radiotap_size + packet_size + sizeof(uint32_t); + unsigned char* buffer = (unsigned char*)malloc(buffer_size); + + memcpy(buffer, radiotap_buffer, radiotap_size); + memcpy(buffer + radiotap_size, packet, packet_size); + memcpy(buffer + radiotap_size + packet_size, (unsigned char*)&fcs, sizeof(uint32_t)); + + int ret = send_packet(sock, device_index, dst_addr, buffer, buffer_size); + + return ret; +} -- cgit 1.4.1