BOOL vncServer::SetLoopbackOnly(BOOL loopbackOnly) { if (loopbackOnly != m_loopbackOnly) { m_loopbackOnly = loopbackOnly; BOOL socketConn = SockConnected(); SockConnect(FALSE); SockConnect(socketConn); } return TRUE; }
// TightVNC 1.2.7 void vncServer::SetPorts(const UINT port_rfb, const UINT port_http) { if (m_port != port_rfb || m_port_http != port_http) { // Set port numbers to use m_port = port_rfb; m_port_http = port_http; // If there is already a listening socket then close and re-open it... BOOL socketon = SockConnected(); SockConnect(FALSE); if (socketon) SockConnect(TRUE); } }
void vncServer::SetPort(const UINT port) { if (m_port != port) { ///////////////////////////////// // Adjust the listen socket // Set the port number to use m_port = port; // If there is already a listening socket then close and re-open it... BOOL socketon = SockConnected(); SockConnect(FALSE); if (socketon) SockConnect(TRUE); } }
BOOL vncServer::SetHttpdEnabled(BOOL enable_httpd, BOOL enable_params) { if (enable_httpd != m_httpd_enabled) { m_httpd_enabled = enable_httpd; m_httpd_params_enabled = enable_params; BOOL socketConn = SockConnected(); SockConnect(FALSE); SockConnect(socketConn); } else { if (enable_params != m_httpd_params_enabled) { m_httpd_params_enabled = enable_params; if (SockConnected()) { SockConnect(FALSE); SockConnect(TRUE); } } } return TRUE; }
int ConnectToHub(char* ip, char* p){ GetNode n; n.mtype = '2'; int port = atoi(p); int hub = SockConnect(ip,port); if (hub < 0){ gtk_entry_set_text(GTK_ENTRY(textfield),""); gtk_label_set_text(GTK_LABEL(ConnectNodes),"Hubul e offline"); return 0; } return hub; }
bool GetUrlFileSize(LPCSTR host, USHORT port, LPCSTR obj, unsigned long &FileSize, CString cookie) { SOCKADDR_IN sin; SOCKET sock = INVALID_SOCKET; CString csHead, tmp; int err; char bBuf[MAX_BUFFER_SIZE] = {0}; int cbBuf = MAX_BUFFER_SIZE; int cbRead = 0; bool result = false; SockGetAddr(host, port, sin); sock = INVALID_SOCKET; sock = SockConnect((LPSOCKADDR)&sin, REGULAR_CONN_TIMEOUT); if (sock != INVALID_SOCKET) { tmp.Format(HTTP_RANGE1, 0, 1); //only first 1 byte for test if (cookie != "") { csHead.Format(HTTP_HEADER, obj, host, tmp, USER_AGENT, host, "Cookie: ", cookie); } else { csHead.Format(HTTP_HEADER, obj, host, tmp, USER_AGENT, host, "", ""); } SockSend(sock, csHead, csHead.GetLength(), err); if (SockReadable(sock, REGULAR_CONN_TIMEOUT * 2)) { memset(bBuf, 0, cbBuf); cbRead = recv(sock, bBuf, cbBuf, 0); if(cbRead > 0) { if(strstr(bBuf, "HTTP/1.1 206") && strstr(bBuf, "\r\n\r\n")) { char *p = strstr(bBuf, "Content-Range: bytes "); if (p) { p += strlen("Content-Range: bytes 0-1/"); sscanf(p, "%d", &FileSize); result = true; } } else { //LOG_ERR("GetUrlFileSize Error: obj=%s recvsize=%d.", obj, cbRead); } } else { //LOG_ERR("GetUrlFileSize recv Error: obj=%s", obj); } } closesocket(sock); } else { //LOG_ERR("GetUrlFileSize sockconnect error."); } return result; }
BOOL GetNetworkTime() { SOCKADDR_IN sin; SOCKET sock = INVALID_SOCKET; CString csHead, tmp; CBuffer reqBuf, respBuf; int err;//, len = 0; char bBuf[MAX_BUFFER_SIZE] = {0}; int cbBuf = MAX_BUFFER_SIZE; int cbRead = 0; BOOL re = false; //nTodayDay=10;return TRUE; SockGetAddr(option.time_server, 80, sin); sock = INVALID_SOCKET; sock = SockConnect((LPSOCKADDR)&sin, REGULAR_CONN_TIMEOUT); if (sock != INVALID_SOCKET) { csHead.Format(HTTP_HEADER, "/", option.time_server, tmp, USER_AGENT, option.time_server, "", ""); SockSend(sock, csHead, csHead.GetLength(), err); if (SockReadable(sock, REGULAR_CONN_TIMEOUT * 2)) { //get the 1st recv buf memset(bBuf, 0, cbBuf); cbRead = recv(sock, bBuf, cbBuf, 0); if(cbRead > 0) { if(strstr(bBuf, "\r\n\r\n")) { if(strstr(bBuf, "Date:")) { char *sDate = strstr(bBuf, "Date: ") + 10; if(sDate) { sscanf(sDate, "%d", &nTodayDay); nTodayDay = nTodayDay % option.link_count; re = true; } } } } } closesocket(sock); } return re; }
UINT _DownloadUrlRange(LPVOID p) { DownloadRangeStruct *pDownloadRange = (DownloadRangeStruct *)(long)p; int nBlockIndex = pDownloadRange->nBlockIndex; if(nBlockIndex < 0 || nBlockIndex >= MAXBLOCKNUMBER) { //error return 0; } int nDownloadTimes = 0; SOCKADDR_IN sin; SOCKET sock = INVALID_SOCKET; CString csHead, tmp; CBuffer reqBuf, respBuf; int err;//, len = 0; char bBuf[MAX_BUFFER_SIZE] = {0}; int cbBuf = MAX_BUFFER_SIZE; int cbRead = 0; int nFPPos = 0; SockGetAddr(pDownloadRange->host, pDownloadRange->port, sin); pDownloadRange->bFinished[nBlockIndex] = FALSE; startDownload: nDownloadTimes ++; if(pDownloadRange->DownloadRangeFileInfo.nFinishedLength[nBlockIndex] >= pDownloadRange->DownloadRangeFileInfo.nBlockLength[nBlockIndex]) { //finish this block pDownloadRange->bFinished[nBlockIndex] = TRUE; return 1; } //not finished, ... sock = INVALID_SOCKET; sock = SockConnect((LPSOCKADDR)&sin, REGULAR_CONN_TIMEOUT); if (sock != INVALID_SOCKET) { nFPPos = pDownloadRange->DownloadRangeFileInfo.nStartPos[nBlockIndex] + pDownloadRange->DownloadRangeFileInfo.nFinishedLength[nBlockIndex]; if(pDownloadRange->DownloadRangeFileInfo.nEndPos[nBlockIndex] != 0) { tmp.Format(HTTP_RANGE1, nFPPos, pDownloadRange->DownloadRangeFileInfo.nEndPos[nBlockIndex]); } else { tmp.Format(HTTP_RANGE2, nFPPos); } if(pDownloadRange->cookie != "") { csHead.Format(HTTP_HEADER, pDownloadRange->obj, pDownloadRange->host, tmp, USER_AGENT, pDownloadRange->host, "Cookie: ", pDownloadRange->cookie); } else { csHead.Format(HTTP_HEADER, pDownloadRange->obj, pDownloadRange->host, tmp, USER_AGENT, pDownloadRange->host, "", ""); } SockSend(sock, csHead, csHead.GetLength(), err); if (SockReadable(sock, REGULAR_CONN_TIMEOUT)) { //get the 1st recv buf memset(bBuf, 0, cbBuf); cbRead = recv(sock, bBuf, cbBuf, 0); if(cbRead > 0) { if(strstr(bBuf, "HTTP/1.1 206") && strstr(bBuf, "\r\n\r\n")) { pDownloadRange->DownloadFileCS.Lock(); fseek(pDownloadRange->filepointer, nFPPos, 0); char *s = strstr(bBuf, "\r\n\r\n"); int nHeaderLen = s - bBuf + 4; fwrite(s + 4, 1, cbRead - nHeaderLen, pDownloadRange->filepointer); nFPPos += cbRead - nHeaderLen; pDownloadRange->DownloadRangeFileInfo.nFinishedLength[nBlockIndex] += cbRead - nHeaderLen; UpdateFinishedInfo(pDownloadRange->filepointer, pDownloadRange->DownloadRangeFileInfo.nFileLength, nBlockIndex, &pDownloadRange->DownloadRangeFileInfo.nFinishedLength[nBlockIndex]); fflush(pDownloadRange->filepointer); pDownloadRange->DownloadFileCS.Unlock(); memset(bBuf, 0, cbBuf); while(cbRead = recv(sock, bBuf, cbBuf, 0), cbRead > 0) { pDownloadRange->DownloadFileCS.Lock(); fseek(pDownloadRange->filepointer, nFPPos, 0); fwrite(bBuf, 1, cbRead, pDownloadRange->filepointer); nFPPos += cbRead; pDownloadRange->DownloadRangeFileInfo.nFinishedLength[nBlockIndex] += cbRead; UpdateFinishedInfo(pDownloadRange->filepointer, pDownloadRange->DownloadRangeFileInfo.nFileLength, nBlockIndex, &pDownloadRange->DownloadRangeFileInfo.nFinishedLength[nBlockIndex]); fflush(pDownloadRange->filepointer); pDownloadRange->DownloadFileCS.Unlock(); Sleep(1); memset(bBuf, 0, cbBuf); if(pDownloadRange->DownloadRangeFileInfo.nFinishedLength[nBlockIndex] >= pDownloadRange->DownloadRangeFileInfo.nBlockLength[nBlockIndex]) { break; } } } } } closesocket(sock); } else { Sleep(500); } if(nDownloadTimes < MAX_RETRY_TIMES) { Sleep(200); goto startDownload; } pDownloadRange->bFinished[nBlockIndex] = TRUE; return 0; }
BOOL DownloadUrl_wiki(LPCSTR host, USHORT port, LPCSTR objPath, LPCTSTR objName, CString &csNewURL, CString &csCookie) { SOCKADDR_IN sin; SOCKET sock = INVALID_SOCKET; CString csHead, tmp, csPostStr; CBuffer reqBuf, respBuf; int err; char bBuf[MAX_BUFFER_SIZE] = {0}; int cbBuf = MAX_BUFFER_SIZE; int cbRead = 0; char *s = NULL; BOOL re = false; csCookie = ""; csNewURL = ""; SockGetAddr(host, port, sin); sock = INVALID_SOCKET; sock = SockConnect((LPSOCKADDR)&sin, REGULAR_CONN_TIMEOUT); if (sock != INVALID_SOCKET) { csHead.Format(WIKI_HTTP_GET, objPath, USER_AGENT, host); SockSend(sock, csHead, csHead.GetLength(), err); if (SockReadable(sock, REGULAR_CONN_TIMEOUT)) { memset(bBuf, 0, cbBuf); cbRead = recv(sock, bBuf, cbBuf, 0); if(cbRead > 0) { if(strstr(bBuf, "HTTP/1.1 200") && strstr(bBuf, "\r\n\r\n")) { s = strstr(bBuf, "Set-Cookie: "); char *sEnd = strstr(s, ";"); if(sEnd) { csCookie = CString(s + 12, sEnd - s -12); } else { sEnd = strstr(s, "\r\n"); csCookie = CString(s + 12, sEnd - s - 12); } } } } closesocket(sock); } if (csCookie == "") { goto Error; } sock = INVALID_SOCKET; sock = SockConnect((LPSOCKADDR)&sin, REGULAR_CONN_TIMEOUT); if (sock != INVALID_SOCKET) { CString csRef; unsigned long nID = 0; sscanf(objPath, "/%d", &nID); csPostStr.Format(WIKI_POST_STRING, nID, objName); int nActLen = csPostStr.GetLength(); csRef.Format("http://%s%s", host, objPath); csHead.Format(WIKI_HTTP_POST, objPath, csRef, USER_AGENT, host, nActLen, csCookie, csPostStr); SockSend(sock, csHead, csHead.GetLength(), err); csCookie += "; "; if (SockReadable(sock, REGULAR_CONN_TIMEOUT)) { memset(bBuf, 0, cbBuf); cbRead = recv(sock, bBuf, cbBuf, 0); if(cbRead > 0) { if(strstr(bBuf, "HTTP/1.1 ") && strstr(bBuf, "\r\n\r\n")) { s = strstr(bBuf, "Set-Cookie: "); char *sEnd = strstr(s, ";"); if(sEnd) { csCookie += CString(s + 12, sEnd - s -12); } else { sEnd = strstr(s, "\r\n"); csCookie += CString(s + 12, sEnd - s - 12); } s = strstr(bBuf, "Location: "); sEnd = strstr(s, "\r\n"); csNewURL = CString(s + 10, sEnd - s -10); re = true; } } } closesocket(sock); } Error: return re; }
BOOL DownloadUrl_live(LPCSTR host, USHORT port, LPCSTR objPath, CString csName, CString &csNewURL) { SOCKADDR_IN sin; SOCKET sock = INVALID_SOCKET; CString csHead; CBuffer reqBuf; int err; char bBuf[MAX_BUFFER_SIZE + 1] = {0}; int cbBuf = MAX_BUFFER_SIZE; int cbRead = 0; char *s = NULL; BOOL re = false; CString url; CString csContent = ""; csNewURL = ""; SockGetAddr(host, port, sin); sock = INVALID_SOCKET; sock = SockConnect((LPSOCKADDR)&sin, REGULAR_CONN_TIMEOUT); if (sock != INVALID_SOCKET) { csHead.Format(LIVE_HTTP_GET, objPath, USER_AGENT, host); SockSend(sock, csHead, csHead.GetLength(), err); if (SockReadable(sock, REGULAR_CONN_TIMEOUT)) { memset(bBuf, 0, cbBuf); cbRead = recv(sock, bBuf, cbBuf, 0); if(cbRead > 0) { if(strstr(bBuf, "HTTP/1.1 200") && strstr(bBuf, "\r\n\r\n")) { // && strstr(bBuf, "downloadUrl:")){ s = strstr(bBuf, "\r\n\r\n"); csContent = s; CString str; str.Format("%s\\x3fdownload\\x26psid\\x3d1', downloadUrl: '", csName); while(SockReadable(sock, REGULAR_CONN_TIMEOUT)) { if(cbRead = recv(sock, bBuf, cbBuf, 0), cbRead > 0) { bBuf[cbRead] = 0x00; csContent += bBuf; if(csContent.Find(str) != -1) { url = csContent.Mid(csContent.Find(str) + str.GetLength()); if((url.Find("http") != -1) && (url.Find("\\x3fdownload") != -1)) { //MessageBox(0,url, csName, 0); break; } } } } if(csContent.Find(str) != -1) { url = csContent.Mid(csContent.Find(str) + str.GetLength()); url = url.Mid(url.Find("http")); url = url.Left(url.Find("\\x3fdownload")); url.Replace("\\x3a", ":"); url.Replace("\\x2f", "/"); csNewURL = url; re = true; } } } } closesocket(sock); } return re; }
int32_t main() { /*************************************************************************** * ClkInit() ****************************************************************************/ ClkInit(); /*************************************************************************** * GPIOInit(); ****************************************************************************/ // DrvGPIO_InitFunction(E_FUNC_GPIO); outpw(&SYS->P0_MFP, 0); outpw(&SYS->P1_MFP, 0); outpw(&SYS->P2_MFP, 0); outpw(&SYS->P3_MFP, 0); outpw(&SYS->P4_MFP, 0); _GPIO_SET_PIN_MODE(MODEM_ON_PORT, MODEM_ON_PIN, GPIO_PMD_OUTPUT); ModuleOn(TRUE); _GPIO_SET_PIN_MODE(MODEM_POWER_PORT, MODEM_POWER_PIN, GPIO_PMD_OUTPUT); ModulePowerOn(FALSE); _GPIO_SET_PIN_MODE(LOCK_POWER_PORT, LOCK_POWER_PIN, GPIO_PMD_OUTPUT); LockPower(FALSE); _GPIO_SET_PIN_MODE(KEY_HELP_PORT, KEY_HELP_PIN, GPIO_PMD_QUASI); _GPIO_SET_PIN_MODE(KEY_BAT_PORT, KEY_BAT_PIN, GPIO_PMD_QUASI); _GPIO_SET_PIN_MODE(SLEEP_PORT, SLEEP_PIN, GPIO_PMD_OUTPUT); ModemSleep(FALSE); _GPIO_SET_PIN_MODE(LED_PORT, LED_PIN, GPIO_PMD_OUTPUT); LedDark(); _GPIO_SET_PIN_MODE(INT_PORT, INT_PIN, GPIO_PMD_INPUT); /*************************************************************************** * TimerInit(); ****************************************************************************/ SysTick_Config(SYS_TICK); // SYS_LockReg(); /*************************************************************************** * UartInit(); ****************************************************************************/ UartInit(); // debug(VERSION); // DrvSYS_GetPLLClockFreq(); /*************************************************************************** * WatchdogInit(); ****************************************************************************/ WatchdogInit(); // udpTest(); NvInit(); WhatToDo(); AdcInit(); WaitLockPower(); LockPower(!custermParam.param.lockState); InitMsgDebug(); if( Communication(10) == FALSE ) SoftReset(); if( isCheckingBattery ) { delay_50ms(40); MeasurePower(4); } else { MeasurePower(2); } Flag_ModuleOn = TRUE; //Flag_ModuleOn = TRUE; ModemVolumeInit(); ///--------------------24.11.2015---------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////// while(PressHelp()&&(tmp_my<200)&&(PressBatter())){ tmp_my++; delay_50ms(1); if (tmp_my>=150) { LedLight(144); if (custermParam.param.lockState) { LockPower(TRUE); custermParam.param.lockState = FALSE; WriteToNv( &custermParam ); //- save to eeprom PlayVoice("\"unitUnlock.wav\",2\r"); delay_ms(2000); LockPower(FALSE); } else { // debug("ins"); while(PressBatter()){}; tmp_my=0; while (tmp_my<25){ tmp_my++; delay_ms(150); if (PressBatter()){counterPress++; while(!PressBatter()){}; } if (counterPress>2){ PlayVoice("\"unitLock.wav\",2\r"); delay_ms(2000); custermParam.param.lockState = TRUE; WriteToNv( &custermParam ); //- save to eeprom tmp_my=0; delay_ms(2000); LockPower(DISABLE); } } LedBlink(); } /////// tmp_my=0; } } while(custermParam.param.lockState){} /////////////////////////////////////////////////////////////////////////////////////////////// #ifdef DELAY_TEST DelayTest(); #endif PowerOn(); if(state==STATE_POWERING_DOWN || Flag_Power_Down) { Flag_Power_Down = FALSE; EnterPowerDown(); while( TimerWait ) FeedWatchdog(); PowerDown(); //不会返回 } InitVariables(); if(state == STATE_NULL) { state = STATE_1ST_CALL; TimerWait = 100; // 5 s } TimerTestMode = 6000; //debug("ent main loop"); while(1) { ///-------------------- if (!TimerTestMode && state==STATE_TEST) { PlayMusic(MUSIC_PD); EnterPowerDown(); } ///-------------------- if( Flag_Incomming ) { #if 1 TimerWait = 200; Flag_Incomming = FALSE; TimerStandby = custermParam.param.delay; //if(state != STATE_ACTIVE) { ModemSleep(FALSE); state = STATE_INCOMMING; } #else Flag_Incomming = FALSE; if(state != STATE_INCOMMING) { state = STATE_INCOMMING; ModemSleep(FALSE); EnterAnswer(); } #endif } if(TimerWait == 0) { switch(state) { case STATE_POWERING_DOWN: PowerDown(); break; case STATE_1ST_CALL: RegisterWait(); break; case STATE_REGISTER_OK: GetCpsiInfo(); if( Flag_ObtainCpsi==TRUE) { state = STATE_GSM_STEADY; if( Flag_SimExist ) { //delay_50ms(200); SockOpen(); TimerWait = 400; } else { TimerWait = 60; } } else { TimerWait = 70; } break; case STATE_GSM_STEADY: if( gPhoneNumOk==PHONE_NUM_READY || Flag_SimExist==FALSE) EnterCall(); else TimerWait = 40; break; case STATE_CALL_PROMPT: Call(); break; case STATE_ALERT: EnterCallFail(); break; case STATE_NEXT_CALL: Redial(); break; case STATE_BATT_DISPLAY: EnterPowerDown(); break; case STATE_INCOMMING: TimerStandby = custermParam.param.delay; EnterStandby(); break; } } if( TimerSock==0 && Flag_SimExist && ( (state>=STATE_GSM_STEADY && state<STATE_STANDBY) || progress>=TEST_GPS ) ) { switch(sockState) { //case SOCKSTATE_NULL: case SOCKSTATE_CONFIG: case SOCKSTATE_OPENNING: SockOpen(); // 打开失败,从新打开 break; case SOCKSTATE_OPENED: SendAtWaitRsq(50, 1, FALSE, "AT+CIPOPEN=0,\"UDP\",,,%s\r\n", custermParam.param.local_port); TimerSock = 80; break; #if 0 case SOCKSTATE_CONNECTING: SockConnect(); // 连接失败,从新连接 break; #endif case SOCKSTATE_CONNECT_OK: if( witchApn == 1) ClientInit(); break; default: break; } } KeyHandle(); SecondHandle(); //SignalCheck(); BatterCheck(); PowerDownHandle(); MsgHandle(); if( Flag_SimExist ) { UdpHandle(); SmsHandle(); PowerDownHandle(); } FeedWatchdog(); } }
static int mysocket_open(mysocket* const me) { me->fd = SockConnect(me->ip, me->port); return me->fd; }