http_server.c 95 KB

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