Class: RNCP::NcpPusher

Inherits:
Object
  • Object
show all
Includes:
Files, Networking
Defined in:
lib/rncp/pusher.rb

Overview

Class to establish a connection to a Listener class and then compress and send files.

Instance Method Summary collapse

Methods included from Files

#directory_list, #tar, #untar

Methods included from Networking

#bind_broadcast, #bind_multicast, #bind_tcp, #join_multicast

Constructor Details

#initializeNcpPusher

Returns a new instance of NcpPusher.



28
29
# File 'lib/rncp/pusher.rb', line 28

def initialize
end

Instance Method Details

#push(files) ⇒ Object

Using Multicast or Broadcast, establishes a connection, compresses files and sends to destination.

Parameters:

  • files (Array)

    array of file names to send



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rncp/pusher.rb', line 57

def push(files)
  begin
    sock = nil
    addr = nil
    msock = bind_multicast

    # check broadcast
    bsock = bind_broadcast
    
    if msock.nil? == true && bsock.nil? == true
      puts "[!] cannot continue without atleast one announcement socket!"
      return 1
    end

    dsock = bind_tcp

    if dsock.nil? == true
      puts "[!] cannot continue without data socket"
      return -1
    end

    puts "[*] starting X-Casting, waiting for TCP connect"
    while sock.nil? == true
      # Multicast Ping
      if msock.nil? == false
        msock.send RNCP::MC_MSG, 0, RNCP::IPV4_GROUP, RNCP::PORT
      end

      # Broadcast Ping
      if bsock.nil? == false
        bsock.send RNCP::BC_MSG, 0, RNCP::IPV4_BC, RNCP::PORT
      end

      puts "."
      result = select( [dsock], nil, nil, 2 )

      next if result.nil? == true
      for inp in result[0]
        if inp == dsock
          sock, addr = dsock.accept
          printf "[*] connection from %s:%s\n",
              addr.ip_address, addr.ip_port
          puts "[*] Client answer: #{sock.recv 1024}"
        end # if
      end # for inp
    end # while sock.nil?

    msock.close if msock.nil? == false
    msock = nil
    bsock.close if bsock.nil? == false
    bsock = nil
    dsock.close if dsock.nil? == false
    dsock = nil

    data = tar files

    sock.send data.string, 0
    sock.flush
  rescue Exception => e
    puts "[!] cannot push data, bailing out"
    puts e
  ensure
    sock.close if sock.nil? == false
    puts "[*] finished"
    msock.close if msock.nil? == false
    bsock.close if bsock.nil? == false
    dsock.close if dsock.nil? == false
  end # begin
end

#send_to(ip, files) ⇒ Object

Sends file directly to a given IP Address or Hostname

Parameters:

  • ip (String)

    the address or hostname of the destination

  • files (Array)

    array of file/directory names to send



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rncp/pusher.rb', line 34

def send_to(ip, files)
  begin
    puts "[*] copying #{files} to ip : #{ip}"
    sock = TCPSocket::new ip, RNCP::PORT
    sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1

    data = tar files
    
    sock.send data.string, 0
    sock.flush

  rescue Exception => e
    puts "[!] cannot create connection to host, bailing out"
    puts e
  ensure
    sock.close if sock.nil? == false
    puts "[#] finished"
  end
end