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 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
35 lwsl_wsi_info(wsi, "failed at set pollfd");
36 return LWS_HPI_RET_WSI_ALREADY_DIED;
37 }
38 n = lws_callback_as_writeable(wsi);
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->a.protocol->callback,
45 wsi, LWS_CALLBACK_RAW_RX_FILE,
46 wsi->user_space, NULL, 0)) {
47 lwsl_wsi_debug(wsi, "raw rx callback closed it");
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->a.vhost->default_protocol_index >=
71 wsi->a.vhost->count_protocols)
72 return 0;
73
74 wsi->a.protocol = &wsi->a.vhost->protocols[
75 wsi->a.vhost->default_protocol_index];
76 }
77
78 return 1; /* bound */
79 }
80
81 static const lws_rops_t rops_table_raw_file[] = {
82 /* 1 */ { .handle_POLLIN = rops_handle_POLLIN_raw_file },
83 /* 2 */ { .adoption_bind = rops_adoption_bind_raw_file },
84 };
85
86 const struct lws_role_ops role_ops_raw_file = {
87 /* role name */ "raw-file",
88 /* alpn id */ NULL,
89
90 /* rops_table */ rops_table_raw_file,
91 /* rops_idx */ {
92 /* LWS_ROPS_check_upgrades */
93 /* LWS_ROPS_pt_init_destroy */ 0x00,
94 /* LWS_ROPS_init_vhost */
95 /* LWS_ROPS_destroy_vhost */ 0x00,
96 /* LWS_ROPS_service_flag_pending */
97 /* LWS_ROPS_handle_POLLIN */ 0x01,
98 /* LWS_ROPS_handle_POLLOUT */
99 /* LWS_ROPS_perform_user_POLLOUT */ 0x00,
100 /* LWS_ROPS_callback_on_writable */
101 /* LWS_ROPS_tx_credit */ 0x00,
102 /* LWS_ROPS_write_role_protocol */
103 /* LWS_ROPS_encapsulation_parent */ 0x00,
104 /* LWS_ROPS_alpn_negotiated */
105 /* LWS_ROPS_close_via_role_protocol */ 0x00,
106 /* LWS_ROPS_close_role */
107 /* LWS_ROPS_close_kill_connection */ 0x00,
108 /* LWS_ROPS_destroy_role */
109 /* LWS_ROPS_adoption_bind */ 0x02,
110 /* LWS_ROPS_client_bind */
111 /* LWS_ROPS_issue_keepalive */ 0x00,
112 },
113
114 /* adoption_cb clnt, srv */ { LWS_CALLBACK_RAW_ADOPT_FILE,
115 LWS_CALLBACK_RAW_ADOPT_FILE },
116 /* rx_cb clnt, srv */ { LWS_CALLBACK_RAW_RX_FILE,
117 LWS_CALLBACK_RAW_RX_FILE },
118 /* writeable cb clnt, srv */ { LWS_CALLBACK_RAW_WRITEABLE_FILE,
119 LWS_CALLBACK_RAW_WRITEABLE_FILE},
120 /* close cb clnt, srv */ { LWS_CALLBACK_RAW_CLOSE_FILE,
121 LWS_CALLBACK_RAW_CLOSE_FILE},
122 /* protocol_bind cb c, srv */ { LWS_CALLBACK_RAW_FILE_BIND_PROTOCOL,
123 LWS_CALLBACK_RAW_FILE_BIND_PROTOCOL },
124 /* protocol_unbind cb c, srv */ { LWS_CALLBACK_RAW_FILE_DROP_PROTOCOL,
125 LWS_CALLBACK_RAW_FILE_DROP_PROTOCOL },
126 /* file_handle */ 1,
127 };
128