libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-network-helper.h
Go to the documentation of this file.
1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2023 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
31lws_routing_table_get(struct lws_context *cx);
32
34
35#if defined(LWS_ESP_PLATFORM)
36#include <lwip/sockets.h>
37#endif
38
39/* cope with large amounts of route information */
41
46
47/*
48 * This represents an entry in the system routing table
49 */
50
51typedef struct lws_route {
53
54 lws_sockaddr46 src; /* lws_dns_sort_t: the source route's src */
57
58 lws_dns_score_t score; /* when used as lws_dns_sort_t */
59
60 int if_idx;
62 int ifa_flags; /* if source_ads */
63
64 lws_route_uidx_t uidx; /* unique index for this route */
65
69 uint8_t scope; /* if source_ads */
70 uint8_t af; /* if source_ads */
71
73 uint8_t has_source:1; /* lws_dns_sort_t: src is set */
75
76/*
77 * We reuse the route object as the dns sort granule, so there's only one
78 * struct needs to know all the gnarly ipv6 details
79 */
80
82
93lws_canonical_hostname(struct lws_context *context);
94
110lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
111 int name_len, char *rip, int rip_len);
112
124LWS_VISIBLE LWS_EXTERN const char *
125lws_get_peer_simple(struct lws *wsi, char *name, size_t namelen);
126
127LWS_VISIBLE LWS_EXTERN const char *
128lws_get_peer_simple_fd(lws_sockfd_type fd, char *name, size_t namelen);
129
130#define LWS_ITOSA_USABLE 0
131#define LWS_ITOSA_NOT_EXIST -1
132#define LWS_ITOSA_NOT_USABLE -2
133#define LWS_ITOSA_BUSY -3 /* only returned by lws_socket_bind() on
134 EADDRINUSE */
135
165lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
166 size_t addrlen);
167
178lws_sa46_compare_ads(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46b);
179
189#if defined(LWS_WITH_IPV6)
190#define lws_sa46_compare_sin6_addr(a, b) memcmp((a)->s6_addr, (b)->s6_addr, 16)
191#endif
192
206lws_sa46_on_net(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46_net,
207 int net_len);
208
209/*
210 * lws_parse_numeric_address() - converts numeric ipv4 or ipv6 to byte address
211 *
212 * \param ads: the numeric ipv4 or ipv6 address string
213 * \param result: result array
214 * \param max_len: max length of result array
215 *
216 * Converts a 1.2.3.4 or 2001:abcd:123:: or ::ffff:1.2.3.4 formatted numeric
217 * address into an array of network ordered byte address elements.
218 *
219 * The parse is as strict as inet_pton(): the string must contain nothing but
220 * the address, and an ipv4 octet may not have a leading zero, since libc's
221 * resolver reads one as octal (to it "012.0.0.1" is 10.0.0.1).
222 *
223 * Returns < 0 on error, else length of result set, either 4 or 16 for ipv4 /
224 * ipv6.
225 */
227lws_parse_numeric_address(const char *ads, uint8_t *result, size_t max_len);
228
247lws_host_is_ambiguous_numeric(const char *host);
248
263lws_host_is_localhost(const char *host);
264
265/*
266 * lws_sa46_parse_numeric_address() - converts numeric ipv4 or ipv6 to sa46
267 *
268 * \param ads: the numeric ipv4 or ipv6 address string
269 * \param sa46: pointer to sa46 to set
270 *
271 * Converts a 1.2.3.4 or 2001:abcd:123:: or ::ffff:1.2.3.4 formatted numeric
272 * address into an sa46, a union of sockaddr_in or sockaddr_in6 depending on
273 * what kind of address was found. sa46->sa4.sin_fmaily will be AF_INET if
274 * ipv4, or AF_INET6 if ipv6.
275 *
276 * Returns 0 if the sa46 was set, else < 0 on error.
277 */
279lws_sa46_parse_numeric_address(const char *ads, lws_sockaddr46 *sa46);
280
293
294/*
295 * Room for any key lws_sa46_host_key() / lws_get_peer_host_key() writes,
296 * including the terminating NUL
297 */
298#define LWS_HOST_KEY_LEN 32
299/* the one key every peer whose address is not IPv4 or IPv6 shares */
300#define LWS_HOST_KEY_UNKNOWN "unknown"
301/*
302 * the one key every peer whose address nothing proved shares, see
303 * lws_get_peer_host_key()
304 */
305#define LWS_HOST_KEY_UNPROVEN "unproven"
306
332lws_sa46_host_key(const lws_sockaddr46 *sa46, char *key, size_t len);
333
361lws_get_peer_host_key(struct lws *wsi, char *key, size_t len);
362
363/*
364 * The peer address stack
365 *
366 * Who a connection's peer is may be known at several layers, each believed
367 * to a different degree: the transport's own peer (the address a TCP
368 * handshake completed with, or a datagram's source), and above it a claim
369 * about the real client (eg, the PROXY v2 header of a trusted front). lws
370 * keeps these as a stack per connection, top (the most specific claim)
371 * first, each entry tagged with how far it can be believed.
372 */
373
394
408
417
418/*
419 * The most entries lws_peer_walk() can give: a stream's own claim, a claim
420 * about the client on the connection, and the transport's peer
421 */
422#define LWS_PEER_WALK_MAX 3
423
439lws_peer_walk(struct lws *wsi, lws_peer_entry_t *dest, int max);
440
460lws_peer_effective(struct lws *wsi, lws_peer_entry_t *dest);
461
477
478/* lws_peer_describe() flags */
479#define LWS_PEER_DESC_TOKEN (1 << 0) /* one token, no spaces, see below */
480#define LWS_PEER_DESC_BARE_SINGLE (1 << 1) /* one proven entry: its address */
481
482/*
483 * Room for any description of what lws_peer_walk() can give, including the
484 * terminating NUL
485 */
486#define LWS_PEER_DESC_LEN 224
487
529lws_peer_entries_describe(const lws_peer_entry_t *e, int count, char *buf,
530 size_t len, int flags);
531
558lws_peer_describe(struct lws *wsi, char *buf, size_t len, int flags);
559
588lws_peer_raise(struct lws *wsi, const lws_sockaddr46 *seen,
589 lws_peer_cred_t cred);
590
600
610
625lws_write_numeric_address(const uint8_t *ads, int size, char *buf, size_t len);
626
640lws_sa46_write_numeric_address(lws_sockaddr46 *sa46, char *buf, size_t len);
641
652lws_parse_mac(const char *ads, uint8_t *result_6_bytes);
653
655lws_parse_cidr(const char *cidr, lws_sockaddr46 *sa46, int *len);
656
665lws_is_lan_address(const char *ads);
666
667/*
668 * lws_is_local_address() - checks if the given string represents a local address
669 *
670 * \param ads: the address string to check
671 *
672 * Returns 1 if the address is a recognized local address like "localhost" or "127.0.0.1",
673 * or a name under "localhost" (see lws_host_is_localhost()), else 0.
674 */
676lws_is_local_address(const char *ads);
677
686lws_is_lan_address(const char *ads);
687
struct lws_dll2 lws_dll2_t
unsigned short uint16_t
#define LWS_EXTERN
unsigned char uint8_t
int lws_sockfd_type
#define LWS_WARN_UNUSED_RESULT
#define LWS_VISIBLE
@ LWS_PEER_CRED_VOUCHED
@ LWS_PEER_CRED_NONE
@ LWS_PEER_CRED_ROUTABLE
@ LWS_PEER_CRED_CRYPTO
@ LWS_PEER_CRED_HEARSAY
LWS_VISIBLE LWS_EXTERN int lws_parse_numeric_address(const char *ads, uint8_t *result, size_t max_len)
LWS_VISIBLE LWS_EXTERN const char * lws_get_peer_simple_fd(lws_sockfd_type fd, char *name, size_t namelen)
LWS_VISIBLE LWS_EXTERN int lws_is_local_address(const char *ads)
uint16_t lws_route_uidx_t
LWS_VISIBLE LWS_EXTERN int lws_sa46_parse_numeric_address(const char *ads, lws_sockaddr46 *sa46)
lws_sockaddr46 src
LWS_VISIBLE LWS_EXTERN int lws_parse_mac(const char *ads, uint8_t *result_6_bytes)
LWS_VISIBLE LWS_EXTERN int lws_peer_effective(struct lws *wsi, lws_peer_entry_t *dest)
LWS_VISIBLE LWS_EXTERN int lws_host_is_localhost(const char *host)
LWS_VISIBLE LWS_EXTERN int lws_parse_cidr(const char *cidr, lws_sockaddr46 *sa46, int *len)
lws_dns_score_t score
LWS_VISIBLE LWS_EXTERN const char * lws_get_peer_host_key(struct lws *wsi, char *key, size_t len)
LWS_VISIBLE LWS_EXTERN const char * lws_peer_entries_describe(const lws_peer_entry_t *e, int count, char *buf, size_t len, int flags)
lws_route_t lws_dns_sort_t
LWS_VISIBLE LWS_EXTERN const char *LWS_WARN_UNUSED_RESULT lws_canonical_hostname(struct lws_context *context)
LWS_VISIBLE LWS_EXTERN struct lws_dll2_owner * lws_routing_table_get(struct lws_context *cx)
lws_dll2_t list
LWS_VISIBLE LWS_EXTERN const char * lws_peer_cred_name(lws_peer_cred_t cred)
LWS_VISIBLE LWS_EXTERN const char * lws_peer_kind_name(lws_peer_kind_t kind)
LWS_VISIBLE LWS_EXTERN int lws_peer_raise(struct lws *wsi, const lws_sockaddr46 *seen, lws_peer_cred_t cred)
lws_route_uidx_t uidx
lws_sockaddr46 sa46
lws_sockaddr46 dest
struct lws_peer_entry lws_peer_entry_t
LWS_VISIBLE LWS_EXTERN int lws_peer_entries_effective(const lws_peer_entry_t *e, int count)
enum lws_peer_cred lws_peer_cred_t
LWS_VISIBLE LWS_EXTERN int lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
struct lws_route lws_route_t
LWS_VISIBLE LWS_EXTERN int lws_host_is_ambiguous_numeric(const char *host)
LWS_VISIBLE LWS_EXTERN int lws_sa46_on_net(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46_net, int net_len)
enum lws_peer_kind lws_peer_kind_t
lws_sockaddr46 gateway
LWS_VISIBLE LWS_EXTERN int lws_peer_walk(struct lws *wsi, lws_peer_entry_t *dest, int max)
LWS_VISIBLE LWS_EXTERN int lws_sa46_compare_ads(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46b)
LWS_VISIBLE LWS_EXTERN int lws_write_numeric_address(const uint8_t *ads, int size, char *buf, size_t len)
struct lws_dns_score lws_dns_score_t
LWS_VISIBLE LWS_EXTERN int lws_sa46_host_key(const lws_sockaddr46 *sa46, char *key, size_t len)
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name, int name_len, char *rip, int rip_len)
LWS_VISIBLE LWS_EXTERN int lws_is_lan_address(const char *ads)
LWS_VISIBLE LWS_EXTERN const char * lws_get_peer_simple(struct lws *wsi, char *name, size_t namelen)
LWS_VISIBLE LWS_EXTERN int lws_sa46_write_numeric_address(lws_sockaddr46 *sa46, char *buf, size_t len)
@ LWS_PEER_KIND_XFF
@ LWS_PEER_KIND_SOCKET
@ LWS_PEER_KIND_DATAGRAM
@ LWS_PEER_KIND_LOCAL
@ LWS_PEER_KIND_RELAY
@ LWS_PEER_KIND_PROXY_V2
LWS_VISIBLE LWS_EXTERN const char * lws_peer_describe(struct lws *wsi, char *buf, size_t len, int flags)
LWS_VISIBLE LWS_EXTERN int lws_sa46_is_ipv4_mapped(const lws_sockaddr46 *sa46)