/*FUNCTION*------------------------------------------------ * * Function Name: shell_ppp_stop * Comments : Shell command "ppp stop" is calling this function to stop PPP connection. * *END*-----------------------------------------------------*/ int_32 shell_ppp_stop(SHELL_PPP_LINK_PTR local_ppp) { uint_32 error; /* Check if PPP is alive. */ if(!local_ppp->PPP_HANDLE) { printf("Can not stop PPP.It is not started\n"); } /* Cleanup PPP connection. */ /* Unbind interface. */ error = RTCS_if_unbind(local_ppp->PPP_IF_HANDLE, IPCP_get_local_addr(local_ppp->PPP_IF_HANDLE)); if(default_ppp.PPP_HANDLE) { /* Send connection terminate request to remote server and close PPP connection. */ error = PPP_close(local_ppp->PPP_HANDLE); /* Clean up all PPP structure. */ error = PPP_shutdown(local_ppp->PPP_HANDLE); local_ppp->PPP_HANDLE =NULL; } /* Destroing the semafor. */ error = _lwsem_destroy(&local_ppp->PPP_SEM); if(local_ppp->PPP_IF_HANDLE) { /* Unregister PPP interface. */ error = RTCS_if_remove(local_ppp->PPP_IF_HANDLE); local_ppp->PPP_IF_HANDLE=0; } if(local_ppp->PPP_IO_DRIVER_HANDLE) { /* Closing PPP driver. */ error = _iopcb_close(local_ppp->PPP_IO_DRIVER_HANDLE); } if(local_ppp->PPP_DEV_HANDLE) { /* Close PPP driver. */ error = fclose(default_ppp.PPP_DEV_HANDLE); } if(local_ppp->PPP_CON_DEV_HANDLE) { /* Close Close PPP_DEVICE. */ error = fclose(default_ppp.PPP_CON_DEV_HANDLE); } local_ppp->PPP_HANDLE = NULL; /* We need remove route. */ RTCS_gate_remove(local_ppp->PPP_GATE_ADDR, INADDR_ANY, INADDR_ANY); if(error) { printf("\nSome error happend during closing PPP connection.\n"); return RTCS_ERROR; } else { printf("PPP connection closed\n"); return RTCS_OK; } }
uint32_t PPP_release ( _ppp_handle handle /* [IN] - the PPP state structure */ ) { /* Body */ #if RTCSCFG_ENABLE_IP4 PPP_CFG_PTR ppp_ptr = handle; _rtcs_sem sem; uint32_t error = RTCS_OK; /* wait time in 0.1 Sec */ uint32_t wait_time = 50; /* delay time in mS */ uint32_t delay_time = 100; if (ppp_ptr == NULL) { return(RTCS_OK); } error = RTCS_if_unbind(ppp_ptr->IF_HANDLE, IPCP_get_local_addr(ppp_ptr->IF_HANDLE)); if (error != RTCS_OK) { return(error); } ppp_ptr->STOP_RX = TRUE; /* Waiting before Rx task will be closed or kill it. */ while(ppp_ptr->STOP_RX) { _time_delay(delay_time); wait_time--; if(!wait_time) { error = RTCSERR_TIMEOUT; RTCS_task_destroy(ppp_ptr->RX_TASKID); break; } } error = PPP_close(ppp_ptr); if (error != PPP_OK) { return(error); } /* Kill Tx task */ RTCS_sem_init(&sem); PPP_send_shutdown(ppp_ptr, &sem); RTCS_sem_wait(&sem); RTCS_sem_destroy(&sem); error = _iopcb_close(ppp_ptr->DEVICE); if (error != RTCS_OK) { return(error); } error = _iopcb_ppphdlc_release(ppp_ptr->DEVICE); if (error != RTCS_OK) { return(error); } ppp_ptr->DEVICE = NULL; error = RTCS_msgpool_destroy(ppp_ptr->MSG_POOL); if (error != MQX_OK) { return(error); } /* error = CCP_destroy(ppp_ptr); if (error != PPP_OK) { return(error); } */ error = LCP_destroy(ppp_ptr); if (error != PPP_OK) { return(error); } PPP_mutex_destroy(&ppp_ptr->MUTEX); if (ppp_ptr->IOPCB_DEVICE) { fflush(ppp_ptr->IOPCB_DEVICE); error = fclose(ppp_ptr->IOPCB_DEVICE); if (error != RTCS_OK) { return(error); } } error = RTCS_if_remove(ppp_ptr->IF_HANDLE); if (error != RTCS_OK) { return(error); } PPP_memfree(handle); return(error); #else return RTCSERR_IP_IS_DISABLED; #endif /* RTCSCFG_ENABLE_IP4 */ } /* Endbody */
/*FUNCTION*------------------------------------------------------------- * * Function Name : PPP_init_fail * Returned Value : none * Comments : * Do cleanup when initialization fails. * *END*-----------------------------------------------------------------*/ void PPP_init_fail(PPP_CFG_PTR ppp_ptr, int stage) { if (stage > 0) { fclose(ppp_ptr->IOPCB_DEVICE); _mem_free(ppp_ptr->DEVICE_NAME); ppp_ptr->DEVICE_NAME = NULL; } if (stage > 1) { _lwsem_destroy(&ppp_ptr->MUTEX); } if (stage > 2) { LCP_destroy(ppp_ptr); } if (stage > 3) { // CCP_close(ppp_ptr); // CCP_destroy(ppp_ptr); } if (stage > 5) { LWSEM_STRUCT sem; RTCS_sem_init(&sem); PPP_send_shutdown(ppp_ptr, &sem); RTCS_sem_wait(&sem); RTCS_sem_destroy(&sem); } /* Message pool must be destroyed after we send shutdown message to TX task */ if (stage > 4) { RTCS_msgpool_destroy(ppp_ptr->MSG_POOL); } if (stage > 6) { uint32_t wait_time = 50; uint32_t delay_time = 100; ppp_ptr->STOP_RX = TRUE; while(ppp_ptr->STOP_RX) { _time_delay(delay_time); wait_time--; if(!wait_time) { RTCS_task_destroy(ppp_ptr->RX_TASKID); break; } } } if (stage > 7) { _iopcb_close(ppp_ptr->DEVICE); } if (stage > 8) { RTCS_if_unbind(ppp_ptr->IF_HANDLE, IPCP_get_local_addr(ppp_ptr->IF_HANDLE)); RTCS_if_remove(ppp_ptr->IF_HANDLE); } _mem_free(ppp_ptr); }
/*FUNCTION*------------------------------------------------ * * Function Name: shell_ppp_start() * Comments : * This function start PPP communication and try to establish PPP connection. *END*-----------------------------------------------------*/ void shell_ppp_start(SHELL_PPP_LINK_PTR ppp_current) { uint_32 error; _ip_address local_address; _ip_address peer_address=0; IPCP_DATA_STRUCT ipcp_data; /* This is configuration for PAP. */ char* localname = ppp_current->LOCAL_NAME; char* localsecret = ppp_current->LOCAL_PASSWORD; PPP_SECRET lsecret; if(default_ppp.SERVER_FLAG==0) { /* Setup PAP for client mode.*/ lsecret.PPP_ID_LENGTH = strlen(localname); lsecret.PPP_PW_LENGTH = strlen(localsecret); lsecret.PPP_ID_PTR = localname; lsecret.PPP_PW_PTR = localsecret; _PPP_PAP_LSECRET = &lsecret; /* lsecrets */ _PPP_PAP_RSECRETS = NULL; /* rsecrets */ _PPP_CHAP_LNAME = NULL; /* localname */ _PPP_CHAP_LSECRETS = NULL; /* lsecrets */ _PPP_CHAP_RSECRETS = NULL; /* rsecrets */ } else if(default_ppp.SERVER_FLAG==1) { /* Setup PAP for server mode.*/ _PPP_PAP_LSECRET = NULL; /* lsecrets */ _PPP_PAP_RSECRETS = ppp_current->rsecrets; /* rsecrets */ _PPP_CHAP_LNAME = NULL; /* localname */ _PPP_CHAP_LSECRETS = NULL; /* lsecrets */ _PPP_CHAP_RSECRETS = NULL; /* rsecrets */ } else { ppp_current->PPP_HANDLE = NULL; return; } /* Install a route for a default gateway */ RTCS_gate_add(ppp_current->PPP_GATE_ADDR, INADDR_ANY, INADDR_ANY); ppp_current->PPP_IO_DRIVER_HANDLE = _iopcb_ppphdlc_init(ppp_current->PPP_DEV_HANDLE); _PPP_ACCM = 0; error = PPP_initialize(ppp_current->PPP_IO_DRIVER_HANDLE, &ppp_current->PPP_HANDLE); if (error) { printf("\n PPP initialize: %lx", error); _task_block(); } _iopcb_open(ppp_current->PPP_IO_DRIVER_HANDLE, PPP_lowerup, PPP_lowerdown,ppp_current->PPP_HANDLE); error = RTCS_if_add(ppp_current->PPP_HANDLE, RTCS_IF_PPP, &ppp_current->PPP_IF_HANDLE); if (error) { printf("\n IF add failed, error = %lx", error); _task_block(); } _lwsem_create(&ppp_current->PPP_SEM, 0); _mem_zero(&ipcp_data, sizeof(ipcp_data)); ipcp_data.IP_UP = PPP_linkup; ipcp_data.IP_DOWN = PPP_linkdown; ipcp_data.IP_PARAM = &ppp_current->PPP_SEM; if(default_ppp.SERVER_FLAG==0) { ipcp_data.ACCEPT_LOCAL_ADDR = TRUE; ipcp_data.ACCEPT_REMOTE_ADDR = TRUE; } else { ipcp_data.ACCEPT_LOCAL_ADDR = FALSE; ipcp_data.ACCEPT_REMOTE_ADDR = FALSE; } ipcp_data.LOCAL_ADDR = ppp_current->PPP_LOCAL_ADDRESS; ipcp_data.REMOTE_ADDR = ppp_current->PPP_REMOTE_ADDRESS; ipcp_data.DEFAULT_NETMASK = TRUE; ipcp_data.DEFAULT_ROUTE = TRUE; error = RTCS_if_bind_IPCP(ppp_current->PPP_IF_HANDLE, &ipcp_data); if (error) { printf("\n IF bind failed, error = %lx", error); _task_block(); } /*In case "client" we need wait for connection to server*/ if(default_ppp.SERVER_FLAG==0) { printf("\n Please wait initiate PPP connection. Waiting...\n"); /*Handshake with RAS server */ /* 10 attempts to connect */ error = 0; while (error < 10) { if (PPP_link == FALSE) { _time_delay(1000); } else { printf("\n PPP_link = %d, time = %d\n",PPP_link,error); break; } error ++; } if (PPP_link == TRUE) { local_address = IPCP_get_local_addr(ppp_current->PPP_IF_HANDLE); peer_address = IPCP_get_peer_addr(ppp_current->PPP_IF_HANDLE); printf("\n PPP device on %s is bound on.\n", ppp_current->PPP_CONNECTION_DEVICE ); printf(" PPP local address is : %d.%d.%d.%d\n", IPBYTES(local_address)); printf(" PPP remote address is : %d.%d.%d.%d\n", IPBYTES(peer_address)); printf(" Now PPP connection is established on %d.%d.%d.%d\n", IPBYTES(peer_address)); } else { if(ppp_current->PPP_HANDLE) { /* clean up all PPP structure */ error = PPP_shutdown(ppp_current->PPP_HANDLE); } ppp_current->PPP_HANDLE = NULL; /* We shoul remove route to clear part block. */ RTCS_gate_remove(ppp_current->PPP_GATE_ADDR, INADDR_ANY, INADDR_ANY); } } else { /* In case of "server" we do not need wait at all. */ printf("\n PPP server ready and waiting for connection.\n"); } }