• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 /** \defgroup net Network related helper APIs
26  * ##Network related helper APIs
27  *
28  * These wrap miscellaneous useful network-related functions
29  */
30 ///@{
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;
41 } lws_dns_score_t;
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 
77 typedef lws_route_t lws_dns_sort_t;
78 
79 /**
80  * lws_canonical_hostname() - returns this host's hostname
81  *
82  * This is typically used by client code to fill in the host parameter
83  * when making a client connection.  You can only call it after the context
84  * has been created.
85  *
86  * \param context:	Websocket context
87  */
88 LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
89 lws_canonical_hostname(struct lws_context *context);
90 
91 /**
92  * lws_get_peer_addresses() - Get client address information
93  * \param wsi:	Local struct lws associated with
94  * \param fd:		Connection socket descriptor
95  * \param name:	Buffer to take client address name
96  * \param name_len:	Length of client address name buffer
97  * \param rip:	Buffer to take client address IP dotted quad
98  * \param rip_len:	Length of client address IP buffer
99  *
100  *	This function fills in name and rip with the name and IP of
101  *	the client connected with socket descriptor fd.  Names may be
102  *	truncated if there is not enough room.  If either cannot be
103  *	determined, they will be returned as valid zero-length strings.
104  */
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 
109 /**
110  * lws_get_peer_simple() - Get client address information without RDNS
111  *
112  * \param wsi:	Local struct lws associated with
113  * \param name:	Buffer to take client address name
114  * \param namelen:	Length of client address name buffer
115  *
116  * This provides a 123.123.123.123 type IP address in name from the
117  * peer that has connected to wsi
118  */
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)
132 /**
133  * lws_interface_to_sa() - Convert interface name or IP to sockaddr struct
134  *
135  * \param ipv6:		Allow IPV6 addresses
136  * \param ifname:	Interface name or IP
137  * \param addr:		struct sockaddr_in * to be written
138  * \param addrlen:	Length of addr
139  *
140  * This converts a textual network interface name to a sockaddr usable by
141  * other network functions.
142  *
143  * If the network interface doesn't exist, it will return LWS_ITOSA_NOT_EXIST.
144  *
145  * If the network interface is not usable, eg ethernet cable is removed, it
146  * may logically exist but not have any IP address.  As such it will return
147  * LWS_ITOSA_NOT_USABLE.
148  *
149  * If the network interface exists and is usable, it will return
150  * LWS_ITOSA_USABLE.
151  */
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 
157 /**
158  * lws_sa46_compare_ads() - checks if two sa46 have the same address
159  *
160  * \param sa46a: first
161  * \param sa46b: second
162  *
163  * Returns 0 if the address family is INET or INET6 and the address is the same,
164  * or if the AF is the same but not INET or INET6, otherwise nonzero.
165  */
166 LWS_VISIBLE LWS_EXTERN int
167 lws_sa46_compare_ads(const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46b);
168 
169 /**
170  * lws_sa46_on_net() - checks if an sa46 is on the subnet represented by another
171  *
172  * \param sa46a: first
173  * \param sa46_net: network
174  * \param net_len: length of network non-mask
175  *
176  * Returns 0 if sa46a belongs on network sa46_net/net_len
177  *
178  * If there is an ipv4 / v6 mismatch between the ip and the net, the ipv4
179  * address is promoted to ::ffff:x.x.x.x before the comparison.
180  */
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 
217 /**
218  * lws_write_numeric_address() - convert network byte order ads to text
219  *
220  * \param ads: network byte order address array
221  * \param size: number of bytes valid in ads
222  * \param buf: result buffer to take text format
223  * \param len: max size of text buffer
224  *
225  * Converts an array of network-ordered byte address elements to a textual
226  * representation of the numeric address, like "1.2.3.4" or "::1".  Returns the
227  * number of chars written into buf, else < 0.  ipv6 only supported with
228  * LWS_IPV6=1 at cmake.
229  */
230 LWS_VISIBLE LWS_EXTERN int
231 lws_write_numeric_address(const uint8_t *ads, int size, char *buf, size_t len);
232 
233 /**
234  * lws_sa46_write_numeric_address() - convert sa46 ads to textual numeric ads
235  *
236  * \param sa46: the sa46 whose address to show
237  * \param buf: result buffer to take text format
238  * \param len: max size of text buffer
239  *
240  * Converts the ipv4 or ipv6 address in an lws_sockaddr46 to a textual
241  * representation of the numeric address, like "1.2.3.4" or "::1".  Returns the
242  * number of chars written into buf, else < 0.  ipv6 only supported with
243  * LWS_IPV6=1 at cmake.
244  */
245 LWS_VISIBLE LWS_EXTERN int
246 lws_sa46_write_numeric_address(lws_sockaddr46 *sa46, char *buf, size_t len);
247 
248 ///@}
249