Linux sockets

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 :





  1. struct socket - BSD, , " ";





  2. struct sock - , Linux "INET socket";





  3. 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 . :





:





  1. Destination: nc -u lvvp 7878







  2. 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. "|" "><" . .





  3. Source: nc 10.0.2.4 4545







Source:





Destination:





, Destination Repeater:





nc -l -p 4545 -o file







, :





, netcat , . , , , . socat.





socat

, . netcat++. 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 .













All Articles