http_server.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343
  1. #include "lwip/opt.h"
  2. #include "lwip/arch.h"
  3. #include "lwip/api.h"
  4. #include "lwip/tcp.h"
  5. #include "http_server.h"
  6. #include "web_params_api.h"
  7. #include "parameters.h"
  8. #include "urlcode.h"
  9. #include "trap_params.h"
  10. #include "fsdata.c"
  11. #include "settings_api.h"
  12. #include "netconf.h"
  13. #include "common_config.h"
  14. #include "testing.h"
  15. #include "rtc.h"
  16. #include "rng.h"
  17. #include "megatec.h"
  18. #include "log.h"
  19. #include "hal.h"
  20. #ifdef PRINTF_STDLIB
  21. #include <stdio.h>
  22. #endif
  23. #ifdef PRINTF_CUSTOM
  24. #include "tinystdio.h"
  25. #endif
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "FreeRTOS.h"
  29. #include "task.h"
  30. #include "fr_timers.h"
  31. static int fs_open(char *name, struct fs_file *file);
  32. static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len);
  33. static err_t http_sent_history(void *arg, struct tcp_pcb *pcb, u16_t len);
  34. static err_t http_sent_log(void *arg, struct tcp_pcb *pcb, u16_t len);
  35. static void http_sent_log_err(void * arg, err_t err);
  36. static void send_data(struct tcp_pcb *pcb, struct http_state *hs);
  37. static void HTTP_GetUserCookie(uint8_t user_id, char *str, uint8_t *len);
  38. static uint32_t Parse_Content_Length(char *data, uint32_t len);
  39. static void HTTP_SetUserCookie(char *str, uint8_t user_id);
  40. static void HTTP_UpdateUserLoginTime(uint8_t user_id);
  41. static void HTTP_ForceUserLogout(uint8_t user_id);
  42. void LogoutTimerCallback(TimerHandle_t pxTimer);
  43. int HTTP_ChangeUserPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);
  44. SET_PAGE_t SET_PAGE = SET_PAGE_IDLE;
  45. #define SEND_BUF_MAX_LEN 2000
  46. #define RECIVE_BUF_MAX_LEN 1500
  47. char sendBuf[SEND_BUF_MAX_LEN];
  48. uint16_t sendBufLoadLen = 0;
  49. uint16_t printLen = 0;
  50. //char printBuf[1000];
  51. char receiveBuf[RECIVE_BUF_MAX_LEN];
  52. uint16_t receivedBufLen = 0;
  53. #define MAX_POST_REQ_LEN 256
  54. char post_req_data[MAX_POST_REQ_LEN];
  55. uint32_t post_data_count;
  56. uint32_t log_post_reqn;
  57. /* Logout timeout, 30 minutes */
  58. #define WEB_LOGOUT_TIME configTICK_RATE_HZ*60*30
  59. /* Max user active sessions count */
  60. #define WEB_USER_MAX_SESSION_COUNT 5
  61. typedef struct {
  62. char cookie[MAX_WEB_COOKIE_LEN];
  63. TimerHandle_t LogoutTimer;
  64. } auth_session_t;
  65. struct {
  66. //auth_session_t session[WEB_USER_MAX_SESSION_COUNT];
  67. char cookie[MAX_WEB_COOKIE_LEN];
  68. TimerHandle_t LogoutTimer;
  69. } users[MAX_WEB_USERS];
  70. bool Authenticated = false;
  71. /* Level of currently logged-in user */
  72. uint8_t seclevel = 0xFF;
  73. static volatile uint32_t DataFlag2=0;
  74. static volatile uint32_t DataFlag=0;
  75. static volatile uint32_t size =0;
  76. static uint32_t TotalReceived=0;
  77. static volatile uint32_t TotalData=0;
  78. static uint32_t ContentLengthOffset =0, BrowserFlag=0;
  79. static const char Content_Length[17] =
  80. /* Content Length */
  81. {0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c, 0x65, 0x6e, 0x67,0x74, 0x68, 0x3a, 0x20, };
  82. const char HTTP_200_OK[] = "HTTP/1.1 200 OK\r\n\r\n";
  83. /* utf-8 marker to support MS Excel */
  84. const char UTF8_BOM[] = {0xEF, 0xBB, 0xBF, 0x00};
  85. unsigned long log_ptr = 0;
  86. unsigned long log_size = 0;
  87. bool fLogTransInprog = false;
  88. /**
  89. * @brief Общая структура настроек
  90. */
  91. extern SETTINGS_t sSettings;
  92. /**
  93. * @brief closes tcp connection
  94. * @param pcb: pointer to a tcp_pcb struct
  95. * @param hs: pointer to a http_state struct
  96. * @retval
  97. */
  98. static void close_conn(struct tcp_pcb *pcb, struct http_state *hs)
  99. {
  100. tcp_arg(pcb, NULL);
  101. tcp_sent(pcb, NULL);
  102. tcp_recv(pcb, NULL);
  103. mem_free(hs);
  104. tcp_close(pcb);
  105. }
  106. /**
  107. * @brief callback function for handling TCP HTTP traffic
  108. * @param arg: pointer to an argument structure to be passed to callback function
  109. * @param pcb: pointer to a tcp_pcb structure
  110. * @param p: pointer to a packet buffer
  111. * @param err: LwIP error code
  112. * @retval err
  113. */
  114. static err_t http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
  115. {
  116. char *data, *ptr;
  117. struct http_state *hs;
  118. char CookieBuf[50];
  119. char *CookiePtr = NULL;
  120. char name[MAX_WEB_COOKIE_LEN];
  121. char id[MAX_WEB_COOKIE_LEN];
  122. uint8_t nameLen = 0, idLen = 0;
  123. uint32_t DataOffset;
  124. struct fs_file file = {0, 0};
  125. uint32_t i;
  126. hs = arg;
  127. if (err == ERR_OK && p != NULL)
  128. {
  129. tcp_recved(pcb, p->tot_len);
  130. if (hs->file == NULL)
  131. {
  132. data = p->payload;
  133. /*
  134. printLen = p->tot_len;
  135. memcpy(printBuf, p->payload , printLen);
  136. printf(printBuf);
  137. */
  138. receivedBufLen = p->tot_len;
  139. memcpy(receiveBuf, p->payload , receivedBufLen);
  140. receiveBuf[receivedBufLen] = '\0';
  141. // printf("receive %s \r\n", receiveBuf);
  142. /* Get cookie "uname" value */
  143. CookiePtr = strstr(receiveBuf, "uname=");
  144. strncpy(CookieBuf, CookiePtr, 50);
  145. //printf("********CookieBuf1= %s\r\n", CookieBuf);
  146. memset(name, 0, MAX_WEB_COOKIE_LEN);
  147. GetCookieValue(CookieBuf, "uname=", name, &nameLen);
  148. //printf("********CookieBuf2= %s\r\n", CookieBuf);
  149. //printf("********uname= %s\r\n", name);
  150. /* Get cookie "id" value */
  151. CookiePtr = strstr(receiveBuf, "id=");
  152. strncpy(CookieBuf, CookiePtr, 50);
  153. //printf("********CookieBuf1= %s\r\n", CookieBuf);
  154. memset(id, 0, MAX_WEB_COOKIE_LEN);
  155. GetCookieValue(CookieBuf, "id=", id, &idLen);
  156. //printf("********CookieBuf2= %s\r\n", CookieBuf);
  157. //printf("********id= %s\r\n", id);
  158. /* Id of currently logged-in user */
  159. uint8_t user_id;
  160. if( DataFlag == 0 && DataFlag2 == 0 ){
  161. /* Level of currently logged-in user */
  162. seclevel = 0xFF;
  163. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  164. HTTP_GetUserCookie(user_id, CookieBuf, &idLen);
  165. if (strncmp(id, CookieBuf, idLen) == 0 ) {
  166. GetUserLevelInt(user_id, &seclevel);
  167. Authenticated = true;
  168. break;
  169. }
  170. Authenticated = false;
  171. seclevel = 0xFF;
  172. }
  173. }
  174. if (DataFlag >= 1)
  175. Authenticated = true;
  176. else if(DataFlag2 >= 1)
  177. Authenticated = true;
  178. if ( Authenticated == false && sSettings.sRADIUS.Auth_enable == false)
  179. {
  180. HTTP_LOGIN(sendBuf, &sendBufLoadLen);
  181. hs->file = sendBuf;
  182. hs->left = sendBufLoadLen;
  183. send_data(pcb, hs);
  184. tcp_sent(pcb, http_sent);
  185. }
  186. else if ( Authenticated == false )//&& sSettings.sRADIUS.Auth_enable == true
  187. {
  188. if (strncmp(data, "GET /main.css", 13) == 0) // +
  189. {
  190. fs_open("/main.css", &file);
  191. hs->file = file.data;
  192. hs->left = file.len;
  193. send_data(pcb, hs);
  194. tcp_sent(pcb, http_sent);
  195. }
  196. else if (strncmp(data, "GET /rotek.png", 14) == 0) // +
  197. {
  198. fs_open("/rotek.png", &file);
  199. hs->file = file.data;
  200. hs->left = file.len;
  201. send_data(pcb, hs);
  202. tcp_sent(pcb, http_sent);
  203. }
  204. else if (strncmp(data, "GET /favicon.ico", 16) == 0) // ?
  205. {
  206. fs_open("/favicon.ico", &file);
  207. hs->file = file.data;
  208. hs->left = file.len;
  209. send_data(pcb, hs);
  210. tcp_sent(pcb, http_sent);
  211. }
  212. else if (strncmp(data, "GET /role.js", 12) == 0)
  213. {
  214. fs_open("/role.js", &file);
  215. hs->file = file.data;
  216. hs->left = file.len;
  217. send_data(pcb, hs);
  218. tcp_sent(pcb, http_sent);
  219. }
  220. else if ((strncmp(data, "POST /login.cgi", 15) == 0) || (log_post_reqn > 0))
  221. {
  222. uint32_t i, offset = 0, req_data_received = 0;
  223. //printf("request 1: %d\r\n", receivedBufLen);
  224. /* parse packet for Content-length field */
  225. post_data_count = Parse_Content_Length(data, p->tot_len);
  226. //printf("Content-length: %d\r\n", (int)post_data_count);
  227. if (post_data_count < MAX_POST_REQ_LEN) {
  228. memset(post_req_data, 0, MAX_POST_REQ_LEN);
  229. /* parse packet for "\r\n\r\n" */
  230. for (i = 0; i < receivedBufLen; i++)
  231. {
  232. if (strncmp ((char*)(data+i), "\r\n\r\n", 4) == 0)
  233. {
  234. offset = i+4;
  235. //printf("offset: %d\r\n", (int)offset);
  236. break;
  237. }
  238. }
  239. req_data_received = receivedBufLen - offset;
  240. //printf("req data received: %d\r\n", (int)req_data_received);
  241. /* Check if "\r\n\r\n" was found */
  242. if (offset != 0) {
  243. /* if data was splited in two packets */
  244. if (req_data_received < post_data_count) {
  245. /* Copy request data to buffer */
  246. snprintf(post_req_data, req_data_received, "%s", receiveBuf);
  247. //printf("copied: %d\r\n", (int)req_data_received);
  248. post_data_count -= req_data_received;
  249. }
  250. /* if data received completely */
  251. else {
  252. strncat(post_req_data, (char *)(data + offset), post_data_count);
  253. //printf("post_req_data: %s\r\n", post_req_data);
  254. if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES) {
  255. hs->file = sendBuf;
  256. hs->left = sendBufLoadLen;
  257. send_data(pcb, hs);
  258. tcp_sent(pcb, http_sent);
  259. }
  260. else {
  261. /* Redirect to login page */
  262. fs_open("/login.html", &file);
  263. hs->file = file.data;
  264. hs->left = file.len;
  265. send_data(pcb, hs);
  266. tcp_sent(pcb, http_sent);
  267. }
  268. /* End reqest */
  269. post_data_count = 0;
  270. log_post_reqn = 0;
  271. }
  272. }
  273. /* request was fragmented before "\r\n\r\n" */
  274. else {
  275. //printf("no data found!\r\n");
  276. /* wait next packet */
  277. log_post_reqn++;
  278. /* wait max 2 requests */
  279. if (log_post_reqn > 1) {
  280. /* Redirect to login page */
  281. fs_open("/login.html", &file);
  282. hs->file = file.data;
  283. hs->left = file.len;
  284. send_data(pcb, hs);
  285. tcp_sent(pcb, http_sent);
  286. /* End reqest */
  287. post_data_count = 0;
  288. log_post_reqn = 0;
  289. }
  290. }
  291. }
  292. else {
  293. printf("Too long POST request!\r\n");
  294. /* Ignore request */
  295. post_data_count = 0;
  296. log_post_reqn = 0;
  297. /* Redirect to login page */
  298. fs_open("/login.html", &file);
  299. hs->file = file.data;
  300. hs->left = file.len;
  301. send_data(pcb, hs);
  302. tcp_sent(pcb, http_sent);
  303. }
  304. }
  305. else if (post_data_count > 0)
  306. {
  307. strncat(post_req_data, data, post_data_count);
  308. //printf("copied: %d\r\n", (int)post_data_count);
  309. //printf("post_req_data: %s\r\n", post_req_data);
  310. if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES) {
  311. hs->file = sendBuf;
  312. hs->left = sendBufLoadLen;
  313. send_data(pcb, hs);
  314. tcp_sent(pcb, http_sent);
  315. }
  316. else {
  317. /* Redirect to login page */
  318. fs_open("/login.html", &file);
  319. hs->file = file.data;
  320. hs->left = file.len;
  321. send_data(pcb, hs);
  322. tcp_sent(pcb, http_sent);
  323. }
  324. /* End reqest */
  325. post_data_count = 0;
  326. log_post_reqn = 0;
  327. }
  328. else
  329. {
  330. fs_open("/login.html", &file);
  331. hs->file = file.data;
  332. hs->left = file.len;
  333. send_data(pcb, hs);
  334. tcp_sent(pcb, http_sent);
  335. }
  336. }
  337. else if ( Authenticated == true ) {
  338. if (strncmp(data, "GET /main.css", 13) == 0) // +
  339. {
  340. fs_open("/main.css", &file);
  341. hs->file = file.data;
  342. hs->left = file.len;
  343. send_data(pcb, hs);
  344. tcp_sent(pcb, http_sent);
  345. }
  346. else if (strncmp(data, "GET /rotek.png", 14) == 0) // +
  347. {
  348. fs_open("/rotek.png", &file);
  349. hs->file = file.data;
  350. hs->left = file.len;
  351. send_data(pcb, hs);
  352. tcp_sent(pcb, http_sent);
  353. }
  354. else if (strncmp(data, "GET /favicon.ico", 16) == 0) // ?
  355. {
  356. fs_open("/favicon.ico", &file);
  357. hs->file = file.data;
  358. hs->left = file.len;
  359. send_data(pcb, hs);
  360. tcp_sent(pcb, http_sent);
  361. }
  362. else if (strncmp(data, "GET /main.js", 12) == 0) // +
  363. {
  364. fs_open("/main.js", &file);
  365. hs->file = file.data;
  366. hs->left = file.len;
  367. send_data(pcb, hs);
  368. tcp_sent(pcb, http_sent);
  369. }
  370. else if (strncmp(data, "GET /role.js", 12) == 0)
  371. {
  372. fs_open("/role.js", &file);
  373. hs->file = file.data;
  374. hs->left = file.len;
  375. send_data(pcb, hs);
  376. tcp_sent(pcb, http_sent);
  377. }
  378. else if (strncmp(data, "GET /settings.html", 18) == 0) // +
  379. {
  380. HTTP_UpdateUserLoginTime(user_id);
  381. if (seclevel == 0){
  382. fs_open("/settings.html", &file);
  383. hs->file = file.data;
  384. hs->left = file.len;
  385. send_data(pcb, hs);
  386. tcp_sent(pcb, http_sent);
  387. }
  388. else {
  389. fs_open("/index.html", &file);
  390. hs->file = file.data;
  391. hs->left = file.len;
  392. send_data(pcb, hs);
  393. tcp_sent(pcb, http_sent);
  394. }
  395. }
  396. else if (strncmp(data, "GET /info.html", 14) == 0) // +
  397. {
  398. HTTP_UpdateUserLoginTime(user_id);
  399. fs_open("/info.html", &file);
  400. hs->file = file.data;
  401. hs->left = file.len;
  402. send_data(pcb, hs);
  403. tcp_sent(pcb, http_sent);
  404. }
  405. else if (strncmp(data, "GET /history.html", 17) == 0)
  406. {
  407. HTTP_UpdateUserLoginTime(user_id);
  408. fs_open("/history.html", &file);
  409. hs->file = file.data;
  410. hs->left = file.len;
  411. send_data(pcb, hs);
  412. tcp_sent(pcb, http_sent);
  413. }
  414. else if (strncmp(data, "GET /ups_history.html", 21) == 0)
  415. {
  416. HTTP_UpdateUserLoginTime(user_id);
  417. fs_open("/ups_history.html", &file);
  418. hs->file = file.data;
  419. hs->left = file.len;
  420. send_data(pcb, hs);
  421. tcp_sent(pcb, http_sent);
  422. }
  423. else if (strncmp(data, "GET /getJson.cgi", 16) == 0) // +
  424. {
  425. HTTP_GetParamsPage1(sendBuf);
  426. hs->file = sendBuf;
  427. hs->left = strlen(sendBuf);
  428. send_data(pcb, hs);
  429. tcp_sent(pcb, http_sent);
  430. }
  431. else if (strncmp(data, "GET /settings.cgi", 17) == 0) // +
  432. {
  433. if (seclevel == 0) {
  434. SET_PAGE = SET_PAGE_PAGE2;
  435. if (HTTP_SettingsPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  436. {
  437. hs->file = sendBuf;
  438. hs->left = sendBufLoadLen;
  439. send_data(pcb, hs);
  440. tcp_sent(pcb, http_sent);
  441. }
  442. /*else
  443. {
  444. fs_open("/settings.html", &file);
  445. hs->file = file.data;
  446. hs->left = file.len;
  447. send_data(pcb, hs);
  448. tcp_sent(pcb, http_sent);
  449. }*/
  450. }
  451. }
  452. else if (strncmp(data, "POST /settings.cgi", 18) == 0 || (DataFlag2 >= 1))
  453. {
  454. if (seclevel == 0) {
  455. DataOffset = 0;
  456. /* POST Packet received */
  457. if (DataFlag2 == 0)
  458. {
  459. BrowserFlag = 0;
  460. TotalReceived = 0;
  461. memset(sendBuf, 0, strlen(sendBuf));
  462. /* parse packet for Content-length field */
  463. size = Parse_Content_Length(data, p->tot_len);
  464. /* parse packet for the octet-stream field */
  465. for (i = 0; i < receivedBufLen; i++)
  466. {
  467. if (strncmp ((char*)(data+i), "managerIP", 8)==0)
  468. {
  469. DataOffset = i;
  470. break;
  471. }
  472. }
  473. /* case of MSIE8 : we do not receive data in the POST packet*/
  474. if (DataOffset == 0)
  475. {
  476. DataFlag2++;
  477. BrowserFlag = 1;
  478. pbuf_free(p);
  479. return ERR_OK;
  480. }
  481. /* case of Mozilla Firefox v3.6 : we receive data in the POST packet*/
  482. else
  483. {
  484. //TotalReceived = receivedBufLen - (ContentLengthOffset + 4);
  485. TotalReceived = receivedBufLen - DataOffset;
  486. }
  487. }
  488. if (((DataFlag2 ==1)&&(BrowserFlag==1)) || ((DataFlag2 ==0)&&(BrowserFlag==0)))
  489. {
  490. if ((DataFlag2 ==0)&&(BrowserFlag==0))
  491. {
  492. DataFlag2++;
  493. }
  494. else if ((DataFlag2 ==1)&&(BrowserFlag==1))
  495. {
  496. /* parse packet for the octet-stream field */
  497. for (i = 0; i < receivedBufLen; i++)
  498. {
  499. if (strncmp ((char*)(data+i), "managerIP", 8)==0)
  500. {
  501. DataOffset = i;
  502. break;
  503. }
  504. }
  505. TotalReceived += receivedBufLen;
  506. DataFlag2++;
  507. }
  508. TotalData =0 ;
  509. }
  510. /* DataFlag >1 => the packet is data only */
  511. else
  512. {
  513. TotalReceived +=receivedBufLen;
  514. }
  515. ptr = (char*)(data + DataOffset);
  516. receivedBufLen-= DataOffset;
  517. /* update Total data received counter */
  518. TotalData +=receivedBufLen;
  519. /* check if last data packet */
  520. if (TotalReceived == size)
  521. {
  522. //DBG printf("State: Received %d bytes\r\n", (int)TotalReceived);
  523. strncat(sendBuf, ptr, receivedBufLen);
  524. strncat(sendBuf, " ", 1);
  525. //ВBG printf("receive %s /r/n", sendBuf);
  526. HTTP_SetSettings(sendBuf, strlen(sendBuf));
  527. DataFlag2=0;
  528. BrowserFlag = 0;
  529. memset(sendBuf, 0, size);
  530. strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
  531. strcat(sendBuf, "\r\n\r\n");
  532. strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/settings.html\"/></head></html>\r\n\r\n");
  533. sendBufLoadLen = strlen(sendBuf);
  534. hs->file = sendBuf;
  535. hs->left = sendBufLoadLen;
  536. send_data(pcb, hs);
  537. /* Tell TCP that we wish be to informed of data that has been
  538. successfully sent by a call to the http_sent() function. */
  539. tcp_sent(pcb, http_sent);
  540. }
  541. /* not last data packet */
  542. else
  543. {
  544. /* write data in flash */
  545. if(receivedBufLen)
  546. {
  547. strncat(sendBuf, ptr, receivedBufLen);
  548. //memcpy(receiveBufTemp, ptr, receivedBufLen);
  549. }
  550. }
  551. }
  552. }
  553. else if (strncmp(data, "GET /info.cgi", 13) == 0) // +
  554. {
  555. if (HTTP_InfoPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  556. {
  557. hs->file = sendBuf;
  558. hs->left = sendBufLoadLen;
  559. send_data(pcb, hs);
  560. tcp_sent(pcb, http_sent);
  561. }
  562. /* else
  563. {
  564. fs_open("/info.html", &file);
  565. hs->file = file.data;
  566. hs->left = file.len;
  567. send_data(pcb, hs);
  568. tcp_sent(pcb, http_sent);
  569. }*/
  570. }
  571. else if (strncmp(data, "POST /info.cgi", 14) == 0 || (DataFlag >= 1))
  572. {
  573. if (seclevel == 0) {
  574. DataOffset = 0;
  575. /* POST Packet received */
  576. if (DataFlag == 0)
  577. {
  578. BrowserFlag = 0;
  579. TotalReceived = 0;
  580. memset(sendBuf, 0, strlen(sendBuf));
  581. /* parse packet for Content-length field */
  582. size = Parse_Content_Length(data, p->tot_len);
  583. /* parse packet for the octet-stream field */
  584. for (i = 0; i < receivedBufLen; i++)
  585. {
  586. if (strncmp ((char*)(data+i), "owner", 5)==0)
  587. {
  588. DataOffset = i;
  589. break;
  590. }
  591. }
  592. /* case of MSIE8 : we do not receive data in the POST packet*/
  593. if (DataOffset == 0)
  594. {
  595. DataFlag++;
  596. BrowserFlag = 1;
  597. pbuf_free(p);
  598. return ERR_OK;
  599. }
  600. /* case of Mozilla Firefox v3.6 : we receive data in the POST packet*/
  601. else
  602. {
  603. //TotalReceived = receivedBufLen - (ContentLengthOffset + 4);
  604. TotalReceived = receivedBufLen - DataOffset;
  605. }
  606. }
  607. if (((DataFlag ==1)&&(BrowserFlag==1)) || ((DataFlag ==0)&&(BrowserFlag==0)))
  608. {
  609. if ((DataFlag ==0)&&(BrowserFlag==0))
  610. {
  611. DataFlag++;
  612. }
  613. else if ((DataFlag ==1)&&(BrowserFlag==1))
  614. {
  615. /* parse packet for the octet-stream field */
  616. for (i = 0; i < receivedBufLen; i++)
  617. {
  618. if (strncmp ((char*)(data+i), "owner", 5)==0)
  619. {
  620. DataOffset = i;
  621. break;
  622. }
  623. }
  624. TotalReceived += receivedBufLen;
  625. DataFlag++;
  626. }
  627. TotalData =0 ;
  628. }
  629. /* DataFlag >1 => the packet is data only */
  630. else
  631. {
  632. TotalReceived +=receivedBufLen;
  633. }
  634. ptr = (char*)(data + DataOffset);
  635. receivedBufLen-= DataOffset;
  636. /* update Total data received counter */
  637. TotalData +=receivedBufLen;
  638. /* check if last data packet */
  639. if (TotalReceived == size)
  640. {
  641. strncat(sendBuf, ptr, receivedBufLen);
  642. strncat(sendBuf, " ", 1);
  643. HTTP_SetInfo(sendBuf, strlen(sendBuf));
  644. DataFlag=0;
  645. BrowserFlag = 0;
  646. memset(sendBuf, 0, size);
  647. strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
  648. strcat(sendBuf, "\r\n\r\n");
  649. strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/info.html\"/></head></html>\r\n\r\n");
  650. sendBufLoadLen = strlen(sendBuf);
  651. hs->file = sendBuf;
  652. hs->left = sendBufLoadLen;
  653. send_data(pcb, hs);
  654. /* Tell TCP that we wish be to informed of data that has been
  655. successfully sent by a call to the http_sent() function. */
  656. tcp_sent(pcb, http_sent);
  657. }
  658. /* not last data packet */
  659. else
  660. {
  661. /* write data in flash */
  662. if(receivedBufLen)
  663. {
  664. strncat(sendBuf, ptr, receivedBufLen);
  665. //memcpy(receiveBufTemp, ptr, receivedBufLen);
  666. }
  667. }
  668. }
  669. }
  670. else if (strncmp(data, "GET /history.cgi", 16) == 0)
  671. {
  672. int res;
  673. res = HTTP_HistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  674. if (res == SEND_REQUIRED_FILE)
  675. {
  676. hs->file = sendBuf;
  677. hs->left = sendBufLoadLen;
  678. send_data(pcb, hs);
  679. tcp_sent(pcb, http_sent_history);
  680. tcp_err(pcb, http_sent_log_err);
  681. }
  682. else if (res == SEND_REQUIRED_YES) {
  683. hs->file = sendBuf;
  684. hs->left = sendBufLoadLen;
  685. send_data(pcb, hs);
  686. tcp_sent(pcb, http_sent);
  687. }
  688. }
  689. else if (strncmp(data, "GET /ups_history.cgi", 19) == 0)
  690. {
  691. int res;
  692. res = HTTP_UpsHistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  693. if (res == SEND_REQUIRED_FILE)
  694. {
  695. hs->file = sendBuf;
  696. hs->left = sendBufLoadLen;
  697. send_data(pcb, hs);
  698. tcp_sent(pcb, http_sent_log);
  699. tcp_err(pcb, http_sent_log_err);
  700. }
  701. else if (res == SEND_REQUIRED_YES) {
  702. hs->file = sendBuf;
  703. hs->left = sendBufLoadLen;
  704. send_data(pcb, hs);
  705. tcp_sent(pcb, http_sent);
  706. }
  707. }
  708. /* Тест АКБ ИБП */
  709. else if (strncmp(data, "GET /bat_test.cgi", 17) == 0)
  710. {
  711. HTTP_UPSTest(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  712. hs->file = sendBuf;
  713. hs->left = sendBufLoadLen;
  714. send_data(pcb, hs);
  715. tcp_sent(pcb, http_sent);
  716. }
  717. /* Выключение ИБП */
  718. else if (strncmp(data, "GET /ups_power.cgi", 18) == 0)
  719. {
  720. HTTP_UPSshutdown(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  721. hs->file = sendBuf;
  722. hs->left = sendBufLoadLen;
  723. send_data(pcb, hs);
  724. tcp_sent(pcb, http_sent);
  725. }
  726. /* Сброс настроек и сохранине */
  727. else if (strncmp(data, "GET /reset.cgi", 14) == 0)
  728. {
  729. HTTP_ResetSettings();
  730. HTTP_SaveSettings();
  731. fs_open("/settings.html", &file);
  732. hs->file = file.data;
  733. hs->left = file.len;
  734. send_data(pcb, hs);
  735. tcp_sent(pcb, http_sent);
  736. }
  737. /* Перезагрузка контроллера */
  738. else if (strncmp(data, "GET /reboot.cgi", 15) == 0)
  739. {
  740. HTTP_Reboot();
  741. }
  742. /* Подтверждение новых сетевых настроек */
  743. else if (strncmp(data, "GET /confirm.cgi", 16) == 0)
  744. {
  745. SetWebReinitFlag(false);
  746. SetConfirmWebParamsFlag();
  747. fs_open("/index.html", &file);
  748. hs->file = file.data;
  749. hs->left = file.len;
  750. send_data(pcb, hs);
  751. tcp_sent(pcb, http_sent);
  752. }
  753. /* Проверка пароля, переход в bootloader */
  754. else if (strncmp(data, "GET /fw_update.cgi", 18) == 0)
  755. {
  756. HTTP_ConfirmBootPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  757. hs->file = sendBuf;
  758. hs->left = sendBufLoadLen;
  759. send_data(pcb, hs);
  760. tcp_sent(pcb, http_sent);
  761. }
  762. /* Смена пароля пользователя */
  763. else if (strncmp(data, "GET /changepwd.cgi", 18) == 0)
  764. {
  765. HTTP_ChangeUserPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  766. hs->file = sendBuf;
  767. hs->left = sendBufLoadLen;
  768. send_data(pcb, hs);
  769. tcp_sent(pcb, http_sent);
  770. }
  771. // На производстве
  772. else if (strncmp(data, "GET /setProdate.cgi", 19) == 0)
  773. {
  774. HTTP_Prodate(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  775. hs->file = sendBuf;
  776. hs->left = sendBufLoadLen;
  777. send_data(pcb, hs);
  778. tcp_sent(pcb, http_sent);
  779. }
  780. else
  781. {
  782. HTTP_UpdateUserLoginTime(user_id);
  783. fs_open("/index.html", &file); // +
  784. hs->file = file.data;
  785. hs->left = file.len;
  786. send_data(pcb, hs);
  787. tcp_sent(pcb, http_sent);
  788. }
  789. }
  790. }
  791. pbuf_free(p);
  792. // close_conn(pcb,hs);
  793. }
  794. /* if (err == ERR_OK && p == NULL)
  795. {
  796. close_conn(pcb, hs);
  797. }*/
  798. return ERR_OK;
  799. }
  800. /**
  801. * @brief Error callback for log file transfer
  802. */
  803. static void http_sent_log_err(void * arg, err_t err)
  804. {
  805. (void)err;
  806. (void)arg;
  807. /* Clear file transfer in progress flag */
  808. fLogTransInprog = false;
  809. }
  810. /**
  811. * @brief Sent callback for log file transfer (messages as is, not ordered)
  812. */
  813. static err_t http_sent_log(void *arg, struct tcp_pcb *pcb, u16_t len)
  814. {
  815. struct http_state *hs;
  816. uint32_t nbytes = 0;
  817. static bool start = true;
  818. (void)len;
  819. hs = arg;
  820. if (hs->left > 0)
  821. {
  822. send_data(pcb, hs);
  823. }
  824. else
  825. {
  826. memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
  827. if (log_ptr + FILE_BUF_MAX_LEN_LOG <= log_size) {
  828. nbytes = LOG_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN_LOG, start);
  829. }
  830. else if (log_ptr < log_size) {
  831. nbytes = LOG_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
  832. }
  833. else {
  834. nbytes = 0;
  835. }
  836. log_ptr += nbytes;
  837. start = false;
  838. if (nbytes == 0) {
  839. /* File transfer finished. */
  840. start = true;
  841. close_conn(pcb, hs);
  842. /* Clear file transfer in progress flag */
  843. fLogTransInprog = false;
  844. return ERR_OK;
  845. }
  846. hs->file = logFileBuf;
  847. hs->left = nbytes;
  848. send_data(pcb, hs);
  849. tcp_sent(pcb, http_sent_log);
  850. }
  851. return ERR_OK;
  852. }
  853. /**
  854. * @brief Sent callback for log file transfer (messages as is, not ordered)
  855. */
  856. static err_t http_sent_history(void *arg, struct tcp_pcb *pcb, u16_t len)
  857. {
  858. struct http_state *hs;
  859. uint32_t nbytes = 0;
  860. static bool start = true;
  861. (void)len;
  862. hs = arg;
  863. if (hs->left > 0)
  864. {
  865. send_data(pcb, hs);
  866. }
  867. else
  868. {
  869. memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
  870. if (log_ptr + FILE_BUF_MAX_LEN <= log_size) {
  871. nbytes = History_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN, start);
  872. }
  873. else if (log_ptr < log_size) {
  874. nbytes = History_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
  875. }
  876. else {
  877. nbytes = 0;
  878. }
  879. log_ptr += nbytes;
  880. start = false;
  881. if (nbytes == 0) {
  882. /* File transfer finished. */
  883. start = true;
  884. close_conn(pcb, hs);
  885. /* Clear file transfer in progress flag */
  886. fLogTransInprog = false;
  887. return ERR_OK;
  888. }
  889. hs->file = logFileBuf;
  890. hs->left = nbytes;
  891. send_data(pcb, hs);
  892. tcp_sent(pcb, http_sent_history);
  893. }
  894. return ERR_OK;
  895. }
  896. /**
  897. * @brief callback function for handling connection errors
  898. * @param arg: pointer to an argument to be passed to callback function
  899. * @param err: LwIP error code
  900. * @retval none
  901. */
  902. static void conn_err(void *arg, err_t err)
  903. {
  904. struct http_state *hs;
  905. hs = arg;
  906. mem_free(hs);
  907. }
  908. /**
  909. * @brief callback function called after a successfull TCP data packet transmission
  910. * @param arg: pointer to an argument to be passed to callback function
  911. * @param pcb: pointer on tcp_pcb structure
  912. * @param len
  913. * @retval err : LwIP error code
  914. */
  915. static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
  916. {
  917. struct http_state *hs;
  918. hs = arg;
  919. if (hs->left > 0)
  920. {
  921. send_data(pcb, hs);
  922. }
  923. else
  924. {
  925. close_conn(pcb, hs);
  926. }
  927. return ERR_OK;
  928. }
  929. /**
  930. * @brief sends data found in member "file" of a http_state struct
  931. * @param pcb: pointer to a tcp_pcb struct
  932. * @param hs: pointer to a http_state struct
  933. * @retval none
  934. */
  935. static void send_data(struct tcp_pcb *pcb, struct http_state *hs)
  936. {
  937. err_t err;
  938. u16_t len;
  939. /* We cannot send more data than space available in the send
  940. buffer */
  941. if (tcp_sndbuf(pcb) < hs->left)
  942. {
  943. len = tcp_sndbuf(pcb);
  944. }
  945. else
  946. {
  947. len = hs->left;
  948. }
  949. err = tcp_write(pcb, hs->file, len, 0);
  950. if (err == ERR_OK)
  951. {
  952. hs->file += len;
  953. hs->left -= len;
  954. }
  955. }
  956. /**
  957. * @brief tcp poll callback function
  958. * @param arg: pointer to an argument to be passed to callback function
  959. * @param pcb: pointer on tcp_pcb structure
  960. * @retval err_t
  961. */
  962. static err_t http_poll(void *arg, struct tcp_pcb *pcb)
  963. {
  964. if (arg == NULL)
  965. {
  966. tcp_close(pcb);
  967. }
  968. else
  969. {
  970. send_data(pcb, (struct http_state *)arg);
  971. }
  972. return ERR_OK;
  973. }
  974. /**
  975. * @brief callback function on TCP connection setup ( on port 80)
  976. * @param arg: pointer to an argument structure to be passed to callback function
  977. * @param pcb: pointer to a tcp_pcb structure
  978. * &param err: Lwip stack error code
  979. * @retval err
  980. */
  981. static err_t http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
  982. {
  983. struct http_state *hs;
  984. /* Allocate memory for the structure that holds the state of the connection */
  985. hs = mem_malloc(sizeof(struct http_state));
  986. if (hs == NULL)
  987. {
  988. return ERR_MEM;
  989. }
  990. /* Initialize the structure. */
  991. hs->file = NULL;
  992. hs->left = 0;
  993. /* Tell TCP that this is the structure we wish to be passed for our
  994. callbacks. */
  995. tcp_arg(pcb, hs);
  996. /* Tell TCP that we wish to be informed of incoming data by a call
  997. to the http_recv() function. */
  998. tcp_recv(pcb, http_recv);
  999. tcp_err(pcb, conn_err);
  1000. tcp_poll(pcb, http_poll, 10);
  1001. return ERR_OK;
  1002. }
  1003. /**
  1004. * @brief Opens a file defined in fsdata.c ROM filesystem
  1005. * @param name : pointer to a file name
  1006. * @param file : pointer to a fs_file structure
  1007. * @retval 1 if success, 0 if fail
  1008. */
  1009. static int fs_open(char *name, struct fs_file *file)
  1010. {
  1011. struct fsdata_file_noconst *f;
  1012. for (f = (struct fsdata_file_noconst *)FS_ROOT; f != NULL; f = (struct fsdata_file_noconst *)f->next)
  1013. {
  1014. if (!strcmp(name, f->name))
  1015. {
  1016. file->data = f->data;
  1017. file->len = f->len;
  1018. return 1;
  1019. }
  1020. }
  1021. return 0;
  1022. }
  1023. /**
  1024. * @brief Initialize the HTTP server (start its thread)
  1025. * @param none
  1026. * @retval None
  1027. */
  1028. void HTTP_Init()
  1029. {
  1030. char buf[MAX_WEB_COOKIE_LEN];
  1031. uint8_t user_id;
  1032. //sys_thread_new("HTTP", http_server_netconn_thread, NULL, 3000, 2);
  1033. struct tcp_pcb *pcb;
  1034. /*create new pcb*/
  1035. pcb = tcp_new();
  1036. /* bind HTTP traffic to pcb */
  1037. tcp_bind(pcb, IP_ADDR_ANY, 80);
  1038. /* start listening on port 80 */
  1039. pcb = tcp_listen(pcb);
  1040. /* define callback function for TCP connection setup */
  1041. tcp_accept(pcb, http_accept);
  1042. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  1043. /* Flush user cookie by random value */
  1044. sprintf(buf, "%X", (unsigned int)GetRandomNumber());
  1045. HTTP_SetUserCookie(buf, user_id);
  1046. /* Create user logout timers */
  1047. users[user_id].LogoutTimer =
  1048. xTimerCreate("LogoutTmr", WEB_LOGOUT_TIME, pdFALSE, ( void * ) user_id, LogoutTimerCallback);
  1049. }
  1050. }
  1051. /**
  1052. * @brief
  1053. * @retval None
  1054. */
  1055. int HTTP_SettingsPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1056. {
  1057. char tempStr[30];
  1058. strncpy(tempStr, bufIn, 30);
  1059. /* В запросе нет параметров, нужно формировать JSON ответ */
  1060. if (strpbrk(tempStr,"?") == 0)
  1061. {
  1062. memset(bufOut, 0, SEND_BUF_MAX_LEN);
  1063. HTTP_GetSettings(bufOut);
  1064. //printf(bufOut);
  1065. *lenBufOut = strlen(bufOut);
  1066. return SEND_REQUIRED_YES;
  1067. }
  1068. /* В запросе есть параметры, нужно парсить и сохранять настройки */
  1069. else
  1070. {
  1071. //HTTP_SetSettings(bufIn, lenBufIn);
  1072. return SEND_REQUIRED_NO;
  1073. }
  1074. }
  1075. /**
  1076. * @brief
  1077. * @retval None
  1078. */
  1079. int HTTP_InfoPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1080. {
  1081. char tempStr[30];
  1082. strncpy(tempStr, bufIn, 30);
  1083. /* В запросе нет параметров, нужно формировать JSON ответ */
  1084. if (strpbrk(tempStr,"?") == 0)
  1085. {
  1086. memset(bufOut, 0, SEND_BUF_MAX_LEN);
  1087. HTTP_GetInfo(bufOut);
  1088. *lenBufOut = strlen(bufOut);
  1089. return SEND_REQUIRED_YES;
  1090. }
  1091. /* В запросе есть параметры, нужно парсить и сохранять настройки */
  1092. else
  1093. {
  1094. //HTTP_SetInfo(bufIn, lenBufIn);
  1095. return SEND_REQUIRED_NO;
  1096. /*
  1097. HTTP_SetSettings(bufIn, lenBufIn);
  1098. return SEND_REQUIRED_NO;
  1099. */
  1100. }
  1101. }
  1102. int HTTP_HistoryPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1103. {
  1104. uint8_t i, valueLen = 0;
  1105. char value[20];
  1106. uint32_t nbytes = 0;
  1107. (void)lenBufIn;
  1108. memset(bufOut, 0, FILE_BUF_MAX_LEN);
  1109. ClearParamString(bufIn);
  1110. memset(value, 0, 20);
  1111. GetParamValue(bufIn, "page=", value, &valueLen);
  1112. if (strcmp(value, "all") == 0)
  1113. {
  1114. if (!LOG_IsInit()) {
  1115. return SEND_REQUIRED_NO;
  1116. }
  1117. if (fLogTransInprog == false) {
  1118. // Send log as raw data
  1119. log_ptr = 0;
  1120. log_size = History_GetTotalSTRCount() * STRING_SIZE_HISTORY + sizeof(UTF8_BOM)-1;
  1121. sprintf(bufOut, "HTTP/1.1 200 OK\r\nContent-Length:%lu\r\n\r\n%s", log_size, UTF8_BOM);
  1122. *lenBufOut = strlen(bufOut);
  1123. // Set file transfer in progress flag
  1124. fLogTransInprog = true;
  1125. return SEND_REQUIRED_FILE;
  1126. }
  1127. else {
  1128. // We send nothing if file transfer already in progress
  1129. return SEND_REQUIRED_NO;
  1130. }
  1131. }
  1132. else {
  1133. if (!LOG_IsInit()) {
  1134. return SEND_REQUIRED_NO;
  1135. }
  1136. else {
  1137. HTTP_GetHistoryPage(bufOut, atoi(value));
  1138. *lenBufOut = strlen(bufOut);
  1139. return SEND_REQUIRED_YES;
  1140. }
  1141. }
  1142. }
  1143. int HTTP_UpsHistoryPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1144. {
  1145. uint8_t i, valueLen = 0;
  1146. char value[20];
  1147. uint32_t nbytes = 0;
  1148. (void)lenBufIn;
  1149. memset(bufOut, 0, FILE_BUF_MAX_LEN);
  1150. ClearParamString(bufIn);
  1151. memset(value, 0, 20);
  1152. GetParamValue(bufIn, "page=", value, &valueLen);
  1153. if (strcmp(value, "all") == 0)
  1154. {
  1155. if (!LOG_IsInit()) {
  1156. return SEND_REQUIRED_NO;
  1157. }
  1158. if (fLogTransInprog == false) {
  1159. // Send log as raw data
  1160. log_ptr = 0;
  1161. log_size = LOG_GetTotalSTRCount() * STRING_SIZE + sizeof(UTF8_BOM)-1;
  1162. sprintf(bufOut, "HTTP/1.1 200 OK\r\nContent-Length:%lu\r\n\r\n%s", log_size, UTF8_BOM);
  1163. *lenBufOut = strlen(bufOut);
  1164. // Set file transfer in progress flag
  1165. fLogTransInprog = true;
  1166. return SEND_REQUIRED_FILE;
  1167. }
  1168. else {
  1169. // We send nothing if file transfer already in progress
  1170. return SEND_REQUIRED_NO;
  1171. }
  1172. }
  1173. else {
  1174. if (!LOG_IsInit()) {
  1175. return SEND_REQUIRED_NO;
  1176. }
  1177. else {
  1178. HTTP_GetUpsHistoryPage(bufOut, atoi(value));
  1179. *lenBufOut = strlen(bufOut);
  1180. return SEND_REQUIRED_YES;
  1181. }
  1182. }
  1183. }
  1184. /**
  1185. * @brief Установка даты производства
  1186. */
  1187. // TODO Убрать заглушку!
  1188. void HTTP_Prodate(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1189. {
  1190. uint8_t valueLen = 0;
  1191. char value[20];
  1192. memset(bufOut, 0, SEND_BUF_MAX_LEN);
  1193. ClearParamString(bufIn);
  1194. memset(value, 0, 20);
  1195. GetParamValue(bufIn, "prodate=", value, &valueLen);
  1196. /*
  1197. printf("Prodate: ");
  1198. printf(value);
  1199. printf("\r\n");
  1200. */
  1201. /* Устанавливаем дату производства */
  1202. SETTINGS_SetProDate(value, valueLen);
  1203. /* Пока отправляем true */
  1204. strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\n\r\nTrue");
  1205. *lenBufOut = strlen(bufOut);
  1206. TEST_SetServerFlag();
  1207. }
  1208. /**
  1209. * @brief
  1210. * @retval None
  1211. */
  1212. void HTTP_SetSettings(char *buf, uint16_t lenBuf)
  1213. {
  1214. uint8_t valueLen = 0;
  1215. const uint8_t len = MAX_WEB_PARAM_LEN;
  1216. char value[MAX_WEB_PARAM_LEN];
  1217. char str[MAX_WEB_PARAM_LEN];
  1218. //printf(buf);
  1219. //ClearParamString(buf);
  1220. memset(value, 0, len);
  1221. memset(str, 0, MAX_WEB_PARAM_LEN);
  1222. /* SNMP */
  1223. GetParamValue(buf, "read_community=", value, &valueLen);
  1224. SetReadCommunity(value);
  1225. memset(value, 0, len);
  1226. GetParamValue(buf, "write_community=", value, &valueLen);
  1227. SetWriteCommunity(value);
  1228. memset(value, 0, len);
  1229. GetParamValue(buf, "managerIP=", value, &valueLen);
  1230. SetManagerIp(value);
  1231. memset(value, 0, len);
  1232. GetParamValue(buf, "managerIP2=", value, &valueLen);
  1233. SetManagerIp2(value);
  1234. memset(value, 0, len);
  1235. GetParamValue(buf, "managerIP3=", value, &valueLen);
  1236. SetManagerIp3(value);
  1237. memset(value, 0, len);
  1238. GetParamValue(buf, "managerIP4=", value, &valueLen);
  1239. SetManagerIp4(value);
  1240. memset(value, 0, len);
  1241. GetParamValue(buf, "managerIP5=", value, &valueLen);
  1242. SetManagerIp5(value);
  1243. memset(value, 0, len);
  1244. /* Сетевые параметры */
  1245. GetParamValue(buf, "dhcp=", value, &valueLen);
  1246. SetDhcpStateStr(value);
  1247. if (strncmp(value, "on", 2) != 0) // Если dhcp off устанавливаем параметры
  1248. {
  1249. memset(value, 0, len);
  1250. GetParamValue(buf, "ipaddr=", value, &valueLen);
  1251. SetIPStr(value);
  1252. memset(value, 0, len);
  1253. GetParamValue(buf, "gw=", value, &valueLen);
  1254. SetGatewayStr(value);
  1255. memset(value, 0, len);
  1256. GetParamValue(buf, "mask=", value, &valueLen);
  1257. SetMaskStr(value);
  1258. memset(value, 0, len);
  1259. }
  1260. memset(value, 0, len);
  1261. GetParamValue(buf, "swauth=", value, &valueLen);
  1262. SetAuthEnableStateStr(value);
  1263. if (strncmp(value, "on", 2) == 0){
  1264. /* параметры RADIUS*/
  1265. memset(value, 0, len);
  1266. GetParamValue(buf, "rs_enabled=", value, &valueLen);
  1267. SetRDSEnableStateStr(value);
  1268. if (strncmp(value, "on", 2) == 0) // Если raddius off устанавливаем параметры
  1269. {
  1270. memset(value, 0, len);
  1271. GetParamValue(buf, "rs_server=", value, &valueLen);
  1272. SetRDSIpStr(value);
  1273. memset(value, 0, len);
  1274. GetParamValue(buf, "rs_port=", value, &valueLen);
  1275. SetRDSPortStr(value);
  1276. memset(value, 0, len);
  1277. GetParamValue(buf, "rs_pwd=", value, &valueLen);
  1278. SetRDSPasswordkStr(value);
  1279. memset(value, 0, len);
  1280. GetParamValue(buf, "rs_key=", value, &valueLen);
  1281. SetRDSKeyAccesstStr(value);
  1282. memset(value, 0, len);
  1283. }
  1284. }
  1285. memset(value, 0, len);
  1286. // Параметры реле и сухих контактов
  1287. GetParamValue(buf, "di1=", value, &valueLen);
  1288. SetDINTypeActStr(value, 0);
  1289. memset(value, 0, len);
  1290. GetParamValue(buf, "ro1=", value, &valueLen);
  1291. SetROTypeActStr(value, 0);
  1292. memset(value, 0, len);
  1293. GetParamValue(buf, "ro2=", value, &valueLen);
  1294. SetROTypeActStr(value, 1);
  1295. memset(value, 0, len);
  1296. // Параметры даты и времени
  1297. GetParamValue(buf, "ntp=", value, &valueLen);
  1298. SetSntpStateStr(value);
  1299. if (strncmp(value, "1", 1) == 0) // Если ntp on устанавливаем параметры
  1300. {
  1301. memset(value, 0, len);
  1302. GetParamValue(buf, "ntpservip=", value, &valueLen);
  1303. SetSntpServerIpStr(value);
  1304. memset(value, 0, len);
  1305. }
  1306. else if (strncmp(value, "0", 1) == 0){
  1307. GetParamValue(buf, "date=", value, &valueLen);
  1308. SetDateStr(value);
  1309. memset(value, 0, len);
  1310. GetParamValue(buf, "time=", value, &valueLen);
  1311. url_decode(str, sizeof(str), value);
  1312. SetTimeStr(str);
  1313. memset(value, 0, len);
  1314. }
  1315. GetParamValue(buf, "utc=", value, &valueLen);
  1316. SetSntpTimeZoneStr(value);
  1317. memset(value, 0, len);
  1318. /* Если параметры WEB изменились выставляем флаг, сохраняем настройки и перезагружаемся */
  1319. if (GetStateWebReinit() == true)
  1320. {
  1321. SetWebReinitFlag(true);
  1322. HTTP_SaveSettings();
  1323. /* Блокируем управление ключем на тау секунд*/
  1324. //IO_KeyBlockOn();
  1325. vTaskDelay(1010);
  1326. Reboot();
  1327. }
  1328. HTTP_SaveSettings();
  1329. }
  1330. /**
  1331. * @brief
  1332. * @retval None
  1333. */
  1334. void HTTP_SetInfo(char *buf, uint16_t lenBuf)
  1335. {
  1336. uint8_t valueLen = 0;
  1337. const uint8_t len = 110;
  1338. char value[110];
  1339. char str[110];
  1340. // ClearParamString(buf);
  1341. memset(value, 0, len);
  1342. /* Владелец */
  1343. GetParamValue(buf, "owner=", value, &valueLen);
  1344. url_decode(str, sizeof(str), value);
  1345. SetOwner(str);
  1346. memset(value, 0, len);
  1347. /* Владелец */
  1348. GetParamValue(buf, "sysLocation=", value, &valueLen);
  1349. url_decode(str, sizeof(str), value);
  1350. SetLocation(str);
  1351. memset(value, 0, len);
  1352. /* Комментарий */
  1353. GetParamValue(buf, "comment=", value, &valueLen);
  1354. url_decode(str, sizeof(str), value);
  1355. SetComment(str);
  1356. memset(value, 0, len);
  1357. HTTP_SaveSettings();
  1358. }
  1359. /**
  1360. * @brief Запуск/останов теста UPS
  1361. */
  1362. void HTTP_UPSTest(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1363. {
  1364. uint8_t valueLen = 0;
  1365. char tempValue[20];
  1366. char tempValue2[20];
  1367. int8_t res = 0;
  1368. memset(tempValue, 0, 20);
  1369. memset(tempValue2, 0, 20);
  1370. strcpy(bufOut, HTTP_200_OK);
  1371. GetParamValue(bufIn, "func=", tempValue, &valueLen);
  1372. if (strcmp(tempValue, "stop") == 0){
  1373. res = ups_metac_service_pdu(ups_cancel_test);
  1374. if(res == 1 || res == 0){
  1375. strcat(bufOut, "Тест остановлен!");
  1376. log_event_data(LOG_TEST_UPS, "Администратор(Останов)");
  1377. }
  1378. if(res == -1)
  1379. strcat(bufOut, "Тест не удалось остановить!");
  1380. *lenBufOut = strlen(bufOut);
  1381. }
  1382. else if (strcmp(tempValue, "discharge") == 0){
  1383. res = ups_metac_service_pdu(ups_test_low_bat);
  1384. if(res == 1 || res == 0){
  1385. strcat(bufOut, "Тест запущен!");
  1386. log_event_data(LOG_TEST_UPS, "Администратор (Запущен)");
  1387. }
  1388. if(res == -1)
  1389. strcat(bufOut, "Тест не удалось запустить!");
  1390. *lenBufOut = strlen(bufOut);
  1391. }
  1392. else if (strncmp(tempValue, "time", 6) == 0){
  1393. GetParamValue(bufIn, "=", tempValue2, &valueLen);
  1394. TimeParam = atoi(tempValue2);
  1395. res = ups_metac_service_pdu(ups_test_time);
  1396. if(res == 1 || res == 0){
  1397. strcat(bufOut, "Тест запущен!");
  1398. log_event_data(LOG_TEST_UPS, "Администратор (Запущен)");
  1399. }
  1400. if(res == -1)
  1401. strcat(bufOut, "Тест не удалось запустить!");
  1402. *lenBufOut = strlen(bufOut);
  1403. }
  1404. }
  1405. /**
  1406. * @brief Выклюение UPS
  1407. */
  1408. void HTTP_UPSshutdown(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1409. {
  1410. uint8_t valueLen = 0;
  1411. char *valueLenEnd = 0;
  1412. char tempValue[50];
  1413. char tempValue2[50];
  1414. int8_t res = 0;
  1415. memset(tempValue, 0, 50);
  1416. strcpy(bufOut, HTTP_200_OK);
  1417. GetParamValue(bufIn, "func=", tempValue, &valueLen);
  1418. if (strcmp(tempValue, "reboot") == 0){
  1419. res = ups_metac_service_pdu(ups_cancel_shut_down);
  1420. if(res == 1){
  1421. log_event_data(LOG_SHUTDOWN_UPS, "Администратор(Останов)");
  1422. strcat(bufOut, "Выключение нагрузки ИБП отменено!");
  1423. }
  1424. else
  1425. strcat(bufOut, "Выключение нагрузки ИБП не удалось отменить!");
  1426. *lenBufOut = strlen(bufOut);
  1427. }
  1428. else if (strncmp(tempValue, "off", 5) == 0){
  1429. memset(tempValue2, 0, 50);
  1430. GetParamValue(bufIn, "after=", tempValue2, &valueLen);
  1431. TimeParamFloat = atof(tempValue2);
  1432. res = ups_metac_service_pdu(ups_shutdown);
  1433. if(res == 1){
  1434. strcat(bufOut, "Отключение нагрузки ИБП!");
  1435. log_event_data(LOG_SHUTDOWN_UPS, "Администратор");
  1436. }else
  1437. strcat(bufOut, "Отключение нагрузки ИБП не удалось!");
  1438. *lenBufOut = strlen(bufOut);
  1439. }
  1440. }
  1441. /**
  1442. * @brief Проверка пароля для перехода в режим bootloader
  1443. * @retval None
  1444. */
  1445. void HTTP_ConfirmBootPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1446. {
  1447. char tempStr[50];
  1448. strncpy(tempStr, bufIn, 50);
  1449. char value[20];
  1450. uint8_t valueLen;
  1451. memset(value, 0, 20);
  1452. //if (GetParamValue(tempStr, "password=", value, &valueLen))
  1453. {
  1454. //if (strcmp(BOOTLOADER_PASWORD, value) == 0)
  1455. {
  1456. // *bufOut = '1';
  1457. /* Запускаем задачу отложенной перезагрузки. Контроллер должен успеть
  1458. отправить ответ серверу о статусе пароля */
  1459. HTTP_StartResetTask(true);
  1460. }
  1461. /* else
  1462. *bufOut = '0';*/
  1463. //*lenBufOut = 1;
  1464. }
  1465. }
  1466. /**
  1467. * @brief Проверка пароля для входа в Web
  1468. * @retval None
  1469. */
  1470. int HTTP_ConfirmWebPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1471. {
  1472. char tempStr[50];
  1473. char login[20];
  1474. char password[20];
  1475. uint8_t valueLen, user_id;
  1476. char *strPtr = 0;
  1477. char WebPassword[MAX_WEB_PASSWD_LEN];
  1478. char WebLogin[MAX_WEB_LOGIN_LEN];
  1479. char buf[40];
  1480. memset(login, 0, 20);
  1481. memset(password, 0, 20);
  1482. memset(tempStr, 0, 50);
  1483. /* Get first 50 bytes of string */
  1484. strncpy(tempStr, bufIn, 49);
  1485. /* Add " " to the string in order GetParamValue() can be able to parse the param */
  1486. strcat(tempStr, " ");
  1487. if (GetParamValue(tempStr, "login=", login, &valueLen) &&
  1488. GetParamValue(tempStr, "password=", password, &valueLen))
  1489. {
  1490. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  1491. GetUserLogin(user_id, WebLogin, &valueLen);
  1492. GetUserPassword(user_id, WebPassword, &valueLen);
  1493. /* Check login and password */
  1494. if ((strncmp(WebLogin, login, MAX_WEB_LOGIN_LEN) == 0) &&
  1495. (strncmp(WebPassword, password, MAX_WEB_PASSWD_LEN) == 0)) {
  1496. /* Login and pass are valid */
  1497. /* Check user's login session */
  1498. /* If "user" has logged in */
  1499. if (user_id >= 1) {
  1500. }
  1501. /* TODO replace global flag with user-pass-cookie */
  1502. Authenticated = true;
  1503. /* Generate cookie */
  1504. sprintf(tempStr, "%X", (unsigned int)GetRandomNumber());
  1505. /* Set users cookie */
  1506. HTTP_SetUserCookie(tempStr, user_id);
  1507. HTTP_UpdateUserLoginTime(user_id);
  1508. /* Send login and cookie back */
  1509. strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\nSet-Cookie: uname=");
  1510. strcat(bufOut, WebLogin);
  1511. strcat(bufOut, "\r\nSet-Cookie: id=");
  1512. strcat(bufOut, tempStr);
  1513. sprintf(tempStr, "%d", user_id);
  1514. strcat(bufOut, "\r\nSet-Cookie: role=");
  1515. strcat(bufOut, tempStr);
  1516. strcat(bufOut, "\r\n\r\n");
  1517. strcat(bufOut,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/index.html\"/></head></html>\r\n\r\n");
  1518. *lenBufOut = strlen(bufOut);
  1519. switch (user_id) {
  1520. case 0:
  1521. snprintf(buf, sizeof(buf), "Администратор");
  1522. break;
  1523. case 1:
  1524. snprintf(buf, sizeof(buf), "Пользователь");
  1525. break;
  1526. default:
  1527. snprintf(buf, sizeof(buf), "", login);
  1528. break;
  1529. }
  1530. log_event_data(LOG_LOGIN, buf);
  1531. /* Запускаем задачу-таймер логаута. */
  1532. /* TODO отправить ответ серверу о статусе пароля */
  1533. return SEND_REQUIRED_YES;
  1534. }
  1535. /*
  1536. else {
  1537. continue;
  1538. }
  1539. */
  1540. }
  1541. }
  1542. /* No valid login and pass found */
  1543. /* TODO replace global flag with user-pass-cookie*/
  1544. Authenticated = false;
  1545. /* Wrong login or pass, return */
  1546. return SEND_REQUIRED_NO;
  1547. }
  1548. void HTTP_LOGIN(char *bufOut, uint16_t *lenBufOut)
  1549. {
  1550. char tempStr[50];
  1551. uint8_t valueLen;
  1552. char WebLogin[MAX_WEB_LOGIN_LEN];
  1553. GetUserLogin(ADMIN, WebLogin, &valueLen);
  1554. memset(tempStr, 0, 50);
  1555. /* TODO replace global flag with user-pass-cookie */
  1556. Authenticated = true;
  1557. /* Generate cookie */
  1558. sprintf(tempStr, "%X", (unsigned int)GetRandomNumber());
  1559. /* Set users cookie */
  1560. HTTP_SetUserCookie(tempStr, ADMIN);
  1561. HTTP_UpdateUserLoginTime(ADMIN);
  1562. /* Send login and cookie back */
  1563. strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\nSet-Cookie: uname=");
  1564. strcat(bufOut, WebLogin);
  1565. strcat(bufOut, "\r\nSet-Cookie: id=");
  1566. strcat(bufOut, tempStr);
  1567. strcat(bufOut, "\r\nSet-Cookie: role=0");
  1568. strcat(bufOut, "\r\nSet-Cookie: auth=0");
  1569. strcat(bufOut, "\r\n\r\n");
  1570. strcat(bufOut,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/index.html\"/></head></html>\r\n\r\n");
  1571. *lenBufOut = strlen(bufOut);
  1572. }
  1573. /**
  1574. * @brief
  1575. * @retval None
  1576. */
  1577. uint8_t GetParamValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
  1578. {
  1579. char *beginValue = 0;
  1580. char *endValue = 0;
  1581. int len = 0;
  1582. char *strPtr = 0;
  1583. strPtr = strstr(inStr, paramName);
  1584. if (strPtr != 0)
  1585. {
  1586. beginValue = strpbrk(strPtr,"=");
  1587. endValue = strpbrk(strPtr,"&");
  1588. if (endValue == 0)
  1589. endValue = strpbrk(strPtr," ");
  1590. len = endValue - beginValue - 1;
  1591. strncpy(paramValue, beginValue + 1, len);
  1592. *endValue = '0';
  1593. *beginValue = '0';
  1594. *paramLen = len;
  1595. return 1;
  1596. }
  1597. else
  1598. {
  1599. *paramLen = 0;
  1600. return 0;
  1601. }
  1602. }
  1603. /**
  1604. * @brief
  1605. * @retval None
  1606. */
  1607. uint8_t GetCookieValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
  1608. {
  1609. char *beginValue = 0;
  1610. char *endValue = 0;
  1611. int len = 0;
  1612. char *strPtr = 0;
  1613. strPtr = strstr(inStr, paramName);
  1614. if (strPtr != 0)
  1615. {
  1616. beginValue = strpbrk(strPtr,"=");
  1617. endValue = strpbrk(strPtr,";");
  1618. if (endValue == 0)
  1619. endValue = strpbrk(strPtr,"\n");
  1620. len = endValue - beginValue - 1;
  1621. strncpy(paramValue, beginValue + 1, len);
  1622. *endValue = '0';
  1623. *beginValue = '0';
  1624. *paramLen = len;
  1625. return 1;
  1626. }
  1627. else
  1628. {
  1629. *paramLen = 0;
  1630. return 0;
  1631. }
  1632. }
  1633. /**
  1634. * @brief
  1635. * @retval None
  1636. */
  1637. /*
  1638. uint8_t GetParamValueInEnd(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
  1639. {
  1640. char *beginValue = 0;
  1641. char *endValue = 0;
  1642. int len = 0;
  1643. char *strPtr = 0;
  1644. strPtr = strstr(inStr, paramName);
  1645. if (strPtr != 0)
  1646. {
  1647. beginValue = strpbrk(strPtr,"=");
  1648. endValue = strpbrk(strPtr," ");
  1649. len = endValue - beginValue - 1;
  1650. strncpy(paramValue, beginValue + 1, len);
  1651. *endValue = '0';
  1652. *beginValue = '0';
  1653. *paramLen = len;
  1654. return 1;
  1655. }
  1656. else
  1657. {
  1658. *paramLen = 0;
  1659. return 0;
  1660. }
  1661. }
  1662. */
  1663. void ClearParamString(char *inBuf)
  1664. {
  1665. uint16_t len;
  1666. char *str;
  1667. str = strstr(inBuf, "HTTP");
  1668. if (str != 0)
  1669. {
  1670. len = str - inBuf;
  1671. memset(str, 0, RECIVE_BUF_MAX_LEN - len - 1);
  1672. }
  1673. }
  1674. /**
  1675. * @brief Чтение Cookie пользователя
  1676. */
  1677. static void HTTP_GetUserCookie(uint8_t user_id, char *str, uint8_t *len)
  1678. {
  1679. sprintf(str, "%s", users[user_id].cookie);
  1680. *len = strlen(str);
  1681. }
  1682. /**
  1683. * @brief Установка Cookie пользователя
  1684. */
  1685. static void HTTP_SetUserCookie(char *str, uint8_t user_id)
  1686. {
  1687. strcpy(users[user_id].cookie, str);
  1688. }
  1689. /**
  1690. * @brief Обновление времени последней активности пользователя
  1691. */
  1692. static void HTTP_UpdateUserLoginTime(uint8_t user_id)
  1693. {
  1694. xTimerStart(users[user_id].LogoutTimer, 0);
  1695. }
  1696. /**
  1697. * @brief Extract the Content_Length data from HTML data
  1698. * @param data : pointer on receive packet buffer
  1699. * @param len : buffer length
  1700. * @retval size : Content_length in numeric format
  1701. */
  1702. static uint32_t Parse_Content_Length(char *data, uint32_t len)
  1703. {
  1704. uint32_t i=0,size=0, S=1;
  1705. int32_t j=0;
  1706. char sizestring[6], *ptr;
  1707. ContentLengthOffset =0;
  1708. /* find Content-Length data in packet buffer */
  1709. for (i=0;i<len;i++)
  1710. {
  1711. if (strncmp ((char*)(data+i), Content_Length, 16)==0)
  1712. {
  1713. ContentLengthOffset = i+16;
  1714. break;
  1715. }
  1716. }
  1717. /* read Content-Length value */
  1718. if (ContentLengthOffset)
  1719. {
  1720. i=0;
  1721. ptr = (char*)(data + ContentLengthOffset);
  1722. while(*(ptr+i)!=0x0d)
  1723. {
  1724. sizestring[i] = *(ptr+i);
  1725. i++;
  1726. ContentLengthOffset++;
  1727. }
  1728. if (i>0)
  1729. {
  1730. /* transform string data into numeric format */
  1731. for(j=i-1;j>=0;j--)
  1732. {
  1733. size += (sizestring[j]-0x30)*S;
  1734. S=S*10;
  1735. }
  1736. }
  1737. }
  1738. return size;
  1739. }
  1740. /**
  1741. * @brief Принудительный логаут пользователя
  1742. */
  1743. static void HTTP_ForceUserLogout(uint8_t user_id)
  1744. {
  1745. char cookie[MAX_WEB_COOKIE_LEN];
  1746. /* Flush user cookie by random value */
  1747. sprintf(cookie, "%X", (unsigned int)GetRandomNumber());
  1748. HTTP_SetUserCookie(cookie, user_id);
  1749. }
  1750. /**
  1751. * @brief >Callback таймера логаута пользователя
  1752. */
  1753. void LogoutTimerCallback(TimerHandle_t pxTimer) {
  1754. uint8_t user_id = (uint8_t)pvTimerGetTimerID( pxTimer );
  1755. if( sSettings.sRADIUS.Auth_enable )
  1756. HTTP_ForceUserLogout(user_id);
  1757. }
  1758. /**
  1759. * @brief Смена пароля пользователя
  1760. * @retval None
  1761. */
  1762. int HTTP_ChangeUserPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1763. {
  1764. char tempStr[110];
  1765. char value[20];
  1766. char login[20];
  1767. char password[20];
  1768. uint8_t valueLen, valueLen2, user_id;
  1769. char WebLogin[MAX_WEB_LOGIN_LEN];
  1770. (void)lenBufIn;
  1771. memset(login, 0, 20);
  1772. memset(password, 0, 20);
  1773. memset(tempStr, 0, 50);
  1774. memset(value, 0, 20);
  1775. ClearParamString(bufIn);
  1776. strncpy(tempStr, bufIn, 110);
  1777. strcpy(bufOut, HTTP_200_OK);
  1778. if (GetParamValue(tempStr, "username=", login, &valueLen) &&
  1779. GetParamValue(tempStr, "oldpass=", password, &valueLen))
  1780. {
  1781. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++)
  1782. {
  1783. memset(value, 0, 20);
  1784. memset(WebLogin, 0, MAX_WEB_LOGIN_LEN);
  1785. GetUserLogin(user_id, WebLogin, &valueLen);
  1786. GetUserPassword(user_id, value, &valueLen2);
  1787. /* Check login and password */
  1788. if ((strncmp(WebLogin, login, MAX_WEB_LOGIN_LEN) == 0) &&
  1789. (memcmp(password, value, 11) == 0))
  1790. {
  1791. memset(password, 0, 20);
  1792. if (GetParamValue(tempStr, "newpass=", password, &valueLen))
  1793. {
  1794. memcpy(sSettings.sAuth[user_id].password, password, 11);
  1795. HTTP_SaveSettings();
  1796. log_event_data(LOG_PSW_CHANGE, "Администратор");
  1797. strcat(bufOut, "Пароль успешно изменён");
  1798. *lenBufOut = strlen(bufOut);
  1799. return SEND_REQUIRED_YES;
  1800. }
  1801. else {
  1802. strcat(bufOut, "Введены некорректные данные!");
  1803. *lenBufOut = strlen(bufOut);
  1804. return SEND_REQUIRED_YES;
  1805. }
  1806. }
  1807. }
  1808. strcat(bufOut, "Введён неверный пароль!");
  1809. *lenBufOut = strlen(bufOut);
  1810. return SEND_REQUIRED_YES;
  1811. }
  1812. else {
  1813. strcat(bufOut, "Введены некорректные данные!");
  1814. *lenBufOut = strlen(bufOut);
  1815. return SEND_REQUIRED_YES;
  1816. }
  1817. }
  1818. // -----------------------------------------------------------------------------
  1819. //
  1820. //
  1821. // SSL тесты
  1822. //
  1823. //
  1824. // -----------------------------------------------------------------------------
  1825. //#include "stm32f4xx.h"
  1826. #include "rtc.h"
  1827. /* PolarSSL includes */
  1828. #include "polarssl/net.h"
  1829. #include "polarssl/ssl.h"
  1830. #include "polarssl/havege.h"
  1831. #include "polarssl/certs.h"
  1832. #include "polarssl/x509.h"
  1833. /* Private typedef -----------------------------------------------------------*/
  1834. /* Private define ------------------------------------------------------------*/
  1835. #define DEBUG_LEVEL 1 /* Set DEBUG_LEVEL if you want to enable SSL debug
  1836. option, this should be set to 2, 3, 4 or 5 */
  1837. #define HTTP_RESPONSE "<p><p>Successful connection using: %s\r\n"
  1838. /* Format of dynamic web page */
  1839. #define PAGE_START \
  1840. "<html>\
  1841. <head>\
  1842. </head>\
  1843. <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\" bgcolor=\"#FFFFFF\" text=\"#000000\">\
  1844. <font size=\"4\" color=\"#0000FF\"><b>STM32F407xx : SSL Server Demo not using HW Crypto :<)</font></b></i>\
  1845. <br><br><pre>\r\nPage Hits = "
  1846. #define PAGE_END \
  1847. " \r\n</pre><br><br><hr>\
  1848. <font size=\"3\" color=\"#808080\">All rights reserved ©2017 ROTEK\
  1849. \r\n</font></BODY>\
  1850. </html>"
  1851. /* Private macro -------------------------------------------------------------*/
  1852. /* Private variables ---------------------------------------------------------*/
  1853. /* SSL structures */
  1854. rng_state rngs;
  1855. ssl_context ssl;
  1856. ssl_session ssn;
  1857. x509_cert srvcert;
  1858. rsa_context rsa;
  1859. uint32_t nPageHits = 0;
  1860. /* Private functions ---------------------------------------------------------*/
  1861. /*
  1862. * Computing a "safe" DH-1024 prime can take a very
  1863. * long time, so a precomputed value is provided below.
  1864. * You may run dh_genprime to generate a new value.
  1865. */
  1866. char *my_dhm_P =
  1867. "E4004C1F94182000103D883A448B3F80" \
  1868. "2CE4B44A83301270002C20D0321CFD00" \
  1869. "11CCEF784C26A400F43DFB901BCA7538" \
  1870. "F2C6B176001CF5A0FD16D2C48B1D0C1C" \
  1871. "F6AC8E1DA6BCC3B4E1F96B0564965300" \
  1872. "FFA1D0B601EB2800F489AA512C4B248C" \
  1873. "01F76949A60BB7F00A40B1EAB64BDD48" \
  1874. "E8A700D60B7F1200FA8E77B0A979DABF";
  1875. char *my_dhm_G = "4";
  1876. /*
  1877. * Sorted by order of preference
  1878. */
  1879. int my_ciphersuites[] =
  1880. {
  1881. SSL_EDH_RSA_AES_256_SHA,
  1882. SSL_EDH_RSA_CAMELLIA_256_SHA,
  1883. SSL_EDH_RSA_AES_128_SHA,
  1884. SSL_EDH_RSA_CAMELLIA_128_SHA,
  1885. SSL_EDH_RSA_DES_168_SHA,
  1886. SSL_RSA_AES_256_SHA,
  1887. SSL_RSA_CAMELLIA_256_SHA,
  1888. SSL_RSA_AES_128_SHA,
  1889. SSL_RSA_CAMELLIA_128_SHA,
  1890. SSL_RSA_DES_168_SHA,
  1891. SSL_RSA_RC4_128_SHA,
  1892. SSL_RSA_RC4_128_MD5,
  1893. 0
  1894. };
  1895. void my_debug(void *ctx, int level, const char *str)
  1896. {
  1897. if(level < DEBUG_LEVEL)
  1898. {
  1899. printf("\r%s", str);
  1900. }
  1901. }
  1902. /*
  1903. * These session callbacks use a simple chained list
  1904. * to store and retrieve the session information.
  1905. */
  1906. ssl_session *s_list_1st = NULL;
  1907. ssl_session *cur, *prv;
  1908. static int my_get_session(ssl_context *ssl)
  1909. {
  1910. time_t t = RTC_GetUnixTime(); //time(NULL);
  1911. if(ssl->resume == 0)
  1912. return(1);
  1913. cur = s_list_1st;
  1914. prv = NULL;
  1915. while(cur != NULL)
  1916. {
  1917. prv = cur;
  1918. cur = cur->next;
  1919. if(ssl->timeout != 0 && t - prv->start > ssl->timeout)
  1920. continue;
  1921. if( ssl->session->ciphersuite != prv->ciphersuite ||
  1922. ssl->session->length != prv->length)
  1923. continue;
  1924. if(memcmp( ssl->session->id, prv->id, prv->length ) != 0)
  1925. continue;
  1926. memcpy(ssl->session->master, prv->master, 48);
  1927. return(0);
  1928. }
  1929. return(1);
  1930. }
  1931. static int my_set_session(ssl_context *ssl)
  1932. {
  1933. time_t t = RTC_GetUnixTime(); //time(NULL);
  1934. cur = s_list_1st;
  1935. prv = NULL;
  1936. while(cur != NULL)
  1937. {
  1938. if(ssl->timeout != 0 && t - cur->start > ssl->timeout)
  1939. break; /* expired, reuse this slot */
  1940. if(memcmp( ssl->session->id, cur->id, cur->length ) == 0)
  1941. break; /* client reconnected */
  1942. prv = cur;
  1943. cur = cur->next;
  1944. }
  1945. if(cur == NULL)
  1946. {
  1947. cur = (ssl_session *) malloc(sizeof(ssl_session));
  1948. if(cur == NULL)
  1949. return(1);
  1950. if(prv == NULL)
  1951. s_list_1st = cur;
  1952. else prv->next = cur;
  1953. }
  1954. memcpy(cur, ssl->session, sizeof(ssl_session));
  1955. return(0);
  1956. }
  1957. /**
  1958. * @brief SSL Server task.
  1959. * @param pvParameters not used
  1960. * @retval None
  1961. */
  1962. void ssl_server(void *pvParameters)
  1963. {
  1964. int ret, len;
  1965. int listen_fd;
  1966. int client_fd;
  1967. char buf[1024];
  1968. char *ptr;
  1969. char CookieBuf[50];
  1970. char *CookiePtr = NULL;
  1971. char name[MAX_WEB_COOKIE_LEN];
  1972. char id[MAX_WEB_COOKIE_LEN];
  1973. uint8_t nameLen = 0, idLen = 0;
  1974. uint32_t DataOffset;
  1975. struct fs_file file = {0, 0};
  1976. uint32_t i;
  1977. memset(&srvcert, 0, sizeof(x509_cert));
  1978. // 1. Load the certificates and private RSA key
  1979. //printf("\n\r Loading the server certificates and key...");
  1980. // This demonstration program uses embedded test certificates.
  1981. // Instead, you may want to use x509parse_crtfile() to read the
  1982. // server and CA certificates, as well as x509parse_keyfile().
  1983. ret = x509parse_crt(&srvcert, (unsigned char *) test_srv_crt, strlen(test_srv_crt));
  1984. if(ret != 0)
  1985. {
  1986. //printf(" failed\n ! x509parse_crt returned %d\n\r", ret);
  1987. goto exit;
  1988. }
  1989. ret = x509parse_crt(&srvcert, (unsigned char *) test_ca_crt, strlen(test_ca_crt));
  1990. if(ret != 0)
  1991. {
  1992. //printf(" failed\n ! x509parse_crt returned %d\n\r", ret);
  1993. goto exit;
  1994. }
  1995. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  1996. ret = x509parse_key(&rsa, (unsigned char *) test_srv_key, strlen(test_srv_key), NULL, 0);
  1997. if( ret != 0 )
  1998. {
  1999. //printf(" failed\n ! x509parse_key returned %d\n\r", ret);
  2000. goto exit;
  2001. }
  2002. // 2. Setup the listening TCP socket
  2003. //printf("\n\r Bind to https port ...");
  2004. // Bind the connection to https port : 443
  2005. ret = net_bind(&listen_fd, NULL, 443);
  2006. if(ret != 0)
  2007. {
  2008. //printf(" failed\n ! net_bind returned %d\n\r", ret);
  2009. goto exit;
  2010. }
  2011. //printf(" ok\n\r");
  2012. // 3. Wait until a client connects
  2013. for(;;)
  2014. {
  2015. //printf("\n\r Waiting for a remote connection ...");
  2016. ret = net_accept(listen_fd, &client_fd, NULL);
  2017. if(ret != 0)
  2018. {
  2019. //printf(" failed\n ! net_accept returned %d\n\n", ret);
  2020. goto exit;
  2021. }
  2022. //printf(" ok\n");
  2023. // 4. Initialize the session data
  2024. //printf("\n\r Setting up the RNG and SSL data....");
  2025. // Initialize the SSL context
  2026. ret = ssl_init(&ssl);
  2027. if(ret != 0)
  2028. {
  2029. //printf(" failed\n ! ssl_init returned %d\n\n", ret);
  2030. goto accept;
  2031. }
  2032. //printf(" ok\n");
  2033. // Set the current session as SSL server
  2034. ssl_set_endpoint(&ssl, SSL_IS_SERVER);
  2035. // No certificate verification
  2036. ssl_set_authmode(&ssl, SSL_VERIFY_NONE);
  2037. // Set the random number generator callback function
  2038. ssl_set_rng(&ssl, RandVal, &rngs);
  2039. // Set the debug callback function
  2040. //ssl_set_dbg(&ssl, my_debug, stdout);
  2041. // Set read and write callback functions
  2042. ssl_set_bio(&ssl, net_recv, &client_fd, net_send, &client_fd);
  2043. // Set the session callback functions
  2044. ssl_set_scb(&ssl, my_get_session, my_set_session);
  2045. // The list of ciphersuites to be used in this session
  2046. ssl_set_ciphersuites(&ssl, my_ciphersuites);
  2047. // Set the session resuming flag, timeout and session context
  2048. ssl_set_session(&ssl, 1, 0, &ssn);
  2049. memset(&ssn, 0, sizeof(ssl_session));
  2050. // Set the data required to verify peer certificate
  2051. ssl_set_ca_chain(&ssl, srvcert.next, NULL, NULL);
  2052. // Set own certificate and private key
  2053. ssl_set_own_cert(&ssl, &srvcert, &rsa);
  2054. // Set the Diffie-Hellman public P and G values
  2055. ssl_set_dh_param(&ssl, my_dhm_P, my_dhm_G);
  2056. // 5. Handshake protocol
  2057. //printf("\n\r Performing the SSL/TLS handshake...");
  2058. // Perform the SSL handshake protocol
  2059. while((ret = ssl_handshake(&ssl)) != 0)
  2060. {
  2061. if(ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE)
  2062. {
  2063. //printf(" failed\n ! ssl_handshake returned %d\n\n", ret);
  2064. goto accept;
  2065. }
  2066. }
  2067. //printf(" ok\n");
  2068. // 6. Read the HTTP Request
  2069. //printf("\n\r <= Read from client :");
  2070. do
  2071. {
  2072. receivedBufLen = RECIVE_BUF_MAX_LEN - 1;
  2073. memset(receiveBuf, 0, RECIVE_BUF_MAX_LEN);
  2074. // Read decrypted application data
  2075. ret = ssl_read(&ssl, (unsigned char*)receiveBuf, receivedBufLen);
  2076. if(ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE)
  2077. continue;
  2078. if(ret <= 0)
  2079. {
  2080. switch(ret)
  2081. {
  2082. case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
  2083. printf("\n\r connection was closed \n");
  2084. break;
  2085. case POLARSSL_ERR_NET_CONN_RESET:
  2086. printf("\n\r connection was reset by peer\n");
  2087. break;
  2088. default:
  2089. //printf("\n\r ssl_read returned %d\n", ret);
  2090. break;
  2091. }
  2092. break;
  2093. }
  2094. receivedBufLen = ret;
  2095. // Display the length of read data
  2096. //printf("\n\r Successfully read %d bytes from client \n\r",len);
  2097. }while(0);
  2098. // -------------------------------------------------------------------------
  2099. //receivedBufLen = p->tot_len;
  2100. //memcpy(receiveBuf, p->payload , receivedBufLen);
  2101. receiveBuf[receivedBufLen] = '\0';
  2102. // printf("receive %s \r\n", receiveBuf);
  2103. // Get cookie "uname" value
  2104. CookiePtr = strstr(receiveBuf, "uname=");
  2105. strncpy(CookieBuf, CookiePtr, 50);
  2106. //printf("********CookieBuf1= %s\r\n", CookieBuf);
  2107. memset(name, 0, MAX_WEB_COOKIE_LEN);
  2108. GetCookieValue(CookieBuf, "uname=", name, &nameLen);
  2109. //printf("********CookieBuf2= %s\r\n", CookieBuf);
  2110. //printf("********uname= %s\r\n", name);
  2111. // Get cookie "id" value
  2112. CookiePtr = strstr(receiveBuf, "id=");
  2113. strncpy(CookieBuf, CookiePtr, 50);
  2114. //printf("********CookieBuf1= %s\r\n", CookieBuf);
  2115. memset(id, 0, MAX_WEB_COOKIE_LEN);
  2116. GetCookieValue(CookieBuf, "id=", id, &idLen);
  2117. //printf("********CookieBuf2= %s\r\n", CookieBuf);
  2118. //printf("********id= %s\r\n", id);
  2119. // Id of currently logged-in user
  2120. uint8_t user_id;
  2121. if( DataFlag == 0 && DataFlag2 == 0) {
  2122. // Level of currently logged-in user
  2123. seclevel = 0xFF;
  2124. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  2125. HTTP_GetUserCookie(user_id, CookieBuf, &idLen);
  2126. if (strncmp(id, CookieBuf, idLen) == 0 ) {
  2127. GetUserLevelInt(user_id, &seclevel);
  2128. Authenticated = true;
  2129. break;
  2130. }
  2131. Authenticated = false;
  2132. seclevel = 0xFF;
  2133. }
  2134. }
  2135. if (DataFlag >= 1)
  2136. Authenticated = true;
  2137. else if(DataFlag2 >= 1)
  2138. Authenticated = true;
  2139. if ( Authenticated == false && sSettings.sRADIUS.Auth_enable == false)
  2140. {
  2141. HTTP_LOGIN(sendBuf, &sendBufLoadLen);
  2142. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2143. }
  2144. else if ( Authenticated == false )//&& sSettings.sRADIUS.Auth_enable == true
  2145. {
  2146. if (strncmp(receiveBuf, "GET /main.css", 13) == 0) // +
  2147. {
  2148. fs_open("/main.css", &file);
  2149. ssl_sendframes(&ssl, file.data, file.len);
  2150. }
  2151. else if (strncmp(receiveBuf, "GET /rotek.png", 14) == 0) // +
  2152. {
  2153. fs_open("/rotek.png", &file);
  2154. ssl_sendframes(&ssl, file.data, file.len);
  2155. }
  2156. else if (strncmp(receiveBuf, "GET /favicon.ico", 16) == 0) // ?
  2157. {
  2158. fs_open("/favicon.ico", &file);
  2159. ssl_sendframes(&ssl, file.data, file.len);
  2160. }
  2161. else if (strncmp(receiveBuf, "GET /role.js", 12) == 0)
  2162. {
  2163. fs_open("/role.js", &file);
  2164. ssl_sendframes(&ssl, file.data, file.len);
  2165. }
  2166. else if ((strncmp(receiveBuf, "POST /login.cgi", 15) == 0) || (log_post_reqn > 0))
  2167. {
  2168. uint32_t i, offset = 0, req_data_received = 0;
  2169. post_data_count = Parse_Content_Length(receiveBuf, receivedBufLen);
  2170. if (post_data_count < MAX_POST_REQ_LEN)
  2171. {
  2172. memset(post_req_data, 0, MAX_POST_REQ_LEN);
  2173. for (i = 0; i < receivedBufLen; i++)
  2174. {
  2175. if (strncmp ((char*)(receiveBuf+i), "\r\n\r\n", 4) == 0)
  2176. {
  2177. offset = i+4;
  2178. break;
  2179. }
  2180. }
  2181. req_data_received = receivedBufLen - offset;
  2182. if (offset != 0)
  2183. {
  2184. if (req_data_received < post_data_count)
  2185. {
  2186. snprintf(post_req_data, req_data_received, "%s", receiveBuf);
  2187. post_data_count -= req_data_received;
  2188. }
  2189. else
  2190. {
  2191. strncat(post_req_data, (char *)(receiveBuf + offset), post_data_count);
  2192. if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES)
  2193. {
  2194. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2195. }
  2196. else
  2197. {
  2198. fs_open("/login.html", &file);
  2199. ssl_sendframes(&ssl, file.data, file.len);
  2200. }
  2201. post_data_count = 0;
  2202. log_post_reqn = 0;
  2203. }
  2204. }
  2205. /* request was fragmented before "\r\n\r\n" */
  2206. else
  2207. {
  2208. log_post_reqn++;
  2209. /* wait max 2 requests */
  2210. if (log_post_reqn > 1)
  2211. {
  2212. /* Redirect to login page */
  2213. fs_open("/login.html", &file);
  2214. ssl_sendframes(&ssl, file.data, file.len);
  2215. /* End reqest */
  2216. post_data_count = 0;
  2217. log_post_reqn = 0;
  2218. }
  2219. }
  2220. }
  2221. else
  2222. {
  2223. //printf("Too long POST request!\r\n");
  2224. /* Ignore request */
  2225. post_data_count = 0;
  2226. log_post_reqn = 0;
  2227. /* Redirect to login page */
  2228. fs_open("/login.html", &file);
  2229. ssl_sendframes(&ssl, file.data, file.len);
  2230. }
  2231. }
  2232. else if (post_data_count > 0)
  2233. {
  2234. strncat(post_req_data, receiveBuf, post_data_count);
  2235. if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES)
  2236. {
  2237. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2238. }
  2239. else
  2240. {
  2241. fs_open("/login.html", &file);
  2242. ssl_sendframes(&ssl, file.data, file.len);
  2243. }
  2244. post_data_count = 0;
  2245. log_post_reqn = 0;
  2246. }
  2247. else
  2248. {
  2249. fs_open("/login.html", &file);
  2250. ssl_sendframes(&ssl, file.data, file.len);
  2251. }
  2252. }
  2253. else if ( Authenticated == true )
  2254. {
  2255. if (strncmp(receiveBuf, "GET /main.css", 13) == 0) // +
  2256. {
  2257. fs_open("/main.css", &file);
  2258. ssl_sendframes(&ssl, file.data, file.len);
  2259. }
  2260. else if (strncmp(receiveBuf, "GET /rotek.png", 14) == 0) // +
  2261. {
  2262. fs_open("/rotek.png", &file);
  2263. ssl_sendframes(&ssl, file.data, file.len);
  2264. }
  2265. else if (strncmp(receiveBuf, "GET /favicon.ico", 16) == 0) // ?
  2266. {
  2267. fs_open("/favicon.ico", &file);
  2268. ssl_sendframes(&ssl, file.data, file.len);
  2269. }
  2270. else if (strncmp(receiveBuf, "GET /main.js", 12) == 0) // +
  2271. {
  2272. fs_open("/main.js", &file);
  2273. ssl_sendframes(&ssl, file.data, file.len);
  2274. }
  2275. else if (strncmp(receiveBuf, "GET /role.js", 12) == 0)
  2276. {
  2277. fs_open("/role.js", &file);
  2278. ssl_sendframes(&ssl, file.data, file.len);
  2279. }
  2280. else if (strncmp(receiveBuf, "GET /settings.html", 18) == 0) // +
  2281. {
  2282. HTTP_UpdateUserLoginTime(user_id);
  2283. if (seclevel == 0)
  2284. {
  2285. fs_open("/settings.html", &file);
  2286. ssl_sendframes(&ssl, file.data, file.len);
  2287. }
  2288. else
  2289. {
  2290. fs_open("/index.html", &file);
  2291. ssl_sendframes(&ssl, file.data, file.len);
  2292. }
  2293. }
  2294. else if (strncmp(receiveBuf, "GET /info.html", 14) == 0) // +
  2295. {
  2296. HTTP_UpdateUserLoginTime(user_id);
  2297. fs_open("/info.html", &file);
  2298. ssl_sendframes(&ssl, file.data, file.len);
  2299. }
  2300. else if (strncmp(receiveBuf, "GET /history.html", 17) == 0)
  2301. {
  2302. HTTP_UpdateUserLoginTime(user_id);
  2303. fs_open("/history.html", &file);
  2304. ssl_sendframes(&ssl, file.data, file.len);
  2305. }
  2306. else if (strncmp(receiveBuf, "GET /ups_history.html", 21) == 0)
  2307. {
  2308. HTTP_UpdateUserLoginTime(user_id);
  2309. fs_open("/ups_history.html", &file);
  2310. ssl_sendframes(&ssl, file.data, file.len);
  2311. }
  2312. else if (strncmp(receiveBuf, "GET /getJson.cgi", 16) == 0) // +
  2313. {
  2314. HTTP_GetParamsPage1(sendBuf);
  2315. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2316. }
  2317. else if (strncmp(receiveBuf, "GET /settings.cgi", 17) == 0) // +
  2318. {
  2319. if (seclevel == 0) {
  2320. SET_PAGE = SET_PAGE_PAGE2;
  2321. if (HTTP_SettingsPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  2322. {
  2323. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2324. }
  2325. }
  2326. }
  2327. else if (strncmp(receiveBuf, "POST /settings.cgi", 18) == 0)
  2328. {
  2329. if (seclevel == 0)
  2330. {
  2331. DataOffset = 0;
  2332. // POST Packet received
  2333. if (DataFlag2 == 0)
  2334. {
  2335. BrowserFlag = 0;
  2336. TotalReceived = 0;
  2337. memset(sendBuf, 0, strlen(sendBuf));
  2338. // parse packet for Content-length field
  2339. size = Parse_Content_Length(receiveBuf, receivedBufLen);
  2340. // parse packet for the octet-stream field
  2341. for (i = 0; i < receivedBufLen; i++)
  2342. {
  2343. if (strncmp ((char*)(receiveBuf+i), "managerIP", 8)==0)
  2344. {
  2345. DataOffset = i;
  2346. break;
  2347. }
  2348. }
  2349. /* case of MSIE8 : we do not receive data in the POST packet*/
  2350. if (DataOffset == 0)
  2351. {
  2352. DataFlag2++;
  2353. BrowserFlag = 1;
  2354. //pbuf_free(p);
  2355. return;
  2356. }
  2357. // case of Mozilla Firefox v3.6 : we receive data in the POST packet
  2358. else
  2359. {
  2360. //TotalReceived = receivedBufLen - (ContentLengthOffset + 4);
  2361. TotalReceived = receivedBufLen - DataOffset;
  2362. }
  2363. }
  2364. if (((DataFlag2 ==1)&&(BrowserFlag==1)) || ((DataFlag2 ==0)&&(BrowserFlag==0)))
  2365. {
  2366. if ((DataFlag2 ==0)&&(BrowserFlag==0))
  2367. {
  2368. DataFlag2++;
  2369. }
  2370. else if ((DataFlag2 ==1)&&(BrowserFlag==1))
  2371. {
  2372. // parse packet for the octet-stream field */
  2373. for (i = 0; i < receivedBufLen; i++)
  2374. {
  2375. if (strncmp ((char*)(receiveBuf+i), "managerIP", 8)==0)
  2376. {
  2377. DataOffset = i;
  2378. break;
  2379. }
  2380. }
  2381. TotalReceived += receivedBufLen;
  2382. DataFlag2++;
  2383. }
  2384. TotalData = 0;
  2385. }
  2386. // DataFlag >1 => the packet is data only
  2387. else
  2388. {
  2389. TotalReceived +=receivedBufLen;
  2390. }
  2391. ptr = (char*)(receiveBuf + DataOffset);
  2392. receivedBufLen-= DataOffset;
  2393. // update Total data received counter
  2394. TotalData +=receivedBufLen;
  2395. // check if last data packet
  2396. if (TotalReceived == size)
  2397. {
  2398. //DBG printf("State: Received %d bytes\r\n", (int)TotalReceived);
  2399. strncat(sendBuf, ptr, receivedBufLen);
  2400. strncat(sendBuf, " ", 1);
  2401. //ВBG printf("receive %s /r/n", sendBuf);
  2402. HTTP_SetSettings(sendBuf, strlen(sendBuf));
  2403. DataFlag2=0;
  2404. BrowserFlag = 0;
  2405. memset(sendBuf, 0, size);
  2406. strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
  2407. strcat(sendBuf, "\r\n\r\n");
  2408. strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/settings.html\"/></head></html>\r\n\r\n");
  2409. sendBufLoadLen = strlen(sendBuf);
  2410. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2411. }
  2412. // not last data packet
  2413. else
  2414. {
  2415. // write data in flash
  2416. if(receivedBufLen)
  2417. {
  2418. strncat(sendBuf, ptr, receivedBufLen);
  2419. //memcpy(receiveBufTemp, ptr, receivedBufLen);
  2420. }
  2421. }
  2422. }
  2423. }
  2424. else if (strncmp(receiveBuf, "GET /info.cgi", 13) == 0) // +
  2425. {
  2426. if (HTTP_InfoPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  2427. {
  2428. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2429. }
  2430. }
  2431. else if (strncmp(receiveBuf, "POST /info.cgi", 14) == 0)
  2432. {
  2433. if (seclevel == 0)
  2434. {
  2435. DataOffset = 0;
  2436. // POST Packet received
  2437. if (DataFlag == 0)
  2438. {
  2439. BrowserFlag = 0;
  2440. TotalReceived = 0;
  2441. memset(sendBuf, 0, strlen(sendBuf));
  2442. // parse packet for Content-length field
  2443. size = Parse_Content_Length(receiveBuf, receivedBufLen);
  2444. // parse packet for the octet-stream field
  2445. for (i = 0; i < receivedBufLen; i++)
  2446. {
  2447. if (strncmp ((char*)(receiveBuf+i), "owner", 5)==0)
  2448. {
  2449. DataOffset = i;
  2450. break;
  2451. }
  2452. }
  2453. // case of MSIE8 : we do not receive data in the POST packet
  2454. if (DataOffset == 0)
  2455. {
  2456. DataFlag++;
  2457. BrowserFlag = 1;
  2458. //pbuf_free(p);
  2459. return;
  2460. }
  2461. // case of Mozilla Firefox v3.6 : we receive data in the POST packet*/
  2462. else
  2463. {
  2464. //TotalReceived = receivedBufLen - (ContentLengthOffset + 4);
  2465. TotalReceived = receivedBufLen - DataOffset;
  2466. }
  2467. }
  2468. if (((DataFlag ==1)&&(BrowserFlag==1)) || ((DataFlag ==0)&&(BrowserFlag==0)))
  2469. {
  2470. if ((DataFlag ==0)&&(BrowserFlag==0))
  2471. {
  2472. DataFlag++;
  2473. }
  2474. else if ((DataFlag ==1)&&(BrowserFlag==1))
  2475. {
  2476. // parse packet for the octet-stream field */
  2477. for (i = 0; i < receivedBufLen; i++)
  2478. {
  2479. if (strncmp ((char*)(receiveBuf+i), "owner", 5)==0)
  2480. {
  2481. DataOffset = i;
  2482. break;
  2483. }
  2484. }
  2485. TotalReceived += receivedBufLen;
  2486. DataFlag++;
  2487. }
  2488. TotalData =0 ;
  2489. }
  2490. // DataFlag >1 => the packet is data only
  2491. else
  2492. {
  2493. TotalReceived +=receivedBufLen;
  2494. }
  2495. ptr = (char*)(receiveBuf + DataOffset);
  2496. receivedBufLen-= DataOffset;
  2497. // update Total data received counter
  2498. TotalData +=receivedBufLen;
  2499. // check if last data packet
  2500. if (TotalReceived == size)
  2501. {
  2502. strncat(sendBuf, ptr, receivedBufLen);
  2503. strncat(sendBuf, " ", 1);
  2504. HTTP_SetInfo(sendBuf, strlen(sendBuf));
  2505. DataFlag = 0;
  2506. BrowserFlag = 0;
  2507. memset(sendBuf, 0, size);
  2508. strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
  2509. strcat(sendBuf, "\r\n\r\n");
  2510. strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/info.html\"/></head></html>\r\n\r\n");
  2511. sendBufLoadLen = strlen(sendBuf);
  2512. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2513. }
  2514. // not last data packet
  2515. else
  2516. {
  2517. // write data in flash
  2518. if(receivedBufLen)
  2519. {
  2520. strncat(sendBuf, ptr, receivedBufLen);
  2521. //memcpy(receiveBufTemp, ptr, receivedBufLen);
  2522. }
  2523. }
  2524. }
  2525. }
  2526. else if (strncmp(receiveBuf, "GET /history.cgi", 16) == 0)
  2527. {
  2528. int res;
  2529. res = HTTP_HistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2530. if (res == SEND_REQUIRED_FILE)
  2531. {
  2532. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2533. //send_data(pcb, hs);
  2534. //tcp_sent(pcb, http_sent_history);
  2535. HTTP_SendHistory();
  2536. //tcp_err(pcb, http_sent_log_err);
  2537. }
  2538. else if (res == SEND_REQUIRED_YES)
  2539. {
  2540. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2541. }
  2542. }
  2543. else if (strncmp(receiveBuf, "GET /ups_history.cgi", 19) == 0)
  2544. {
  2545. int res;
  2546. res = HTTP_UpsHistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2547. if (res == SEND_REQUIRED_FILE)
  2548. {
  2549. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2550. //send_data(pcb, hs);
  2551. //tcp_sent(pcb, http_sent_log);
  2552. HTTP_SendLog();
  2553. //tcp_err(pcb, http_sent_log_err);
  2554. }
  2555. else if (res == SEND_REQUIRED_YES)
  2556. {
  2557. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2558. }
  2559. }
  2560. /* Тест АКБ ИБП */
  2561. else if (strncmp(receiveBuf, "GET /bat_test.cgi", 17) == 0)
  2562. {
  2563. HTTP_UPSTest(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2564. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2565. }
  2566. /* Выключение ИБП */
  2567. else if (strncmp(receiveBuf, "GET /ups_power.cgi", 18) == 0)
  2568. {
  2569. HTTP_UPSshutdown(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2570. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2571. }
  2572. /* Сброс настроек и сохранине */
  2573. else if (strncmp(receiveBuf, "GET /reset.cgi", 14) == 0)
  2574. {
  2575. HTTP_ResetSettings();
  2576. HTTP_SaveSettings();
  2577. fs_open("/settings.html", &file);
  2578. ssl_sendframes(&ssl, file.data, file.len);
  2579. }
  2580. /* Перезагрузка контроллера */
  2581. else if (strncmp(receiveBuf, "GET /reboot.cgi", 15) == 0)
  2582. {
  2583. HTTP_Reboot();
  2584. }
  2585. /* Подтверждение новых сетевых настроек */
  2586. else if (strncmp(receiveBuf, "GET /confirm.cgi", 16) == 0)
  2587. {
  2588. SetWebReinitFlag(false);
  2589. SetConfirmWebParamsFlag();
  2590. fs_open("/index.html", &file);
  2591. ssl_sendframes(&ssl, file.data, file.len);
  2592. }
  2593. /* Проверка пароля, переход в bootloader */
  2594. else if (strncmp(receiveBuf, "GET /fw_update.cgi", 18) == 0)
  2595. {
  2596. HTTP_ConfirmBootPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2597. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2598. }
  2599. /* Смена пароля пользователя */
  2600. else if (strncmp(receiveBuf, "GET /changepwd.cgi", 18) == 0)
  2601. {
  2602. HTTP_ChangeUserPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2603. ssl_sendframes(&ssl, file.data, file.len);
  2604. }
  2605. // На производстве
  2606. else if (strncmp(receiveBuf, "GET /setProdate.cgi", 19) == 0)
  2607. {
  2608. HTTP_Prodate(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2609. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2610. }
  2611. else
  2612. {
  2613. HTTP_UpdateUserLoginTime(user_id);
  2614. fs_open("/index.html", &file); // +
  2615. ssl_sendframes(&ssl, file.data, file.len);
  2616. }
  2617. }
  2618. //}
  2619. /*
  2620. if (strncmp(buf, "GET /main.css", 13) == 0) // +
  2621. {
  2622. fs_open("/main.css", &file);
  2623. ssl_sendframes(&ssl, file.data, file.len);
  2624. }
  2625. else if (strncmp(buf, "GET /rotek.png", 14) == 0) // +
  2626. {
  2627. fs_open("/rotek.png", &file);
  2628. ssl_sendframes(&ssl, file.data, file.len);
  2629. }
  2630. else if (strncmp(buf, "GET /favicon.ico", 16) == 0) // ?
  2631. {
  2632. fs_open("/favicon.ico", &file);
  2633. ssl_sendframes(&ssl, file.data, file.len);
  2634. }
  2635. else if (strncmp(buf, "GET /main.js", 12) == 0) // +
  2636. {
  2637. fs_open("/main.js", &file);
  2638. ssl_sendframes(&ssl, file.data, file.len);
  2639. }
  2640. else if (strncmp(buf, "GET /getJson.cgi", 16) == 0) // +
  2641. {
  2642. HTTP_GetParamsPage1(sendBuf);
  2643. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2644. }
  2645. else if (strncmp(buf, "GET /settings.html", 18) == 0) // +
  2646. {
  2647. fs_open("/settings.html", &file);
  2648. ssl_sendframes(&ssl, file.data, file.len);
  2649. }
  2650. else if (strncmp(buf, "GET /info.html", 14) == 0) // +
  2651. {
  2652. fs_open("/info.html", &file);
  2653. ssl_sendframes(&ssl, file.data, file.len);
  2654. }
  2655. else if (strncmp(buf, "GET /getJson.cgi", 16) == 0) // +
  2656. {
  2657. HTTP_GetParamsPage1(sendBuf);
  2658. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2659. }
  2660. else if (strncmp(buf, "GET /settings.cgi", 17) == 0) // +
  2661. {
  2662. SET_PAGE = SET_PAGE_PAGE2;
  2663. if (HTTP_SettingsPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  2664. {
  2665. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2666. }
  2667. else
  2668. {
  2669. fs_open("/settings.html", &file);
  2670. ssl_sendframes(&ssl, file.data, file.len);
  2671. }
  2672. }
  2673. else if (strncmp(buf, "GET /info.cgi", 13) == 0) // +
  2674. {
  2675. if (HTTP_InfoPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  2676. {
  2677. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2678. }
  2679. else
  2680. {
  2681. fs_open("/info.html", &file);
  2682. ssl_sendframes(&ssl, file.data, file.len);
  2683. }
  2684. }
  2685. // Сброс настроек и сохранине
  2686. else if (strncmp(buf, "GET /reset.cgi", 14) == 0)
  2687. {
  2688. HTTP_ResetSettings();
  2689. HTTP_SaveSettings();
  2690. fs_open("/settings.html", &file);
  2691. ssl_sendframes(&ssl, file.data, file.len);
  2692. }
  2693. // Перезагрузка контроллера
  2694. else if (strncmp(buf, "GET /reboot.cgi", 15) == 0)
  2695. {
  2696. HTTP_Reboot();
  2697. }
  2698. // Подтверждение новых сетевых настроек
  2699. else if (strncmp(buf, "GET /confirm.cgi", 16) == 0)
  2700. {
  2701. SetWebReinitFlag(false);
  2702. SetConfirmWebParamsFlag();
  2703. fs_open("/index.html", &file);
  2704. ssl_sendframes(&ssl, file.data, file.len);
  2705. }
  2706. // Проверка пароля, переход в bootloader
  2707. else if (strncmp(buf, "POST /checkpwd.cgi", 18) == 0)
  2708. {
  2709. HTTP_ConfirmBootPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2710. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2711. }
  2712. // На производстве
  2713. else if (strncmp(buf, "GET /setProdate.cgi", 19) == 0)
  2714. {
  2715. HTTP_Prodate(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2716. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2717. }
  2718. // На производстве
  2719. else if (strncmp(buf, "GET /progon.cgi", 15) == 0)
  2720. {
  2721. HTTP_Progon(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2722. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2723. }
  2724. else
  2725. {
  2726. fs_open("/index.html", &file);
  2727. ssl_sendframes(&ssl, file.data, file.len);
  2728. }
  2729. // -------------------------------------------------------------------------
  2730. */
  2731. // Для теста
  2732. // 7. Write the Response
  2733. //printf("\n\r => Write to client :\n\r");
  2734. // Send the dynamic html page
  2735. //ssl_DynPage(&ssl);
  2736. // Close the connection
  2737. ssl_close_notify(&ssl);
  2738. goto accept;
  2739. exit:
  2740. // Close and delete the current session data: certificate, RSA key and SSL session
  2741. x509_free(&srvcert);
  2742. rsa_free(&rsa);
  2743. cur = s_list_1st;
  2744. while(cur != NULL)
  2745. {
  2746. prv = cur;
  2747. cur = cur->next;
  2748. memset(prv, 0, sizeof(ssl_session));
  2749. free(prv);
  2750. }
  2751. memset(&ssl, 0, sizeof(ssl_context));
  2752. accept:
  2753. // Wait 200s until next retry
  2754. vTaskDelay(200);
  2755. // Close and free the SSL context
  2756. net_close(client_fd);
  2757. ssl_free(&ssl);
  2758. }
  2759. }
  2760. /**
  2761. * @brief Create and send a dynamic Web Page. This page contains the list of
  2762. * running tasks and the number of page hits.
  2763. * @param ssl the SSL context
  2764. * @retval None
  2765. */
  2766. void ssl_DynPage(ssl_context *ssl)
  2767. {
  2768. portCHAR buf[2024];
  2769. portCHAR pagehits[10];
  2770. portCHAR getcipher[100];
  2771. uint32_t len = 0;
  2772. memset(buf, 0, 2024);
  2773. // Update the hit count
  2774. nPageHits++;
  2775. sprintf( pagehits, "%d", nPageHits );
  2776. sprintf( (char *) getcipher, HTTP_RESPONSE, ssl_get_ciphersuite(ssl));
  2777. // Generate the dynamic page
  2778. strcpy(buf, PAGE_START);
  2779. // Page header
  2780. strcat(buf, pagehits);
  2781. strcat((char *) buf, "<br><pre>** The list of tasks and their status **");
  2782. strcat((char *) buf, "<br><pre>---------------------------------------------");
  2783. strcat((char *) buf, "<br>Name State Priority Stack Num" );
  2784. //strcat((char *) buf, "<br>---------------------------------------------");
  2785. strcat((char *) buf, "<br>---------------------------------------------<br>");
  2786. // The list of tasks and their status
  2787. vTaskList((signed char *)buf + strlen(buf));
  2788. strcat((char *) buf, "<br>---------------------------------------------");
  2789. strcat((char *) buf, "<br>B : Blocked, R : Ready, D : Deleted, S : Suspended");
  2790. // The current cipher used
  2791. strcat(buf, getcipher);
  2792. // Page footer
  2793. strcat(buf, PAGE_END);
  2794. // Send the dynamically generated page
  2795. len = ssl_write(ssl, (unsigned char *)buf, strlen(buf));
  2796. // Display the length of application data
  2797. //printf( "\n Successfully write %d bytes to client\n\r", len);
  2798. }
  2799. /**
  2800. * @brief This function is used to send messages with size upper 16k bytes.
  2801. * @param ssl SSL context
  2802. * @param data buffer holding the data
  2803. * @param len how many bytes must be written
  2804. * @retval None
  2805. */
  2806. #define FRAME_SIZE (1000)
  2807. void ssl_sendframes( ssl_context *ssl, char *data, int datalen )
  2808. {
  2809. int index = 0;
  2810. int k = 0;
  2811. int lastframe, nbrframes;
  2812. // Calculate the number of frames
  2813. nbrframes = datalen / FRAME_SIZE;
  2814. // Send nbrframes frames
  2815. while(nbrframes > 0)
  2816. {
  2817. index = k * FRAME_SIZE;
  2818. ssl_write( ssl, (unsigned char *)(data + index), FRAME_SIZE );
  2819. nbrframes--;
  2820. k++;
  2821. }
  2822. // Send the last frame
  2823. index = k * FRAME_SIZE;
  2824. lastframe = datalen % FRAME_SIZE ;
  2825. ssl_write( ssl, (unsigned char *)(data + index), lastframe );
  2826. }
  2827. /**
  2828. * @brief Returns a 32-bit random number.
  2829. * @param arg not used
  2830. * @retval 32-bit random number
  2831. */
  2832. int RandVal(void* arg)
  2833. {
  2834. return (int)GetRandomNumber();
  2835. }
  2836. /**
  2837. * @brief Send callback for log file transfer (messages as is, not ordered)
  2838. */
  2839. void HTTP_SendHistory(void)
  2840. {
  2841. uint32_t nbytes = 0;
  2842. static bool start = true;
  2843. memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
  2844. if (log_ptr + FILE_BUF_MAX_LEN <= log_size) {
  2845. nbytes = History_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN, start);
  2846. }
  2847. else if (log_ptr < log_size) {
  2848. nbytes = History_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
  2849. }
  2850. else {
  2851. nbytes = 0;
  2852. }
  2853. log_ptr += nbytes;
  2854. start = false;
  2855. if (nbytes == 0) {
  2856. // File transfer finished.
  2857. start = true;
  2858. // Clear file transfer in progress flag
  2859. fLogTransInprog = false;
  2860. return;
  2861. }
  2862. ssl_sendframes(&ssl, logFileBuf, nbytes);
  2863. HTTP_SendHistory();
  2864. }
  2865. /**
  2866. * @brief Sent callback for log file transfer (messages as is, not ordered)
  2867. */
  2868. void HTTP_SendLog(void)
  2869. {
  2870. uint32_t nbytes = 0;
  2871. static bool start = true;
  2872. memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
  2873. if (log_ptr + FILE_BUF_MAX_LEN_LOG <= log_size) {
  2874. nbytes = LOG_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN_LOG, start);
  2875. }
  2876. else if (log_ptr < log_size) {
  2877. nbytes = LOG_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
  2878. }
  2879. else {
  2880. nbytes = 0;
  2881. }
  2882. log_ptr += nbytes;
  2883. start = false;
  2884. if (nbytes == 0) {
  2885. // File transfer finished.
  2886. start = true;
  2887. // Clear file transfer in progress flag
  2888. fLogTransInprog = false;
  2889. return;
  2890. }
  2891. ssl_sendframes(&ssl, logFileBuf, nbytes);
  2892. HTTP_SendLog();
  2893. return;
  2894. }
  2895. /**
  2896. * @brief Initialize the HTTPS server (start its thread)
  2897. */
  2898. void HTTPS_Init()
  2899. {
  2900. char buf[MAX_WEB_COOKIE_LEN];
  2901. uint8_t user_id;
  2902. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  2903. // Flush user cookie by random value
  2904. sprintf(buf, "%X", (unsigned int)GetRandomNumber());
  2905. HTTP_SetUserCookie(buf, user_id);
  2906. // Create user logout timers
  2907. users[user_id].LogoutTimer =
  2908. xTimerCreate("LogoutTmr", WEB_LOGOUT_TIME, pdFALSE, ( void * ) user_id, LogoutTimerCallback);
  2909. }
  2910. }