wifictld-analyzer/capture/socket_msg.go

133 lines
3.4 KiB
Go
Raw Normal View History

package capture
2018-06-02 01:00:54 +02:00
import (
"encoding/binary"
"fmt"
"time"
2019-02-28 16:24:29 +01:00
"github.com/bdlm/log"
"dev.sum7.eu/genofire/wifictld-analyzer/data"
2018-06-02 01:00:54 +02:00
)
2018-06-03 20:37:52 +02:00
var DEBUG = false
2018-06-02 01:00:54 +02:00
// SocketMSGType kind of packages
type SocketMSGType uint32
const (
SocketMSGTypeRequest SocketMSGType = (1 << 0)
SocketMSGTypeResponse SocketMSGType = (1 << 1)
SocketMSGTypeClient SocketMSGType = (1 << 2)
SocketMSGTypeStats SocketMSGType = (1 << 3)
)
func (a SocketMSGType) Is(b SocketMSGType) bool {
2018-06-03 20:37:52 +02:00
if DEBUG {
2019-02-28 17:26:55 +01:00
log.Debugf("SocketType: %x & %x = %x -> %t", a, b, (a & b), (a&b) > 0)
2018-06-03 20:37:52 +02:00
}
2018-06-02 01:00:54 +02:00
return (a & b) > 0
}
// SocketMSG package of wifictld format
type SocketMSG struct {
2019-03-01 10:54:19 +01:00
Types SocketMSGType `json:"types"`
Client *WifiClient `json:"client,omitempty"`
2018-06-02 01:00:54 +02:00
}
func NewSocketMSG(obj []byte) (*SocketMSG, error) {
msg := &SocketMSG{}
if err := msg.Unmarshal(obj); err != nil {
return nil, err
}
return msg, nil
}
func (msg *SocketMSG) Marshal() ([]byte, error) {
obj := make([]byte, MaxDataGramSize)
pos := 0
binary.BigEndian.PutUint32(obj[pos:(pos+4)], uint32(msg.Types))
pos += 4
if msg.Types.Is(SocketMSGTypeClient) {
2019-03-08 15:53:45 +01:00
obj[pos] = msg.Client.Addr[0]
2018-06-02 01:00:54 +02:00
pos++
2019-03-08 15:53:45 +01:00
obj[pos] = msg.Client.Addr[1]
2018-06-02 01:00:54 +02:00
pos++
2019-03-08 15:53:45 +01:00
obj[pos] = msg.Client.Addr[2]
2018-06-02 01:00:54 +02:00
pos++
2019-03-08 15:53:45 +01:00
obj[pos] = msg.Client.Addr[3]
2018-06-02 01:00:54 +02:00
pos++
2019-03-08 15:53:45 +01:00
obj[pos] = msg.Client.Addr[4]
2018-06-02 01:00:54 +02:00
pos++
2019-03-08 15:53:45 +01:00
obj[pos] = msg.Client.Addr[5]
2018-06-02 01:00:54 +02:00
pos++
binary.BigEndian.PutUint32(obj[pos:(pos+4)], uint32(msg.Client.Time.Unix()))
pos += 4
binary.BigEndian.PutUint16(obj[pos:(pos+2)], msg.Client.TryProbe)
pos += 2
binary.BigEndian.PutUint16(obj[pos:(pos+2)], msg.Client.TryAuth)
pos += 2
if msg.Client.Connected {
obj[pos] = byte(1)
}
pos++
if msg.Client.Authed {
obj[pos] = byte(1)
}
pos++
binary.BigEndian.PutUint16(obj[pos:(pos+2)], msg.Client.FreqHighest)
pos += 2
binary.BigEndian.PutUint16(obj[pos:(pos+2)], uint16(msg.Client.SignalLowFreq))
pos += 2
binary.BigEndian.PutUint16(obj[pos:(pos+2)], uint16(msg.Client.SignalHighFreq))
//pos += 2
}
return obj, nil
}
func (msg *SocketMSG) Unmarshal(obj []byte) error {
2018-06-03 20:37:52 +02:00
if DEBUG {
log.Debugf("hex unmarshal: %x", obj)
}
2018-06-02 01:00:54 +02:00
pos := 0
msg.Types = SocketMSGType(binary.BigEndian.Uint32(obj[pos:(pos + 4)]))
pos += 4
if msg.Types.Is(SocketMSGTypeClient) {
msg.Client = &WifiClient{
2019-03-08 15:53:45 +01:00
Addr: data.HardwareAddr(obj[pos:(pos + 6)]),
2018-06-02 01:00:54 +02:00
Time: time.Unix(int64(binary.BigEndian.Uint32(obj[(pos+6):(pos+10)])), 0),
TryProbe: binary.BigEndian.Uint16(obj[(pos + 10):(pos + 12)]),
TryAuth: binary.BigEndian.Uint16(obj[(pos + 12):(pos + 14)]),
Connected: (obj[(pos+14)] == 1),
Authed: (obj[(pos+15)] == 1),
FreqHighest: binary.BigEndian.Uint16(obj[(pos + 16):(pos + 18)]),
SignalLowFreq: int16(binary.BigEndian.Uint16(obj[(pos + 18):(pos + 20)])),
SignalHighFreq: int16(binary.BigEndian.Uint16(obj[(pos + 20):(pos + 22)])),
}
//pos += 22
}
return nil
}
func (msg *SocketMSG) String() string {
answer := ""
if msg.Types.Is(SocketMSGTypeRequest) {
answer += "request "
}
if msg.Types.Is(SocketMSGTypeResponse) {
answer += "response "
}
if msg.Types.Is(SocketMSGTypeClient) {
answer += fmt.Sprintf("client(mac=%s freq=%d ssi_l=%d ssi_h=%d tprobe=%d tauth=%d authed=%v time=%s) ", msg.Client.Addr, msg.Client.FreqHighest, msg.Client.SignalLowFreq, msg.Client.SignalHighFreq, msg.Client.TryProbe, msg.Client.TryAuth, msg.Client.Authed, msg.Client.Time.String())
}
return answer
}