-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingerlinux.go
215 lines (180 loc) · 6.62 KB
/
pingerlinux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package goping
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"net"
"os"
"sync"
"syscall"
"time"
)
type linuxPinger struct {
Sweep time.Duration
mypid int
stopListen chan struct{}
seqEchoReplyChanArray []chan EchoReply //Array to send EchoReply to the caller
done chan struct{} //Channel to stop the pongreceiver
mu sync.Mutex
}
func newLinuxPinger(sweep time.Duration) linuxPinger {
if sweep == 0 {
debug.Println("Setting sweep to 1 nanosecond")
sweep = 1 * time.Nanosecond
}
return linuxPinger{
Sweep: sweep,
seqEchoReplyChanArray: make([]chan EchoReply, g_SEQUENCE.max),
mypid: os.Getpid(),
done: make(chan struct{}),
}
}
//Send Icmp Packets over the network
//Receives request from the Ping Channel
//Send Icmp Packet through the network
//Puts the Request back on the pong channel. The caller will verify the response and timeout
func (p *linuxPinger) Start(ping chan EchoRequest) {
//Create a raw socket to read icmp packets
debug.Printf("Creating the socket to send pings\n")
fd, _ := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_ICMP)
//Tells the socket we will build the ip header
debug.Printf("Set the options to warn the kernel that the program will build the pi header\n")
if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1); err != nil {
log.Fatal("Could not set sock opt syscall HDRINCL")
}
//Starts the pong receiver in a goroutine
debug.Printf("Starts the pong receiver in a go routine")
go p.pongReceiver()
var pingcounter int64
//Process the ping requests
//1) should get the pongchan channel from the received Ping pongchan array, using the pi.sent-1 as the array index
//2) Use local logic to make the ping. If anything goes wrong should set the err field of the received Ping object
//3) Should always return the Ping object without block waitng for a reply. The reply will be checked later through the pongchan channel
for echo := range ping {
pingcounter++
//Extracting the sequence field from the Echo Request
sequence := echo.GetSequence()
debug.Println("Received Echo Request", echo.Id, sequence)
//Storing the pongchannel in the pongchanarray indexed by the sequence number
//the pongchan is used to send the echoreply back (EchoReply object)
p.seqEchoReplyChanArray[sequence] = echo.chreply
//Get the marshalled bytes
pkt := append(echo.bipv4, echo.bicmp...)
//This information will be read by the listener in order to build the EchoReply object
when := time.Now()
info.Println(pkt)
//Sending the packet through the network
if err := syscall.Sendto(fd, pkt, 0, echo.tosockaddr); err != nil {
echo.chreply <- EchoReply{
When: time.Now(),
Err: fmt.Errorf("Could not send packet over socket: %v", err),
}
echo.chwhen <- time.Now()
continue
}
//Send the time the ping was generated
echo.chwhen <- when
//This permits other goroutines to run and control the smoothness of te pings over the devices and the network
debug.Printf("Sleeping for %v [%v]\n", sequence, p.Sweep)
time.Sleep(p.Sweep)
}
info.Printf("Number of pings sent %v\n", pingcounter)
}
//Verify if the packet is a reply from this process PID
//Get the Packet Sequence Number
//Get the time when the packet arrived in the Kernel using the SO_TIMESTAMP control message
//Create a EchoReply object and send it through the channel inside pingarray using the seq field as index
func (p *linuxPinger) pongReceiver() {
//Create a raw socket to read icmp packets
fd, _ := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_ICMP)
//Set the option to receive the kernel timestamp from each received message
if err := syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TIMESTAMP, 1); err != nil {
log.Fatal("Could not set sock opt syscall")
}
// if err := syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_RCVBUF, 1024*1024*1024); err != nil {
// log.Fatal("Could not set sock opt syscall Increase buffer")
// }
//Create an ip address to listen for
var addr syscall.Sockaddr = &syscall.SockaddrInet4{
Port: 0,
Addr: [4]byte{0, 0, 0, 0},
}
//Bind the created socket with the address to listen for
if err := syscall.Bind(fd, addr); err != nil {
log.Fatal("Could not bind")
}
for {
//Buffer to receive the ping packet
buf := make([]byte, 1024)
//Buffer to receive the control message
oob := make([]byte, 64)
//Receives a message from the socket sent by the kernel
//Continue on error
pktsz, oobn, _, peer, err := syscall.Recvmsg(fd, buf, oob, 0)
if err != nil {
severe.Printf("Error reading icmp packet from the socket: %v\n", err)
continue
}
info.Println(buf)
//Exracting ip header from the packet
iph := buf[:20]
//Extracting icmp message from thr packet
msg := buf[20:pktsz]
//Find the first 8 bytes from the Echo Request message
emsg := msg[:8]
//Finds if it was an error message, changes emsg accordingly
//The default section ignores the message and tries to read other packets
var icmperror error
switch msg[0] {
case 0:
case 8:
continue
default:
emsg = msg[28:]
icmperror = fmt.Errorf("There was an error with code %v", msg[0])
}
//Extracts id and seq
pid := int(uint16(emsg[4])<<8 | uint16(emsg[5]))
seq := int(uint16(emsg[6])<<8 | uint16(emsg[7]))
//Ignores processing if id is different from mypid
if pid != p.mypid {
continue
}
//Build the EchoReply Object to be sent. The Echo End is filled with time.Now in case the timestamp
//from SO_TIMESTAMP fails
b := peer.(*syscall.SockaddrInet4).Addr[:]
reply := EchoReply{
From: net.IPv4(b[0], b[1], b[2], b[3]),
When: time.Now(),
Err: icmperror,
iph: iph,
imh: msg,
}
//Parse the received control message until the oobn size
cmsgs, err := syscall.ParseSocketControlMessage(oob[:oobn])
if err != nil {
reply.Err = os.NewSyscallError("parse socket control message", err)
}
//Iterate over the control messages
for _, m := range cmsgs {
//Continue if control message is not syscall.SOL_SOCKET
if m.Header.Level != syscall.SOL_SOCKET {
continue
}
//Control Message is SOL_SOCKET, Verifyng if syscall is SO_TIMESTAMP
switch m.Header.Type {
case syscall.SO_TIMESTAMP:
//Found Timestamp. Using binary package to read from
var tv syscall.Timeval
binary.Read(bytes.NewBuffer(m.Data), binary.LittleEndian, &tv)
reply.When = time.Unix(tv.Unix())
}
}
//Send the pong object back to the caller
go func() {
p.seqEchoReplyChanArray[seq] <- reply
debug.Println("Sent pong reference to the ping.pongchan reference ", p.seqEchoReplyChanArray[seq], seq)
}()
}
}