libwebsockets.h 163 KB

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