/** * @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); }
//processing http protocol , and excuting the followed fuction. void WebServer(uint8 s) { int ret; uint32 header_len=0, content_len=0, received_len=0; char sub[10]; /* http service start */ ret = TCPRecv(s, (int8*)rx_buf, MAX_URI_SIZE); if(ret > 0){ // If Received.. *(((uint8*)rx_buf)+ret) = '\0'; if(strstr(rx_buf, "Content-Length: ")){ mid((char*)rx_buf, "Content-Length: ", "\r\n", sub); content_len=atoi(sub); header_len = (uint32)(strstr(rx_buf, "\r\n\r\n") - rx_buf + 4); received_len = ret; while(received_len!=(content_len+header_len)) { ret = TCPRecv(s, (int8*)rx_buf+received_len, MAX_URI_SIZE); received_len+=ret; } *(((uint8*)rx_buf)+received_len) = '\0'; } HTTPProcessor(s, (char*)rx_buf); // request is processed memset(rx_buf,0x00,MAX_URI_SIZE); TCPDisconnect(s); } else if(ret == SOCKERR_NOT_TCP){ // Not TCP Socket, It's UDP Socket DBG("UDP Socket Close"); UDPClose(s); } else if(ret == SOCKERR_CLOSED){ // Socket Closed LOGA("HTTP Server Started - ch(%d)",(uint16)s); TCPServerOpen(s, DEFAULT_HTTP_PORT); } if(GetTCPSocketStatus(s) == SOCKERR_CLOSE_WAIT){// Close waiting TCPClose(s); } }
/** * @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; } }
/** * @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 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); } }
void act_nrecv(int8 sock, uint16 maxlen) { uint8 dstip[4], i; uint16 dstport; int32 len; DBGA("Asock(%d)", sock); if(sock == VAL_NONE) { if(recvnum == 0) CMD_RESP_RET(RET_NO_DATA, VAL_NONE); for(i=ATC_SOCK_NUM_START; i<=ATC_SOCK_NUM_END; i++) { if(recvord[i] == recvnum) { sock = i; break; } } }DBGA("Bsock(%d)", sock); if(sockstat[sock] == SOCK_STAT_IDLE) CMD_RESP_RET(RET_SOCK_CLS, VAL_NONE); if(sockstat[sock] & SOCK_STAT_TCP_MASK) { // TCP if(!(sockstat[sock] & SOCK_STAT_CONNECTED)) CMD_RESP_RET(RET_NOT_CONN, VAL_NONE); if(GetSocketRxRecvBufferSize(sock) == 0) CMD_RESP_RET(RET_NO_DATA, VAL_NONE); len = TCPRecv(sock, atci.recvbuf, maxlen); } else { // UDP if(GetSocketRxRecvBufferSize(sock) == 0) CMD_RESP_RET(RET_NO_DATA, VAL_NONE); len = UDPRecv(sock, atci.recvbuf, maxlen, dstip, &dstport); if(len == 0) CMD_RESP_RET(RET_NO_DATA, VAL_NONE); else if(len == SOCKERR_CLOSED) CMD_RESP_RET(RET_SOCK_CLS, VAL_NONE); else if(len < 0) CMD_RESP_RET(RET_UNSPECIFIED, VAL_NONE); } atci.recvbuf[len] = 0; MAKE_TCMD_DIGIT(atci.tcmd.arg1, len); MAKE_TCMD_ADDR(atci.tcmd.arg2, dstip[0], dstip[1], dstip[2], dstip[3]); MAKE_TCMD_DIGIT(atci.tcmd.arg3, dstport); cmd_resp(RET_RECV, sock); printf("%s\r\n", atci.recvbuf); sockwatch_set(sock, WATCH_SOCK_RECV); }
void ftp_client_PI (uint8 sock) { int ret; int idx; char msg[STR_SIZE]; uint16 status; static uint16 any_port = 50000; memset(cmd_buf, 0, sizeof(cmd_buf)); ret = TCPRecv(sock, cmd_buf, TX_RX_MAX_BUF_SIZE); if(ret > 0) // Received { for( idx=0; idx < ret; idx++ ) { if( idx == 0 ) printf("\r\n"); printf("%c",cmd_buf[idx]); } switch( status = Parse_FTPServer_Statuscode(cmd_buf) ) { case R_220: // Service ready for new user. printf("\r\nInput your User ID > "); GetInputString(msg); Send_USER_ID(sock,msg); break; case R_331: // User name okay, need password. printf("\r\nInput your Password > "); GetInputString(msg); Send_PW(sock,msg); break; case R_230: // User logged in, proceed printf("\r\nUser logged in, proceed"); TCPClose(FTP_CLIENT_DTP_SOCK); Send_Port(sock, IP, DTP_Port); break; case R_200: // 'Generic' command ok printf("\r\nInput FTP CMD > "); GetInputString(msg); Send_cmd(sock, msg); break; case R_150: // File Status ok: opening data conn bRunDTP = 1; break; case R_250: TCPClose(FTP_CLIENT_DTP_SOCK); Send_Port(sock, IP, DTP_Port); break; case R_226: /* Closing data connection. File transfer/abort successful */ TCPClose(FTP_CLIENT_DTP_SOCK); Send_Port(sock, IP, DTP_Port); break; case R_425: TCPClose(FTP_CLIENT_DTP_SOCK); Send_Port(sock, IP, DTP_Port); break; default: printf("\r\nDefault Status = %d",(uint16)status); JumpAddress = *(volatile unsigned long*) (ApplicationAddress + 4); // reset app Jump_To_Application = (pFunction) JumpAddress; Jump_To_Application(); while(1); } } else if(ret == ERROR_CLOSED || ret == ERROR_CLOSE_WAIT ) { printf("\r\n%d : FTP Client Start.\r\n",(u16)sock); if( TCPClientOpen( sock, any_port++, FTP_SERVER_IP, FTP_Server_CMD_Port ) == 0 ) printf("< Fail : Socket Connect >\r\n"); } }
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; } }