about summary refs log tree commit diff stats
path: root/src/transmit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transmit.c')
-rw-r--r--src/transmit.c55
1 files changed, 55 insertions, 0 deletions
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 @@
1#include "transmit.h"
2
3#include <libwifi.h>
4#include <linux/if_packet.h>
5#include <netinet/ether.h>
6#include <pthread.h>
7#include <stdio.h>
8#include <stdlib.h>
9
10static pthread_mutex_t socket_mutex = PTHREAD_MUTEX_INITIALIZER;
11
12int send_packet(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char *packet, int packet_size)
13{
14 struct sockaddr_ll socket_address;
15 socket_address.sll_ifindex = device_index;
16 socket_address.sll_halen = ETH_ALEN;
17 memcpy(socket_address.sll_addr, dst_addr, 6);
18
19 pthread_mutex_lock(&socket_mutex);
20
21 int ret = 0;
22 if (sendto(sock, packet, packet_size, 0, (struct sockaddr *)&socket_address, sizeof(struct sockaddr_ll)) < 0)
23 {
24 printf("Could not send packet: %d\n", errno);
25 ret = errno;
26 }
27
28 pthread_mutex_unlock(&socket_mutex);
29
30 return ret;
31}
32
33int send_packet_with_radiotap(int sock, int device_index, const unsigned char dst_addr[6], const unsigned char* packet, int packet_size)
34{
35 struct libwifi_radiotap_info radiotap_info = {0};
36 radiotap_info.present = (1 << IEEE80211_RADIOTAP_FLAGS) | (1 << IEEE80211_RADIOTAP_RATE);
37 radiotap_info.flags = IEEE80211_RADIOTAP_F_FCS | IEEE80211_RADIOTAP_F_SHORTPRE;
38 radiotap_info.rate_raw = 4;
39
40 unsigned char radiotap_buffer[256];
41 int radiotap_size = libwifi_create_radiotap(&radiotap_info, radiotap_buffer);
42
43 uint32_t fcs = libwifi_calculate_fcs(packet, packet_size);
44
45 int buffer_size = radiotap_size + packet_size + sizeof(uint32_t);
46 unsigned char* buffer = (unsigned char*)malloc(buffer_size);
47
48 memcpy(buffer, radiotap_buffer, radiotap_size);
49 memcpy(buffer + radiotap_size, packet, packet_size);
50 memcpy(buffer + radiotap_size + packet_size, (unsigned char*)&fcs, sizeof(uint32_t));
51
52 int ret = send_packet(sock, device_index, dst_addr, buffer, buffer_size);
53
54 return ret;
55}