void dhcpv6RelayTask(void *param) { error_t error; uint_t i; //Point to the DHCPv6 relay agent context Dhcpv6RelayCtx *context = (Dhcpv6RelayCtx *) param; //Specify the events the application is interested in for //each client-facing sockets for(i = 0; i < context->clientInterfaceCount; i++) { context->eventDesc[i].socket = context->clientSocket[i]; context->eventDesc[i].eventMask = SOCKET_EVENT_RX_READY; } //Specify the events the application is interested in for //the network-facing socket context->eventDesc[i].socket = context->serverSocket; context->eventDesc[i].eventMask = SOCKET_EVENT_RX_READY; //Main loop while(1) { //Wait for incoming packets on network-facing or client-facing interfaces error = socketPoll(context->eventDesc, context->clientInterfaceCount + 1, context->event, INFINITE_DELAY); //Stop DHCPv6 relay agent? if(context->stopRequest) { //The DHCPv6 relay agent is about to stop context->stopRequest = FALSE; context->running = FALSE; //Acknowledge the reception of the user request osEventSet(context->ackEvent); //Kill ourselves osTaskDelete(NULL); } //Verify status code if(!error) { //Check the state of each client-facing socket for(i = 0; i < context->clientInterfaceCount; i++) { //Relay client messages if applicable if(context->eventDesc[i].eventFlags & SOCKET_EVENT_RX_READY) dhcpv6ForwardClientMessage(context, i); } //Check the state of the network-facing socket if(context->eventDesc[i].eventFlags & SOCKET_EVENT_RX_READY) { //Forward Relay-Reply messages from the network dhcpv6ForwardRelayReplyMessage(context); } } } }
void tcpEchoConnectionTask(void *param) { error_t error; uint_t n; uint_t writeIndex; uint_t readIndex; uint_t bufferLength; uint_t rxByteCount; uint_t txByteCount; time_t startTime; time_t duration; SocketEventDesc eventDesc; EchoServiceContext *context; //Get a pointer to the context context = (EchoServiceContext *) param; //Get current time startTime = osGetTickCount(); //Initialize variables writeIndex = 0; readIndex = 0; bufferLength = 0; rxByteCount = 0; txByteCount = 0; //Main loop while(1) { //Buffer is empty? if(!bufferLength) { //Get notified when the socket is readable eventDesc.socket = context->socket; eventDesc.eventMask = SOCKET_EVENT_RX_READY; } //Buffer is not empty of full? else if(bufferLength < ECHO_BUFFER_SIZE) { //Get notified when the socket is readable or writable eventDesc.socket = context->socket; eventDesc.eventMask = SOCKET_EVENT_RX_READY | SOCKET_EVENT_TX_READY; } //Buffer is full? else { //Get notified when the socket is writable eventDesc.socket = context->socket; eventDesc.eventMask = SOCKET_EVENT_TX_READY; } //Wait for an event to be fired error = socketPoll(&eventDesc, 1, NULL, ECHO_TIMEOUT); //Timeout error or any other exception to report? if(error) break; //The socket is available for reading if(eventDesc.eventFlags & SOCKET_EVENT_RX_READY) { //Read as much data as possible n = min(ECHO_BUFFER_SIZE - writeIndex, ECHO_BUFFER_SIZE - bufferLength); //Read incoming data error = socketReceive(context->socket, context->buffer + writeIndex, n, &n, 0); //Any error to report? if(error) break; //Increment write index writeIndex += n; //Wrap around if necessary if(writeIndex >= ECHO_BUFFER_SIZE) writeIndex = 0; //Increment buffer length bufferLength += n; //Total number of bytes received rxByteCount += n; } //The socket is available for writing? if(eventDesc.eventFlags & SOCKET_EVENT_TX_READY) { //Write as much data as possible n = min(ECHO_BUFFER_SIZE - readIndex, bufferLength); //Send data back to the client error = socketSend(context->socket, context->buffer + readIndex, n, &n, 0); //Any error to report? if(error && error != ERROR_TIMEOUT) break; //Increment read index readIndex += n; //Wrap around if necessary if(readIndex >= ECHO_BUFFER_SIZE) readIndex = 0; //Update buffer length bufferLength -= n; //Total number of bytes sent txByteCount += n; } } //Adjust timeout value socketSetTimeout(context->socket, ECHO_TIMEOUT); //Graceful shutdown socketShutdown(context->socket, SOCKET_SD_BOTH); //Compute total duration duration = osGetTickCount() - startTime; //Debug message TRACE_INFO("Echo service: %u bytes received, %u bytes sent in %lu ms\r\n", rxByteCount, txByteCount, duration); //Close socket socketClose(context->socket); //Release previously allocated memory osMemFree(context); //Kill ourselves osTaskDelete(NULL); }
void tcpChargenConnectionTask(void *param) { error_t error; //size_t i; size_t n; //size_t offset; size_t byteCount; systime_t startTime; systime_t duration; ChargenServiceContext *context; //Get a pointer to the context context = (ChargenServiceContext *) param; //Get current time startTime = osGetTickCount(); //Initialize counters byteCount = 0; //offset = 0; //Once a connection is established a stream of data is sent out //the connection (and any data received is thrown away). This //continues until the calling user terminates the connection while(1) { //Format output data /*for(i = 0; i < CHARGEN_BUFFER_SIZE; i += 95) { //Calculate the length of the current line n = min(CHARGEN_BUFFER_SIZE - i, 95); //Copy character pattern memcpy(context->buffer + i, pattern + offset, n); } //Update offset offset += CHARGEN_BUFFER_SIZE + 95 - i; //Wrap around if necessary if(offset >= 95) offset = 0;*/ //Send data error = socketSend(context->socket, context->buffer, CHARGEN_BUFFER_SIZE, &n, 0); //Any error to report? if(error) break; //Total number of bytes sent byteCount += n; } //Graceful shutdown socketShutdown(context->socket, SOCKET_SD_BOTH); //Compute total duration duration = osGetTickCount() - startTime; //Avoid division by zero... if(!duration) duration = 1; //Debug message TRACE_INFO("Chargen service: %" PRIuSIZE " bytes " "sent in %" PRIu32 " ms (%" PRIu32 " kBps, %" PRIu32 " kbps)\r\n", byteCount, duration, byteCount / duration, (byteCount * 8) / duration); //Close socket socketClose(context->socket); //Release previously allocated memory osMemFree(context); //Kill ourselves osTaskDelete(NULL); }