summaryrefslogtreecommitdiff
path: root/client/example.c
diff options
context:
space:
mode:
authorGravatar moon <moon@coffin.ir> 2025-09-13 23:42:29 +0330
committerGravatar moon <moon@coffin.ir> 2025-09-13 23:42:29 +0330
commit8f73120e81f8ee903a4c50566501ea36efe55edc (patch)
treebcd2d0b806b5225209b408c53a181369abbda4b3 /client/example.c
parentplugin file support (diff)
client library
Diffstat (limited to 'client/example.c')
-rw-r--r--client/example.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/client/example.c b/client/example.c
new file mode 100644
index 0000000..e2384d0
--- /dev/null
+++ b/client/example.c
@@ -0,0 +1,102 @@
+#include "client.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+// helper to print a hash in hex format
+void print_hash(const hash h) {
+ for (int i = 0; i < 32; ++i) { printf("%02x", h[ i ]); }
+}
+
+// helper to print an IP address
+void print_ip(const struct relay_neighbor *n) {
+ if (n->has_ipv4) {
+ printf("%d.%d.%d.%d", n->v4_addr[ 0 ], n->v4_addr[ 1 ], n->v4_addr[ 2 ], n->v4_addr[ 3 ]);
+ } else if (n->has_ipv6) {
+ char ip_string[ 46 ];
+ int offset = 0;
+ for (int j = 0; j < 8; ++j) {
+ uint16_t word = (n->v6_addr[ j * 2 ] << 8) | n->v6_addr[ j * 2 + 1 ];
+ offset += snprintf(
+ ip_string + offset, sizeof(ip_string) - offset, "%x%s", word, (j < 7) ? ":" : "");
+ }
+ printf("%s", ip_string);
+ } else {
+ printf("N/A");
+ }
+}
+
+int main() {
+ printf("moonpaste client library test!\n\n");
+
+ const char *server_ip = "127.0.0.1";
+ const int server_port = 8040;
+ mp_client_error_t err;
+
+ // 1. audit the server
+ printf("Auditing server at %s:%d...\n", server_ip, server_port);
+ struct relay_info info;
+ err = mp_get_server_info(server_ip, server_port, &info);
+
+ if (err != MP_CLIENT_SUCCESS) {
+ fprintf(stderr, "Failed to audit server. Error code: %d\n", err);
+ return 1;
+ }
+ printf(" Server Audit Complete:\n");
+ printf(" - Version: v%d.%d\n", info.major_version, info.minor_version);
+ printf(" - MOTD: %s\n", info.motd);
+ printf(" - PoW Zeros: %d\n", info.capabilities.leading_zeros);
+ printf(" - Held Packets: %lu\n\n", info.statistics.held_packets);
+
+ // 2. create and push a new packet
+ printf("Generating keypair and creating a packet...\n");
+ keypair author = mp_generate_keypair();
+ const char message[] = "This is a test message from the moonpaste client library.";
+ struct data_packet new_packet
+ = mp_create_packet_from_bytes((const byte *) message, strlen(message), 0, NULL, author);
+
+ printf("Running Proof-of-Work with %d leading zeros...\n", info.capabilities.leading_zeros);
+ new_packet = mp_run_pow(new_packet, info.capabilities.leading_zeros);
+ printf(" PoW complete. Nonce: %u, Hash: ", new_packet.nonce);
+ print_hash(new_packet.hash);
+ printf("\n");
+
+ printf("Pushing packet to server...\n");
+ bool success;
+ err = mp_push_packet(server_ip, server_port, new_packet, &success);
+ if (err != MP_CLIENT_SUCCESS || !success) {
+ fprintf(stderr, "Failed to push packet. Error: %d, Success: %d\n", err, success);
+ } else {
+ printf(" Packet pushed successfully!\n\n");
+ }
+
+ // 3. retrieve the packet we just pushed
+ printf("Retrieving the packet by hash...\n");
+ struct data_packet retrieved_packet;
+ err = mp_get_packet(server_ip, server_port, new_packet.hash, &retrieved_packet);
+ if (err != MP_CLIENT_SUCCESS) {
+ fprintf(stderr, "Failed to retrieve packet. Error: %d\n", err);
+ } else {
+ printf(" Packet retrieved successfully!\n");
+ printf(" Contents: '%.*s'\n\n", (int) strlen(message), retrieved_packet.contents.data);
+ }
+
+ // 4. get a list of neighbors
+ printf("Requesting 5 neighbors...\n");
+ struct relay_neighbor *neighbors = NULL;
+ byte neighbor_count = 0;
+ err = mp_get_server_neighbors(server_ip, server_port, 5, &neighbors, &neighbor_count);
+ if (err != MP_CLIENT_SUCCESS) {
+ fprintf(stderr, "Failed to get neighbors. Error: %d\n", err);
+ } else {
+ printf(" Received %d neighbors:\n", neighbor_count);
+ for (byte i = 0; i < neighbor_count; ++i) {
+ printf(" - Neighbor %d: ", i + 1);
+ print_ip(&neighbors[ i ]);
+ printf("\n");
+ }
+ free(neighbors); // free me
+ }
+
+ return 0;
+}