boolean RoomReader(FILE * datFile) { /*----local vars----*/ int x = 0, y = 0, // x,y coords i = 0, // counter value = 0; // generic input value char title[ROOM_MAX_TITLE_LEN] = {'\0'}, descrip[MAX_DESCRIP_LEN] = {'\0'}, delimiter, temp; // used to check for eof //roomType rooms[UNI_MAX_X][UNI_MAX_Y]; //update 4/30 Michael Simpson if(!datFile) { //fprintf(stderr, "Invalid data file passed to RoomReader!\n"); return(FALSE); } temp = fgetc(datFile); while(!feof(datFile)) { ungetc(temp, datFile); fscanf(datFile, " %d %d", &x, &y); ReadLine(title, ROOM_MAX_TITLE_LEN, datFile); rmSetTitle(&(rooms[x][y]), title); delimiter = fgetc(datFile); ReadUntil(descrip, MAX_DESCRIP_LEN, delimiter, datFile); rmSetDescrip(&(rooms[x][y]), descrip); for(i = 0; i < MAX_DIRECTIONS; i++) { fscanf(datFile, " %d", &value); if(value == 1) { directionType dir = (directionType)i; rmSetValidExit(&(rooms[x][y]), dir); } } fscanf(datFile, " %d", &value); if(value == 0) rmUnlock(&(rooms[x][y])); rmSetWall(&(rooms[x][y]), FALSE); ClearStr(title, ROOM_MAX_TITLE_LEN); ClearStr(descrip, MAX_DESCRIP_LEN); value = 0; temp = fgetc(datFile); } return(TRUE); }
// Convert an integer list to a string void IntListToStr(char *str, UINT str_size, LIST *o, char *separate_str) { UINT i; ClearStr(str, str_size); // Validate arguments if (o == NULL) { return; } if (IsEmptyStr(separate_str)) { separate_str = ", "; } for (i = 0;i < LIST_NUM(o);i++) { char tmp[MAX_SIZE]; UINT *v = LIST_DATA(o, i); ToStr(tmp, *v); StrCat(str, str_size, tmp); if (i != (LIST_NUM(o) - 1)) { StrCat(str, str_size, separate_str); } } }
MiniApache::MiniApache(int port){ _server = new EthernetServer(port); //_server(port); _storage_path = ""; ClearStr(_HTTP_request, REQUEST_BUFFER_SIZE); //ClearStr(_request_path, REQUEST_BUFFER_SIZE); _error_status = 0; _serve_status = 0; }
void MiniApache::ReportClientServed(int status){ #if SERIAL_DEBUG == 1 Serial.print(_HTTP_request); // Debug Serial.print(" -> "); Serial.println(status); #endif ClearStr(_HTTP_request, REQUEST_BUFFER_SIZE); _request_index = 0; _serve_status = 1; //delay(10); // give the web browser time to receive the data client.stop(); // close the connection }
bool MiniApache::PendingRequest(){ if (_serve_status == 1){ // Waiting for client client = _server->available(); // try to get client if (client) { // got client? ClearStr(_HTTP_request, REQUEST_BUFFER_SIZE); _serve_status = 2; } } if (_serve_status == 2){ // Reading request if (client.connected()){ while (client.available()){ // client data available to read char c = client.read(); if (_request_index < (REQUEST_BUFFER_SIZE - 1)){ // save HTTP request character _HTTP_request[_request_index] = c; _request_index++; } else { PrintHeader(400, "Bad request", "text/html", false); ReportClientServed(400); break; } if (c == '\r' || c == '\n'){ _serve_status = 3; delete [] GET_data; delete [] request_path; request_path = ExtractRequestPath(); // Get path of the HTTP request, ignore type (assume GET) GET_data = ExtractRequestData(); // Get GET data of the HTTP request if (_error_status){ // Error was detected (earlier) PrintHeader(500, "Internal Server Error", "text/html", false); client.println("<thml><hrad><title>Internal Server Error</title></head><body>500: Internal Server Error<br>Error code:"); client.println(_error_status); client.println("<br>Error message:"); client.println(GetErrorMessage()); client.println("<hr>MiniApache</body></html>"); ReportClientServed(500); } #if SERIAL_DEBUG == 1 Serial.print("Free memory: "); Serial.println(freeRam()); #endif break; // Ignore rest of the HTTP request } } } } return (_serve_status == 3); }
// Decompose combined name UINT Win32EthGetNameAndIdFromCombinedName(char *name, UINT name_size, char *str) { UINT ret = 0; char id_str[MAX_SIZE]; UINT len; // Validate arguments ClearStr(name, name_size); StrCpy(name, name_size, str); if (name == NULL || str == NULL) { return 0; } len = StrLen(str); if (len >= 16) { StrCpy(id_str, sizeof(id_str), str + len - 16); if (StartWith(id_str, " (ID=")) { if (EndWith(id_str, ")")) { char num[MAX_SIZE]; Zero(num, sizeof(num)); StrCpy(num, sizeof(num), id_str + 5); num[StrLen(num) - 1] = 0; ret = ToInt(num); if (ret != 0) { name[len - 16] = 0; } } } } return ret; }
// VPN Azure client main thread void AcMainThread(THREAD *thread, void *param) { AZURE_CLIENT *ac = (AZURE_CLIENT *)param; UINT last_ip_revision = INFINITE; UINT64 last_reconnect_tick = 0; UINT64 next_reconnect_interval = AZURE_CONNECT_INITIAL_RETRY_INTERVAL; UINT num_reconnect_retry = 0; UINT64 next_ddns_retry_tick = 0; bool last_connect_ok = false; // Validate arguments if (ac == NULL || thread == NULL) { return; } while (ac->Halt == false) { UINT64 now = Tick64(); bool connect_was_ok = false; // Wait for enabling VPN Azure function if (ac->IsEnabled) { // VPN Azure is enabled DDNS_CLIENT_STATUS st; bool connect_now = false; bool azure_ip_changed = false; Lock(ac->Lock); { Copy(&st, &ac->DDnsStatus, sizeof(DDNS_CLIENT_STATUS)); if (StrCmpi(st.CurrentAzureIp, ac->DDnsStatusCopy.CurrentAzureIp) != 0) { if (IsEmptyStr(st.CurrentAzureIp) == false) { // Destination IP address is changed connect_now = true; num_reconnect_retry = 0; } } if (StrCmpi(st.CurrentHostName, ac->DDnsStatusCopy.CurrentHostName) != 0) { // DDNS host name is changed connect_now = true; num_reconnect_retry = 0; } Copy(&ac->DDnsStatusCopy, &st, sizeof(DDNS_CLIENT_STATUS)); } Unlock(ac->Lock); if (last_ip_revision != ac->IpStatusRevision) { last_ip_revision = ac->IpStatusRevision; connect_now = true; num_reconnect_retry = 0; } if (last_reconnect_tick == 0 || (now >= (last_reconnect_tick + next_reconnect_interval))) { UINT r; last_reconnect_tick = now; num_reconnect_retry++; next_reconnect_interval = (UINT64)num_reconnect_retry * AZURE_CONNECT_INITIAL_RETRY_INTERVAL; next_reconnect_interval = MIN(next_reconnect_interval, AZURE_CONNECT_MAX_RETRY_INTERVAL); r = (UINT)next_reconnect_interval; r = GenRandInterval(r / 2, r); next_reconnect_interval = r; connect_now = true; } if (IsEmptyStr(st.CurrentAzureIp) == false && IsEmptyStr(st.CurrentHostName) == false) { if (connect_now) { SOCK *s; char *host = NULL; UINT port = AZURE_SERVER_PORT; Debug("VPN Azure: Connecting to %s...\n", st.CurrentAzureIp); if (ParseHostPort(st.CurrentAzureIp, &host, &port, AZURE_SERVER_PORT)) { if (st.InternetSetting.ProxyType == PROXY_DIRECT) { s = ConnectEx2(host, port, 0, (bool *)&ac->Halt); } else { s = WpcSockConnect2(host, port, &st.InternetSetting, NULL, AZURE_VIA_PROXY_TIMEOUT); } if (s != NULL) { PACK *p; UINT64 established_tick = 0; Debug("VPN Azure: Connected.\n"); SetTimeout(s, AZURE_PROTOCOL_CONTROL_TIMEOUT_DEFAULT); Lock(ac->Lock); { ac->CurrentSock = s; ac->IsConnected = true; StrCpy(ac->ConnectingAzureIp, sizeof(ac->ConnectingAzureIp), st.CurrentAzureIp); } Unlock(ac->Lock); SendAll(s, AZURE_PROTOCOL_CONTROL_SIGNATURE, StrLen(AZURE_PROTOCOL_CONTROL_SIGNATURE), false); // Receive parameter p = RecvPackWithHash(s); if (p != NULL) { UCHAR c; AZURE_PARAM param; bool hostname_changed = false; Zero(¶m, sizeof(param)); param.ControlKeepAlive = PackGetInt(p, "ControlKeepAlive"); param.ControlTimeout = PackGetInt(p, "ControlTimeout"); param.DataTimeout = PackGetInt(p, "DataTimeout"); param.SslTimeout = PackGetInt(p, "SslTimeout"); FreePack(p); param.ControlKeepAlive = MAKESURE(param.ControlKeepAlive, 1000, AZURE_SERVER_MAX_KEEPALIVE); param.ControlTimeout = MAKESURE(param.ControlTimeout, 1000, AZURE_SERVER_MAX_TIMEOUT); param.DataTimeout = MAKESURE(param.DataTimeout, 1000, AZURE_SERVER_MAX_TIMEOUT); param.SslTimeout = MAKESURE(param.SslTimeout, 1000, AZURE_SERVER_MAX_TIMEOUT); Lock(ac->Lock); { Copy(&ac->AzureParam, ¶m, sizeof(AZURE_PARAM)); } Unlock(ac->Lock); SetTimeout(s, param.ControlTimeout); // Send parameter p = NewPack(); PackAddStr(p, "CurrentHostName", st.CurrentHostName); PackAddStr(p, "CurrentAzureIp", st.CurrentAzureIp); PackAddInt64(p, "CurrentAzureTimestamp", st.CurrentAzureTimestamp); PackAddStr(p, "CurrentAzureSignature", st.CurrentAzureSignature); Lock(ac->Lock); { if (StrCmpi(st.CurrentHostName, ac->DDnsStatus.CurrentHostName) != 0) { hostname_changed = true; } } Unlock(ac->Lock); if (hostname_changed == false) { if (SendPackWithHash(s, p)) { // Receive result if (RecvAll(s, &c, 1, false)) { if (c && ac->Halt == false) { connect_was_ok = true; established_tick = Tick64(); AcWaitForRequest(ac, s, ¶m); } } } } FreePack(p); } else { WHERE; } Debug("VPN Azure: Disconnected.\n"); Lock(ac->Lock); { ac->IsConnected = false; ac->CurrentSock = NULL; ClearStr(ac->ConnectingAzureIp, sizeof(ac->ConnectingAzureIp)); } Unlock(ac->Lock); if (established_tick != 0) { if ((established_tick + (UINT64)AZURE_CONNECT_MAX_RETRY_INTERVAL) <= Tick64()) { // If the connected time exceeds the AZURE_CONNECT_MAX_RETRY_INTERVAL, reset the retry counter. last_reconnect_tick = 0; num_reconnect_retry = 0; next_reconnect_interval = AZURE_CONNECT_INITIAL_RETRY_INTERVAL; } } Disconnect(s); ReleaseSock(s); } else { Debug("VPN Azure: Error: Connect Failed.\n"); } Free(host); } } } } else { last_reconnect_tick = 0; num_reconnect_retry = 0; next_reconnect_interval = AZURE_CONNECT_INITIAL_RETRY_INTERVAL; } if (ac->Halt) { break; } if (connect_was_ok) { // If connection goes out after connected, increment connection success count to urge DDNS client query next_ddns_retry_tick = Tick64() + MIN((UINT64)DDNS_VPN_AZURE_CONNECT_ERROR_DDNS_RETRY_TIME_DIFF * (UINT64)(num_reconnect_retry + 1), (UINT64)DDNS_VPN_AZURE_CONNECT_ERROR_DDNS_RETRY_TIME_DIFF_MAX); } if ((next_ddns_retry_tick != 0) && (Tick64() >= next_ddns_retry_tick)) { next_ddns_retry_tick = 0; ac->DDnsTriggerInt++; } Wait(ac->Event, rand() % 1000); } }
void fly_to_analyse(char *path, char *config) { char *p = ReadLines(config); char *last=p; char title[128],description[512],reference[512],match[1024],relevance[512]; int result=0,sz=0; while(!result && strlen(last)>16) { switch (parse_eggs(&p, &last)) { case TITLE: sz = p - last; memset(title,0,127); snprintf(title,127,"%.*s", sz, last); strcpy(title,ClearStr(title,10)); break; case DESCRIPTION: sz = p - last; memset(description,0,511); snprintf(description,511,"%.*s", sz, last); strcpy(description,ClearStr(description,16)); break; case REFERENCE: sz = p - last - 1; memset(reference,0,511); snprintf(reference,511,"%.*s", sz, last); strcpy(reference,ClearStr(reference,14)); break; case RELEVANCE: sz = p - last; memset(relevance,0,511); snprintf(relevance,511,"%.*s", sz, last); strcpy(relevance,ClearStr(relevance,14)); break; case MATCH: sz = p - last; memset(match,0,1023); snprintf(match,1023,"%.*s", sz, last); strcpy(match,ClearStr(match,10)); char *result2=Search_for(path,match); // TODO* need validate before print out if(result2!=NULL) if(strlen(result2)>8) { fprintf(stdout,"\n-------------------\n %sTitle:%s %s \n %sDescription:%s %s \n %sRelevance:%s %s \n %sReference:%s %s \n %sMatch:%s %s \n%s%s%s\n",YELLOW,LAST,title,YELLOW,LAST,description,YELLOW,LAST,relevance,YELLOW,LAST,reference,YELLOW,LAST,match,CYAN,result2,LAST); if(log_file != NULL) { FILE *arq; arq=fopen(log_file,"a"); if ( arq == NULL ) { DEBUG("error in XML file %s",log_file); perror("Error "); exit(-1); } fprintf(arq,"<report_mosca>\n <Path>%s</Path>\n <Module>%s</Module>\n <Title>%s</Title>\n <Description>%s</Description>\n <Level>%s</Level>\n <Reference>%s</Reference>\n <Match>%s</Match>\n <Result>%s</Result>\n</report_mosca>\n\n",path,config,title,description,relevance,reference,match,result2); if( fclose(arq) == EOF ) { DEBUG("error in Write() file %s",log_file); perror("Error "); exit(-1); } arq=NULL; } } xfree((void **)&result2); break; case END: result=1; break; } } }
void fly_to_analyse(char *path, char *config) { char *p = ReadLines(config); char *last=p; char *result2=NULL; char title[128],description[512],reference[512],match[128],relevance[512]; int result=0,sz=0; while(!result) switch (parse_eggs(&p, &last)) { case TITLE: sz = p - last; memset(title,0,127); snprintf(title,127,"%.*s", sz, last); // DEBUG("%s\n",title); strcpy(title,ClearStr(title,10)); break; case DESCRIPTION: sz = p - last; memset(description,0,511); snprintf(description,511,"%.*s", sz, last); strcpy(description,ClearStr(description,16)); break; case REFERENCE: sz = p - last - 1; memset(reference,0,511); snprintf(reference,511,"%.*s", sz, last); strcpy(reference,ClearStr(reference,14)); break; case RELEVANCE: sz = p - last; memset(relevance,0,511); snprintf(relevance,511,"%.*s", sz, last); strcpy(relevance,ClearStr(relevance,14)); break; /* TODO* fix bug when test first rule of egg file */ case MATCH: sz = p - last; memset(match,0,127); snprintf(match,127,"%.*s", sz, last); strcpy(match,ClearStr(match,10)); result2=Search_for(path,match); // TODO* need validate before print out if(strlen(result2)>8) { fprintf(stdout,"\n-------------------\n %sTitle:%s %s \n %sDescription:%s %s \n %sRelevance:%s %s \n %sReference:%s %s \n %sMatch:%s %s \n%s%s%s\n",YELLOW,LAST,title,YELLOW,LAST,description,YELLOW,LAST,relevance,YELLOW,LAST,reference,YELLOW,LAST,match,CYAN,result2,LAST); if(log_file != NULL) { // TODO* call one time write()... optimize WriteFile(log_file," Path: "); WriteFile(log_file,path); WriteFile(log_file,"\n Module: "); WriteFile(log_file,config); WriteFile(log_file,"\n Title: "); WriteFile(log_file,title); WriteFile(log_file,"\n Description: "); WriteFile(log_file,description); WriteFile(log_file,"\n reference: "); WriteFile(log_file,reference); WriteFile(log_file,"\n Match: "); WriteFile(log_file,match); WriteFile(log_file,"\n Result: \n"); WriteFile(log_file,result2); } } break; case END: result=1; break; } }