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" omm "git.loafle.net/overflow/model/meta"
) )
const ( // const (
ZONE_NETWORK = "192.168.1" // ZONE_NETWORK = "192.168.1"
ZONE_IFACE = "enp3s0" // ZONE_IFACE = "enp3s0"
ZONE_ADDRESS = "101" // ZONE_ADDRESS = "101"
ZONE_MAC = "44:8a:5b:f1:f1:f3" // ZONE_MAC = "44:8a:5b:f1:f1:f3"
) // )
// const ( // const (
// ZONE_NETWORK = "192.168.1" // ZONE_NETWORK = "192.168.1"
@ -28,6 +28,13 @@ const (
// ZONE_MAC = "d0:7e:35:da:26:68" // 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 { func Zone() *omd.Zone {
return omd.NewZone( return omd.NewZone(
ZONE_IFACE, ZONE_IFACE,

View File

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

View File

@ -12,31 +12,76 @@ import (
) )
func Ping(service *omd.Service, pingOption ounp.Option) (ounp.Result, error) { func Ping(service *omd.Service, pingOption ounp.Option) (ounp.Result, error) {
responses := make(map[int]ounp.Response, 0)
summary := &ounp.PingSummary{}
pingResult := &ounp.PingResult{ pingResult := &ounp.PingResult{
Responses: make(map[int]ounp.Response, 0), Responses: responses,
Summary: &ounp.PingSummary{}, Summary: summary,
} }
LOOP: var _sum float64
var _res *ounp.PingResponse
for indexR := 0; indexR < pingOption.GetCount(); indexR++ { for indexR := 0; indexR < pingOption.GetCount(); indexR++ {
conn, err := getConnection(service, pingOption) summary.SendCount = summary.SendCount + 1
if nil != err { _res = &ounp.PingResponse{}
pingResult.Responses[indexR] = &ounp.PingResponse{ if 0 < indexR {
Error: err.Error(), select {
case <-time.After(time.Duration(pingOption.GetDeadline()) * time.Second):
} }
continue LOOP
} }
pingResult.Responses[indexR] = &ounp.PingResponse{ _, elapsedTime, err := sendPing(service, pingOption)
Time: 0, 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 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) { func getConnection(service *omd.Service, pingOption ounp.Option) (net.Conn, error) {
addr := net.JoinHostPort(service.Port.Host.Address, service.Port.PortNumber.String()) addr := net.JoinHostPort(service.Port.Host.Address, service.Port.PortNumber.String())
portType := strings.ToLower(service.Port.MetaPortType.Key) portType := strings.ToLower(service.Port.MetaPortType.Key)

View File

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