Software: Apache/2.2.22 (Debian). PHP/5.6.36 uname -a: Linux h05.hvosting.ua 4.9.110-amd64 #3 SMP Sun Nov 4 16:27:09 UTC 2018 x86_64 uid=1389(h33678) gid=1099(h33678) groups=1099(h33678),502(mgrsecure) Safe-mode: OFF (not secure) /usr/share/doc/socat/ drwxr-xr-x |
Viewing file: Select action/file-type: Generic sockets with SocatIntroductionBeginning with version 1.7.0 socat provides means to freely control important aspects of socket handling. This allows to experiment with socket types and protocols that are not explicitely implemented in socat. The related socat features fall into three major categories:
In practice this gives you two possibilities: If you want to cope with sockets staying within the usual domains ( = protocol families = address families) which are IPv4, IPv6, UNIX/local, and raw interface for socat 1.7.0, it is sufficient to learn about a couple of address options that allow to change default parameters, and to apply generic socket options. For other address families socat provides generic socket addresses. Generic socket optionsExample 1: DCCP communicationA relatively new communication protocol has been introduced in the Internet community for which no socat address type has been implemented up to version 1.7.0 (see IETF's Datagram Congestion Control Protocol and Linux foundation Net:DCCP for more info). Taken that the operating system implements DCCP, it is possible to use this protocol with socat while just employing standard socket addresses and some options. A simple server that accepts a DCCP connection, passes the arriving data to a subprocess for converting upper case to lower case characters, and then returns it to the client: socat TCP4-LISTEN:4096,reuseaddr,type=6,prototype=33 exec:'tr A-Z a-z',pty,raw,echo=0A simple client that sends some upper case characters to the server via DCCP and prints what the server returns: echo ABCD |socat - TCP4-CONNECT:localhost:4096,type=6,prototype=33We choose the TCP4 addresses as base because it best matches the DCCP requirements:
Option type=6 changes TCP's SOCK_STREAM parameter to SOCK_DCCP, and prototype=33 replaces the default IPPROTO_TCP with IPPROTO_DCCP. DCCP has an important parameter, the service code. It provides another multiplexing layer beyond the protocol ports. The Linux implementation of DCCP allows to set this parameter with code like setsocktopt(fd, SOL_DCCP, DCCP_SOCKOPT_SERVICE, {1}, sizeof(int)). The equivalent generic socat option is: setsockopt-int=269:2:1 for service code 1. If the service codes on server and client do not match the connect() operation fails with error:
Please note that this examples works with IPv6 as well, you just need to replace the TCP4 words with TCP6, and the IPv4 socket address with an appropriate IPv6 socket address, e.g. [::1]! Generic socket addressessocat's generic socket addresses are a more comprehensive mechanism that allows to deal with protocol families whose socket addresses are not supported by socat - no semantical parsing, no structured assignment to the struct components are available. Instead, the socket address records for binding and connecting/sending are specified in unstructured hexadecimal form. The following example demonstrates this by performing simple data transfer over raw AppleTalk protocol. Note: I do not have any knowledge about AppleTalk. I just managed to configure my Linux host to tolerate the creation of a receiving and a sending socket. Don't blame me nor ask me for support if it does not work for you. Enabling AppleTalk protocolInstall the netatalk package. Check that /etc/netatalk/atalkd.conf has an entry like eth0 -phase 2 -net 0-65534 -addr 65280.243. The last part is an arbitrary (?) host address, some of the following values must fit it. Make sure the atalkd daemon is running. Run the AppleTalk ping command: aecho 65280.243If you get an error like:
then try to restart atalkd: /etc/init.d/atalkd restartWhen aecho works like ping you are ready for the next step. Example 2: AppleTalk datagram communicationWe start a socat process with a receiver and echo service: socat SOCKET-RECVFROM:5:2:0:x40x00x0000x00x00x0000000000000000 PIPEThen, in another shell on the same host, we start a client socket process that sends data to the server and gets the answer: echo ABCD |socat - SOCKET-DATAGRAM:5:2:0:x40x00xff00xf3x00x0000000000000000The client process should print the data. How did this work? The generic socat address has just used the system call parameters that were provided on command line, without knowing anything about AppleTalk sockets and protocol. The values 5, 2, and 0 are directly used for the socket() call: they specify the domain (PF_APPLETALK=5), socket type (SOCK_DGRAM=2), and no protocol (0) - values for Linux. The long hex strings define the socket addresses. They can only be constructed with knowledge of the underlying structure. In /usr/include/linux/atalk.h we find the following declarations: struct atalk_addr { __be16 s_net; __u8 s_node; }; struct sockaddr_at { sa_family_t sat_family; __u8 sat_port; struct atalk_addr sat_addr; char sat_zero[8]; After rolling out atalk_addr and considering implicit padding by the C programming language we get the following byte map:
Note that hexadecimal ff00 is the same as decimal 65280, and hexadecimal xf3 is the same as decimal 243 - these are the numbers specified in atalkd.conf. The address family component must be omitted from the socket address because it is added by socat implicitely. The resulting hexadecimal representation of the target socket address is therefore: x40x00xff00xf3x00x0000000000000000The receiver just has to specify the port, so its bind address data is: x40x00x0000x00x00x0000000000000000Parameters for well known socket typesFinding the correct parameters and socket addresses is not always trivial. Therefore this section provides tables with the parameters of common socket types. Some of these types are directly implemented by socat (and other programs). Establishing interoperability between a directly implemented socket and a generic socket might be your first step before entering unknown ground. Socket parametersTable: parameter names for "well known" sockets:
Table: parameter values:
Socket address specificationsThese hexadecimal data define socket addresses for local and remote sockets, and for bind and range options. The basis is the struct sockaddr_* for the respective address family that should be declared in the C include files. Please keep in mind that their first two bytes (sa_family and - on BSD - sa_len) are implicitely prepended by socat. Linux on 32bit Intel:
For AppleTalk see above example. Solaris on 32bit Intel:
Forever - play on...Eager to experiment with exotic socket types? Run nmap's protocol scan and see what is available on your system: nmap -sO localhost
Copyright: Gerhard Rieger 2008 |
:: Command execute :: | |
--[ c99shell v. 2.0 [PHP 7 Update] [25.02.2019] maintained by PinoyWH1Z | C99Shell Github | Generation time: 0.0423 ]-- |