libwebsockets
Lightweight C library for HTML5 websockets
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 
31 
32 #if defined(LWS_ESP_PLATFORM)
33 #include <lwip/sockets.h>
34 #endif
35 
36 /* cope with large amounts of route information */
38 
39 typedef struct lws_dns_score {
43 
44 /*
45  * This represents an entry in the system routing table
46  */
47 
48 typedef struct lws_route {
50 
54 
55  struct lws_route *source; /* when used as lws_dns_sort_t */
56  lws_dns_score_t score; /* when used as lws_dns_sort_t */
57 
58  int if_idx;
59  int priority;
60  int ifa_flags; /* if source_ads */
61 
62  lws_route_uidx_t uidx; /* unique index for this route */
63 
67  uint8_t scope; /* if source_ads */
68  uint8_t af; /* if source_ads */
69 
72 
73 /*
74  * We reuse the route object as the dns sort granule, so there's only one
75  * struct needs to know all the gnarly ipv6 details
76  */
77 
79 
90 lws_canonical_hostname(struct lws_context *context);
91 
107 lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
108  int name_len, char *rip, int rip_len);
109 
120 LWS_VISIBLE LWS_EXTERN const char *
121 lws_get_peer_simple(struct lws *wsi, char *name, size_t namelen);
122 
123 LWS_VISIBLE LWS_EXTERN const char *
124 lws_get_peer_simple_fd(lws_sockfd_type fd, char *name, size_t namelen);
125 
126 #define LWS_ITOSA_USABLE 0
127 #define LWS_ITOSA_NOT_EXIST -1
128 #define LWS_ITOSA_NOT_USABLE -2
129 #define LWS_ITOSA_BUSY -3 /* only returned by lws_socket_bind() on
130  EADDRINUSE */
131 
132 #if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE)
154 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
155  size_t addrlen);
156 #endif
157 
168 lws_sa46_compare_ads(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46b);
169 
183 lws_sa46_on_net(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46_net,
184  int net_len);
185 
186 /*
187  * lws_parse_numeric_address() - converts numeric ipv4 or ipv6 to byte address
188  *
189  * \param ads: the numeric ipv4 or ipv6 address string
190  * \param result: result array
191  * \param max_len: max length of result array
192  *
193  * Converts a 1.2.3.4 or 2001:abcd:123:: or ::ffff:1.2.3.4 formatted numeric
194  * address into an array of network ordered byte address elements.
195  *
196  * Returns < 0 on error, else length of result set, either 4 or 16 for ipv4 /
197  * ipv6.
198  */
200 lws_parse_numeric_address(const char *ads, uint8_t *result, size_t max_len);
201 
202 /*
203  * lws_sa46_parse_numeric_address() - converts numeric ipv4 or ipv6 to sa46
204  *
205  * \param ads: the numeric ipv4 or ipv6 address string
206  * \param sa46: pointer to sa46 to set
207  *
208  * Converts a 1.2.3.4 or 2001:abcd:123:: or ::ffff:1.2.3.4 formatted numeric
209  * address into an sa46, a union of sockaddr_in or sockaddr_in6 depending on
210  * what kind of address was found. sa46->sa4.sin_fmaily will be AF_INET if
211  * ipv4, or AF_INET6 if ipv6.
212  *
213  * Returns 0 if the sa46 was set, else < 0 on error.
214  */
216 lws_sa46_parse_numeric_address(const char *ads, lws_sockaddr46 *sa46);
217 
232 lws_write_numeric_address(const uint8_t *ads, int size, char *buf, size_t len);
233 
247 lws_sa46_write_numeric_address(lws_sockaddr46 *sa46, char *buf, size_t len);
248 
259 lws_parse_mac(const char *ads, uint8_t *result_6_bytes);
260 
struct lws_route * source
lws_sockaddr46 src
lws_dns_score_t score
uint8_t dest_len
lws_dll2_t list
lws_route_uidx_t uidx
lws_sockaddr46 dest
lws_sockaddr46 gateway
uint8_t source_ads
LWS_VISIBLE LWS_EXTERN void 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_parse_numeric_address(const char *ads, uint8_t *result, size_t max_len)
uint16_t lws_route_uidx_t
LWS_VISIBLE LWS_EXTERN int lws_sa46_parse_numeric_address(const char *ads, lws_sockaddr46 *sa46)
LWS_VISIBLE LWS_EXTERN int lws_parse_mac(const char *ads, uint8_t *result_6_bytes)
LWS_VISIBLE LWS_EXTERN const char * lws_get_peer_simple_fd(lws_sockfd_type fd, char *name, size_t namelen)
lws_route_t lws_dns_sort_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_sa46_on_net(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46_net, int net_len)
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 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_VISIBLE LWS_EXTERN const char *LWS_WARN_UNUSED_RESULT lws_canonical_hostname(struct lws_context *context)
unsigned short uint16_t
#define LWS_EXTERN
unsigned char uint8_t
int lws_sockfd_type
#define LWS_WARN_UNUSED_RESULT
#define LWS_VISIBLE