1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2021 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 conmon Connection Latency information
26  * ## Connection Latency information
27  *
28  * When LWS_WITH_CONMON is enabled at build, collects detailed statistics
29  * about the client connection setup latency, available to the connection
30  * itself
31  */
32 ///@{
33 
34 /* enough for 4191us, or just over an hour */
35 typedef uint32_t lws_conmon_interval_us_t;
36 
37 /*
38  * Connection latency information... note that not all wsi actually make
39  * connections, for example h2 streams after the initial one will have 0
40  * for everything except ciu_txn_resp.
41  *
42  * If represented in JSON, it should look like this
43  *
44  *     {
45  *        "peer": "46.105.127.147",
46  *        "dns_us": 1234,
47  *        "sockconn_us": 1234,
48  *        "tls_us": 1234,
49  *        "txn_resp_us": 1234,
50  *        "dns":["46.105.127.147", "2001:41d0:2:ee93::1"]
51  *      }
52  */
53 
54 struct lws_conmon {
55 	lws_sockaddr46				peer46;
56 	/**< The peer we actually connected to, if any.  .peer46.sa4.sa_family
57 	 * is either 0 if invalid, or the AF_ */
58 
59 	struct addrinfo				*dns_results_copy;
60 	/**< NULL, or Allocated copy of dns results, owned by this object and
61 	 * freed when object destroyed.
62 	 * Only set if client flag LCCSCF_CONMON applied  */
63 
64 	lws_conmon_interval_us_t		ciu_dns;
65 	/**< 0, or if a socket connection, us taken to acquire this DNS response
66 	 *
67 	 */
68 	lws_conmon_interval_us_t		ciu_sockconn;
69 	/**< 0, or if connection-based, the us interval between the socket
70 	 * connect() attempt that succeeded, and the connection setup */
71 	lws_conmon_interval_us_t		ciu_tls;
72 	/**< 0 if no tls, or us taken to establish the tls tunnel */
73 	lws_conmon_interval_us_t		ciu_txn_resp;
74 	/**< 0, or if the protocol supports transactions, the interval between
75 	 * sending the transaction request and starting to receive the resp */
76 };
77 
78 /**
79  * lws_conmon_wsi_take() - create a connection latency object from client wsi
80  *
81  * \param context: lws wsi
82  * \param dest: conmon struct to fill
83  *
84  * Copies wsi conmon data into the caller's struct.  Passes ownership of
85  * any allocations in the addrinfo list to the caller, lws will not delete that
86  * any more on wsi close after this call.  The caller must call
87  * lws_conmon_release() on the struct to destroy any addrinfo in the struct
88  * that is prepared by this eventually but it can defer it as long as it wants.
89  *
90  * Other than the addrinfo list, the contents of the returned object are
91  * completely selfcontained and don't point outside of the object itself, ie,
92  * everything else in there remains in scope while the object itself does.
93  */
94 LWS_VISIBLE LWS_EXTERN void
95 lws_conmon_wsi_take(struct lws *wsi, struct lws_conmon *dest);
96 
97 /**
98  * lws_conmon_release() - free any allocations in the conmon struct
99  *
100  * \param conmon: pointer to conmon struct
101  *
102  * Destroys any allocations in the conmon struct so it can go out of scope.
103  * It doesn't free \p dest itself, it's designed to clean out a struct that
104  * is on the stack or embedded in another object.
105  */
106 LWS_VISIBLE LWS_EXTERN void
107 lws_conmon_release(struct lws_conmon *conmon);
108 
109 ///@}
110