Compare commits

..

1 commit
nix ... main

Author SHA1 Message Date
2d0fc4d9c6 update? 2024-12-20 00:16:25 -05:00
3 changed files with 95 additions and 19 deletions

View file

@ -1,14 +0,0 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [
pkgs.gcc
pkgs.libpcap
];
# Set any environment variables if needed
shellHook = ''
echo "Environment set up for ip-reporter-c"
'';
}

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;
}

View file

@ -3,8 +3,3 @@
I'm starting to look at programming languages like vehicles. The older ones are much simpler and easier to hack around on, but this is probably not as 'safe' as a language like Rust. I'm generally in favor of *most* safety features they put on cars, but, speaking as a former mechanic, I can't say I agreed with all of them.
I think this may be similar to the arguments over languages like C/C++ vs Rust and Zig.
I installed NixOS tonight because I have a WFH day at work tomorrow due to the heat. I've messed with Nix and building in the past and it's not gone well. Got this working it seems.
After checking out the `nix` branch, run `nix-shell` in the cloned directory and then `gcc -o ip-reporter ip-reporter.c -lpcap`. Worked great.
test trust