libsparklepaw
PawSD library
Loading...
Searching...
No Matches
Examples of usage

All of these are subject to change, as the API isn't remotely stable yet.

Example of constructing a service and writing it out:

void main() {
struct sprkl_tagval_pair pair = {
.tag = { .upper = 0xFFFFFFFFFFFFFFFF, .lower = 0 },
.valuesz = 8,
.value = "hello!!!"
};
struct sprkl_record record = {
.paircnt = 1,
.pairs = &pair
};
uint8_t signature[4] = { 0x12, 0x34, 0x56, 0x78 };
struct sprkl_service service = {
.sigsz = sizeof(signature),
.index = 0x6969,
.flags = 0,
.reccnt = 1,
.records = &record
};
int n = sprkl_service_write(&service, stdout);
fprintf(stderr, "\nWrote %d bytes\n", n);
}
Services, records and tags.
int sprkl_service_write(struct sprkl_service *service, FILE *stream)
Encodes and writes a service in wire format to a stream.
Definition service.c:6
A record, made up of multiple tag-value pairs.
Definition service.h:25
A service, contains a collection of related records and is cryptographically signed.
Definition service.h:32
struct sprkl_record * records
Array of records.
Definition service.h:41
uint8_t * signature
Signature (no terminator).
Definition service.h:34
uint32_t flags
Flags (bitfield).
Definition service.h:39
uint16_t index
Service index within its zone.
Definition service.h:35
uint16_t reccnt
Number of records in the service.
Definition service.h:40
A tag-value pair, contains one labelled piece of data.
Definition service.h:18

Example of reading a service in and printing it for debugging:

#include <stdlib.h>
void main() {
struct sprkl_service* service = malloc(sizeof(struct sprkl_service));
sprkl_service_read(service, stdin);
sprkl_service_printdebug(service, stderr);
}
int sprkl_service_printdebug(struct sprkl_service *service, FILE *stream)
Prints out a service for debugging purposes.
Definition service.c:34
void sprkl_service_freeall(struct sprkl_service *service)
Frees all the stuff in the service, and then the service itself.
Definition service.c:77
int sprkl_service_read(struct sprkl_service *service, FILE *stream)
Reads and decodes a service in wire format from a stream.
Definition service.c:19

Example of making a request to a daemon via TCP transport:

#include <arpa/inet.h>
#include <errno.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
};
);
// 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+");
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_SIGALGO_ED25519
Ed25519.
Definition common.h:10
void sprkl_request_freeall(struct sprkl_request *request)
Frees any pointers contained within the request, and then the request itself.
Definition request.c:87
@ SPRKL_STATUS_OK
Success.
Definition request.h:32
void sprkl_response_freeparts(struct sprkl_response *response, bool errmsg)
Frees any pointers contained within the response, but not the response itself.
Definition request.c:208
struct sprkl_request * sprkl_request_make_fetchservice(enum sprkl_sigalgo keyalgo, uint8_t *key, uint16_t serviceidx)
Allocates and returns a new request to fetch a service given its key and index.
Definition request.c:92
Request tagged union.
Definition request.h:74
enum sprkl_request_verb verb
Union tag, request verb.
Definition request.h:75
Response tagged union (varies on the associated request verb and status).
Definition request.h:84
enum sprkl_request_verb verb
Union tag, original request verb.
Definition request.h:85
Generic transport vtable.
Definition transport.h:15
Generic transport mechanism and concrete implementations.
int sprkl_transport_sendrequest(struct sprkl_transport *t, struct sprkl_request *request)
Sends a request via the given transport.
Definition transport.c:11
int sprkl_transport_recvresponse(struct sprkl_transport *t, struct sprkl_response *response)
Receives a response via the given transport.
Definition transport.c:26
char * sprkl_transport_strerror(struct sprkl_transport *t, int error)
Converts a transport-defined error number to a string description.
Definition transport.c:31
struct sprkl_transport sprkl_transport_make_stream(FILE *stream)
Creates a new transport operating on the given stdio stream.
Definition transport.c:69

As above, but using the system configured default transport:

#include <stdio.h>
int main(int argc, char** argv) {
// 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_transport t;
if (err == -1) {
fprintf(stderr, "error: no default transport is available\n");
return err;
} else if (err) {
fprintf(stderr, "make transport error: %s\n", sprkl_transport_strerror(&t, err));
return err;
}
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;
}
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);
}
}
void sprkl_transport_fini_default(struct sprkl_transport *transport)
Finalises the default transport.
Definition transport.c:101
int sprkl_transport_make_default(struct sprkl_transport *transport)
Creates the platform's default transport, usually configured by environment variable(s).
Definition transport.c:82