mongoose.h 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933
  1. // Copyright (c) 2004-2013 Sergey Lyubka
  2. // Copyright (c) 2013-2024 Cesanta Software Limited
  3. // All rights reserved
  4. //
  5. // This software is dual-licensed: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License version 2 as
  7. // published by the Free Software Foundation. For the terms of this
  8. // license, see http://www.gnu.org/licenses/
  9. //
  10. // You are free to use this software under the terms of the GNU General
  11. // Public License, but WITHOUT ANY WARRANTY; without even the implied
  12. // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. // See the GNU General Public License for more details.
  14. //
  15. // Alternatively, you can license this software under a commercial
  16. // license, as set out in https://www.mongoose.ws/licensing/
  17. //
  18. // SPDX-License-Identifier: GPL-2.0-only or commercial
  19. #ifndef MONGOOSE_H
  20. #define MONGOOSE_H
  21. #define MG_VERSION "7.12"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define MG_ARCH_CUSTOM 0 // User creates its own mongoose_custom.h
  26. #define MG_ARCH_UNIX 1 // Linux, BSD, Mac, ...
  27. #define MG_ARCH_WIN32 2 // Windows
  28. #define MG_ARCH_ESP32 3 // ESP32
  29. #define MG_ARCH_ESP8266 4 // ESP8266
  30. #define MG_ARCH_FREERTOS 5 // FreeRTOS
  31. #define MG_ARCH_AZURERTOS 6 // MS Azure RTOS
  32. #define MG_ARCH_ZEPHYR 7 // Zephyr RTOS
  33. #define MG_ARCH_NEWLIB 8 // Bare metal ARM
  34. #define MG_ARCH_CMSIS_RTOS1 9 // CMSIS-RTOS API v1 (Keil RTX)
  35. #define MG_ARCH_TIRTOS 10 // Texas Semi TI-RTOS
  36. #define MG_ARCH_RP2040 11 // Raspberry Pi RP2040
  37. #define MG_ARCH_ARMCC 12 // Keil MDK-Core with Configuration Wizard
  38. #define MG_ARCH_CMSIS_RTOS2 13 // CMSIS-RTOS API v2 (Keil RTX5, FreeRTOS)
  39. #define MG_ARCH_RTTHREAD 14 // RT-Thread RTOS
  40. #if !defined(MG_ARCH)
  41. #if defined(__unix__) || defined(__APPLE__)
  42. #define MG_ARCH MG_ARCH_UNIX
  43. #elif defined(_WIN32)
  44. #define MG_ARCH MG_ARCH_WIN32
  45. #elif defined(ICACHE_FLASH) || defined(ICACHE_RAM_ATTR)
  46. #define MG_ARCH MG_ARCH_ESP8266
  47. #elif defined(__ZEPHYR__)
  48. #define MG_ARCH MG_ARCH_ZEPHYR
  49. #elif defined(ESP_PLATFORM)
  50. #define MG_ARCH MG_ARCH_ESP32
  51. #elif defined(FREERTOS_IP_H)
  52. #define MG_ARCH MG_ARCH_FREERTOS
  53. #define MG_ENABLE_FREERTOS_TCP 1
  54. #elif defined(AZURE_RTOS_THREADX)
  55. #define MG_ARCH MG_ARCH_AZURERTOS
  56. #elif defined(PICO_TARGET_NAME)
  57. #define MG_ARCH MG_ARCH_RP2040
  58. #elif defined(__ARMCC_VERSION)
  59. #define MG_ARCH MG_ARCH_ARMCC
  60. #elif defined(__RTTHREAD__)
  61. #define MG_ARCH MG_ARCH_RTTHREAD
  62. #endif
  63. #endif // !defined(MG_ARCH)
  64. // if the user did not specify an MG_ARCH, or specified a custom one, OR
  65. // we guessed a known IDE, pull the customized config (Configuration Wizard)
  66. #if !defined(MG_ARCH) || (MG_ARCH == MG_ARCH_CUSTOM) || MG_ARCH == MG_ARCH_ARMCC
  67. #include "mongoose_custom.h" // keep this include
  68. #endif
  69. #if !defined(MG_ARCH)
  70. #error "MG_ARCH is not specified and we couldn't guess it. Set -D MG_ARCH=..."
  71. #endif
  72. // http://esr.ibiblio.org/?p=5095
  73. #define MG_BIG_ENDIAN (*(uint16_t *) "\0\xff" < 0x100)
  74. #if MG_ARCH == MG_ARCH_AZURERTOS
  75. #include <stdarg.h>
  76. #include <stdbool.h>
  77. #include <stdint.h>
  78. #include <stdio.h>
  79. #include <time.h>
  80. #include <fx_api.h>
  81. #include <tx_api.h>
  82. #include <nx_api.h>
  83. #include <nx_bsd.h>
  84. #include <nx_port.h>
  85. #include <tx_port.h>
  86. #define PATH_MAX FX_MAXIMUM_PATH
  87. #define MG_DIRSEP '\\'
  88. #define socklen_t int
  89. #define closesocket(x) soc_close(x)
  90. #undef FOPEN_MAX
  91. #endif
  92. #if MG_ARCH == MG_ARCH_ESP32
  93. #include <ctype.h>
  94. #include <dirent.h>
  95. #include <errno.h>
  96. #include <fcntl.h>
  97. #include <limits.h>
  98. #include <netdb.h>
  99. #include <stdarg.h>
  100. #include <stddef.h>
  101. #include <stdio.h>
  102. #include <stdlib.h>
  103. #include <string.h>
  104. #include <sys/stat.h>
  105. #include <sys/types.h>
  106. #include <time.h>
  107. #include <esp_timer.h>
  108. #define MG_PATH_MAX 128
  109. #endif
  110. #if MG_ARCH == MG_ARCH_ESP8266
  111. #include <ctype.h>
  112. #include <dirent.h>
  113. #include <errno.h>
  114. #include <fcntl.h>
  115. #include <limits.h>
  116. #include <netdb.h>
  117. #include <stdarg.h>
  118. #include <stdbool.h>
  119. #include <stddef.h>
  120. #include <stdio.h>
  121. #include <stdlib.h>
  122. #include <string.h>
  123. #include <sys/stat.h>
  124. #include <sys/time.h>
  125. #include <sys/types.h>
  126. #include <time.h>
  127. #include <esp_system.h>
  128. #define MG_PATH_MAX 128
  129. #endif
  130. #if MG_ARCH == MG_ARCH_FREERTOS
  131. #include <ctype.h>
  132. #if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP
  133. #include <errno.h>
  134. #endif
  135. #include <stdarg.h>
  136. #include <stdbool.h>
  137. #include <stddef.h>
  138. #include <stdint.h>
  139. #include <stdio.h>
  140. #include <stdlib.h> // rand(), strtol(), atoi()
  141. #include <string.h>
  142. #if defined(__ARMCC_VERSION)
  143. #define mode_t size_t
  144. #include <time.h>
  145. #else
  146. #include <sys/stat.h>
  147. #endif
  148. #include <FreeRTOS.h>
  149. #include <task.h>
  150. #ifndef MG_IO_SIZE
  151. #define MG_IO_SIZE 512
  152. #endif
  153. #define calloc(a, b) mg_calloc(a, b)
  154. #define free(a) vPortFree(a)
  155. #define malloc(a) pvPortMalloc(a)
  156. #define strdup(s) ((char *) mg_strdup(mg_str(s)).ptr)
  157. // Re-route calloc/free to the FreeRTOS's functions, don't use stdlib
  158. static inline void *mg_calloc(size_t cnt, size_t size) {
  159. void *p = pvPortMalloc(cnt * size);
  160. if (p != NULL) memset(p, 0, size * cnt);
  161. return p;
  162. }
  163. #define mkdir(a, b) mg_mkdir(a, b)
  164. static inline int mg_mkdir(const char *path, mode_t mode) {
  165. (void) path, (void) mode;
  166. return -1;
  167. }
  168. #endif // MG_ARCH == MG_ARCH_FREERTOS
  169. #if MG_ARCH == MG_ARCH_NEWLIB
  170. #define _POSIX_TIMERS
  171. #include <ctype.h>
  172. #include <errno.h>
  173. #include <stdarg.h>
  174. #include <stdbool.h>
  175. #include <stdio.h>
  176. #include <stdlib.h>
  177. #include <string.h>
  178. #include <sys/stat.h>
  179. #include <sys/time.h>
  180. #include <sys/types.h>
  181. #include <time.h>
  182. #include <unistd.h>
  183. #define MG_PATH_MAX 100
  184. #define MG_ENABLE_SOCKET 0
  185. #define MG_ENABLE_DIRLIST 0
  186. #endif
  187. #if MG_ARCH == MG_ARCH_RP2040
  188. #include <errno.h>
  189. #include <stdarg.h>
  190. #include <stdbool.h>
  191. #include <stdint.h>
  192. #include <stdio.h>
  193. #include <stdlib.h>
  194. #include <string.h>
  195. #include <time.h>
  196. #include <pico/stdlib.h>
  197. int mkdir(const char *, mode_t);
  198. #endif
  199. #if MG_ARCH == MG_ARCH_RTTHREAD
  200. #include <rtthread.h>
  201. #include <ctype.h>
  202. #include <errno.h>
  203. #include <fcntl.h>
  204. #include <sys/socket.h>
  205. #include <sys/select.h>
  206. #include <stdarg.h>
  207. #include <stdbool.h>
  208. #include <stdint.h>
  209. #include <stdio.h>
  210. #include <stdlib.h>
  211. #include <string.h>
  212. #include <sys/types.h>
  213. #include <time.h>
  214. #ifndef MG_IO_SIZE
  215. #define MG_IO_SIZE 1460
  216. #endif
  217. #endif // MG_ARCH == MG_ARCH_RTTHREAD
  218. #if MG_ARCH == MG_ARCH_ARMCC || MG_ARCH == MG_ARCH_CMSIS_RTOS1 || \
  219. MG_ARCH == MG_ARCH_CMSIS_RTOS2
  220. #include <ctype.h>
  221. #include <errno.h>
  222. #include <stdarg.h>
  223. #include <stdbool.h>
  224. #include <stddef.h>
  225. #include <stdint.h>
  226. #include <stdio.h>
  227. #include <stdlib.h>
  228. #include <string.h>
  229. #include <time.h>
  230. #if MG_ARCH == MG_ARCH_CMSIS_RTOS1
  231. #include "cmsis_os.h" // keep this include
  232. // https://developer.arm.com/documentation/ka003821/latest
  233. extern uint32_t rt_time_get(void);
  234. #elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
  235. #include "cmsis_os2.h" // keep this include
  236. #endif
  237. #define strdup(s) ((char *) mg_strdup(mg_str(s)).ptr)
  238. #if defined(__ARMCC_VERSION)
  239. #define mode_t size_t
  240. #define mkdir(a, b) mg_mkdir(a, b)
  241. static inline int mg_mkdir(const char *path, mode_t mode) {
  242. (void) path, (void) mode;
  243. return -1;
  244. }
  245. #endif
  246. #if (MG_ARCH == MG_ARCH_CMSIS_RTOS1 || MG_ARCH == MG_ARCH_CMSIS_RTOS2) && \
  247. !defined MG_ENABLE_RL && (!defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP) && \
  248. (!defined(MG_ENABLE_TCPIP) || !MG_ENABLE_TCPIP)
  249. #define MG_ENABLE_RL 1
  250. #ifndef MG_SOCK_LISTEN_BACKLOG_SIZE
  251. #define MG_SOCK_LISTEN_BACKLOG_SIZE 3
  252. #endif
  253. #endif
  254. #endif
  255. #if MG_ARCH == MG_ARCH_TIRTOS
  256. #include <stdlib.h>
  257. #include <ctype.h>
  258. #include <stdarg.h>
  259. #include <stdbool.h>
  260. #include <stdint.h>
  261. #include <stdio.h>
  262. #include <string.h>
  263. #include <time.h>
  264. #include <serrno.h>
  265. #include <sys/socket.h>
  266. #include <ti/sysbios/knl/Clock.h>
  267. #endif
  268. #if MG_ARCH == MG_ARCH_UNIX
  269. #define _DARWIN_UNLIMITED_SELECT 1 // No limit on file descriptors
  270. #if defined(__APPLE__)
  271. #include <mach/mach_time.h>
  272. #endif
  273. #if !defined(MG_ENABLE_EPOLL) && defined(__linux__)
  274. #define MG_ENABLE_EPOLL 1
  275. #elif !defined(MG_ENABLE_POLL)
  276. #define MG_ENABLE_POLL 1
  277. #endif
  278. #include <arpa/inet.h>
  279. #include <ctype.h>
  280. #include <dirent.h>
  281. #include <errno.h>
  282. #include <fcntl.h>
  283. #include <inttypes.h>
  284. #include <limits.h>
  285. #include <netdb.h>
  286. #include <netinet/in.h>
  287. #include <netinet/tcp.h>
  288. #include <signal.h>
  289. #include <stdarg.h>
  290. #include <stdbool.h>
  291. #include <stddef.h>
  292. #include <stdint.h>
  293. #include <stdio.h>
  294. #include <stdlib.h>
  295. #include <string.h>
  296. #if defined(MG_ENABLE_EPOLL) && MG_ENABLE_EPOLL
  297. #include <sys/epoll.h>
  298. #elif defined(MG_ENABLE_POLL) && MG_ENABLE_POLL
  299. #include <poll.h>
  300. #else
  301. #include <sys/select.h>
  302. #endif
  303. #include <sys/socket.h>
  304. #include <sys/stat.h>
  305. #include <sys/time.h>
  306. #include <sys/types.h>
  307. #include <time.h>
  308. #include <unistd.h>
  309. #ifndef MG_ENABLE_DIRLIST
  310. #define MG_ENABLE_DIRLIST 1
  311. #endif
  312. #ifndef MG_PATH_MAX
  313. #define MG_PATH_MAX FILENAME_MAX
  314. #endif
  315. #endif
  316. #if MG_ARCH == MG_ARCH_WIN32
  317. #ifndef WIN32_LEAN_AND_MEAN
  318. #define WIN32_LEAN_AND_MEAN
  319. #endif
  320. #ifndef _CRT_SECURE_NO_WARNINGS
  321. #define _CRT_SECURE_NO_WARNINGS
  322. #endif
  323. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  324. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  325. #endif
  326. #include <ctype.h>
  327. #include <direct.h>
  328. #include <errno.h>
  329. #include <fcntl.h>
  330. #include <limits.h>
  331. #include <signal.h>
  332. #include <stdarg.h>
  333. #include <stddef.h>
  334. #include <stdio.h>
  335. #include <stdlib.h>
  336. #include <string.h>
  337. #include <sys/stat.h>
  338. #include <sys/types.h>
  339. #include <time.h>
  340. #if defined(_MSC_VER) && _MSC_VER < 1700
  341. #define __func__ ""
  342. typedef __int64 int64_t;
  343. typedef unsigned __int64 uint64_t;
  344. typedef unsigned char uint8_t;
  345. typedef char int8_t;
  346. typedef unsigned short uint16_t;
  347. typedef short int16_t;
  348. typedef unsigned int uint32_t;
  349. typedef int int32_t;
  350. typedef enum { false = 0, true = 1 } bool;
  351. #else
  352. #include <stdbool.h>
  353. #include <stdint.h>
  354. #include <ws2tcpip.h>
  355. #endif
  356. #include <process.h>
  357. #include <winerror.h>
  358. #include <winsock2.h>
  359. // Protect from calls like std::snprintf in app code
  360. // See https://github.com/cesanta/mongoose/issues/1047
  361. #ifndef __cplusplus
  362. #define snprintf _snprintf
  363. #define vsnprintf _vsnprintf
  364. #ifndef strdup // For MSVC with _DEBUG, see #1359
  365. #define strdup(x) _strdup(x)
  366. #endif
  367. #endif
  368. #define MG_INVALID_SOCKET INVALID_SOCKET
  369. #define MG_SOCKET_TYPE SOCKET
  370. typedef unsigned long nfds_t;
  371. #if defined(_MSC_VER)
  372. #pragma comment(lib, "ws2_32.lib")
  373. #ifndef alloca
  374. #define alloca(a) _alloca(a)
  375. #endif
  376. #endif
  377. #define poll(a, b, c) WSAPoll((a), (b), (c))
  378. #define closesocket(x) closesocket(x)
  379. typedef int socklen_t;
  380. #define MG_DIRSEP '\\'
  381. #ifndef MG_PATH_MAX
  382. #define MG_PATH_MAX FILENAME_MAX
  383. #endif
  384. #ifndef SO_EXCLUSIVEADDRUSE
  385. #define SO_EXCLUSIVEADDRUSE ((int) (~SO_REUSEADDR))
  386. #endif
  387. #define MG_SOCK_ERR(errcode) ((errcode) < 0 ? WSAGetLastError() : 0)
  388. #define MG_SOCK_PENDING(errcode) \
  389. (((errcode) < 0) && \
  390. (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEINPROGRESS || \
  391. WSAGetLastError() == WSAEWOULDBLOCK))
  392. #define MG_SOCK_RESET(errcode) \
  393. (((errcode) < 0) && (WSAGetLastError() == WSAECONNRESET))
  394. #define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
  395. #define sleep(x) Sleep((x) *1000)
  396. #define mkdir(a, b) _mkdir(a)
  397. #define timegm(x) _mkgmtime(x)
  398. #ifndef S_ISDIR
  399. #define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR)
  400. #endif
  401. #ifndef MG_ENABLE_DIRLIST
  402. #define MG_ENABLE_DIRLIST 1
  403. #endif
  404. #ifndef SIGPIPE
  405. #define SIGPIPE 0
  406. #endif
  407. #endif
  408. #if MG_ARCH == MG_ARCH_ZEPHYR
  409. #include <zephyr/kernel.h>
  410. #include <ctype.h>
  411. #include <errno.h>
  412. #include <fcntl.h>
  413. #include <zephyr/net/socket.h>
  414. #include <stdarg.h>
  415. #include <stdbool.h>
  416. #include <stdint.h>
  417. #include <stdio.h>
  418. #include <stdlib.h>
  419. #include <string.h>
  420. #include <sys/types.h>
  421. #include <time.h>
  422. #define MG_PUTCHAR(x) printk("%c", x)
  423. #ifndef strdup
  424. #define strdup(s) ((char *) mg_strdup(mg_str(s)).ptr)
  425. #endif
  426. #define strerror(x) zsock_gai_strerror(x)
  427. #ifndef FD_CLOEXEC
  428. #define FD_CLOEXEC 0
  429. #endif
  430. #ifndef F_SETFD
  431. #define F_SETFD 0
  432. #endif
  433. #define MG_ENABLE_SSI 0
  434. int rand(void);
  435. int sscanf(const char *, const char *, ...);
  436. #endif
  437. #if defined(MG_ENABLE_FREERTOS_TCP) && MG_ENABLE_FREERTOS_TCP
  438. #include <limits.h>
  439. #include <list.h>
  440. #include <FreeRTOS_IP.h>
  441. #include <FreeRTOS_Sockets.h>
  442. #include <FreeRTOS_errno_TCP.h> // contents to be moved and file removed, some day
  443. #define MG_SOCKET_TYPE Socket_t
  444. #define MG_INVALID_SOCKET FREERTOS_INVALID_SOCKET
  445. // Why FreeRTOS-TCP did not implement a clean BSD API, but its own thing
  446. // with FreeRTOS_ prefix, is beyond me
  447. #define IPPROTO_TCP FREERTOS_IPPROTO_TCP
  448. #define IPPROTO_UDP FREERTOS_IPPROTO_UDP
  449. #define AF_INET FREERTOS_AF_INET
  450. #define SOCK_STREAM FREERTOS_SOCK_STREAM
  451. #define SOCK_DGRAM FREERTOS_SOCK_DGRAM
  452. #define SO_BROADCAST 0
  453. #define SO_ERROR 0
  454. #define SOL_SOCKET 0
  455. #define SO_REUSEADDR 0
  456. #define MG_SOCK_ERR(errcode) ((errcode) < 0 ? (errcode) : 0)
  457. #define MG_SOCK_PENDING(errcode) \
  458. ((errcode) == -pdFREERTOS_ERRNO_EWOULDBLOCK || \
  459. (errcode) == -pdFREERTOS_ERRNO_EISCONN || \
  460. (errcode) == -pdFREERTOS_ERRNO_EINPROGRESS || \
  461. (errcode) == -pdFREERTOS_ERRNO_EAGAIN)
  462. #define MG_SOCK_RESET(errcode) ((errcode) == -pdFREERTOS_ERRNO_ENOTCONN)
  463. // actually only if optional timeout is enabled
  464. #define MG_SOCK_INTR(fd) (fd == NULL)
  465. #define sockaddr_in freertos_sockaddr
  466. #define sockaddr freertos_sockaddr
  467. #define accept(a, b, c) FreeRTOS_accept((a), (b), (c))
  468. #define connect(a, b, c) FreeRTOS_connect((a), (b), (c))
  469. #define bind(a, b, c) FreeRTOS_bind((a), (b), (c))
  470. #define listen(a, b) FreeRTOS_listen((a), (b))
  471. #define socket(a, b, c) FreeRTOS_socket((a), (b), (c))
  472. #define send(a, b, c, d) FreeRTOS_send((a), (b), (c), (d))
  473. #define recv(a, b, c, d) FreeRTOS_recv((a), (b), (c), (d))
  474. #define setsockopt(a, b, c, d, e) FreeRTOS_setsockopt((a), (b), (c), (d), (e))
  475. #define sendto(a, b, c, d, e, f) FreeRTOS_sendto((a), (b), (c), (d), (e), (f))
  476. #define recvfrom(a, b, c, d, e, f) \
  477. FreeRTOS_recvfrom((a), (b), (c), (d), (e), (f))
  478. #define closesocket(x) FreeRTOS_closesocket(x)
  479. #define gethostbyname(x) FreeRTOS_gethostbyname(x)
  480. #define getsockname(a, b, c) mg_getsockname((a), (b), (c))
  481. #define getpeername(a, b, c) mg_getpeername((a), (b), (c))
  482. static inline int mg_getsockname(MG_SOCKET_TYPE fd, void *buf, socklen_t *len) {
  483. (void) fd, (void) buf, (void) len;
  484. return -1;
  485. }
  486. static inline int mg_getpeername(MG_SOCKET_TYPE fd, void *buf, socklen_t *len) {
  487. (void) fd, (void) buf, (void) len;
  488. return 0;
  489. }
  490. #endif
  491. #if defined(MG_ENABLE_LWIP) && MG_ENABLE_LWIP
  492. #if defined(__GNUC__) && !defined(__ARMCC_VERSION)
  493. #include <sys/stat.h>
  494. #endif
  495. struct timeval;
  496. #include <lwip/sockets.h>
  497. #if !LWIP_TIMEVAL_PRIVATE
  498. #if defined(__GNUC__) && !defined(__ARMCC_VERSION) // armclang sets both
  499. #include <sys/time.h>
  500. #else
  501. struct timeval {
  502. time_t tv_sec;
  503. long tv_usec;
  504. };
  505. #endif
  506. #endif
  507. #if LWIP_SOCKET != 1
  508. // Sockets support disabled in LWIP by default
  509. #error Set LWIP_SOCKET variable to 1 (in lwipopts.h)
  510. #endif
  511. #endif
  512. #if defined(MG_ENABLE_RL) && MG_ENABLE_RL
  513. #include <rl_net.h>
  514. #define closesocket(x) closesocket(x)
  515. #define TCP_NODELAY SO_KEEPALIVE
  516. #define MG_SOCK_ERR(errcode) ((errcode) < 0 ? (errcode) : 0)
  517. #define MG_SOCK_PENDING(errcode) \
  518. ((errcode) == BSD_EWOULDBLOCK || (errcode) == BSD_EALREADY || \
  519. (errcode) == BSD_EINPROGRESS)
  520. #define MG_SOCK_RESET(errcode) \
  521. ((errcode) == BSD_ECONNABORTED || (errcode) == BSD_ECONNRESET)
  522. // In blocking mode, which is enabled by default, accept() waits for a
  523. // connection request. In non blocking mode, you must call accept()
  524. // again if the error code BSD_EWOULDBLOCK is returned.
  525. #define MG_SOCK_INTR(fd) (fd == BSD_EWOULDBLOCK)
  526. #define socklen_t int
  527. #endif
  528. #ifndef MG_ENABLE_LOG
  529. #define MG_ENABLE_LOG 1
  530. #endif
  531. #ifndef MG_ENABLE_CUSTOM_LOG
  532. #define MG_ENABLE_CUSTOM_LOG 0 // Let user define their own MG_LOG
  533. #endif
  534. #ifndef MG_ENABLE_TCPIP
  535. #define MG_ENABLE_TCPIP 0 // Mongoose built-in network stack
  536. #endif
  537. #ifndef MG_ENABLE_LWIP
  538. #define MG_ENABLE_LWIP 0 // lWIP network stack
  539. #endif
  540. #ifndef MG_ENABLE_FREERTOS_TCP
  541. #define MG_ENABLE_FREERTOS_TCP 0 // Amazon FreeRTOS-TCP network stack
  542. #endif
  543. #ifndef MG_ENABLE_RL
  544. #define MG_ENABLE_RL 0 // ARM MDK network stack
  545. #endif
  546. #ifndef MG_ENABLE_SOCKET
  547. #define MG_ENABLE_SOCKET !MG_ENABLE_TCPIP
  548. #endif
  549. #ifndef MG_ENABLE_POLL
  550. #define MG_ENABLE_POLL 0
  551. #endif
  552. #ifndef MG_ENABLE_EPOLL
  553. #define MG_ENABLE_EPOLL 0
  554. #endif
  555. #ifndef MG_ENABLE_FATFS
  556. #define MG_ENABLE_FATFS 0
  557. #endif
  558. #ifndef MG_ENABLE_SSI
  559. #define MG_ENABLE_SSI 0
  560. #endif
  561. #ifndef MG_ENABLE_IPV6
  562. #define MG_ENABLE_IPV6 0
  563. #endif
  564. #ifndef MG_IPV6_V6ONLY
  565. #define MG_IPV6_V6ONLY 0 // IPv6 socket binds only to V6, not V4 address
  566. #endif
  567. #ifndef MG_ENABLE_MD5
  568. #define MG_ENABLE_MD5 1
  569. #endif
  570. // Set MG_ENABLE_WINSOCK=0 for Win32 builds with external IP stack (like LWIP)
  571. #ifndef MG_ENABLE_WINSOCK
  572. #define MG_ENABLE_WINSOCK 1
  573. #endif
  574. #ifndef MG_ENABLE_DIRLIST
  575. #define MG_ENABLE_DIRLIST 0
  576. #endif
  577. #ifndef MG_ENABLE_CUSTOM_RANDOM
  578. #define MG_ENABLE_CUSTOM_RANDOM 0
  579. #endif
  580. #ifndef MG_ENABLE_CUSTOM_MILLIS
  581. #define MG_ENABLE_CUSTOM_MILLIS 0
  582. #endif
  583. #ifndef MG_ENABLE_PACKED_FS
  584. #define MG_ENABLE_PACKED_FS 0
  585. #endif
  586. #ifndef MG_ENABLE_ASSERT
  587. #define MG_ENABLE_ASSERT 0
  588. #endif
  589. #ifndef MG_IO_SIZE
  590. #define MG_IO_SIZE 2048 // Granularity of the send/recv IO buffer growth
  591. #endif
  592. #ifndef MG_MAX_RECV_SIZE
  593. #define MG_MAX_RECV_SIZE (3UL * 1024UL * 1024UL) // Maximum recv IO buffer size
  594. #endif
  595. #ifndef MG_DATA_SIZE
  596. #define MG_DATA_SIZE 32 // struct mg_connection :: data size
  597. #endif
  598. #ifndef MG_MAX_HTTP_HEADERS
  599. #define MG_MAX_HTTP_HEADERS 30
  600. #endif
  601. #ifndef MG_HTTP_INDEX
  602. #define MG_HTTP_INDEX "index.html"
  603. #endif
  604. #ifndef MG_PATH_MAX
  605. #ifdef PATH_MAX
  606. #define MG_PATH_MAX PATH_MAX
  607. #else
  608. #define MG_PATH_MAX 128
  609. #endif
  610. #endif
  611. #ifndef MG_SOCK_LISTEN_BACKLOG_SIZE
  612. #define MG_SOCK_LISTEN_BACKLOG_SIZE 128
  613. #endif
  614. #ifndef MG_DIRSEP
  615. #define MG_DIRSEP '/'
  616. #endif
  617. #ifndef MG_ENABLE_FILE
  618. #if defined(FOPEN_MAX)
  619. #define MG_ENABLE_FILE 1
  620. #else
  621. #define MG_ENABLE_FILE 0
  622. #endif
  623. #endif
  624. #ifndef MG_INVALID_SOCKET
  625. #define MG_INVALID_SOCKET (-1)
  626. #endif
  627. #ifndef MG_SOCKET_TYPE
  628. #define MG_SOCKET_TYPE int
  629. #endif
  630. #ifndef MG_SOCKET_ERRNO
  631. #define MG_SOCKET_ERRNO errno
  632. #endif
  633. #if MG_ENABLE_EPOLL
  634. #define MG_EPOLL_ADD(c) \
  635. do { \
  636. struct epoll_event ev = {EPOLLIN | EPOLLERR | EPOLLHUP, {c}}; \
  637. epoll_ctl(c->mgr->epoll_fd, EPOLL_CTL_ADD, (int) (size_t) c->fd, &ev); \
  638. } while (0)
  639. #define MG_EPOLL_MOD(c, wr) \
  640. do { \
  641. struct epoll_event ev = {EPOLLIN | EPOLLERR | EPOLLHUP, {c}}; \
  642. if (wr) ev.events |= EPOLLOUT; \
  643. epoll_ctl(c->mgr->epoll_fd, EPOLL_CTL_MOD, (int) (size_t) c->fd, &ev); \
  644. } while (0)
  645. #else
  646. #define MG_EPOLL_ADD(c)
  647. #define MG_EPOLL_MOD(c, wr)
  648. #endif
  649. #ifndef MG_ENABLE_PROFILE
  650. #define MG_ENABLE_PROFILE 0
  651. #endif
  652. struct mg_str {
  653. const char *ptr; // Pointer to string data
  654. size_t len; // String len
  655. };
  656. #define MG_NULL_STR \
  657. { NULL, 0 }
  658. #define MG_C_STR(a) \
  659. { (a), sizeof(a) - 1 }
  660. // Using macro to avoid shadowing C++ struct constructor, see #1298
  661. #define mg_str(s) mg_str_s(s)
  662. struct mg_str mg_str(const char *s);
  663. struct mg_str mg_str_n(const char *s, size_t n);
  664. int mg_lower(const char *s);
  665. int mg_ncasecmp(const char *s1, const char *s2, size_t len);
  666. int mg_casecmp(const char *s1, const char *s2);
  667. int mg_vcmp(const struct mg_str *s1, const char *s2);
  668. int mg_vcasecmp(const struct mg_str *str1, const char *str2);
  669. int mg_strcmp(const struct mg_str str1, const struct mg_str str2);
  670. struct mg_str mg_strstrip(struct mg_str s);
  671. struct mg_str mg_strdup(const struct mg_str s);
  672. const char *mg_strstr(const struct mg_str haystack, const struct mg_str needle);
  673. bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps);
  674. bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n);
  675. bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v);
  676. bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char delim);
  677. char *mg_hex(const void *buf, size_t len, char *dst);
  678. void mg_unhex(const char *buf, size_t len, unsigned char *to);
  679. unsigned long mg_unhexn(const char *s, size_t len);
  680. bool mg_path_is_sane(const char *path);
  681. // Single producer, single consumer non-blocking queue
  682. struct mg_queue {
  683. char *buf;
  684. size_t size;
  685. volatile size_t tail;
  686. volatile size_t head;
  687. };
  688. void mg_queue_init(struct mg_queue *, char *, size_t); // Init queue
  689. size_t mg_queue_book(struct mg_queue *, char **buf, size_t); // Reserve space
  690. void mg_queue_add(struct mg_queue *, size_t); // Add new message
  691. size_t mg_queue_next(struct mg_queue *, char **); // Get oldest message
  692. void mg_queue_del(struct mg_queue *, size_t); // Delete oldest message
  693. typedef void (*mg_pfn_t)(char, void *); // Output function
  694. typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer
  695. size_t mg_vxprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
  696. size_t mg_xprintf(void (*fn)(char, void *), void *, const char *fmt, ...);
  697. // Convenience wrappers around mg_xprintf
  698. size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap);
  699. size_t mg_snprintf(char *, size_t, const char *fmt, ...);
  700. char *mg_vmprintf(const char *fmt, va_list *ap);
  701. char *mg_mprintf(const char *fmt, ...);
  702. size_t mg_queue_vprintf(struct mg_queue *, const char *fmt, va_list *);
  703. size_t mg_queue_printf(struct mg_queue *, const char *fmt, ...);
  704. // %M print helper functions
  705. size_t mg_print_base64(void (*out)(char, void *), void *arg, va_list *ap);
  706. size_t mg_print_esc(void (*out)(char, void *), void *arg, va_list *ap);
  707. size_t mg_print_hex(void (*out)(char, void *), void *arg, va_list *ap);
  708. size_t mg_print_ip(void (*out)(char, void *), void *arg, va_list *ap);
  709. size_t mg_print_ip_port(void (*out)(char, void *), void *arg, va_list *ap);
  710. size_t mg_print_ip4(void (*out)(char, void *), void *arg, va_list *ap);
  711. size_t mg_print_ip6(void (*out)(char, void *), void *arg, va_list *ap);
  712. size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap);
  713. // Various output functions
  714. void mg_pfn_iobuf(char ch, void *param); // param: struct mg_iobuf *
  715. void mg_pfn_stdout(char c, void *param); // param: ignored
  716. // A helper macro for printing JSON: mg_snprintf(buf, len, "%m", MG_ESC("hi"))
  717. #define MG_ESC(str) mg_print_esc, 0, (str)
  718. enum { MG_LL_NONE, MG_LL_ERROR, MG_LL_INFO, MG_LL_DEBUG, MG_LL_VERBOSE };
  719. extern int mg_log_level; // Current log level, one of MG_LL_*
  720. void mg_log(const char *fmt, ...);
  721. void mg_log_prefix(int ll, const char *file, int line, const char *fname);
  722. // bool mg_log2(int ll, const char *file, int line, const char *fmt, ...);
  723. void mg_hexdump(const void *buf, size_t len);
  724. void mg_log_set_fn(mg_pfn_t fn, void *param);
  725. #define mg_log_set(level_) mg_log_level = (level_)
  726. #if MG_ENABLE_LOG
  727. #define MG_LOG(level, args) \
  728. do { \
  729. if ((level) <= mg_log_level) { \
  730. mg_log_prefix((level), __FILE__, __LINE__, __func__); \
  731. mg_log args; \
  732. } \
  733. } while (0)
  734. #else
  735. #define MG_LOG(level, args) \
  736. do { \
  737. if (0) mg_log args; \
  738. } while (0)
  739. #endif
  740. #define MG_ERROR(args) MG_LOG(MG_LL_ERROR, args)
  741. #define MG_INFO(args) MG_LOG(MG_LL_INFO, args)
  742. #define MG_DEBUG(args) MG_LOG(MG_LL_DEBUG, args)
  743. #define MG_VERBOSE(args) MG_LOG(MG_LL_VERBOSE, args)
  744. struct mg_timer {
  745. unsigned long id; // Timer ID
  746. uint64_t period_ms; // Timer period in milliseconds
  747. uint64_t expire; // Expiration timestamp in milliseconds
  748. unsigned flags; // Possible flags values below
  749. #define MG_TIMER_ONCE 0 // Call function once
  750. #define MG_TIMER_REPEAT 1 // Call function periodically
  751. #define MG_TIMER_RUN_NOW 2 // Call immediately when timer is set
  752. void (*fn)(void *); // Function to call
  753. void *arg; // Function argument
  754. struct mg_timer *next; // Linkage
  755. };
  756. void mg_timer_init(struct mg_timer **head, struct mg_timer *timer,
  757. uint64_t milliseconds, unsigned flags, void (*fn)(void *),
  758. void *arg);
  759. void mg_timer_free(struct mg_timer **head, struct mg_timer *);
  760. void mg_timer_poll(struct mg_timer **head, uint64_t new_ms);
  761. bool mg_timer_expired(uint64_t *expiration, uint64_t period, uint64_t now);
  762. enum { MG_FS_READ = 1, MG_FS_WRITE = 2, MG_FS_DIR = 4 };
  763. // Filesystem API functions
  764. // st() returns MG_FS_* flags and populates file size and modification time
  765. // ls() calls fn() for every directory entry, allowing to list a directory
  766. //
  767. // NOTE: UNIX-style shorthand names for the API functions are deliberately
  768. // chosen to avoid conflicts with some libraries that make macros for e.g.
  769. // stat(), write(), read() calls.
  770. struct mg_fs {
  771. int (*st)(const char *path, size_t *size, time_t *mtime); // stat file
  772. void (*ls)(const char *path, void (*fn)(const char *, void *),
  773. void *); // List directory entries: call fn(file_name, fn_data)
  774. // for each directory entry
  775. void *(*op)(const char *path, int flags); // Open file
  776. void (*cl)(void *fd); // Close file
  777. size_t (*rd)(void *fd, void *buf, size_t len); // Read file
  778. size_t (*wr)(void *fd, const void *buf, size_t len); // Write file
  779. size_t (*sk)(void *fd, size_t offset); // Set file position
  780. bool (*mv)(const char *from, const char *to); // Rename file
  781. bool (*rm)(const char *path); // Delete file
  782. bool (*mkd)(const char *path); // Create directory
  783. };
  784. extern struct mg_fs mg_fs_posix; // POSIX open/close/read/write/seek
  785. extern struct mg_fs mg_fs_packed; // Packed FS, see examples/device-dashboard
  786. extern struct mg_fs mg_fs_fat; // FAT FS
  787. // File descriptor
  788. struct mg_fd {
  789. void *fd;
  790. struct mg_fs *fs;
  791. };
  792. struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags);
  793. void mg_fs_close(struct mg_fd *fd);
  794. char *mg_file_read(struct mg_fs *fs, const char *path, size_t *size);
  795. bool mg_file_write(struct mg_fs *fs, const char *path, const void *, size_t);
  796. bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...);
  797. // Packed API
  798. const char *mg_unpack(const char *path, size_t *size, time_t *mtime);
  799. const char *mg_unlist(size_t no); // Get no'th packed filename
  800. struct mg_str mg_unpacked(const char *path); // Packed file as mg_str
  801. #if MG_ENABLE_ASSERT
  802. #include <assert.h>
  803. #elif !defined(assert)
  804. #define assert(x)
  805. #endif
  806. void mg_bzero(volatile unsigned char *buf, size_t len);
  807. void mg_random(void *buf, size_t len);
  808. char *mg_random_str(char *buf, size_t len);
  809. uint16_t mg_ntohs(uint16_t net);
  810. uint32_t mg_ntohl(uint32_t net);
  811. uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len);
  812. uint64_t mg_millis(void); // Return milliseconds since boot
  813. uint64_t mg_now(void); // Return milliseconds since Epoch
  814. #define mg_htons(x) mg_ntohs(x)
  815. #define mg_htonl(x) mg_ntohl(x)
  816. #define MG_U32(a, b, c, d) \
  817. (((uint32_t) ((a) &255) << 24) | ((uint32_t) ((b) &255) << 16) | \
  818. ((uint32_t) ((c) &255) << 8) | (uint32_t) ((d) &255))
  819. // For printing IPv4 addresses: printf("%d.%d.%d.%d\n", MG_IPADDR_PARTS(&ip))
  820. #define MG_U8P(ADDR) ((uint8_t *) (ADDR))
  821. #define MG_IPADDR_PARTS(ADDR) \
  822. MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3]
  823. #define MG_REG(x) ((volatile uint32_t *) (x))[0]
  824. #define MG_BIT(x) (((uint32_t) 1U) << (x))
  825. #define MG_SET_BITS(R, CLRMASK, SETMASK) (R) = ((R) & ~(CLRMASK)) | (SETMASK)
  826. #define MG_ROUND_UP(x, a) ((a) == 0 ? (x) : ((((x) + (a) -1) / (a)) * (a)))
  827. #define MG_ROUND_DOWN(x, a) ((a) == 0 ? (x) : (((x) / (a)) * (a)))
  828. #ifdef __GNUC__
  829. #define MG_ARM_DISABLE_IRQ() asm volatile("cpsid i" : : : "memory")
  830. #define MG_ARM_ENABLE_IRQ() asm volatile("cpsie i" : : : "memory")
  831. #else
  832. #define MG_ARM_DISABLE_IRQ()
  833. #define MG_ARM_ENABLE_IRQ()
  834. #endif
  835. #if defined(__CC_ARM)
  836. #define MG_DSB() __dsb(0xf)
  837. #elif defined(__ARMCC_VERSION)
  838. #define MG_DSB() __builtin_arm_dsb(0xf)
  839. #elif defined(__GNUC__) && defined(__arm__) && defined(__thumb__)
  840. #define MG_DSB() asm("DSB 0xf")
  841. #elif defined(__ICCARM__)
  842. #define MG_DSB() __iar_builtin_DSB()
  843. #else
  844. #define MG_DSB()
  845. #endif
  846. struct mg_addr;
  847. int mg_check_ip_acl(struct mg_str acl, struct mg_addr *remote_ip);
  848. // Linked list management macros
  849. #define LIST_ADD_HEAD(type_, head_, elem_) \
  850. do { \
  851. (elem_)->next = (*head_); \
  852. *(head_) = (elem_); \
  853. } while (0)
  854. #define LIST_ADD_TAIL(type_, head_, elem_) \
  855. do { \
  856. type_ **h = head_; \
  857. while (*h != NULL) h = &(*h)->next; \
  858. *h = (elem_); \
  859. } while (0)
  860. #define LIST_DELETE(type_, head_, elem_) \
  861. do { \
  862. type_ **h = head_; \
  863. while (*h != (elem_)) h = &(*h)->next; \
  864. *h = (elem_)->next; \
  865. } while (0)
  866. unsigned short mg_url_port(const char *url);
  867. int mg_url_is_ssl(const char *url);
  868. struct mg_str mg_url_host(const char *url);
  869. struct mg_str mg_url_user(const char *url);
  870. struct mg_str mg_url_pass(const char *url);
  871. const char *mg_url_uri(const char *url);
  872. struct mg_iobuf {
  873. unsigned char *buf; // Pointer to stored data
  874. size_t size; // Total size available
  875. size_t len; // Current number of bytes
  876. size_t align; // Alignment during allocation
  877. };
  878. int mg_iobuf_init(struct mg_iobuf *, size_t, size_t);
  879. int mg_iobuf_resize(struct mg_iobuf *, size_t);
  880. void mg_iobuf_free(struct mg_iobuf *);
  881. size_t mg_iobuf_add(struct mg_iobuf *, size_t, const void *, size_t);
  882. size_t mg_iobuf_del(struct mg_iobuf *, size_t ofs, size_t len);
  883. size_t mg_base64_update(unsigned char input_byte, char *buf, size_t len);
  884. size_t mg_base64_final(char *buf, size_t len);
  885. size_t mg_base64_encode(const unsigned char *p, size_t n, char *buf, size_t);
  886. size_t mg_base64_decode(const char *src, size_t n, char *dst, size_t);
  887. typedef struct {
  888. uint32_t buf[4];
  889. uint32_t bits[2];
  890. unsigned char in[64];
  891. } mg_md5_ctx;
  892. void mg_md5_init(mg_md5_ctx *c);
  893. void mg_md5_update(mg_md5_ctx *c, const unsigned char *data, size_t len);
  894. void mg_md5_final(mg_md5_ctx *c, unsigned char[16]);
  895. typedef struct {
  896. uint32_t state[5];
  897. uint32_t count[2];
  898. unsigned char buffer[64];
  899. } mg_sha1_ctx;
  900. void mg_sha1_init(mg_sha1_ctx *);
  901. void mg_sha1_update(mg_sha1_ctx *, const unsigned char *data, size_t len);
  902. void mg_sha1_final(unsigned char digest[20], mg_sha1_ctx *);
  903. typedef struct {
  904. uint32_t state[8];
  905. uint64_t bits;
  906. uint32_t len;
  907. unsigned char buffer[64];
  908. } mg_sha256_ctx;
  909. void mg_sha256_init(mg_sha256_ctx *);
  910. void mg_sha256_update(mg_sha256_ctx *, const unsigned char *data, size_t len);
  911. void mg_sha256_final(unsigned char digest[32], mg_sha256_ctx *);
  912. void mg_hmac_sha256(uint8_t dst[32], uint8_t *key, size_t keysz, uint8_t *data,
  913. size_t datasz);
  914. /******************************************************************************
  915. *
  916. * THIS SOURCE CODE IS HEREBY PLACED INTO THE PUBLIC DOMAIN FOR THE GOOD OF ALL
  917. *
  918. * This is a simple and straightforward implementation of the AES Rijndael
  919. * 128-bit block cipher designed by Vincent Rijmen and Joan Daemen. The focus
  920. * of this work was correctness & accuracy. It is written in 'C' without any
  921. * particular focus upon optimization or speed. It should be endian (memory
  922. * byte order) neutral since the few places that care are handled explicitly.
  923. *
  924. * This implementation of Rijndael was created by Steven M. Gibson of GRC.com.
  925. *
  926. * It is intended for general purpose use, but was written in support of GRC's
  927. * reference implementation of the SQRL (Secure Quick Reliable Login) client.
  928. *
  929. * See: http://csrc.nist.gov/archive/aes/rijndael/wsdindex.html
  930. *
  931. * NO COPYRIGHT IS CLAIMED IN THIS WORK, HOWEVER, NEITHER IS ANY WARRANTY MADE
  932. * REGARDING ITS FITNESS FOR ANY PARTICULAR PURPOSE. USE IT AT YOUR OWN RISK.
  933. *
  934. *******************************************************************************/
  935. #ifndef AES_HEADER
  936. #define AES_HEADER
  937. /******************************************************************************/
  938. #define AES_DECRYPTION 1 // whether AES decryption is supported
  939. /******************************************************************************/
  940. #define ENCRYPT 1 // specify whether we're encrypting
  941. #define DECRYPT 0 // or decrypting
  942. typedef unsigned char uchar; // add some convienent shorter types
  943. typedef unsigned int uint;
  944. /******************************************************************************
  945. * AES_INIT_KEYGEN_TABLES : MUST be called once before any AES use
  946. ******************************************************************************/
  947. void aes_init_keygen_tables(void);
  948. /******************************************************************************
  949. * AES_CONTEXT : cipher context / holds inter-call data
  950. ******************************************************************************/
  951. typedef struct {
  952. int mode; // 1 for Encryption, 0 for Decryption
  953. int rounds; // keysize-based rounds count
  954. uint32_t *rk; // pointer to current round key
  955. uint32_t buf[68]; // key expansion buffer
  956. } aes_context;
  957. /******************************************************************************
  958. * AES_SETKEY : called to expand the key for encryption or decryption
  959. ******************************************************************************/
  960. int aes_setkey(aes_context *ctx, // pointer to context
  961. int mode, // 1 or 0 for Encrypt/Decrypt
  962. const uchar *key, // AES input key
  963. uint keysize); // size in bytes (must be 16, 24, 32 for
  964. // 128, 192 or 256-bit keys respectively)
  965. // returns 0 for success
  966. /******************************************************************************
  967. * AES_CIPHER : called to encrypt or decrypt ONE 128-bit block of data
  968. ******************************************************************************/
  969. int aes_cipher(aes_context *ctx, // pointer to context
  970. const uchar input[16], // 128-bit block to en/decipher
  971. uchar output[16]); // 128-bit output result block
  972. // returns 0 for success
  973. #endif /* AES_HEADER */
  974. /******************************************************************************
  975. *
  976. * THIS SOURCE CODE IS HEREBY PLACED INTO THE PUBLIC DOMAIN FOR THE GOOD OF ALL
  977. *
  978. * This is a simple and straightforward implementation of AES-GCM authenticated
  979. * encryption. The focus of this work was correctness & accuracy. It is written
  980. * in straight 'C' without any particular focus upon optimization or speed. It
  981. * should be endian (memory byte order) neutral since the few places that care
  982. * are handled explicitly.
  983. *
  984. * This implementation of AES-GCM was created by Steven M. Gibson of GRC.com.
  985. *
  986. * It is intended for general purpose use, but was written in support of GRC's
  987. * reference implementation of the SQRL (Secure Quick Reliable Login) client.
  988. *
  989. * See: http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
  990. * http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/ \
  991. * gcm/gcm-revised-spec.pdf
  992. *
  993. * NO COPYRIGHT IS CLAIMED IN THIS WORK, HOWEVER, NEITHER IS ANY WARRANTY MADE
  994. * REGARDING ITS FITNESS FOR ANY PARTICULAR PURPOSE. USE IT AT YOUR OWN RISK.
  995. *
  996. *******************************************************************************/
  997. #ifndef GCM_HEADER
  998. #define GCM_HEADER
  999. #define GCM_AUTH_FAILURE 0x55555555 // authentication failure
  1000. /******************************************************************************
  1001. * GCM_CONTEXT : GCM context / holds keytables, instance data, and AES ctx
  1002. ******************************************************************************/
  1003. typedef struct {
  1004. int mode; // cipher direction: encrypt/decrypt
  1005. uint64_t len; // cipher data length processed so far
  1006. uint64_t add_len; // total add data length
  1007. uint64_t HL[16]; // precalculated lo-half HTable
  1008. uint64_t HH[16]; // precalculated hi-half HTable
  1009. uchar base_ectr[16]; // first counter-mode cipher output for tag
  1010. uchar y[16]; // the current cipher-input IV|Counter value
  1011. uchar buf[16]; // buf working value
  1012. aes_context aes_ctx; // cipher context used
  1013. } gcm_context;
  1014. /******************************************************************************
  1015. * GCM_CONTEXT : MUST be called once before ANY use of this library
  1016. ******************************************************************************/
  1017. int gcm_initialize(void);
  1018. /******************************************************************************
  1019. * GCM_SETKEY : sets the GCM (and AES) keying material for use
  1020. ******************************************************************************/
  1021. int gcm_setkey(gcm_context *ctx, // caller-provided context ptr
  1022. const uchar *key, // pointer to cipher key
  1023. const uint keysize // size in bytes (must be 16, 24, 32 for
  1024. // 128, 192 or 256-bit keys respectively)
  1025. ); // returns 0 for success
  1026. /******************************************************************************
  1027. *
  1028. * GCM_CRYPT_AND_TAG
  1029. *
  1030. * This either encrypts or decrypts the user-provided data and, either
  1031. * way, generates an authentication tag of the requested length. It must be
  1032. * called with a GCM context whose key has already been set with GCM_SETKEY.
  1033. *
  1034. * The user would typically call this explicitly to ENCRYPT a buffer of data
  1035. * and optional associated data, and produce its an authentication tag.
  1036. *
  1037. * To reverse the process the user would typically call the companion
  1038. * GCM_AUTH_DECRYPT function to decrypt data and verify a user-provided
  1039. * authentication tag. The GCM_AUTH_DECRYPT function calls this function
  1040. * to perform its decryption and tag generation, which it then compares.
  1041. *
  1042. ******************************************************************************/
  1043. int gcm_crypt_and_tag(
  1044. gcm_context *ctx, // gcm context with key already setup
  1045. int mode, // cipher direction: ENCRYPT (1) or DECRYPT (0)
  1046. const uchar *iv, // pointer to the 12-byte initialization vector
  1047. size_t iv_len, // byte length if the IV. should always be 12
  1048. const uchar *add, // pointer to the non-ciphered additional data
  1049. size_t add_len, // byte length of the additional AEAD data
  1050. const uchar *input, // pointer to the cipher data source
  1051. uchar *output, // pointer to the cipher data destination
  1052. size_t length, // byte length of the cipher data
  1053. uchar *tag, // pointer to the tag to be generated
  1054. size_t tag_len); // byte length of the tag to be generated
  1055. /******************************************************************************
  1056. *
  1057. * GCM_AUTH_DECRYPT
  1058. *
  1059. * This DECRYPTS a user-provided data buffer with optional associated data.
  1060. * It then verifies a user-supplied authentication tag against the tag just
  1061. * re-created during decryption to verify that the data has not been altered.
  1062. *
  1063. * This function calls GCM_CRYPT_AND_TAG (above) to perform the decryption
  1064. * and authentication tag generation.
  1065. *
  1066. ******************************************************************************/
  1067. int gcm_auth_decrypt(
  1068. gcm_context *ctx, // gcm context with key already setup
  1069. const uchar *iv, // pointer to the 12-byte initialization vector
  1070. size_t iv_len, // byte length if the IV. should always be 12
  1071. const uchar *add, // pointer to the non-ciphered additional data
  1072. size_t add_len, // byte length of the additional AEAD data
  1073. const uchar *input, // pointer to the cipher data source
  1074. uchar *output, // pointer to the cipher data destination
  1075. size_t length, // byte length of the cipher data
  1076. const uchar *tag, // pointer to the tag to be authenticated
  1077. size_t tag_len); // byte length of the tag <= 16
  1078. /******************************************************************************
  1079. *
  1080. * GCM_START
  1081. *
  1082. * Given a user-provided GCM context, this initializes it, sets the encryption
  1083. * mode, and preprocesses the initialization vector and additional AEAD data.
  1084. *
  1085. ******************************************************************************/
  1086. int gcm_start(
  1087. gcm_context *ctx, // pointer to user-provided GCM context
  1088. int mode, // ENCRYPT (1) or DECRYPT (0)
  1089. const uchar *iv, // pointer to initialization vector
  1090. size_t iv_len, // IV length in bytes (should == 12)
  1091. const uchar *add, // pointer to additional AEAD data (NULL if none)
  1092. size_t add_len); // length of additional AEAD data (bytes)
  1093. /******************************************************************************
  1094. *
  1095. * GCM_UPDATE
  1096. *
  1097. * This is called once or more to process bulk plaintext or ciphertext data.
  1098. * We give this some number of bytes of input and it returns the same number
  1099. * of output bytes. If called multiple times (which is fine) all but the final
  1100. * invocation MUST be called with length mod 16 == 0. (Only the final call can
  1101. * have a partial block length of < 128 bits.)
  1102. *
  1103. ******************************************************************************/
  1104. int gcm_update(gcm_context *ctx, // pointer to user-provided GCM context
  1105. size_t length, // length, in bytes, of data to process
  1106. const uchar *input, // pointer to source data
  1107. uchar *output); // pointer to destination data
  1108. /******************************************************************************
  1109. *
  1110. * GCM_FINISH
  1111. *
  1112. * This is called once after all calls to GCM_UPDATE to finalize the GCM.
  1113. * It performs the final GHASH to produce the resulting authentication TAG.
  1114. *
  1115. ******************************************************************************/
  1116. int gcm_finish(gcm_context *ctx, // pointer to user-provided GCM context
  1117. uchar *tag, // ptr to tag buffer - NULL if tag_len = 0
  1118. size_t tag_len); // length, in bytes, of the tag-receiving buf
  1119. /******************************************************************************
  1120. *
  1121. * GCM_ZERO_CTX
  1122. *
  1123. * The GCM context contains both the GCM context and the AES context.
  1124. * This includes keying and key-related material which is security-
  1125. * sensitive, so it MUST be zeroed after use. This function does that.
  1126. *
  1127. ******************************************************************************/
  1128. void gcm_zero_ctx(gcm_context *ctx);
  1129. #endif /* GCM_HEADER */
  1130. //
  1131. // aes-gcm.h
  1132. // MKo
  1133. //
  1134. // Created by Markus Kosmal on 20/11/14.
  1135. //
  1136. //
  1137. #ifndef mko_aes_gcm_h
  1138. #define mko_aes_gcm_h
  1139. int aes_gcm_encrypt(unsigned char *output, const unsigned char *input,
  1140. size_t input_length, const unsigned char *key,
  1141. const size_t key_len, const unsigned char *iv,
  1142. const size_t iv_len, unsigned char *aead, size_t aead_len,
  1143. unsigned char *tag, const size_t tag_len);
  1144. int aes_gcm_decrypt(unsigned char *output, const unsigned char *input,
  1145. size_t input_length, const unsigned char *key,
  1146. const size_t key_len, const unsigned char *iv,
  1147. const size_t iv_len);
  1148. #endif
  1149. // End of aes128 PD
  1150. #define uECC_SUPPORTS_secp256r1 1
  1151. /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
  1152. #ifndef _UECC_H_
  1153. #define _UECC_H_
  1154. /* Platform selection options.
  1155. If uECC_PLATFORM is not defined, the code will try to guess it based on compiler
  1156. macros. Possible values for uECC_PLATFORM are defined below: */
  1157. #define uECC_arch_other 0
  1158. #define uECC_x86 1
  1159. #define uECC_x86_64 2
  1160. #define uECC_arm 3
  1161. #define uECC_arm_thumb 4
  1162. #define uECC_arm_thumb2 5
  1163. #define uECC_arm64 6
  1164. #define uECC_avr 7
  1165. /* If desired, you can define uECC_WORD_SIZE as appropriate for your platform
  1166. (1, 4, or 8 bytes). If uECC_WORD_SIZE is not explicitly defined then it will be
  1167. automatically set based on your platform. */
  1168. /* Optimization level; trade speed for code size.
  1169. Larger values produce code that is faster but larger.
  1170. Currently supported values are 0 - 4; 0 is unusably slow for most
  1171. applications. Optimization level 4 currently only has an effect ARM platforms
  1172. where more than one curve is enabled. */
  1173. #ifndef uECC_OPTIMIZATION_LEVEL
  1174. #define uECC_OPTIMIZATION_LEVEL 2
  1175. #endif
  1176. /* uECC_SQUARE_FUNC - If enabled (defined as nonzero), this will cause a
  1177. specific function to be used for (scalar) squaring instead of the generic
  1178. multiplication function. This can make things faster somewhat faster, but
  1179. increases the code size. */
  1180. #ifndef uECC_SQUARE_FUNC
  1181. #define uECC_SQUARE_FUNC 0
  1182. #endif
  1183. /* uECC_VLI_NATIVE_LITTLE_ENDIAN - If enabled (defined as nonzero), this will
  1184. switch to native little-endian format for *all* arrays passed in and out of the
  1185. public API. This includes public and private keys, shared secrets, signatures
  1186. and message hashes. Using this switch reduces the amount of call stack memory
  1187. used by uECC, since less intermediate translations are required. Note that this
  1188. will *only* work on native little-endian processors and it will treat the
  1189. uint8_t arrays passed into the public API as word arrays, therefore requiring
  1190. the provided byte arrays to be word aligned on architectures that do not support
  1191. unaligned accesses. IMPORTANT: Keys and signatures generated with
  1192. uECC_VLI_NATIVE_LITTLE_ENDIAN=1 are incompatible with keys and signatures
  1193. generated with uECC_VLI_NATIVE_LITTLE_ENDIAN=0; all parties must use the same
  1194. endianness. */
  1195. #ifndef uECC_VLI_NATIVE_LITTLE_ENDIAN
  1196. #define uECC_VLI_NATIVE_LITTLE_ENDIAN 0
  1197. #endif
  1198. /* Curve support selection. Set to 0 to remove that curve. */
  1199. #ifndef uECC_SUPPORTS_secp160r1
  1200. #define uECC_SUPPORTS_secp160r1 0
  1201. #endif
  1202. #ifndef uECC_SUPPORTS_secp192r1
  1203. #define uECC_SUPPORTS_secp192r1 0
  1204. #endif
  1205. #ifndef uECC_SUPPORTS_secp224r1
  1206. #define uECC_SUPPORTS_secp224r1 0
  1207. #endif
  1208. #ifndef uECC_SUPPORTS_secp256r1
  1209. #define uECC_SUPPORTS_secp256r1 1
  1210. #endif
  1211. #ifndef uECC_SUPPORTS_secp256k1
  1212. #define uECC_SUPPORTS_secp256k1 0
  1213. #endif
  1214. /* Specifies whether compressed point format is supported.
  1215. Set to 0 to disable point compression/decompression functions. */
  1216. #ifndef uECC_SUPPORT_COMPRESSED_POINT
  1217. #define uECC_SUPPORT_COMPRESSED_POINT 1
  1218. #endif
  1219. struct uECC_Curve_t;
  1220. typedef const struct uECC_Curve_t *uECC_Curve;
  1221. #ifdef __cplusplus
  1222. extern "C" {
  1223. #endif
  1224. #if uECC_SUPPORTS_secp160r1
  1225. uECC_Curve uECC_secp160r1(void);
  1226. #endif
  1227. #if uECC_SUPPORTS_secp192r1
  1228. uECC_Curve uECC_secp192r1(void);
  1229. #endif
  1230. #if uECC_SUPPORTS_secp224r1
  1231. uECC_Curve uECC_secp224r1(void);
  1232. #endif
  1233. #if uECC_SUPPORTS_secp256r1
  1234. uECC_Curve uECC_secp256r1(void);
  1235. #endif
  1236. #if uECC_SUPPORTS_secp256k1
  1237. uECC_Curve uECC_secp256k1(void);
  1238. #endif
  1239. /* uECC_RNG_Function type
  1240. The RNG function should fill 'size' random bytes into 'dest'. It should return 1
  1241. if 'dest' was filled with random data, or 0 if the random data could not be
  1242. generated. The filled-in values should be either truly random, or from a
  1243. cryptographically-secure PRNG.
  1244. A correctly functioning RNG function must be set (using uECC_set_rng()) before
  1245. calling uECC_make_key() or uECC_sign().
  1246. Setting a correctly functioning RNG function improves the resistance to
  1247. side-channel attacks for uECC_shared_secret() and uECC_sign_deterministic().
  1248. A correct RNG function is set by default when building for Windows, Linux, or OS
  1249. X. If you are building on another POSIX-compliant system that supports
  1250. /dev/random or /dev/urandom, you can define uECC_POSIX to use the predefined
  1251. RNG. For embedded platforms there is no predefined RNG function; you must
  1252. provide your own.
  1253. */
  1254. typedef int (*uECC_RNG_Function)(uint8_t *dest, unsigned size);
  1255. /* uECC_set_rng() function.
  1256. Set the function that will be used to generate random bytes. The RNG function
  1257. should return 1 if the random data was generated, or 0 if the random data could
  1258. not be generated.
  1259. On platforms where there is no predefined RNG function (eg embedded platforms),
  1260. this must be called before uECC_make_key() or uECC_sign() are used.
  1261. Inputs:
  1262. rng_function - The function that will be used to generate random bytes.
  1263. */
  1264. void uECC_set_rng(uECC_RNG_Function rng_function);
  1265. /* uECC_get_rng() function.
  1266. Returns the function that will be used to generate random bytes.
  1267. */
  1268. uECC_RNG_Function uECC_get_rng(void);
  1269. /* uECC_curve_private_key_size() function.
  1270. Returns the size of a private key for the curve in bytes.
  1271. */
  1272. int uECC_curve_private_key_size(uECC_Curve curve);
  1273. /* uECC_curve_public_key_size() function.
  1274. Returns the size of a public key for the curve in bytes.
  1275. */
  1276. int uECC_curve_public_key_size(uECC_Curve curve);
  1277. /* uECC_make_key() function.
  1278. Create a public/private key pair.
  1279. Outputs:
  1280. public_key - Will be filled in with the public key. Must be at least 2 *
  1281. the curve size (in bytes) long. For example, if the curve is secp256r1,
  1282. public_key must be 64 bytes long. private_key - Will be filled in with the
  1283. private key. Must be as long as the curve order; this is typically the same as
  1284. the curve size, except for secp160r1. For example, if the curve is secp256r1,
  1285. private_key must be 32 bytes long.
  1286. For secp160r1, private_key must be 21 bytes long! Note that
  1287. the first byte will almost always be 0 (there is about a 1 in 2^80 chance of it
  1288. being non-zero).
  1289. Returns 1 if the key pair was generated successfully, 0 if an error occurred.
  1290. */
  1291. int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve);
  1292. /* uECC_shared_secret() function.
  1293. Compute a shared secret given your secret key and someone else's public key. If
  1294. the public key is not from a trusted source and has not been previously
  1295. verified, you should verify it first using uECC_valid_public_key(). Note: It is
  1296. recommended that you hash the result of uECC_shared_secret() before using it for
  1297. symmetric encryption or HMAC.
  1298. Inputs:
  1299. public_key - The public key of the remote party.
  1300. private_key - Your private key.
  1301. Outputs:
  1302. secret - Will be filled in with the shared secret value. Must be the same
  1303. size as the curve size; for example, if the curve is secp256r1, secret must be
  1304. 32 bytes long.
  1305. Returns 1 if the shared secret was generated successfully, 0 if an error
  1306. occurred.
  1307. */
  1308. int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,
  1309. uint8_t *secret, uECC_Curve curve);
  1310. #if uECC_SUPPORT_COMPRESSED_POINT
  1311. /* uECC_compress() function.
  1312. Compress a public key.
  1313. Inputs:
  1314. public_key - The public key to compress.
  1315. Outputs:
  1316. compressed - Will be filled in with the compressed public key. Must be at
  1317. least (curve size + 1) bytes long; for example, if the curve is secp256r1,
  1318. compressed must be 33 bytes long.
  1319. */
  1320. void uECC_compress(const uint8_t *public_key, uint8_t *compressed,
  1321. uECC_Curve curve);
  1322. /* uECC_decompress() function.
  1323. Decompress a compressed public key.
  1324. Inputs:
  1325. compressed - The compressed public key.
  1326. Outputs:
  1327. public_key - Will be filled in with the decompressed public key.
  1328. */
  1329. void uECC_decompress(const uint8_t *compressed, uint8_t *public_key,
  1330. uECC_Curve curve);
  1331. #endif /* uECC_SUPPORT_COMPRESSED_POINT */
  1332. /* uECC_valid_public_key() function.
  1333. Check to see if a public key is valid.
  1334. Note that you are not required to check for a valid public key before using any
  1335. other uECC functions. However, you may wish to avoid spending CPU time computing
  1336. a shared secret or verifying a signature using an invalid public key.
  1337. Inputs:
  1338. public_key - The public key to check.
  1339. Returns 1 if the public key is valid, 0 if it is invalid.
  1340. */
  1341. int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve);
  1342. /* uECC_compute_public_key() function.
  1343. Compute the corresponding public key for a private key.
  1344. Inputs:
  1345. private_key - The private key to compute the public key for
  1346. Outputs:
  1347. public_key - Will be filled in with the corresponding public key
  1348. Returns 1 if the key was computed successfully, 0 if an error occurred.
  1349. */
  1350. int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
  1351. uECC_Curve curve);
  1352. /* uECC_sign() function.
  1353. Generate an ECDSA signature for a given hash value.
  1354. Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and
  1355. pass it in to this function along with your private key.
  1356. Inputs:
  1357. private_key - Your private key.
  1358. message_hash - The hash of the message to sign.
  1359. hash_size - The size of message_hash in bytes.
  1360. Outputs:
  1361. signature - Will be filled in with the signature value. Must be at least 2 *
  1362. curve size long. For example, if the curve is secp256r1, signature must be 64
  1363. bytes long.
  1364. Returns 1 if the signature generated successfully, 0 if an error occurred.
  1365. */
  1366. int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
  1367. unsigned hash_size, uint8_t *signature, uECC_Curve curve);
  1368. /* uECC_HashContext structure.
  1369. This is used to pass in an arbitrary hash function to uECC_sign_deterministic().
  1370. The structure will be used for multiple hash computations; each time a new hash
  1371. is computed, init_hash() will be called, followed by one or more calls to
  1372. update_hash(), and finally a call to finish_hash() to produce the resulting
  1373. hash.
  1374. The intention is that you will create a structure that includes uECC_HashContext
  1375. followed by any hash-specific data. For example:
  1376. typedef struct SHA256_HashContext {
  1377. uECC_HashContext uECC;
  1378. SHA256_CTX ctx;
  1379. } SHA256_HashContext;
  1380. void init_SHA256(uECC_HashContext *base) {
  1381. SHA256_HashContext *context = (SHA256_HashContext *)base;
  1382. SHA256_Init(&context->ctx);
  1383. }
  1384. void update_SHA256(uECC_HashContext *base,
  1385. const uint8_t *message,
  1386. unsigned message_size) {
  1387. SHA256_HashContext *context = (SHA256_HashContext *)base;
  1388. SHA256_Update(&context->ctx, message, message_size);
  1389. }
  1390. void finish_SHA256(uECC_HashContext *base, uint8_t *hash_result) {
  1391. SHA256_HashContext *context = (SHA256_HashContext *)base;
  1392. SHA256_Final(hash_result, &context->ctx);
  1393. }
  1394. ... when signing ...
  1395. {
  1396. uint8_t tmp[32 + 32 + 64];
  1397. SHA256_HashContext ctx = {{&init_SHA256, &update_SHA256, &finish_SHA256, 64,
  1398. 32, tmp}}; uECC_sign_deterministic(key, message_hash, &ctx.uECC, signature);
  1399. }
  1400. */
  1401. typedef struct uECC_HashContext {
  1402. void (*init_hash)(const struct uECC_HashContext *context);
  1403. void (*update_hash)(const struct uECC_HashContext *context,
  1404. const uint8_t *message, unsigned message_size);
  1405. void (*finish_hash)(const struct uECC_HashContext *context,
  1406. uint8_t *hash_result);
  1407. unsigned
  1408. block_size; /* Hash function block size in bytes, eg 64 for SHA-256. */
  1409. unsigned
  1410. result_size; /* Hash function result size in bytes, eg 32 for SHA-256. */
  1411. uint8_t *tmp; /* Must point to a buffer of at least (2 * result_size +
  1412. block_size) bytes. */
  1413. } uECC_HashContext;
  1414. /* uECC_sign_deterministic() function.
  1415. Generate an ECDSA signature for a given hash value, using a deterministic
  1416. algorithm (see RFC 6979). You do not need to set the RNG using uECC_set_rng()
  1417. before calling this function; however, if the RNG is defined it will improve
  1418. resistance to side-channel attacks.
  1419. Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and
  1420. pass it to this function along with your private key and a hash context. Note
  1421. that the message_hash does not need to be computed with the same hash function
  1422. used by hash_context.
  1423. Inputs:
  1424. private_key - Your private key.
  1425. message_hash - The hash of the message to sign.
  1426. hash_size - The size of message_hash in bytes.
  1427. hash_context - A hash context to use.
  1428. Outputs:
  1429. signature - Will be filled in with the signature value.
  1430. Returns 1 if the signature generated successfully, 0 if an error occurred.
  1431. */
  1432. int uECC_sign_deterministic(const uint8_t *private_key,
  1433. const uint8_t *message_hash, unsigned hash_size,
  1434. const uECC_HashContext *hash_context,
  1435. uint8_t *signature, uECC_Curve curve);
  1436. /* uECC_verify() function.
  1437. Verify an ECDSA signature.
  1438. Usage: Compute the hash of the signed data using the same hash as the signer and
  1439. pass it to this function along with the signer's public key and the signature
  1440. values (r and s).
  1441. Inputs:
  1442. public_key - The signer's public key.
  1443. message_hash - The hash of the signed data.
  1444. hash_size - The size of message_hash in bytes.
  1445. signature - The signature value.
  1446. Returns 1 if the signature is valid, 0 if it is invalid.
  1447. */
  1448. int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
  1449. unsigned hash_size, const uint8_t *signature, uECC_Curve curve);
  1450. #ifdef __cplusplus
  1451. } /* end of extern "C" */
  1452. #endif
  1453. #endif /* _UECC_H_ */
  1454. /* Copyright 2015, Kenneth MacKay. Licensed under the BSD 2-clause license. */
  1455. #ifndef _UECC_VLI_H_
  1456. #define _UECC_VLI_H_
  1457. //
  1458. //
  1459. /* Functions for raw large-integer manipulation. These are only available
  1460. if uECC.c is compiled with uECC_ENABLE_VLI_API defined to 1. */
  1461. #ifndef uECC_ENABLE_VLI_API
  1462. #define uECC_ENABLE_VLI_API 0
  1463. #endif
  1464. #ifdef __cplusplus
  1465. extern "C" {
  1466. #endif
  1467. #if uECC_ENABLE_VLI_API
  1468. void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words);
  1469. /* Constant-time comparison to zero - secure way to compare long integers */
  1470. /* Returns 1 if vli == 0, 0 otherwise. */
  1471. uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words);
  1472. /* Returns nonzero if bit 'bit' of vli is set. */
  1473. uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit);
  1474. /* Counts the number of bits required to represent vli. */
  1475. bitcount_t uECC_vli_numBits(const uECC_word_t *vli,
  1476. const wordcount_t max_words);
  1477. /* Sets dest = src. */
  1478. void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src,
  1479. wordcount_t num_words);
  1480. /* Constant-time comparison function - secure way to compare long integers */
  1481. /* Returns one if left == right, zero otherwise */
  1482. uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right,
  1483. wordcount_t num_words);
  1484. /* Constant-time comparison function - secure way to compare long integers */
  1485. /* Returns sign of left - right, in constant time. */
  1486. cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right,
  1487. wordcount_t num_words);
  1488. /* Computes vli = vli >> 1. */
  1489. void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words);
  1490. /* Computes result = left + right, returning carry. Can modify in place. */
  1491. uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left,
  1492. const uECC_word_t *right, wordcount_t num_words);
  1493. /* Computes result = left - right, returning borrow. Can modify in place. */
  1494. uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
  1495. const uECC_word_t *right, wordcount_t num_words);
  1496. /* Computes result = left * right. Result must be 2 * num_words long. */
  1497. void uECC_vli_mult(uECC_word_t *result, const uECC_word_t *left,
  1498. const uECC_word_t *right, wordcount_t num_words);
  1499. /* Computes result = left^2. Result must be 2 * num_words long. */
  1500. void uECC_vli_square(uECC_word_t *result, const uECC_word_t *left,
  1501. wordcount_t num_words);
  1502. /* Computes result = (left + right) % mod.
  1503. Assumes that left < mod and right < mod, and that result does not overlap
  1504. mod. */
  1505. void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
  1506. const uECC_word_t *right, const uECC_word_t *mod,
  1507. wordcount_t num_words);
  1508. /* Computes result = (left - right) % mod.
  1509. Assumes that left < mod and right < mod, and that result does not overlap
  1510. mod. */
  1511. void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
  1512. const uECC_word_t *right, const uECC_word_t *mod,
  1513. wordcount_t num_words);
  1514. /* Computes result = product % mod, where product is 2N words long.
  1515. Currently only designed to work for mod == curve->p or curve_n. */
  1516. void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
  1517. const uECC_word_t *mod, wordcount_t num_words);
  1518. /* Calculates result = product (mod curve->p), where product is up to
  1519. 2 * curve->num_words long. */
  1520. void uECC_vli_mmod_fast(uECC_word_t *result, uECC_word_t *product,
  1521. uECC_Curve curve);
  1522. /* Computes result = (left * right) % mod.
  1523. Currently only designed to work for mod == curve->p or curve_n. */
  1524. void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
  1525. const uECC_word_t *right, const uECC_word_t *mod,
  1526. wordcount_t num_words);
  1527. /* Computes result = (left * right) % curve->p. */
  1528. void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
  1529. const uECC_word_t *right, uECC_Curve curve);
  1530. /* Computes result = left^2 % mod.
  1531. Currently only designed to work for mod == curve->p or curve_n. */
  1532. void uECC_vli_modSquare(uECC_word_t *result, const uECC_word_t *left,
  1533. const uECC_word_t *mod, wordcount_t num_words);
  1534. /* Computes result = left^2 % curve->p. */
  1535. void uECC_vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left,
  1536. uECC_Curve curve);
  1537. /* Computes result = (1 / input) % mod.*/
  1538. void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
  1539. const uECC_word_t *mod, wordcount_t num_words);
  1540. #if uECC_SUPPORT_COMPRESSED_POINT
  1541. /* Calculates a = sqrt(a) (mod curve->p) */
  1542. void uECC_vli_mod_sqrt(uECC_word_t *a, uECC_Curve curve);
  1543. #endif
  1544. /* Converts an integer in uECC native format to big-endian bytes. */
  1545. void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
  1546. const uECC_word_t *native);
  1547. /* Converts big-endian bytes to an integer in uECC native format. */
  1548. void uECC_vli_bytesToNative(uECC_word_t *native, const uint8_t *bytes,
  1549. int num_bytes);
  1550. unsigned uECC_curve_num_words(uECC_Curve curve);
  1551. unsigned uECC_curve_num_bytes(uECC_Curve curve);
  1552. unsigned uECC_curve_num_bits(uECC_Curve curve);
  1553. unsigned uECC_curve_num_n_words(uECC_Curve curve);
  1554. unsigned uECC_curve_num_n_bytes(uECC_Curve curve);
  1555. unsigned uECC_curve_num_n_bits(uECC_Curve curve);
  1556. const uECC_word_t *uECC_curve_p(uECC_Curve curve);
  1557. const uECC_word_t *uECC_curve_n(uECC_Curve curve);
  1558. const uECC_word_t *uECC_curve_G(uECC_Curve curve);
  1559. const uECC_word_t *uECC_curve_b(uECC_Curve curve);
  1560. int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve);
  1561. /* Multiplies a point by a scalar. Points are represented by the X coordinate
  1562. followed by the Y coordinate in the same array, both coordinates are
  1563. curve->num_words long. Note that scalar must be curve->num_n_words long (NOT
  1564. curve->num_words). */
  1565. void uECC_point_mult(uECC_word_t *result, const uECC_word_t *point,
  1566. const uECC_word_t *scalar, uECC_Curve curve);
  1567. /* Generates a random integer in the range 0 < random < top.
  1568. Both random and top have num_words words. */
  1569. int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
  1570. wordcount_t num_words);
  1571. #endif /* uECC_ENABLE_VLI_API */
  1572. #ifdef __cplusplus
  1573. } /* end of extern "C" */
  1574. #endif
  1575. #endif /* _UECC_VLI_H_ */
  1576. /* Copyright 2015, Kenneth MacKay. Licensed under the BSD 2-clause license. */
  1577. #ifndef _UECC_TYPES_H_
  1578. #define _UECC_TYPES_H_
  1579. #ifndef uECC_PLATFORM
  1580. #if defined(__AVR__) && __AVR__
  1581. #define uECC_PLATFORM uECC_avr
  1582. #elif defined(__thumb2__) || \
  1583. defined(_M_ARMT) /* I think MSVC only supports Thumb-2 targets */
  1584. #define uECC_PLATFORM uECC_arm_thumb2
  1585. #elif defined(__thumb__)
  1586. #define uECC_PLATFORM uECC_arm_thumb
  1587. #elif defined(__arm__) || defined(_M_ARM)
  1588. #define uECC_PLATFORM uECC_arm
  1589. #elif defined(__aarch64__)
  1590. #define uECC_PLATFORM uECC_arm64
  1591. #elif defined(__i386__) || defined(_M_IX86) || defined(_X86_) || \
  1592. defined(__I86__)
  1593. #define uECC_PLATFORM uECC_x86
  1594. #elif defined(__amd64__) || defined(_M_X64)
  1595. #define uECC_PLATFORM uECC_x86_64
  1596. #else
  1597. #define uECC_PLATFORM uECC_arch_other
  1598. #endif
  1599. #endif
  1600. #ifndef uECC_ARM_USE_UMAAL
  1601. #if (uECC_PLATFORM == uECC_arm) && (__ARM_ARCH >= 6)
  1602. #define uECC_ARM_USE_UMAAL 1
  1603. #elif (uECC_PLATFORM == uECC_arm_thumb2) && (__ARM_ARCH >= 6) && \
  1604. (!defined(__ARM_ARCH_7M__) || !__ARM_ARCH_7M__)
  1605. #define uECC_ARM_USE_UMAAL 1
  1606. #else
  1607. #define uECC_ARM_USE_UMAAL 0
  1608. #endif
  1609. #endif
  1610. #ifndef uECC_WORD_SIZE
  1611. #if uECC_PLATFORM == uECC_avr
  1612. #define uECC_WORD_SIZE 1
  1613. #elif (uECC_PLATFORM == uECC_x86_64 || uECC_PLATFORM == uECC_arm64)
  1614. #define uECC_WORD_SIZE 8
  1615. #else
  1616. #define uECC_WORD_SIZE 4
  1617. #endif
  1618. #endif
  1619. #if (uECC_WORD_SIZE != 1) && (uECC_WORD_SIZE != 4) && (uECC_WORD_SIZE != 8)
  1620. #error "Unsupported value for uECC_WORD_SIZE"
  1621. #endif
  1622. #if ((uECC_PLATFORM == uECC_avr) && (uECC_WORD_SIZE != 1))
  1623. #pragma message("uECC_WORD_SIZE must be 1 for AVR")
  1624. #undef uECC_WORD_SIZE
  1625. #define uECC_WORD_SIZE 1
  1626. #endif
  1627. #if ((uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb || \
  1628. uECC_PLATFORM == uECC_arm_thumb2) && \
  1629. (uECC_WORD_SIZE != 4))
  1630. #pragma message("uECC_WORD_SIZE must be 4 for ARM")
  1631. #undef uECC_WORD_SIZE
  1632. #define uECC_WORD_SIZE 4
  1633. #endif
  1634. typedef int8_t wordcount_t;
  1635. typedef int16_t bitcount_t;
  1636. typedef int8_t cmpresult_t;
  1637. #if (uECC_WORD_SIZE == 1)
  1638. typedef uint8_t uECC_word_t;
  1639. typedef uint16_t uECC_dword_t;
  1640. #define HIGH_BIT_SET 0x80
  1641. #define uECC_WORD_BITS 8
  1642. #define uECC_WORD_BITS_SHIFT 3
  1643. #define uECC_WORD_BITS_MASK 0x07
  1644. #elif (uECC_WORD_SIZE == 4)
  1645. typedef uint32_t uECC_word_t;
  1646. typedef uint64_t uECC_dword_t;
  1647. #define HIGH_BIT_SET 0x80000000
  1648. #define uECC_WORD_BITS 32
  1649. #define uECC_WORD_BITS_SHIFT 5
  1650. #define uECC_WORD_BITS_MASK 0x01F
  1651. #elif (uECC_WORD_SIZE == 8)
  1652. typedef uint64_t uECC_word_t;
  1653. #define HIGH_BIT_SET 0x8000000000000000U
  1654. #define uECC_WORD_BITS 64
  1655. #define uECC_WORD_BITS_SHIFT 6
  1656. #define uECC_WORD_BITS_MASK 0x03F
  1657. #endif /* uECC_WORD_SIZE */
  1658. #endif /* _UECC_TYPES_H_ */
  1659. // End of uecc BSD-2
  1660. struct mg_connection;
  1661. typedef void (*mg_event_handler_t)(struct mg_connection *, int ev,
  1662. void *ev_data);
  1663. void mg_call(struct mg_connection *c, int ev, void *ev_data);
  1664. void mg_error(struct mg_connection *c, const char *fmt, ...);
  1665. enum {
  1666. MG_EV_ERROR, // Error char *error_message
  1667. MG_EV_OPEN, // Connection created NULL
  1668. MG_EV_POLL, // mg_mgr_poll iteration uint64_t *uptime_millis
  1669. MG_EV_RESOLVE, // Host name is resolved NULL
  1670. MG_EV_CONNECT, // Connection established NULL
  1671. MG_EV_ACCEPT, // Connection accepted NULL
  1672. MG_EV_TLS_HS, // TLS handshake succeeded NULL
  1673. MG_EV_READ, // Data received from socket long *bytes_read
  1674. MG_EV_WRITE, // Data written to socket long *bytes_written
  1675. MG_EV_CLOSE, // Connection closed NULL
  1676. MG_EV_HTTP_MSG, // HTTP request/response struct mg_http_message *
  1677. MG_EV_WS_OPEN, // Websocket handshake done struct mg_http_message *
  1678. MG_EV_WS_MSG, // Websocket msg, text or bin struct mg_ws_message *
  1679. MG_EV_WS_CTL, // Websocket control msg struct mg_ws_message *
  1680. MG_EV_MQTT_CMD, // MQTT low-level command struct mg_mqtt_message *
  1681. MG_EV_MQTT_MSG, // MQTT PUBLISH received struct mg_mqtt_message *
  1682. MG_EV_MQTT_OPEN, // MQTT CONNACK received int *connack_status_code
  1683. MG_EV_SNTP_TIME, // SNTP time received uint64_t *epoch_millis
  1684. MG_EV_WAKEUP, // mg_wakeup() data received struct mg_str *data
  1685. MG_EV_USER // Starting ID for user events
  1686. };
  1687. struct mg_dns {
  1688. const char *url; // DNS server URL
  1689. struct mg_connection *c; // DNS server connection
  1690. };
  1691. struct mg_addr {
  1692. uint8_t ip[16]; // Holds IPv4 or IPv6 address, in network byte order
  1693. uint16_t port; // TCP or UDP port in network byte order
  1694. uint8_t scope_id; // IPv6 scope ID
  1695. bool is_ip6; // True when address is IPv6 address
  1696. };
  1697. struct mg_mgr {
  1698. struct mg_connection *conns; // List of active connections
  1699. struct mg_dns dns4; // DNS for IPv4
  1700. struct mg_dns dns6; // DNS for IPv6
  1701. int dnstimeout; // DNS resolve timeout in milliseconds
  1702. bool use_dns6; // Use DNS6 server by default, see #1532
  1703. unsigned long nextid; // Next connection ID
  1704. unsigned long timerid; // Next timer ID
  1705. void *userdata; // Arbitrary user data pointer
  1706. void *tls_ctx; // TLS context shared by all TLS sessions
  1707. uint16_t mqtt_id; // MQTT IDs for pub/sub
  1708. void *active_dns_requests; // DNS requests in progress
  1709. struct mg_timer *timers; // Active timers
  1710. int epoll_fd; // Used when MG_EPOLL_ENABLE=1
  1711. void *priv; // Used by the MIP stack
  1712. size_t extraconnsize; // Used by the MIP stack
  1713. MG_SOCKET_TYPE pipe; // Socketpair end for mg_wakeup()
  1714. #if MG_ENABLE_FREERTOS_TCP
  1715. SocketSet_t ss; // NOTE(lsm): referenced from socket struct
  1716. #endif
  1717. };
  1718. struct mg_connection {
  1719. struct mg_connection *next; // Linkage in struct mg_mgr :: connections
  1720. struct mg_mgr *mgr; // Our container
  1721. struct mg_addr loc; // Local address
  1722. struct mg_addr rem; // Remote address
  1723. void *fd; // Connected socket, or LWIP data
  1724. unsigned long id; // Auto-incrementing unique connection ID
  1725. struct mg_iobuf recv; // Incoming data
  1726. struct mg_iobuf send; // Outgoing data
  1727. struct mg_iobuf prof; // Profile data enabled by MG_ENABLE_PROFILE
  1728. struct mg_iobuf rtls; // TLS only. Incoming encrypted data
  1729. mg_event_handler_t fn; // User-specified event handler function
  1730. void *fn_data; // User-specified function parameter
  1731. mg_event_handler_t pfn; // Protocol-specific handler function
  1732. void *pfn_data; // Protocol-specific function parameter
  1733. char data[MG_DATA_SIZE]; // Arbitrary connection data
  1734. void *tls; // TLS specific data
  1735. unsigned is_listening : 1; // Listening connection
  1736. unsigned is_client : 1; // Outbound (client) connection
  1737. unsigned is_accepted : 1; // Accepted (server) connection
  1738. unsigned is_resolving : 1; // Non-blocking DNS resolution is in progress
  1739. unsigned is_arplooking : 1; // Non-blocking ARP resolution is in progress
  1740. unsigned is_connecting : 1; // Non-blocking connect is in progress
  1741. unsigned is_tls : 1; // TLS-enabled connection
  1742. unsigned is_tls_hs : 1; // TLS handshake is in progress
  1743. unsigned is_udp : 1; // UDP connection
  1744. unsigned is_websocket : 1; // WebSocket connection
  1745. unsigned is_mqtt5 : 1; // For MQTT connection, v5 indicator
  1746. unsigned is_hexdumping : 1; // Hexdump in/out traffic
  1747. unsigned is_draining : 1; // Send remaining data, then close and free
  1748. unsigned is_closing : 1; // Close and free the connection immediately
  1749. unsigned is_full : 1; // Stop reads, until cleared
  1750. unsigned is_resp : 1; // Response is still being generated
  1751. unsigned is_readable : 1; // Connection is ready to read
  1752. unsigned is_writable : 1; // Connection is ready to write
  1753. };
  1754. void mg_mgr_poll(struct mg_mgr *, int ms);
  1755. void mg_mgr_init(struct mg_mgr *);
  1756. void mg_mgr_free(struct mg_mgr *);
  1757. struct mg_connection *mg_listen(struct mg_mgr *, const char *url,
  1758. mg_event_handler_t fn, void *fn_data);
  1759. struct mg_connection *mg_connect(struct mg_mgr *, const char *url,
  1760. mg_event_handler_t fn, void *fn_data);
  1761. struct mg_connection *mg_wrapfd(struct mg_mgr *mgr, int fd,
  1762. mg_event_handler_t fn, void *fn_data);
  1763. void mg_connect_resolved(struct mg_connection *);
  1764. bool mg_send(struct mg_connection *, const void *, size_t);
  1765. size_t mg_printf(struct mg_connection *, const char *fmt, ...);
  1766. size_t mg_vprintf(struct mg_connection *, const char *fmt, va_list *ap);
  1767. bool mg_aton(struct mg_str str, struct mg_addr *addr);
  1768. // These functions are used to integrate with custom network stacks
  1769. struct mg_connection *mg_alloc_conn(struct mg_mgr *);
  1770. void mg_close_conn(struct mg_connection *c);
  1771. bool mg_open_listener(struct mg_connection *c, const char *url);
  1772. // Utility functions
  1773. bool mg_wakeup(struct mg_mgr *, unsigned long id, const void *buf, size_t len);
  1774. bool mg_wakeup_init(struct mg_mgr *);
  1775. struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
  1776. unsigned flags, void (*fn)(void *), void *arg);
  1777. struct mg_http_header {
  1778. struct mg_str name; // Header name
  1779. struct mg_str value; // Header value
  1780. };
  1781. struct mg_http_message {
  1782. struct mg_str method, uri, query, proto; // Request/response line
  1783. struct mg_http_header headers[MG_MAX_HTTP_HEADERS]; // Headers
  1784. struct mg_str body; // Body
  1785. struct mg_str head; // Request + headers
  1786. struct mg_str message; // Request + headers + body
  1787. };
  1788. // Parameter for mg_http_serve_dir()
  1789. struct mg_http_serve_opts {
  1790. const char *root_dir; // Web root directory, must be non-NULL
  1791. const char *ssi_pattern; // SSI file name pattern, e.g. #.shtml
  1792. const char *extra_headers; // Extra HTTP headers to add in responses
  1793. const char *mime_types; // Extra mime types, ext1=type1,ext2=type2,..
  1794. const char *page404; // Path to the 404 page, or NULL by default
  1795. struct mg_fs *fs; // Filesystem implementation. Use NULL for POSIX
  1796. };
  1797. // Parameter for mg_http_next_multipart
  1798. struct mg_http_part {
  1799. struct mg_str name; // Form field name
  1800. struct mg_str filename; // Filename for file uploads
  1801. struct mg_str body; // Part contents
  1802. };
  1803. int mg_http_parse(const char *s, size_t len, struct mg_http_message *);
  1804. int mg_http_get_request_len(const unsigned char *buf, size_t buf_len);
  1805. void mg_http_printf_chunk(struct mg_connection *cnn, const char *fmt, ...);
  1806. void mg_http_write_chunk(struct mg_connection *c, const char *buf, size_t len);
  1807. void mg_http_delete_chunk(struct mg_connection *c, struct mg_http_message *hm);
  1808. struct mg_connection *mg_http_listen(struct mg_mgr *, const char *url,
  1809. mg_event_handler_t fn, void *fn_data);
  1810. struct mg_connection *mg_http_connect(struct mg_mgr *, const char *url,
  1811. mg_event_handler_t fn, void *fn_data);
  1812. void mg_http_serve_dir(struct mg_connection *, struct mg_http_message *hm,
  1813. const struct mg_http_serve_opts *);
  1814. void mg_http_serve_file(struct mg_connection *, struct mg_http_message *hm,
  1815. const char *path, const struct mg_http_serve_opts *);
  1816. void mg_http_reply(struct mg_connection *, int status_code, const char *headers,
  1817. const char *body_fmt, ...);
  1818. struct mg_str *mg_http_get_header(struct mg_http_message *, const char *name);
  1819. struct mg_str mg_http_var(struct mg_str buf, struct mg_str name);
  1820. int mg_http_get_var(const struct mg_str *, const char *name, char *, size_t);
  1821. int mg_url_decode(const char *s, size_t n, char *to, size_t to_len, int form);
  1822. size_t mg_url_encode(const char *s, size_t n, char *buf, size_t len);
  1823. void mg_http_creds(struct mg_http_message *, char *, size_t, char *, size_t);
  1824. bool mg_http_match_uri(const struct mg_http_message *, const char *glob);
  1825. long mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
  1826. struct mg_fs *fs, const char *path, size_t max_size);
  1827. void mg_http_bauth(struct mg_connection *, const char *user, const char *pass);
  1828. struct mg_str mg_http_get_header_var(struct mg_str s, struct mg_str v);
  1829. size_t mg_http_next_multipart(struct mg_str, size_t, struct mg_http_part *);
  1830. int mg_http_status(const struct mg_http_message *hm);
  1831. void mg_hello(const char *url);
  1832. void mg_http_serve_ssi(struct mg_connection *c, const char *root,
  1833. const char *fullpath);
  1834. #define MG_TLS_NONE 0 // No TLS support
  1835. #define MG_TLS_MBED 1 // mbedTLS
  1836. #define MG_TLS_OPENSSL 2 // OpenSSL
  1837. #define MG_TLS_BUILTIN 3 // Built-in
  1838. #define MG_TLS_CUSTOM 4 // Custom implementation
  1839. #ifndef MG_TLS
  1840. #define MG_TLS MG_TLS_NONE
  1841. #endif
  1842. struct mg_tls_opts {
  1843. struct mg_str ca; // PEM or DER
  1844. struct mg_str cert; // PEM or DER
  1845. struct mg_str key; // PEM or DER
  1846. struct mg_str name; // If not empty, enable host name verification
  1847. };
  1848. void mg_tls_init(struct mg_connection *, const struct mg_tls_opts *opts);
  1849. void mg_tls_free(struct mg_connection *);
  1850. long mg_tls_send(struct mg_connection *, const void *buf, size_t len);
  1851. long mg_tls_recv(struct mg_connection *, void *buf, size_t len);
  1852. size_t mg_tls_pending(struct mg_connection *);
  1853. void mg_tls_handshake(struct mg_connection *);
  1854. // Private
  1855. void mg_tls_ctx_init(struct mg_mgr *);
  1856. void mg_tls_ctx_free(struct mg_mgr *);
  1857. // Low-level IO primives used by TLS layer
  1858. enum { MG_IO_ERR = -1, MG_IO_WAIT = -2, MG_IO_RESET = -3 };
  1859. long mg_io_send(struct mg_connection *c, const void *buf, size_t len);
  1860. long mg_io_recv(struct mg_connection *c, void *buf, size_t len);
  1861. #if MG_TLS == MG_TLS_MBED
  1862. #include <mbedtls/debug.h>
  1863. #include <mbedtls/net_sockets.h>
  1864. #include <mbedtls/ssl.h>
  1865. #include <mbedtls/ssl_ticket.h>
  1866. struct mg_tls_ctx {
  1867. int dummy;
  1868. #ifdef MBEDTLS_SSL_SESSION_TICKETS
  1869. mbedtls_ssl_ticket_context tickets;
  1870. #endif
  1871. };
  1872. struct mg_tls {
  1873. mbedtls_x509_crt ca; // Parsed CA certificate
  1874. mbedtls_x509_crt cert; // Parsed certificate
  1875. mbedtls_pk_context pk; // Private key context
  1876. mbedtls_ssl_context ssl; // SSL/TLS context
  1877. mbedtls_ssl_config conf; // SSL-TLS config
  1878. #ifdef MBEDTLS_SSL_SESSION_TICKETS
  1879. mbedtls_ssl_ticket_context ticket; // Session tickets context
  1880. #endif
  1881. };
  1882. #endif
  1883. #if MG_TLS == MG_TLS_OPENSSL
  1884. #include <openssl/err.h>
  1885. #include <openssl/ssl.h>
  1886. struct mg_tls {
  1887. BIO_METHOD *bm;
  1888. SSL_CTX *ctx;
  1889. SSL *ssl;
  1890. };
  1891. #endif
  1892. #define WEBSOCKET_OP_CONTINUE 0
  1893. #define WEBSOCKET_OP_TEXT 1
  1894. #define WEBSOCKET_OP_BINARY 2
  1895. #define WEBSOCKET_OP_CLOSE 8
  1896. #define WEBSOCKET_OP_PING 9
  1897. #define WEBSOCKET_OP_PONG 10
  1898. struct mg_ws_message {
  1899. struct mg_str data; // Websocket message data
  1900. uint8_t flags; // Websocket message flags
  1901. };
  1902. struct mg_connection *mg_ws_connect(struct mg_mgr *, const char *url,
  1903. mg_event_handler_t fn, void *fn_data,
  1904. const char *fmt, ...);
  1905. void mg_ws_upgrade(struct mg_connection *, struct mg_http_message *,
  1906. const char *fmt, ...);
  1907. size_t mg_ws_send(struct mg_connection *, const void *buf, size_t len, int op);
  1908. size_t mg_ws_wrap(struct mg_connection *, size_t len, int op);
  1909. size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...);
  1910. size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt,
  1911. va_list *);
  1912. struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
  1913. mg_event_handler_t fn, void *fn_data);
  1914. void mg_sntp_request(struct mg_connection *c);
  1915. int64_t mg_sntp_parse(const unsigned char *buf, size_t len);
  1916. #define MQTT_CMD_CONNECT 1
  1917. #define MQTT_CMD_CONNACK 2
  1918. #define MQTT_CMD_PUBLISH 3
  1919. #define MQTT_CMD_PUBACK 4
  1920. #define MQTT_CMD_PUBREC 5
  1921. #define MQTT_CMD_PUBREL 6
  1922. #define MQTT_CMD_PUBCOMP 7
  1923. #define MQTT_CMD_SUBSCRIBE 8
  1924. #define MQTT_CMD_SUBACK 9
  1925. #define MQTT_CMD_UNSUBSCRIBE 10
  1926. #define MQTT_CMD_UNSUBACK 11
  1927. #define MQTT_CMD_PINGREQ 12
  1928. #define MQTT_CMD_PINGRESP 13
  1929. #define MQTT_CMD_DISCONNECT 14
  1930. #define MQTT_CMD_AUTH 15
  1931. #define MQTT_PROP_PAYLOAD_FORMAT_INDICATOR 0x01
  1932. #define MQTT_PROP_MESSAGE_EXPIRY_INTERVAL 0x02
  1933. #define MQTT_PROP_CONTENT_TYPE 0x03
  1934. #define MQTT_PROP_RESPONSE_TOPIC 0x08
  1935. #define MQTT_PROP_CORRELATION_DATA 0x09
  1936. #define MQTT_PROP_SUBSCRIPTION_IDENTIFIER 0x0B
  1937. #define MQTT_PROP_SESSION_EXPIRY_INTERVAL 0x11
  1938. #define MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER 0x12
  1939. #define MQTT_PROP_SERVER_KEEP_ALIVE 0x13
  1940. #define MQTT_PROP_AUTHENTICATION_METHOD 0x15
  1941. #define MQTT_PROP_AUTHENTICATION_DATA 0x16
  1942. #define MQTT_PROP_REQUEST_PROBLEM_INFORMATION 0x17
  1943. #define MQTT_PROP_WILL_DELAY_INTERVAL 0x18
  1944. #define MQTT_PROP_REQUEST_RESPONSE_INFORMATION 0x19
  1945. #define MQTT_PROP_RESPONSE_INFORMATION 0x1A
  1946. #define MQTT_PROP_SERVER_REFERENCE 0x1C
  1947. #define MQTT_PROP_REASON_STRING 0x1F
  1948. #define MQTT_PROP_RECEIVE_MAXIMUM 0x21
  1949. #define MQTT_PROP_TOPIC_ALIAS_MAXIMUM 0x22
  1950. #define MQTT_PROP_TOPIC_ALIAS 0x23
  1951. #define MQTT_PROP_MAXIMUM_QOS 0x24
  1952. #define MQTT_PROP_RETAIN_AVAILABLE 0x25
  1953. #define MQTT_PROP_USER_PROPERTY 0x26
  1954. #define MQTT_PROP_MAXIMUM_PACKET_SIZE 0x27
  1955. #define MQTT_PROP_WILDCARD_SUBSCRIPTION_AVAILABLE 0x28
  1956. #define MQTT_PROP_SUBSCRIPTION_IDENTIFIER_AVAILABLE 0x29
  1957. #define MQTT_PROP_SHARED_SUBSCRIPTION_AVAILABLE 0x2A
  1958. enum {
  1959. MQTT_PROP_TYPE_BYTE,
  1960. MQTT_PROP_TYPE_STRING,
  1961. MQTT_PROP_TYPE_STRING_PAIR,
  1962. MQTT_PROP_TYPE_BINARY_DATA,
  1963. MQTT_PROP_TYPE_VARIABLE_INT,
  1964. MQTT_PROP_TYPE_INT,
  1965. MQTT_PROP_TYPE_SHORT
  1966. };
  1967. enum { MQTT_OK, MQTT_INCOMPLETE, MQTT_MALFORMED };
  1968. struct mg_mqtt_prop {
  1969. uint8_t id; // Enumerated at MQTT5 Reference
  1970. uint32_t iv; // Integer value for 8-, 16-, 32-bit integers types
  1971. struct mg_str key; // Non-NULL only for user property type
  1972. struct mg_str val; // Non-NULL only for UTF-8 types and user properties
  1973. };
  1974. struct mg_mqtt_opts {
  1975. struct mg_str user; // Username, can be empty
  1976. struct mg_str pass; // Password, can be empty
  1977. struct mg_str client_id; // Client ID
  1978. struct mg_str topic; // message/subscription topic
  1979. struct mg_str message; // message content
  1980. uint8_t qos; // message quality of service
  1981. uint8_t version; // Can be 4 (3.1.1), or 5. If 0, assume 4
  1982. uint16_t keepalive; // Keep-alive timer in seconds
  1983. bool retain; // Retain flag
  1984. bool clean; // Clean session flag
  1985. struct mg_mqtt_prop *props; // MQTT5 props array
  1986. size_t num_props; // number of props
  1987. struct mg_mqtt_prop *will_props; // Valid only for CONNECT packet (MQTT5)
  1988. size_t num_will_props; // Number of will props
  1989. };
  1990. struct mg_mqtt_message {
  1991. struct mg_str topic; // Parsed topic for PUBLISH
  1992. struct mg_str data; // Parsed message for PUBLISH
  1993. struct mg_str dgram; // Whole MQTT packet, including headers
  1994. uint16_t id; // For PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, PUBLISH
  1995. uint8_t cmd; // MQTT command, one of MQTT_CMD_*
  1996. uint8_t qos; // Quality of service
  1997. uint8_t ack; // CONNACK return code, 0 = success
  1998. size_t props_start; // Offset to the start of the properties (MQTT5)
  1999. size_t props_size; // Length of the properties
  2000. };
  2001. struct mg_connection *mg_mqtt_connect(struct mg_mgr *, const char *url,
  2002. const struct mg_mqtt_opts *opts,
  2003. mg_event_handler_t fn, void *fn_data);
  2004. struct mg_connection *mg_mqtt_listen(struct mg_mgr *mgr, const char *url,
  2005. mg_event_handler_t fn, void *fn_data);
  2006. void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts);
  2007. void mg_mqtt_pub(struct mg_connection *c, const struct mg_mqtt_opts *opts);
  2008. void mg_mqtt_sub(struct mg_connection *, const struct mg_mqtt_opts *opts);
  2009. int mg_mqtt_parse(const uint8_t *, size_t, uint8_t, struct mg_mqtt_message *);
  2010. void mg_mqtt_send_header(struct mg_connection *, uint8_t cmd, uint8_t flags,
  2011. uint32_t len);
  2012. void mg_mqtt_ping(struct mg_connection *);
  2013. void mg_mqtt_pong(struct mg_connection *);
  2014. void mg_mqtt_disconnect(struct mg_connection *, const struct mg_mqtt_opts *);
  2015. size_t mg_mqtt_next_prop(struct mg_mqtt_message *, struct mg_mqtt_prop *,
  2016. size_t ofs);
  2017. // Mongoose sends DNS queries that contain only one question:
  2018. // either A (IPv4) or AAAA (IPv6) address lookup.
  2019. // Therefore, we expect zero or one answer.
  2020. // If `resolved` is true, then `addr` contains resolved IPv4 or IPV6 address.
  2021. struct mg_dns_message {
  2022. uint16_t txnid; // Transaction ID
  2023. bool resolved; // Resolve successful, addr is set
  2024. struct mg_addr addr; // Resolved address
  2025. char name[256]; // Host name
  2026. };
  2027. struct mg_dns_header {
  2028. uint16_t txnid; // Transaction ID
  2029. uint16_t flags;
  2030. uint16_t num_questions;
  2031. uint16_t num_answers;
  2032. uint16_t num_authority_prs;
  2033. uint16_t num_other_prs;
  2034. };
  2035. // DNS resource record
  2036. struct mg_dns_rr {
  2037. uint16_t nlen; // Name or pointer length
  2038. uint16_t atype; // Address type
  2039. uint16_t aclass; // Address class
  2040. uint16_t alen; // Address length
  2041. };
  2042. void mg_resolve(struct mg_connection *, const char *url);
  2043. void mg_resolve_cancel(struct mg_connection *);
  2044. bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *);
  2045. size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs,
  2046. bool is_question, struct mg_dns_rr *);
  2047. #ifndef MG_JSON_MAX_DEPTH
  2048. #define MG_JSON_MAX_DEPTH 30
  2049. #endif
  2050. // Error return values - negative. Successful returns are >= 0
  2051. enum { MG_JSON_TOO_DEEP = -1, MG_JSON_INVALID = -2, MG_JSON_NOT_FOUND = -3 };
  2052. int mg_json_get(struct mg_str json, const char *path, int *toklen);
  2053. bool mg_json_get_num(struct mg_str json, const char *path, double *v);
  2054. bool mg_json_get_bool(struct mg_str json, const char *path, bool *v);
  2055. long mg_json_get_long(struct mg_str json, const char *path, long dflt);
  2056. char *mg_json_get_str(struct mg_str json, const char *path);
  2057. char *mg_json_get_hex(struct mg_str json, const char *path, int *len);
  2058. char *mg_json_get_b64(struct mg_str json, const char *path, int *len);
  2059. bool mg_json_unescape(struct mg_str str, char *buf, size_t len);
  2060. size_t mg_json_next(struct mg_str obj, size_t ofs, struct mg_str *key,
  2061. struct mg_str *val);
  2062. // JSON-RPC request descriptor
  2063. struct mg_rpc_req {
  2064. struct mg_rpc **head; // RPC handlers list head
  2065. struct mg_rpc *rpc; // RPC handler being called
  2066. mg_pfn_t pfn; // Response printing function
  2067. void *pfn_data; // Response printing function data
  2068. void *req_data; // Arbitrary request data
  2069. struct mg_str frame; // Request, e.g. {"id":1,"method":"add","params":[1,2]}
  2070. };
  2071. // JSON-RPC method handler
  2072. struct mg_rpc {
  2073. struct mg_rpc *next; // Next in list
  2074. struct mg_str method; // Method pattern
  2075. void (*fn)(struct mg_rpc_req *); // Handler function
  2076. void *fn_data; // Handler function argument
  2077. };
  2078. void mg_rpc_add(struct mg_rpc **head, struct mg_str method_pattern,
  2079. void (*handler)(struct mg_rpc_req *), void *handler_data);
  2080. void mg_rpc_del(struct mg_rpc **head, void (*handler)(struct mg_rpc_req *));
  2081. void mg_rpc_process(struct mg_rpc_req *);
  2082. // Helper functions to print result or error frame
  2083. void mg_rpc_ok(struct mg_rpc_req *, const char *fmt, ...);
  2084. void mg_rpc_vok(struct mg_rpc_req *, const char *fmt, va_list *ap);
  2085. void mg_rpc_err(struct mg_rpc_req *, int code, const char *fmt, ...);
  2086. void mg_rpc_verr(struct mg_rpc_req *, int code, const char *fmt, va_list *);
  2087. void mg_rpc_list(struct mg_rpc_req *r);
  2088. // Copyright (c) 2023 Cesanta Software Limited
  2089. // All rights reserved
  2090. #define MG_OTA_NONE 0 // No OTA support
  2091. #define MG_OTA_FLASH 1 // OTA via an internal flash
  2092. #define MG_OTA_CUSTOM 100 // Custom implementation
  2093. #ifndef MG_OTA
  2094. #define MG_OTA MG_OTA_NONE
  2095. #endif
  2096. #if defined(__GNUC__) && !defined(__APPLE__)
  2097. #define MG_IRAM __attribute__((section(".iram")))
  2098. #else
  2099. #define MG_IRAM
  2100. #endif
  2101. // Firmware update API
  2102. bool mg_ota_begin(size_t new_firmware_size); // Start writing
  2103. bool mg_ota_write(const void *buf, size_t len); // Write chunk, aligned to 1k
  2104. bool mg_ota_end(void); // Stop writing
  2105. enum {
  2106. MG_OTA_UNAVAILABLE = 0, // No OTA information is present
  2107. MG_OTA_FIRST_BOOT = 1, // Device booting the first time after the OTA
  2108. MG_OTA_UNCOMMITTED = 2, // Ditto, but marking us for the rollback
  2109. MG_OTA_COMMITTED = 3 // The firmware is good
  2110. };
  2111. enum { MG_FIRMWARE_CURRENT = 0, MG_FIRMWARE_PREVIOUS = 1 };
  2112. int mg_ota_status(int firmware); // Return firmware status MG_OTA_*
  2113. uint32_t mg_ota_crc32(int firmware); // Return firmware checksum
  2114. uint32_t mg_ota_timestamp(int firmware); // Firmware timestamp, UNIX UTC epoch
  2115. size_t mg_ota_size(int firmware); // Firmware size
  2116. bool mg_ota_commit(void); // Commit current firmware
  2117. bool mg_ota_rollback(void); // Rollback to the previous firmware
  2118. MG_IRAM void mg_ota_boot(void); // Bootloader function
  2119. // Copyright (c) 2023 Cesanta Software Limited
  2120. // All rights reserved
  2121. #define MG_DEVICE_NONE 0 // Dummy system
  2122. #define MG_DEVICE_STM32H5 1 // STM32 H5
  2123. #define MG_DEVICE_STM32H7 2 // STM32 H7
  2124. #define MG_DEVICE_CH32V307 100 // WCH CH32V307
  2125. #define MG_DEVICE_CUSTOM 1000 // Custom implementation
  2126. #ifndef MG_DEVICE
  2127. #define MG_DEVICE MG_DEVICE_NONE
  2128. #endif
  2129. // Flash information
  2130. void *mg_flash_start(void); // Return flash start address
  2131. size_t mg_flash_size(void); // Return flash size
  2132. size_t mg_flash_sector_size(void); // Return flash sector size
  2133. size_t mg_flash_write_align(void); // Return flash write align, minimum 4
  2134. int mg_flash_bank(void); // 0: not dual bank, 1: bank1, 2: bank2
  2135. // Write, erase, swap bank
  2136. bool mg_flash_write(void *addr, const void *buf, size_t len);
  2137. bool mg_flash_erase(void *sector);
  2138. bool mg_flash_swap_bank(void);
  2139. // Convenience functions to store data on a flash sector with wear levelling
  2140. // If `sector` is NULL, then the last sector of flash is used
  2141. bool mg_flash_load(void *sector, uint32_t key, void *buf, size_t len);
  2142. bool mg_flash_save(void *sector, uint32_t key, const void *buf, size_t len);
  2143. void mg_device_reset(void); // Reboot device immediately
  2144. #if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP
  2145. struct mg_tcpip_if; // Mongoose TCP/IP network interface
  2146. struct mg_tcpip_driver {
  2147. bool (*init)(struct mg_tcpip_if *); // Init driver
  2148. size_t (*tx)(const void *, size_t, struct mg_tcpip_if *); // Transmit frame
  2149. size_t (*rx)(void *buf, size_t len, struct mg_tcpip_if *); // Receive frame
  2150. bool (*up)(struct mg_tcpip_if *); // Up/down status
  2151. };
  2152. // Network interface
  2153. struct mg_tcpip_if {
  2154. uint8_t mac[6]; // MAC address. Must be set to a valid MAC
  2155. uint32_t ip, mask, gw; // IP address, mask, default gateway
  2156. struct mg_str tx; // Output (TX) buffer
  2157. bool enable_dhcp_client; // Enable DCHP client
  2158. bool enable_dhcp_server; // Enable DCHP server
  2159. bool enable_get_gateway; // DCHP server sets client as gateway
  2160. bool enable_crc32_check; // Do a CRC check on RX frames and strip it
  2161. bool enable_mac_check; // Do a MAC check on RX frames
  2162. struct mg_tcpip_driver *driver; // Low level driver
  2163. void *driver_data; // Driver-specific data
  2164. struct mg_mgr *mgr; // Mongoose event manager
  2165. struct mg_queue recv_queue; // Receive queue
  2166. uint16_t mtu; // Interface MTU
  2167. #define MG_TCPIP_MTU_DEFAULT 1500
  2168. // Internal state, user can use it but should not change it
  2169. uint8_t gwmac[6]; // Router's MAC
  2170. uint64_t now; // Current time
  2171. uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state
  2172. uint64_t lease_expire; // Lease expiration time, in ms
  2173. uint16_t eport; // Next ephemeral port
  2174. volatile uint32_t ndrop; // Number of received, but dropped frames
  2175. volatile uint32_t nrecv; // Number of received frames
  2176. volatile uint32_t nsent; // Number of transmitted frames
  2177. volatile uint32_t nerr; // Number of driver errors
  2178. uint8_t state; // Current state
  2179. #define MG_TCPIP_STATE_DOWN 0 // Interface is down
  2180. #define MG_TCPIP_STATE_UP 1 // Interface is up
  2181. #define MG_TCPIP_STATE_REQ 2 // Interface is up and has requested an IP
  2182. #define MG_TCPIP_STATE_READY 3 // Interface is up and has an IP assigned
  2183. };
  2184. void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *);
  2185. void mg_tcpip_free(struct mg_tcpip_if *);
  2186. void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp);
  2187. extern struct mg_tcpip_driver mg_tcpip_driver_stm32f;
  2188. extern struct mg_tcpip_driver mg_tcpip_driver_w5500;
  2189. extern struct mg_tcpip_driver mg_tcpip_driver_tm4c;
  2190. extern struct mg_tcpip_driver mg_tcpip_driver_stm32h;
  2191. extern struct mg_tcpip_driver mg_tcpip_driver_imxrt;
  2192. extern struct mg_tcpip_driver mg_tcpip_driver_same54;
  2193. extern struct mg_tcpip_driver mg_tcpip_driver_cmsis;
  2194. // Drivers that require SPI, can use this SPI abstraction
  2195. struct mg_tcpip_spi {
  2196. void *spi; // Opaque SPI bus descriptor
  2197. void (*begin)(void *); // SPI begin: slave select low
  2198. void (*end)(void *); // SPI end: slave select high
  2199. uint8_t (*txn)(void *, uint8_t); // SPI transaction: write 1 byte, read reply
  2200. };
  2201. #endif
  2202. // Macros to record timestamped events that happens with a connection.
  2203. // They are saved into a c->prof IO buffer, each event is a name and a 32-bit
  2204. // timestamp in milliseconds since connection init time.
  2205. //
  2206. // Test (run in two separate terminals):
  2207. // make -C examples/http-server/ CFLAGS_EXTRA=-DMG_ENABLE_PROFILE=1
  2208. // curl localhost:8000
  2209. // Output:
  2210. // 1ea1f1e7 2 net.c:150:mg_close_conn 3 profile:
  2211. // 1ea1f1e8 2 net.c:150:mg_close_conn 1ea1f1e6 init
  2212. // 1ea1f1e8 2 net.c:150:mg_close_conn 0 EV_OPEN
  2213. // 1ea1f1e8 2 net.c:150:mg_close_conn 0 EV_ACCEPT
  2214. // 1ea1f1e8 2 net.c:150:mg_close_conn 0 EV_READ
  2215. // 1ea1f1e8 2 net.c:150:mg_close_conn 0 EV_HTTP_MSG
  2216. // 1ea1f1e8 2 net.c:150:mg_close_conn 0 EV_WRITE
  2217. // 1ea1f1e8 2 net.c:150:mg_close_conn 1 EV_CLOSE
  2218. //
  2219. // Usage:
  2220. // Enable profiling by setting MG_ENABLE_PROFILE=1
  2221. // Invoke MG_PROF_ADD(c, "MY_EVENT_1") in the places you'd like to measure
  2222. #if MG_ENABLE_PROFILE
  2223. struct mg_profitem {
  2224. const char *name; // Event name
  2225. uint32_t timestamp; // Milliseconds since connection creation (MG_EV_OPEN)
  2226. };
  2227. #define MG_PROFILE_ALLOC_GRANULARITY 256 // Can save 32 items wih to realloc
  2228. // Adding a profile item to the c->prof. Must be as fast as possible.
  2229. // Reallocation of the c->prof iobuf is not desirable here, that's why we
  2230. // pre-allocate c->prof with MG_PROFILE_ALLOC_GRANULARITY.
  2231. // This macro just inits and copies 8 bytes, and calls mg_millis(),
  2232. // which should be fast enough.
  2233. #define MG_PROF_ADD(c, name_) \
  2234. do { \
  2235. struct mg_iobuf *io = &c->prof; \
  2236. uint32_t inittime = ((struct mg_profitem *) io->buf)->timestamp; \
  2237. struct mg_profitem item = {name_, (uint32_t) mg_millis() - inittime}; \
  2238. mg_iobuf_add(io, io->len, &item, sizeof(item)); \
  2239. } while (0)
  2240. // Initialising profile for a new connection. Not time sensitive
  2241. #define MG_PROF_INIT(c) \
  2242. do { \
  2243. struct mg_profitem first = {"init", (uint32_t) mg_millis()}; \
  2244. mg_iobuf_init(&(c)->prof, 0, MG_PROFILE_ALLOC_GRANULARITY); \
  2245. mg_iobuf_add(&c->prof, c->prof.len, &first, sizeof(first)); \
  2246. } while (0)
  2247. #define MG_PROF_FREE(c) mg_iobuf_free(&(c)->prof)
  2248. // Dumping the profile. Not time sensitive
  2249. #define MG_PROF_DUMP(c) \
  2250. do { \
  2251. struct mg_iobuf *io = &c->prof; \
  2252. struct mg_profitem *p = (struct mg_profitem *) io->buf; \
  2253. struct mg_profitem *e = &p[io->len / sizeof(*p)]; \
  2254. MG_INFO(("%lu profile:", c->id)); \
  2255. while (p < e) { \
  2256. MG_INFO(("%5lx %s", (unsigned long) p->timestamp, p->name)); \
  2257. p++; \
  2258. } \
  2259. } while (0)
  2260. #else
  2261. #define MG_PROF_INIT(c)
  2262. #define MG_PROF_FREE(c)
  2263. #define MG_PROF_ADD(c, name)
  2264. #define MG_PROF_DUMP(c)
  2265. #endif
  2266. #if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_CMSIS) && MG_ENABLE_DRIVER_CMSIS
  2267. #include "Driver_ETH_MAC.h" // keep this include
  2268. #include "Driver_ETH_PHY.h" // keep this include
  2269. #endif
  2270. struct mg_tcpip_driver_imxrt_data {
  2271. // MDC clock divider. MDC clock is derived from IPS Bus clock (ipg_clk),
  2272. // must not exceed 2.5MHz. Configuration for clock range 2.36~2.50 MHz
  2273. // 37.5.1.8.2, Table 37-46 : f = ipg_clk / (2(mdc_cr + 1))
  2274. // ipg_clk mdc_cr VALUE
  2275. // --------------------------
  2276. // -1 <-- TODO() tell driver to guess the value
  2277. // 25 MHz 4
  2278. // 33 MHz 6
  2279. // 40 MHz 7
  2280. // 50 MHz 9
  2281. // 66 MHz 13
  2282. int mdc_cr; // Valid values: -1 to 63
  2283. uint8_t phy_addr; // PHY address
  2284. };
  2285. struct mg_tcpip_driver_same54_data {
  2286. int mdc_cr;
  2287. };
  2288. struct mg_tcpip_driver_stm32f_data {
  2289. // MDC clock divider. MDC clock is derived from HCLK, must not exceed 2.5MHz
  2290. // HCLK range DIVIDER mdc_cr VALUE
  2291. // -------------------------------------
  2292. // -1 <-- tell driver to guess the value
  2293. // 60-100 MHz HCLK/42 0
  2294. // 100-150 MHz HCLK/62 1
  2295. // 20-35 MHz HCLK/16 2
  2296. // 35-60 MHz HCLK/26 3
  2297. // 150-216 MHz HCLK/102 4 <-- value for Nucleo-F* on max speed
  2298. // 216-310 MHz HCLK/124 5
  2299. // 110, 111 Reserved
  2300. int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
  2301. uint8_t phy_addr; // PHY address
  2302. };
  2303. struct mg_tcpip_driver_stm32h_data {
  2304. // MDC clock divider. MDC clock is derived from HCLK, must not exceed 2.5MHz
  2305. // HCLK range DIVIDER mdc_cr VALUE
  2306. // -------------------------------------
  2307. // -1 <-- tell driver to guess the value
  2308. // 60-100 MHz HCLK/42 0
  2309. // 100-150 MHz HCLK/62 1
  2310. // 20-35 MHz HCLK/16 2
  2311. // 35-60 MHz HCLK/26 3
  2312. // 150-250 MHz HCLK/102 4 <-- value for Nucleo-H* on max speed driven by HSI
  2313. // 250-300 MHz HCLK/124 5 <-- value for Nucleo-H* on max speed driven by CSI
  2314. // 110, 111 Reserved
  2315. int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
  2316. };
  2317. struct mg_tcpip_driver_tm4c_data {
  2318. // MDC clock divider. MDC clock is derived from SYSCLK, must not exceed 2.5MHz
  2319. // SYSCLK range DIVIDER mdc_cr VALUE
  2320. // -------------------------------------
  2321. // -1 <-- tell driver to guess the value
  2322. // 60-100 MHz SYSCLK/42 0
  2323. // 100-150 MHz SYSCLK/62 1 <-- value for EK-TM4C129* on max speed
  2324. // 20-35 MHz SYSCLK/16 2
  2325. // 35-60 MHz SYSCLK/26 3
  2326. // 0x4-0xF Reserved
  2327. int mdc_cr; // Valid values: -1, 0, 1, 2, 3
  2328. };
  2329. #ifdef __cplusplus
  2330. }
  2331. #endif
  2332. #endif // MONGOOSE_H