/********************************************************************* * Function: BYTE* HTTPURLDecode(BYTE *data) * * PreCondition: data has at least one extra byte free * * Input: *data the string to decode * * Output: A pointer to the end of the data * * Side Effects: *data is URL decoded * * Overview: Parses a string, converting: * '=' to '\0' * '&' to '\0' * '+' to ' ' * "%xx" to a single byte, as defined by xx (hex) * After completion, the data is de-escaped, and * each null terminator signifies the end of a name * or value. * * Note: This function is called by the stack to parse * GET arguments and modified cookie data. User * applications can use this function to decode POST * data, but need to verify that the string is * null terminated before passing. ********************************************************************/ BYTE* HTTPURLDecode(BYTE *data) { BYTE *read, *write; WORD len; BYTE c; WORD_VAL d; // Determine length of input len = strlen((char*)data); // Read all characters in the string for(read = write = data; len != 0; len--, read++, write++) { c = *read; if(c == '=' || c == '&') *write = '\0'; else if(c == '+') *write = ' '; else if(c == '%') { if(len < 2) len = 0; else { read++; d.v[1] = *read; read++; d.v[0] = *read; *write = hexatob(d); } } else *write = c; } // Double null terminate the last value *write++ = '\0'; *write = '\0'; return write; }
/***************************************************************************** Function: bool TCPIP_Helper_StringToMACAddress(const char* str, uint8_t macAddr[6]) Summary: Converts a string to an MAC address Description: This function parses a MAC address string "aa:bb:cc:dd:ee:ff" or "aa-bb-cc-dd-ee-ff" into an hex MAC address. Precondition: None Parameters: str - Pointer to a colon separated MAC address string macAddr - Pointer to buffer to store the result Return Values: true - a MAC address was successfully decoded false - no MAC address could be found, or the format was incorrect ***************************************************************************/ bool TCPIP_Helper_StringToMACAddress(const char* str, uint8_t macAddr[6]) { const char *beg; TCPIP_UINT16_VAL hexDigit; int ix; beg = str; for(ix=0; ix<6; ix++) { if(!isxdigit(beg[0]) || !isxdigit(beg[1])) { return false; } // found valid byte hexDigit.v[0] = beg[1]; hexDigit.v[1] = beg[0]; *macAddr++ = hexatob(hexDigit.Val); // next colon number beg += 2; if(beg[0] == '\0') { break; // done } else if(beg[0] != ':' && beg[0] != '-') { return false; // invalid delimiter } beg++; // next digit } return ix==5?true:false; // false if not enough digits }
void HTTPExecCmd(BYTE** argv, BYTE argc) { BYTE command; BYTE var; #if defined(ENABLE_REMOTE_CONFIG) DWORD_VAL dwVal; BYTE CurrentArg; WORD_VAL TmpWord; #endif /* * Design your pages such that they contain command code * as a one character numerical value. * Being a one character numerical value greatly simplifies * the job. */ command = argv[0][0] - '0'; /* * Find out the cgi file name and interpret parameters * accordingly */ switch(command) { case CGI_CMD_DIGOUT: // ACTION=0 /* * Identify the parameters. * Compare it in upper case format. */ var = argv[1][0] - '0'; switch(var) { case CMD_LED1: // NAME=0 // Toggle LED. LED1_IO ^= 1; break; case CMD_LED2: // NAME=1 // Toggle LED. LED2_IO ^= 1; break; } memcpypgm2ram((void*)argv[0], (ROM void*)COMMANDS_OK_PAGE, COMMANDS_OK_PAGE_LEN); break; #if defined(USE_LCD) case CGI_CMD_LCDOUT: // ACTION=1 if(argc > 2u) // Text provided in argv[2] { // Convert %20 to spaces, and other URL transformations UnencodeURL(argv[2]); // Write 32 received characters or less to LCDText if(strlen((char*)argv[2]) < 32u) { memset(LCDText, ' ', 32); strcpy((char*)LCDText, (char*)argv[2]); } else { memcpy(LCDText, (void*)argv[2], 32); } // Write LCDText to the LCD LCDUpdate(); } else // No text provided { LCDErase(); } memcpypgm2ram((void*)argv[0], (ROM void*)COMMANDS_OK_PAGE, COMMANDS_OK_PAGE_LEN); break; #endif #if defined(ENABLE_REMOTE_CONFIG) // Possibly useful code for remotely reconfiguring the board through // HTTP case CGI_CMD_RECONFIG: // ACTION=2 // Loop through all variables that we've been given CurrentArg = 1; while(argc > CurrentArg) { // Get the variable identifier (HTML "name"), and // increment to the variable's value TmpWord.v[1] = argv[CurrentArg][0]; TmpWord.v[0] = argv[CurrentArg++][1]; var = hexatob(TmpWord); // Make sure the variable's value exists if(CurrentArg >= argc) break; // Take action with this variable/value switch(var) { case VAR_IP_ADDRESS: case VAR_SUBNET_MASK: case VAR_GATEWAY_ADDRESS: { // Convert the returned value to the 4 octect // binary representation if(!StringToIPAddress(argv[CurrentArg], (IP_ADDR*)&dwVal)) break; // Reconfigure the App to use the new values if(var == VAR_IP_ADDRESS) { // Cause the IP address to be rebroadcast // through Announce.c or the RS232 port since // we now have a new IP address if(dwVal.Val != *(DWORD*)&AppConfig.MyIPAddr) DHCPBindCount++; // Set the new address memcpy((void*)&AppConfig.MyIPAddr, (void*)&dwVal, sizeof(AppConfig.MyIPAddr)); } else if(var == VAR_SUBNET_MASK) memcpy((void*)&AppConfig.MyMask, (void*)&dwVal, sizeof(AppConfig.MyMask)); else if(var == VAR_GATEWAY_ADDRESS) memcpy((void*)&AppConfig.MyGateway, (void*)&dwVal, sizeof(AppConfig.MyGateway)); } break; case VAR_DHCP: if(AppConfig.Flags.bIsDHCPEnabled) { if(!(argv[CurrentArg][0]-'0')) { AppConfig.Flags.bIsDHCPEnabled = FALSE; } } else { if(argv[CurrentArg][0]-'0') { AppConfig.MyIPAddr.Val = 0x00000000ul; AppConfig.Flags.bIsDHCPEnabled = TRUE; AppConfig.Flags.bInConfigMode = TRUE; DHCPInit(0); } } break; } // Advance to the next variable (if present) CurrentArg++; } // Save any changes to non-volatile memory SaveAppConfig(&AppConfig); // Return the same CONFIG.CGI file as a result. memcpypgm2ram((void*)argv[0], (ROM void*)CONFIG_UPDATE_PAGE, CONFIG_UPDATE_PAGE_LEN); break; #endif // #if defined(ENABLE_REMOTE_CONFIG) default: memcpypgm2ram((void*)argv[0], (ROM void*)COMMANDS_OK_PAGE, COMMANDS_OK_PAGE_LEN); break; } }
/***************************************************************************** Function: HTTP_IO_RESULT HTTPExecutePost(void) This function processes every POST request from the pages. ***************************************************************************/ HTTP_IO_RESULT HTTPExecutePost(void) { // Resolve which function to use and pass along BYTE filename[20]; int len; // Load the file name // Make sure BYTE filename[] above is large enough for your longest name MPFSGetFilename(curHTTP.file, filename, sizeof(filename)); while(curHTTP.byteCount) { // Check for a complete variable len = TCPFind(sktHTTP, '&', 0, FALSE); if(len == 0xffff) { // Check if is the last post, otherwise continue in the loop if( TCPIsGetReady(sktHTTP) == curHTTP.byteCount) len = curHTTP.byteCount - 1; else { return HTTP_IO_NEED_DATA; // No last post, we need more data } } if(len > HTTP_MAX_DATA_LEN - 2) { // Make sure we don't overflow curHTTP.byteCount -= TCPGetArray(sktHTTP, (BYTE*)String_post, len+1); continue; } len = TCPGetArray(sktHTTP,curHTTP.data, len+1); curHTTP.byteCount -= len; curHTTP.data[len] = '\0'; HTTPURLDecode(curHTTP.data); // NETWORK TYPE SELECTION: ADHOC/INFRASTRUCTURE/SOFTAP(WIFI G only) if(memcmppgm2ram(curHTTP.data,(ROM void*)"NETTYPE", 7) == 0) { memcpy(String_post,(void*)&curHTTP.data[8], len-8); WFSetParam(NETWORK_TYPE, String_post); } // DHCP CLIENT ENABLING/DISABLING else if(memcmppgm2ram(curHTTP.data,(ROM void*)"DHCPCL", 6) == 0) { memcpy(String_post,(void*)&curHTTP.data[7], len-7); if (String_post [0] == 'd') WFSetParam(DHCP_ENABLE , DISABLED); else WFSetParam(DHCP_ENABLE , ENABLED); } // IP ADDRESS OF THE DEVICE else if(memcmppgm2ram(curHTTP.data,(ROM void*)"IPADDR", 6) == 0) { memcpy(String_post,(void*)&curHTTP.data[7], len-7); WFSetParam(MY_IP_ADDR, String_post); } // SUBNET MASK else if(memcmppgm2ram(curHTTP.data,(ROM void*)"SUBNET", 6) == 0) { memcpy(String_post,(void*)&curHTTP.data[7], len-7); WFSetParam(SUBNET_MASK, String_post); } // DEFAULT GATEWAY else if(memcmppgm2ram(curHTTP.data,(ROM void*)"GATEWAY", 7) == 0) { memcpy(String_post,(void*)&curHTTP.data[8], len-8); WFSetParam(MY_GATEWAY, String_post); } // DNS SERVER #1 else if(memcmppgm2ram(curHTTP.data,(ROM void*)"DNS1", 4) == 0) { memcpy(String_post,(void*)&curHTTP.data[5], len-5); WFSetParam(PRIMARY_DNS, String_post); } // DNS SERVER #2 else if(memcmppgm2ram(curHTTP.data,(ROM void*)"DNS2", 4) == 0) { memcpy(String_post,(void*)&curHTTP.data[5], len-5); WFSetParam(SECONDARY_DNS, String_post); } // SSID else if(memcmppgm2ram(curHTTP.data,(ROM void*)"SSID", 4) == 0) { memcpy(String_post,(void*)&curHTTP.data[5], len-5); WFSetParam(SSID_NAME, String_post); } // SECURITY TYPE else if(memcmppgm2ram(curHTTP.data,(ROM void*)"SECTYPE", 7) == 0) { memcpy(String_post,(void*)&curHTTP.data[8], len-8); if (String_post[2] == 'E') { security = 0; WFSetSecurity(WF_SECURITY_OPEN, "", 0, 0); ParamSet = TRUE; } else if (String_post[2] == 'A') { if (String_post[3] == '2') security = 5; else security = 3; } else if (String_post[2] == 'P') { if (String_post[3] == '4') security = 1; else security = 2; } } // ---------- SECURITY KEY AND PASSWORD ---------- // WEP40 KEY else if (memcmppgm2ram(curHTTP.data,(ROM void*)"WEP40KEY4", 9) == 0) { if (security == 1) { if (len > 10) { int j = 0, j1 = 0; WORD_VAL dummy; for ( j=0; j<40; j=j+2) { memcpy(String_post,(void*)&curHTTP.data[10+j], 2); dummy.v[1] = String_post[0]; dummy.v[0] = String_post[1]; PassKey[j1] = hexatob(dummy); j1++; } PassKey[j1]= '\0'; security = 1; } } } // WEP40 KEY INDEX else if (memcmppgm2ram(curHTTP.data,(ROM void*)"WEP40KEYID", 10) == 0) { memcpy(String_post,(void*)&curHTTP.data[11], len-11); int k_index; k_index = atoi(String_post); k_index--; if (security == 1) { WFSetSecurity(WF_SECURITY_WEP_40, PassKey, 20, k_index); ParamSet = TRUE; } } // WEP104 KEY INDEX else if (memcmppgm2ram(curHTTP.data,(ROM void*)"WEP104KEY", 9) == 0) { if (security == 2) { int j = 0, j1 = 0; WORD_VAL dummy; for ( j=0; j<32; j=j+2) { memcpy(String_post,(void*)&curHTTP.data[10+j], 2); dummy.v[1] = String_post[0]; dummy.v[0] = String_post[1]; PassKey[j1] = hexatob(dummy); j1++; } PassKey[j1]= '\0'; WFSetSecurity(WF_SECURITY_WEP_104, PassKey, 16, 0); ParamSet = TRUE; } } // WPA WITH PASSPHRASE else if (memcmppgm2ram(curHTTP.data,(ROM void*)"WPAPASS", 7) == 0) { if (security == 3) { if (len > 10) { memcpy(String_post,(void*)&curHTTP.data[8], len-8); WFSetSecurity(WF_SECURITY_WPA_WITH_PASS_PHRASE, String_post, len-9, 0); ParamSet = TRUE; } } } // WPA WITH PASSKEY else if (memcmppgm2ram(curHTTP.data,(ROM void*)"WPAKEY", 6) == 0) { if (security == 3) { if (len > 10) { int j = 0, j1 = 0; WORD_VAL dummy; for ( j=0; j<64; j=j+2) { memcpy(String_post,(void*)&curHTTP.data[7+j], 2); dummy.v[1] = String_post[0]; dummy.v[0] = String_post[1]; PassKey[j1] = hexatob(dummy); j1++; } PassKey[j1]= '\0'; WFSetSecurity(WF_SECURITY_WPA_WITH_KEY, PassKey, 32, 0); ParamSet = TRUE; } } } // WPA2 WITH PASSPHRASE else if (memcmppgm2ram(curHTTP.data,(ROM void*)"WPA2PASS", 8) == 0) { if (len > 10) { memcpy(String_post,(void*)&curHTTP.data[9], len-9); WFSetSecurity(WF_SECURITY_WPA2_WITH_PASS_PHRASE, String_post, len-9, 0); ParamSet = TRUE; } } // WPA2 WITH PASSKEY else if (memcmppgm2ram(curHTTP.data,(ROM void*)"WPA2KEY", 7) == 0) { if (len > 10) { int j = 0, j1 = 0; WORD_VAL dummy; for ( j=0; j<64; j=j+2) { memcpy(String_post,(void*)&curHTTP.data[8+j], 2); dummy.v[1] = String_post[0]; dummy.v[0] = String_post[1]; PassKey[j1] = hexatob(dummy); j1++; } PassKey[j1]= '\0'; WFSetSecurity(WF_SECURITY_WPA2_WITH_KEY, PassKey, 32, 0); ParamSet = TRUE; } } /* EMAIL */ else if (memcmppgm2ram(curHTTP.data,(ROM void*)"TXEMAIL", 7) == 0) { memcpy(MY_EMAIL,(void*)&curHTTP.data[8], len-8); } else if (memcmppgm2ram(curHTTP.data,(ROM void*)"USEREMAIL", 9) == 0) { memcpy(MY_EMAIL_USER,(void*)&curHTTP.data[10], len-10); } else if (memcmppgm2ram(curHTTP.data,(ROM void*)"PASSEMAIL", 9) == 0) { memcpy(MY_EMAIL_PASS,(void*)&curHTTP.data[10], len-10); } else if (memcmppgm2ram(curHTTP.data,(ROM void*)"SERVER", 6) == 0) { memcpy(MY_SMTP,(void*)&curHTTP.data[7], len-7); } else if (memcmppgm2ram(curHTTP.data,(ROM void*)"PORT", 4) == 0) { memcpy(MY_SMTP_PORT,(void*)&curHTTP.data[5], len-5); } else if (memcmppgm2ram(curHTTP.data,(ROM void*)"SUBJ", 4) == 0) { memcpy(EMAIL_SUBJECT,(void*)&curHTTP.data[5], len-5); } else if (memcmppgm2ram(curHTTP.data,(ROM void*)"TEXT", 4) == 0) { memcpy(EMAIL_BODY,(void*)&curHTTP.data[5], len-5); } else if (memcmppgm2ram(curHTTP.data,(ROM void*)"RXEMAIL", 7) == 0) { memcpy(EMAIL_DEST,(void*)&curHTTP.data[8], len-8); } /* ALARM */ else if (memcmppgm2ram(curHTTP.data,(ROM void*)"GMT", 3) == 0) { memcpy(GMT,(void*)&curHTTP.data[4], len-4); } else if (memcmppgm2ram(curHTTP.data,(ROM void*)"START", 5) == 0) { memcpy(start_string,(void*)&curHTTP.data[6], len-6); } else if (memcmppgm2ram(curHTTP.data,(ROM void*)"STOP", 4) == 0) { memcpy(stop_string,(void*)&curHTTP.data[5], len-5); } } return HTTP_IO_DONE; }