This article will explore the concept of a socket in the Linux operating system: basic data structures, how they work, and whether it is possible to manipulate socket state using an application. As a practice, we will consider the netcat and socat tools.
What is a socket?
- Linux. IP- + . , , . , . , IP- + - . - " ". , . Linux - , . , , . Linux :
struct socket - BSD, , " ";
struct sock - , Linux "INET socket";
struct sk_buff - "" , ;
, . . , . :
socket - ;
bind - . - , ;
listen - . ;
connect - ;
accept - , ;
send/recv - / ;
close - , .
, , , , . netcat socat.
netcat
25 . c , : Debian, Ubuntu, FreeBSD, MacOS. nc, nc.traditional ncat . , TCP UDP . , , , :
/ ;
.
. , TCP netcat UDP . :
:
Destination:
nc -u lvvp 7878
Repeater. TCP, UDP, , . FIFO . :
sudo mkfifo /tmp/repeater # FIFO
sudo nc -l -p 4545 < /tmp/repeater | nc -u -l 10.0.3.5 7878 > tmp/repeater
IP 10.0.3.5 - Destination. "|" "><" . .
Source:
nc 10.0.2.4 4545
Source:
Destination:
, Destination Repeater:
nc -l -p 4545 -o file
, :
, netcat , . , , , . socat.
socat
STDIO -> TCP Socket;
FILE -> TCP Socket;
TCP Socket -> Custom Application;
UDP Socket -> Custom Application;
Socket -> Socket.
, - , socat . :
socat -h
socat , chroot .
, , socat:
socat additionalOptions addr1 addr2
additionalOptions - , , ;
addr1 - ( U u), , , ;
addr2 - ( U u), , , ;
. 1 . , socat , 2 . , , .
, socat netcat TCP , :
socat TCP-LISTEN:4545, STDOUT
netcat:
nc localhost 4545
, socat , "-u", . :
, :
socat TCP-LISTEN:4545,reuseaddr,keepalive,fork STDOUT
, socat . "SOCKET option group".
socat .
Network engineer. Basic. , , , 4 .