Created
March 21, 2020 02:41
-
-
Save gr33n7007h/7f3e1272c911ca535d9beead58e52384 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'socket' | |
| TCP_STATES = { | |
| 1 => :established, | |
| 2 => :syn_sent, | |
| 3 => :syn_recv, | |
| 4 => :fin_wait1, | |
| 5 => :fin_wait2, | |
| 6 => :time_wait, | |
| 7 => :close, | |
| 8 => :close_wait, | |
| 9 => :last_ack | |
| }.freeze | |
| def unpack_state(s) | |
| s.getsockopt(6, 11).unpack('C')[0] | |
| end | |
| sock = Socket.new(2,1,0) | |
| p TCP_STATES.fetch(unpack_state(sock), 'unknown state') | |
| sock.connect(Addrinfo.tcp('www.google.com', 80)) | |
| p TCP_STATES.fetch(unpack_state(sock), 'unknown state') | |
| #sock.puts(%{GET / HTTP/1.1\r\n\r\n}) | |
| sock.puts(%{yo!\n\n}) | |
| p TCP_STATES.fetch(unpack_state(sock), 'unknown state') | |
| sock.gets('') | |
| p TCP_STATES.fetch(unpack_state(sock), 'unknown state') | |
| sock.shutdown(2) | |
| p TCP_STATES.fetch(unpack_state(sock), 'unknown state') | |
| sock.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment