summaryrefslogtreecommitdiff
path: root/client/example.c
blob: e2384d0f52f7bee17ca360ef3a6ee2702190636e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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;
}