diff options
| author | jade Sparkles | 2026-04-28 22:31:24 +0100 |
|---|---|---|
| committer | jade Sparkles | 2026-04-28 22:31:24 +0100 |
| commit | ab498f3a772c7e13139c79057c9eabde68aa952a (patch) | |
| tree | af343bf75ae0c8e1361bbba917ff3d5aa72bbc1b /docs/fetch-tcp.example.c | |
| parent | c38bcb25aa1805009e399cea40e66d6f31195744 (diff) | |
responses and simple transports
Diffstat (limited to 'docs/fetch-tcp.example.c')
| -rw-r--r-- | docs/fetch-tcp.example.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/docs/fetch-tcp.example.c b/docs/fetch-tcp.example.c new file mode 100644 index 0000000..3d3b608 --- /dev/null +++ b/docs/fetch-tcp.example.c @@ -0,0 +1,71 @@ +#include <arpa/inet.h> +#include <errno.h> +#include <libsparklepaw/transport.h> +#include <netinet/in.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> + +int main(int argc, char** argv) { + if (argc != 3) { + fprintf(stderr, "usage: %s <inet4-addr> <tcp-port>\n", argv[0]); + return 1; + } + + // key for my example zone, replace it with one you can test against + uint8_t key[0x20] = { + 0xc5, 0x82, 0x1e, 0x05, 0x34, 0x80, 0x17, 0x25, + 0x3c, 0xfc, 0x80, 0x87, 0x0b, 0xca, 0x03, 0xed, + 0x0a, 0x8b, 0x93, 0xbc, 0x33, 0x58, 0x33, 0x9e, + 0x7e, 0xee, 0x44, 0x08, 0xff, 0xfd, 0x2c, 0x2d + }; + + struct sprkl_request* req = sprkl_request_make_fetchservice( + SPRKL_SIGALGO_ED25519, key, 0 + ); + + // horrible awful code practices ensue. don't write your code like this, i + // just did this cause it's shorter for the example :3 + + struct sockaddr_in saddr; + saddr.sin_family = AF_INET; + saddr.sin_addr.s_addr = inet_addr(argv[1]); + saddr.sin_port = htons(atoi(argv[2])); + + int sock = socket(AF_INET, SOCK_STREAM, 6); + errno = 0; + if (connect(sock, (struct sockaddr*) &saddr, sizeof saddr)) { + fprintf(stderr, "connect error: %s\n", strerror(errno)); + return errno; + } + + FILE* stream = fdopen(sock, "r+"); + struct sprkl_transport t = sprkl_transport_make_stream(stream); + + int err = sprkl_transport_sendrequest(&t, req); + if (err) { + fprintf(stderr, "req error: %s\n", sprkl_transport_strerror(&t, err)); + return err; + } + + struct sprkl_response resp; + resp.verb = req->verb; + err = sprkl_transport_recvresponse(&t, &resp); + if (err) { + fprintf(stderr, "resp error: %s\n", sprkl_transport_strerror(&t, err)); + return err; + } + + fclose(stream); + + fprintf(stderr, "response status code is %x\n", resp.status); + if (resp.status != SPRKL_STATUS_OK) { + fprintf(stderr, "server said: %*s\n", resp.errmsgsz, resp.errmsg); + } else { + sprkl_service_printdebug(resp.fetchsvc.svc, stdout); + } + + sprkl_response_freeparts(&resp, true); + sprkl_request_freeall(req); +} |
