diff options
| -rw-r--r-- | libsparklepaw/transport.c | 26 | ||||
| -rw-r--r-- | libsparklepaw/transport.h | 26 |
2 files changed, 50 insertions, 2 deletions
diff --git a/libsparklepaw/transport.c b/libsparklepaw/transport.c index cb92b40..28fcac5 100644 --- a/libsparklepaw/transport.c +++ b/libsparklepaw/transport.c @@ -1,8 +1,12 @@ #include "request.h" #include "transport.h" +#include <arpa/inet.h> #include <errno.h> +#include <stdbool.h> +#include <stdlib.h> #include <string.h> +#include <sys/socket.h> int sprkl_transport_sendrequest( struct sprkl_transport* t, struct sprkl_request* request) { @@ -73,3 +77,25 @@ struct sprkl_transport sprkl_transport_make_stream(FILE* stream) { }; return t; } + + +int sprkl_transport_make_default(struct sprkl_transport* transport) { + char* host = getenv("SPRKL_TMP_DEFTP_HOST"); + char* port = getenv("SPRKL_TMP_DEFTP_PORT"); + if (host == NULL || port == NULL) return -1; + //! \todo make this much more robust + struct sockaddr_in sa; + sa.sin_family = AF_INET; + sa.sin_addr.s_addr = inet_addr(host); + sa.sin_port = htons(atoi(port)); + + int sock = socket(AF_INET, SOCK_STREAM, 6); + if (connect(sock, (struct sockaddr*) &sa, sizeof sa)) return errno; + FILE* stream = fdopen(sock, "r+"); + *transport = sprkl_transport_make_stream(stream); + return 0; +} + +void sprkl_transport_fini_default(struct sprkl_transport* transport) { + fclose((FILE*) transport->opaque); // i hope it's a stream transport... +} diff --git a/libsparklepaw/transport.h b/libsparklepaw/transport.h index 152eaf1..ff71e39 100644 --- a/libsparklepaw/transport.h +++ b/libsparklepaw/transport.h @@ -10,7 +10,7 @@ /** Generic transport vtable. * Generally you shouldn't use the contained functions directly, instead invoke - * them via their wrappers, e.g. sprkl_trasport_sendrequest(). + * them via their wrappers, e.g. sprkl_transport_sendrequest(). * \todo Non-blocking version? */ struct sprkl_transport { void* opaque; /**< Instance-specific data. */ @@ -71,7 +71,7 @@ int sprkl_transport_sendresponse( * except for the sprkl_response::verb field, which you should set to match the * verb of the associated request, in order for the response to be parsed * correctly. - * \returns 0 if successfulm or a transport-defined error number. */ + * \returns 0 if successful, or a transport-defined error number. */ int sprkl_transport_recvresponse( struct sprkl_transport* t, struct sprkl_response* response); @@ -90,4 +90,26 @@ char* sprkl_transport_strerror(struct sprkl_transport* t, int error); * \returns a transport that reads and writes to the stream. */ struct sprkl_transport sprkl_transport_make_stream(FILE* stream); +/** Creates the platform's default transport, usually configured by environment + * variable(s). The default transport will usually connect to the user's + * preferred "local daemon" (although it may or may not be on the same physical + * machine). It should only be used to act as a client (i.e. sending requests + * and receiving responses). + * + * You must call this function before every new request to allow the underlying + * transport connection to be configured appropriately, and also call + * sprkl_transport_fini_default() after receiving each response. + * + * For now, the default transport will always use TCP/IPv4 as configured by the + * environment variables SPRKL_TMP_DEFTP_HOST and SPRKL_TMP_DEFTP_PORT. This + * *will* change in future. + * \param[out] Pointer to transport struct to be filled in appropriately. + * \returns 0 if successful, -1 if there is no default transport configured, or + * a transport-defined positive integer error number otherwise. */ +int sprkl_transport_make_default(struct sprkl_transport* transport); + +/** Finalises the default transport. + * \param transport Reference to the default transport. */ +void sprkl_transport_fini_default(struct sprkl_transport* transport); + #endif |
