• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 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 #include <private-lib-core.h>
26 
27 static int
rops_handle_POLLIN_raw_file(struct lws_context_per_thread * pt,struct lws * wsi,struct lws_pollfd * pollfd)28 rops_handle_POLLIN_raw_file(struct lws_context_per_thread *pt, struct lws *wsi,
29 			    struct lws_pollfd *pollfd)
30 {
31 	int n;
32 
33 	if (pollfd->revents & LWS_POLLOUT) {
34 		n = lws_callback_as_writeable(wsi);
35 		if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
36 			lwsl_info("failed at set pollfd\n");
37 			return LWS_HPI_RET_WSI_ALREADY_DIED;
38 		}
39 		if (n)
40 			return LWS_HPI_RET_PLEASE_CLOSE_ME;
41 	}
42 
43 	if (pollfd->revents & LWS_POLLIN) {
44 		if (user_callback_handle_rxflow(wsi->protocol->callback,
45 						wsi, LWS_CALLBACK_RAW_RX_FILE,
46 						wsi->user_space, NULL, 0)) {
47 			lwsl_debug("raw rx callback closed it\n");
48 			return LWS_HPI_RET_PLEASE_CLOSE_ME;
49 		}
50 	}
51 
52 	if (pollfd->revents & LWS_POLLHUP)
53 		if (!(pollfd->revents & LWS_POLLIN))
54 			return LWS_HPI_RET_PLEASE_CLOSE_ME;
55 
56 	return LWS_HPI_RET_HANDLED;
57 }
58 
59 static int
rops_adoption_bind_raw_file(struct lws * wsi,int type,const char * vh_prot_name)60 rops_adoption_bind_raw_file(struct lws *wsi, int type, const char *vh_prot_name)
61 {
62 	/* no socket or http: it can only be a raw file */
63 	if ((type & LWS_ADOPT_HTTP) || (type & LWS_ADOPT_SOCKET) ||
64 	    (type & _LWS_ADOPT_FINISH))
65 		return 0; /* no match */
66 
67 	lws_role_transition(wsi, 0, LRS_ESTABLISHED, &role_ops_raw_file);
68 
69 	if (!vh_prot_name) {
70 		if (wsi->vhost->default_protocol_index >=
71 		    wsi->vhost->count_protocols)
72 			return 0;
73 
74 		wsi->protocol = &wsi->vhost->protocols[
75 					wsi->vhost->default_protocol_index];
76 	}
77 
78 	return 1; /* bound */
79 }
80 
81 const struct lws_role_ops role_ops_raw_file = {
82 	/* role name */			"raw-file",
83 	/* alpn id */			NULL,
84 	/* check_upgrades */		NULL,
85 	/* pt_init_destroy */		NULL,
86 	/* init_vhost */		NULL,
87 	/* destroy_vhost */		NULL,
88 	/* service_flag_pending */	NULL,
89 	/* handle_POLLIN */		rops_handle_POLLIN_raw_file,
90 	/* handle_POLLOUT */		NULL,
91 	/* perform_user_POLLOUT */	NULL,
92 	/* callback_on_writable */	NULL,
93 	/* tx_credit */			NULL,
94 	/* write_role_protocol */	NULL,
95 	/* encapsulation_parent */	NULL,
96 	/* alpn_negotiated */		NULL,
97 	/* close_via_role_protocol */	NULL,
98 	/* close_role */		NULL,
99 	/* close_kill_connection */	NULL,
100 	/* destroy_role */		NULL,
101 	/* adoption_bind */		rops_adoption_bind_raw_file,
102 	/* client_bind */		NULL,
103 	/* issue_keepalive */		NULL,
104 	/* adoption_cb clnt, srv */	{ LWS_CALLBACK_RAW_ADOPT_FILE,
105 					  LWS_CALLBACK_RAW_ADOPT_FILE },
106 	/* rx_cb clnt, srv */		{ LWS_CALLBACK_RAW_RX_FILE,
107 					  LWS_CALLBACK_RAW_RX_FILE },
108 	/* writeable cb clnt, srv */	{ LWS_CALLBACK_RAW_WRITEABLE_FILE,
109 					  LWS_CALLBACK_RAW_WRITEABLE_FILE},
110 	/* close cb clnt, srv */	{ LWS_CALLBACK_RAW_CLOSE_FILE,
111 					  LWS_CALLBACK_RAW_CLOSE_FILE},
112 	/* protocol_bind cb c, srv */	{ LWS_CALLBACK_RAW_FILE_BIND_PROTOCOL,
113 					  LWS_CALLBACK_RAW_FILE_BIND_PROTOCOL },
114 	/* protocol_unbind cb c, srv */	{ LWS_CALLBACK_RAW_FILE_DROP_PROTOCOL,
115 					  LWS_CALLBACK_RAW_FILE_DROP_PROTOCOL },
116 	/* file_handle */		1,
117 };
118