Skip to content

Instantly share code, notes, and snippets.

@eriknl
Created June 7, 2021 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eriknl/145f8fd2ab8bab176ae1ecca928e92c8 to your computer and use it in GitHub Desktop.
Save eriknl/145f8fd2ab8bab176ae1ecca928e92c8 to your computer and use it in GitHub Desktop.
#include "loratap.h"
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
#define LINKTYPE_LORA_LORATAP 270
typedef struct pcap_hdr_s {
uint32_t magic_number; /* magic number */
uint16_t version_major; /* major version number */
uint16_t version_minor; /* minor version number */
int32_t thiszone; /* GMT to local correction */
uint32_t sigfigs; /* accuracy of timestamps */
uint32_t snaplen; /* max length of captured packets, in octets */
uint32_t network; /* data link type */
} pcap_hdr_t;
typedef struct pcaprec_hdr_s {
uint32_t ts_sec; /* timestamp seconds */
uint32_t ts_usec; /* timestamp microseconds */
uint32_t incl_len; /* number of octets of packet saved in file */
uint32_t orig_len; /* actual length of packet */
} pcaprec_hdr_t;
void dumpLoraTapHeader(loratap_header_t header) {
printf("LoRaTap packet header\n");
printf("Packet RSSI: %d\n", header.rssi.packet_rssi);
printf("RSSI: %d\n", header.rssi.current_rssi);
printf("SNR: %d\n", header.rssi.snr);
printf("Frequency: %u\n", ntohl(header.channel.frequency));
printf("SF: %2u\n", header.channel.sf);
}
void writeFakePcap(const char *outputfile, int payloadsize, unsigned char *payload) {
FILE * captureFile;
pcap_hdr_t file_header;
loratap_header_t loratap_packet_header;
pcaprec_hdr_t pcap_packet_header;
/* Create pcap with header */
captureFile = fopen(outputfile, "w");
file_header.magic_number = 0xa1b2c3d4;
file_header.version_major = 2;
file_header.version_minor = 4;
file_header.thiszone = 0;
file_header.sigfigs = 0;
file_header.snaplen = 255;
file_header.network = LINKTYPE_LORA_LORATAP;
fwrite(&file_header, sizeof(pcap_hdr_t), 1, captureFile);
/* Write packet */
pcap_packet_header.ts_sec = 0;
pcap_packet_header.ts_usec = 0;
pcap_packet_header.incl_len = payloadsize + sizeof(loratap_header_t);
pcap_packet_header.orig_len = payloadsize + sizeof(loratap_header_t);
loratap_packet_header.lt_version = 0;
loratap_packet_header.lt_length = htons(sizeof(loratap_header_t));
loratap_packet_header.channel.frequency = htonl(868100000);
loratap_packet_header.channel.bandwidth = 1;
loratap_packet_header.channel.sf = 7;
loratap_packet_header.rssi.packet_rssi = 0;
loratap_packet_header.rssi.current_rssi = 0;
loratap_packet_header.rssi.max_rssi = 255;
loratap_packet_header.rssi.snr = 0;
loratap_packet_header.sync_word = 0x34; // LoRaWAN
dumpLoraTapHeader(loratap_packet_header);
fwrite(&pcap_packet_header, sizeof(pcaprec_hdr_t), 1, captureFile);
fwrite(&loratap_packet_header, sizeof(loratap_header_t), 1, captureFile);
fwrite(payload, payloadsize, 1, captureFile);
fclose(captureFile);
}
int main() {
unsigned char buffer[] = "Hello LoraTap";
writeFakePcap("loratap.pcap", strlen(buffer), buffer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment