Guest User

Untitled

a guest
Nov 25th, 2014
298
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #==============================================================================
  2. # Module Win32
  3. #==============================================================================
  4. module Win32
  5.  
  6.   def copymem(len)
  7.     buf = "\0" * len
  8.     Win32API.new("kernel32", "RtlMoveMemory", "ppl", "").call(buf, self, len)
  9.     buf
  10.   end
  11.  
  12. end
  13.  
  14. class Numeric
  15.   include Win32
  16. end
  17.  
  18. class String
  19.   include Win32
  20. end
  21.  
  22. #------------------------------------------------------------------------------
  23. module Winsock
  24.  
  25.   DLL = "ws2_32"
  26.  
  27.   def self.accept(*args)
  28.     Win32API.new(DLL, "accept", "ppl", "l").call(*args)
  29.   end
  30.  
  31.   def self.bind(*args)
  32.     Win32API.new(DLL, "bind", "ppl", "l").call(*args)
  33.   end
  34.  
  35.   def self.closesocket(*args)
  36.     Win32API.new(DLL, "closesocket", "p", "l").call(*args)
  37.   end  
  38.  
  39.   def self.connect(*args)
  40.     Win32API.new(DLL, "connect", "ppl", "l").call(*args)
  41.   end    
  42.  
  43.   def self.gethostbyaddr(*args)
  44.     Win32API.new(DLL, "gethostbyaddr", "pll", "l").call(*args)
  45.   end
  46.  
  47.   def self.gethostbyname(*args)
  48.     Win32API.new(DLL, "gethostbyname", "p", "l").call(*args)
  49.   end
  50.  
  51.   def self.gethostname(*args)
  52.     Win32API.new(DLL, "gethostname", "pl", "").call(*args)
  53.   end
  54.  
  55.   def self.getservbyname(*args)
  56.     Win32API.new(DLL, "getservbyname", "pp", "p").call(*args)
  57.   end
  58.  
  59.   def self.htonl(*args)
  60.     Win32API.new(DLL, "htonl", "l", "l").call(*args)
  61.   end
  62.  
  63.   def self.htons(*args)
  64.     Win32API.new(DLL, "htons", "l", "l").call(*args)
  65.   end
  66.  
  67.   def self.inet_addr(*args)
  68.     Win32API.new(DLL, "inet_addr", "p", "l").call(*args)
  69.   end
  70.  
  71.   def self.inet_ntoa(*args)
  72.     Win32API.new(DLL, "inet_ntoa", "l", "p").call(*args)
  73.   end  
  74.  
  75.   def self.listen(*args)
  76.     Win32API.new(DLL, "listen", "pl", "l").call(*args)
  77.   end
  78.  
  79.   def self.recv(*args)
  80.     Win32API.new(DLL, "recv", "ppll", "l").call(*args)
  81.   end
  82.  
  83.   def self.select(*args)
  84.     Win32API.new(DLL, "select", "lpppp", "l").call(*args)
  85.   end
  86.  
  87.   def self.send(*args)
  88.     Win32API.new(DLL, "send", "ppll", "l").call(*args)
  89.   end
  90.  
  91.   def self.setsockopt(*args)
  92.     Win32API.new(DLL, "setsockopt", "pllpl", "l").call(*args)
  93.   end  
  94.  
  95.   def self.shutdown(*args)
  96.     Win32API.new(DLL, "shutdown", "pl", "l").call(*args)
  97.   end
  98.  
  99.   def self.socket(*args)
  100.     Win32API.new(DLL, "socket", "lll", "l").call(*args)  
  101.   end
  102.  
  103.   def self.WSAGetLastError(*args)
  104.     Win32API.new(DLL, "WSAGetLastError", "", "l").call(*args)
  105.   end
  106.  
  107. end
  108.  
  109. #------------------------------------------------------------------------------
  110. class Socket
  111.  
  112.   AF_UNSPEC                 = 0  
  113.   AF_UNIX                   = 1
  114.   AF_INET                   = 2
  115.   AF_IPX                    = 6
  116.   AF_APPLETALK              = 16
  117.  
  118.   PF_UNSPEC                 = 0  
  119.   PF_UNIX                   = 1
  120.   PF_INET                   = 2
  121.   PF_IPX                    = 6
  122.   PF_APPLETALK              = 16
  123.  
  124.   SOCK_STREAM               = 1
  125.   SOCK_DGRAM                = 2
  126.   SOCK_RAW                  = 3
  127.   SOCK_RDM                  = 4
  128.   SOCK_SEQPACKET            = 5
  129.  
  130.   IPPROTO_IP                = 0
  131.   IPPROTO_ICMP              = 1
  132.   IPPROTO_IGMP              = 2
  133.   IPPROTO_GGP               = 3
  134.   IPPROTO_TCP               = 6
  135.   IPPROTO_PUP               = 12
  136.   IPPROTO_UDP               = 17
  137.   IPPROTO_IDP               = 22
  138.   IPPROTO_ND                = 77
  139.   IPPROTO_RAW               = 255
  140.   IPPROTO_MAX               = 256
  141.  
  142.   SOL_SOCKET                = 65535
  143.  
  144.   SO_DEBUG                  = 1
  145.   SO_REUSEADDR              = 4
  146.   SO_KEEPALIVE              = 8
  147.   SO_DONTROUTE              = 16
  148.   SO_BROADCAST              = 32
  149.   SO_LINGER                 = 128
  150.   SO_OOBINLINE              = 256
  151.   SO_RCVLOWAT               = 4100
  152.   SO_SNDTIMEO               = 4101
  153.   SO_RCVTIMEO               = 4102
  154.   SO_ERROR                  = 4103
  155.   SO_TYPE                   = 4104
  156.   SO_SNDBUF                 = 4097
  157.   SO_RCVBUF                 = 4098
  158.   SO_SNDLOWAT               = 4099
  159.  
  160.   TCP_NODELAY               =   1
  161.  
  162.   MSG_OOB                   = 1
  163.   MSG_PEEK                  = 2
  164.   MSG_DONTROUTE             = 4
  165.  
  166.   IP_OPTIONS                =   1
  167.   IP_DEFAULT_MULTICAST_LOOP =   1
  168.   IP_DEFAULT_MULTICAST_TTL  =   1
  169.   IP_MULTICAST_IF           =   2
  170.   IP_MULTICAST_TTL          =   3
  171.   IP_MULTICAST_LOOP         =   4
  172.   IP_ADD_MEMBERSHIP         =   5
  173.   IP_DROP_MEMBERSHIP        =   6
  174.   IP_TTL                    =   7
  175.   IP_TOS                    =   8
  176.   IP_MAX_MEMBERSHIPS        =   20
  177.  
  178.   EAI_ADDRFAMILY            = 1
  179.   EAI_AGAIN                 = 2
  180.   EAI_BADFLAGS              = 3
  181.   EAI_FAIL                  = 4
  182.   EAI_FAMILY                = 5
  183.   EAI_MEMORY                = 6
  184.   EAI_NODATA                = 7
  185.   EAI_NONAME                = 8
  186.   EAI_SERVICE               = 9
  187.   EAI_SOCKTYPE              = 10
  188.   EAI_SYSTEM                = 11
  189.   EAI_BADHINTS              = 12
  190.   EAI_PROTOCOL              = 13
  191.   EAI_MAX                   = 14
  192.  
  193.   AI_PASSIVE                = 1
  194.   AI_CANONNAME              = 2
  195.   AI_NUMERICHOST            = 4
  196.   AI_MASK                   = 7
  197.   AI_ALL                    = 256
  198.   AI_V4MAPPED_CFG           = 512
  199.   AI_ADDRCONFIG             = 1024
  200.   AI_DEFAULT                = 1536
  201.   AI_V4MAPPED               = 2048
  202.  
  203.   def self.getaddress(host)
  204.     gethostbyname(host)[3].unpack("C4").join(".")
  205.   end
  206.  
  207.   def self.getservice(serv)
  208.     case serv
  209.     when Numeric
  210.       return serv
  211.     when String
  212.       return getservbyname(serv)
  213.     else
  214.       raise "Please us an interger or string for services."
  215.     end
  216.   end
  217.  
  218.   def self.gethostbyname(name)
  219.     raise SocketError::ENOASSOCHOST if (ptr = Winsock.gethostbyname(name)) == 0
  220.     host = ptr.copymem(16).unpack("iissi")
  221.     [host[0].copymem(64).split("\0")[0], [], host[2], host[4].copymem(4).unpack("l")[0].copymem(4)]
  222.   end
  223.  
  224.   def self.gethostname
  225.     buf = "\0" * 256
  226.     Winsock.gethostname(buf, 256)
  227.     buf.strip
  228.   end
  229.  
  230.   def self.getservbyname(name)
  231.     case name
  232.     when /echo/i
  233.       return 7
  234.     when /daytime/i
  235.       return 13
  236.     when /ftp/i
  237.       return 21
  238.     when /telnet/i
  239.       return 23
  240.     when /smtp/i
  241.       return 25
  242.     when /time/i
  243.       return 37
  244.     when /http/i
  245.       return 80
  246.     when /pop/i
  247.       return 110
  248.     else
  249.       if Network.testing? or Network.test_completed
  250.         Network.test_result(true)
  251.         return if Network.test_completed
  252.       else
  253.         raise "Service not recognized."
  254.       end
  255.     end
  256.   end
  257.  
  258.   def self.sockaddr_in(host, port)
  259.     begin
  260.       [AF_INET, getservice(port)].pack("sn") + gethostbyname(host)[3] + [].pack("x8")
  261.     rescue
  262.       if Network.testing? or Network.test_completed
  263.         Network.test_result(true)
  264.         return if Network.test_completed
  265.       end
  266.     end
  267.   end
  268.  
  269.   def self.open(*args)
  270.     socket = new(*args)
  271.     if block_given?
  272.       begin
  273.         yield socket
  274.       ensure
  275.         socket.close
  276.       end
  277.     end
  278.     nil
  279.   end
  280.  
  281.   def initialize(domain, type, protocol)
  282.     SocketError.check if (@fd = Winsock.socket(domain, type, protocol)) == -1
  283.     @fd
  284.   end
  285.  
  286.   def accept(flags = 0)
  287.     buf = "\0" * 16
  288.     SocketError.check if Winsock.accept(@fd, buf, flags) == -1
  289.     buf
  290.   end
  291.  
  292.   def bind(sockaddr)
  293.     SocketError.check if (ret = Winsock.bind(@fd, sockaddr, sockaddr.size)) == -1
  294.     ret
  295.   end
  296.  
  297.   def close
  298.     SocketError.check if (ret = Winsock.closesocket(@fd)) == -1
  299.     ret
  300.   end
  301.  
  302.   def connect(sockaddr)
  303.     return if Network.test_completed
  304.     SocketError.check if (ret = Winsock.connect(@fd, sockaddr, sockaddr.size)) == -1
  305.     ret
  306.   end
  307.  
  308.   def listen(backlog)
  309.     SocketError.check if (ret = Winsock.listen(@fd, backlog)) == -1
  310.     ret
  311.   end
  312.  
  313.   def select(timeout)
  314.     SocketError.check if (ret = Winsock.select(1, [1, @fd].pack("ll"), 0, 0, [timeout, timeout * 1000000].pack("ll"))) == -1
  315.     ret
  316.   end
  317.  
  318.   def ready?
  319.     not select(0) == 0
  320.   end  
  321.  
  322.   def read(len)
  323.     buf = "\0" * len
  324.     Win32API.new("msvcrt", "_read", "lpl", "l").call(@fd, buf, len)
  325.     buf
  326.   end
  327.  
  328.   def recv(len, flags = 0)
  329.     buf = "\0" * len
  330.     SocketError.check if Winsock.recv(@fd, buf, buf.size, flags) == -1
  331.     buf
  332.   end
  333.  
  334.   def send(data, flags = 0)
  335.     SocketError.check if (ret = Winsock.send(@fd, data, data.size, flags)) == -1
  336.     ret
  337.   end
  338.  
  339.   def write(data)
  340.     Win32API.new("msvcrt", "_write", "lpl", "l").call(@fd, data, 1)
  341.   end
  342.  
  343. end
  344.  
  345. #------------------------------------------------------------------------------
  346. class TCPSocket < Socket
  347.  
  348.   def self.open(*args)
  349.     socket = new(*args)
  350.     if block_given?
  351.       begin
  352.         yield socket
  353.       ensure
  354.         socket.close
  355.       end
  356.     end
  357.     nil
  358.   end
  359.  
  360.   def initialize(host, port)
  361.     super(AF_INET, SOCK_STREAM, IPPROTO_TCP)
  362.     connect(Socket.sockaddr_in(host, port))
  363.   end
  364.  
  365. end
  366.  
  367. #------------------------------------------------------------------------------
  368. class SocketError < StandardError
  369.  
  370.   ENOASSOCHOST = "getaddrinfo: no address associated with hostname."
  371.  
  372.   def self.check
  373.     errno = Winsock.WSAGetLastError
  374.     if !Network.testing?
  375.       $window["alert"].open(Vocab::ConnectionClosed) { disconnect; exit }
  376. #      raise Errno.const_get(Errno.constants.detect { |c| Errno.const_get(c).new.errno == errno })
  377. #      Erro.new
  378.     elsif errno != 0
  379.       Network.test_result(true)
  380.     else
  381.       Network.socket.send("<test>Test Completed</test>")
  382.       Network.test_result(false)
  383.     end
  384.   end
  385.  
  386. end
RAW Paste Data