Download
cyberslak
/lightsout
/net.h
(View History)
cyberslak Add LICENSE information | Latest amendment: 22 on 2025-03-15 |
1 | // SPDX-License-Identifier: MIT |
2 | |
3 | #pragma once |
4 | |
5 | #include <OpenTransport.h> |
6 | #include <OpenTptInternet.h> |
7 | #include "dbuf.h" |
8 | |
9 | void net_init(); |
10 | void net_fini(); |
11 | |
12 | // DNS |
13 | |
14 | /** Resolve a hostname via DNS. |
15 | * |
16 | * If resolution fails, returns -1, otherwise 0. |
17 | */ |
18 | short net_dns_lookup(const char* hostname, InetHost* host); |
19 | |
20 | |
21 | // CONNECTION |
22 | |
23 | /** Create a new synchronous, non-blocking |
24 | * client endpoint using TCP/IP. |
25 | * |
26 | * Failure is fatal - presumably, if we cannot create an endpoint, |
27 | * there is no TCP network connectivity at all. |
28 | */ |
29 | EndpointRef net_create_endpoint(); |
30 | |
31 | /* Connect an endpoint to a host-port combination. |
32 | * |
33 | * On failure, returns NULL. |
34 | */ |
35 | EndpointRef net_connect(const char* host, short port); |
36 | |
37 | /* Connect an endpoint to a host, proxying |
38 | * via the given SOCKSv5 proxy. */ |
39 | EndpointRef net_socks_connect( |
40 | const char* proxy_host, short proxy_port, |
41 | const char* host, short port); |
42 | |
43 | |
44 | // WAIT |
45 | |
46 | /* Busy-wait until an endpoint is readable, periodically |
47 | * calling YieldToAnyThread(). If the endpoint is readable, |
48 | * returns 0. If the endpoint is closed, returns -1. */ |
49 | short net_wait_readable(EndpointRef ep); |
50 | |
51 | |
52 | /* Send an HTTP POST request to the given path with the |
53 | * given content. Returns the reply as a struct dbuf. |
54 | * The caller is responsible for freeing the result. */ |
55 | struct dbuf net_post( |
56 | EndpointRef ep, const char* path, |
57 | const char* token, const char* content); |
58 | |
59 | |
60 | /* Send an HTTP GET request to the given path. |
61 | * Returns the reply as a struct dbuf. |
62 | * The caller is responsible for freeing the result. */ |
63 | struct dbuf net_get( |
64 | EndpointRef ep, const char* path, |
65 | const char* token); |
66 | |
67 | /* Get a pointer to the content in an HTTP response. |
68 | * Assumes a properly formatted HTTP response (i.e. |
69 | * CRLF line endings, ...). If the response does not |
70 | * contain CRLFCRLF, returns NULL. |
71 | */ |
72 | char* net_http_response_content(const char* response); |
73 | |
74 | short net_test_socks(); |