libwebsockets
Lightweight C library for HTML5 websockets
lws-network-helper.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2020 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 typedef uint8_t lws_route_uidx_t;
37 
38 typedef struct lws_dns_score {
39  uint8_t precedence;
40  uint8_t label;
42 
43 /*
44  * This represents an entry in the system routing table
45  */
46 
47 typedef struct lws_route {
48  lws_dll2_t list;
49 
50  lws_sockaddr46 src;
51  lws_sockaddr46 dest;
52  lws_sockaddr46 gateway;
53 
54  struct lws_route *source; /* when used as lws_dns_sort_t */
55  lws_dns_score_t score; /* when used as lws_dns_sort_t */
56 
57  int if_idx;
58  int priority;
59  int ifa_flags; /* if source_ads */
60 
61  lws_route_uidx_t uidx; /* unique index for this route */
62 
63  uint8_t proto;
64  uint8_t dest_len;
65  uint8_t src_len;
66  uint8_t scope; /* if source_ads */
67  uint8_t af; /* if source_ads */
68 
69  uint8_t source_ads:1;
70 } lws_route_t;
71 
72 /*
73  * We reuse the route object as the dns sort granule, so there's only one
74  * struct needs to know all the gnarly ipv6 details
75  */
76 
78 
88 LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
89 lws_canonical_hostname(struct lws_context *context);
90 
105 LWS_VISIBLE LWS_EXTERN void
106 lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
107  int name_len, char *rip, int rip_len);
108 
119 LWS_VISIBLE LWS_EXTERN const char *
120 lws_get_peer_simple(struct lws *wsi, char *name, size_t namelen);
121 
122 LWS_VISIBLE LWS_EXTERN const char *
123 lws_get_peer_simple_fd(lws_sockfd_type fd, char *name, size_t namelen);
124 
125 #define LWS_ITOSA_USABLE 0
126 #define LWS_ITOSA_NOT_EXIST -1
127 #define LWS_ITOSA_NOT_USABLE -2
128 #define LWS_ITOSA_BUSY -3 /* only returned by lws_socket_bind() on
129  EADDRINUSE */
130 
131 #if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE)
152 LWS_VISIBLE LWS_EXTERN int
153 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
154  size_t addrlen);
155 #endif
156 
166 LWS_VISIBLE LWS_EXTERN int
167 lws_sa46_compare_ads(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46b);
168 
181 LWS_VISIBLE LWS_EXTERN int
182 lws_sa46_on_net(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46_net,
183  int net_len);
184 
185 /*
186  * lws_parse_numeric_address() - converts numeric ipv4 or ipv6 to byte address
187  *
188  * \param ads: the numeric ipv4 or ipv6 address string
189  * \param result: result array
190  * \param max_len: max length of result array
191  *
192  * Converts a 1.2.3.4 or 2001:abcd:123:: or ::ffff:1.2.3.4 formatted numeric
193  * address into an array of network ordered byte address elements.
194  *
195  * Returns < 0 on error, else length of result set, either 4 or 16 for ipv4 /
196  * ipv6.
197  */
198 LWS_VISIBLE LWS_EXTERN int
199 lws_parse_numeric_address(const char *ads, uint8_t *result, size_t max_len);
200 
201 /*
202  * lws_sa46_parse_numeric_address() - converts numeric ipv4 or ipv6 to sa46
203  *
204  * \param ads: the numeric ipv4 or ipv6 address string
205  * \param sa46: pointer to sa46 to set
206  *
207  * Converts a 1.2.3.4 or 2001:abcd:123:: or ::ffff:1.2.3.4 formatted numeric
208  * address into an sa46, a union of sockaddr_in or sockaddr_in6 depending on
209  * what kind of address was found. sa46->sa4.sin_fmaily will be AF_INET if
210  * ipv4, or AF_INET6 if ipv6.
211  *
212  * Returns 0 if the sa46 was set, else < 0 on error.
213  */
214 LWS_VISIBLE LWS_EXTERN int
215 lws_sa46_parse_numeric_address(const char *ads, lws_sockaddr46 *sa46);
216 
229 LWS_VISIBLE LWS_EXTERN int
230 lws_write_numeric_address(const uint8_t *ads, int size, char *buf, size_t len);
231 
243 LWS_VISIBLE LWS_EXTERN int
244 lws_sa46_write_numeric_address(lws_sockaddr46 *sa46, char *buf, size_t len);
245 
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_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
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)
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)
Definition: lws-dll2.h:197
Definition: lws-network-helper.h:38
Definition: lws-network-helper.h:47
Definition: lws-adopt.h:86