simple_ping 0.1.0

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
+ SHA256:
3
+ metadata.gz: 88ca1bd08781db9e2ba867c8599767be9163b272b0045adb5fd612a2f79d4ab9
4
+ data.tar.gz: db299c8b1bedca36d463a483c37ddc5799411461910296e940f520938d0a9e1d
5
+ SHA512:
6
+ metadata.gz: 2433fe0679a8de4a99d90ecda60446590f4eb0dacb590f91efba668ea1e1631da652bd99d47a8abf39508406a770c7b680d87cd90874b53da4c21393e78ee2c0
7
+ data.tar.gz: 06dd89c9f9026672e3439e0d59e66304714aa3ada3803681a6ed7c279885faa2fff13492e79f41f454d6996c91b040b455c707563bf1cb3a88e55aa24c6e8d5c
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/*
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ test2.rb
14
+ test.rb
15
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at rapuntuleru2@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in simple_ping.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Akira Kure
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,20 @@
1
+ # Overview
2
+ A Simpe Ping Client for Ruby.
3
+
4
+ # How to use
5
+ ※ Need root privileges to run.
6
+
7
+ ```
8
+ ping_client = SimplePing::Client.new(src_ip_addr: "192.168.1.100")
9
+ ping_client.exec(dest_ip_addr: "192.168.1.101")
10
+ ```
11
+
12
+ # Specification
13
+
14
+ - Number of executions: 1
15
+ - Timeout Seconds: 10s
16
+ - Does not support retries
17
+ - Confirmed the operation with Ruby 2.7.1
18
+
19
+ # License
20
+ MIT
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_ping"
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(__FILE__)
@@ -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,91 @@
1
+ require "logger"
2
+ require "socket"
3
+ require "timeout"
4
+ require_relative "recv_message"
5
+ require_relative "icmp"
6
+
7
+ # Simple Ping (ICMP) client
8
+ # Root privilege required to run
9
+ # ex)
10
+ # require_relative "./simple_ping/client"
11
+ #
12
+ # client = SimplePing::Client.new(src_ip_addr: "192.168.1.100")
13
+ # client.exec(dest_ip_addr: "192.168.1.101") # => true or false
14
+ module SimplePing
15
+ class Client
16
+ # Wait time for ICMP Reply
17
+ TIMEOUT_TIME = 10
18
+
19
+ # constructor
20
+ #
21
+ # @param src_ip_addr [String] IP address of the interface to send ping, ex: "192.168.1.100"
22
+ def initialize(src_ip_addr:, log_level: Logger::INFO)
23
+ @src_ip_addr = src_ip_addr
24
+ @log_level = log_level
25
+ end
26
+
27
+ # Execute ping(ICMP).
28
+ # Basically, it returns Boolean depending on the result.
29
+ # Exception may be thrown due to unexpected error etc.
30
+ #
31
+ # @param dest_ip_addr [String] IP address of destination to send ping, ex: "192.168.1.101"
32
+ # @param data [String] ICMP Datagram, ex: "abc"
33
+ # @return [Boolean]
34
+ def exec(dest_ip_addr:, data: nil)
35
+ # Transmission
36
+ icmp = ICMP.new(type: ICMP::TYPE_ICMP_ECHO_REQUEST, data: data)
37
+ sockaddr = Socket.sockaddr_in(nil, dest_ip_addr)
38
+ trans_data = icmp.to_trans_data
39
+ socket.send(trans_data, 0, sockaddr)
40
+
41
+ # Receive
42
+ begin
43
+ Timeout.timeout(TIMEOUT_TIME) do
44
+ loop do
45
+ mesg, _ = socket.recvfrom(1500)
46
+ icmp_reply = RecvMessage.new(mesg).to_icmp
47
+
48
+ if icmp.successful_reply?(icmp_reply)
49
+ return true
50
+ elsif icmp_reply.is_type_destination_unreachable?
51
+ logger.warn { "Destination Unreachable!!" }
52
+ return false
53
+ elsif icmp_reply.is_type_redirect?
54
+ logger.warn { "Redirect Required!!" }
55
+ return false
56
+ end
57
+ end
58
+ end
59
+ rescue Timeout::Error => e
60
+ logger.warn { "Timeout Occurred! #{e}" }
61
+ false
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ # @return [Logger]
68
+ def logger
69
+ @logger ||= begin
70
+ logger = Logger.new(STDOUT)
71
+ logger.level = @log_level
72
+ logger
73
+ end
74
+ end
75
+
76
+ # Socket instance
77
+ #
78
+ # @return [Socket]
79
+ def socket
80
+ @socket ||= begin
81
+ socket = Socket.open(
82
+ Socket::AF_INET, # IPv4
83
+ Socket::SOCK_RAW, # RAW Socket
84
+ Socket::IPPROTO_ICMP # ICMP
85
+ )
86
+ socket.bind(Socket.sockaddr_in(nil, @src_ip_addr))
87
+ socket
88
+ end
89
+ end
90
+ end
91
+ end
data/icmp.rb ADDED
@@ -0,0 +1,132 @@
1
+ module SimplePing
2
+ class ICMP
3
+ attr_accessor :id, :seq_number, :data, :type
4
+
5
+ # ICMP TYPES
6
+ TYPE_ICMP_ECHO_REPLY = 0x00
7
+ TYPE_ICMP_DESTINATION_UNREACHABLE = 0x03
8
+ TYPE_ICMP_REDIRECT = 0x05
9
+ TYPE_ICMP_ECHO_REQUEST = 0x08
10
+
11
+ # constructor
12
+ #
13
+ # @param code [Integer] 0x01
14
+ # @param type [Integer] 0x01
15
+ # @param id [Integer] 0x01
16
+ # @param seq_number [Integer] 0x01
17
+ # @param data [String] 0x01
18
+ def initialize(type:, code: 0, id: nil, seq_number: nil, data: nil)
19
+ @type = type
20
+ @code = code
21
+ @id = id || gen_id
22
+ @seq_number = seq_number || gen_seq_number
23
+ @data = data || gen_data
24
+ @checksum = checksum
25
+ end
26
+
27
+ # @return [Boolean]
28
+ def is_type_redirect?
29
+ @type == TYPE_ICMP_REDIRECT
30
+ end
31
+
32
+ # @return [Boolean]
33
+ def is_type_echo?
34
+ @type == TYPE_ICMP_ECHO_REPLY || @type == TYPE_ICMP_ECHO_REQUEST
35
+ end
36
+
37
+ # @return [Boolean]
38
+ def is_type_echo_reply?
39
+ @type == TYPE_ICMP_ECHO_REPLY
40
+ end
41
+
42
+ # @return [Boolean]
43
+ def is_type_destination_unreachable?
44
+ @type == TYPE_ICMP_DESTINATION_UNREACHABLE
45
+ end
46
+
47
+ # Whether the argument ICMP is a reply of this ICMP
48
+ #
49
+ # @param [ICMP]
50
+ # @return [Boolean]
51
+ def successful_reply?(icmp)
52
+ icmp.id == @id && icmp.seq_number == @seq_number && icmp.is_type_echo_reply?
53
+ end
54
+
55
+ # Return the data format for sending with the Socket::send method
56
+ #
57
+ # @return [String]
58
+ def to_trans_data
59
+ bynary_data =
60
+ @type.to_s(2).rjust(8, "0") +
61
+ @code.to_s(2).rjust(8, "0") +
62
+ @checksum.to_s(2).rjust(16, "0") +
63
+ @id.to_s(2).rjust(16, "0") +
64
+ @seq_number.to_s(2).rjust(16, "0")
65
+
66
+ data_byte_arr = bynary_data.scan(/.{1,8}/)
67
+ data_byte_arr.map! { |byte| byte.to_i(2).chr }
68
+ data_byte_arr.join + @data
69
+ end
70
+
71
+ private
72
+
73
+ # Calculate carry in 16bit
74
+ # memo: https://qiita.com/kure/items/fa7e665c2259375d9a81
75
+ #
76
+ # @param num [String] ex: "11001100110100011"
77
+ # @return [Integer]
78
+ def carry_up(num)
79
+ carry_up_num = num.length - 16
80
+ original_value = num[carry_up_num, 16]
81
+ carry_up_value = num[0, carry_up_num]
82
+ sum = original_value.to_i(2) + carry_up_value&.to_i(2)
83
+ sum ^ 0xffff
84
+ end
85
+
86
+ # return checksum value
87
+ # Calculate 1's complement sum for each 16 bits
88
+ # memo: https://qiita.com/kure/items/fa7e665c2259375d9a81
89
+ #
90
+ # @return [Integer]
91
+ def checksum
92
+ # Divide into 16 bits
93
+ # ex: ["pi", "ng"]
94
+ data_arr = @data.scan(/.{1,2}/)
95
+ # Calculate each ASCII code
96
+ # ex: [28777, 28263]
97
+ data_arr_int = data_arr.map do |data|
98
+ (data.bytes[0] << 8) + (data.bytes[1].nil? ? 0 : data.bytes[1])
99
+ end
100
+ data_sum = data_arr_int.sum
101
+
102
+ sum_with_16bit = (@type << 8 + @code) + @id + @seq_number + data_sum
103
+
104
+ # calculate carry
105
+ carry_up(sum_with_16bit.to_s(2).rjust(16, "0"))
106
+ end
107
+
108
+ # generate data
109
+ #
110
+ # TODO: random
111
+ # @return [String]
112
+ def gen_data
113
+ "abcd"
114
+ end
115
+
116
+ # generate ID
117
+ #
118
+ # TODO: random
119
+ # @return [Integer]
120
+ def gen_id
121
+ 0x01
122
+ end
123
+
124
+ # generate sequence number
125
+ #
126
+ # TODO: random
127
+ # @return [Integer]
128
+ def gen_seq_number
129
+ 0x00af
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,5 @@
1
+ require_relative "simple_ping/version"
2
+ require_relative "simple_ping/client"
3
+
4
+ module SimplePing
5
+ end
@@ -0,0 +1,89 @@
1
+ require "logger"
2
+ require "socket"
3
+ require "timeout"
4
+ require_relative "recv_message"
5
+ require_relative "icmp"
6
+
7
+ # Simple Ping (ICMP) client
8
+ # Root privilege required to run
9
+ # ex)
10
+ # require_relative "./simple_ping/client"
11
+ #
12
+ # client = SimplePing::Client.new(src_ip_addr: "192.168.1.100")
13
+ # client.exec(dest_ip_addr: "192.168.1.101") # => true or false
14
+ class SimplePing::Client
15
+ # Wait time for ICMP Reply
16
+ TIMEOUT_TIME = 10
17
+
18
+ # constructor
19
+ #
20
+ # @param src_ip_addr [String] IP address of the interface to send ping, ex: "192.168.1.100"
21
+ def initialize(src_ip_addr:, log_level: Logger::INFO)
22
+ @src_ip_addr = src_ip_addr
23
+ @log_level = log_level
24
+ end
25
+
26
+ # Execute ping(ICMP).
27
+ # Basically, it returns Boolean depending on the result.
28
+ # Exception may be thrown due to unexpected error etc.
29
+ #
30
+ # @param dest_ip_addr [String] IP address of destination to send ping, ex: "192.168.1.101"
31
+ # @param data [String] ICMP Datagram, ex: "abc"
32
+ # @return [Boolean]
33
+ def exec(dest_ip_addr:, data: nil)
34
+ # Transmission
35
+ icmp = SimplePing::ICMP.new(type: SimplePing::ICMP::TYPE_ICMP_ECHO_REQUEST, data: data)
36
+ sockaddr = Socket.sockaddr_in(nil, dest_ip_addr)
37
+ trans_data = icmp.to_trans_data
38
+ socket.send(trans_data, 0, sockaddr)
39
+
40
+ # Receive
41
+ begin
42
+ Timeout.timeout(TIMEOUT_TIME) do
43
+ loop do
44
+ mesg, _ = socket.recvfrom(1500)
45
+ icmp_reply = SimplePing::RecvMessage.new(mesg).to_icmp
46
+
47
+ if icmp.successful_reply?(icmp_reply)
48
+ return true
49
+ elsif icmp_reply.is_type_destination_unreachable?
50
+ logger.warn { "Destination Unreachable!!" }
51
+ return false
52
+ elsif icmp_reply.is_type_redirect?
53
+ logger.warn { "Redirect Required!!" }
54
+ return false
55
+ end
56
+ end
57
+ end
58
+ rescue Timeout::Error => e
59
+ logger.warn { "Timeout Occurred! #{e}" }
60
+ false
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ # @return [Logger]
67
+ def logger
68
+ @logger ||= begin
69
+ logger = Logger.new(STDOUT)
70
+ logger.level = @log_level
71
+ logger
72
+ end
73
+ end
74
+
75
+ # Socket instance
76
+ #
77
+ # @return [Socket]
78
+ def socket
79
+ @socket ||= begin
80
+ socket = Socket.open(
81
+ Socket::AF_INET, # IPv4
82
+ Socket::SOCK_RAW, # RAW Socket
83
+ Socket::IPPROTO_ICMP # ICMP
84
+ )
85
+ socket.bind(Socket.sockaddr_in(nil, @src_ip_addr))
86
+ socket
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,131 @@
1
+
2
+ class SimplePing::ICMP
3
+ attr_accessor :id, :seq_number, :data, :type
4
+
5
+ # ICMP TYPES
6
+ TYPE_ICMP_ECHO_REPLY = 0x00
7
+ TYPE_ICMP_DESTINATION_UNREACHABLE = 0x03
8
+ TYPE_ICMP_REDIRECT = 0x05
9
+ TYPE_ICMP_ECHO_REQUEST = 0x08
10
+
11
+ # constructor
12
+ #
13
+ # @param code [Integer] 0x01
14
+ # @param type [Integer] 0x01
15
+ # @param id [Integer] 0x01
16
+ # @param seq_number [Integer] 0x01
17
+ # @param data [String] 0x01
18
+ def initialize(type:, code: 0, id: nil, seq_number: nil, data: nil)
19
+ @type = type
20
+ @code = code
21
+ @id = id || gen_id
22
+ @seq_number = seq_number || gen_seq_number
23
+ @data = data || gen_data
24
+ @checksum = checksum
25
+ end
26
+
27
+ # @return [Boolean]
28
+ def is_type_redirect?
29
+ @type == TYPE_ICMP_REDIRECT
30
+ end
31
+
32
+ # @return [Boolean]
33
+ def is_type_echo?
34
+ @type == TYPE_ICMP_ECHO_REPLY || @type == TYPE_ICMP_ECHO_REQUEST
35
+ end
36
+
37
+ # @return [Boolean]
38
+ def is_type_echo_reply?
39
+ @type == TYPE_ICMP_ECHO_REPLY
40
+ end
41
+
42
+ # @return [Boolean]
43
+ def is_type_destination_unreachable?
44
+ @type == TYPE_ICMP_DESTINATION_UNREACHABLE
45
+ end
46
+
47
+ # Whether the argument ICMP is a reply of this ICMP
48
+ #
49
+ # @param [ICMP]
50
+ # @return [Boolean]
51
+ def successful_reply?(icmp)
52
+ icmp.id == @id && icmp.seq_number == @seq_number && icmp.is_type_echo_reply?
53
+ end
54
+
55
+ # Return the data format for sending with the Socket::send method
56
+ #
57
+ # @return [String]
58
+ def to_trans_data
59
+ bynary_data =
60
+ @type.to_s(2).rjust(8, "0") +
61
+ @code.to_s(2).rjust(8, "0") +
62
+ @checksum.to_s(2).rjust(16, "0") +
63
+ @id.to_s(2).rjust(16, "0") +
64
+ @seq_number.to_s(2).rjust(16, "0")
65
+
66
+ data_byte_arr = bynary_data.scan(/.{1,8}/)
67
+ data_byte_arr.map! { |byte| byte.to_i(2).chr }
68
+ data_byte_arr.join + @data
69
+ end
70
+
71
+ private
72
+
73
+ # Calculate carry in 16bit
74
+ # memo: https://qiita.com/kure/items/fa7e665c2259375d9a81
75
+ #
76
+ # @param num [String] ex: "11001100110100011"
77
+ # @return [Integer]
78
+ def carry_up(num)
79
+ carry_up_num = num.length - 16
80
+ original_value = num[carry_up_num, 16]
81
+ carry_up_value = num[0, carry_up_num]
82
+ sum = original_value.to_i(2) + carry_up_value&.to_i(2)
83
+ sum ^ 0xffff
84
+ end
85
+
86
+ # return checksum value
87
+ # Calculate 1's complement sum for each 16 bits
88
+ # memo: https://qiita.com/kure/items/fa7e665c2259375d9a81
89
+ #
90
+ # @return [Integer]
91
+ def checksum
92
+ # Divide into 16 bits
93
+ # ex: ["pi", "ng"]
94
+ data_arr = @data.scan(/.{1,2}/)
95
+ # Calculate each ASCII code
96
+ # ex: [28777, 28263]
97
+ data_arr_int = data_arr.map do |data|
98
+ (data.bytes[0] << 8) + (data.bytes[1].nil? ? 0 : data.bytes[1])
99
+ end
100
+ data_sum = data_arr_int.sum
101
+
102
+ sum_with_16bit = (@type << 8 + @code) + @id + @seq_number + data_sum
103
+
104
+ # calculate carry
105
+ carry_up(sum_with_16bit.to_s(2).rjust(16, "0"))
106
+ end
107
+
108
+ # generate data
109
+ #
110
+ # TODO: random
111
+ # @return [String]
112
+ def gen_data
113
+ "abcd"
114
+ end
115
+
116
+ # generate ID
117
+ #
118
+ # TODO: random
119
+ # @return [Integer]
120
+ def gen_id
121
+ 0x01
122
+ end
123
+
124
+ # generate sequence number
125
+ #
126
+ # TODO: random
127
+ # @return [Integer]
128
+ def gen_seq_number
129
+ 0x00af
130
+ end
131
+ end
@@ -0,0 +1,58 @@
1
+ # Class that stores the received message
2
+ # Implements a method to retrieve the ICMP header
3
+ class SimplePing::RecvMessage
4
+ # Code
5
+ #
6
+ # @return [Integer]
7
+ def code
8
+ @mesg[21].bytes[0]
9
+ end
10
+
11
+ # ID
12
+ #
13
+ # @return [Integer]
14
+ def id
15
+ (@mesg[24].bytes[0] << 8) + @mesg[25].bytes[0]
16
+ end
17
+
18
+ # constructor
19
+ #
20
+ # @param [String] mesg
21
+ def initialize(mesg)
22
+ @mesg = mesg
23
+ end
24
+
25
+ # Data
26
+ #
27
+ # @return [String]
28
+ def data
29
+ @mesg[28, @mesg.length.to_i - 28]
30
+ end
31
+
32
+ # sequence numebr
33
+ #
34
+ # @return [Integer]
35
+ def seq_number
36
+ (@mesg[26].bytes[0] << 8) + @mesg[27].bytes[0]
37
+ end
38
+
39
+ # create icmp object
40
+ #
41
+ # @return [SimplePing::ICMP]
42
+ def to_icmp
43
+ icmp = SimplePing::ICMP.new(code: code, type: type)
44
+ if icmp.is_type_echo?
45
+ icmp.id = id
46
+ icmp.seq_number = seq_number
47
+ icmp.data = data
48
+ end
49
+ icmp
50
+ end
51
+
52
+ # Type
53
+ #
54
+ # @return [Integer]
55
+ def type
56
+ @mesg[20].bytes[0]
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module SimplePing
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,60 @@
1
+ module SimplePing
2
+ # Class that stores the received message
3
+ # Implements a method to retrieve the ICMP header
4
+ class RecvMessage
5
+ # Code
6
+ #
7
+ # @return [Integer]
8
+ def code
9
+ @mesg[21].bytes[0]
10
+ end
11
+
12
+ # ID
13
+ #
14
+ # @return [Integer]
15
+ def id
16
+ (@mesg[24].bytes[0] << 8) + @mesg[25].bytes[0]
17
+ end
18
+
19
+ # constructor
20
+ #
21
+ # @param [String] mesg
22
+ def initialize(mesg)
23
+ @mesg = mesg
24
+ end
25
+
26
+ # Data
27
+ #
28
+ # @return [String]
29
+ def data
30
+ @mesg[28, @mesg.length.to_i - 28]
31
+ end
32
+
33
+ # sequence numebr
34
+ #
35
+ # @return [Integer]
36
+ def seq_number
37
+ (@mesg[26].bytes[0] << 8) + @mesg[27].bytes[0]
38
+ end
39
+
40
+ # create icmp object
41
+ #
42
+ # @return [SimplePing::ICMP]
43
+ def to_icmp
44
+ icmp = ICMP.new(code: code, type: type)
45
+ if icmp.is_type_echo?
46
+ icmp.id = id
47
+ icmp.seq_number = seq_number
48
+ icmp.data = data
49
+ end
50
+ icmp
51
+ end
52
+
53
+ # Type
54
+ #
55
+ # @return [Integer]
56
+ def type
57
+ @mesg[20].bytes[0]
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "simple_ping/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "simple_ping"
7
+ spec.version = SimplePing::VERSION
8
+ spec.authors = ["Akira Kure"]
9
+ spec.email = ["kuredev@gmail.com"]
10
+
11
+ spec.summary = %q{A Simpe Ping Client for Ruby.}
12
+ spec.description = %q{A Simpe Ping Client for Ruby.}
13
+ spec.homepage = "https://github.com/kuredev/simple_ping"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/kuredev/simple_ping"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 2.0"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_ping
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Akira Kure
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-26 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: A Simpe Ping Client for Ruby.
56
+ email:
57
+ - kuredev@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - client.rb
73
+ - icmp.rb
74
+ - lib/simple_ping.rb
75
+ - lib/simple_ping/client.rb
76
+ - lib/simple_ping/icmp.rb
77
+ - lib/simple_ping/recv_message.rb
78
+ - lib/simple_ping/version.rb
79
+ - recv_message.rb
80
+ - simple_ping.gemspec
81
+ homepage: https://github.com/kuredev/simple_ping
82
+ licenses:
83
+ - MIT
84
+ metadata:
85
+ homepage_uri: https://github.com/kuredev/simple_ping
86
+ source_code_uri: https://github.com/kuredev/simple_ping
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.0.3
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: A Simpe Ping Client for Ruby.
106
+ test_files: []