http_server.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345
  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. /* Устанавливаем дату следующей профилактики +1 год */
  1204. RTC_SetProfTime(value);
  1205. /* Пока отправляем true */
  1206. strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\n\r\nTrue");
  1207. *lenBufOut = strlen(bufOut);
  1208. // TEST_SetServerFlag();
  1209. }
  1210. /**
  1211. * @brief
  1212. * @retval None
  1213. */
  1214. void HTTP_SetSettings(char *buf, uint16_t lenBuf)
  1215. {
  1216. uint8_t valueLen = 0;
  1217. const uint8_t len = MAX_WEB_PARAM_LEN;
  1218. char value[MAX_WEB_PARAM_LEN];
  1219. char str[MAX_WEB_PARAM_LEN];
  1220. //printf(buf);
  1221. //ClearParamString(buf);
  1222. memset(value, 0, len);
  1223. memset(str, 0, MAX_WEB_PARAM_LEN);
  1224. /* SNMP */
  1225. GetParamValue(buf, "read_community=", value, &valueLen);
  1226. SetReadCommunity(value);
  1227. memset(value, 0, len);
  1228. GetParamValue(buf, "write_community=", value, &valueLen);
  1229. SetWriteCommunity(value);
  1230. memset(value, 0, len);
  1231. GetParamValue(buf, "managerIP=", value, &valueLen);
  1232. SetManagerIp(value);
  1233. memset(value, 0, len);
  1234. GetParamValue(buf, "managerIP2=", value, &valueLen);
  1235. SetManagerIp2(value);
  1236. memset(value, 0, len);
  1237. GetParamValue(buf, "managerIP3=", value, &valueLen);
  1238. SetManagerIp3(value);
  1239. memset(value, 0, len);
  1240. GetParamValue(buf, "managerIP4=", value, &valueLen);
  1241. SetManagerIp4(value);
  1242. memset(value, 0, len);
  1243. GetParamValue(buf, "managerIP5=", value, &valueLen);
  1244. SetManagerIp5(value);
  1245. memset(value, 0, len);
  1246. /* Сетевые параметры */
  1247. GetParamValue(buf, "dhcp=", value, &valueLen);
  1248. SetDhcpStateStr(value);
  1249. if (strncmp(value, "on", 2) != 0) // Если dhcp off устанавливаем параметры
  1250. {
  1251. memset(value, 0, len);
  1252. GetParamValue(buf, "ipaddr=", value, &valueLen);
  1253. SetIPStr(value);
  1254. memset(value, 0, len);
  1255. GetParamValue(buf, "gw=", value, &valueLen);
  1256. SetGatewayStr(value);
  1257. memset(value, 0, len);
  1258. GetParamValue(buf, "mask=", value, &valueLen);
  1259. SetMaskStr(value);
  1260. memset(value, 0, len);
  1261. }
  1262. memset(value, 0, len);
  1263. GetParamValue(buf, "swauth=", value, &valueLen);
  1264. SetAuthEnableStateStr(value);
  1265. if (strncmp(value, "on", 2) == 0){
  1266. /* параметры RADIUS*/
  1267. memset(value, 0, len);
  1268. GetParamValue(buf, "rs_enabled=", value, &valueLen);
  1269. SetRDSEnableStateStr(value);
  1270. if (strncmp(value, "on", 2) == 0) // Если raddius off устанавливаем параметры
  1271. {
  1272. memset(value, 0, len);
  1273. GetParamValue(buf, "rs_server=", value, &valueLen);
  1274. SetRDSIpStr(value);
  1275. memset(value, 0, len);
  1276. GetParamValue(buf, "rs_port=", value, &valueLen);
  1277. SetRDSPortStr(value);
  1278. memset(value, 0, len);
  1279. GetParamValue(buf, "rs_pwd=", value, &valueLen);
  1280. SetRDSPasswordkStr(value);
  1281. memset(value, 0, len);
  1282. GetParamValue(buf, "rs_key=", value, &valueLen);
  1283. SetRDSKeyAccesstStr(value);
  1284. memset(value, 0, len);
  1285. }
  1286. }
  1287. memset(value, 0, len);
  1288. // Параметры реле и сухих контактов
  1289. GetParamValue(buf, "di1=", value, &valueLen);
  1290. SetDINTypeActStr(value, 0);
  1291. memset(value, 0, len);
  1292. GetParamValue(buf, "ro1=", value, &valueLen);
  1293. SetROTypeActStr(value, 0);
  1294. memset(value, 0, len);
  1295. GetParamValue(buf, "ro2=", value, &valueLen);
  1296. SetROTypeActStr(value, 1);
  1297. memset(value, 0, len);
  1298. // Параметры даты и времени
  1299. GetParamValue(buf, "ntp=", value, &valueLen);
  1300. SetSntpStateStr(value);
  1301. if (strncmp(value, "1", 1) == 0) // Если ntp on устанавливаем параметры
  1302. {
  1303. memset(value, 0, len);
  1304. GetParamValue(buf, "ntpservip=", value, &valueLen);
  1305. SetSntpServerIpStr(value);
  1306. memset(value, 0, len);
  1307. }
  1308. else if (strncmp(value, "0", 1) == 0){
  1309. GetParamValue(buf, "date=", value, &valueLen);
  1310. SetDateStr(value);
  1311. memset(value, 0, len);
  1312. GetParamValue(buf, "time=", value, &valueLen);
  1313. url_decode(str, sizeof(str), value);
  1314. SetTimeStr(str);
  1315. memset(value, 0, len);
  1316. }
  1317. GetParamValue(buf, "utc=", value, &valueLen);
  1318. SetSntpTimeZoneStr(value);
  1319. memset(value, 0, len);
  1320. /* Если параметры WEB изменились выставляем флаг, сохраняем настройки и перезагружаемся */
  1321. if (GetStateWebReinit() == true)
  1322. {
  1323. SetWebReinitFlag(true);
  1324. HTTP_SaveSettings();
  1325. /* Блокируем управление ключем на тау секунд*/
  1326. //IO_KeyBlockOn();
  1327. vTaskDelay(1010);
  1328. Reboot();
  1329. }
  1330. HTTP_SaveSettings();
  1331. }
  1332. /**
  1333. * @brief
  1334. * @retval None
  1335. */
  1336. void HTTP_SetInfo(char *buf, uint16_t lenBuf)
  1337. {
  1338. uint8_t valueLen = 0;
  1339. const uint8_t len = 110;
  1340. char value[110];
  1341. char str[110];
  1342. // ClearParamString(buf);
  1343. memset(value, 0, len);
  1344. /* Владелец */
  1345. GetParamValue(buf, "owner=", value, &valueLen);
  1346. url_decode(str, sizeof(str), value);
  1347. SetOwner(str);
  1348. memset(value, 0, len);
  1349. /* Владелец */
  1350. GetParamValue(buf, "sysLocation=", value, &valueLen);
  1351. url_decode(str, sizeof(str), value);
  1352. SetLocation(str);
  1353. memset(value, 0, len);
  1354. /* Комментарий */
  1355. GetParamValue(buf, "comment=", value, &valueLen);
  1356. url_decode(str, sizeof(str), value);
  1357. SetComment(str);
  1358. memset(value, 0, len);
  1359. HTTP_SaveSettings();
  1360. }
  1361. /**
  1362. * @brief Запуск/останов теста UPS
  1363. */
  1364. void HTTP_UPSTest(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1365. {
  1366. uint8_t valueLen = 0;
  1367. char tempValue[20];
  1368. char tempValue2[20];
  1369. int8_t res = 0;
  1370. memset(tempValue, 0, 20);
  1371. memset(tempValue2, 0, 20);
  1372. strcpy(bufOut, HTTP_200_OK);
  1373. GetParamValue(bufIn, "func=", tempValue, &valueLen);
  1374. if (strcmp(tempValue, "stop") == 0){
  1375. res = ups_metac_service_pdu(ups_cancel_test);
  1376. if(res == 1 || res == 0){
  1377. strcat(bufOut, "Тест остановлен!");
  1378. log_event_data(LOG_TEST_UPS, "Администратор(Останов)");
  1379. }
  1380. if(res == -1)
  1381. strcat(bufOut, "Тест не удалось остановить!");
  1382. *lenBufOut = strlen(bufOut);
  1383. }
  1384. else if (strcmp(tempValue, "discharge") == 0){
  1385. res = ups_metac_service_pdu(ups_test_low_bat);
  1386. if(res == 1 || res == 0){
  1387. strcat(bufOut, "Тест запущен!");
  1388. log_event_data(LOG_TEST_UPS, "Администратор (Запущен)");
  1389. }
  1390. if(res == -1)
  1391. strcat(bufOut, "Тест не удалось запустить!");
  1392. *lenBufOut = strlen(bufOut);
  1393. }
  1394. else if (strncmp(tempValue, "time", 6) == 0){
  1395. GetParamValue(bufIn, "=", tempValue2, &valueLen);
  1396. TimeParam = atoi(tempValue2);
  1397. res = ups_metac_service_pdu(ups_test_time);
  1398. if(res == 1 || res == 0){
  1399. strcat(bufOut, "Тест запущен!");
  1400. log_event_data(LOG_TEST_UPS, "Администратор (Запущен)");
  1401. }
  1402. if(res == -1)
  1403. strcat(bufOut, "Тест не удалось запустить!");
  1404. *lenBufOut = strlen(bufOut);
  1405. }
  1406. }
  1407. /**
  1408. * @brief Выклюение UPS
  1409. */
  1410. void HTTP_UPSshutdown(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1411. {
  1412. uint8_t valueLen = 0;
  1413. char *valueLenEnd = 0;
  1414. char tempValue[50];
  1415. char tempValue2[50];
  1416. int8_t res = 0;
  1417. memset(tempValue, 0, 50);
  1418. strcpy(bufOut, HTTP_200_OK);
  1419. GetParamValue(bufIn, "func=", tempValue, &valueLen);
  1420. if (strcmp(tempValue, "reboot") == 0){
  1421. res = ups_metac_service_pdu(ups_cancel_shut_down);
  1422. if(res == 1){
  1423. log_event_data(LOG_SHUTDOWN_UPS, "Администратор(Останов)");
  1424. strcat(bufOut, "Выключение нагрузки ИБП отменено!");
  1425. }
  1426. else
  1427. strcat(bufOut, "Выключение нагрузки ИБП не удалось отменить!");
  1428. *lenBufOut = strlen(bufOut);
  1429. }
  1430. else if (strncmp(tempValue, "off", 5) == 0){
  1431. memset(tempValue2, 0, 50);
  1432. GetParamValue(bufIn, "after=", tempValue2, &valueLen);
  1433. TimeParamFloat = atof(tempValue2);
  1434. res = ups_metac_service_pdu(ups_shutdown);
  1435. if(res == 1){
  1436. strcat(bufOut, "Отключение нагрузки ИБП!");
  1437. log_event_data(LOG_SHUTDOWN_UPS, "Администратор");
  1438. }else
  1439. strcat(bufOut, "Отключение нагрузки ИБП не удалось!");
  1440. *lenBufOut = strlen(bufOut);
  1441. }
  1442. }
  1443. /**
  1444. * @brief Проверка пароля для перехода в режим bootloader
  1445. * @retval None
  1446. */
  1447. void HTTP_ConfirmBootPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1448. {
  1449. char tempStr[50];
  1450. strncpy(tempStr, bufIn, 50);
  1451. char value[20];
  1452. uint8_t valueLen;
  1453. memset(value, 0, 20);
  1454. //if (GetParamValue(tempStr, "password=", value, &valueLen))
  1455. {
  1456. //if (strcmp(BOOTLOADER_PASWORD, value) == 0)
  1457. {
  1458. // *bufOut = '1';
  1459. /* Запускаем задачу отложенной перезагрузки. Контроллер должен успеть
  1460. отправить ответ серверу о статусе пароля */
  1461. HTTP_StartResetTask(true);
  1462. }
  1463. /* else
  1464. *bufOut = '0';*/
  1465. //*lenBufOut = 1;
  1466. }
  1467. }
  1468. /**
  1469. * @brief Проверка пароля для входа в Web
  1470. * @retval None
  1471. */
  1472. int HTTP_ConfirmWebPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1473. {
  1474. char tempStr[50];
  1475. char login[20];
  1476. char password[20];
  1477. uint8_t valueLen, user_id;
  1478. char *strPtr = 0;
  1479. char WebPassword[MAX_WEB_PASSWD_LEN];
  1480. char WebLogin[MAX_WEB_LOGIN_LEN];
  1481. char buf[40];
  1482. memset(login, 0, 20);
  1483. memset(password, 0, 20);
  1484. memset(tempStr, 0, 50);
  1485. /* Get first 50 bytes of string */
  1486. strncpy(tempStr, bufIn, 49);
  1487. /* Add " " to the string in order GetParamValue() can be able to parse the param */
  1488. strcat(tempStr, " ");
  1489. if (GetParamValue(tempStr, "login=", login, &valueLen) &&
  1490. GetParamValue(tempStr, "password=", password, &valueLen))
  1491. {
  1492. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  1493. GetUserLogin(user_id, WebLogin, &valueLen);
  1494. GetUserPassword(user_id, WebPassword, &valueLen);
  1495. /* Check login and password */
  1496. if ((strncmp(WebLogin, login, MAX_WEB_LOGIN_LEN) == 0) &&
  1497. (strncmp(WebPassword, password, MAX_WEB_PASSWD_LEN) == 0)) {
  1498. /* Login and pass are valid */
  1499. /* Check user's login session */
  1500. /* If "user" has logged in */
  1501. if (user_id >= 1) {
  1502. }
  1503. /* TODO replace global flag with user-pass-cookie */
  1504. Authenticated = true;
  1505. /* Generate cookie */
  1506. sprintf(tempStr, "%X", (unsigned int)GetRandomNumber());
  1507. /* Set users cookie */
  1508. HTTP_SetUserCookie(tempStr, user_id);
  1509. HTTP_UpdateUserLoginTime(user_id);
  1510. /* Send login and cookie back */
  1511. strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\nSet-Cookie: uname=");
  1512. strcat(bufOut, WebLogin);
  1513. strcat(bufOut, "\r\nSet-Cookie: id=");
  1514. strcat(bufOut, tempStr);
  1515. sprintf(tempStr, "%d", user_id);
  1516. strcat(bufOut, "\r\nSet-Cookie: role=");
  1517. strcat(bufOut, tempStr);
  1518. strcat(bufOut, "\r\n\r\n");
  1519. strcat(bufOut,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/index.html\"/></head></html>\r\n\r\n");
  1520. *lenBufOut = strlen(bufOut);
  1521. switch (user_id) {
  1522. case 0:
  1523. snprintf(buf, sizeof(buf), "Администратор");
  1524. break;
  1525. case 1:
  1526. snprintf(buf, sizeof(buf), "Пользователь");
  1527. break;
  1528. default:
  1529. snprintf(buf, sizeof(buf), "", login);
  1530. break;
  1531. }
  1532. log_event_data(LOG_LOGIN, buf);
  1533. /* Запускаем задачу-таймер логаута. */
  1534. /* TODO отправить ответ серверу о статусе пароля */
  1535. return SEND_REQUIRED_YES;
  1536. }
  1537. /*
  1538. else {
  1539. continue;
  1540. }
  1541. */
  1542. }
  1543. }
  1544. /* No valid login and pass found */
  1545. /* TODO replace global flag with user-pass-cookie*/
  1546. Authenticated = false;
  1547. /* Wrong login or pass, return */
  1548. return SEND_REQUIRED_NO;
  1549. }
  1550. void HTTP_LOGIN(char *bufOut, uint16_t *lenBufOut)
  1551. {
  1552. char tempStr[50];
  1553. uint8_t valueLen;
  1554. char WebLogin[MAX_WEB_LOGIN_LEN];
  1555. GetUserLogin(ADMIN, WebLogin, &valueLen);
  1556. memset(tempStr, 0, 50);
  1557. /* TODO replace global flag with user-pass-cookie */
  1558. Authenticated = true;
  1559. /* Generate cookie */
  1560. sprintf(tempStr, "%X", (unsigned int)GetRandomNumber());
  1561. /* Set users cookie */
  1562. HTTP_SetUserCookie(tempStr, ADMIN);
  1563. HTTP_UpdateUserLoginTime(ADMIN);
  1564. /* Send login and cookie back */
  1565. strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\nSet-Cookie: uname=");
  1566. strcat(bufOut, WebLogin);
  1567. strcat(bufOut, "\r\nSet-Cookie: id=");
  1568. strcat(bufOut, tempStr);
  1569. strcat(bufOut, "\r\nSet-Cookie: role=0");
  1570. strcat(bufOut, "\r\nSet-Cookie: auth=0");
  1571. strcat(bufOut, "\r\n\r\n");
  1572. strcat(bufOut,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/index.html\"/></head></html>\r\n\r\n");
  1573. *lenBufOut = strlen(bufOut);
  1574. }
  1575. /**
  1576. * @brief
  1577. * @retval None
  1578. */
  1579. uint8_t GetParamValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
  1580. {
  1581. char *beginValue = 0;
  1582. char *endValue = 0;
  1583. int len = 0;
  1584. char *strPtr = 0;
  1585. strPtr = strstr(inStr, paramName);
  1586. if (strPtr != 0)
  1587. {
  1588. beginValue = strpbrk(strPtr,"=");
  1589. endValue = strpbrk(strPtr,"&");
  1590. if (endValue == 0)
  1591. endValue = strpbrk(strPtr," ");
  1592. len = endValue - beginValue - 1;
  1593. strncpy(paramValue, beginValue + 1, len);
  1594. *endValue = '0';
  1595. *beginValue = '0';
  1596. *paramLen = len;
  1597. return 1;
  1598. }
  1599. else
  1600. {
  1601. *paramLen = 0;
  1602. return 0;
  1603. }
  1604. }
  1605. /**
  1606. * @brief
  1607. * @retval None
  1608. */
  1609. uint8_t GetCookieValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
  1610. {
  1611. char *beginValue = 0;
  1612. char *endValue = 0;
  1613. int len = 0;
  1614. char *strPtr = 0;
  1615. strPtr = strstr(inStr, paramName);
  1616. if (strPtr != 0)
  1617. {
  1618. beginValue = strpbrk(strPtr,"=");
  1619. endValue = strpbrk(strPtr,";");
  1620. if (endValue == 0)
  1621. endValue = strpbrk(strPtr,"\n");
  1622. len = endValue - beginValue - 1;
  1623. strncpy(paramValue, beginValue + 1, len);
  1624. *endValue = '0';
  1625. *beginValue = '0';
  1626. *paramLen = len;
  1627. return 1;
  1628. }
  1629. else
  1630. {
  1631. *paramLen = 0;
  1632. return 0;
  1633. }
  1634. }
  1635. /**
  1636. * @brief
  1637. * @retval None
  1638. */
  1639. /*
  1640. uint8_t GetParamValueInEnd(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
  1641. {
  1642. char *beginValue = 0;
  1643. char *endValue = 0;
  1644. int len = 0;
  1645. char *strPtr = 0;
  1646. strPtr = strstr(inStr, paramName);
  1647. if (strPtr != 0)
  1648. {
  1649. beginValue = strpbrk(strPtr,"=");
  1650. endValue = strpbrk(strPtr," ");
  1651. len = endValue - beginValue - 1;
  1652. strncpy(paramValue, beginValue + 1, len);
  1653. *endValue = '0';
  1654. *beginValue = '0';
  1655. *paramLen = len;
  1656. return 1;
  1657. }
  1658. else
  1659. {
  1660. *paramLen = 0;
  1661. return 0;
  1662. }
  1663. }
  1664. */
  1665. void ClearParamString(char *inBuf)
  1666. {
  1667. uint16_t len;
  1668. char *str;
  1669. str = strstr(inBuf, "HTTP");
  1670. if (str != 0)
  1671. {
  1672. len = str - inBuf;
  1673. memset(str, 0, RECIVE_BUF_MAX_LEN - len - 1);
  1674. }
  1675. }
  1676. /**
  1677. * @brief Чтение Cookie пользователя
  1678. */
  1679. static void HTTP_GetUserCookie(uint8_t user_id, char *str, uint8_t *len)
  1680. {
  1681. sprintf(str, "%s", users[user_id].cookie);
  1682. *len = strlen(str);
  1683. }
  1684. /**
  1685. * @brief Установка Cookie пользователя
  1686. */
  1687. static void HTTP_SetUserCookie(char *str, uint8_t user_id)
  1688. {
  1689. strcpy(users[user_id].cookie, str);
  1690. }
  1691. /**
  1692. * @brief Обновление времени последней активности пользователя
  1693. */
  1694. static void HTTP_UpdateUserLoginTime(uint8_t user_id)
  1695. {
  1696. xTimerStart(users[user_id].LogoutTimer, 0);
  1697. }
  1698. /**
  1699. * @brief Extract the Content_Length data from HTML data
  1700. * @param data : pointer on receive packet buffer
  1701. * @param len : buffer length
  1702. * @retval size : Content_length in numeric format
  1703. */
  1704. static uint32_t Parse_Content_Length(char *data, uint32_t len)
  1705. {
  1706. uint32_t i=0,size=0, S=1;
  1707. int32_t j=0;
  1708. char sizestring[6], *ptr;
  1709. ContentLengthOffset =0;
  1710. /* find Content-Length data in packet buffer */
  1711. for (i=0;i<len;i++)
  1712. {
  1713. if (strncmp ((char*)(data+i), Content_Length, 16)==0)
  1714. {
  1715. ContentLengthOffset = i+16;
  1716. break;
  1717. }
  1718. }
  1719. /* read Content-Length value */
  1720. if (ContentLengthOffset)
  1721. {
  1722. i=0;
  1723. ptr = (char*)(data + ContentLengthOffset);
  1724. while(*(ptr+i)!=0x0d)
  1725. {
  1726. sizestring[i] = *(ptr+i);
  1727. i++;
  1728. ContentLengthOffset++;
  1729. }
  1730. if (i>0)
  1731. {
  1732. /* transform string data into numeric format */
  1733. for(j=i-1;j>=0;j--)
  1734. {
  1735. size += (sizestring[j]-0x30)*S;
  1736. S=S*10;
  1737. }
  1738. }
  1739. }
  1740. return size;
  1741. }
  1742. /**
  1743. * @brief Принудительный логаут пользователя
  1744. */
  1745. static void HTTP_ForceUserLogout(uint8_t user_id)
  1746. {
  1747. char cookie[MAX_WEB_COOKIE_LEN];
  1748. /* Flush user cookie by random value */
  1749. sprintf(cookie, "%X", (unsigned int)GetRandomNumber());
  1750. HTTP_SetUserCookie(cookie, user_id);
  1751. }
  1752. /**
  1753. * @brief >Callback таймера логаута пользователя
  1754. */
  1755. void LogoutTimerCallback(TimerHandle_t pxTimer) {
  1756. uint8_t user_id = (uint8_t)pvTimerGetTimerID( pxTimer );
  1757. if( sSettings.sRADIUS.Auth_enable )
  1758. HTTP_ForceUserLogout(user_id);
  1759. }
  1760. /**
  1761. * @brief Смена пароля пользователя
  1762. * @retval None
  1763. */
  1764. int HTTP_ChangeUserPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
  1765. {
  1766. char tempStr[110];
  1767. char value[20];
  1768. char login[20];
  1769. char password[20];
  1770. uint8_t valueLen, valueLen2, user_id;
  1771. char WebLogin[MAX_WEB_LOGIN_LEN];
  1772. (void)lenBufIn;
  1773. memset(login, 0, 20);
  1774. memset(password, 0, 20);
  1775. memset(tempStr, 0, 50);
  1776. memset(value, 0, 20);
  1777. ClearParamString(bufIn);
  1778. strncpy(tempStr, bufIn, 110);
  1779. strcpy(bufOut, HTTP_200_OK);
  1780. if (GetParamValue(tempStr, "username=", login, &valueLen) &&
  1781. GetParamValue(tempStr, "oldpass=", password, &valueLen))
  1782. {
  1783. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++)
  1784. {
  1785. memset(value, 0, 20);
  1786. memset(WebLogin, 0, MAX_WEB_LOGIN_LEN);
  1787. GetUserLogin(user_id, WebLogin, &valueLen);
  1788. GetUserPassword(user_id, value, &valueLen2);
  1789. /* Check login and password */
  1790. if ((strncmp(WebLogin, login, MAX_WEB_LOGIN_LEN) == 0) &&
  1791. (memcmp(password, value, 11) == 0))
  1792. {
  1793. memset(password, 0, 20);
  1794. if (GetParamValue(tempStr, "newpass=", password, &valueLen))
  1795. {
  1796. memcpy(sSettings.sAuth[user_id].password, password, 11);
  1797. HTTP_SaveSettings();
  1798. log_event_data(LOG_PSW_CHANGE, "Администратор");
  1799. strcat(bufOut, "Пароль успешно изменён");
  1800. *lenBufOut = strlen(bufOut);
  1801. return SEND_REQUIRED_YES;
  1802. }
  1803. else {
  1804. strcat(bufOut, "Введены некорректные данные!");
  1805. *lenBufOut = strlen(bufOut);
  1806. return SEND_REQUIRED_YES;
  1807. }
  1808. }
  1809. }
  1810. strcat(bufOut, "Введён неверный пароль!");
  1811. *lenBufOut = strlen(bufOut);
  1812. return SEND_REQUIRED_YES;
  1813. }
  1814. else {
  1815. strcat(bufOut, "Введены некорректные данные!");
  1816. *lenBufOut = strlen(bufOut);
  1817. return SEND_REQUIRED_YES;
  1818. }
  1819. }
  1820. // -----------------------------------------------------------------------------
  1821. //
  1822. //
  1823. // SSL тесты
  1824. //
  1825. //
  1826. // -----------------------------------------------------------------------------
  1827. //#include "stm32f4xx.h"
  1828. #include "rtc.h"
  1829. /* PolarSSL includes */
  1830. #include "polarssl/net.h"
  1831. #include "polarssl/ssl.h"
  1832. #include "polarssl/havege.h"
  1833. #include "polarssl/certs.h"
  1834. #include "polarssl/x509.h"
  1835. /* Private typedef -----------------------------------------------------------*/
  1836. /* Private define ------------------------------------------------------------*/
  1837. #define DEBUG_LEVEL 1 /* Set DEBUG_LEVEL if you want to enable SSL debug
  1838. option, this should be set to 2, 3, 4 or 5 */
  1839. #define HTTP_RESPONSE "<p><p>Successful connection using: %s\r\n"
  1840. /* Format of dynamic web page */
  1841. #define PAGE_START \
  1842. "<html>\
  1843. <head>\
  1844. </head>\
  1845. <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\" bgcolor=\"#FFFFFF\" text=\"#000000\">\
  1846. <font size=\"4\" color=\"#0000FF\"><b>STM32F407xx : SSL Server Demo not using HW Crypto :<)</font></b></i>\
  1847. <br><br><pre>\r\nPage Hits = "
  1848. #define PAGE_END \
  1849. " \r\n</pre><br><br><hr>\
  1850. <font size=\"3\" color=\"#808080\">All rights reserved ©2017 ROTEK\
  1851. \r\n</font></BODY>\
  1852. </html>"
  1853. /* Private macro -------------------------------------------------------------*/
  1854. /* Private variables ---------------------------------------------------------*/
  1855. /* SSL structures */
  1856. rng_state rngs;
  1857. ssl_context ssl;
  1858. ssl_session ssn;
  1859. x509_cert srvcert;
  1860. rsa_context rsa;
  1861. uint32_t nPageHits = 0;
  1862. /* Private functions ---------------------------------------------------------*/
  1863. /*
  1864. * Computing a "safe" DH-1024 prime can take a very
  1865. * long time, so a precomputed value is provided below.
  1866. * You may run dh_genprime to generate a new value.
  1867. */
  1868. char *my_dhm_P =
  1869. "E4004C1F94182000103D883A448B3F80" \
  1870. "2CE4B44A83301270002C20D0321CFD00" \
  1871. "11CCEF784C26A400F43DFB901BCA7538" \
  1872. "F2C6B176001CF5A0FD16D2C48B1D0C1C" \
  1873. "F6AC8E1DA6BCC3B4E1F96B0564965300" \
  1874. "FFA1D0B601EB2800F489AA512C4B248C" \
  1875. "01F76949A60BB7F00A40B1EAB64BDD48" \
  1876. "E8A700D60B7F1200FA8E77B0A979DABF";
  1877. char *my_dhm_G = "4";
  1878. /*
  1879. * Sorted by order of preference
  1880. */
  1881. int my_ciphersuites[] =
  1882. {
  1883. SSL_EDH_RSA_AES_256_SHA,
  1884. SSL_EDH_RSA_CAMELLIA_256_SHA,
  1885. SSL_EDH_RSA_AES_128_SHA,
  1886. SSL_EDH_RSA_CAMELLIA_128_SHA,
  1887. SSL_EDH_RSA_DES_168_SHA,
  1888. SSL_RSA_AES_256_SHA,
  1889. SSL_RSA_CAMELLIA_256_SHA,
  1890. SSL_RSA_AES_128_SHA,
  1891. SSL_RSA_CAMELLIA_128_SHA,
  1892. SSL_RSA_DES_168_SHA,
  1893. SSL_RSA_RC4_128_SHA,
  1894. SSL_RSA_RC4_128_MD5,
  1895. 0
  1896. };
  1897. void my_debug(void *ctx, int level, const char *str)
  1898. {
  1899. if(level < DEBUG_LEVEL)
  1900. {
  1901. printf("\r%s", str);
  1902. }
  1903. }
  1904. /*
  1905. * These session callbacks use a simple chained list
  1906. * to store and retrieve the session information.
  1907. */
  1908. ssl_session *s_list_1st = NULL;
  1909. ssl_session *cur, *prv;
  1910. static int my_get_session(ssl_context *ssl)
  1911. {
  1912. time_t t = RTC_GetUnixTime(); //time(NULL);
  1913. if(ssl->resume == 0)
  1914. return(1);
  1915. cur = s_list_1st;
  1916. prv = NULL;
  1917. while(cur != NULL)
  1918. {
  1919. prv = cur;
  1920. cur = cur->next;
  1921. if(ssl->timeout != 0 && t - prv->start > ssl->timeout)
  1922. continue;
  1923. if( ssl->session->ciphersuite != prv->ciphersuite ||
  1924. ssl->session->length != prv->length)
  1925. continue;
  1926. if(memcmp( ssl->session->id, prv->id, prv->length ) != 0)
  1927. continue;
  1928. memcpy(ssl->session->master, prv->master, 48);
  1929. return(0);
  1930. }
  1931. return(1);
  1932. }
  1933. static int my_set_session(ssl_context *ssl)
  1934. {
  1935. time_t t = RTC_GetUnixTime(); //time(NULL);
  1936. cur = s_list_1st;
  1937. prv = NULL;
  1938. while(cur != NULL)
  1939. {
  1940. if(ssl->timeout != 0 && t - cur->start > ssl->timeout)
  1941. break; /* expired, reuse this slot */
  1942. if(memcmp( ssl->session->id, cur->id, cur->length ) == 0)
  1943. break; /* client reconnected */
  1944. prv = cur;
  1945. cur = cur->next;
  1946. }
  1947. if(cur == NULL)
  1948. {
  1949. cur = (ssl_session *) malloc(sizeof(ssl_session));
  1950. if(cur == NULL)
  1951. return(1);
  1952. if(prv == NULL)
  1953. s_list_1st = cur;
  1954. else prv->next = cur;
  1955. }
  1956. memcpy(cur, ssl->session, sizeof(ssl_session));
  1957. return(0);
  1958. }
  1959. /**
  1960. * @brief SSL Server task.
  1961. * @param pvParameters not used
  1962. * @retval None
  1963. */
  1964. void ssl_server(void *pvParameters)
  1965. {
  1966. int ret, len;
  1967. int listen_fd;
  1968. int client_fd;
  1969. char buf[1024];
  1970. char *ptr;
  1971. char CookieBuf[50];
  1972. char *CookiePtr = NULL;
  1973. char name[MAX_WEB_COOKIE_LEN];
  1974. char id[MAX_WEB_COOKIE_LEN];
  1975. uint8_t nameLen = 0, idLen = 0;
  1976. uint32_t DataOffset;
  1977. struct fs_file file = {0, 0};
  1978. uint32_t i;
  1979. memset(&srvcert, 0, sizeof(x509_cert));
  1980. // 1. Load the certificates and private RSA key
  1981. //printf("\n\r Loading the server certificates and key...");
  1982. // This demonstration program uses embedded test certificates.
  1983. // Instead, you may want to use x509parse_crtfile() to read the
  1984. // server and CA certificates, as well as x509parse_keyfile().
  1985. ret = x509parse_crt(&srvcert, (unsigned char *) test_srv_crt, strlen(test_srv_crt));
  1986. if(ret != 0)
  1987. {
  1988. //printf(" failed\n ! x509parse_crt returned %d\n\r", ret);
  1989. goto exit;
  1990. }
  1991. ret = x509parse_crt(&srvcert, (unsigned char *) test_ca_crt, strlen(test_ca_crt));
  1992. if(ret != 0)
  1993. {
  1994. //printf(" failed\n ! x509parse_crt returned %d\n\r", ret);
  1995. goto exit;
  1996. }
  1997. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  1998. ret = x509parse_key(&rsa, (unsigned char *) test_srv_key, strlen(test_srv_key), NULL, 0);
  1999. if( ret != 0 )
  2000. {
  2001. //printf(" failed\n ! x509parse_key returned %d\n\r", ret);
  2002. goto exit;
  2003. }
  2004. // 2. Setup the listening TCP socket
  2005. //printf("\n\r Bind to https port ...");
  2006. // Bind the connection to https port : 443
  2007. ret = net_bind(&listen_fd, NULL, 443);
  2008. if(ret != 0)
  2009. {
  2010. //printf(" failed\n ! net_bind returned %d\n\r", ret);
  2011. goto exit;
  2012. }
  2013. //printf(" ok\n\r");
  2014. // 3. Wait until a client connects
  2015. for(;;)
  2016. {
  2017. //printf("\n\r Waiting for a remote connection ...");
  2018. ret = net_accept(listen_fd, &client_fd, NULL);
  2019. if(ret != 0)
  2020. {
  2021. //printf(" failed\n ! net_accept returned %d\n\n", ret);
  2022. goto exit;
  2023. }
  2024. //printf(" ok\n");
  2025. // 4. Initialize the session data
  2026. //printf("\n\r Setting up the RNG and SSL data....");
  2027. // Initialize the SSL context
  2028. ret = ssl_init(&ssl);
  2029. if(ret != 0)
  2030. {
  2031. //printf(" failed\n ! ssl_init returned %d\n\n", ret);
  2032. goto accept;
  2033. }
  2034. //printf(" ok\n");
  2035. // Set the current session as SSL server
  2036. ssl_set_endpoint(&ssl, SSL_IS_SERVER);
  2037. // No certificate verification
  2038. ssl_set_authmode(&ssl, SSL_VERIFY_NONE);
  2039. // Set the random number generator callback function
  2040. ssl_set_rng(&ssl, RandVal, &rngs);
  2041. // Set the debug callback function
  2042. //ssl_set_dbg(&ssl, my_debug, stdout);
  2043. // Set read and write callback functions
  2044. ssl_set_bio(&ssl, net_recv, &client_fd, net_send, &client_fd);
  2045. // Set the session callback functions
  2046. ssl_set_scb(&ssl, my_get_session, my_set_session);
  2047. // The list of ciphersuites to be used in this session
  2048. ssl_set_ciphersuites(&ssl, my_ciphersuites);
  2049. // Set the session resuming flag, timeout and session context
  2050. ssl_set_session(&ssl, 1, 0, &ssn);
  2051. memset(&ssn, 0, sizeof(ssl_session));
  2052. // Set the data required to verify peer certificate
  2053. ssl_set_ca_chain(&ssl, srvcert.next, NULL, NULL);
  2054. // Set own certificate and private key
  2055. ssl_set_own_cert(&ssl, &srvcert, &rsa);
  2056. // Set the Diffie-Hellman public P and G values
  2057. ssl_set_dh_param(&ssl, my_dhm_P, my_dhm_G);
  2058. // 5. Handshake protocol
  2059. //printf("\n\r Performing the SSL/TLS handshake...");
  2060. // Perform the SSL handshake protocol
  2061. while((ret = ssl_handshake(&ssl)) != 0)
  2062. {
  2063. if(ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE)
  2064. {
  2065. //printf(" failed\n ! ssl_handshake returned %d\n\n", ret);
  2066. goto accept;
  2067. }
  2068. }
  2069. //printf(" ok\n");
  2070. // 6. Read the HTTP Request
  2071. //printf("\n\r <= Read from client :");
  2072. do
  2073. {
  2074. receivedBufLen = RECIVE_BUF_MAX_LEN - 1;
  2075. memset(receiveBuf, 0, RECIVE_BUF_MAX_LEN);
  2076. // Read decrypted application data
  2077. ret = ssl_read(&ssl, (unsigned char*)receiveBuf, receivedBufLen);
  2078. if(ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE)
  2079. continue;
  2080. if(ret <= 0)
  2081. {
  2082. switch(ret)
  2083. {
  2084. case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
  2085. printf("\n\r connection was closed \n");
  2086. break;
  2087. case POLARSSL_ERR_NET_CONN_RESET:
  2088. printf("\n\r connection was reset by peer\n");
  2089. break;
  2090. default:
  2091. //printf("\n\r ssl_read returned %d\n", ret);
  2092. break;
  2093. }
  2094. break;
  2095. }
  2096. receivedBufLen = ret;
  2097. // Display the length of read data
  2098. //printf("\n\r Successfully read %d bytes from client \n\r",len);
  2099. }while(0);
  2100. // -------------------------------------------------------------------------
  2101. //receivedBufLen = p->tot_len;
  2102. //memcpy(receiveBuf, p->payload , receivedBufLen);
  2103. receiveBuf[receivedBufLen] = '\0';
  2104. // printf("receive %s \r\n", receiveBuf);
  2105. // Get cookie "uname" value
  2106. CookiePtr = strstr(receiveBuf, "uname=");
  2107. strncpy(CookieBuf, CookiePtr, 50);
  2108. //printf("********CookieBuf1= %s\r\n", CookieBuf);
  2109. memset(name, 0, MAX_WEB_COOKIE_LEN);
  2110. GetCookieValue(CookieBuf, "uname=", name, &nameLen);
  2111. //printf("********CookieBuf2= %s\r\n", CookieBuf);
  2112. //printf("********uname= %s\r\n", name);
  2113. // Get cookie "id" value
  2114. CookiePtr = strstr(receiveBuf, "id=");
  2115. strncpy(CookieBuf, CookiePtr, 50);
  2116. //printf("********CookieBuf1= %s\r\n", CookieBuf);
  2117. memset(id, 0, MAX_WEB_COOKIE_LEN);
  2118. GetCookieValue(CookieBuf, "id=", id, &idLen);
  2119. //printf("********CookieBuf2= %s\r\n", CookieBuf);
  2120. //printf("********id= %s\r\n", id);
  2121. // Id of currently logged-in user
  2122. uint8_t user_id;
  2123. if( DataFlag == 0 && DataFlag2 == 0 ) {
  2124. // Level of currently logged-in user
  2125. seclevel = 0xFF;
  2126. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  2127. HTTP_GetUserCookie(user_id, CookieBuf, &idLen);
  2128. if (strncmp(id, CookieBuf, idLen) == 0 ) {
  2129. GetUserLevelInt(user_id, &seclevel);
  2130. Authenticated = true;
  2131. break;
  2132. }
  2133. Authenticated = false;
  2134. seclevel = 0xFF;
  2135. }
  2136. }
  2137. if (DataFlag >= 1)
  2138. Authenticated = true;
  2139. else if(DataFlag2 >= 1)
  2140. Authenticated = true;
  2141. if ( Authenticated == false && sSettings.sRADIUS.Auth_enable == false)
  2142. {
  2143. HTTP_LOGIN(sendBuf, &sendBufLoadLen);
  2144. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2145. }
  2146. else if ( Authenticated == false )//&& sSettings.sRADIUS.Auth_enable == true
  2147. {
  2148. if (strncmp(receiveBuf, "GET /main.css", 13) == 0) // +
  2149. {
  2150. fs_open("/main.css", &file);
  2151. ssl_sendframes(&ssl, file.data, file.len);
  2152. }
  2153. else if (strncmp(receiveBuf, "GET /rotek.png", 14) == 0) // +
  2154. {
  2155. fs_open("/rotek.png", &file);
  2156. ssl_sendframes(&ssl, file.data, file.len);
  2157. }
  2158. else if (strncmp(receiveBuf, "GET /favicon.ico", 16) == 0) // ?
  2159. {
  2160. fs_open("/favicon.ico", &file);
  2161. ssl_sendframes(&ssl, file.data, file.len);
  2162. }
  2163. else if (strncmp(receiveBuf, "GET /role.js", 12) == 0)
  2164. {
  2165. fs_open("/role.js", &file);
  2166. ssl_sendframes(&ssl, file.data, file.len);
  2167. }
  2168. else if ((strncmp(receiveBuf, "POST /login.cgi", 15) == 0) || (log_post_reqn > 0))
  2169. {
  2170. uint32_t i, offset = 0, req_data_received = 0;
  2171. post_data_count = Parse_Content_Length(receiveBuf, receivedBufLen);
  2172. if (post_data_count < MAX_POST_REQ_LEN)
  2173. {
  2174. memset(post_req_data, 0, MAX_POST_REQ_LEN);
  2175. for (i = 0; i < receivedBufLen; i++)
  2176. {
  2177. if (strncmp ((char*)(receiveBuf+i), "\r\n\r\n", 4) == 0)
  2178. {
  2179. offset = i+4;
  2180. break;
  2181. }
  2182. }
  2183. req_data_received = receivedBufLen - offset;
  2184. if (offset != 0)
  2185. {
  2186. if (req_data_received < post_data_count)
  2187. {
  2188. snprintf(post_req_data, req_data_received, "%s", receiveBuf);
  2189. post_data_count -= req_data_received;
  2190. }
  2191. else
  2192. {
  2193. strncat(post_req_data, (char *)(receiveBuf + offset), post_data_count);
  2194. if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES)
  2195. {
  2196. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2197. }
  2198. else
  2199. {
  2200. fs_open("/login.html", &file);
  2201. ssl_sendframes(&ssl, file.data, file.len);
  2202. }
  2203. post_data_count = 0;
  2204. log_post_reqn = 0;
  2205. }
  2206. }
  2207. /* request was fragmented before "\r\n\r\n" */
  2208. else
  2209. {
  2210. log_post_reqn++;
  2211. /* wait max 2 requests */
  2212. if (log_post_reqn > 1)
  2213. {
  2214. /* Redirect to login page */
  2215. fs_open("/login.html", &file);
  2216. ssl_sendframes(&ssl, file.data, file.len);
  2217. /* End reqest */
  2218. post_data_count = 0;
  2219. log_post_reqn = 0;
  2220. }
  2221. }
  2222. }
  2223. else
  2224. {
  2225. //printf("Too long POST request!\r\n");
  2226. /* Ignore request */
  2227. post_data_count = 0;
  2228. log_post_reqn = 0;
  2229. /* Redirect to login page */
  2230. fs_open("/login.html", &file);
  2231. ssl_sendframes(&ssl, file.data, file.len);
  2232. }
  2233. }
  2234. else if (post_data_count > 0)
  2235. {
  2236. strncat(post_req_data, receiveBuf, post_data_count);
  2237. if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES)
  2238. {
  2239. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2240. }
  2241. else
  2242. {
  2243. fs_open("/login.html", &file);
  2244. ssl_sendframes(&ssl, file.data, file.len);
  2245. }
  2246. post_data_count = 0;
  2247. log_post_reqn = 0;
  2248. }
  2249. else
  2250. {
  2251. fs_open("/login.html", &file);
  2252. ssl_sendframes(&ssl, file.data, file.len);
  2253. }
  2254. }
  2255. else if ( Authenticated == true )
  2256. {
  2257. if (strncmp(receiveBuf, "GET /main.css", 13) == 0) // +
  2258. {
  2259. fs_open("/main.css", &file);
  2260. ssl_sendframes(&ssl, file.data, file.len);
  2261. }
  2262. else if (strncmp(receiveBuf, "GET /rotek.png", 14) == 0) // +
  2263. {
  2264. fs_open("/rotek.png", &file);
  2265. ssl_sendframes(&ssl, file.data, file.len);
  2266. }
  2267. else if (strncmp(receiveBuf, "GET /favicon.ico", 16) == 0) // ?
  2268. {
  2269. fs_open("/favicon.ico", &file);
  2270. ssl_sendframes(&ssl, file.data, file.len);
  2271. }
  2272. else if (strncmp(receiveBuf, "GET /main.js", 12) == 0) // +
  2273. {
  2274. fs_open("/main.js", &file);
  2275. ssl_sendframes(&ssl, file.data, file.len);
  2276. }
  2277. else if (strncmp(receiveBuf, "GET /role.js", 12) == 0)
  2278. {
  2279. fs_open("/role.js", &file);
  2280. ssl_sendframes(&ssl, file.data, file.len);
  2281. }
  2282. else if (strncmp(receiveBuf, "GET /settings.html", 18) == 0) // +
  2283. {
  2284. HTTP_UpdateUserLoginTime(user_id);
  2285. if (seclevel == 0)
  2286. {
  2287. fs_open("/settings.html", &file);
  2288. ssl_sendframes(&ssl, file.data, file.len);
  2289. }
  2290. else
  2291. {
  2292. fs_open("/index.html", &file);
  2293. ssl_sendframes(&ssl, file.data, file.len);
  2294. }
  2295. }
  2296. else if (strncmp(receiveBuf, "GET /info.html", 14) == 0) // +
  2297. {
  2298. HTTP_UpdateUserLoginTime(user_id);
  2299. fs_open("/info.html", &file);
  2300. ssl_sendframes(&ssl, file.data, file.len);
  2301. }
  2302. else if (strncmp(receiveBuf, "GET /history.html", 17) == 0)
  2303. {
  2304. HTTP_UpdateUserLoginTime(user_id);
  2305. fs_open("/history.html", &file);
  2306. ssl_sendframes(&ssl, file.data, file.len);
  2307. }
  2308. else if (strncmp(receiveBuf, "GET /ups_history.html", 21) == 0)
  2309. {
  2310. HTTP_UpdateUserLoginTime(user_id);
  2311. fs_open("/ups_history.html", &file);
  2312. ssl_sendframes(&ssl, file.data, file.len);
  2313. }
  2314. else if (strncmp(receiveBuf, "GET /getJson.cgi", 16) == 0) // +
  2315. {
  2316. HTTP_GetParamsPage1(sendBuf);
  2317. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2318. }
  2319. else if (strncmp(receiveBuf, "GET /settings.cgi", 17) == 0) // +
  2320. {
  2321. if (seclevel == 0) {
  2322. SET_PAGE = SET_PAGE_PAGE2;
  2323. if (HTTP_SettingsPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  2324. {
  2325. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2326. }
  2327. }
  2328. }
  2329. else if (strncmp(receiveBuf, "POST /settings.cgi", 18) == 0)
  2330. {
  2331. if (seclevel == 0)
  2332. {
  2333. DataOffset = 0;
  2334. // POST Packet received
  2335. if (DataFlag2 == 0)
  2336. {
  2337. BrowserFlag = 0;
  2338. TotalReceived = 0;
  2339. memset(sendBuf, 0, strlen(sendBuf));
  2340. // parse packet for Content-length field
  2341. size = Parse_Content_Length(receiveBuf, receivedBufLen);
  2342. // parse packet for the octet-stream field
  2343. for (i = 0; i < receivedBufLen; i++)
  2344. {
  2345. if (strncmp ((char*)(receiveBuf+i), "managerIP", 8)==0)
  2346. {
  2347. DataOffset = i;
  2348. break;
  2349. }
  2350. }
  2351. /* case of MSIE8 : we do not receive data in the POST packet*/
  2352. if (DataOffset == 0)
  2353. {
  2354. DataFlag2++;
  2355. BrowserFlag = 1;
  2356. //pbuf_free(p);
  2357. return;
  2358. }
  2359. // case of Mozilla Firefox v3.6 : we receive data in the POST packet
  2360. else
  2361. {
  2362. //TotalReceived = receivedBufLen - (ContentLengthOffset + 4);
  2363. TotalReceived = receivedBufLen - DataOffset;
  2364. }
  2365. }
  2366. if (((DataFlag2 ==1)&&(BrowserFlag==1)) || ((DataFlag2 ==0)&&(BrowserFlag==0)))
  2367. {
  2368. if ((DataFlag2 ==0)&&(BrowserFlag==0))
  2369. {
  2370. DataFlag2++;
  2371. }
  2372. else if ((DataFlag2 ==1)&&(BrowserFlag==1))
  2373. {
  2374. // parse packet for the octet-stream field */
  2375. for (i = 0; i < receivedBufLen; i++)
  2376. {
  2377. if (strncmp ((char*)(receiveBuf+i), "managerIP", 8)==0)
  2378. {
  2379. DataOffset = i;
  2380. break;
  2381. }
  2382. }
  2383. TotalReceived += receivedBufLen;
  2384. DataFlag2++;
  2385. }
  2386. TotalData = 0;
  2387. }
  2388. // DataFlag >1 => the packet is data only
  2389. else
  2390. {
  2391. TotalReceived +=receivedBufLen;
  2392. }
  2393. ptr = (char*)(receiveBuf + DataOffset);
  2394. receivedBufLen-= DataOffset;
  2395. // update Total data received counter
  2396. TotalData +=receivedBufLen;
  2397. // check if last data packet
  2398. if (TotalReceived == size)
  2399. {
  2400. //DBG printf("State: Received %d bytes\r\n", (int)TotalReceived);
  2401. strncat(sendBuf, ptr, receivedBufLen);
  2402. strncat(sendBuf, " ", 1);
  2403. //ВBG printf("receive %s /r/n", sendBuf);
  2404. HTTP_SetSettings(sendBuf, strlen(sendBuf));
  2405. DataFlag2=0;
  2406. BrowserFlag = 0;
  2407. memset(sendBuf, 0, size);
  2408. strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
  2409. strcat(sendBuf, "\r\n\r\n");
  2410. strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/settings.html\"/></head></html>\r\n\r\n");
  2411. sendBufLoadLen = strlen(sendBuf);
  2412. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2413. }
  2414. // not last data packet
  2415. else
  2416. {
  2417. // write data in flash
  2418. if(receivedBufLen)
  2419. {
  2420. strncat(sendBuf, ptr, receivedBufLen);
  2421. //memcpy(receiveBufTemp, ptr, receivedBufLen);
  2422. }
  2423. }
  2424. }
  2425. }
  2426. else if (strncmp(receiveBuf, "GET /info.cgi", 13) == 0) // +
  2427. {
  2428. if (HTTP_InfoPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  2429. {
  2430. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2431. }
  2432. }
  2433. else if (strncmp(receiveBuf, "POST /info.cgi", 14) == 0)
  2434. {
  2435. if (seclevel == 0)
  2436. {
  2437. DataOffset = 0;
  2438. // POST Packet received
  2439. if (DataFlag == 0)
  2440. {
  2441. BrowserFlag = 0;
  2442. TotalReceived = 0;
  2443. memset(sendBuf, 0, strlen(sendBuf));
  2444. // parse packet for Content-length field
  2445. size = Parse_Content_Length(receiveBuf, receivedBufLen);
  2446. // parse packet for the octet-stream field
  2447. for (i = 0; i < receivedBufLen; i++)
  2448. {
  2449. if (strncmp ((char*)(receiveBuf+i), "owner", 5)==0)
  2450. {
  2451. DataOffset = i;
  2452. break;
  2453. }
  2454. }
  2455. // case of MSIE8 : we do not receive data in the POST packet
  2456. if (DataOffset == 0)
  2457. {
  2458. DataFlag++;
  2459. BrowserFlag = 1;
  2460. //pbuf_free(p);
  2461. return;
  2462. }
  2463. // case of Mozilla Firefox v3.6 : we receive data in the POST packet*/
  2464. else
  2465. {
  2466. //TotalReceived = receivedBufLen - (ContentLengthOffset + 4);
  2467. TotalReceived = receivedBufLen - DataOffset;
  2468. }
  2469. }
  2470. if (((DataFlag ==1)&&(BrowserFlag==1)) || ((DataFlag ==0)&&(BrowserFlag==0)))
  2471. {
  2472. if ((DataFlag ==0)&&(BrowserFlag==0))
  2473. {
  2474. DataFlag++;
  2475. }
  2476. else if ((DataFlag ==1)&&(BrowserFlag==1))
  2477. {
  2478. // parse packet for the octet-stream field */
  2479. for (i = 0; i < receivedBufLen; i++)
  2480. {
  2481. if (strncmp ((char*)(receiveBuf+i), "owner", 5)==0)
  2482. {
  2483. DataOffset = i;
  2484. break;
  2485. }
  2486. }
  2487. TotalReceived += receivedBufLen;
  2488. DataFlag++;
  2489. }
  2490. TotalData =0 ;
  2491. }
  2492. // DataFlag >1 => the packet is data only
  2493. else
  2494. {
  2495. TotalReceived +=receivedBufLen;
  2496. }
  2497. ptr = (char*)(receiveBuf + DataOffset);
  2498. receivedBufLen-= DataOffset;
  2499. // update Total data received counter
  2500. TotalData +=receivedBufLen;
  2501. // check if last data packet
  2502. if (TotalReceived == size)
  2503. {
  2504. strncat(sendBuf, ptr, receivedBufLen);
  2505. strncat(sendBuf, " ", 1);
  2506. HTTP_SetInfo(sendBuf, strlen(sendBuf));
  2507. DataFlag = 0;
  2508. BrowserFlag = 0;
  2509. memset(sendBuf, 0, size);
  2510. strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
  2511. strcat(sendBuf, "\r\n\r\n");
  2512. strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/info.html\"/></head></html>\r\n\r\n");
  2513. sendBufLoadLen = strlen(sendBuf);
  2514. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2515. }
  2516. // not last data packet
  2517. else
  2518. {
  2519. // write data in flash
  2520. if(receivedBufLen)
  2521. {
  2522. strncat(sendBuf, ptr, receivedBufLen);
  2523. //memcpy(receiveBufTemp, ptr, receivedBufLen);
  2524. }
  2525. }
  2526. }
  2527. }
  2528. else if (strncmp(receiveBuf, "GET /history.cgi", 16) == 0)
  2529. {
  2530. int res;
  2531. res = HTTP_HistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2532. if (res == SEND_REQUIRED_FILE)
  2533. {
  2534. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2535. //send_data(pcb, hs);
  2536. //tcp_sent(pcb, http_sent_history);
  2537. HTTP_SendHistory();
  2538. //tcp_err(pcb, http_sent_log_err);
  2539. }
  2540. else if (res == SEND_REQUIRED_YES)
  2541. {
  2542. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2543. }
  2544. }
  2545. else if (strncmp(receiveBuf, "GET /ups_history.cgi", 19) == 0)
  2546. {
  2547. int res;
  2548. res = HTTP_UpsHistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2549. if (res == SEND_REQUIRED_FILE)
  2550. {
  2551. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2552. //send_data(pcb, hs);
  2553. //tcp_sent(pcb, http_sent_log);
  2554. HTTP_SendLog();
  2555. //tcp_err(pcb, http_sent_log_err);
  2556. }
  2557. else if (res == SEND_REQUIRED_YES)
  2558. {
  2559. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2560. }
  2561. }
  2562. /* Тест АКБ ИБП */
  2563. else if (strncmp(receiveBuf, "POST /bat_test.cgi", 18) == 0)
  2564. {
  2565. HTTP_UPSTest(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2566. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2567. }
  2568. /* Выключение ИБП */
  2569. else if (strncmp(receiveBuf, "POST /ups_power.cgi", 19) == 0)
  2570. {
  2571. HTTP_UPSshutdown(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2572. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2573. }
  2574. /* Сброс настроек и сохранине */
  2575. else if (strncmp(receiveBuf, "GET /reset.cgi", 14) == 0)
  2576. {
  2577. HTTP_ResetSettings();
  2578. HTTP_SaveSettings();
  2579. fs_open("/settings.html", &file);
  2580. ssl_sendframes(&ssl, file.data, file.len);
  2581. }
  2582. /* Перезагрузка контроллера */
  2583. else if (strncmp(receiveBuf, "GET /reboot.cgi", 15) == 0)
  2584. {
  2585. HTTP_Reboot();
  2586. }
  2587. /* Подтверждение новых сетевых настроек */
  2588. else if (strncmp(receiveBuf, "GET /confirm.cgi", 16) == 0)
  2589. {
  2590. SetWebReinitFlag(false);
  2591. SetConfirmWebParamsFlag();
  2592. fs_open("/index.html", &file);
  2593. ssl_sendframes(&ssl, file.data, file.len);
  2594. }
  2595. /* Проверка пароля, переход в bootloader */
  2596. else if (strncmp(receiveBuf, "GET /fw_update.cgi", 18) == 0)
  2597. {
  2598. HTTP_ConfirmBootPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2599. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2600. }
  2601. /* Смена пароля пользователя */
  2602. else if (strncmp(receiveBuf, "POST /changepwd.cgi", 19) == 0)
  2603. {
  2604. HTTP_ChangeUserPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2605. ssl_sendframes(&ssl, file.data, file.len);
  2606. }
  2607. // На производстве
  2608. else if (strncmp(receiveBuf, "GET /setProdate.cgi", 19) == 0)
  2609. {
  2610. HTTP_Prodate(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2611. ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
  2612. }
  2613. else
  2614. {
  2615. HTTP_UpdateUserLoginTime(user_id);
  2616. fs_open("/index.html", &file); // +
  2617. ssl_sendframes(&ssl, file.data, file.len);
  2618. }
  2619. }
  2620. //}
  2621. /*
  2622. if (strncmp(buf, "GET /main.css", 13) == 0) // +
  2623. {
  2624. fs_open("/main.css", &file);
  2625. ssl_sendframes(&ssl, file.data, file.len);
  2626. }
  2627. else if (strncmp(buf, "GET /rotek.png", 14) == 0) // +
  2628. {
  2629. fs_open("/rotek.png", &file);
  2630. ssl_sendframes(&ssl, file.data, file.len);
  2631. }
  2632. else if (strncmp(buf, "GET /favicon.ico", 16) == 0) // ?
  2633. {
  2634. fs_open("/favicon.ico", &file);
  2635. ssl_sendframes(&ssl, file.data, file.len);
  2636. }
  2637. else if (strncmp(buf, "GET /main.js", 12) == 0) // +
  2638. {
  2639. fs_open("/main.js", &file);
  2640. ssl_sendframes(&ssl, file.data, file.len);
  2641. }
  2642. else if (strncmp(buf, "GET /getJson.cgi", 16) == 0) // +
  2643. {
  2644. HTTP_GetParamsPage1(sendBuf);
  2645. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2646. }
  2647. else if (strncmp(buf, "GET /settings.html", 18) == 0) // +
  2648. {
  2649. fs_open("/settings.html", &file);
  2650. ssl_sendframes(&ssl, file.data, file.len);
  2651. }
  2652. else if (strncmp(buf, "GET /info.html", 14) == 0) // +
  2653. {
  2654. fs_open("/info.html", &file);
  2655. ssl_sendframes(&ssl, file.data, file.len);
  2656. }
  2657. else if (strncmp(buf, "GET /getJson.cgi", 16) == 0) // +
  2658. {
  2659. HTTP_GetParamsPage1(sendBuf);
  2660. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2661. }
  2662. else if (strncmp(buf, "GET /settings.cgi", 17) == 0) // +
  2663. {
  2664. SET_PAGE = SET_PAGE_PAGE2;
  2665. if (HTTP_SettingsPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  2666. {
  2667. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2668. }
  2669. else
  2670. {
  2671. fs_open("/settings.html", &file);
  2672. ssl_sendframes(&ssl, file.data, file.len);
  2673. }
  2674. }
  2675. else if (strncmp(buf, "GET /info.cgi", 13) == 0) // +
  2676. {
  2677. if (HTTP_InfoPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
  2678. {
  2679. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2680. }
  2681. else
  2682. {
  2683. fs_open("/info.html", &file);
  2684. ssl_sendframes(&ssl, file.data, file.len);
  2685. }
  2686. }
  2687. // Сброс настроек и сохранине
  2688. else if (strncmp(buf, "GET /reset.cgi", 14) == 0)
  2689. {
  2690. HTTP_ResetSettings();
  2691. HTTP_SaveSettings();
  2692. fs_open("/settings.html", &file);
  2693. ssl_sendframes(&ssl, file.data, file.len);
  2694. }
  2695. // Перезагрузка контроллера
  2696. else if (strncmp(buf, "GET /reboot.cgi", 15) == 0)
  2697. {
  2698. HTTP_Reboot();
  2699. }
  2700. // Подтверждение новых сетевых настроек
  2701. else if (strncmp(buf, "GET /confirm.cgi", 16) == 0)
  2702. {
  2703. SetWebReinitFlag(false);
  2704. SetConfirmWebParamsFlag();
  2705. fs_open("/index.html", &file);
  2706. ssl_sendframes(&ssl, file.data, file.len);
  2707. }
  2708. // Проверка пароля, переход в bootloader
  2709. else if (strncmp(buf, "POST /checkpwd.cgi", 18) == 0)
  2710. {
  2711. HTTP_ConfirmBootPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2712. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2713. }
  2714. // На производстве
  2715. else if (strncmp(buf, "GET /setProdate.cgi", 19) == 0)
  2716. {
  2717. HTTP_Prodate(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2718. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2719. }
  2720. // На производстве
  2721. else if (strncmp(buf, "GET /progon.cgi", 15) == 0)
  2722. {
  2723. HTTP_Progon(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
  2724. ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
  2725. }
  2726. else
  2727. {
  2728. fs_open("/index.html", &file);
  2729. ssl_sendframes(&ssl, file.data, file.len);
  2730. }
  2731. // -------------------------------------------------------------------------
  2732. */
  2733. // Для теста
  2734. // 7. Write the Response
  2735. //printf("\n\r => Write to client :\n\r");
  2736. // Send the dynamic html page
  2737. //ssl_DynPage(&ssl);
  2738. // Close the connection
  2739. ssl_close_notify(&ssl);
  2740. goto accept;
  2741. exit:
  2742. // Close and delete the current session data: certificate, RSA key and SSL session
  2743. x509_free(&srvcert);
  2744. rsa_free(&rsa);
  2745. cur = s_list_1st;
  2746. while(cur != NULL)
  2747. {
  2748. prv = cur;
  2749. cur = cur->next;
  2750. memset(prv, 0, sizeof(ssl_session));
  2751. free(prv);
  2752. }
  2753. memset(&ssl, 0, sizeof(ssl_context));
  2754. accept:
  2755. // Wait 200s until next retry
  2756. vTaskDelay(200);
  2757. // Close and free the SSL context
  2758. net_close(client_fd);
  2759. ssl_free(&ssl);
  2760. }
  2761. }
  2762. /**
  2763. * @brief Create and send a dynamic Web Page. This page contains the list of
  2764. * running tasks and the number of page hits.
  2765. * @param ssl the SSL context
  2766. * @retval None
  2767. */
  2768. void ssl_DynPage(ssl_context *ssl)
  2769. {
  2770. portCHAR buf[2024];
  2771. portCHAR pagehits[10];
  2772. portCHAR getcipher[100];
  2773. uint32_t len = 0;
  2774. memset(buf, 0, 2024);
  2775. // Update the hit count
  2776. nPageHits++;
  2777. sprintf( pagehits, "%d", nPageHits );
  2778. sprintf( (char *) getcipher, HTTP_RESPONSE, ssl_get_ciphersuite(ssl));
  2779. // Generate the dynamic page
  2780. strcpy(buf, PAGE_START);
  2781. // Page header
  2782. strcat(buf, pagehits);
  2783. strcat((char *) buf, "<br><pre>** The list of tasks and their status **");
  2784. strcat((char *) buf, "<br><pre>---------------------------------------------");
  2785. strcat((char *) buf, "<br>Name State Priority Stack Num" );
  2786. //strcat((char *) buf, "<br>---------------------------------------------");
  2787. strcat((char *) buf, "<br>---------------------------------------------<br>");
  2788. // The list of tasks and their status
  2789. vTaskList((signed char *)buf + strlen(buf));
  2790. strcat((char *) buf, "<br>---------------------------------------------");
  2791. strcat((char *) buf, "<br>B : Blocked, R : Ready, D : Deleted, S : Suspended");
  2792. // The current cipher used
  2793. strcat(buf, getcipher);
  2794. // Page footer
  2795. strcat(buf, PAGE_END);
  2796. // Send the dynamically generated page
  2797. len = ssl_write(ssl, (unsigned char *)buf, strlen(buf));
  2798. // Display the length of application data
  2799. //printf( "\n Successfully write %d bytes to client\n\r", len);
  2800. }
  2801. /**
  2802. * @brief This function is used to send messages with size upper 16k bytes.
  2803. * @param ssl SSL context
  2804. * @param data buffer holding the data
  2805. * @param len how many bytes must be written
  2806. * @retval None
  2807. */
  2808. #define FRAME_SIZE (1000)
  2809. void ssl_sendframes( ssl_context *ssl, char *data, int datalen )
  2810. {
  2811. int index = 0;
  2812. int k = 0;
  2813. int lastframe, nbrframes;
  2814. // Calculate the number of frames
  2815. nbrframes = datalen / FRAME_SIZE;
  2816. // Send nbrframes frames
  2817. while(nbrframes > 0)
  2818. {
  2819. index = k * FRAME_SIZE;
  2820. ssl_write( ssl, (unsigned char *)(data + index), FRAME_SIZE );
  2821. nbrframes--;
  2822. k++;
  2823. }
  2824. // Send the last frame
  2825. index = k * FRAME_SIZE;
  2826. lastframe = datalen % FRAME_SIZE ;
  2827. ssl_write( ssl, (unsigned char *)(data + index), lastframe );
  2828. }
  2829. /**
  2830. * @brief Returns a 32-bit random number.
  2831. * @param arg not used
  2832. * @retval 32-bit random number
  2833. */
  2834. int RandVal(void* arg)
  2835. {
  2836. return (int)GetRandomNumber();
  2837. }
  2838. /**
  2839. * @brief Send callback for log file transfer (messages as is, not ordered)
  2840. */
  2841. void HTTP_SendHistory(void)
  2842. {
  2843. uint32_t nbytes = 0;
  2844. static bool start = true;
  2845. memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
  2846. if (log_ptr + FILE_BUF_MAX_LEN <= log_size) {
  2847. nbytes = History_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN, start);
  2848. }
  2849. else if (log_ptr < log_size) {
  2850. nbytes = History_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
  2851. }
  2852. else {
  2853. nbytes = 0;
  2854. }
  2855. log_ptr += nbytes;
  2856. start = false;
  2857. if (nbytes == 0) {
  2858. // File transfer finished.
  2859. start = true;
  2860. // Clear file transfer in progress flag
  2861. fLogTransInprog = false;
  2862. return;
  2863. }
  2864. ssl_sendframes(&ssl, logFileBuf, nbytes);
  2865. HTTP_SendHistory();
  2866. }
  2867. /**
  2868. * @brief Sent callback for log file transfer (messages as is, not ordered)
  2869. */
  2870. void HTTP_SendLog(void)
  2871. {
  2872. uint32_t nbytes = 0;
  2873. static bool start = true;
  2874. memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
  2875. if (log_ptr + FILE_BUF_MAX_LEN_LOG <= log_size) {
  2876. nbytes = LOG_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN_LOG, start);
  2877. }
  2878. else if (log_ptr < log_size) {
  2879. nbytes = LOG_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
  2880. }
  2881. else {
  2882. nbytes = 0;
  2883. }
  2884. log_ptr += nbytes;
  2885. start = false;
  2886. if (nbytes == 0) {
  2887. // File transfer finished.
  2888. start = true;
  2889. // Clear file transfer in progress flag
  2890. fLogTransInprog = false;
  2891. return;
  2892. }
  2893. ssl_sendframes(&ssl, logFileBuf, nbytes);
  2894. HTTP_SendLog();
  2895. return;
  2896. }
  2897. /**
  2898. * @brief Initialize the HTTPS server (start its thread)
  2899. */
  2900. void HTTPS_Init()
  2901. {
  2902. char buf[MAX_WEB_COOKIE_LEN];
  2903. uint8_t user_id;
  2904. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  2905. // Flush user cookie by random value
  2906. sprintf(buf, "%X", (unsigned int)GetRandomNumber());
  2907. HTTP_SetUserCookie(buf, user_id);
  2908. // Create user logout timers
  2909. users[user_id].LogoutTimer =
  2910. xTimerCreate("LogoutTmr", WEB_LOGOUT_TIME, pdFALSE, ( void * ) user_id, LogoutTimerCallback);
  2911. }
  2912. }