libwebsockets.h 145 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. /** @file */
  22. #ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
  23. #define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
  24. #ifdef __cplusplus
  25. #include <cstddef>
  26. #include <cstdarg>
  27. #ifdef MBED_OPERATORS
  28. #include "mbed-drivers/mbed.h"
  29. #include "sal-iface-eth/EthernetInterface.h"
  30. #include "sockets/TCPListener.h"
  31. #include "sal-stack-lwip/lwipv4_init.h"
  32. namespace {
  33. }
  34. using namespace mbed::Sockets::v0;
  35. struct sockaddr_in;
  36. struct lws;
  37. class lws_conn {
  38. public:
  39. lws_conn():
  40. ts(NULL),
  41. wsi(NULL),
  42. writeable(1),
  43. awaiting_on_writeable(0)
  44. {
  45. }
  46. public:
  47. void set_wsi(struct lws *_wsi) { wsi = _wsi; }
  48. int actual_onRX(Socket *s);
  49. void onRX(Socket *s);
  50. void onError(Socket *s, socket_error_t err);
  51. void onDisconnect(TCPStream *s);
  52. void onSent(Socket *s, uint16_t len);
  53. void serialized_writeable(struct lws *wsi);
  54. public:
  55. TCPStream *ts;
  56. public:
  57. struct lws *wsi;
  58. char writeable;
  59. char awaiting_on_writeable;
  60. };
  61. class lws_conn_listener : lws_conn {
  62. public:
  63. lws_conn_listener():
  64. srv(SOCKET_STACK_LWIP_IPV4)
  65. {
  66. srv.setOnError(TCPStream::ErrorHandler_t(this,
  67. &lws_conn_listener::onError));
  68. }
  69. void start(const uint16_t port); /**< start listening */
  70. protected:
  71. void onRX(Socket *s); /**< incoming data ready */
  72. void onError(Socket *s, socket_error_t err); /**< if error occurs */
  73. void onIncoming(TCPListener *s, void *impl); /**< new connection */
  74. void onDisconnect(TCPStream *s); /**< disconnection */
  75. public:
  76. TCPListener srv;
  77. };
  78. #endif
  79. extern "C" {
  80. #else
  81. #include <stdarg.h>
  82. #endif
  83. #if defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
  84. struct sockaddr_in;
  85. #define LWS_POSIX 0
  86. #else
  87. #define LWS_POSIX 1
  88. #endif
  89. #include "lws_config.h"
  90. #if defined(WIN32) || defined(_WIN32)
  91. #ifndef WIN32_LEAN_AND_MEAN
  92. #define WIN32_LEAN_AND_MEAN
  93. #endif
  94. #include <winsock2.h>
  95. #include <ws2tcpip.h>
  96. #include <stddef.h>
  97. #include <basetsd.h>
  98. #ifndef _WIN32_WCE
  99. #include <fcntl.h>
  100. #else
  101. #define _O_RDONLY 0x0000
  102. #define O_RDONLY _O_RDONLY
  103. #endif
  104. // Visual studio older than 2015 and WIN_CE has only _stricmp
  105. #if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(_WIN32_WCE)
  106. #define strcasecmp _stricmp
  107. #else
  108. #define strcasecmp stricmp
  109. #endif
  110. #define getdtablesize() 30000
  111. #define LWS_INLINE __inline
  112. #define LWS_VISIBLE
  113. #define LWS_WARN_UNUSED_RESULT
  114. #define LWS_WARN_DEPRECATED
  115. #ifdef LWS_DLL
  116. #ifdef LWS_INTERNAL
  117. #define LWS_EXTERN extern __declspec(dllexport)
  118. #else
  119. #define LWS_EXTERN extern __declspec(dllimport)
  120. #endif
  121. #else
  122. #define LWS_EXTERN
  123. #endif
  124. #define LWS_INVALID_FILE INVALID_HANDLE_VALUE
  125. #define LWS_O_RDONLY _O_RDONLY
  126. #if !defined(_MSC_VER) || _MSC_VER < 1900 /* Visual Studio 2015 already defines this in <stdio.h> */
  127. #define lws_snprintf _snprintf
  128. #endif
  129. #ifndef __func__
  130. #define __func__ __FUNCTION__
  131. #endif
  132. #else /* NOT WIN32 */
  133. #include <unistd.h>
  134. #if defined(__NetBSD__) || defined(__FreeBSD__)
  135. #include <netinet/in.h>
  136. #endif
  137. #define LWS_INLINE inline
  138. #define LWS_O_RDONLY O_RDONLY
  139. #if !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
  140. #include <poll.h>
  141. #include <netdb.h>
  142. #define LWS_INVALID_FILE -1
  143. #else
  144. #define getdtablesize() (20)
  145. #define LWS_INVALID_FILE NULL
  146. #endif
  147. #if defined(__GNUC__)
  148. /* warn_unused_result attribute only supported by GCC 3.4 or later */
  149. #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  150. #define LWS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  151. #else
  152. #define LWS_WARN_UNUSED_RESULT
  153. #endif
  154. #define LWS_VISIBLE __attribute__((visibility("default")))
  155. #define LWS_WARN_DEPRECATED __attribute__ ((deprecated))
  156. #else
  157. #define LWS_VISIBLE
  158. #define LWS_WARN_UNUSED_RESULT
  159. #define LWS_WARN_DEPRECATED
  160. #endif
  161. #if defined(__ANDROID__)
  162. #include <unistd.h>
  163. #define getdtablesize() sysconf(_SC_OPEN_MAX)
  164. #endif
  165. #endif
  166. #ifdef LWS_USE_LIBEV
  167. #include <ev.h>
  168. #endif /* LWS_USE_LIBEV */
  169. #ifdef LWS_USE_LIBUV
  170. #include <uv.h>
  171. #ifdef LWS_HAVE_UV_VERSION_H
  172. #include <uv-version.h>
  173. #endif
  174. #endif /* LWS_USE_LIBUV */
  175. #ifndef LWS_EXTERN
  176. #define LWS_EXTERN extern
  177. #endif
  178. #ifdef _WIN32
  179. #define random rand
  180. #else
  181. #include <sys/time.h>
  182. #include <unistd.h>
  183. #endif
  184. #ifdef LWS_OPENSSL_SUPPORT
  185. #ifdef USE_WOLFSSL
  186. #ifdef USE_OLD_CYASSL
  187. #include <cyassl/openssl/ssl.h>
  188. #include <cyassl/error-ssl.h>
  189. #else
  190. #include <wolfssl/openssl/ssl.h>
  191. #include <wolfssl/error-ssl.h>
  192. #endif /* not USE_OLD_CYASSL */
  193. #else
  194. #if defined(LWS_USE_POLARSSL)
  195. #include <polarssl/ssl.h>
  196. struct lws_polarssl_context {
  197. x509_crt ca; /**< ca */
  198. x509_crt certificate; /**< cert */
  199. rsa_context key; /**< key */
  200. };
  201. typedef struct lws_polarssl_context SSL_CTX;
  202. typedef ssl_context SSL;
  203. #else
  204. #if defined(LWS_USE_MBEDTLS)
  205. #include <mbedtls/ssl.h>
  206. #else
  207. #include <openssl/ssl.h>
  208. #include <openssl/err.h>
  209. #endif /* not USE_MBEDTLS */
  210. #endif /* not USE_POLARSSL */
  211. #endif /* not USE_WOLFSSL */
  212. #endif
  213. #define CONTEXT_PORT_NO_LISTEN -1
  214. /** \defgroup log Logging
  215. *
  216. * ##Logging
  217. *
  218. * Lws provides flexible and filterable logging facilities, which can be
  219. * used inside lws and in user code.
  220. *
  221. * Log categories may be individually filtered bitwise, and directed to built-in
  222. * sinks for syslog-compatible logging, or a user-defined function.
  223. */
  224. ///@{
  225. enum lws_log_levels {
  226. LLL_ERR = 1 << 0,
  227. LLL_WARN = 1 << 1,
  228. LLL_NOTICE = 1 << 2,
  229. LLL_INFO = 1 << 3,
  230. LLL_DEBUG = 1 << 4,
  231. LLL_PARSER = 1 << 5,
  232. LLL_HEADER = 1 << 6,
  233. LLL_EXT = 1 << 7,
  234. LLL_CLIENT = 1 << 8,
  235. LLL_LATENCY = 1 << 9,
  236. LLL_COUNT = 10 /* set to count of valid flags */
  237. };
  238. LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...);
  239. LWS_VISIBLE LWS_EXTERN void _lws_logv(int filter, const char *format, va_list vl);
  240. /**
  241. * lwsl_timestamp: generate logging timestamp string
  242. *
  243. * \param level: logging level
  244. * \param p: char * buffer to take timestamp
  245. * \param len: length of p
  246. *
  247. * returns length written in p
  248. */
  249. LWS_VISIBLE LWS_EXTERN int
  250. lwsl_timestamp(int level, char *p, int len);
  251. #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
  252. #if !defined(LWS_WITH_NO_LOGS)
  253. /* notice, warn and log are always compiled in */
  254. #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
  255. #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
  256. #endif
  257. /*
  258. * weaker logging can be deselected at configure time using --disable-debug
  259. * that gets rid of the overhead of checking while keeping _warn and _err
  260. * active
  261. */
  262. #if defined(LWS_WITH_ESP8266)
  263. #undef _DEBUG
  264. #endif
  265. #ifdef _DEBUG
  266. #if defined(LWS_WITH_NO_LOGS)
  267. /* notice, warn and log are always compiled in */
  268. //#define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
  269. #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
  270. #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
  271. #endif
  272. #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
  273. #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
  274. #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
  275. #define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
  276. #define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
  277. #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
  278. #define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
  279. /**
  280. * lwsl_hexdump() - helper to hexdump a buffer (DEBUG builds only)
  281. *
  282. * \param buf: buffer start to dump
  283. * \param len: length of buffer to dump
  284. */
  285. LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len);
  286. #else /* no debug */
  287. #if defined(LWS_WITH_NO_LOGS)
  288. //#define lwsl_err(...) do {} while(0)
  289. #define lwsl_warn(...) do {} while(0)
  290. #define lwsl_notice(...) do {} while(0)
  291. #endif
  292. #define lwsl_info(...) do {} while(0)
  293. #define lwsl_debug(...) do {} while(0)
  294. #define lwsl_parser(...) do {} while(0)
  295. #define lwsl_header(...) do {} while(0)
  296. #define lwsl_ext(...) do {} while(0)
  297. #define lwsl_client(...) do {} while(0)
  298. #define lwsl_latency(...) do {} while(0)
  299. #define lwsl_hexdump(a, b)
  300. #endif
  301. /**
  302. * lws_set_log_level() - Set the logging bitfield
  303. * \param level: OR together the LLL_ debug contexts you want output from
  304. * \param log_emit_function: NULL to leave it as it is, or a user-supplied
  305. * function to perform log string emission instead of
  306. * the default stderr one.
  307. *
  308. * log level defaults to "err", "warn" and "notice" contexts enabled and
  309. * emission on stderr.
  310. */
  311. LWS_VISIBLE LWS_EXTERN void
  312. lws_set_log_level(int level,
  313. void (*log_emit_function)(int level, const char *line));
  314. /**
  315. * lwsl_emit_syslog() - helper log emit function writes to system log
  316. *
  317. * \param level: one of LLL_ log level indexes
  318. * \param line: log string
  319. *
  320. * You use this by passing the function pointer to lws_set_log_level(), to set
  321. * it as the log emit function, it is not called directly.
  322. */
  323. LWS_VISIBLE LWS_EXTERN void
  324. lwsl_emit_syslog(int level, const char *line);
  325. ///@}
  326. #include <stddef.h>
  327. #ifndef lws_container_of
  328. #define lws_container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
  329. #endif
  330. struct lws;
  331. #ifndef ARRAY_SIZE
  332. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  333. #endif
  334. /* api change list for user code to test against */
  335. #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
  336. /* the struct lws_protocols has the id field present */
  337. #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
  338. /* you can call lws_get_peer_write_allowance */
  339. #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
  340. /* extra parameter introduced in 917f43ab821 */
  341. #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
  342. /* File operations stuff exists */
  343. #define LWS_FEATURE_FOPS
  344. #if defined(_WIN32)
  345. typedef SOCKET lws_sockfd_type;
  346. typedef HANDLE lws_filefd_type;
  347. #define lws_sockfd_valid(sfd) (!!sfd)
  348. struct lws_pollfd {
  349. lws_sockfd_type fd; /**< file descriptor */
  350. SHORT events; /**< which events to respond to */
  351. SHORT revents; /**< which events happened */
  352. };
  353. #define LWS_POLLHUP (FD_CLOSE)
  354. #define LWS_POLLIN (FD_READ | FD_ACCEPT)
  355. #define LWS_POLLOUT (FD_WRITE)
  356. #else
  357. #if defined(MBED_OPERATORS)
  358. /* it's a class lws_conn * */
  359. typedef void * lws_sockfd_type;
  360. typedef void * lws_filefd_type;
  361. #define lws_sockfd_valid(sfd) (!!sfd)
  362. struct pollfd {
  363. lws_sockfd_type fd; /**< fd related to */
  364. short events; /**< which POLL... events to respond to */
  365. short revents; /**< which POLL... events occurred */
  366. };
  367. #define POLLIN 0x0001
  368. #define POLLPRI 0x0002
  369. #define POLLOUT 0x0004
  370. #define POLLERR 0x0008
  371. #define POLLHUP 0x0010
  372. #define POLLNVAL 0x0020
  373. struct lws;
  374. void * mbed3_create_tcp_stream_socket(void);
  375. void mbed3_delete_tcp_stream_socket(void *sockfd);
  376. void mbed3_tcp_stream_bind(void *sock, int port, struct lws *);
  377. void mbed3_tcp_stream_accept(void *sock, struct lws *);
  378. #else
  379. #if defined(LWS_WITH_ESP8266)
  380. #include <user_interface.h>
  381. #include <espconn.h>
  382. typedef struct espconn * lws_sockfd_type;
  383. typedef void * lws_filefd_type;
  384. #define lws_sockfd_valid(sfd) (!!sfd)
  385. struct pollfd {
  386. lws_sockfd_type fd; /**< fd related to */
  387. short events; /**< which POLL... events to respond to */
  388. short revents; /**< which POLL... events occurred */
  389. };
  390. #define POLLIN 0x0001
  391. #define POLLPRI 0x0002
  392. #define POLLOUT 0x0004
  393. #define POLLERR 0x0008
  394. #define POLLHUP 0x0010
  395. #define POLLNVAL 0x0020
  396. struct lws_vhost;
  397. lws_sockfd_type esp8266_create_tcp_listen_socket(struct lws_vhost *vh);
  398. void esp8266_tcp_stream_accept(lws_sockfd_type fd, struct lws *wsi);
  399. #include <os_type.h>
  400. #include <osapi.h>
  401. #include "ets_sys.h"
  402. int ets_snprintf(char *str, size_t size, const char *format, ...);
  403. #define snprintf ets_snprintf
  404. typedef os_timer_t uv_timer_t;
  405. typedef void uv_cb_t(uv_timer_t *);
  406. void os_timer_disarm(void *);
  407. void os_timer_setfn(os_timer_t *, os_timer_func_t *, void *);
  408. void ets_timer_arm_new(os_timer_t *, int, int, int);
  409. //void os_timer_arm(os_timer_t *, int, int);
  410. #define UV_VERSION_MAJOR 1
  411. #define lws_uv_getloop(a, b) (NULL)
  412. static inline void uv_timer_init(void *l, uv_timer_t *t)
  413. {
  414. (void)l;
  415. memset(t, 0, sizeof(*t));
  416. os_timer_disarm(t);
  417. }
  418. static inline void uv_timer_start(uv_timer_t *t, uv_cb_t *cb, int first, int rep)
  419. {
  420. os_timer_setfn(t, (os_timer_func_t *)cb, t);
  421. /* ms, repeat */
  422. os_timer_arm(t, first, !!rep);
  423. }
  424. static inline void uv_timer_stop(uv_timer_t *t)
  425. {
  426. os_timer_disarm(t);
  427. }
  428. #else
  429. typedef int lws_sockfd_type;
  430. typedef int lws_filefd_type;
  431. #define lws_sockfd_valid(sfd) (sfd >= 0)
  432. #endif
  433. #endif
  434. #define lws_pollfd pollfd
  435. #define LWS_POLLHUP (POLLHUP|POLLERR)
  436. #define LWS_POLLIN (POLLIN)
  437. #define LWS_POLLOUT (POLLOUT)
  438. #endif
  439. /** struct lws_pollargs - argument structure for all external poll related calls
  440. * passed in via 'in' */
  441. struct lws_pollargs {
  442. lws_sockfd_type fd; /**< applicable socket descriptor */
  443. int events; /**< the new event mask */
  444. int prev_events; /**< the previous event mask */
  445. };
  446. struct lws_tokens;
  447. struct lws_token_limits;
  448. /*! \defgroup wsclose Websocket Close
  449. *
  450. * ##Websocket close frame control
  451. *
  452. * When we close a ws connection, we can send a reason code and a short
  453. * UTF-8 description back with the close packet.
  454. */
  455. ///@{
  456. /*
  457. * NOTE: These public enums are part of the abi. If you want to add one,
  458. * add it at where specified so existing users are unaffected.
  459. */
  460. /** enum lws_close_status - RFC6455 close status codes */
  461. enum lws_close_status {
  462. LWS_CLOSE_STATUS_NOSTATUS = 0,
  463. LWS_CLOSE_STATUS_NORMAL = 1000,
  464. /**< 1000 indicates a normal closure, meaning that the purpose for
  465. which the connection was established has been fulfilled. */
  466. LWS_CLOSE_STATUS_GOINGAWAY = 1001,
  467. /**< 1001 indicates that an endpoint is "going away", such as a server
  468. going down or a browser having navigated away from a page. */
  469. LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002,
  470. /**< 1002 indicates that an endpoint is terminating the connection due
  471. to a protocol error. */
  472. LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003,
  473. /**< 1003 indicates that an endpoint is terminating the connection
  474. because it has received a type of data it cannot accept (e.g., an
  475. endpoint that understands only text data MAY send this if it
  476. receives a binary message). */
  477. LWS_CLOSE_STATUS_RESERVED = 1004,
  478. /**< Reserved. The specific meaning might be defined in the future. */
  479. LWS_CLOSE_STATUS_NO_STATUS = 1005,
  480. /**< 1005 is a reserved value and MUST NOT be set as a status code in a
  481. Close control frame by an endpoint. It is designated for use in
  482. applications expecting a status code to indicate that no status
  483. code was actually present. */
  484. LWS_CLOSE_STATUS_ABNORMAL_CLOSE = 1006,
  485. /**< 1006 is a reserved value and MUST NOT be set as a status code in a
  486. Close control frame by an endpoint. It is designated for use in
  487. applications expecting a status code to indicate that the
  488. connection was closed abnormally, e.g., without sending or
  489. receiving a Close control frame. */
  490. LWS_CLOSE_STATUS_INVALID_PAYLOAD = 1007,
  491. /**< 1007 indicates that an endpoint is terminating the connection
  492. because it has received data within a message that was not
  493. consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
  494. data within a text message). */
  495. LWS_CLOSE_STATUS_POLICY_VIOLATION = 1008,
  496. /**< 1008 indicates that an endpoint is terminating the connection
  497. because it has received a message that violates its policy. This
  498. is a generic status code that can be returned when there is no
  499. other more suitable status code (e.g., 1003 or 1009) or if there
  500. is a need to hide specific details about the policy. */
  501. LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009,
  502. /**< 1009 indicates that an endpoint is terminating the connection
  503. because it has received a message that is too big for it to
  504. process. */
  505. LWS_CLOSE_STATUS_EXTENSION_REQUIRED = 1010,
  506. /**< 1010 indicates that an endpoint (client) is terminating the
  507. connection because it has expected the server to negotiate one or
  508. more extension, but the server didn't return them in the response
  509. message of the WebSocket handshake. The list of extensions that
  510. are needed SHOULD appear in the /reason/ part of the Close frame.
  511. Note that this status code is not used by the server, because it
  512. can fail the WebSocket handshake instead */
  513. LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011,
  514. /**< 1011 indicates that a server is terminating the connection because
  515. it encountered an unexpected condition that prevented it from
  516. fulfilling the request. */
  517. LWS_CLOSE_STATUS_TLS_FAILURE = 1015,
  518. /**< 1015 is a reserved value and MUST NOT be set as a status code in a
  519. Close control frame by an endpoint. It is designated for use in
  520. applications expecting a status code to indicate that the
  521. connection was closed due to a failure to perform a TLS handshake
  522. (e.g., the server certificate can't be verified). */
  523. /****** add new things just above ---^ ******/
  524. LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY = 9999,
  525. };
  526. /**
  527. * lws_close_reason - Set reason and aux data to send with Close packet
  528. * If you are going to return nonzero from the callback
  529. * requesting the connection to close, you can optionally
  530. * call this to set the reason the peer will be told if
  531. * possible.
  532. *
  533. * \param wsi: The websocket connection to set the close reason on
  534. * \param status: A valid close status from websocket standard
  535. * \param buf: NULL or buffer containing up to 124 bytes of auxiliary data
  536. * \param len: Length of data in \param buf to send
  537. */
  538. LWS_VISIBLE LWS_EXTERN void
  539. lws_close_reason(struct lws *wsi, enum lws_close_status status,
  540. unsigned char *buf, size_t len);
  541. ///@}
  542. struct lws;
  543. struct lws_context;
  544. /* needed even with extensions disabled for create context */
  545. struct lws_extension;
  546. /*! \defgroup usercb User Callback
  547. *
  548. * ##User protocol callback
  549. *
  550. * The protocol callback is the primary way lws interacts with
  551. * user code. For one of a list of a few dozen reasons the callback gets
  552. * called at some event to be handled.
  553. *
  554. * All of the events can be ignored, returning 0 is taken as "OK" and returning
  555. * nonzero in most cases indicates that the connection should be closed.
  556. */
  557. ///@{
  558. /*
  559. * NOTE: These public enums are part of the abi. If you want to add one,
  560. * add it at where specified so existing users are unaffected.
  561. */
  562. /** enum lws_callback_reasons - reason you're getting a protocol callback */
  563. enum lws_callback_reasons {
  564. LWS_CALLBACK_ESTABLISHED = 0,
  565. /**< (VH) after the server completes a handshake with an incoming
  566. * client. If you built the library with ssl support, in is a
  567. * pointer to the ssl struct associated with the connection or NULL.*/
  568. LWS_CALLBACK_CLIENT_CONNECTION_ERROR = 1,
  569. /**< the request client connection has been unable to complete a
  570. * handshake with the remote server. If in is non-NULL, you can
  571. * find an error string of length len where it points to
  572. *
  573. * Diagnostic strings that may be returned include
  574. *
  575. * "getaddrinfo (ipv6) failed"
  576. * "unknown address family"
  577. * "getaddrinfo (ipv4) failed"
  578. * "set socket opts failed"
  579. * "insert wsi failed"
  580. * "lws_ssl_client_connect1 failed"
  581. * "lws_ssl_client_connect2 failed"
  582. * "Peer hung up"
  583. * "read failed"
  584. * "HS: URI missing"
  585. * "HS: Redirect code but no Location"
  586. * "HS: URI did not parse"
  587. * "HS: Redirect failed"
  588. * "HS: Server did not return 200"
  589. * "HS: OOM"
  590. * "HS: disallowed by client filter"
  591. * "HS: disallowed at ESTABLISHED"
  592. * "HS: ACCEPT missing"
  593. * "HS: ws upgrade response not 101"
  594. * "HS: UPGRADE missing"
  595. * "HS: Upgrade to something other than websocket"
  596. * "HS: CONNECTION missing"
  597. * "HS: UPGRADE malformed"
  598. * "HS: PROTOCOL malformed"
  599. * "HS: Cannot match protocol"
  600. * "HS: EXT: list too big"
  601. * "HS: EXT: failed setting defaults"
  602. * "HS: EXT: failed parsing defaults"
  603. * "HS: EXT: failed parsing options"
  604. * "HS: EXT: Rejects server options"
  605. * "HS: EXT: unknown ext"
  606. * "HS: Accept hash wrong"
  607. * "HS: Rejected by filter cb"
  608. * "HS: OOM"
  609. * "HS: SO_SNDBUF failed"
  610. * "HS: Rejected at CLIENT_ESTABLISHED"
  611. */
  612. LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH = 2,
  613. /**< this is the last chance for the client user code to examine the
  614. * http headers and decide to reject the connection. If the
  615. * content in the headers is interesting to the
  616. * client (url, etc) it needs to copy it out at
  617. * this point since it will be destroyed before
  618. * the CLIENT_ESTABLISHED call */
  619. LWS_CALLBACK_CLIENT_ESTABLISHED = 3,
  620. /**< after your client connection completed
  621. * a handshake with the remote server */
  622. LWS_CALLBACK_CLOSED = 4,
  623. /**< when the websocket session ends */
  624. LWS_CALLBACK_CLOSED_HTTP = 5,
  625. /**< when a HTTP (non-websocket) session ends */
  626. LWS_CALLBACK_RECEIVE = 6,
  627. /**< data has appeared for this server endpoint from a
  628. * remote client, it can be found at *in and is
  629. * len bytes long */
  630. LWS_CALLBACK_RECEIVE_PONG = 7,
  631. /**< servers receive PONG packets with this callback reason */
  632. LWS_CALLBACK_CLIENT_RECEIVE = 8,
  633. /**< data has appeared from the server for the client connection, it
  634. * can be found at *in and is len bytes long */
  635. LWS_CALLBACK_CLIENT_RECEIVE_PONG = 9,
  636. /**< clients receive PONG packets with this callback reason */
  637. LWS_CALLBACK_CLIENT_WRITEABLE = 10,
  638. /**< If you call lws_callback_on_writable() on a connection, you will
  639. * get one of these callbacks coming when the connection socket
  640. * is able to accept another write packet without blocking.
  641. * If it already was able to take another packet without blocking,
  642. * you'll get this callback at the next call to the service loop
  643. * function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
  644. * and servers get LWS_CALLBACK_SERVER_WRITEABLE. */
  645. LWS_CALLBACK_SERVER_WRITEABLE = 11,
  646. /**< See LWS_CALLBACK_CLIENT_WRITEABLE */
  647. LWS_CALLBACK_HTTP = 12,
  648. /**< an http request has come from a client that is not
  649. * asking to upgrade the connection to a websocket
  650. * one. This is a chance to serve http content,
  651. * for example, to send a script to the client
  652. * which will then open the websockets connection.
  653. * in points to the URI path requested and
  654. * lws_serve_http_file() makes it very
  655. * simple to send back a file to the client.
  656. * Normally after sending the file you are done
  657. * with the http connection, since the rest of the
  658. * activity will come by websockets from the script
  659. * that was delivered by http, so you will want to
  660. * return 1; to close and free up the connection. */
  661. LWS_CALLBACK_HTTP_BODY = 13,
  662. /**< the next len bytes data from the http
  663. * request body HTTP connection is now available in in. */
  664. LWS_CALLBACK_HTTP_BODY_COMPLETION = 14,
  665. /**< the expected amount of http request body has been delivered */
  666. LWS_CALLBACK_HTTP_FILE_COMPLETION = 15,
  667. /**< a file requested to be sent down http link has completed. */
  668. LWS_CALLBACK_HTTP_WRITEABLE = 16,
  669. /**< you can write more down the http protocol link now. */
  670. LWS_CALLBACK_FILTER_NETWORK_CONNECTION = 17,
  671. /**< called when a client connects to
  672. * the server at network level; the connection is accepted but then
  673. * passed to this callback to decide whether to hang up immediately
  674. * or not, based on the client IP. in contains the connection
  675. * socket's descriptor. Since the client connection information is
  676. * not available yet, wsi still pointing to the main server socket.
  677. * Return non-zero to terminate the connection before sending or
  678. * receiving anything. Because this happens immediately after the
  679. * network connection from the client, there's no websocket protocol
  680. * selected yet so this callback is issued only to protocol 0. */
  681. LWS_CALLBACK_FILTER_HTTP_CONNECTION = 18,
  682. /**< called when the request has
  683. * been received and parsed from the client, but the response is
  684. * not sent yet. Return non-zero to disallow the connection.
  685. * user is a pointer to the connection user space allocation,
  686. * in is the URI, eg, "/"
  687. * In your handler you can use the public APIs
  688. * lws_hdr_total_length() / lws_hdr_copy() to access all of the
  689. * headers using the header enums lws_token_indexes from
  690. * libwebsockets.h to check for and read the supported header
  691. * presence and content before deciding to allow the http
  692. * connection to proceed or to kill the connection. */
  693. LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED = 19,
  694. /**< A new client just had
  695. * been connected, accepted, and instantiated into the pool. This
  696. * callback allows setting any relevant property to it. Because this
  697. * happens immediately after the instantiation of a new client,
  698. * there's no websocket protocol selected yet so this callback is
  699. * issued only to protocol 0. Only wsi is defined, pointing to the
  700. * new client, and the return value is ignored. */
  701. LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION = 20,
  702. /**< called when the handshake has
  703. * been received and parsed from the client, but the response is
  704. * not sent yet. Return non-zero to disallow the connection.
  705. * user is a pointer to the connection user space allocation,
  706. * in is the requested protocol name
  707. * In your handler you can use the public APIs
  708. * lws_hdr_total_length() / lws_hdr_copy() to access all of the
  709. * headers using the header enums lws_token_indexes from
  710. * libwebsockets.h to check for and read the supported header
  711. * presence and content before deciding to allow the handshake
  712. * to proceed or to kill the connection. */
  713. LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS = 21,
  714. /**< if configured for
  715. * including OpenSSL support, this callback allows your user code
  716. * to perform extra SSL_CTX_load_verify_locations() or similar
  717. * calls to direct OpenSSL where to find certificates the client
  718. * can use to confirm the remote server identity. user is the
  719. * OpenSSL SSL_CTX* */
  720. LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS = 22,
  721. /**< if configured for
  722. * including OpenSSL support, this callback allows your user code
  723. * to load extra certifcates into the server which allow it to
  724. * verify the validity of certificates returned by clients. user
  725. * is the server's OpenSSL SSL_CTX* */
  726. LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION = 23,
  727. /**< if the libwebsockets vhost was created with the option
  728. * LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
  729. * callback is generated during OpenSSL verification of the cert
  730. * sent from the client. It is sent to protocol[0] callback as
  731. * no protocol has been negotiated on the connection yet.
  732. * Notice that the libwebsockets context and wsi are both NULL
  733. * during this callback. See
  734. * http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
  735. * to understand more detail about the OpenSSL callback that
  736. * generates this libwebsockets callback and the meanings of the
  737. * arguments passed. In this callback, user is the x509_ctx,
  738. * in is the ssl pointer and len is preverify_ok
  739. * Notice that this callback maintains libwebsocket return
  740. * conventions, return 0 to mean the cert is OK or 1 to fail it.
  741. * This also means that if you don't handle this callback then
  742. * the default callback action of returning 0 allows the client
  743. * certificates. */
  744. LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER = 24,
  745. /**< this callback happens
  746. * when a client handshake is being compiled. user is NULL,
  747. * in is a char **, it's pointing to a char * which holds the
  748. * next location in the header buffer where you can add
  749. * headers, and len is the remaining space in the header buffer,
  750. * which is typically some hundreds of bytes. So, to add a canned
  751. * cookie, your handler code might look similar to:
  752. *
  753. * char **p = (char **)in;
  754. *
  755. * if (len < 100)
  756. * return 1;
  757. *
  758. * *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
  759. *
  760. * return 0;
  761. *
  762. * Notice if you add anything, you just have to take care about
  763. * the CRLF on the line you added. Obviously this callback is
  764. * optional, if you don't handle it everything is fine.
  765. *
  766. * Notice the callback is coming to protocols[0] all the time,
  767. * because there is no specific protocol negotiated yet. */
  768. LWS_CALLBACK_CONFIRM_EXTENSION_OKAY = 25,
  769. /**< When the server handshake code
  770. * sees that it does support a requested extension, before
  771. * accepting the extension by additing to the list sent back to
  772. * the client it gives this callback just to check that it's okay
  773. * to use that extension. It calls back to the requested protocol
  774. * and with in being the extension name, len is 0 and user is
  775. * valid. Note though at this time the ESTABLISHED callback hasn't
  776. * happened yet so if you initialize user content there, user
  777. * content during this callback might not be useful for anything.
  778. * Notice this callback comes to protocols[0]. */
  779. LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED = 26,
  780. /**< When a client
  781. * connection is being prepared to start a handshake to a server,
  782. * each supported extension is checked with protocols[0] callback
  783. * with this reason, giving the user code a chance to suppress the
  784. * claim to support that extension by returning non-zero. If
  785. * unhandled, by default 0 will be returned and the extension
  786. * support included in the header to the server. Notice this
  787. * callback comes to protocols[0]. */
  788. LWS_CALLBACK_PROTOCOL_INIT = 27,
  789. /**< One-time call per protocol, per-vhost using it, so it can
  790. * do initial setup / allocations etc */
  791. LWS_CALLBACK_PROTOCOL_DESTROY = 28,
  792. /**< One-time call per protocol, per-vhost using it, indicating
  793. * this protocol won't get used at all after this callback, the
  794. * vhost is getting destroyed. Take the opportunity to
  795. * deallocate everything that was allocated by the protocol. */
  796. LWS_CALLBACK_WSI_CREATE = 29,
  797. /**< outermost (earliest) wsi create notification to protocols[0] */
  798. LWS_CALLBACK_WSI_DESTROY = 30,
  799. /**< outermost (latest) wsi destroy notification to protocols[0] */
  800. LWS_CALLBACK_GET_THREAD_ID = 31,
  801. /**< lws can accept callback when writable requests from other
  802. * threads, if you implement this callback and return an opaque
  803. * current thread ID integer. */
  804. /* external poll() management support */
  805. LWS_CALLBACK_ADD_POLL_FD = 32,
  806. /**< lws normally deals with its poll() or other event loop
  807. * internally, but in the case you are integrating with another
  808. * server you will need to have lws sockets share a
  809. * polling array with the other server. This and the other
  810. * POLL_FD related callbacks let you put your specialized
  811. * poll array interface code in the callback for protocol 0, the
  812. * first protocol you support, usually the HTTP protocol in the
  813. * serving case.
  814. * This callback happens when a socket needs to be
  815. * added to the polling loop: in points to a struct
  816. * lws_pollargs; the fd member of the struct is the file
  817. * descriptor, and events contains the active events
  818. *
  819. * If you are using the internal lws polling / event loop
  820. * you can just ignore these callbacks. */
  821. LWS_CALLBACK_DEL_POLL_FD = 33,
  822. /**< This callback happens when a socket descriptor
  823. * needs to be removed from an external polling array. in is
  824. * again the struct lws_pollargs containing the fd member
  825. * to be removed. If you are using the internal polling
  826. * loop, you can just ignore it. */
  827. LWS_CALLBACK_CHANGE_MODE_POLL_FD = 34,
  828. /**< This callback happens when lws wants to modify the events for
  829. * a connection.
  830. * in is the struct lws_pollargs with the fd to change.
  831. * The new event mask is in events member and the old mask is in
  832. * the prev_events member.
  833. * If you are using the internal polling loop, you can just ignore
  834. * it. */
  835. LWS_CALLBACK_LOCK_POLL = 35,
  836. /**< These allow the external poll changes driven
  837. * by lws to participate in an external thread locking
  838. * scheme around the changes, so the whole thing is threadsafe.
  839. * These are called around three activities in the library,
  840. * - inserting a new wsi in the wsi / fd table (len=1)
  841. * - deleting a wsi from the wsi / fd table (len=1)
  842. * - changing a wsi's POLLIN/OUT state (len=0)
  843. * Locking and unlocking external synchronization objects when
  844. * len == 1 allows external threads to be synchronized against
  845. * wsi lifecycle changes if it acquires the same lock for the
  846. * duration of wsi dereference from the other thread context. */
  847. LWS_CALLBACK_UNLOCK_POLL = 36,
  848. /**< See LWS_CALLBACK_LOCK_POLL, ignore if using lws internal poll */
  849. LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY = 37,
  850. /**< if configured for including OpenSSL support but no private key
  851. * file has been specified (ssl_private_key_filepath is NULL), this is
  852. * called to allow the user to set the private key directly via
  853. * libopenssl and perform further operations if required; this might be
  854. * useful in situations where the private key is not directly accessible
  855. * by the OS, for example if it is stored on a smartcard.
  856. * user is the server's OpenSSL SSL_CTX* */
  857. LWS_CALLBACK_WS_PEER_INITIATED_CLOSE = 38,
  858. /**< The peer has sent an unsolicited Close WS packet. in and
  859. * len are the optional close code (first 2 bytes, network
  860. * order) and the optional additional information which is not
  861. * defined in the standard, and may be a string or non-human- readable data.
  862. * If you return 0 lws will echo the close and then close the
  863. * connection. If you return nonzero lws will just close the
  864. * connection. */
  865. LWS_CALLBACK_WS_EXT_DEFAULTS = 39,
  866. /**< */
  867. LWS_CALLBACK_CGI = 40,
  868. /**< */
  869. LWS_CALLBACK_CGI_TERMINATED = 41,
  870. /**< */
  871. LWS_CALLBACK_CGI_STDIN_DATA = 42,
  872. /**< */
  873. LWS_CALLBACK_CGI_STDIN_COMPLETED = 43,
  874. /**< */
  875. LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP = 44,
  876. /**< */
  877. LWS_CALLBACK_CLOSED_CLIENT_HTTP = 45,
  878. /**< */
  879. LWS_CALLBACK_RECEIVE_CLIENT_HTTP = 46,
  880. /**< */
  881. LWS_CALLBACK_COMPLETED_CLIENT_HTTP = 47,
  882. /**< */
  883. LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ = 48,
  884. /**< */
  885. LWS_CALLBACK_HTTP_BIND_PROTOCOL = 49,
  886. /**< */
  887. LWS_CALLBACK_HTTP_DROP_PROTOCOL = 50,
  888. /**< */
  889. LWS_CALLBACK_CHECK_ACCESS_RIGHTS = 51,
  890. /**< */
  891. LWS_CALLBACK_PROCESS_HTML = 52,
  892. /**< */
  893. LWS_CALLBACK_ADD_HEADERS = 53,
  894. /**< */
  895. LWS_CALLBACK_SESSION_INFO = 54,
  896. /**< */
  897. LWS_CALLBACK_GS_EVENT = 55,
  898. /**< */
  899. LWS_CALLBACK_HTTP_PMO = 56,
  900. /**< per-mount options for this connection, called before
  901. * the normal LWS_CALLBACK_HTTP when the mount has per-mount
  902. * options
  903. */
  904. LWS_CALLBACK_CLIENT_HTTP_WRITEABLE = 57,
  905. /**< when doing an HTTP type client connection, you can call
  906. * lws_client_http_body_pending(wsi, 1) from
  907. * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER to get these callbacks
  908. * sending the HTTP headers.
  909. *
  910. * From this callback, when you have sent everything, you should let
  911. * lws know by calling lws_client_http_body_pending(wsi, 0)
  912. */
  913. /****** add new things just above ---^ ******/
  914. LWS_CALLBACK_USER = 1000,
  915. /**< user code can use any including / above without fear of clashes */
  916. };
  917. /**
  918. * typedef lws_callback_function() - User server actions
  919. * \param wsi: Opaque websocket instance pointer
  920. * \param reason: The reason for the call
  921. * \param user: Pointer to per-session user data allocated by library
  922. * \param in: Pointer used for some callback reasons
  923. * \param len: Length set for some callback reasons
  924. *
  925. * This callback is the way the user controls what is served. All the
  926. * protocol detail is hidden and handled by the library.
  927. *
  928. * For each connection / session there is user data allocated that is
  929. * pointed to by "user". You set the size of this user data area when
  930. * the library is initialized with lws_create_server.
  931. */
  932. typedef int
  933. lws_callback_function(struct lws *wsi, enum lws_callback_reasons reason,
  934. void *user, void *in, size_t len);
  935. ///@}
  936. /*! \defgroup extensions
  937. *
  938. * ##Extension releated functions
  939. *
  940. * Ws defines optional extensions, lws provides the ability to implement these
  941. * in user code if so desired.
  942. *
  943. * We provide one extensions permessage-deflate.
  944. */
  945. ///@{
  946. /*
  947. * NOTE: These public enums are part of the abi. If you want to add one,
  948. * add it at where specified so existing users are unaffected.
  949. */
  950. enum lws_extension_callback_reasons {
  951. LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT = 0,
  952. LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT = 1,
  953. LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT = 2,
  954. LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT = 3,
  955. LWS_EXT_CB_CONSTRUCT = 4,
  956. LWS_EXT_CB_CLIENT_CONSTRUCT = 5,
  957. LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE = 6,
  958. LWS_EXT_CB_CHECK_OK_TO_PROPOSE_EXTENSION = 7,
  959. LWS_EXT_CB_DESTROY = 8,
  960. LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING = 9,
  961. LWS_EXT_CB_ANY_WSI_ESTABLISHED = 10,
  962. LWS_EXT_CB_PACKET_RX_PREPARSE = 11,
  963. LWS_EXT_CB_PACKET_TX_PRESEND = 12,
  964. LWS_EXT_CB_PACKET_TX_DO_SEND = 13,
  965. LWS_EXT_CB_HANDSHAKE_REPLY_TX = 14,
  966. LWS_EXT_CB_FLUSH_PENDING_TX = 15,
  967. LWS_EXT_CB_EXTENDED_PAYLOAD_RX = 16,
  968. LWS_EXT_CB_CAN_PROXY_CLIENT_CONNECTION = 17,
  969. LWS_EXT_CB_1HZ = 18,
  970. LWS_EXT_CB_REQUEST_ON_WRITEABLE = 19,
  971. LWS_EXT_CB_IS_WRITEABLE = 20,
  972. LWS_EXT_CB_PAYLOAD_TX = 21,
  973. LWS_EXT_CB_PAYLOAD_RX = 22,
  974. LWS_EXT_CB_OPTION_DEFAULT = 23,
  975. LWS_EXT_CB_OPTION_SET = 24,
  976. LWS_EXT_CB_OPTION_CONFIRM = 25,
  977. LWS_EXT_CB_NAMED_OPTION_SET = 26,
  978. /****** add new things just above ---^ ******/
  979. };
  980. /** enum lws_ext_options_types */
  981. enum lws_ext_options_types {
  982. EXTARG_NONE, /**< does not take an argument */
  983. EXTARG_DEC, /**< requires a decimal argument */
  984. EXTARG_OPT_DEC /**< may have an optional decimal argument */
  985. /* Add new things just above here ---^
  986. * This is part of the ABI, don't needlessly break compatibility */
  987. };
  988. /** struct lws_ext_options - Option arguments to the extension. These are
  989. * used in the negotiation at ws upgrade time.
  990. * The helper function lws_ext_parse_options()
  991. * uses these to generate callbacks */
  992. struct lws_ext_options {
  993. const char *name; /**< Option name, eg, "server_no_context_takeover" */
  994. enum lws_ext_options_types type; /**< What kind of args the option can take */
  995. /* Add new things just above here ---^
  996. * This is part of the ABI, don't needlessly break compatibility */
  997. };
  998. /** struct lws_ext_option_arg */
  999. struct lws_ext_option_arg {
  1000. const char *option_name; /**< may be NULL, option_index used then */
  1001. int option_index; /**< argument ordinal to use if option_name missing */
  1002. const char *start; /**< value */
  1003. int len; /**< length of value */
  1004. };
  1005. /**
  1006. * typedef lws_extension_callback_function() - Hooks to allow extensions to operate
  1007. * \param context: Websockets context
  1008. * \param ext: This extension
  1009. * \param wsi: Opaque websocket instance pointer
  1010. * \param reason: The reason for the call
  1011. * \param user: Pointer to ptr to per-session user data allocated by library
  1012. * \param in: Pointer used for some callback reasons
  1013. * \param len: Length set for some callback reasons
  1014. *
  1015. * Each extension that is active on a particular connection receives
  1016. * callbacks during the connection lifetime to allow the extension to
  1017. * operate on websocket data and manage itself.
  1018. *
  1019. * Libwebsockets takes care of allocating and freeing "user" memory for
  1020. * each active extension on each connection. That is what is pointed to
  1021. * by the user parameter.
  1022. *
  1023. * LWS_EXT_CB_CONSTRUCT: called when the server has decided to
  1024. * select this extension from the list provided by the client,
  1025. * just before the server will send back the handshake accepting
  1026. * the connection with this extension active. This gives the
  1027. * extension a chance to initialize its connection context found
  1028. * in user.
  1029. *
  1030. * LWS_EXT_CB_CLIENT_CONSTRUCT: same as LWS_EXT_CB_CONSTRUCT
  1031. * but called when client is instantiating this extension. Some
  1032. * extensions will work the same on client and server side and then
  1033. * you can just merge handlers for both CONSTRUCTS.
  1034. *
  1035. * LWS_EXT_CB_DESTROY: called when the connection the extension was
  1036. * being used on is about to be closed and deallocated. It's the
  1037. * last chance for the extension to deallocate anything it has
  1038. * allocated in the user data (pointed to by user) before the
  1039. * user data is deleted. This same callback is used whether you
  1040. * are in client or server instantiation context.
  1041. *
  1042. * LWS_EXT_CB_PACKET_RX_PREPARSE: when this extension was active on
  1043. * a connection, and a packet of data arrived at the connection,
  1044. * it is passed to this callback to give the extension a chance to
  1045. * change the data, eg, decompress it. user is pointing to the
  1046. * extension's private connection context data, in is pointing
  1047. * to an lws_tokens struct, it consists of a char * pointer called
  1048. * token, and an int called token_len. At entry, these are
  1049. * set to point to the received buffer and set to the content
  1050. * length. If the extension will grow the content, it should use
  1051. * a new buffer allocated in its private user context data and
  1052. * set the pointed-to lws_tokens members to point to its buffer.
  1053. *
  1054. * LWS_EXT_CB_PACKET_TX_PRESEND: this works the same way as
  1055. * LWS_EXT_CB_PACKET_RX_PREPARSE above, except it gives the
  1056. * extension a chance to change websocket data just before it will
  1057. * be sent out. Using the same lws_token pointer scheme in in,
  1058. * the extension can change the buffer and the length to be
  1059. * transmitted how it likes. Again if it wants to grow the
  1060. * buffer safely, it should copy the data into its own buffer and
  1061. * set the lws_tokens token pointer to it.
  1062. *
  1063. * LWS_EXT_CB_ARGS_VALIDATE:
  1064. */
  1065. typedef int
  1066. lws_extension_callback_function(struct lws_context *context,
  1067. const struct lws_extension *ext, struct lws *wsi,
  1068. enum lws_extension_callback_reasons reason,
  1069. void *user, void *in, size_t len);
  1070. /** struct lws_extension - An extension we support */
  1071. struct lws_extension {
  1072. const char *name; /**< Formal extension name, eg, "permessage-deflate" */
  1073. lws_extension_callback_function *callback; /**< Service callback */
  1074. const char *client_offer; /**< String containing exts and options client offers */
  1075. /* Add new things just above here ---^
  1076. * This is part of the ABI, don't needlessly break compatibility */
  1077. };
  1078. /**
  1079. * lws_set_extension_option(): set extension option if possible
  1080. *
  1081. * \param wsi: websocket connection
  1082. * \param ext_name: name of ext, like "permessage-deflate"
  1083. * \param opt_name: name of option, like "rx_buf_size"
  1084. * \param opt_val: value to set option to
  1085. */
  1086. LWS_VISIBLE LWS_EXTERN int
  1087. lws_set_extension_option(struct lws *wsi, const char *ext_name,
  1088. const char *opt_name, const char *opt_val);
  1089. #ifndef LWS_NO_EXTENSIONS
  1090. /* lws_get_internal_extensions() - DEPRECATED
  1091. *
  1092. * \Deprecated There is no longer a set internal extensions table. The table is provided
  1093. * by user code along with application-specific settings. See the test
  1094. * client and server for how to do.
  1095. */
  1096. static LWS_INLINE LWS_WARN_DEPRECATED const struct lws_extension *
  1097. lws_get_internal_extensions() { return NULL; }
  1098. /**
  1099. * lws_ext_parse_options() - deal with parsing negotiated extension options
  1100. *
  1101. * \param ext: related extension struct
  1102. * \param wsi: websocket connection
  1103. * \param ext_user: per-connection extension private data
  1104. * \param opts: list of supported options
  1105. * \param o: option string to parse
  1106. * \param len: length
  1107. */
  1108. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1109. lws_ext_parse_options(const struct lws_extension *ext, struct lws *wsi,
  1110. void *ext_user, const struct lws_ext_options *opts,
  1111. const char *o, int len);
  1112. #endif
  1113. /** lws_extension_callback_pm_deflate() - extension for RFC7692
  1114. *
  1115. * \param context: lws context
  1116. * \param ext: related lws_extension struct
  1117. * \param wsi: websocket connection
  1118. * \param reason: incoming callback reason
  1119. * \param user: per-connection extension private data
  1120. * \param in: pointer parameter
  1121. * \param len: length parameter
  1122. *
  1123. * Built-in callback implementing RFC7692 permessage-deflate
  1124. */
  1125. LWS_EXTERN
  1126. int lws_extension_callback_pm_deflate(
  1127. struct lws_context *context, const struct lws_extension *ext,
  1128. struct lws *wsi, enum lws_extension_callback_reasons reason,
  1129. void *user, void *in, size_t len);
  1130. /*
  1131. * The internal exts are part of the public abi
  1132. * If we add more extensions, publish the callback here ------v
  1133. */
  1134. ///@}
  1135. /*! \defgroup Protocols-and-Plugins Protocols and Plugins
  1136. * \ingroup lwsapi
  1137. *
  1138. * ##Protocol and protocol plugin -related apis
  1139. *
  1140. * Protocols bind ws protocol names to a custom callback specific to that
  1141. * protocol implementaion.
  1142. *
  1143. * A list of protocols can be passed in at context creation time, but it is
  1144. * also legal to leave that NULL and add the protocols and their callback code
  1145. * using plugins.
  1146. *
  1147. * Plugins are much preferable compared to cut and pasting code into an
  1148. * application each time, since they can be used standalone.
  1149. */
  1150. ///@{
  1151. /** struct lws_protocols - List of protocols and handlers client or server
  1152. * supports. */
  1153. struct lws_protocols {
  1154. const char *name;
  1155. /**< Protocol name that must match the one given in the client
  1156. * Javascript new WebSocket(url, 'protocol') name. */
  1157. lws_callback_function *callback;
  1158. /**< The service callback used for this protocol. It allows the
  1159. * service action for an entire protocol to be encapsulated in
  1160. * the protocol-specific callback */
  1161. size_t per_session_data_size;
  1162. /**< Each new connection using this protocol gets
  1163. * this much memory allocated on connection establishment and
  1164. * freed on connection takedown. A pointer to this per-connection
  1165. * allocation is passed into the callback in the 'user' parameter */
  1166. size_t rx_buffer_size;
  1167. /**< lws allocates this much space for rx data and informs callback
  1168. * when something came. Due to rx flow control, the callback may not
  1169. * be able to consume it all without having to return to the event
  1170. * loop. That is supported in lws.
  1171. *
  1172. * This also controls how much may be sent at once at the moment,
  1173. * although this is likely to change.
  1174. */
  1175. unsigned int id;
  1176. /**< ignored by lws, but useful to contain user information bound
  1177. * to the selected protocol. For example if this protocol was
  1178. * called "myprotocol-v2", you might set id to 2, and the user
  1179. * code that acts differently according to the version can do so by
  1180. * switch (wsi->protocol->id), user code might use some bits as
  1181. * capability flags based on selected protocol version, etc. */
  1182. void *user; /**< ignored by lws, but user code can pass a pointer
  1183. here it can later access from the protocol callback */
  1184. /* Add new things just above here ---^
  1185. * This is part of the ABI, don't needlessly break compatibility */
  1186. };
  1187. struct lws_vhost;
  1188. /**
  1189. * lws_vhost_name_to_protocol() - get vhost's protocol object from its name
  1190. *
  1191. * \param vh: vhost to search
  1192. * \param name: protocol name
  1193. *
  1194. * Returns NULL or a pointer to the vhost's protocol of the requested name
  1195. */
  1196. LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
  1197. lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name);
  1198. /**
  1199. * lws_get_protocol() - Returns a protocol pointer from a websocket
  1200. * connection.
  1201. * \param wsi: pointer to struct websocket you want to know the protocol of
  1202. *
  1203. *
  1204. * Some apis can act on all live connections of a given protocol,
  1205. * this is how you can get a pointer to the active protocol if needed.
  1206. */
  1207. LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
  1208. lws_get_protocol(struct lws *wsi);
  1209. /** lws_protocol_get() - deprecated: use lws_get_protocol */
  1210. LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
  1211. lws_protocol_get(struct lws *wsi) LWS_WARN_DEPRECATED;
  1212. /**
  1213. * lws_protocol_vh_priv_zalloc() - Allocate and zero down a protocol's per-vhost
  1214. * storage
  1215. * \param vhost: vhost the instance is related to
  1216. * \param prot: protocol the instance is related to
  1217. * \param size: bytes to allocate
  1218. *
  1219. * Protocols often find it useful to allocate a per-vhost struct, this is a
  1220. * helper to be called in the per-vhost init LWS_CALLBACK_PROTOCOL_INIT
  1221. */
  1222. LWS_VISIBLE LWS_EXTERN void *
  1223. lws_protocol_vh_priv_zalloc(struct lws_vhost *vhost, const struct lws_protocols *prot,
  1224. int size);
  1225. /**
  1226. * lws_protocol_vh_priv_get() - retreive a protocol's per-vhost storage
  1227. *
  1228. * \param vhost: vhost the instance is related to
  1229. * \param prot: protocol the instance is related to
  1230. *
  1231. * Recover a pointer to the allocated per-vhost storage for the protocol created
  1232. * by lws_protocol_vh_priv_zalloc() earlier
  1233. */
  1234. LWS_VISIBLE LWS_EXTERN void *
  1235. lws_protocol_vh_priv_get(struct lws_vhost *vhost, const struct lws_protocols *prot);
  1236. /**
  1237. * lws_finalize_startup() - drop initial process privileges
  1238. *
  1239. * \param context: lws context
  1240. *
  1241. * This is called after the end of the vhost protocol initializations, but
  1242. * you may choose to call it earlier
  1243. */
  1244. LWS_VISIBLE LWS_EXTERN int
  1245. lws_finalize_startup(struct lws_context *context);
  1246. #ifdef LWS_WITH_PLUGINS
  1247. /* PLUGINS implies LIBUV */
  1248. #define LWS_PLUGIN_API_MAGIC 180
  1249. /** struct lws_plugin_capability - how a plugin introduces itself to lws */
  1250. struct lws_plugin_capability {
  1251. unsigned int api_magic; /**< caller fills this in, plugin fills rest */
  1252. const struct lws_protocols *protocols; /**< array of supported protocols provided by plugin */
  1253. int count_protocols; /**< how many protocols */
  1254. const struct lws_extension *extensions; /**< array of extensions provided by plugin */
  1255. int count_extensions; /**< how many extensions */
  1256. };
  1257. typedef int (*lws_plugin_init_func)(struct lws_context *,
  1258. struct lws_plugin_capability *);
  1259. typedef int (*lws_plugin_destroy_func)(struct lws_context *);
  1260. /** struct lws_plugin */
  1261. struct lws_plugin {
  1262. struct lws_plugin *list; /**< linked list */
  1263. #if (UV_VERSION_MAJOR > 0)
  1264. uv_lib_t lib; /**< shared library pointer */
  1265. #else
  1266. void *l; /**< so we can compile on ancient libuv */
  1267. #endif
  1268. char name[64]; /**< name of the plugin */
  1269. struct lws_plugin_capability caps; /**< plugin capabilities */
  1270. };
  1271. #endif
  1272. ///@}
  1273. /*! \defgroup generic-sessions plugin: generic-sessions
  1274. * \ingroup Protocols-and-Plugins
  1275. *
  1276. * ##Plugin Generic-sessions related
  1277. *
  1278. * generic-sessions plugin provides a reusable, generic session and login /
  1279. * register / forgot password framework including email verification.
  1280. */
  1281. ///@{
  1282. #define LWSGS_EMAIL_CONTENT_SIZE 16384
  1283. /**< Maximum size of email we might send */
  1284. /* SHA-1 binary and hexified versions */
  1285. /** typedef struct lwsgw_hash_bin */
  1286. typedef struct { unsigned char bin[20]; /**< binary representation of hash */} lwsgw_hash_bin;
  1287. /** typedef struct lwsgw_hash */
  1288. typedef struct { char id[41]; /**< ascii hex representation of hash */ } lwsgw_hash;
  1289. /** enum lwsgs_auth_bits */
  1290. enum lwsgs_auth_bits {
  1291. LWSGS_AUTH_LOGGED_IN = 1, /**< user is logged in as somebody */
  1292. LWSGS_AUTH_ADMIN = 2, /**< logged in as the admin user */
  1293. LWSGS_AUTH_VERIFIED = 4, /**< user has verified his email */
  1294. LWSGS_AUTH_FORGOT_FLOW = 8, /**< he just completed "forgot password" flow */
  1295. };
  1296. /** struct lws_session_info - information about user session status */
  1297. struct lws_session_info {
  1298. char username[32]; /**< username logged in as, or empty string */
  1299. char email[100]; /**< email address associated with login, or empty string */
  1300. char ip[72]; /**< ip address session was started from */
  1301. unsigned int mask; /**< access rights mask associated with session
  1302. * see enum lwsgs_auth_bits */
  1303. char session[42]; /**< session id string, usable as opaque uid when not logged in */
  1304. };
  1305. /** enum lws_gs_event */
  1306. enum lws_gs_event {
  1307. LWSGSE_CREATED, /**< a new user was created */
  1308. LWSGSE_DELETED /**< an existing user was deleted */
  1309. };
  1310. /** struct lws_gs_event_args */
  1311. struct lws_gs_event_args {
  1312. enum lws_gs_event event; /**< which event happened */
  1313. const char *username; /**< which username the event happened to */
  1314. const char *email; /**< the email address of that user */
  1315. };
  1316. ///@}
  1317. /*! \defgroup context-and-vhost
  1318. * \ingroup lwsapi
  1319. *
  1320. * ##Context and Vhost releated functions
  1321. *
  1322. * LWS requires that there is one context, in which you may define multiple
  1323. * vhosts. Each vhost is a virtual host, with either its own listen port
  1324. * or sharing an existing one. Each vhost has its own SSL context that can
  1325. * be set up individually or left disabled.
  1326. *
  1327. * If you don't care about multiple "site" support, you can ignore it and
  1328. * lws will create a single default vhost at context creation time.
  1329. */
  1330. ///@{
  1331. /*
  1332. * NOTE: These public enums are part of the abi. If you want to add one,
  1333. * add it at where specified so existing users are unaffected.
  1334. */
  1335. /** enum lws_context_options - context and vhost options */
  1336. enum lws_context_options {
  1337. LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = (1 << 1) |
  1338. (1 << 12),
  1339. /**< (VH) Don't allow the connection unless the client has a
  1340. * client cert that we recognize; provides
  1341. * LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT */
  1342. LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = (1 << 2),
  1343. /**< (CTX) Don't try to get the server's hostname */
  1344. LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT = (1 << 3) |
  1345. (1 << 12),
  1346. /**< (VH) Allow non-SSL (plaintext) connections on the same
  1347. * port as SSL is listening... undermines the security of SSL;
  1348. * provides LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT */
  1349. LWS_SERVER_OPTION_LIBEV = (1 << 4),
  1350. /**< (CTX) Use libev event loop */
  1351. LWS_SERVER_OPTION_DISABLE_IPV6 = (1 << 5),
  1352. /**< (VH) Disable IPV6 support */
  1353. LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS = (1 << 6),
  1354. /**< (VH) Don't load OS CA certs, you will need to load your
  1355. * own CA cert(s) */
  1356. LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED = (1 << 7),
  1357. /**< (VH) Accept connections with no valid Cert (eg, selfsigned) */
  1358. LWS_SERVER_OPTION_VALIDATE_UTF8 = (1 << 8),
  1359. /**< (VH) Check UT-8 correctness */
  1360. LWS_SERVER_OPTION_SSL_ECDH = (1 << 9) |
  1361. (1 << 12),
  1362. /**< (VH) initialize ECDH ciphers */
  1363. LWS_SERVER_OPTION_LIBUV = (1 << 10),
  1364. /**< (CTX) Use libuv event loop */
  1365. LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS = (1 << 11) |
  1366. (1 << 12),
  1367. /**< (VH) Use http redirect to force http to https
  1368. * (deprecated: use mount redirection) */
  1369. LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT = (1 << 12),
  1370. /**< (CTX) Initialize the SSL library at all */
  1371. LWS_SERVER_OPTION_EXPLICIT_VHOSTS = (1 << 13),
  1372. /**< (CTX) Only create the context when calling context
  1373. * create api, implies user code will create its own vhosts */
  1374. LWS_SERVER_OPTION_UNIX_SOCK = (1 << 14),
  1375. /**< (VH) Use Unix socket */
  1376. LWS_SERVER_OPTION_STS = (1 << 15),
  1377. /**< (VH) Send Strict Transport Security header, making
  1378. * clients subsequently go to https even if user asked for http */
  1379. LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY = (1 << 16),
  1380. /**< (VH) Enable LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE to take effect */
  1381. LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE = (1 << 17),
  1382. /**< (VH) if set, only ipv6 allowed on the vhost */
  1383. LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN = (1 << 18),
  1384. /**< (CTX) Libuv only: Do not spin on SIGSEGV / SIGFPE. A segfault
  1385. * normally makes the lib spin so you can attach a debugger to it
  1386. * even if it happened without a debugger in place. You can disable
  1387. * that by giving this option.
  1388. */
  1389. /****** add new things just above ---^ ******/
  1390. };
  1391. #define lws_check_opt(c, f) (((c) & (f)) == (f))
  1392. /** struct lws_context_creation_info - parameters to create context and /or vhost with
  1393. *
  1394. * This is also used to create vhosts.... if LWS_SERVER_OPTION_EXPLICIT_VHOSTS
  1395. * is not given, then for backwards compatibility one vhost is created at
  1396. * context-creation time using the info from this struct.
  1397. *
  1398. * If LWS_SERVER_OPTION_EXPLICIT_VHOSTS is given, then no vhosts are created
  1399. * at the same time as the context, they are expected to be created afterwards.
  1400. */
  1401. struct lws_context_creation_info {
  1402. int port;
  1403. /**< VHOST: Port to listen on... you can use CONTEXT_PORT_NO_LISTEN to
  1404. * suppress listening on any port, that's what you want if you are
  1405. * not running a websocket server at all but just using it as a
  1406. * client */
  1407. const char *iface;
  1408. /**< VHOST: NULL to bind the listen socket to all interfaces, or the
  1409. * interface name, eg, "eth2"
  1410. * If options specifies LWS_SERVER_OPTION_UNIX_SOCK, this member is
  1411. * the pathname of a UNIX domain socket. you can use the UNIX domain
  1412. * sockets in abstract namespace, by prepending an at symbol to the
  1413. * socket name. */
  1414. const struct lws_protocols *protocols;
  1415. /**< VHOST: Array of structures listing supported protocols and a protocol-
  1416. * specific callback for each one. The list is ended with an
  1417. * entry that has a NULL callback pointer. */
  1418. const struct lws_extension *extensions;
  1419. /**< VHOST: NULL or array of lws_extension structs listing the
  1420. * extensions this context supports. */
  1421. const struct lws_token_limits *token_limits;
  1422. /**< CONTEXT: NULL or struct lws_token_limits pointer which is initialized
  1423. * with a token length limit for each possible WSI_TOKEN_ */
  1424. const char *ssl_private_key_password;
  1425. /**< VHOST: NULL or the passphrase needed for the private key */
  1426. const char *ssl_cert_filepath;
  1427. /**< VHOST: If libwebsockets was compiled to use ssl, and you want
  1428. * to listen using SSL, set to the filepath to fetch the
  1429. * server cert from, otherwise NULL for unencrypted */
  1430. const char *ssl_private_key_filepath;
  1431. /**< VHOST: filepath to private key if wanting SSL mode;
  1432. * if this is set to NULL but sll_cert_filepath is set, the
  1433. * OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY callback is called
  1434. * to allow setting of the private key directly via openSSL
  1435. * library calls */
  1436. const char *ssl_ca_filepath;
  1437. /**< VHOST: CA certificate filepath or NULL */
  1438. const char *ssl_cipher_list;
  1439. /**< VHOST: List of valid ciphers to use (eg,
  1440. * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
  1441. * or you can leave it as NULL to get "DEFAULT" */
  1442. const char *http_proxy_address;
  1443. /**< VHOST: If non-NULL, attempts to proxy via the given address.
  1444. * If proxy auth is required, use format "username:password\@server:port" */
  1445. unsigned int http_proxy_port;
  1446. /**< VHOST: If http_proxy_address was non-NULL, uses this port */
  1447. int gid;
  1448. /**< CONTEXT: group id to change to after setting listen socket, or -1. */
  1449. int uid;
  1450. /**< CONTEXT: user id to change to after setting listen socket, or -1. */
  1451. unsigned int options;
  1452. /**< VHOST + CONTEXT: 0, or LWS_SERVER_OPTION_... bitfields */
  1453. void *user;
  1454. /**< CONTEXT: optional user pointer that can be recovered via the context
  1455. * pointer using lws_context_user */
  1456. int ka_time;
  1457. /**< CONTEXT: 0 for no TCP keepalive, otherwise apply this keepalive
  1458. * timeout to all libwebsocket sockets, client or server */
  1459. int ka_probes;
  1460. /**< CONTEXT: if ka_time was nonzero, after the timeout expires how many
  1461. * times to try to get a response from the peer before giving up
  1462. * and killing the connection */
  1463. int ka_interval;
  1464. /**< CONTEXT: if ka_time was nonzero, how long to wait before each ka_probes
  1465. * attempt */
  1466. #ifdef LWS_OPENSSL_SUPPORT
  1467. SSL_CTX *provided_client_ssl_ctx;
  1468. /**< CONTEXT: If non-null, swap out libwebsockets ssl
  1469. * implementation for the one provided by provided_ssl_ctx.
  1470. * Libwebsockets no longer is responsible for freeing the context
  1471. * if this option is selected. */
  1472. #else /* maintain structure layout either way */
  1473. void *provided_client_ssl_ctx; /**< dummy if ssl disabled */
  1474. #endif
  1475. short max_http_header_data;
  1476. /**< CONTEXT: The max amount of header payload that can be handled
  1477. * in an http request (unrecognized header payload is dropped) */
  1478. short max_http_header_pool;
  1479. /**< CONTEXT: The max number of connections with http headers that
  1480. * can be processed simultaneously (the corresponding memory is
  1481. * allocated for the lifetime of the context). If the pool is
  1482. * busy new incoming connections must wait for accept until one
  1483. * becomes free. */
  1484. unsigned int count_threads;
  1485. /**< CONTEXT: how many contexts to create in an array, 0 = 1 */
  1486. unsigned int fd_limit_per_thread;
  1487. /**< CONTEXT: nonzero means restrict each service thread to this
  1488. * many fds, 0 means the default which is divide the process fd
  1489. * limit by the number of threads. */
  1490. unsigned int timeout_secs;
  1491. /**< VHOST: various processes involving network roundtrips in the
  1492. * library are protected from hanging forever by timeouts. If
  1493. * nonzero, this member lets you set the timeout used in seconds.
  1494. * Otherwise a default timeout is used. */
  1495. const char *ecdh_curve;
  1496. /**< VHOST: if NULL, defaults to initializing server with "prime256v1" */
  1497. const char *vhost_name;
  1498. /**< VHOST: name of vhost, must match external DNS name used to
  1499. * access the site, like "warmcat.com" as it's used to match
  1500. * Host: header and / or SNI name for SSL. */
  1501. const char * const *plugin_dirs;
  1502. /**< CONTEXT: NULL, or NULL-terminated array of directories to
  1503. * scan for lws protocol plugins at context creation time */
  1504. const struct lws_protocol_vhost_options *pvo;
  1505. /**< VHOST: pointer to optional linked list of per-vhost
  1506. * options made accessible to protocols */
  1507. int keepalive_timeout;
  1508. /**< VHOST: (default = 0 = 60s) seconds to allow remote
  1509. * client to hold on to an idle HTTP/1.1 connection */
  1510. const char *log_filepath;
  1511. /**< VHOST: filepath to append logs to... this is opened before
  1512. * any dropping of initial privileges */
  1513. const struct lws_http_mount *mounts;
  1514. /**< VHOST: optional linked list of mounts for this vhost */
  1515. const char *server_string;
  1516. /**< CONTEXT: string used in HTTP headers to identify server
  1517. * software, if NULL, "libwebsockets". */
  1518. unsigned int pt_serv_buf_size;
  1519. /**< CONTEXT: 0 = default of 4096. This buffer is used by
  1520. * various service related features including file serving, it
  1521. * defines the max chunk of file that can be sent at once.
  1522. * At the risk of lws having to buffer failed large sends, it
  1523. * can be increased to, eg, 128KiB to improve throughput. */
  1524. unsigned int max_http_header_data2;
  1525. /**< CONTEXT: if max_http_header_data is 0 and this
  1526. * is nonzero, this will be used in place of the default. It's
  1527. * like this for compatibility with the original short version,
  1528. * this is unsigned int length. */
  1529. long ssl_options_set;
  1530. /**< VHOST: Any bits set here will be set as SSL options */
  1531. long ssl_options_clear;
  1532. /**< VHOST: Any bits set here will be cleared as SSL options */
  1533. unsigned short ws_ping_pong_interval;
  1534. /**< CONTEXT: 0 for none, else interval in seconds between sending
  1535. * PINGs on idle websocket connections. When the PING is sent,
  1536. * the PONG must come within the normal timeout_secs timeout period
  1537. * or the connection will be dropped.
  1538. * Any RX or TX traffic on the connection restarts the interval timer,
  1539. * so a connection which always sends or receives something at intervals
  1540. * less than the interval given here will never send PINGs / expect
  1541. * PONGs. Conversely as soon as the ws connection is established, an
  1542. * idle connection will do the PING / PONG roundtrip as soon as
  1543. * ws_ping_pong_interval seconds has passed without traffic
  1544. */
  1545. const struct lws_protocol_vhost_options *headers;
  1546. /**< VHOST: pointer to optional linked list of per-vhost
  1547. * canned headers that are added to server responses */
  1548. /* Add new things just above here ---^
  1549. * This is part of the ABI, don't needlessly break compatibility
  1550. *
  1551. * The below is to ensure later library versions with new
  1552. * members added above will see 0 (default) even if the app
  1553. * was not built against the newer headers.
  1554. */
  1555. void *_unused[8]; /**< dummy */
  1556. };
  1557. /**
  1558. * lws_create_context() - Create the websocket handler
  1559. * \param info: pointer to struct with parameters
  1560. *
  1561. * This function creates the listening socket (if serving) and takes care
  1562. * of all initialization in one step.
  1563. *
  1564. * If option LWS_SERVER_OPTION_EXPLICIT_VHOSTS is given, no vhost is
  1565. * created; you're expected to create your own vhosts afterwards using
  1566. * lws_create_vhost(). Otherwise a vhost named "default" is also created
  1567. * using the information in the vhost-related members, for compatibility.
  1568. *
  1569. * After initialization, it returns a struct lws_context * that
  1570. * represents this server. After calling, user code needs to take care
  1571. * of calling lws_service() with the context pointer to get the
  1572. * server's sockets serviced. This must be done in the same process
  1573. * context as the initialization call.
  1574. *
  1575. * The protocol callback functions are called for a handful of events
  1576. * including http requests coming in, websocket connections becoming
  1577. * established, and data arriving; it's also called periodically to allow
  1578. * async transmission.
  1579. *
  1580. * HTTP requests are sent always to the FIRST protocol in protocol, since
  1581. * at that time websocket protocol has not been negotiated. Other
  1582. * protocols after the first one never see any HTTP callack activity.
  1583. *
  1584. * The server created is a simple http server by default; part of the
  1585. * websocket standard is upgrading this http connection to a websocket one.
  1586. *
  1587. * This allows the same server to provide files like scripts and favicon /
  1588. * images or whatever over http and dynamic data over websockets all in
  1589. * one place; they're all handled in the user callback.
  1590. */
  1591. LWS_VISIBLE LWS_EXTERN struct lws_context *
  1592. lws_create_context(struct lws_context_creation_info *info);
  1593. /**
  1594. * lws_context_destroy() - Destroy the websocket context
  1595. * \param context: Websocket context
  1596. *
  1597. * This function closes any active connections and then frees the
  1598. * context. After calling this, any further use of the context is
  1599. * undefined.
  1600. */
  1601. LWS_VISIBLE LWS_EXTERN void
  1602. lws_context_destroy(struct lws_context *context);
  1603. /**
  1604. * lws_set_proxy() - Setups proxy to lws_context.
  1605. * \param vhost: pointer to struct lws_vhost you want set proxy for
  1606. * \param proxy: pointer to c string containing proxy in format address:port
  1607. *
  1608. * Returns 0 if proxy string was parsed and proxy was setup.
  1609. * Returns -1 if proxy is NULL or has incorrect format.
  1610. *
  1611. * This is only required if your OS does not provide the http_proxy
  1612. * environment variable (eg, OSX)
  1613. *
  1614. * IMPORTANT! You should call this function right after creation of the
  1615. * lws_context and before call to connect. If you call this
  1616. * function after connect behavior is undefined.
  1617. * This function will override proxy settings made on lws_context
  1618. * creation with genenv() call.
  1619. */
  1620. LWS_VISIBLE LWS_EXTERN int
  1621. lws_set_proxy(struct lws_vhost *vhost, const char *proxy);
  1622. struct lws_vhost;
  1623. /**
  1624. * lws_create_vhost() - Create a vhost (virtual server context)
  1625. * \param context: pointer to result of lws_create_context()
  1626. * \param info: pointer to struct with parameters
  1627. *
  1628. * This function creates a virtual server (vhost) using the vhost-related
  1629. * members of the info struct. You can create many vhosts inside one context
  1630. * if you created the context with the option LWS_SERVER_OPTION_EXPLICIT_VHOSTS
  1631. */
  1632. LWS_EXTERN LWS_VISIBLE struct lws_vhost *
  1633. lws_create_vhost(struct lws_context *context,
  1634. struct lws_context_creation_info *info);
  1635. /**
  1636. * lwsws_get_config_globals() - Parse a JSON server config file
  1637. * \param info: pointer to struct with parameters
  1638. * \param d: filepath of the config file
  1639. * \param config_strings: storage for the config strings extracted from JSON,
  1640. * the pointer is incremented as strings are stored
  1641. * \param len: pointer to the remaining length left in config_strings
  1642. * the value is decremented as strings are stored
  1643. *
  1644. * This function prepares a n lws_context_creation_info struct with global
  1645. * settings from a file d.
  1646. *
  1647. * Requires CMake option LWS_WITH_LEJP_CONF to have been enabled
  1648. */
  1649. LWS_VISIBLE LWS_EXTERN int
  1650. lwsws_get_config_globals(struct lws_context_creation_info *info, const char *d,
  1651. char **config_strings, int *len);
  1652. /**
  1653. * lwsws_get_config_vhosts() - Create vhosts from a JSON server config file
  1654. * \param context: pointer to result of lws_create_context()
  1655. * \param info: pointer to struct with parameters
  1656. * \param d: filepath of the config file
  1657. * \param config_strings: storage for the config strings extracted from JSON,
  1658. * the pointer is incremented as strings are stored
  1659. * \param len: pointer to the remaining length left in config_strings
  1660. * the value is decremented as strings are stored
  1661. *
  1662. * This function creates vhosts into a context according to the settings in
  1663. *JSON files found in directory d.
  1664. *
  1665. * Requires CMake option LWS_WITH_LEJP_CONF to have been enabled
  1666. */
  1667. LWS_VISIBLE LWS_EXTERN int
  1668. lwsws_get_config_vhosts(struct lws_context *context,
  1669. struct lws_context_creation_info *info, const char *d,
  1670. char **config_strings, int *len);
  1671. /** lws_vhost_get() - \deprecated deprecated: use lws_get_vhost() */
  1672. LWS_VISIBLE LWS_EXTERN struct lws_vhost *
  1673. lws_vhost_get(struct lws *wsi) LWS_WARN_DEPRECATED;
  1674. /**
  1675. * lws_get_vhost() - return the vhost a wsi belongs to
  1676. *
  1677. * \param wsi: which connection
  1678. */
  1679. LWS_VISIBLE LWS_EXTERN struct lws_vhost *
  1680. lws_get_vhost(struct lws *wsi);
  1681. /**
  1682. * lws_json_dump_vhost() - describe vhost state and stats in JSON
  1683. *
  1684. * \param vh: the vhost
  1685. * \param buf: buffer to fill with JSON
  1686. * \param len: max length of buf
  1687. */
  1688. LWS_VISIBLE LWS_EXTERN int
  1689. lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len);
  1690. /**
  1691. * lws_json_dump_context() - describe context state and stats in JSON
  1692. *
  1693. * \param context: the context
  1694. * \param buf: buffer to fill with JSON
  1695. * \param len: max length of buf
  1696. */
  1697. LWS_VISIBLE LWS_EXTERN int
  1698. lws_json_dump_context(const struct lws_context *context, char *buf, int len);
  1699. /**
  1700. * lws_context_user() - get the user data associated with the context
  1701. * \param context: Websocket context
  1702. *
  1703. * This returns the optional user allocation that can be attached to
  1704. * the context the sockets live in at context_create time. It's a way
  1705. * to let all sockets serviced in the same context share data without
  1706. * using globals statics in the user code.
  1707. */
  1708. LWS_VISIBLE LWS_EXTERN void *
  1709. lws_context_user(struct lws_context *context);
  1710. /*! \defgroup vhost-mounts Vhost mounts and options
  1711. * \ingroup context-and-vhost-creation
  1712. *
  1713. * ##Vhost mounts and options
  1714. */
  1715. ///@{
  1716. /** struct lws_protocol_vhost_options - linked list of per-vhost protocol
  1717. * name=value options
  1718. *
  1719. * This provides a general way to attach a linked-list of name=value pairs,
  1720. * which can also have an optional child link-list using the options member.
  1721. */
  1722. struct lws_protocol_vhost_options {
  1723. const struct lws_protocol_vhost_options *next; /**< linked list */
  1724. const struct lws_protocol_vhost_options *options; /**< child linked-list of more options for this node */
  1725. const char *name; /**< name of name=value pair */
  1726. const char *value; /**< value of name=value pair */
  1727. };
  1728. /** enum lws_mount_protocols
  1729. * This specifies the mount protocol for a mountpoint, whether it is to be
  1730. * served from a filesystem, or it is a cgi etc.
  1731. */
  1732. enum lws_mount_protocols {
  1733. LWSMPRO_HTTP = 0, /**< not supported yet */
  1734. LWSMPRO_HTTPS = 1, /**< not supported yet */
  1735. LWSMPRO_FILE = 2, /**< serve from filesystem directory */
  1736. LWSMPRO_CGI = 3, /**< pass to CGI to handle */
  1737. LWSMPRO_REDIR_HTTP = 4, /**< redirect to http:// url */
  1738. LWSMPRO_REDIR_HTTPS = 5, /**< redirect to https:// url */
  1739. LWSMPRO_CALLBACK = 6, /**< hand by named protocol's callback */
  1740. };
  1741. /** struct lws_http_mount
  1742. *
  1743. * arguments for mounting something in a vhost's url namespace
  1744. */
  1745. struct lws_http_mount {
  1746. const struct lws_http_mount *mount_next;
  1747. /**< pointer to next struct lws_http_mount */
  1748. const char *mountpoint;
  1749. /**< mountpoint in http pathspace, eg, "/" */
  1750. const char *origin;
  1751. /**< path to be mounted, eg, "/var/www/warmcat.com" */
  1752. const char *def;
  1753. /**< default target, eg, "index.html" */
  1754. const char *protocol;
  1755. /**<"protocol-name" to handle mount */
  1756. const struct lws_protocol_vhost_options *cgienv;
  1757. /**< optional linked-list of cgi options. These are created
  1758. * as environment variables for the cgi process
  1759. */
  1760. const struct lws_protocol_vhost_options *extra_mimetypes;
  1761. /**< optional linked-list of mimetype mappings */
  1762. const struct lws_protocol_vhost_options *interpret;
  1763. /**< optional linked-list of files to be interpreted */
  1764. int cgi_timeout;
  1765. /**< seconds cgi is allowed to live, if cgi://mount type */
  1766. int cache_max_age;
  1767. /**< max-age for reuse of client cache of files, seconds */
  1768. unsigned int auth_mask;
  1769. /**< bits set here must be set for authorized client session */
  1770. unsigned int cache_reusable:1; /**< set if client cache may reuse this */
  1771. unsigned int cache_revalidate:1; /**< set if client cache should revalidate on use */
  1772. unsigned int cache_intermediaries:1; /**< set if intermediaries are allowed to cache */
  1773. unsigned char origin_protocol; /**< one of enum lws_mount_protocols */
  1774. unsigned char mountpoint_len; /**< length of mountpoint string */
  1775. };
  1776. ///@}
  1777. ///@}
  1778. /*! \defgroup client
  1779. * \ingroup lwsapi
  1780. *
  1781. * ##Client releated functions
  1782. * */
  1783. ///@{
  1784. /** enum lws_client_connect_ssl_connection_flags - flags that may be used
  1785. * with struct lws_client_connect_info ssl_connection member to control if
  1786. * and how SSL checks apply to the client connection being created
  1787. */
  1788. enum lws_client_connect_ssl_connection_flags {
  1789. LCCSCF_USE_SSL = (1 << 0),
  1790. LCCSCF_ALLOW_SELFSIGNED = (1 << 1),
  1791. LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK = (1 << 2)
  1792. };
  1793. /** struct lws_client_connect_info - parameters to connect with when using
  1794. * lws_client_connect_via_info() */
  1795. struct lws_client_connect_info {
  1796. struct lws_context *context;
  1797. /**< lws context to create connection in */
  1798. const char *address;
  1799. /**< remote address to connect to */
  1800. int port;
  1801. /**< remote port to connect to */
  1802. int ssl_connection;
  1803. /**< nonzero for ssl */
  1804. const char *path;
  1805. /**< uri path */
  1806. const char *host;
  1807. /**< content of host header */
  1808. const char *origin;
  1809. /**< content of origin header */
  1810. const char *protocol;
  1811. /**< list of ws protocols we could accept */
  1812. int ietf_version_or_minus_one;
  1813. /**< deprecated: currently leave at 0 or -1 */
  1814. void *userdata;
  1815. /**< if non-NULL, use this as wsi user_data instead of malloc it */
  1816. const struct lws_extension *client_exts;
  1817. /**< array of extensions that may be used on connection */
  1818. const char *method;
  1819. /**< if non-NULL, do this http method instead of ws[s] upgrade.
  1820. * use "GET" to be a simple http client connection */
  1821. struct lws *parent_wsi;
  1822. /**< if another wsi is responsible for this connection, give it here.
  1823. * this is used to make sure if the parent closes so do any
  1824. * child connections first. */
  1825. const char *uri_replace_from;
  1826. /**< if non-NULL, when this string is found in URIs in
  1827. * text/html content-encoding, it's replaced with uri_replace_to */
  1828. const char *uri_replace_to;
  1829. /**< see uri_replace_from */
  1830. struct lws_vhost *vhost;
  1831. /**< vhost to bind to (used to determine related SSL_CTX) */
  1832. struct lws **pwsi;
  1833. /**< if not NULL, store the new wsi here early in the connection
  1834. * process. Although we return the new wsi, the call to create the
  1835. * client connection does progress the connection somewhat and may
  1836. * meet an error that will result in the connection being scrubbed and
  1837. * NULL returned. While the wsi exists though, he may process a
  1838. * callback like CLIENT_CONNECTION_ERROR with his wsi: this gives the
  1839. * user callback a way to identify which wsi it is that faced the error
  1840. * even before the new wsi is returned and even if ultimately no wsi
  1841. * is returned.
  1842. */
  1843. /* Add new things just above here ---^
  1844. * This is part of the ABI, don't needlessly break compatibility
  1845. *
  1846. * The below is to ensure later library versions with new
  1847. * members added above will see 0 (default) even if the app
  1848. * was not built against the newer headers.
  1849. */
  1850. void *_unused[4]; /**< dummy */
  1851. };
  1852. /**
  1853. * lws_client_connect_via_info() - Connect to another websocket server
  1854. * \param ccinfo: pointer to lws_client_connect_info struct
  1855. *
  1856. * This function creates a connection to a remote server using the
  1857. * information provided in ccinfo.
  1858. */
  1859. LWS_VISIBLE LWS_EXTERN struct lws *
  1860. lws_client_connect_via_info(struct lws_client_connect_info * ccinfo);
  1861. /**
  1862. * lws_client_connect() - Connect to another websocket server
  1863. * \deprecated DEPRECATED use lws_client_connect_via_info
  1864. * \param clients: Websocket context
  1865. * \param address: Remote server address, eg, "myserver.com"
  1866. * \param port: Port to connect to on the remote server, eg, 80
  1867. * \param ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
  1868. * signed certs
  1869. * \param path: Websocket path on server
  1870. * \param host: Hostname on server
  1871. * \param origin: Socket origin name
  1872. * \param protocol: Comma-separated list of protocols being asked for from
  1873. * the server, or just one. The server will pick the one it
  1874. * likes best. If you don't want to specify a protocol, which is
  1875. * legal, use NULL here.
  1876. * \param ietf_version_or_minus_one: -1 to ask to connect using the default, latest
  1877. * protocol supported, or the specific protocol ordinal
  1878. *
  1879. * This function creates a connection to a remote server
  1880. */
  1881. /* deprecated, use lws_client_connect_via_info() */
  1882. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  1883. lws_client_connect(struct lws_context *clients, const char *address,
  1884. int port, int ssl_connection, const char *path,
  1885. const char *host, const char *origin, const char *protocol,
  1886. int ietf_version_or_minus_one) LWS_WARN_DEPRECATED;
  1887. /* deprecated, use lws_client_connect_via_info() */
  1888. /**
  1889. * lws_client_connect_extended() - Connect to another websocket server
  1890. * \deprecated DEPRECATED use lws_client_connect_via_info
  1891. * \param clients: Websocket context
  1892. * \param address: Remote server address, eg, "myserver.com"
  1893. * \param port: Port to connect to on the remote server, eg, 80
  1894. * \param ssl_connection: 0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
  1895. * signed certs
  1896. * \param path: Websocket path on server
  1897. * \param host: Hostname on server
  1898. * \param origin: Socket origin name
  1899. * \param protocol: Comma-separated list of protocols being asked for from
  1900. * the server, or just one. The server will pick the one it
  1901. * likes best.
  1902. * \param ietf_version_or_minus_one: -1 to ask to connect using the default, latest
  1903. * protocol supported, or the specific protocol ordinal
  1904. * \param userdata: Pre-allocated user data
  1905. *
  1906. * This function creates a connection to a remote server
  1907. */
  1908. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  1909. lws_client_connect_extended(struct lws_context *clients, const char *address,
  1910. int port, int ssl_connection, const char *path,
  1911. const char *host, const char *origin,
  1912. const char *protocol, int ietf_version_or_minus_one,
  1913. void *userdata) LWS_WARN_DEPRECATED;
  1914. /**
  1915. * lws_init_vhost_client_ssl() - also enable client SSL on an existing vhost
  1916. *
  1917. * \param info: client ssl related info
  1918. * \param vhost: which vhost to initialize client ssl operations on
  1919. *
  1920. * You only need to call this if you plan on using SSL client connections on
  1921. * the vhost. For non-SSL client connections, it's not necessary to call this.
  1922. *
  1923. * The following members of info are used during the call
  1924. *
  1925. * - options must have LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT set,
  1926. * otherwise the call does nothing
  1927. * - provided_client_ssl_ctx must be NULL to get a generated client
  1928. * ssl context, otherwise you can pass a prepared one in by setting it
  1929. * - ssl_cipher_list may be NULL or set to the client valid cipher list
  1930. * - ssl_ca_filepath may be NULL or client cert filepath
  1931. * - ssl_cert_filepath may be NULL or client cert filepath
  1932. * - ssl_private_key_filepath may be NULL or client cert private key
  1933. *
  1934. * You must create your vhost explicitly if you want to use this, so you have
  1935. * a pointer to the vhost. Create the context first with the option flag
  1936. * LWS_SERVER_OPTION_EXPLICIT_VHOSTS and then call lws_create_vhost() with
  1937. * the same info struct.
  1938. */
  1939. LWS_VISIBLE LWS_EXTERN int
  1940. lws_init_vhost_client_ssl(const struct lws_context_creation_info *info,
  1941. struct lws_vhost *vhost);
  1942. LWS_VISIBLE LWS_EXTERN int
  1943. lws_http_client_read(struct lws *wsi, char **buf, int *len);
  1944. LWS_VISIBLE LWS_EXTERN void
  1945. lws_client_http_body_pending(struct lws *wsi, int something_left_to_send);
  1946. /**
  1947. * lws_client_http_body_pending() - control if client connection neeeds to send body
  1948. *
  1949. * \param wsi: client connection
  1950. * \param something_left_to_send: nonzero if need to send more body, 0 (default)
  1951. * if nothing more to send
  1952. *
  1953. * If you will send payload data with your HTTP client connection, eg, for POST,
  1954. * when you set the related http headers in
  1955. * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER callback you should also call
  1956. * this API with something_left_to_send nonzero, and call
  1957. * lws_callback_on_writable(wsi);
  1958. *
  1959. * After sending the headers, lws will call your callback with
  1960. * LWS_CALLBACK_CLIENT_HTTP_WRITEABLE reason when writable. You can send the
  1961. * next part of the http body payload, calling lws_callback_on_writable(wsi);
  1962. * if there is more to come, or lws_client_http_body_pending(wsi, 0); to
  1963. * let lws know the last part is sent and the connection can move on.
  1964. */
  1965. ///@}
  1966. /** \defgroup service Built-in service loop entry
  1967. *
  1968. * ##Built-in service loop entry
  1969. *
  1970. * If you're not using libev / libuv, these apis are needed to enter the poll()
  1971. * wait in lws and service any connections with pending events.
  1972. */
  1973. ///@{
  1974. /**
  1975. * lws_service() - Service any pending websocket activity
  1976. * \param context: Websocket context
  1977. * \param timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
  1978. * service otherwise block and service immediately, returning
  1979. * after the timeout if nothing needed service.
  1980. *
  1981. * This function deals with any pending websocket traffic, for three
  1982. * kinds of event. It handles these events on both server and client
  1983. * types of connection the same.
  1984. *
  1985. * 1) Accept new connections to our context's server
  1986. *
  1987. * 2) Call the receive callback for incoming frame data received by
  1988. * server or client connections.
  1989. *
  1990. * You need to call this service function periodically to all the above
  1991. * functions to happen; if your application is single-threaded you can
  1992. * just call it in your main event loop.
  1993. *
  1994. * Alternatively you can fork a new process that asynchronously handles
  1995. * calling this service in a loop. In that case you are happy if this
  1996. * call blocks your thread until it needs to take care of something and
  1997. * would call it with a large nonzero timeout. Your loop then takes no
  1998. * CPU while there is nothing happening.
  1999. *
  2000. * If you are calling it in a single-threaded app, you don't want it to
  2001. * wait around blocking other things in your loop from happening, so you
  2002. * would call it with a timeout_ms of 0, so it returns immediately if
  2003. * nothing is pending, or as soon as it services whatever was pending.
  2004. */
  2005. LWS_VISIBLE LWS_EXTERN int
  2006. lws_service(struct lws_context *context, int timeout_ms);
  2007. /**
  2008. * lws_service() - Service any pending websocket activity
  2009. *
  2010. * \param context: Websocket context
  2011. * \param timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
  2012. * service otherwise block and service immediately, returning
  2013. * after the timeout if nothing needed service.
  2014. *
  2015. * Same as lws_service(), but for a specific thread service index. Only needed
  2016. * if you are spawning multiple service threads.
  2017. */
  2018. LWS_VISIBLE LWS_EXTERN int
  2019. lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
  2020. /**
  2021. * lws_cancel_service_pt() - Cancel servicing of pending socket activity
  2022. * on one thread
  2023. * \param wsi: Cancel service on the thread this wsi is serviced by
  2024. *
  2025. * This function lets a call to lws_service() waiting for a timeout
  2026. * immediately return.
  2027. *
  2028. * It works by creating a phony event and then swallowing it silently.
  2029. *
  2030. * The reason it may be needed is when waiting in poll(), changes to
  2031. * the event masks are ignored by the OS until poll() is reentered. This
  2032. * lets you halt the poll() wait and make the reentry happen immediately
  2033. * instead of having the wait out the rest of the poll timeout.
  2034. */
  2035. LWS_VISIBLE LWS_EXTERN void
  2036. lws_cancel_service_pt(struct lws *wsi);
  2037. /**
  2038. * lws_cancel_service() - Cancel wait for new pending socket activity
  2039. * \param context: Websocket context
  2040. *
  2041. * This function let a call to lws_service() waiting for a timeout
  2042. * immediately return.
  2043. *
  2044. * What it basically does is provide a fake event that will be swallowed,
  2045. * so the wait in poll() is ended. That's useful because poll() doesn't
  2046. * attend to changes in POLLIN/OUT/ERR until it re-enters the wait.
  2047. */
  2048. LWS_VISIBLE LWS_EXTERN void
  2049. lws_cancel_service(struct lws_context *context);
  2050. /**
  2051. * lws_service_fd() - Service polled socket with something waiting
  2052. * \param context: Websocket context
  2053. * \param pollfd: The pollfd entry describing the socket fd and which events
  2054. * happened.
  2055. *
  2056. * This function takes a pollfd that has POLLIN or POLLOUT activity and
  2057. * services it according to the state of the associated
  2058. * struct lws.
  2059. *
  2060. * The one call deals with all "service" that might happen on a socket
  2061. * including listen accepts, http files as well as websocket protocol.
  2062. *
  2063. * If a pollfd says it has something, you can just pass it to
  2064. * lws_service_fd() whether it is a socket handled by lws or not.
  2065. * If it sees it is a lws socket, the traffic will be handled and
  2066. * pollfd->revents will be zeroed now.
  2067. *
  2068. * If the socket is foreign to lws, it leaves revents alone. So you can
  2069. * see if you should service yourself by checking the pollfd revents
  2070. * after letting lws try to service it.
  2071. */
  2072. LWS_VISIBLE LWS_EXTERN int
  2073. lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd);
  2074. /**
  2075. * lws_service_fd_tsi() - Service polled socket in specific service thread
  2076. * \param context: Websocket context
  2077. * \param pollfd: The pollfd entry describing the socket fd and which events
  2078. * happened.
  2079. * \param tsi: thread service index
  2080. *
  2081. * Same as lws_service_fd() but used with multiple service threads
  2082. */
  2083. LWS_VISIBLE LWS_EXTERN int
  2084. lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd,
  2085. int tsi);
  2086. /**
  2087. * lws_service_adjust_timeout() - Check for any connection needing forced service
  2088. * \param context: Websocket context
  2089. * \param timeout_ms: The original poll timeout value. You can just set this
  2090. * to 1 if you don't really have a poll timeout.
  2091. * \param tsi: thread service index
  2092. *
  2093. * Under some conditions connections may need service even though there is no
  2094. * pending network action on them, this is "forced service". For default
  2095. * poll() and libuv / libev, the library takes care of calling this and
  2096. * dealing with it for you. But for external poll() integration, you need
  2097. * access to the apis.
  2098. *
  2099. * If anybody needs "forced service", returned timeout is zero. In that case,
  2100. * you can call lws_plat_service_tsi() with a timeout of -1 to only service
  2101. * guys who need forced service.
  2102. */
  2103. LWS_VISIBLE LWS_EXTERN int
  2104. lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi);
  2105. /**
  2106. * lws_plat_service_tsi() - Lowlevel platform-specific service api
  2107. * \param context: Websocket context
  2108. * \param timeout_ms: The original poll timeout value. You can just set this
  2109. * to 1 if you don't really have a poll timeout.
  2110. * \param tsi: thread service index
  2111. *
  2112. * For default poll() and libuv/ev, lws takes care of using this for you. and
  2113. * you can ignore it.
  2114. *
  2115. * But for external poll() integration, you need access to this api to service
  2116. * connections that need to be serviced but have no pending network activity.
  2117. *
  2118. * See lws_service_adjust_timeout() for more info.
  2119. */
  2120. LWS_EXTERN LWS_VISIBLE int
  2121. lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
  2122. ///@}
  2123. /*! \defgroup http HTTP
  2124. Modules related to handling HTTP
  2125. */
  2126. //@{
  2127. /*! \defgroup httpft HTTP File transfer
  2128. * \ingroup http
  2129. APIs for sending local files in response to HTTP requests
  2130. */
  2131. //@{
  2132. /**
  2133. * lws_get_mimetype() - Determine mimetype to use from filename
  2134. *
  2135. * \param file: filename
  2136. * \param m: NULL, or mount context
  2137. *
  2138. * This uses a canned list of known filetypes first, if no match and m is
  2139. * non-NULL, then tries a list of per-mount file suffix to mimtype mappings.
  2140. *
  2141. * Returns either NULL or a pointer to the mimetype matching the file.
  2142. */
  2143. LWS_VISIBLE LWS_EXTERN const char *
  2144. lws_get_mimetype(const char *file, const struct lws_http_mount *m);
  2145. /**
  2146. * lws_serve_http_file() - Send a file back to the client using http
  2147. * \param wsi: Websocket instance (available from user callback)
  2148. * \param file: The file to issue over http
  2149. * \param content_type: The http content type, eg, text/html
  2150. * \param other_headers: NULL or pointer to header string
  2151. * \param other_headers_len: length of the other headers if non-NULL
  2152. *
  2153. * This function is intended to be called from the callback in response
  2154. * to http requests from the client. It allows the callback to issue
  2155. * local files down the http link in a single step.
  2156. *
  2157. * Returning <0 indicates error and the wsi should be closed. Returning
  2158. * >0 indicates the file was completely sent and
  2159. * lws_http_transaction_completed() called on the wsi (and close if != 0)
  2160. * ==0 indicates the file transfer is started and needs more service later,
  2161. * the wsi should be left alone.
  2162. */
  2163. LWS_VISIBLE LWS_EXTERN int
  2164. lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
  2165. const char *other_headers, int other_headers_len);
  2166. LWS_VISIBLE LWS_EXTERN int
  2167. lws_serve_http_file_fragment(struct lws *wsi);
  2168. //@}
  2169. /*! \defgroup html-chunked-substitution HTML Chunked Substitution
  2170. * \ingroup http
  2171. *
  2172. * ##HTML chunked Substitution
  2173. *
  2174. * APIs for receiving chunks of text, replacing a set of variable names via
  2175. * a callback, and then prepending and appending HTML chunked encoding
  2176. * headers.
  2177. */
  2178. //@{
  2179. enum http_status {
  2180. HTTP_STATUS_OK = 200,
  2181. HTTP_STATUS_NO_CONTENT = 204,
  2182. HTTP_STATUS_MOVED_PERMANENTLY = 301,
  2183. HTTP_STATUS_FOUND = 302,
  2184. HTTP_STATUS_SEE_OTHER = 303,
  2185. HTTP_STATUS_BAD_REQUEST = 400,
  2186. HTTP_STATUS_UNAUTHORIZED,
  2187. HTTP_STATUS_PAYMENT_REQUIRED,
  2188. HTTP_STATUS_FORBIDDEN,
  2189. HTTP_STATUS_NOT_FOUND,
  2190. HTTP_STATUS_METHOD_NOT_ALLOWED,
  2191. HTTP_STATUS_NOT_ACCEPTABLE,
  2192. HTTP_STATUS_PROXY_AUTH_REQUIRED,
  2193. HTTP_STATUS_REQUEST_TIMEOUT,
  2194. HTTP_STATUS_CONFLICT,
  2195. HTTP_STATUS_GONE,
  2196. HTTP_STATUS_LENGTH_REQUIRED,
  2197. HTTP_STATUS_PRECONDITION_FAILED,
  2198. HTTP_STATUS_REQ_ENTITY_TOO_LARGE,
  2199. HTTP_STATUS_REQ_URI_TOO_LONG,
  2200. HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
  2201. HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE,
  2202. HTTP_STATUS_EXPECTATION_FAILED,
  2203. HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
  2204. HTTP_STATUS_NOT_IMPLEMENTED,
  2205. HTTP_STATUS_BAD_GATEWAY,
  2206. HTTP_STATUS_SERVICE_UNAVAILABLE,
  2207. HTTP_STATUS_GATEWAY_TIMEOUT,
  2208. HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED,
  2209. };
  2210. struct lws_process_html_args {
  2211. char *p; /**< pointer to the buffer containing the data */
  2212. int len; /**< length of the original data at p */
  2213. int max_len; /**< maximum length we can grow the data to */
  2214. int final; /**< set if this is the last chunk of the file */
  2215. };
  2216. typedef const char *(*lws_process_html_state_cb)(void *data, int index);
  2217. struct lws_process_html_state {
  2218. char *start; /**< pointer to start of match */
  2219. char swallow[16]; /**< matched character buffer */
  2220. int pos; /**< position in match */
  2221. void *data; /**< opaque pointer */
  2222. const char * const *vars; /**< list of variable names */
  2223. int count_vars; /**< count of variable names */
  2224. lws_process_html_state_cb replace; /**< called on match to perform substitution */
  2225. };
  2226. /*! lws_chunked_html_process() - generic chunked substitution
  2227. * \param args: buffer to process using chunked encoding
  2228. * \param s: current processing state
  2229. */
  2230. LWS_VISIBLE LWS_EXTERN int
  2231. lws_chunked_html_process(struct lws_process_html_args *args,
  2232. struct lws_process_html_state *s);
  2233. //@}
  2234. /** \defgroup HTTP-headers-read HTTP headers: read
  2235. * \ingroup http
  2236. *
  2237. * ##HTTP header releated functions
  2238. *
  2239. * In lws the client http headers are temporarily stored in a pool, only for the
  2240. * duration of the http part of the handshake. It's because in most cases,
  2241. * the header content is ignored for the whole rest of the connection lifetime
  2242. * and would then just be taking up space needlessly.
  2243. *
  2244. * During LWS_CALLBACK_HTTP when the URI path is delivered is the last time
  2245. * the http headers are still allocated, you can use these apis then to
  2246. * look at and copy out interesting header content (cookies, etc)
  2247. *
  2248. * Notice that the header total length reported does not include a terminating
  2249. * '\0', however you must allocate for it when using the _copy apis. So the
  2250. * length reported for a header containing "123" is 3, but you must provide
  2251. * a buffer of length 4 so that "123\0" may be copied into it, or the copy
  2252. * will fail with a nonzero return code.
  2253. *
  2254. * In the special case of URL arguments, like ?x=1&y=2, the arguments are
  2255. * stored in a token named for the method, eg, WSI_TOKEN_GET_URI if it
  2256. * was a GET or WSI_TOKEN_POST_URI if POST. You can check the total
  2257. * length to confirm the method.
  2258. *
  2259. * For URL arguments, each argument is stored urldecoded in a "fragment", so
  2260. * you can use the fragment-aware api lws_hdr_copy_fragment() to access each
  2261. * argument in turn: the fragments contain urldecoded strings like x=1 or y=2.
  2262. *
  2263. * As a convenience, lws has an api that will find the fragment with a
  2264. * given name= part, lws_get_urlarg_by_name().
  2265. */
  2266. ///@{
  2267. /** struct lws_tokens
  2268. * you need these to look at headers that have been parsed if using the
  2269. * LWS_CALLBACK_FILTER_CONNECTION callback. If a header from the enum
  2270. * list below is absent, .token = NULL and token_len = 0. Otherwise .token
  2271. * points to .token_len chars containing that header content.
  2272. */
  2273. struct lws_tokens {
  2274. char *token; /**< pointer to start of the token */
  2275. int token_len; /**< length of the token's value */
  2276. };
  2277. /* enum lws_token_indexes
  2278. * these have to be kept in sync with lextable.h / minilex.c
  2279. *
  2280. * NOTE: These public enums are part of the abi. If you want to add one,
  2281. * add it at where specified so existing users are unaffected.
  2282. */
  2283. enum lws_token_indexes {
  2284. WSI_TOKEN_GET_URI = 0,
  2285. WSI_TOKEN_POST_URI = 1,
  2286. WSI_TOKEN_OPTIONS_URI = 2,
  2287. WSI_TOKEN_HOST = 3,
  2288. WSI_TOKEN_CONNECTION = 4,
  2289. WSI_TOKEN_UPGRADE = 5,
  2290. WSI_TOKEN_ORIGIN = 6,
  2291. WSI_TOKEN_DRAFT = 7,
  2292. WSI_TOKEN_CHALLENGE = 8,
  2293. WSI_TOKEN_EXTENSIONS = 9,
  2294. WSI_TOKEN_KEY1 = 10,
  2295. WSI_TOKEN_KEY2 = 11,
  2296. WSI_TOKEN_PROTOCOL = 12,
  2297. WSI_TOKEN_ACCEPT = 13,
  2298. WSI_TOKEN_NONCE = 14,
  2299. WSI_TOKEN_HTTP = 15,
  2300. WSI_TOKEN_HTTP2_SETTINGS = 16,
  2301. WSI_TOKEN_HTTP_ACCEPT = 17,
  2302. WSI_TOKEN_HTTP_AC_REQUEST_HEADERS = 18,
  2303. WSI_TOKEN_HTTP_IF_MODIFIED_SINCE = 19,
  2304. WSI_TOKEN_HTTP_IF_NONE_MATCH = 20,
  2305. WSI_TOKEN_HTTP_ACCEPT_ENCODING = 21,
  2306. WSI_TOKEN_HTTP_ACCEPT_LANGUAGE = 22,
  2307. WSI_TOKEN_HTTP_PRAGMA = 23,
  2308. WSI_TOKEN_HTTP_CACHE_CONTROL = 24,
  2309. WSI_TOKEN_HTTP_AUTHORIZATION = 25,
  2310. WSI_TOKEN_HTTP_COOKIE = 26,
  2311. WSI_TOKEN_HTTP_CONTENT_LENGTH = 27,
  2312. WSI_TOKEN_HTTP_CONTENT_TYPE = 28,
  2313. WSI_TOKEN_HTTP_DATE = 29,
  2314. WSI_TOKEN_HTTP_RANGE = 30,
  2315. WSI_TOKEN_HTTP_REFERER = 31,
  2316. WSI_TOKEN_KEY = 32,
  2317. WSI_TOKEN_VERSION = 33,
  2318. WSI_TOKEN_SWORIGIN = 34,
  2319. WSI_TOKEN_HTTP_COLON_AUTHORITY = 35,
  2320. WSI_TOKEN_HTTP_COLON_METHOD = 36,
  2321. WSI_TOKEN_HTTP_COLON_PATH = 37,
  2322. WSI_TOKEN_HTTP_COLON_SCHEME = 38,
  2323. WSI_TOKEN_HTTP_COLON_STATUS = 39,
  2324. WSI_TOKEN_HTTP_ACCEPT_CHARSET = 40,
  2325. WSI_TOKEN_HTTP_ACCEPT_RANGES = 41,
  2326. WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN = 42,
  2327. WSI_TOKEN_HTTP_AGE = 43,
  2328. WSI_TOKEN_HTTP_ALLOW = 44,
  2329. WSI_TOKEN_HTTP_CONTENT_DISPOSITION = 45,
  2330. WSI_TOKEN_HTTP_CONTENT_ENCODING = 46,
  2331. WSI_TOKEN_HTTP_CONTENT_LANGUAGE = 47,
  2332. WSI_TOKEN_HTTP_CONTENT_LOCATION = 48,
  2333. WSI_TOKEN_HTTP_CONTENT_RANGE = 49,
  2334. WSI_TOKEN_HTTP_ETAG = 50,
  2335. WSI_TOKEN_HTTP_EXPECT = 51,
  2336. WSI_TOKEN_HTTP_EXPIRES = 52,
  2337. WSI_TOKEN_HTTP_FROM = 53,
  2338. WSI_TOKEN_HTTP_IF_MATCH = 54,
  2339. WSI_TOKEN_HTTP_IF_RANGE = 55,
  2340. WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE = 56,
  2341. WSI_TOKEN_HTTP_LAST_MODIFIED = 57,
  2342. WSI_TOKEN_HTTP_LINK = 58,
  2343. WSI_TOKEN_HTTP_LOCATION = 59,
  2344. WSI_TOKEN_HTTP_MAX_FORWARDS = 60,
  2345. WSI_TOKEN_HTTP_PROXY_AUTHENTICATE = 61,
  2346. WSI_TOKEN_HTTP_PROXY_AUTHORIZATION = 62,
  2347. WSI_TOKEN_HTTP_REFRESH = 63,
  2348. WSI_TOKEN_HTTP_RETRY_AFTER = 64,
  2349. WSI_TOKEN_HTTP_SERVER = 65,
  2350. WSI_TOKEN_HTTP_SET_COOKIE = 66,
  2351. WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY = 67,
  2352. WSI_TOKEN_HTTP_TRANSFER_ENCODING = 68,
  2353. WSI_TOKEN_HTTP_USER_AGENT = 69,
  2354. WSI_TOKEN_HTTP_VARY = 70,
  2355. WSI_TOKEN_HTTP_VIA = 71,
  2356. WSI_TOKEN_HTTP_WWW_AUTHENTICATE = 72,
  2357. WSI_TOKEN_PATCH_URI = 73,
  2358. WSI_TOKEN_PUT_URI = 74,
  2359. WSI_TOKEN_DELETE_URI = 75,
  2360. WSI_TOKEN_HTTP_URI_ARGS = 76,
  2361. WSI_TOKEN_PROXY = 77,
  2362. WSI_TOKEN_HTTP_X_REAL_IP = 78,
  2363. WSI_TOKEN_HTTP1_0 = 79,
  2364. /****** add new things just above ---^ ******/
  2365. /* use token storage to stash these internally, not for
  2366. * user use */
  2367. _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
  2368. _WSI_TOKEN_CLIENT_PEER_ADDRESS,
  2369. _WSI_TOKEN_CLIENT_URI,
  2370. _WSI_TOKEN_CLIENT_HOST,
  2371. _WSI_TOKEN_CLIENT_ORIGIN,
  2372. _WSI_TOKEN_CLIENT_METHOD,
  2373. /* always last real token index*/
  2374. WSI_TOKEN_COUNT,
  2375. /* parser state additions, no storage associated */
  2376. WSI_TOKEN_NAME_PART,
  2377. WSI_TOKEN_SKIPPING,
  2378. WSI_TOKEN_SKIPPING_SAW_CR,
  2379. WSI_PARSING_COMPLETE,
  2380. WSI_INIT_TOKEN_MUXURL,
  2381. };
  2382. struct lws_token_limits {
  2383. unsigned short token_limit[WSI_TOKEN_COUNT]; /**< max chars for this token */
  2384. };
  2385. /**
  2386. * lws_token_to_string() - returns a textual representation of a hdr token index
  2387. *
  2388. * \param: token index
  2389. */
  2390. LWS_VISIBLE LWS_EXTERN const unsigned char *
  2391. lws_token_to_string(enum lws_token_indexes token);
  2392. /**
  2393. * lws_hdr_total_length: report length of all fragments of a header totalled up
  2394. * The returned length does not include the space for a
  2395. * terminating '\0'
  2396. *
  2397. * \param wsi: websocket connection
  2398. * \param h: which header index we are interested in
  2399. */
  2400. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  2401. lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h);
  2402. /**
  2403. * lws_hdr_fragment_length: report length of a single fragment of a header
  2404. * The returned length does not include the space for a
  2405. * terminating '\0'
  2406. *
  2407. * \param wsi: websocket connection
  2408. * \param h: which header index we are interested in
  2409. * \param frag_idx: which fragment of h we want to get the length of
  2410. */
  2411. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  2412. lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx);
  2413. /**
  2414. * lws_hdr_copy() - copy a single fragment of the given header to a buffer
  2415. * The buffer length len must include space for an additional
  2416. * terminating '\0', or it will fail returning -1.
  2417. *
  2418. * \param wsi: websocket connection
  2419. * \param dest: destination buffer
  2420. * \param len: length of destination buffer
  2421. * \param h: which header index we are interested in
  2422. *
  2423. * copies the whole, aggregated header, even if it was delivered in
  2424. * several actual headers piece by piece
  2425. */
  2426. LWS_VISIBLE LWS_EXTERN int
  2427. lws_hdr_copy(struct lws *wsi, char *dest, int len, enum lws_token_indexes h);
  2428. /**
  2429. * lws_hdr_copy_fragment() - copy a single fragment of the given header to a buffer
  2430. * The buffer length len must include space for an additional
  2431. * terminating '\0', or it will fail returning -1.
  2432. * If the requested fragment index is not present, it fails
  2433. * returning -1.
  2434. *
  2435. * \param wsi: websocket connection
  2436. * \param dest: destination buffer
  2437. * \param len: length of destination buffer
  2438. * \param h: which header index we are interested in
  2439. * \param frag_idx: which fragment of h we want to copy
  2440. *
  2441. * Normally this is only useful
  2442. * to parse URI arguments like ?x=1&y=2, token index WSI_TOKEN_HTTP_URI_ARGS
  2443. * fragment 0 will contain "x=1" and fragment 1 "y=2"
  2444. */
  2445. LWS_VISIBLE LWS_EXTERN int
  2446. lws_hdr_copy_fragment(struct lws *wsi, char *dest, int len,
  2447. enum lws_token_indexes h, int frag_idx);
  2448. /**
  2449. * lws_get_urlarg_by_name() - return pointer to arg value if present
  2450. * \param wsi: the connection to check
  2451. * \param name: the arg name, like "token="
  2452. * \param buf: the buffer to receive the urlarg (including the name= part)
  2453. * \param len: the length of the buffer to receive the urlarg
  2454. *
  2455. * Returns NULL if not found or a pointer inside buf to just after the
  2456. * name= part.
  2457. */
  2458. LWS_VISIBLE LWS_EXTERN const char *
  2459. lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len);
  2460. ///@}
  2461. /*! \defgroup HTTP-headers-create HTTP headers: create
  2462. *
  2463. * ## HTTP headers: Create
  2464. *
  2465. * These apis allow you to create HTTP response headers in a way compatible with
  2466. * both HTTP/1.x and HTTP/2.
  2467. *
  2468. * They each append to a buffer taking care about the buffer end, which is
  2469. * passed in as a pointer. When data is written to the buffer, the current
  2470. * position p is updated accordingly.
  2471. *
  2472. * All of these apis are LWS_WARN_UNUSED_RESULT as they can run out of space
  2473. * and fail with nonzero return.
  2474. */
  2475. ///@{
  2476. /**
  2477. * lws_add_http_header_status() - add the HTTP response status code
  2478. *
  2479. * \param wsi: the connection to check
  2480. * \param code: an HTTP code like 200, 404 etc (see enum http_status)
  2481. * \param p: pointer to current position in buffer pointer
  2482. * \param end: pointer to end of buffer
  2483. *
  2484. * Adds the initial response code, so should be called first
  2485. */
  2486. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  2487. lws_add_http_header_status(struct lws *wsi,
  2488. unsigned int code, unsigned char **p,
  2489. unsigned char *end);
  2490. /**
  2491. * lws_add_http_header_by_name() - append named header and value
  2492. *
  2493. * \param wsi: the connection to check
  2494. * \param name: the hdr name, like "my-header"
  2495. * \param value: the value after the = for this header
  2496. * \param length: the length of the value
  2497. * \param p: pointer to current position in buffer pointer
  2498. * \param end: pointer to end of buffer
  2499. *
  2500. * Appends name: value to the headers
  2501. */
  2502. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  2503. lws_add_http_header_by_name(struct lws *wsi, const unsigned char *name,
  2504. const unsigned char *value, int length,
  2505. unsigned char **p, unsigned char *end);
  2506. /**
  2507. * lws_add_http_header_by_token() - append given header and value
  2508. *
  2509. * \param wsi: the connection to check
  2510. * \param token: the token index for the hdr
  2511. * \param value: the value after the = for this header
  2512. * \param length: the length of the value
  2513. * \param p: pointer to current position in buffer pointer
  2514. * \param end: pointer to end of buffer
  2515. *
  2516. * Appends name=value to the headers, but is able to take advantage of better
  2517. * HTTP/2 coding mechanisms where possible.
  2518. */
  2519. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  2520. lws_add_http_header_by_token(struct lws *wsi, enum lws_token_indexes token,
  2521. const unsigned char *value, int length,
  2522. unsigned char **p, unsigned char *end);
  2523. /**
  2524. * lws_add_http_header_by_name() - append content-length helper
  2525. *
  2526. * \param wsi: the connection to check
  2527. * \param content_length: the content length to use
  2528. * \param p: pointer to current position in buffer pointer
  2529. * \param end: pointer to end of buffer
  2530. *
  2531. * Appends content-length: content_length to the headers
  2532. */
  2533. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  2534. lws_add_http_header_content_length(struct lws *wsi,
  2535. unsigned long content_length,
  2536. unsigned char **p, unsigned char *end);
  2537. /**
  2538. * lws_finalize_http_header() - terminate header block
  2539. *
  2540. * \param wsi: the connection to check
  2541. * \param p: pointer to current position in buffer pointer
  2542. * \param end: pointer to end of buffer
  2543. *
  2544. * Indicates no more headers will be added
  2545. */
  2546. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  2547. lws_finalize_http_header(struct lws *wsi, unsigned char **p,
  2548. unsigned char *end);
  2549. ///@}
  2550. /** \defgroup form-parsing Form Parsing
  2551. * \ingroup http
  2552. * ##POSTed form parsing functions
  2553. *
  2554. * These lws_spa (stateful post arguments) apis let you parse and urldecode
  2555. * POSTed form arguments, both using simple urlencoded and multipart transfer
  2556. * encoding.
  2557. *
  2558. * It's capable of handling file uploads as well a named input parsing,
  2559. * and the apis are the same for both form upload styles.
  2560. *
  2561. * You feed it a list of parameter names and it creates pointers to the
  2562. * urldecoded arguments: file upload parameters pass the file data in chunks to
  2563. * a user-supplied callback as they come.
  2564. *
  2565. * Since it's stateful, it handles the incoming data needing more than one
  2566. * POST_BODY callback and has no limit on uploaded file size.
  2567. */
  2568. ///@{
  2569. /** enum lws_spa_fileupload_states */
  2570. enum lws_spa_fileupload_states {
  2571. LWS_UFS_CONTENT,
  2572. /**< a chunk of file content has arrived */
  2573. LWS_UFS_FINAL_CONTENT,
  2574. /**< the last chunk (possibly zero length) of file content has arrived */
  2575. LWS_UFS_OPEN
  2576. /**< a new file is starting to arrive */
  2577. };
  2578. /**
  2579. * lws_spa_fileupload_cb() - callback to receive file upload data
  2580. *
  2581. * \param data: opt_data pointer set in lws_spa_create
  2582. * \param name: name of the form field being uploaded
  2583. * \param filename: original filename from client
  2584. * \param buf: start of data to receive
  2585. * \param len: length of data to receive
  2586. * \param state: information about how this call relates to file
  2587. *
  2588. * Notice name and filename shouldn't be trusted, as they are passed from
  2589. * HTTP provided by the client.
  2590. */
  2591. typedef int (*lws_spa_fileupload_cb)(void *data, const char *name,
  2592. const char *filename, char *buf, int len,
  2593. enum lws_spa_fileupload_states state);
  2594. /** struct lws_spa - opaque urldecode parser capable of handling multipart
  2595. * and file uploads */
  2596. struct lws_spa;
  2597. /**
  2598. * lws_spa_create() - create urldecode parser
  2599. *
  2600. * \param wsi: lws connection (used to find Content Type)
  2601. * \param param_names: array of form parameter names, like "username"
  2602. * \param count_params: count of param_names
  2603. * \param max_storage: total amount of form parameter values we can store
  2604. * \param opt_cb: NULL, or callback to receive file upload data.
  2605. * \param opt_data: NULL, or user pointer provided to opt_cb.
  2606. *
  2607. * Creates a urldecode parser and initializes it.
  2608. *
  2609. * opt_cb can be NULL if you just want normal name=value parsing, however
  2610. * if one or more entries in your form are bulk data (file transfer), you
  2611. * can provide this callback and filter on the name callback parameter to
  2612. * treat that urldecoded data separately. The callback should return -1
  2613. * in case of fatal error, and 0 if OK.
  2614. */
  2615. LWS_VISIBLE LWS_EXTERN struct lws_spa *
  2616. lws_spa_create(struct lws *wsi, const char * const *param_names,
  2617. int count_params, int max_storage, lws_spa_fileupload_cb opt_cb,
  2618. void *opt_data);
  2619. /**
  2620. * lws_spa_process() - parses a chunk of input data
  2621. *
  2622. * \param spa: the parser object previously created
  2623. * \param in: incoming, urlencoded data
  2624. * \param len: count of bytes valid at \param in
  2625. */
  2626. LWS_VISIBLE LWS_EXTERN int
  2627. lws_spa_process(struct lws_spa *spa, const char *in, int len);
  2628. /**
  2629. * lws_spa_finalize() - indicate incoming data completed
  2630. *
  2631. * \param spa: the parser object previously created
  2632. */
  2633. LWS_VISIBLE LWS_EXTERN int
  2634. lws_spa_finalize(struct lws_spa *spa);
  2635. /**
  2636. * lws_spa_get_length() - return length of parameter value
  2637. *
  2638. * \param spa: the parser object previously created
  2639. * \param n: parameter ordinal to return length of value for
  2640. */
  2641. LWS_VISIBLE LWS_EXTERN int
  2642. lws_spa_get_length(struct lws_spa *spa, int n);
  2643. /**
  2644. * lws_spa_get_string() - return pointer to parameter value
  2645. * \param spa: the parser object previously created
  2646. * \param n: parameter ordinal to return pointer to value for
  2647. */
  2648. LWS_VISIBLE LWS_EXTERN const char *
  2649. lws_spa_get_string(struct lws_spa *spa, int n);
  2650. /**
  2651. * lws_spa_destroy() - destroy parser object
  2652. *
  2653. * \param spa: the parser object previously created
  2654. */
  2655. LWS_VISIBLE LWS_EXTERN int
  2656. lws_spa_destroy(struct lws_spa *spa);
  2657. ///@}
  2658. /*! \defgroup urlendec Urlencode and Urldecode
  2659. * \ingroup http
  2660. *
  2661. * ##HTML chunked Substitution
  2662. *
  2663. * APIs for receiving chunks of text, replacing a set of variable names via
  2664. * a callback, and then prepending and appending HTML chunked encoding
  2665. * headers.
  2666. */
  2667. //@{
  2668. /**
  2669. * lws_urlencode() - like strncpy but with urlencoding
  2670. *
  2671. * \param escaped: output buffer
  2672. * \param string: input buffer ('/0' terminated)
  2673. * \param len: output buffer max length
  2674. *
  2675. * Because urlencoding expands the output string, it's not
  2676. * possible to do it in-place, ie, with escaped == string
  2677. */
  2678. LWS_VISIBLE LWS_EXTERN const char *
  2679. lws_urlencode(char *escaped, const char *string, int len);
  2680. /*
  2681. * URLDECODE 1 / 2
  2682. *
  2683. * This simple urldecode only operates until the first '\0' and requires the
  2684. * data to exist all at once
  2685. */
  2686. /**
  2687. * lws_urldecode() - like strncpy but with urldecoding
  2688. *
  2689. * \param string: output buffer
  2690. * \param escaped: input buffer ('\0' terminated)
  2691. * \param len: output buffer max length
  2692. *
  2693. * This is only useful for '\0' terminated strings
  2694. *
  2695. * Since urldecoding only shrinks the output string, it is possible to
  2696. * do it in-place, ie, string == escaped
  2697. */
  2698. LWS_VISIBLE LWS_EXTERN int
  2699. lws_urldecode(char *string, const char *escaped, int len);
  2700. ///@}
  2701. /**
  2702. * lws_return_http_status() - Return simple http status
  2703. * \param wsi: Websocket instance (available from user callback)
  2704. * \param code: Status index, eg, 404
  2705. * \param html_body: User-readable HTML description < 1KB, or NULL
  2706. *
  2707. * Helper to report HTTP errors back to the client cleanly and
  2708. * consistently
  2709. */
  2710. LWS_VISIBLE LWS_EXTERN int
  2711. lws_return_http_status(struct lws *wsi, unsigned int code,
  2712. const char *html_body);
  2713. /**
  2714. * lws_http_redirect() - write http redirect into buffer
  2715. *
  2716. * \param wsi: websocket connection
  2717. * \param code: HTTP response code (eg, 301)
  2718. * \param loc: where to redirect to
  2719. * \param len: length of loc
  2720. * \param p: pointer current position in buffer (updated as we write)
  2721. * \param end: pointer to end of buffer
  2722. */
  2723. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  2724. lws_http_redirect(struct lws *wsi, int code, const unsigned char *loc, int len,
  2725. unsigned char **p, unsigned char *end);
  2726. /**
  2727. * lws_http_transaction_completed() - wait for new http transaction or close
  2728. * \param wsi: websocket connection
  2729. *
  2730. * Returns 1 if the HTTP connection must close now
  2731. * Returns 0 and resets connection to wait for new HTTP header /
  2732. * transaction if possible
  2733. */
  2734. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  2735. lws_http_transaction_completed(struct lws *wsi);
  2736. ///@}
  2737. /*! \defgroup pur Sanitize / purify SQL and JSON helpers
  2738. *
  2739. * ##Sanitize / purify SQL and JSON helpers
  2740. *
  2741. * APIs for escaping untrusted JSON and SQL safely before use
  2742. */
  2743. //@{
  2744. /**
  2745. * lws_sql_purify() - like strncpy but with escaping for sql quotes
  2746. *
  2747. * \param escaped: output buffer
  2748. * \param string: input buffer ('/0' terminated)
  2749. * \param len: output buffer max length
  2750. *
  2751. * Because escaping expands the output string, it's not
  2752. * possible to do it in-place, ie, with escaped == string
  2753. */
  2754. LWS_VISIBLE LWS_EXTERN const char *
  2755. lws_sql_purify(char *escaped, const char *string, int len);
  2756. /**
  2757. * lws_json_purify() - like strncpy but with escaping for json chars
  2758. *
  2759. * \param escaped: output buffer
  2760. * \param string: input buffer ('/0' terminated)
  2761. * \param len: output buffer max length
  2762. *
  2763. * Because escaping expands the output string, it's not
  2764. * possible to do it in-place, ie, with escaped == string
  2765. */
  2766. LWS_VISIBLE LWS_EXTERN const char *
  2767. lws_json_purify(char *escaped, const char *string, int len);
  2768. ///@}
  2769. /*! \defgroup ev libev helpers
  2770. *
  2771. * ##libev helpers
  2772. *
  2773. * APIs specific to libev event loop itegration
  2774. */
  2775. ///@{
  2776. #ifdef LWS_USE_LIBEV
  2777. typedef void (lws_ev_signal_cb_t)(EV_P_ struct ev_signal *w, int revents);
  2778. LWS_VISIBLE LWS_EXTERN int
  2779. lws_ev_sigint_cfg(struct lws_context *context, int use_ev_sigint,
  2780. lws_ev_signal_cb_t *cb);
  2781. LWS_VISIBLE LWS_EXTERN int
  2782. lws_ev_initloop(struct lws_context *context, struct ev_loop *loop, int tsi);
  2783. LWS_VISIBLE LWS_EXTERN void
  2784. lws_ev_sigint_cb(struct ev_loop *loop, struct ev_signal *watcher, int revents);
  2785. #endif /* LWS_USE_LIBEV */
  2786. ///@}
  2787. /*! \defgroup uv libuv helpers
  2788. *
  2789. * ##libuv helpers
  2790. *
  2791. * APIs specific to libuv event loop itegration
  2792. */
  2793. ///@{
  2794. #ifdef LWS_USE_LIBUV
  2795. LWS_VISIBLE LWS_EXTERN int
  2796. lws_uv_sigint_cfg(struct lws_context *context, int use_uv_sigint,
  2797. uv_signal_cb cb);
  2798. LWS_VISIBLE LWS_EXTERN void
  2799. lws_libuv_run(const struct lws_context *context, int tsi);
  2800. LWS_VISIBLE LWS_EXTERN void
  2801. lws_libuv_stop(struct lws_context *context);
  2802. LWS_VISIBLE LWS_EXTERN int
  2803. lws_uv_initloop(struct lws_context *context, uv_loop_t *loop, int tsi);
  2804. LWS_VISIBLE LWS_EXTERN uv_loop_t *
  2805. lws_uv_getloop(struct lws_context *context, int tsi);
  2806. LWS_VISIBLE LWS_EXTERN void
  2807. lws_uv_sigint_cb(uv_signal_t *watcher, int signum);
  2808. #endif /* LWS_USE_LIBUV */
  2809. ///@}
  2810. /*! \defgroup timeout Connection timeouts
  2811. APIs related to setting connection timeouts
  2812. */
  2813. //@{
  2814. /*
  2815. * NOTE: These public enums are part of the abi. If you want to add one,
  2816. * add it at where specified so existing users are unaffected.
  2817. */
  2818. enum pending_timeout {
  2819. NO_PENDING_TIMEOUT = 0,
  2820. PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE = 1,
  2821. PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE = 2,
  2822. PENDING_TIMEOUT_ESTABLISH_WITH_SERVER = 3,
  2823. PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE = 4,
  2824. PENDING_TIMEOUT_AWAITING_PING = 5,
  2825. PENDING_TIMEOUT_CLOSE_ACK = 6,
  2826. PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE = 7,
  2827. PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE = 8,
  2828. PENDING_TIMEOUT_SSL_ACCEPT = 9,
  2829. PENDING_TIMEOUT_HTTP_CONTENT = 10,
  2830. PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND = 11,
  2831. PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE = 12,
  2832. PENDING_TIMEOUT_SHUTDOWN_FLUSH = 13,
  2833. PENDING_TIMEOUT_CGI = 14,
  2834. PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE = 15,
  2835. PENDING_TIMEOUT_WS_PONG_CHECK_SEND_PING = 16,
  2836. PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG = 17,
  2837. PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD = 18,
  2838. /****** add new things just above ---^ ******/
  2839. };
  2840. /**
  2841. * lws_set_timeout() - marks the wsi as subject to a timeout
  2842. *
  2843. * You will not need this unless you are doing something special
  2844. *
  2845. * \param wsi: Websocket connection instance
  2846. * \param reason: timeout reason
  2847. * \param secs: how many seconds
  2848. */
  2849. LWS_VISIBLE LWS_EXTERN void
  2850. lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs);
  2851. ///@}
  2852. /*! \defgroup sending-data Sending data
  2853. APIs related to writing data on a connection
  2854. */
  2855. //@{
  2856. #if !defined(LWS_SIZEOFPTR)
  2857. #define LWS_SIZEOFPTR (sizeof (void *))
  2858. #endif
  2859. #if !defined(u_int64_t)
  2860. #define u_int64_t unsigned long long
  2861. #endif
  2862. #if defined(__x86_64__)
  2863. #define _LWS_PAD_SIZE 16 /* Intel recommended for best performance */
  2864. #else
  2865. #define _LWS_PAD_SIZE LWS_SIZEOFPTR /* Size of a pointer on the target arch */
  2866. #endif
  2867. #define _LWS_PAD(n) (((n) % _LWS_PAD_SIZE) ? \
  2868. ((n) + (_LWS_PAD_SIZE - ((n) % _LWS_PAD_SIZE))) : (n))
  2869. #define LWS_PRE _LWS_PAD(4 + 10)
  2870. /* used prior to 1.7 and retained for backward compatibility */
  2871. #define LWS_SEND_BUFFER_PRE_PADDING LWS_PRE
  2872. #define LWS_SEND_BUFFER_POST_PADDING 0
  2873. /*
  2874. * NOTE: These public enums are part of the abi. If you want to add one,
  2875. * add it at where specified so existing users are unaffected.
  2876. */
  2877. enum lws_write_protocol {
  2878. LWS_WRITE_TEXT = 0,
  2879. /**< Send a ws TEXT message,the pointer must have LWS_PRE valid
  2880. * memory behind it. The receiver expects only valid utf-8 in the
  2881. * payload */
  2882. LWS_WRITE_BINARY = 1,
  2883. /**< Send a ws BINARY message, the pointer must have LWS_PRE valid
  2884. * memory behind it. Any sequence of bytes is valid */
  2885. LWS_WRITE_CONTINUATION = 2,
  2886. /**< Continue a previous ws message, the pointer must have LWS_PRE valid
  2887. * memory behind it */
  2888. LWS_WRITE_HTTP = 3,
  2889. /**< Send HTTP content */
  2890. /* LWS_WRITE_CLOSE is handled by lws_close_reason() */
  2891. LWS_WRITE_PING = 5,
  2892. LWS_WRITE_PONG = 6,
  2893. /* Same as write_http but we know this write ends the transaction */
  2894. LWS_WRITE_HTTP_FINAL = 7,
  2895. /* HTTP2 */
  2896. LWS_WRITE_HTTP_HEADERS = 8,
  2897. /**< Send http headers (http2 encodes this payload and LWS_WRITE_HTTP
  2898. * payload differently, http 1.x links also handle this correctly. so
  2899. * to be compatible with both in the future,header response part should
  2900. * be sent using this regardless of http version expected)
  2901. */
  2902. /****** add new things just above ---^ ******/
  2903. /* flags */
  2904. LWS_WRITE_NO_FIN = 0x40,
  2905. /**< This part of the message is not the end of the message */
  2906. LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
  2907. /**< client packet payload goes out on wire unmunged
  2908. * only useful for security tests since normal servers cannot
  2909. * decode the content if used */
  2910. };
  2911. /**
  2912. * lws_write() - Apply protocol then write data to client
  2913. * \param wsi: Websocket instance (available from user callback)
  2914. * \param buf: The data to send. For data being sent on a websocket
  2915. * connection (ie, not default http), this buffer MUST have
  2916. * LWS_PRE bytes valid BEFORE the pointer.
  2917. * This is so the protocol header data can be added in-situ.
  2918. * \param len: Count of the data bytes in the payload starting from buf
  2919. * \param protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one
  2920. * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
  2921. * data on a websockets connection. Remember to allow the extra
  2922. * bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
  2923. * are used.
  2924. *
  2925. * This function provides the way to issue data back to the client
  2926. * for both http and websocket protocols.
  2927. *
  2928. * IMPORTANT NOTICE!
  2929. *
  2930. * When sending with websocket protocol
  2931. *
  2932. * LWS_WRITE_TEXT,
  2933. * LWS_WRITE_BINARY,
  2934. * LWS_WRITE_CONTINUATION,
  2935. * LWS_WRITE_PING,
  2936. * LWS_WRITE_PONG
  2937. *
  2938. * the send buffer has to have LWS_PRE bytes valid BEFORE
  2939. * the buffer pointer you pass to lws_write().
  2940. *
  2941. * This allows us to add protocol info before and after the data, and send as
  2942. * one packet on the network without payload copying, for maximum efficiency.
  2943. *
  2944. * So for example you need this kind of code to use lws_write with a
  2945. * 128-byte payload
  2946. *
  2947. * char buf[LWS_PRE + 128];
  2948. *
  2949. * // fill your part of the buffer... for example here it's all zeros
  2950. * memset(&buf[LWS_PRE], 0, 128);
  2951. *
  2952. * lws_write(wsi, &buf[LWS_PRE], 128, LWS_WRITE_TEXT);
  2953. *
  2954. * When sending HTTP, with
  2955. *
  2956. * LWS_WRITE_HTTP,
  2957. * LWS_WRITE_HTTP_HEADERS
  2958. * LWS_WRITE_HTTP_FINAL
  2959. *
  2960. * there is no protocol data prepended, and don't need to take care about the
  2961. * LWS_PRE bytes valid before the buffer pointer.
  2962. *
  2963. * LWS_PRE is at least the frame nonce + 2 header + 8 length
  2964. * LWS_SEND_BUFFER_POST_PADDING is deprecated, it's now 0 and can be left off.
  2965. * The example apps no longer use it.
  2966. *
  2967. * Pad LWS_PRE to the CPU word size, so that word references
  2968. * to the address immediately after the padding won't cause an unaligned access
  2969. * error. Sometimes for performance reasons the recommended padding is even
  2970. * larger than sizeof(void *).
  2971. *
  2972. * In the case of sending using websocket protocol, be sure to allocate
  2973. * valid storage before and after buf as explained above. This scheme
  2974. * allows maximum efficiency of sending data and protocol in a single
  2975. * packet while not burdening the user code with any protocol knowledge.
  2976. *
  2977. * Return may be -1 for a fatal error needing connection close, or a
  2978. * positive number reflecting the amount of bytes actually sent. This
  2979. * can be less than the requested number of bytes due to OS memory
  2980. * pressure at any given time.
  2981. */
  2982. LWS_VISIBLE LWS_EXTERN int
  2983. lws_write(struct lws *wsi, unsigned char *buf, size_t len,
  2984. enum lws_write_protocol protocol);
  2985. /* helper for case where buffer may be const */
  2986. #define lws_write_http(wsi, buf, len) \
  2987. lws_write(wsi, (unsigned char *)(buf), len, LWS_WRITE_HTTP)
  2988. ///@}
  2989. /** \defgroup callback-when-writeable Callback when writeable
  2990. *
  2991. * ##Callback When Writeable
  2992. *
  2993. * lws can only write data on a connection when it is able to accept more
  2994. * data without blocking.
  2995. *
  2996. * So a basic requirement is we should only use the lws_write() apis when the
  2997. * connection we want to write on says that he can accept more data.
  2998. *
  2999. * When lws cannot complete your send at the time, it will buffer the data
  3000. * and send it in the background, suppressing any further WRITEABLE callbacks
  3001. * on that connection until it completes. So it is important to write new
  3002. * things in a new writeable callback.
  3003. *
  3004. * These apis reflect the various ways we can indicate we would like to be
  3005. * called back when one or more connections is writeable.
  3006. */
  3007. ///@{
  3008. /**
  3009. * lws_callback_on_writable() - Request a callback when this socket
  3010. * becomes able to be written to without
  3011. * blocking
  3012. *
  3013. * \param wsi: Websocket connection instance to get callback for
  3014. *
  3015. * - Which: only this wsi
  3016. * - When: when the individual connection becomes writeable
  3017. * - What: LWS_CALLBACK_*_WRITEABLE
  3018. */
  3019. LWS_VISIBLE LWS_EXTERN int
  3020. lws_callback_on_writable(struct lws *wsi);
  3021. /**
  3022. * lws_callback_on_writable_all_protocol() - Request a callback for all
  3023. * connections on same vhost using the given protocol when it
  3024. * becomes possible to write to each socket without
  3025. * blocking in turn.
  3026. *
  3027. * \param context: lws_context
  3028. * \param protocol: Protocol whose connections will get callbacks
  3029. *
  3030. * - Which: connections using this protocol on ANY VHOST
  3031. * - When: when the individual connection becomes writeable
  3032. * - What: LWS_CALLBACK_*_WRITEABLE
  3033. */
  3034. LWS_VISIBLE LWS_EXTERN int
  3035. lws_callback_on_writable_all_protocol(const struct lws_context *context,
  3036. const struct lws_protocols *protocol);
  3037. /**
  3038. * lws_callback_on_writable_all_protocol_vhost() - Request a callback for
  3039. * all connections using the given protocol when it
  3040. * becomes possible to write to each socket without
  3041. * blocking in turn.
  3042. *
  3043. * \param vhost: Only consider connections on this lws_vhost
  3044. * \param protocol: Protocol whose connections will get callbacks
  3045. *
  3046. * - Which: connections using this protocol on GIVEN VHOST ONLY
  3047. * - When: when the individual connection becomes writeable
  3048. * - What: LWS_CALLBACK_*_WRITEABLE
  3049. */
  3050. LWS_VISIBLE LWS_EXTERN int
  3051. lws_callback_on_writable_all_protocol_vhost(const struct lws_vhost *vhost,
  3052. const struct lws_protocols *protocol);
  3053. /**
  3054. * lws_callback_all_protocol() - Callback all connections using
  3055. * the given protocol with the given reason
  3056. *
  3057. * \param context: lws_context
  3058. * \param protocol: Protocol whose connections will get callbacks
  3059. * \param reason: Callback reason index
  3060. *
  3061. * - Which: connections using this protocol on ALL VHOSTS
  3062. * - When: when the individual connection becomes writeable
  3063. * - What: reason
  3064. */
  3065. LWS_VISIBLE LWS_EXTERN int
  3066. lws_callback_all_protocol(struct lws_context *context,
  3067. const struct lws_protocols *protocol, int reason);
  3068. /**
  3069. * lws_callback_all_protocol_vhost() - Callback all connections using
  3070. * the given protocol with the given reason
  3071. *
  3072. * \param vh: Vhost whose connections will get callbacks
  3073. * \param protocol: Which protocol to match
  3074. * \param reason: Callback reason index
  3075. *
  3076. * - Which: connections using this protocol on GIVEN VHOST ONLY
  3077. * - When: now
  3078. * - What: reason
  3079. */
  3080. LWS_VISIBLE LWS_EXTERN int
  3081. lws_callback_all_protocol_vhost(struct lws_vhost *vh,
  3082. const struct lws_protocols *protocol, int reason);
  3083. /**
  3084. * lws_callback_vhost_protocols() - Callback all protocols enabled on a vhost
  3085. * with the given reason
  3086. *
  3087. * \param wsi: wsi whose vhost will get callbacks
  3088. * \param reason: Callback reason index
  3089. * \param in: in argument to callback
  3090. * \param len: len argument to callback
  3091. *
  3092. * - Which: connections using this protocol on same VHOST as wsi ONLY
  3093. * - When: now
  3094. * - What: reason
  3095. */
  3096. LWS_VISIBLE LWS_EXTERN int
  3097. lws_callback_vhost_protocols(struct lws *wsi, int reason, void *in, int len);
  3098. LWS_VISIBLE LWS_EXTERN int
  3099. lws_callback_http_dummy(struct lws *wsi, enum lws_callback_reasons reason,
  3100. void *user, void *in, size_t len);
  3101. /**
  3102. * lws_get_socket_fd() - returns the socket file descriptor
  3103. *
  3104. * You will not need this unless you are doing something special
  3105. *
  3106. * \param wsi: Websocket connection instance
  3107. */
  3108. LWS_VISIBLE LWS_EXTERN int
  3109. lws_get_socket_fd(struct lws *wsi);
  3110. /**
  3111. * lws_get_peer_write_allowance() - get the amount of data writeable to peer
  3112. * if known
  3113. *
  3114. * \param wsi: Websocket connection instance
  3115. *
  3116. * if the protocol does not have any guidance, returns -1. Currently only
  3117. * http2 connections get send window information from this API. But your code
  3118. * should use it so it can work properly with any protocol.
  3119. *
  3120. * If nonzero return is the amount of payload data the peer or intermediary has
  3121. * reported it has buffer space for. That has NO relationship with the amount
  3122. * of buffer space your OS can accept on this connection for a write action.
  3123. *
  3124. * This number represents the maximum you could send to the peer or intermediary
  3125. * on this connection right now without the protocol complaining.
  3126. *
  3127. * lws manages accounting for send window updates and payload writes
  3128. * automatically, so this number reflects the situation at the peer or
  3129. * intermediary dynamically.
  3130. */
  3131. LWS_VISIBLE LWS_EXTERN size_t
  3132. lws_get_peer_write_allowance(struct lws *wsi);
  3133. ///@}
  3134. /**
  3135. * lws_rx_flow_control() - Enable and disable socket servicing for
  3136. * received packets.
  3137. *
  3138. * If the output side of a server process becomes choked, this allows flow
  3139. * control for the input side.
  3140. *
  3141. * \param wsi: Websocket connection instance to get callback for
  3142. * \param enable: 0 = disable read servicing for this connection, 1 = enable
  3143. */
  3144. LWS_VISIBLE LWS_EXTERN int
  3145. lws_rx_flow_control(struct lws *wsi, int enable);
  3146. /**
  3147. * lws_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive
  3148. *
  3149. * When the user server code realizes it can accept more input, it can
  3150. * call this to have the RX flow restriction removed from all connections using
  3151. * the given protocol.
  3152. * \param context: lws_context
  3153. * \param protocol: all connections using this protocol will be allowed to receive
  3154. */
  3155. LWS_VISIBLE LWS_EXTERN void
  3156. lws_rx_flow_allow_all_protocol(const struct lws_context *context,
  3157. const struct lws_protocols *protocol);
  3158. /**
  3159. * lws_remaining_packet_payload() - Bytes to come before "overall"
  3160. * rx packet is complete
  3161. * \param wsi: Websocket instance (available from user callback)
  3162. *
  3163. * This function is intended to be called from the callback if the
  3164. * user code is interested in "complete packets" from the client.
  3165. * libwebsockets just passes through payload as it comes and issues a buffer
  3166. * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
  3167. * callback handler can use this API to find out if the buffer it has just
  3168. * been given is the last piece of a "complete packet" from the client --
  3169. * when that is the case lws_remaining_packet_payload() will return
  3170. * 0.
  3171. *
  3172. * Many protocols won't care becuse their packets are always small.
  3173. */
  3174. LWS_VISIBLE LWS_EXTERN size_t
  3175. lws_remaining_packet_payload(struct lws *wsi);
  3176. /** \defgroup sock-adopt Socket adoption helpers
  3177. * ##Socket adoption helpers
  3178. *
  3179. * When integrating with an external app with its own event loop, these can
  3180. * be used to accept connections from someone else's listening socket.
  3181. *
  3182. * When using lws own event loop, these are not needed.
  3183. */
  3184. ///@{
  3185. /**
  3186. * lws_adopt_socket() - adopt foreign socket as if listen socket accepted it
  3187. * \param context: lws context
  3188. * \param accept_fd: fd of already-accepted socket to adopt
  3189. *
  3190. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  3191. * returns NULL, having cleaned up any new wsi pieces.
  3192. *
  3193. * LWS adopts the socket in http serving mode, it's ready to accept an upgrade
  3194. * to ws or just serve http.
  3195. */
  3196. LWS_VISIBLE LWS_EXTERN struct lws *
  3197. lws_adopt_socket(struct lws_context *context, lws_sockfd_type accept_fd);
  3198. /**
  3199. * lws_adopt_socket_readbuf() - adopt foreign socket and first rx as if listen socket accepted it
  3200. * \param context: lws context
  3201. * \param accept_fd: fd of already-accepted socket to adopt
  3202. * \param readbuf: NULL or pointer to data that must be drained before reading from
  3203. * accept_fd
  3204. * \param len: The length of the data held at \param readbuf
  3205. *
  3206. * Either returns new wsi bound to accept_fd, or closes accept_fd and
  3207. * returns NULL, having cleaned up any new wsi pieces.
  3208. *
  3209. * LWS adopts the socket in http serving mode, it's ready to accept an upgrade
  3210. * to ws or just serve http.
  3211. *
  3212. * If your external code did not already read from the socket, you can use
  3213. * lws_adopt_socket() instead.
  3214. *
  3215. * This api is guaranteed to use the data at \param readbuf first, before reading from
  3216. * the socket.
  3217. *
  3218. * readbuf is limited to the size of the ah rx buf, currently 2048 bytes.
  3219. */
  3220. LWS_VISIBLE LWS_EXTERN struct lws *
  3221. lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
  3222. const char *readbuf, size_t len);
  3223. ///@}
  3224. /** \defgroup net Network related helper APIs
  3225. * ##Network related helper APIs
  3226. *
  3227. * These wrap miscellaneous useful network-related functions
  3228. */
  3229. ///@{
  3230. /**
  3231. * lws_canonical_hostname() - returns this host's hostname
  3232. *
  3233. * This is typically used by client code to fill in the host parameter
  3234. * when making a client connection. You can only call it after the context
  3235. * has been created.
  3236. *
  3237. * \param context: Websocket context
  3238. */
  3239. LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
  3240. lws_canonical_hostname(struct lws_context *context);
  3241. /**
  3242. * lws_get_peer_addresses() - Get client address information
  3243. * \param wsi: Local struct lws associated with
  3244. * \param fd: Connection socket descriptor
  3245. * \param name: Buffer to take client address name
  3246. * \param name_len: Length of client address name buffer
  3247. * \param rip: Buffer to take client address IP dotted quad
  3248. * \param rip_len: Length of client address IP buffer
  3249. *
  3250. * This function fills in name and rip with the name and IP of
  3251. * the client connected with socket descriptor fd. Names may be
  3252. * truncated if there is not enough room. If either cannot be
  3253. * determined, they will be returned as valid zero-length strings.
  3254. */
  3255. LWS_VISIBLE LWS_EXTERN void
  3256. lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
  3257. int name_len, char *rip, int rip_len);
  3258. /**
  3259. * lws_get_peer_simple() - Get client address information without RDNS
  3260. *
  3261. * \param wsi: Local struct lws associated with
  3262. * \param name: Buffer to take client address name
  3263. * \param namelen: Length of client address name buffer
  3264. *
  3265. * This provides a 123.123.123.123 type IP address in name from the
  3266. * peer that has connected to wsi
  3267. */
  3268. LWS_VISIBLE LWS_EXTERN const char *
  3269. lws_get_peer_simple(struct lws *wsi, char *name, int namelen);
  3270. #ifndef LWS_WITH_ESP8266
  3271. /**
  3272. * lws_interface_to_sa() - Convert interface name or IP to sockaddr struct
  3273. *
  3274. * \param ipv6: Allow IPV6 addresses
  3275. * \param ifname: Interface name or IP
  3276. * \param addr: struct sockaddr_in * to be written
  3277. * \param addrlen: Length of addr
  3278. *
  3279. * This converts a textual network interface name to a sockaddr usable by
  3280. * other network functions
  3281. */
  3282. LWS_VISIBLE LWS_EXTERN int
  3283. lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
  3284. size_t addrlen);
  3285. ///@}
  3286. #endif
  3287. /** \defgroup misc Miscellaneous APIs
  3288. * ##Miscellaneous APIs
  3289. *
  3290. * Various APIs outside of other categories
  3291. */
  3292. ///@{
  3293. /**
  3294. * lws_snprintf(): snprintf that truncates the returned length too
  3295. *
  3296. * \param str: destination buffer
  3297. * \param size: bytes left in destination buffer
  3298. * \param format: format string
  3299. * \param ...: args for format
  3300. *
  3301. * This lets you correctly truncate buffers by concatenating lengths, if you
  3302. * reach the limit the reported length doesn't exceed the limit.
  3303. */
  3304. LWS_VISIBLE LWS_EXTERN int
  3305. lws_snprintf(char *str, size_t size, const char *format, ...);
  3306. /**
  3307. * lws_get_random(): fill a buffer with platform random data
  3308. *
  3309. * \param context: the lws context
  3310. * \param buf: buffer to fill
  3311. * \param len: how much to fill
  3312. *
  3313. * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
  3314. * it's interested to see if the frame it's dealing with was sent in binary
  3315. * mode.
  3316. */
  3317. LWS_VISIBLE LWS_EXTERN int
  3318. lws_get_random(struct lws_context *context, void *buf, int len);
  3319. /**
  3320. * lws_daemonize(): fill a buffer with platform random data
  3321. *
  3322. * \param _lock_path: the filepath to write the lock file
  3323. *
  3324. * Spawn lws as a background process, taking care of various things
  3325. */
  3326. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3327. lws_daemonize(const char *_lock_path);
  3328. /**
  3329. * lws_get_library_version(): return string describing the version of lws
  3330. *
  3331. * On unix, also includes the git describe
  3332. */
  3333. LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
  3334. lws_get_library_version(void);
  3335. /**
  3336. * lws_wsi_user() - get the user data associated with the connection
  3337. * \param wsi: lws connection
  3338. *
  3339. * Not normally needed since it's passed into the callback
  3340. */
  3341. LWS_VISIBLE LWS_EXTERN void *
  3342. lws_wsi_user(struct lws *wsi);
  3343. /**
  3344. * lws_parse_uri: cut up prot:/ads:port/path into pieces
  3345. * Notice it does so by dropping '\0' into input string
  3346. * and the leading / on the path is consequently lost
  3347. *
  3348. * \param p: incoming uri string.. will get written to
  3349. * \param prot: result pointer for protocol part (https://)
  3350. * \param ads: result pointer for address part
  3351. * \param port: result pointer for port part
  3352. * \param path: result pointer for path part
  3353. */
  3354. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3355. lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
  3356. const char **path);
  3357. /**
  3358. * lws_now_secs(): return seconds since 1970-1-1
  3359. */
  3360. LWS_VISIBLE LWS_EXTERN unsigned long
  3361. lws_now_secs(void);
  3362. /**
  3363. * lws_get_context - Allow geting lws_context from a Websocket connection
  3364. * instance
  3365. *
  3366. * With this function, users can access context in the callback function.
  3367. * Otherwise users may have to declare context as a global variable.
  3368. *
  3369. * \param wsi: Websocket connection instance
  3370. */
  3371. LWS_VISIBLE LWS_EXTERN struct lws_context * LWS_WARN_UNUSED_RESULT
  3372. lws_get_context(const struct lws *wsi);
  3373. /**
  3374. * lws_get_count_threads(): how many service threads the context uses
  3375. *
  3376. * \param context: the lws context
  3377. *
  3378. * By default this is always 1, if you asked for more than lws can handle it
  3379. * will clip the number of threads. So you can use this to find out how many
  3380. * threads are actually in use.
  3381. */
  3382. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3383. lws_get_count_threads(struct lws_context *context);
  3384. /**
  3385. * lws_get_parent() - get parent wsi or NULL
  3386. * \param wsi: lws connection
  3387. *
  3388. * Specialized wsi like cgi stdin/out/err are associated to a parent wsi,
  3389. * this allows you to get their parent.
  3390. */
  3391. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  3392. lws_get_parent(const struct lws *wsi);
  3393. /**
  3394. * lws_get_child() - get child wsi or NULL
  3395. * \param wsi: lws connection
  3396. *
  3397. * Allows you to find a related wsi from the parent wsi.
  3398. */
  3399. LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  3400. lws_get_child(const struct lws *wsi);
  3401. /*
  3402. * \deprecated DEPRECATED Note: this is not normally needed as a user api.
  3403. * It's provided in case it is
  3404. * useful when integrating with other app poll loop service code.
  3405. */
  3406. LWS_VISIBLE LWS_EXTERN int
  3407. lws_read(struct lws *wsi, unsigned char *buf, size_t len);
  3408. /**
  3409. * lws_set_allocator() - custom allocator support
  3410. *
  3411. * \param realloc
  3412. *
  3413. * Allows you to replace the allocator (and deallocator) used by lws
  3414. */
  3415. LWS_VISIBLE LWS_EXTERN void
  3416. lws_set_allocator(void *(*realloc)(void *ptr, size_t size));
  3417. ///@}
  3418. /** \defgroup wsstatus Websocket status APIs
  3419. * ##Websocket connection status APIs
  3420. *
  3421. * These provide information about ws connection or message status
  3422. */
  3423. ///@{
  3424. /**
  3425. * lws_send_pipe_choked() - tests if socket is writable or not
  3426. * \param wsi: lws connection
  3427. *
  3428. * Allows you to check if you can write more on the socket
  3429. */
  3430. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3431. lws_send_pipe_choked(struct lws *wsi);
  3432. /**
  3433. * lws_is_final_fragment() - tests if last part of ws message
  3434. * \param wsi: lws connection
  3435. */
  3436. LWS_VISIBLE LWS_EXTERN int
  3437. lws_is_final_fragment(struct lws *wsi);
  3438. /**
  3439. * lws_get_reserved_bits() - access reserved bits of ws frame
  3440. * \param wsi: lws connection
  3441. */
  3442. LWS_VISIBLE LWS_EXTERN unsigned char
  3443. lws_get_reserved_bits(struct lws *wsi);
  3444. /**
  3445. * lws_partial_buffered() - find out if lws buffered the last write
  3446. * \param wsi: websocket connection to check
  3447. *
  3448. * Returns 1 if you cannot use lws_write because the last
  3449. * write on this connection is still buffered, and can't be cleared without
  3450. * returning to the service loop and waiting for the connection to be
  3451. * writeable again.
  3452. *
  3453. * If you will try to do >1 lws_write call inside a single
  3454. * WRITEABLE callback, you must check this after every write and bail if
  3455. * set, ask for a new writeable callback and continue writing from there.
  3456. *
  3457. * This is never set at the start of a writeable callback, but any write
  3458. * may set it.
  3459. */
  3460. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3461. lws_partial_buffered(struct lws *wsi);
  3462. /**
  3463. * lws_frame_is_binary(): true if the current frame was sent in binary mode
  3464. *
  3465. * \param wsi: the connection we are inquiring about
  3466. *
  3467. * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
  3468. * it's interested to see if the frame it's dealing with was sent in binary
  3469. * mode.
  3470. */
  3471. LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  3472. lws_frame_is_binary(struct lws *wsi);
  3473. /**
  3474. * lws_is_ssl() - Find out if connection is using SSL
  3475. * \param wsi: websocket connection to check
  3476. *
  3477. * Returns 0 if the connection is not using SSL, 1 if using SSL and
  3478. * using verified cert, and 2 if using SSL but the cert was not
  3479. * checked (appears for client wsi told to skip check on connection)
  3480. */
  3481. LWS_VISIBLE LWS_EXTERN int
  3482. lws_is_ssl(struct lws *wsi);
  3483. /**
  3484. * lws_is_cgi() - find out if this wsi is running a cgi process
  3485. * \param wsi: lws connection
  3486. */
  3487. LWS_VISIBLE LWS_EXTERN int
  3488. lws_is_cgi(struct lws *wsi);
  3489. ///@}
  3490. /** \defgroup sha SHA and B64 helpers
  3491. * ##SHA and B64 helpers
  3492. *
  3493. * These provide SHA-1 and B64 helper apis
  3494. */
  3495. ///@{
  3496. #ifdef LWS_SHA1_USE_OPENSSL_NAME
  3497. #define lws_SHA1 SHA1
  3498. #else
  3499. /**
  3500. * lws_SHA1(): make a SHA-1 digest of a buffer
  3501. *
  3502. * \param d: incoming buffer
  3503. * \param n: length of incoming buffer
  3504. * \param md: buffer for message digest (must be >= 20 bytes)
  3505. *
  3506. * Reduces any size buffer into a 20-byte SHA-1 hash.
  3507. */
  3508. LWS_VISIBLE LWS_EXTERN unsigned char *
  3509. lws_SHA1(const unsigned char *d, size_t n, unsigned char *md);
  3510. #endif
  3511. /**
  3512. * lws_b64_encode_string(): encode a string into base 64
  3513. *
  3514. * \param in: incoming buffer
  3515. * \param in_len: length of incoming buffer
  3516. * \param out: result buffer
  3517. * \param out_size: length of result buffer
  3518. *
  3519. * Encodes a string using b64
  3520. */
  3521. LWS_VISIBLE LWS_EXTERN int
  3522. lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
  3523. /**
  3524. * lws_b64_decode_string(): decode a string from base 64
  3525. *
  3526. * \param in: incoming buffer
  3527. * \param out: result buffer
  3528. * \param out_size: length of result buffer
  3529. *
  3530. * Decodes a string using b64
  3531. */
  3532. LWS_VISIBLE LWS_EXTERN int
  3533. lws_b64_decode_string(const char *in, char *out, int out_size);
  3534. ///@}
  3535. /*! \defgroup cgi cgi handling
  3536. *
  3537. * ##CGI handling
  3538. *
  3539. * These functions allow low-level control over stdin/out/err of the cgi.
  3540. *
  3541. * However for most cases, binding the cgi to http in and out, the default
  3542. * lws implementation already does the right thing.
  3543. */
  3544. #ifdef LWS_WITH_CGI
  3545. enum lws_enum_stdinouterr {
  3546. LWS_STDIN = 0,
  3547. LWS_STDOUT = 1,
  3548. LWS_STDERR = 2,
  3549. };
  3550. enum lws_cgi_hdr_state {
  3551. LCHS_HEADER,
  3552. LCHS_CR1,
  3553. LCHS_LF1,
  3554. LCHS_CR2,
  3555. LCHS_LF2,
  3556. LHCS_PAYLOAD,
  3557. LCHS_SINGLE_0A,
  3558. };
  3559. struct lws_cgi_args {
  3560. struct lws **stdwsi; /**< get fd with lws_get_socket_fd() */
  3561. enum lws_enum_stdinouterr ch; /**< channel index */
  3562. unsigned char *data; /**< for messages with payload */
  3563. enum lws_cgi_hdr_state hdr_state; /**< track where we are in cgi headers */
  3564. int len; /**< length */
  3565. };
  3566. /**
  3567. * lws_cgi: spawn network-connected cgi process
  3568. *
  3569. * \param wsi: connection to own the process
  3570. * \param exec_array: array of "exec-name" "arg1" ... "argn" NULL
  3571. * \param script_uri_path_len: how many chars on the left of the uri are the path to the cgi
  3572. * \param timeout_secs: seconds script should be allowed to run
  3573. * \param mp_cgienv: pvo list with per-vhost cgi options to put in env
  3574. */
  3575. LWS_VISIBLE LWS_EXTERN int
  3576. lws_cgi(struct lws *wsi, const char * const *exec_array,
  3577. int script_uri_path_len, int timeout_secs,
  3578. const struct lws_protocol_vhost_options *mp_cgienv);
  3579. /**
  3580. * lws_cgi_write_split_stdout_headers: write cgi output accounting for header part
  3581. *
  3582. * \param wsi: connection to own the process
  3583. */
  3584. LWS_VISIBLE LWS_EXTERN int
  3585. lws_cgi_write_split_stdout_headers(struct lws *wsi);
  3586. /**
  3587. * lws_cgi_kill: terminate cgi process associated with wsi
  3588. *
  3589. * \param wsi: connection to own the process
  3590. */
  3591. LWS_VISIBLE LWS_EXTERN int
  3592. lws_cgi_kill(struct lws *wsi);
  3593. #endif
  3594. ///@}
  3595. /*! \defgroup fops file operation wrapping
  3596. *
  3597. * ##File operation wrapping
  3598. *
  3599. * Use these helper functions if you want to access a file from the perspective
  3600. * of a specific wsi, which is usually the case. If you just want contextless
  3601. * file access, use the fops callbacks directly with NULL wsi instead of these
  3602. * helpers.
  3603. *
  3604. * If so, then it calls the platform handler or user overrides where present
  3605. * (as defined in info->fops)
  3606. *
  3607. * The advantage from all this is user code can be portable for file operations
  3608. * without having to deal with differences between platforms.
  3609. */
  3610. //@{
  3611. /** struct lws_plat_file_ops - Platform-specific file operations
  3612. *
  3613. * These provide platform-agnostic ways to deal with filesystem access in the
  3614. * library and in the user code.
  3615. */
  3616. struct lws_plat_file_ops {
  3617. lws_filefd_type (*open)(struct lws *wsi, const char *filename,
  3618. unsigned long *filelen, int flags);
  3619. /**< Open file (always binary access if plat supports it)
  3620. * filelen is filled on exit to be the length of the file
  3621. * flags should be set to O_RDONLY or O_RDWR */
  3622. int (*close)(struct lws *wsi, lws_filefd_type fd);
  3623. /**< close file */
  3624. unsigned long (*seek_cur)(struct lws *wsi, lws_filefd_type fd,
  3625. long offset_from_cur_pos);
  3626. /**< seek from current position */
  3627. int (*read)(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
  3628. unsigned char *buf, unsigned long len);
  3629. /**< Read from file, on exit *amount is set to amount actually read */
  3630. int (*write)(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
  3631. unsigned char *buf, unsigned long len);
  3632. /**< Write to file, on exit *amount is set to amount actually written */
  3633. /* Add new things just above here ---^
  3634. * This is part of the ABI, don't needlessly break compatibility */
  3635. };
  3636. /**
  3637. * lws_get_fops() - get current file ops
  3638. *
  3639. * \param context: context
  3640. */
  3641. LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops * LWS_WARN_UNUSED_RESULT
  3642. lws_get_fops(struct lws_context *context);
  3643. /**
  3644. * lws_plat_file_open() - file open operations
  3645. *
  3646. * \param wsi: connection doing the opening
  3647. * \param filename: filename to open
  3648. * \param filelen: length of file (filled in by call)
  3649. * \param flags: open flags
  3650. */
  3651. static LWS_INLINE lws_filefd_type LWS_WARN_UNUSED_RESULT
  3652. lws_plat_file_open(struct lws *wsi, const char *filename,
  3653. unsigned long *filelen, int flags)
  3654. {
  3655. return lws_get_fops(lws_get_context(wsi))->open(wsi, filename,
  3656. filelen, flags);
  3657. }
  3658. /**
  3659. * lws_plat_file_close() - close file
  3660. *
  3661. * \param wsi: connection opened by
  3662. * \param fd: file descriptor
  3663. */
  3664. static LWS_INLINE int
  3665. lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
  3666. {
  3667. return lws_get_fops(lws_get_context(wsi))->close(wsi, fd);
  3668. }
  3669. /**
  3670. * lws_plat_file_seek_cur() - close file
  3671. *
  3672. * \param wsi: connection opened by
  3673. * \param fd: file descriptor
  3674. * \param offset: position to seek to
  3675. */
  3676. static LWS_INLINE unsigned long
  3677. lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
  3678. {
  3679. return lws_get_fops(lws_get_context(wsi))->seek_cur(wsi, fd, offset);
  3680. }
  3681. /**
  3682. * lws_plat_file_read() - read from file
  3683. *
  3684. * \param wsi: connection opened by
  3685. * \param fd: file descriptor
  3686. * \param amount: how much to read (rewritten by call)
  3687. * \param buf: buffer to write to
  3688. * \param len: max length
  3689. */
  3690. static LWS_INLINE int LWS_WARN_UNUSED_RESULT
  3691. lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
  3692. unsigned char *buf, unsigned long len)
  3693. {
  3694. return lws_get_fops(lws_get_context(wsi))->read(wsi, fd, amount, buf,
  3695. len);
  3696. }
  3697. /**
  3698. * lws_plat_file_write() - write from file
  3699. *
  3700. * \param wsi: connection opened by
  3701. * \param fd: file descriptor
  3702. * \param amount: how much to write (rewritten by call)
  3703. * \param buf: buffer to read from
  3704. * \param len: max length
  3705. */
  3706. static LWS_INLINE int LWS_WARN_UNUSED_RESULT
  3707. lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
  3708. unsigned char *buf, unsigned long len)
  3709. {
  3710. return lws_get_fops(lws_get_context(wsi))->write(wsi, fd, amount, buf,
  3711. len);
  3712. }
  3713. //@}
  3714. /** \defgroup smtp
  3715. * \ingroup lwsapi
  3716. * ##SMTP related functions
  3717. *
  3718. * These apis let you communicate with a local SMTP server to send email from
  3719. * lws. It handles all the SMTP sequencing and protocol actions.
  3720. *
  3721. * Your system should have postfix, sendmail or another MTA listening on port
  3722. * 25 and able to send email using the "mail" commandline app. Usually distro
  3723. * MTAs are configured for this by default.
  3724. *
  3725. * It runs via its own libuv events if initialized (which requires giving it
  3726. * a libuv loop to attach to).
  3727. *
  3728. * It operates using three callbacks, on_next() queries if there is a new email
  3729. * to send, on_get_body() asks for the body of the email, and on_sent() is
  3730. * called after the email is successfully sent.
  3731. *
  3732. * To use it
  3733. *
  3734. * - create an lws_email struct
  3735. *
  3736. * - initialize data, loop, the email_* strings, max_content_size and
  3737. * the callbacks
  3738. *
  3739. * - call lws_email_init()
  3740. *
  3741. * When you have at least one email to send, call lws_email_check() to
  3742. * schedule starting to send it.
  3743. */
  3744. //@{
  3745. #ifdef LWS_WITH_SMTP
  3746. /** enum lwsgs_smtp_states - where we are in SMTP protocol sequence */
  3747. enum lwsgs_smtp_states {
  3748. LGSSMTP_IDLE, /**< awaiting new email */
  3749. LGSSMTP_CONNECTING, /**< opening tcp connection to MTA */
  3750. LGSSMTP_CONNECTED, /**< tcp connection to MTA is connected */
  3751. LGSSMTP_SENT_HELO, /**< sent the HELO */
  3752. LGSSMTP_SENT_FROM, /**< sent FROM */
  3753. LGSSMTP_SENT_TO, /**< sent TO */
  3754. LGSSMTP_SENT_DATA, /**< sent DATA request */
  3755. LGSSMTP_SENT_BODY, /**< sent the email body */
  3756. LGSSMTP_SENT_QUIT, /**< sent the session quit */
  3757. };
  3758. /** struct lws_email - abstract context for performing SMTP operations */
  3759. struct lws_email {
  3760. void *data;
  3761. /**< opaque pointer set by user code and available to the callbacks */
  3762. uv_loop_t *loop;
  3763. /**< the libuv loop we will work on */
  3764. char email_smtp_ip[32]; /**< Fill before init, eg, "127.0.0.1" */
  3765. char email_helo[32]; /**< Fill before init, eg, "myserver.com" */
  3766. char email_from[100]; /**< Fill before init or on_next */
  3767. char email_to[100]; /**< Fill before init or on_next */
  3768. unsigned int max_content_size;
  3769. /**< largest possible email body size */
  3770. /* Fill all the callbacks before init */
  3771. int (*on_next)(struct lws_email *email);
  3772. /**< (Fill in before calling lws_email_init)
  3773. * called when idle, 0 = another email to send, nonzero is idle.
  3774. * If you return 0, all of the email_* char arrays must be set
  3775. * to something useful. */
  3776. int (*on_sent)(struct lws_email *email);
  3777. /**< (Fill in before calling lws_email_init)
  3778. * called when transfer of the email to the SMTP server was
  3779. * successful, your callback would remove the current email
  3780. * from its queue */
  3781. int (*on_get_body)(struct lws_email *email, char *buf, int len);
  3782. /**< (Fill in before calling lws_email_init)
  3783. * called when the body part of the queued email is about to be
  3784. * sent to the SMTP server. */
  3785. /* private things */
  3786. uv_timer_t timeout_email; /**< private */
  3787. enum lwsgs_smtp_states estate; /**< private */
  3788. uv_connect_t email_connect_req; /**< private */
  3789. uv_tcp_t email_client; /**< private */
  3790. time_t email_connect_started; /**< private */
  3791. char email_buf[256]; /**< private */
  3792. char *content; /**< private */
  3793. };
  3794. /**
  3795. * lws_email_init() - Initialize a struct lws_email
  3796. *
  3797. * \param email: struct lws_email to init
  3798. * \param loop: libuv loop to use
  3799. * \param max_content: max email content size
  3800. *
  3801. * Prepares a struct lws_email for use ending SMTP
  3802. */
  3803. LWS_VISIBLE LWS_EXTERN int
  3804. lws_email_init(struct lws_email *email, uv_loop_t *loop, int max_content);
  3805. /**
  3806. * lws_email_check() - Request check for new email
  3807. *
  3808. * \param email: struct lws_email context to check
  3809. *
  3810. * Schedules a check for new emails in 1s... call this when you have queued an
  3811. * email for send.
  3812. */
  3813. LWS_VISIBLE LWS_EXTERN void
  3814. lws_email_check(struct lws_email *email);
  3815. /**
  3816. * lws_email_destroy() - stop using the struct lws_email
  3817. *
  3818. * \param email: the struct lws_email context
  3819. *
  3820. * Stop sending email using email and free allocations
  3821. */
  3822. LWS_VISIBLE LWS_EXTERN void
  3823. lws_email_destroy(struct lws_email *email);
  3824. #endif
  3825. //@}
  3826. #ifdef __cplusplus
  3827. }
  3828. #endif
  3829. #endif