This commit is contained in:
Tristan 2024-12-20 00:16:25 -05:00
parent fb2a0ca4c3
commit 2d0fc4d9c6

95
gtk.c Normal file
View file

@ -0,0 +1,95 @@
#include <gtk/gtk.h>
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#define SOURCE_PORT 14236
#define DEST_PORT 14235
GtkWidget *text_view;
GtkTextBuffer *text_buffer;
void update_text_view(const char *message) {
GtkTextIter end;
gtk_text_buffer_get_end_iter(text_buffer, &end);
gtk_text_buffer_insert(text_buffer, &end, message, -1);
}
static void activate(GtkApplication *app, gpointer user_data) {
GtkWidget *window;
// Create a new window for the application
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "IP Reporter");
gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
// Add any widgets here (e.g., labels, buttons)
GtkWidget *label = gtk_label_new("Listening...");
gtk_window_set_child(GTK_WINDOW(window), label);
// Show the window
gtk_window_present(GTK_WINDOW(window));
}
void packet_handler(unsigned char *user, const struct pcap_pkthdr *h, const unsigned char *bytes) {
struct ether_header *eth_header = (struct ether_header *)bytes;
struct ip *ip_header = (struct ip *)(bytes + sizeof(struct ether_header));
struct udphdr *udp_header = (struct udphdr *)(bytes + sizeof(struct ether_header) + sizeof(struct ip));
if (ntohs(eth_header->ether_type) == ETHERTYPE_IP && ip_header->ip_p == IPPROTO_UDP) {
char source_ip[INET_ADDRSTRLEN];
char dest_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(ip_header->ip_src), source_ip, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &(ip_header->ip_dst), dest_ip, INET_ADDRSTRLEN);
if (strcmp(dest_ip, "255.255.255.255") == 0 &&
ntohs(udp_header->source) == SOURCE_PORT &&
ntohs(udp_header->dest) == DEST_PORT) {
char message[256];
snprintf(message, sizeof(message), "Miner IP: %s\n", source_ip);
g_main_context_invoke(NULL, (GSourceFunc)update_text_view, strdup(message));
}
}
}
void start_packet_capture(const char *device) {
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle = pcap_open_live(device, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL) {
fprintf(stderr, "Error: %s\n", errbuf);
return;
}
pcap_loop(handle, 0, packet_handler, NULL);
pcap_close(handle);
}
void on_start_button_clicked(GtkButton *button, gpointer user_data) {
const char *device = user_data;
g_thread_new("pcap_thread", (GThreadFunc)start_packet_capture, (gpointer)device);
}
int main(int argc, char **argv) {
GtkApplication *app;
int status;
// Create a new GtkApplication
app = gtk_application_new("org.monotreme.antminer", G_APPLICATION_DEFAULT_FLAGS);
// Connect the activate signal to the activate function
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
// Run the application
status = g_application_run(G_APPLICATION(app), argc, argv);
// Free the application object
g_object_unref(app);
return status;
}