Article title: simple implementation of Linux serial port internet access. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
This article mainly describes some simple implementation of serial port internet access in Linux or embedded Linux, which is widely used in industrial control. It is expected that the method for achieving the internet access without Nic devices will be helpful.
Linux provides rich support for Serial port internet access, such as PPP (Peer-to-Peer Protocol) and SLIP (Serial Line Interface Protocol, this is an old serial line interface protocol. "surfing the Internet" refers to using the serial port as a network interface by encapsulating network packets (such as IP packets) to achieve a terminal without a Nic, you can use the serial port for network communication. However, the two protocols must be supported by the kernel. For example, if you use PPP in a Linux environment without PPP configuration, you must re-compile the kernel in addition to installing the PPP application layer software. SLIP is an old simple protocol. Currently, the default configurations of Linux kernel are supported and you do not need to re-compile the kernel. Despite this, its source code looks a bit "weird and complex ". In the embedded Linux system, if the kernel has been burned into Flash, and the kernel does not provide support such as PPP or SLIP to save space, of course, there is no way to directly use PPP or SLIP without re-writing Flash. In fact, users must dynamically load the kernel implementation modules of PPP and SLIP. For some embedded applications, porting or modifying the PPP source code becomes tedious and complicated. This article introduces a very economical and practical method for implementing serial port internet access.
Linux simple serial port internet access principle
The implementation principle of simple serial port access 1 is shown.
Figure 1
Linux Box A and Linux Box B are two terminals installed with A Linux operating system (either A PC or an embedded device). they use A serial port communication line (null modem cable line) connection. The service process server that controls serial communication reads and writes two character devices: the sending character device sending device and the receiving character device sending ing device. In the kernel space, the pseudo network device driver can directly read and write the sending and receiving character devices. In fact, the communication between the pseudo network driver and the pseudo network driver in the kernel space is only for reading and writing the shared cache. The driver of a pseudo network device provides the service functions provided by most common Nic drivers, but does not implement some hardware code. When a user space process needs to send data, it first needs to package the data through the TCP/IP processing layer of the Linux operating system, and then directly writes the packaged data to the sending device, wait for the server process to read and finally send the data to the server process of another Linux Box through the serial port. when the server process finds that data is transmitted from the serial port, it writes the data to the processing device, when the pseudo-network driver finds that the device has new data, it transfers the data to the TCP/IP layer for processing. Finally, the network application receives the data from the other party. The source programs designed in this article mainly include ed_device.c, ed_device.h, and server. c. Ed_device.c is the main program of the kernel part of the serial port for Internet access, including the program of character devices and pseudo network interface devices. server. c is responsible for serial port communication. The header files included in the main file ed_device.c are not listed here.
Loading and deregistering Linux serial port Internet access devices
The entire Kernel of the Linux serial port surfing program is implemented in the form of LKM (Loadable Kernel Module. During LKM loading, the pseudo network device, the sending character device, and the receiving character device are initialized and registered. The purpose of registration is to allow the operating system to identify the device to be operated by the user process and complete the operations on the device (such as read, write, and other system calls ). The Linux loading module is actually the insertion of the module linked list; the deletion module is like the deletion of the module linked list members.
The init_module () entry function of the initialization kernel module includes the initialization entry function eddev_module_init () for character devices and the pseudo network device initialization entry function ednet_module_init ().
When the kernel needs to be detached, resources must be released and devices must be logged out. cleanup_module () completes this task. In the cleanup_module () function, eddev_module_cleanup () is used to release resources occupied by character devices (such as allocated cache areas). ednet_module_cleanup () is used to release resources occupied by pseudo network devices. Some module programs in this article are compiled with ed_device.o. after being loaded, run the lsmod command to check whether the module name is ed_device. The function 2 for loading and deregistering the module ed_device is shown in.
Figure 2
When we need to load the module, we only need to use the insmod command. if we need to uninstall the module, we use the rmmod command. For example, load the ed_device module and configure the pseudo network interface IP address as 192.168.5.1.
[root@localhost test]insmod ed_device.o,[root@localhost test]ifconfig ed0 192.168.5.1 up
|
You can see the ed0 pseudo network device in the/proc/net/dev file. To uninstall the ed_device module, stop sending and receiving network data, and then uninstall the module:
[root@localhost test]ifconfig ed0 down[root@localhost test]rmmod ed_device
|
If we set the pseudo-network interface address of another Linux box to 192.168.5.2, we can use a serial line to directly connect two terminals and use network applications, run the server daemon on two terminals and then execute telnet:
[root@localhost test]# telnet 192.168.5.2Trying 192.168.5.2...Connected to 192.168.5.2 (192.168.5.2).Escape character is '^]'.Red Hat Linux release 9 (Shrike)Kernel 2.4.20-8 on an i686login:
|
[1] [2] [3] [4] [5] [6] [7] Next page