1 /* 2 * libwebsockets - small server side websockets and web server implementation 3 * 4 * Copyright (C) 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 * C++ classes for Secure Streams 25 */ 26 27 #include <map> 28 #include <set> 29 #include <list> 30 #include <string> 31 #include <vector> 32 #include <exception> 33 34 #include "libwebsockets.h" 35 36 class lss; 37 38 /* 39 * Exception subclass for lss-specific issues 40 */ 41 42 class lssException : public std::exception 43 { 44 private: 45 std::string details; 46 public: lssException(std::string _details)47 lssException(std::string _details) { details = _details; } ~lssException()48 ~lssException() throw() { } what() const49 virtual const char *what() const throw() { return details.c_str(); } 50 }; 51 52 typedef struct lssbuf { 53 uint8_t *buf; 54 size_t len; 55 } lssbuf_t; 56 57 class lssAc 58 { 59 private: 60 struct lwsac *ac; 61 struct lwsac *iter; lssAc()62 lssAc() { ac = NULL; } ~lssAc()63 ~lssAc() { lwsac_free(&ac); } 64 65 public: 66 void append(lssbuf_t *lb); 67 void start(bool atomic); 68 int get(lssbuf_t *lb); 69 }; 70 71 /* 72 * Fixed userdata priv used with ss creation... userdata lives in the lss 73 * subclasses' members 74 */ 75 76 class lssPriv 77 { 78 public: 79 struct lws_ss_handle *m_ss; 80 void *m_plss; 81 }; 82 83 #define userobj_to_lss(uo) ((lss *)(((lssPriv *)userobj)->m_plss)) 84 85 /* 86 * The completion callback... it's called once, and state will be one of 87 * 88 * LWSSSCS_QOS_ACK_REMOTE: it completed OK 89 * LWSSSCS_DESTROYING: we didn't complete 90 * LWSSSCS_ALL_RETRIES_FAILED: " 91 * LWSSSCS_QOS_NACK_REMOTE: " 92 */ 93 94 typedef int (*lsscomp_t)(lss *lss, lws_ss_constate_t state, void *arg); 95 96 /* 97 * Base class for Secure Stream objects 98 */ 99 100 class lss 101 { 102 public: 103 lss(lws_ctx_t _ctx, std::string _uri, lsscomp_t _comp, bool _psh, 104 lws_sscb_rx rx, lws_sscb_tx tx, lws_sscb_state state); 105 virtual ~lss(); 106 int call_completion(lws_ss_constate_t state); 107 108 lsscomp_t comp; 109 struct lws_ss_handle *m_ss; 110 uint64_t rxlen; 111 lws_usec_t us_start; 112 113 private: 114 lws_ctx_t ctx; 115 char *uri; 116 lws_ss_policy_t pol; 117 bool comp_done; 118 }; 119 120 /* 121 * Subclass of lss for atomic messages on heap 122 */ 123 124 class lssMsg : public lss 125 { 126 public: 127 lssMsg(lws_ctx_t _ctx, lsscomp_t _comp, std::string _uri); 128 virtual ~lssMsg(); 129 }; 130 131 /* 132 * Subclass of lss for file transactions 133 */ 134 135 class lssFile : public lss 136 { 137 public: 138 lssFile(lws_ctx_t _ctx, std::string _uri, std::string _path, 139 lsscomp_t _comp, bool _psh); 140 virtual ~lssFile(); 141 lws_ss_state_return_t write(const uint8_t *buf, size_t len, int flags); 142 143 std::string path; 144 145 private: 146 lws_filefd_type fd; 147 bool push; 148 }; 149