Network Component
Version 6.6
MDK-Professional Middleware for IP Networking
|
To enable a user to have access to a remote device, a command line interface needs to be in place on that device.
The Network Component's Telnet Server calls the telnet_server_process function when a command is received from the user. A command is any sequence of characters that is terminated by the CRLF sequence (the Enter key is pressed). The Telnet Server assembles this command and passes it as an argument to the telnet_server_process function.
The command line interface functions are located in the Telnet_Server_UIF.c template file. You must add this file to your project and customize it. You can add new commands or remove existing commands from the file. To add the template file to your project, simply right-click on the Source group, select Add New Item to Group, then click on User Code Template and scroll in the template files list until you find the Telnet Server template.
The following functions are implemented in this template file:
The following functions are included in the Network Component library rl_net.h:
Code Example Telnet_Server_UIF.c
For access filtering the function telnet_accept_client is used. It is part of the file Telnet_Server_Access.c. You need to adapt the function to the application's needs.
Code Example Telnet_Server_Access.c
The multi-user login allows you to create different profiles for different users or groups of users. The profiles define the access rights on the Telnet server. The users which are allowed to access the Telnet server are in the user database. The multi-user login allows you to selectively disable commands for unprivileged users.
If you want to use multi-user authentication, you must Enable User Authentication in the Net_Config_Telnet_Server.h configuration file.
The account defined in the Net_Config_Telnet_Server.h configuration file is a system administrator account, which has no restrictions. All other accounts are created in a separate Telnet_Server_Multiuser.c template file. You must add this file to your project and customize it. You can add new commands or remove existing commands from it. To add the template to your project, simply right-click on the Source group, select Add Net Item to Group, then click on User Code Template and scroll in the template files list until you find the Telnet Server Multi-user template (you will find a listing here).
The following function is included in this template:
The following function is included in the Network Component library rl_net.h:
Code Example Telnet_Server_Multiuser.c
The telnet_server_process function processes the Telnet command when it is received from a remote client. This function then generates a reply message and sends it back to the user. It is part of the Telnet_Server_UIF.c template file.
When the reply message is short, the whole message can be sent in a single TCP packet. However, when long reports are generated, multiple TCP packets must be sent to transfer the whole message. For example, when the log files are displayed this is often the case. Both single and multiple packets are supported by the Embedded Telnet Server.
In the following example, the Telnet command HELP is sent by the Telnet client:
This command is answered by the predefined help message tnet_help. This message is copied to the output buffer and sent to the remote Telnet client. The following code sends the reply message:
A long reply message requires multiple calls to the function telnet_server_process. Each call to this function generates part of the reply message until the entire message is generated and sent. To distinguish between different calls to the function, the argument pvar is used. This argument is a pointer to a variable that is set to 0 on the first call and not altered on each subsequent call to this function. The function's return value, which specifies the number of bytes in the reply message, cannot exceed 1500. Hence the high bits of the function's return value is used to store the flags:
In the following example, the MEAS command is given by the user using the Telnet client.
When a new Telnet command is received, the function telnet_server_process is called with the argument pvar set to 0. The command buffer cmd is checked to identify the command.
When a command is recognized, you can reuse the same command buffer to store local variables, which might be needed in repeated calls. During the repeated call to this function, the cmd buffer is locked and is not altered by the system. You can use it as temporary storage of variables for the repeated calls. Each Telnet session has its own buffer of 96 bytes, of which only 95 bytes can be used.
The above example uses 3 bytes of a storage variable pointed by pvar pointer for the following structure:
When the call to telnet_server_process is repeated for the same command, the value of a storage variable pointed to by argument pvar is not altered any more. You can use the value of pvar to process the command differently. The pvar buffer now holds the private structure MY_BUF
, which is valid for the lifetime of processing the command. When the command processing is finished, this buffer is not used any more until the next command.
After giving a meas
4
command, the Telnet Client screen looks like this:
The Telnet server configuration file Net_Config_Telnet_Server.h contains the following settings: