http_server.c 98 KB

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