relp 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db59e2522c2fbe9177fb101703ac7f95a2c4dd06
4
+ data.tar.gz: 12b1e5a6bf959148fabf1a3e622b8aa84703e86f
5
+ SHA512:
6
+ metadata.gz: 13241da1e76938e563e5c07837093d3f176a5539a5c203bade38aff95e7cd6b1c66d8d0c6de8f0814123b42fe842c1304193cfd6ec6accb4b461705250db2a48
7
+ data.tar.gz: 65c3b5cbef9f3521210b750a4b01486aaf05d98ee5a3d2e31725b01b73272a1cb1b4361679b559ac1d05315e6e190873a84a6e59e80f05907aa6a55092600a3f
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 dhlavac
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # RELP
2
+
3
+ This library contains native implementation of [RELP protocol](http://www.rsyslog.com/doc/relp.html) in ruby. At the moment only server-side
4
+ is properly implemented and (to some extent) tested.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'relp'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install relp
21
+
22
+ ## Usage
23
+
24
+ To use this library, require it in your ruby application file, then you can create `Relp::RelpServer`. Arguments are `(bind, port, [log,] callback)`,
25
+ where `bind` is :string and specifies address you want to bind to, use "0.0.0.0" to bind to any address. `port` is :integer and sets on which port you want to listen for incoming RELP connections.
26
+ `log` is optional and allows you to pass you application's logger into relp library to receive its logs, otherwise the server creates its own standard logger. Finally `callback` is method you want to be
27
+ executed upon successfully accepted message, it has only one :string parameter, which is message itself.
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dhlavac/RELP.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
43
+
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ desc "Run Tests"
11
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "relp"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ require 'relp/server'
2
+
3
+ module Relp
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'socket'
2
+
3
+ #TODO client side
4
+
@@ -0,0 +1,16 @@
1
+ module Relp
2
+ class RelpProtocolError < StandardError
3
+ end
4
+
5
+ class ConnectionClosed < RelpProtocolError
6
+ end
7
+
8
+ class ConnectionRefused < RelpProtocolError
9
+ end
10
+
11
+ class FrameReadException < RelpProtocolError
12
+ end
13
+
14
+ class InvalidCommand < RelpProtocolError
15
+ end
16
+ end
@@ -0,0 +1,58 @@
1
+ require 'relp/exceptions'
2
+ require 'socket'
3
+ module Relp
4
+
5
+ class RelpProtocol
6
+ @@relp_version = '0'
7
+ @@relp_software = 'librelp,1.2.13,http://librelp.adiscon.com'
8
+
9
+ def frame_write(socket, frame)
10
+ raw_data=[
11
+ frame[:txnr],
12
+ frame[:command],
13
+ frame[:message]
14
+ ].join(' ')
15
+ @logger.debug "Writing Frame #{frame.inspect}"
16
+ begin
17
+ socket.write(raw_data)
18
+ rescue Errno::EPIPE,IOError,Errno::ECONNRESET
19
+ raise Relp::ConnectionClosed
20
+ end
21
+ end
22
+
23
+ def frame_read(socket)
24
+ begin
25
+ socket_content = socket.read_nonblock(4096)
26
+ frame = Hash.new
27
+ if match = socket_content.match(/(^[0-9]+) ([\S]*) (\d+)([\s\S]*)/)
28
+ frame[:txnr], frame[:command], frame[:data_length], frame[:message] = match.captures
29
+ frame[:message].lstrip! #message could be empty
30
+ else
31
+ raise raise Relp::FrameReadException.new('Problem with reading RELP frame')
32
+ end
33
+ @logger.debug "Reading Frame #{frame.inspect}"
34
+ rescue IOError
35
+ @logger.error 'Problem with reading RELP frame'
36
+ raise Relp::FrameReadException.new 'Problem with reading RELP frame'
37
+ rescue Errno::ECONNRESET
38
+ @logger.error 'Connection reset'
39
+ raise Relp::ConnectionClosed.new 'Connection closed'
40
+ end
41
+ is_valid_command(frame[:command])
42
+
43
+ return frame
44
+ end
45
+
46
+ def is_valid_command(command)
47
+ valid_commands = ["open", "close", "rsp", "syslog"]
48
+ if !valid_commands.include?(command)
49
+ @logger.error 'Invalid RELP command'
50
+ raise Relp::InvalidCommand.new('Invalid command')
51
+ end
52
+ end
53
+
54
+ def extract_message_information(message)
55
+ informations = Hash[message.scan(/^(.*)=(.*)$/).map { |(key, value)| [key.to_sym, value] }]
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,165 @@
1
+ require 'relp/relp_protocol'
2
+ require 'logger'
3
+ require 'thread'
4
+
5
+ module Relp
6
+ class RelpServer < RelpProtocol
7
+
8
+ def initialize(host, port, logger = nil, callback)
9
+ @logger = logger
10
+ @logger = Logger.new(STDOUT) if logger.nil?
11
+ @socket_list = Array.new
12
+ #@logger.level = Logger::INFO #TODO find how to set log level
13
+ @callback = callback
14
+ @required_command = 'syslog'
15
+
16
+ begin
17
+ @server = TCPServer.new(host, port)
18
+ @logger.info "Starting #{self.class} on %s:%i" % @server.local_address.ip_unpack
19
+ rescue Errno::EADDRINUSE
20
+ @logger.error "ERROR Could not start relp server: Port #{port} in use" #add port number
21
+ raise Errno::EADDRINUSE
22
+ end
23
+ end
24
+
25
+ def run
26
+ loop do
27
+ Thread.start(@server.accept) do |client_socket|
28
+ begin
29
+ @socket_list.push client_socket
30
+ remote_ip = client_socket.peeraddr[3]
31
+ @logger.info "New client connection coming from ip #{remote_ip}"
32
+ @logger.debug "New client started with object id=#{client_socket.object_id}"
33
+ connection_setup(client_socket)
34
+ while Thread.current.alive? do
35
+ ready = IO.select([client_socket], nil, nil, 10)
36
+ if ready
37
+ frame = communication_processing(client_socket)
38
+ return_message(frame[:message], (@callback))
39
+ ack_frame(client_socket,frame[:txnr])
40
+ end
41
+ end
42
+ rescue Relp::ConnectionClosed
43
+ @logger.info "Connection closed"
44
+ rescue Relp::RelpProtocolError => err
45
+ @logger.warn 'Relp error: ' + err.class.to_s + ' ' + err.message
46
+ ensure
47
+ server_close_message(client_socket) rescue nil
48
+ @logger.debug "Closing client socket=#{client_socket.object_id}"
49
+ @logger.info "Client from ip #{remote_ip} closed"
50
+ end
51
+ end
52
+ end
53
+ rescue Errno::EINVAL
54
+ # Swallowing exception here because it results even from properly closed socket
55
+ @logger.info "Socket close."
56
+ end
57
+
58
+ def return_message(message, callback)
59
+ list_of_messages = message.split(/\n+/)
60
+ list_of_messages.each do |msg|
61
+ remove = msg.split(": ").first + ": "
62
+ msg.slice! remove
63
+ callback.call(msg)
64
+ end
65
+ end
66
+
67
+ def create_frame( txnr, command, message)
68
+ frame = {:txnr => txnr,
69
+ :command => command,
70
+ :message => message
71
+ }
72
+ end
73
+
74
+ def ack_frame(socket, txnr)
75
+ frame = {:txnr => txnr,
76
+ :command => 'rsp',
77
+ :message => "6 200 OK\n"
78
+ }
79
+ frame_write(socket, frame)
80
+ end
81
+
82
+ def server_close_message(socket)
83
+ Hash.new frame = {:txnr => 0,
84
+ :command => 'serverclose',
85
+ :message => '0'
86
+ }
87
+ begin
88
+ frame_write(socket,frame)
89
+ @logger.debug 'Server close message send'
90
+ socket.close
91
+ @socket_list.delete socket
92
+ rescue Relp::ConnectionClosed
93
+ end
94
+ end
95
+
96
+ def server_shut_down
97
+ @socket_list.each do |client_socket|
98
+ if client_socket != nil
99
+ server_close_message(client_socket)
100
+ end
101
+ end
102
+ @logger.info 'Server shutdown'
103
+ @server.shutdown
104
+ @server = nil
105
+ end
106
+
107
+ private
108
+
109
+ def communication_processing(socket)
110
+ @logger.debug 'Communication processing'
111
+ frame = frame_read(socket)
112
+ if frame == nil
113
+ return nil
114
+ end
115
+ if frame[:command] == 'syslog'
116
+ return frame
117
+ elsif frame[:command] == 'close'
118
+ response_frame = create_frame(frame[:txnr], "rsp", "0")
119
+ frame_write(socket,response_frame)
120
+ @logger.info 'Client send close message'
121
+ server_close_message(socket)
122
+ raise Relp::ConnectionClosed
123
+ else
124
+ server_close_message(socket)
125
+ raise Relp::RelpProtocolError, 'Wrong relp command'
126
+ end
127
+ end
128
+
129
+ def connection_setup(socket)
130
+ @logger.debug 'Connection setup'
131
+ begin
132
+ read_ready = IO.select([socket], nil, nil, 10)
133
+ if read_ready
134
+ frame = frame_read(socket)
135
+ @logger.debug 'Frame read complete, processing..'
136
+ if frame[:command] == 'open'
137
+ @logger.debug 'Client command open'
138
+ message_informations = extract_message_information(frame[:message])
139
+ if message_informations[:relp_version].empty?
140
+ @logger.warn 'Missing RELP version specification'
141
+ server_close_message(socket)
142
+ raise Relp::RelpProtocolError
143
+ elsif @required_command != message_informations[:commands]
144
+ @logger.warn 'Missing required commands - syslog'
145
+ Hash.new response_frame = create_frame(frame[:txnr], 'rsp', '20 500 Missing required command ' + @required_command)
146
+ frame_write(socket, response_frame)
147
+ server_close_message(socket)
148
+ raise Relp::InvalidCommand, 'Missing required command'
149
+ else
150
+ Hash.new response_frame = create_frame(frame[:txnr], 'rsp', '93 200 OK' + "\n" + 'relp_version=' +@@relp_version + "\n" + 'relp_software=' + @@relp_software + "\n" + 'commands=' + @required_command + "\n")
151
+ @logger.debug 'Sending response to client'
152
+ frame_write(socket, response_frame)
153
+ end
154
+ else
155
+ server_close_message(socket)
156
+ raise Relp::InvalidCommand, frame[:command] + ' expecting open command'
157
+ end
158
+ end
159
+ rescue Relp::RelpProtocolError
160
+ @logger.debug 'Timed out (no frame to read)'
161
+ server_close_message(socket)
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,3 @@
1
+ module Relp
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class RELPTest < Minitest::Test
4
+ def test_that_it_has_a_version_number
5
+ refute_nil ::Relp::VERSION
6
+ end
7
+
8
+
9
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'relp'
3
+
4
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: relp
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Dominik Hlaváč Ďurán
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: If you want to receive or send message via RELP protocol you can use
56
+ this gem
57
+ email:
58
+ - dhlavacd@redhat.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - LICENSE.txt
64
+ - README.md
65
+ - Rakefile
66
+ - bin/console
67
+ - bin/setup
68
+ - lib/relp.rb
69
+ - lib/relp/client.rb
70
+ - lib/relp/exceptions.rb
71
+ - lib/relp/relp_protocol.rb
72
+ - lib/relp/server.rb
73
+ - lib/relp/version.rb
74
+ - test/RELP_test.rb
75
+ - test/test_helper.rb
76
+ homepage: https://github.com/dhlavac/Relp
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.0.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.5.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Ruby implementation of RELP (Reliable Event Logging Protocol) protocol.
100
+ test_files:
101
+ - test/RELP_test.rb
102
+ - test/test_helper.rb