void Send_cmd(uint8 sock, char* msg) { uint16 len; char new_msg[STR_SIZE]; char dat[STR_SIZE]; if( strstr(msg,"ls") != NULL ) { TCPSend(sock,ls,strlen(ls)); } else if( strstr(msg,"get") != NULL ) { memset(dat, 0, sizeof(dat)); memset(new_msg, 0, sizeof(new_msg)); memcpy(new_msg, msg+4, strlen(msg+4)); len = sprintf(dat, "RETR %s\r\n",new_msg); TCPSend(sock, (uint8*)dat, len); } else if( strstr(msg, "put") != NULL ) { memset(dat, 0, sizeof(dat)); memset(new_msg, 0, sizeof(new_msg)); memcpy(new_msg, msg+4, strlen(msg+4)); len = sprintf(dat, "STOR %s\r\n",new_msg); TCPSend(sock, (uint8*)dat, len); bRunPutFile = 1; } else if( strstr(msg, "cd") != NULL ) { memset(dat, 0, sizeof(dat)); memset(new_msg, 0, sizeof(new_msg)); memcpy(new_msg, msg+3, strlen(msg+3)); len = sprintf(dat, "CWD %s\r\n", new_msg); TCPSend(sock, (uint8*)dat, len); } else if( strstr(msg, "bye") != NULL ) { TCPSend(sock, bye, strlen(bye) ); TCPClose(FTP_CLIENT_DTP_SOCK); TCPClose(FTP_CLIENT_PI_SOCK); JumpAddress = *(volatile unsigned long*) (ApplicationAddress + 4); Jump_To_Application = (pFunction) JumpAddress; Jump_To_Application(); while(1); } else { printf("\r\n Sorry, this FTP client only support ls, put and get commands!"); TCPClose(FTP_CLIENT_DTP_SOCK); Send_Port(sock, IP, DTP_Port); } }
/** * @brief This function processes the delete port to IGD(Internet Gateway Device). * @return 0: success, -2: Invalid UPnP Step, -1: reply packet timeout, 1: received xml parse error, other: UPnP error code */ signed short DeletePortProcess( SOCKET sockfd, /**< a socket number. */ const char* protocol, /**< a procotol name. "TCP" or "UDP" */ const unsigned int extertnal_port /**< an external port number. */ ) { short len=0; long endTime=0; uint32 ipaddr; uint16 port; // Check UPnP Step if(UPnP_Step < 2) return -2; // Make "Delete Port" XML(SOAP) memset(content, '\0', MAX_BUFFER); MakeSOAPDeleteControl(content, protocol, extertnal_port); // Make HTTP POST Header memset(send_buffer, '\0', MAX_BUFFER); len = strlen(content); MakePOSTHeader(send_buffer, len, DELETE_PORT); strcat(send_buffer, content); #ifdef UPNP_DEBUG printf("%s\r\n", send_buffer); #endif ipaddr = inet_addr((uint8*)descIP); ipaddr = swapl(ipaddr); port = ATOI(descPORT, 10); // Connect to IGD(Internet Gateway Device) if(TCPClientOpen(sockfd, PORT_UPNP, (uint8*)&ipaddr, port) == FAIL) printf("TCP Socket Error!!\r\n"); // Send "Delete Port" Message while(GetTCPSocketStatus(sockfd) != STATUS_ESTABLISHED); TCPSend(sockfd, (void *)send_buffer, strlen(send_buffer)); // Receive Reply memset(recv_buffer, '\0', MAX_BUFFER); Delay_ms(500); endTime = my_time + 3; while (TCPRecv(sockfd, (void *)recv_buffer, MAX_BUFFER) <= 0 && my_time < endTime); // Check Receive Buffer of W5200 if(my_time >= endTime){ // Check Timeout TCPClose(sockfd); return -1; } // TCP Socket Close TCPClose(sockfd); #ifdef UPNP_DEBUG printf("\r\nReceiveData\r\n%s\r\n", recv_buffer); #endif // Parse Replied Message return parseDeletePort(recv_buffer); }
void Send_Port(uint8 sock, uint8* ip, u32 port) { char dat[STR_SIZE]; uint8 h_val, l_val; h_val = port/256; l_val = port%256; sprintf(dat,"PORT %d,%d,%d,%d,%d,%d\r\n",ip[0],ip[1],ip[2],ip[3],h_val,l_val); TCPSend(sock,(uint8*)dat,strlen(dat)); }
/** * @brief This function gets the description message from IGD(Internet Gateway Device). * @return 0: success, -2: Invalid UPnP Step, -1: reply packet timeout, 1: received xml parse error */ signed char GetDescriptionProcess( SOCKET sockfd /**< a socket number. */ ) { char ret_value=0; long endTime=0; uint32 ipaddr; uint16 port; // Check UPnP Step if(UPnP_Step < 1) return -2; // Make HTTP GET Header memset(send_buffer, '\0', MAX_BUFFER); MakeGETHeader(send_buffer); #ifdef UPNP_DEBUG printf("%s\r\n", send_buffer); #endif ipaddr = inet_addr((uint8*)descIP); ipaddr = swapl(ipaddr); port = ATOI(descPORT, 10); // Connect to IGD(Internet Gateway Device) if(TCPClientOpen(sockfd, PORT_UPNP, (uint8*)&ipaddr, port) == FAIL) printf("TCP Socket Error!!\r\n"); // Send Get Discription Message while(GetTCPSocketStatus(sockfd) != STATUS_ESTABLISHED); TCPSend(sockfd, (void *)send_buffer, strlen(send_buffer)); // Receive Reply memset(recv_buffer, '\0', MAX_BUFFER); Delay_ms(500); endTime = my_time + 3; while (TCPRecv(sockfd, (void *)recv_buffer, MAX_BUFFER) <= 0 && my_time < endTime); // Check Receive Buffer of W5200 if(my_time >= endTime){ // Check Timeout TCPClose(sockfd); return -1; } // TCP Socket Close TCPClose(sockfd); #ifdef UPNP_DEBUG printf("\r\nReceiveData\r\n%s\r\n", recv_buffer); #endif // Parse Discription Message if((ret_value = parseDescription(recv_buffer)) == 0) UPnP_Step = 2; return ret_value; }
void HTTPProcessor(uint8 s, char * buf) { uint8* http_response; st_http_request *http_request; http_response = (uint8*)rx_buf; http_request = (st_http_request*)tx_buf; memset(tx_buf,0x00,MAX_URI_SIZE); parse_http_request(http_request, buf); // After analyze request, convert into http_request memset(rx_buf,0x00,MAX_URI_SIZE); //method Analyze switch (http_request->METHOD) { case METHOD_ERR : memcpy(http_response, ERROR_REQUEST_PAGE, sizeof(ERROR_REQUEST_PAGE)); TCPSend(s, (int8 *)http_response, strlen((char*)http_response)); break; case METHOD_HEAD: case METHOD_GET: case METHOD_POST: if (!strcmp((char*)http_request->URI, "/")) strcpy(http_request->URI, (char const*)homepage_default); // If URI is "/", respond by index.htm RESTProcessor(http_request); //get http type from type find_http_uri_type(&http_request->TYPE, http_request->URI); //Check file type (HTML, TEXT, GIF, JPEG are included) if(http_request->TYPE == PTYPE_PL || http_request->TYPE == PTYPE_CGI) { CGIProcessor(http_request, (char*)http_response); } FILESend(s, http_request, (char*)http_response); break; default: break; } }
void loopback_tcps(uint8 ch, uint16 port) { int ret; int SendLen, ReSendLen; ret = TCPRecv(ch, data_buf, TX_RX_MAX_BUF_SIZE); if(ret > 0) { // Received SendLen = TCPSend(ch, data_buf, ret); if(SendLen < ret) { while(SendLen != ret) { ReSendLen = TCPReSend(ch); if(ReSendLen > 0) { SendLen += ReSendLen; } else if(ReSendLen == ERROR_WINDOW_FULL) { LOG("Window Full"); TCPClose(ch); DBG("TCP Socket Close"); while(1); } else { break; } } } } else if(ret == ERROR_NOT_TCP_SOCKET) { // Not TCP Socket, It's UDP Socket DBG("UDP Socket Close"); UDPClose(ch); } else if(ret == ERROR_CLOSED) { // Socket Closed LOGA("TCP Loop-Back Started - ch(%d)",(uint16)ch); TCPServerOpen(ch, port); } if(GetTCPSocketStatus(ch) == STATUS_CLOSE_WAIT) { // Close waiting TCPClose(ch); } }
/** * @brief This function listenes the eventing message from IGD(Internet Gateway Device). */ void eventing_listener( SOCKET sockfd /**< a socket number. */ ) { uint16 len; const uint8 HTTP_OK[] = "HTTP/1.1 200 OK\r\n\r\n"; switch (getSn_SR(sockfd)) { case SOCK_ESTABLISHED: /* if connection is established */ Delay_ms(500); if((len = getSn_RX_RSR(sockfd)) > 0){ TCPRecv(sockfd, (void *)recv_buffer, len); TCPSend(sockfd, (void *)HTTP_OK, strlen((void *)HTTP_OK)); parseEventing(recv_buffer); #ifdef UPNP_DEBUG printf("\r\nReceiveData\r\n%s\r\n", recv_buffer); #endif } break; case SOCK_CLOSE_WAIT: /* If the client request to close */ if ((len = getSn_RX_RSR(sockfd)) > 0) /* check Rx data */ { TCPRecv(sockfd, (void *)recv_buffer, len); /* read the received data */ } TCPClose(sockfd); break; case SOCK_CLOSED: /* if a socket is closed */ if(TCPServerOpen(sockfd,PORT_UPNP_EVENTING) == FAIL) /* reinitialize the socket */ { printf("\r\n%d : Fail to create socket.",sockfd); } break; default: break; } }
void Send_PW(uint8 sock,char* msg) { char dat[STR_SIZE]; sprintf(dat,"PASS %s\r\n",msg); TCPSend(sock,(uint8*)dat,strlen(dat)); }
void Send_USER_ID(uint8 sock,char* msg) { char dat[STR_SIZE]; sprintf(dat,"USER %s\r\n",msg); TCPSend(sock,(uint8*)dat,strlen(dat)); }
void ftp_client_DTP(uint8 sock) { int len; int idx; switch( GetTCPSocketStatus(sock) ) { case STATUS_ESTABLISHED: if(bRunDTP) { bRunDTP = 0; #ifdef Debug printf("< FTP_DTP Connect OK>\r\n"); #endif } len = TCPRecv(sock, data_buf, TX_RX_MAX_BUF_SIZE); if( len > 0 ) { for( idx = 0; idx < len; idx++ ) { if( idx == 0 ) printf("\r\n"); printf("%c",data_buf[idx]); } } if( bRunPutFile ) { bRunPutFile = 0; TCPSend(sock, (void *)PUTFILE, strlen(PUTFILE)); TCPClose(sock); } break; case STATUS_CLOSE_WAIT: len = TCPRecv(sock, data_buf, TX_RX_MAX_BUF_SIZE); if( len > 0 ) { for( idx = 0; idx < len; idx++ ) { if( idx == 0 ) printf("\r\n"); printf("%c",data_buf[idx]); } } if( bRunPutFile ) { bRunPutFile = 0; TCPSend(sock, (void *)PUTFILE, strlen(PUTFILE)); } TCPClose(sock); break; case STATUS_CLOSED: TCPClose(sock); if( TCPServerOpen( sock, DTP_Port++ ) == 0 ) { printf("\r\nDTP Socket Open Error"); break; } if( DTP_Port > 70000 ) DTP_Port = FTP_Client_TCPS_Port; bRunDTP = 1; break; } }
/** send data via tcp connection */ long Network::TCPServer::TCPSend(char * ucBufferSend, int iLenBufferSend) { return TCPSend(m_iSession_id, ucBufferSend, iLenBufferSend); }
void FILESend(uint8 s, st_http_request *http_request, char* buf) { uint32 file_len=0, file_len_tmp=0; uint32 send_len=0, content_len=0; uint32 content = 0; char* name=(char*)http_request->URI; if(strcmp(name,"/")) name++; /* Search the specified file in stored binaray html image */ if(!search_file_rom((unsigned char *)name, &content, &file_len)) { memcpy(buf, ERROR_HTML_PAGE, sizeof(ERROR_HTML_PAGE)); TCPSend(s, (int8*)buf, strlen((char const*)buf)); } else { file_len_tmp = file_len; send_len=0; while(file_len_tmp) { if(file_len_tmp>1024) { read_from_flashbuf(content+send_len, (uint8*)buf, 1024); // Replace html's system environment value to real value and check size content_len += HTTPSend(NULL, buf, NULL, 1024, 2); send_len+=1024; file_len_tmp-=1024; } else { read_from_flashbuf(content+send_len, (uint8*)buf, file_len_tmp); buf[file_len_tmp] = '\0'; // Replace html's system environment value to real value and check size content_len += HTTPSend(NULL, buf, NULL, file_len_tmp, 2); send_len+=file_len_tmp; file_len_tmp-=file_len_tmp; } } make_http_response_head((unsigned char*)buf, http_request->TYPE, content_len); TCPSend(s, (int8*)buf, strlen((char const*)buf)); send_len=0; while(file_len) { if(file_len>1024) { read_from_flashbuf(content+send_len, (uint8*)buf, 1024); // Replace html's system environment value to real value and send if(HTTPSend(s, buf, NULL, 1024, 0)<0) { return; } send_len+=1024; file_len-=1024; } else { read_from_flashbuf(content+send_len, (uint8*)buf, file_len); buf[file_len] = '\0'; // Replace html's system environment value to real value and send HTTPSend(s, buf, NULL, (uint16)file_len, 0); send_len+=file_len; file_len-=file_len; } } } }
int32 HTTPSend(uint8 s, char *src, char *dest, uint16 len, uint8 mode) { int32 ret=0; char *oldtmp=0, *newtmp=0; char sub[32]; uint16 i, mlen=0; char *tmp = (char*)BUFPUB; oldtmp=src; newtmp=oldtmp; while((newtmp = strstr(oldtmp, "<="))){ if(mode == 0) ret += TCPSend(s, (int8*)oldtmp, (uint16)(newtmp-oldtmp)); else if(mode == 1) { strncat(dest, oldtmp, (uint16)(newtmp-oldtmp)); ret += (uint16)(newtmp-oldtmp); } else if(mode == 2) ret += (uint16)(newtmp-oldtmp); mid(newtmp, "<=", ">", sub);// mid 함수의 리턴값에 따라 "<=" 까지만 읽었고, ">" 까지 읽지 않았는지 판단한 후 다음 파일을 읽고나서 처리 하도록 구현해야 함. for(i=0; i<MAX_CGI_CALLBACK; i++){ if(!strcmp(sub, cgi_callback[i].tokken)){ if(cgi_callback[i].get_func == NULL){ i = MAX_CGI_CALLBACK; break; } cgi_callback[i].get_func(tmp, &mlen); if(mode == 0) ret += TCPSend(s, (int8*)tmp, mlen); else if(mode == 1) { strncat(dest, tmp, mlen); ret += mlen; } else if(mode == 2) ret += mlen; break; } } if(i==MAX_CGI_CALLBACK){ mlen = sprintf(tmp, "<=%s>", sub); if(mode == 0) ret += TCPSend(s, (int8*)tmp, mlen); else if(mode == 1) { strncat(dest, tmp, mlen); ret += mlen; } else if(mode == 2) ret += mlen; } oldtmp = newtmp + strlen(sub)+3; } if(mode == 0) ret += TCPSend(s, (int8*)oldtmp, (len-(uint16)(oldtmp-src))); else if(mode == 1) { strncat(dest, oldtmp, (len-(uint16)(oldtmp-src))); ret += (len-(uint16)(oldtmp-src)); } else if(mode == 2) ret += (len-(uint16)(oldtmp-src)); return ret; }