This commit is contained in:
crusader 2018-09-22 01:18:59 +09:00
parent 099bf25fc2
commit 159f314307
4 changed files with 80 additions and 22 deletions

View File

@ -7,12 +7,12 @@ import (
omm "git.loafle.net/overflow/model/meta"
)
const (
ZONE_NETWORK = "192.168.1"
ZONE_IFACE = "enp3s0"
ZONE_ADDRESS = "101"
ZONE_MAC = "44:8a:5b:f1:f1:f3"
)
// const (
// ZONE_NETWORK = "192.168.1"
// ZONE_IFACE = "enp3s0"
// ZONE_ADDRESS = "101"
// ZONE_MAC = "44:8a:5b:f1:f1:f3"
// )
// const (
// ZONE_NETWORK = "192.168.1"
@ -28,6 +28,13 @@ const (
// ZONE_MAC = "d0:7e:35:da:26:68"
// )
const (
ZONE_NETWORK = "192.168.35"
ZONE_IFACE = "\\Device\\NPF_{51459A06-A513-4202-9D79-93A4E394566A}"
ZONE_ADDRESS = "179"
ZONE_MAC = "08:00:27:f0:dc:35"
)
func Zone() *omd.Zone {
return omd.NewZone(
ZONE_IFACE,

View File

@ -33,8 +33,8 @@ func Test_scanV4(t *testing.T) {
targetHost := __test.Host(
"atGame",
"1",
"00:11:32:7f:20:61",
"234",
"d0:7e:35:da:26:68",
)
type args struct {

View File

@ -12,31 +12,76 @@ import (
)
func Ping(service *omd.Service, pingOption ounp.Option) (ounp.Result, error) {
responses := make(map[int]ounp.Response, 0)
summary := &ounp.PingSummary{}
pingResult := &ounp.PingResult{
Responses: make(map[int]ounp.Response, 0),
Summary: &ounp.PingSummary{},
Responses: responses,
Summary: summary,
}
LOOP:
var _sum float64
var _res *ounp.PingResponse
for indexR := 0; indexR < pingOption.GetCount(); indexR++ {
conn, err := getConnection(service, pingOption)
if nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
summary.SendCount = summary.SendCount + 1
_res = &ounp.PingResponse{}
if 0 < indexR {
select {
case <-time.After(time.Duration(pingOption.GetDeadline()) * time.Second):
}
continue LOOP
}
pingResult.Responses[indexR] = &ounp.PingResponse{
Time: 0,
_, elapsedTime, err := sendPing(service, pingOption)
if 0 == indexR {
summary.MinTime = elapsedTime
summary.MaxTime = elapsedTime
}
if nil == err {
_sum = _sum + elapsedTime
summary.ReceiveCount = summary.ReceiveCount + 1
if summary.MinTime > elapsedTime {
summary.MinTime = elapsedTime
}
conn.Close()
if summary.MaxTime < elapsedTime {
summary.MaxTime = elapsedTime
}
_res.Time = elapsedTime
} else {
_res.Error = err.Error()
}
pingResult.Responses[indexR] = _res
}
if 0 == summary.ReceiveCount {
summary.AvgTime = 0
summary.LossPercent = 100
} else {
summary.AvgTime = _sum / float64(summary.ReceiveCount)
summary.LossPercent = (float32(summary.SendCount) - float32(summary.ReceiveCount)) / float32(summary.SendCount) * float32(100)
}
return pingResult, nil
}
func sendPing(service *omd.Service, pingOption ounp.Option) (ttl int, elapsedTime float64, err error) {
startTime := time.Now()
var conn net.Conn
conn, err = getConnection(service, pingOption)
if nil != err {
return
}
defer conn.Close()
elapsed := time.Since(startTime)
elapsedTime = elapsed.Seconds() * 1E3
return
}
func getConnection(service *omd.Service, pingOption ounp.Option) (net.Conn, error) {
addr := net.JoinHostPort(service.Port.Host.Address, service.Port.PortNumber.String())
portType := strings.ToLower(service.Port.MetaPortType.Key)

View File

@ -1,6 +1,8 @@
package connection
import (
"encoding/json"
"log"
"net"
"reflect"
"testing"
@ -14,13 +16,13 @@ import (
func TestPing(t *testing.T) {
targetHost := __test.Host(
"",
"99",
"00:25:b3:fa:ca:9b",
"234",
"d0:7e:35:da:26:68",
)
targetPort := __test.Port(
targetHost,
7,
139,
)
type args struct {
@ -54,6 +56,10 @@ func TestPing(t *testing.T) {
t.Errorf("Ping() error = %v, wantErr %v", err, tt.wantErr)
return
}
_buf, _ := json.Marshal(got)
log.Print(string(_buf))
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Ping() = %v, want %v", got, tt.want)
}