service_matcher-go/nbss/nbss.go
crusader 45121e83ae ing
2018-09-03 16:56:41 +09:00

111 lines
2.1 KiB
Go

package nbss
import (
"bytes"
"encoding/binary"
osm "git.loafle.net/overflow/service_matcher-go"
)
const (
NBSS_SESSION_REQUEST uint8 = 0x81
NBSS_POSITIVE_SESSION_RESPONSE uint8 = 0x82
NBSS_NEGATIVE_SESSION_RESPONSE uint8 = 0x83
)
type NBSS struct {
MsgType uint8
Flags uint8 //0-6 : Reserved, must be zero. 7 : Length extension.
Length uint16
CalledNameLen uint8
CalledName [16]uint16
_ uint8
CallingNameLen uint8
CallingName [16]uint16
_ uint8
}
type NBSSMatcher struct {
osm.Matchers
}
func (t *NBSSMatcher) Key() string {
return "NBSS"
}
func (t *NBSSMatcher) Name() string {
return "NBSS"
}
func (t *NBSSMatcher) Meta() osm.Metadata {
return nil
}
func (t *NBSSMatcher) IsPrePacket() bool {
return false
}
func (t *NBSSMatcher) HasResponse(index int) bool {
return true
}
func (t *NBSSMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
return false
}
func (t *NBSSMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error {
if packet == nil || !packet.Valid() {
return osm.NoPacketReceivedError()
}
reader := new(bytes.Buffer)
reader.Write(packet.Buffer)
n := NBSS{}
if err := binary.Read(reader, binary.LittleEndian, &n); err != nil {
return osm.NotMatchedError()
}
switch n.MsgType {
case NBSS_POSITIVE_SESSION_RESPONSE, NBSS_NEGATIVE_SESSION_RESPONSE:
default:
return osm.NotMatchedError()
}
return nil
}
func NewMatcher() osm.Matcher {
m := &NBSSMatcher{}
tempBuf := new(bytes.Buffer)
binary.Write(tempBuf, binary.BigEndian, NBSS{})
query := NBSS{
MsgType: NBSS_SESSION_REQUEST,
Flags: 0x00,
Length: 0x4400,
CalledNameLen: 0x20,
CallingNameLen: 0x20,
}
query.CalledName[0] = 0x4D45 // L
query.CalledName[1] = 0x4745 // F
query.CallingName[0] = 0x4D45
query.CallingName[1] = 0x4745
for i := 2; i < 16; i++ {
query.CalledName[i] = 0x4143 //Space
query.CallingName[i] = 0x4143
}
writer := new(bytes.Buffer)
binary.Write(writer, binary.LittleEndian, query)
m.AddPacket(osm.NewPacket(writer.Bytes(), writer.Len()))
return m
}