http_server.c 98 KB

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