Skip to content

Commit

Permalink
move socket contansts to netlib pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard committed Dec 5, 2023
1 parent 7168eab commit 342cd64
Show file tree
Hide file tree
Showing 29 changed files with 144 additions and 114 deletions.
8 changes: 4 additions & 4 deletions collectors/dnstap.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,17 @@ func (c *Dnstap) Listen() error {
tlsConfig.MinVersion = pkgconfig.TLSVersion[c.config.Collectors.Dnstap.TLSMinVersion]

if len(c.sockPath) > 0 {
listener, err = tls.Listen(pkgconfig.SocketUnix, c.sockPath, tlsConfig)
listener, err = tls.Listen(netlib.SocketUnix, c.sockPath, tlsConfig)
} else {
listener, err = tls.Listen(pkgconfig.SocketTCP, addrlisten, tlsConfig)
listener, err = tls.Listen(netlib.SocketTCP, addrlisten, tlsConfig)
}

} else {
// basic listening
if len(c.sockPath) > 0 {
listener, err = net.Listen(pkgconfig.SocketUnix, c.sockPath)
listener, err = net.Listen(netlib.SocketUnix, c.sockPath)
} else {
listener, err = net.Listen(pkgconfig.SocketTCP, addrlisten)
listener, err = net.Listen(netlib.SocketTCP, addrlisten)
}
}

Expand Down
9 changes: 5 additions & 4 deletions collectors/dnstap_proxifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/netlib"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-framestream"
"github.com/dmachard/go-logger"
Expand Down Expand Up @@ -182,16 +183,16 @@ func (c *DnstapProxifier) Listen() error {
tlsConfig.MinVersion = pkgconfig.TLSVersion[c.config.Collectors.DnstapProxifier.TLSMinVersion]

if len(c.sockPath) > 0 {
listener, err = tls.Listen(pkgconfig.SocketUnix, c.sockPath, tlsConfig)
listener, err = tls.Listen(netlib.SocketUnix, c.sockPath, tlsConfig)
} else {
listener, err = tls.Listen(pkgconfig.SocketTCP, addrlisten, tlsConfig)
listener, err = tls.Listen(netlib.SocketTCP, addrlisten, tlsConfig)
}
} else {
// basic listening
if len(c.sockPath) > 0 {
listener, err = net.Listen(pkgconfig.SocketUnix, c.sockPath)
listener, err = net.Listen(netlib.SocketUnix, c.sockPath)
} else {
listener, err = net.Listen(pkgconfig.SocketTCP, addrlisten)
listener, err = net.Listen(netlib.SocketTCP, addrlisten)
}
}

Expand Down
9 changes: 5 additions & 4 deletions collectors/dnstap_proxifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/loggers"
"github.com/dmachard/go-dnscollector/netlib"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-dnscollector/processors"
"github.com/dmachard/go-framestream"
Expand All @@ -25,19 +26,19 @@ func Test_DnstapProxifier(t *testing.T) {
}{
{
name: "tcp_default",
mode: pkgconfig.SocketTCP,
mode: netlib.SocketTCP,
address: ":6000",
listenPort: 0,
},
{
name: "tcp_custom_port",
mode: pkgconfig.SocketTCP,
mode: netlib.SocketTCP,
address: ":7100",
listenPort: 7100,
},
{
name: "unix_default",
mode: pkgconfig.SocketUnix,
mode: netlib.SocketUnix,
address: "/tmp/dnscollector_relay.sock",
listenPort: 0,
},
Expand All @@ -51,7 +52,7 @@ func Test_DnstapProxifier(t *testing.T) {
if tc.listenPort > 0 {
config.Collectors.DnstapProxifier.ListenPort = tc.listenPort
}
if tc.mode == pkgconfig.SocketUnix {
if tc.mode == netlib.SocketUnix {
config.Collectors.DnstapProxifier.SockPath = tc.address
}

Expand Down
11 changes: 6 additions & 5 deletions collectors/dnstap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/loggers"
"github.com/dmachard/go-dnscollector/netlib"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-dnscollector/processors"
"github.com/dmachard/go-framestream"
Expand All @@ -28,21 +29,21 @@ func Test_DnstapCollector(t *testing.T) {
}{
{
name: "tcp_default",
mode: pkgconfig.SocketTCP,
mode: netlib.SocketTCP,
address: ":6000",
listenPort: 0,
operation: "CLIENT_QUERY",
},
{
name: "tcp_custom_port",
mode: pkgconfig.SocketTCP,
mode: netlib.SocketTCP,
address: ":7000",
listenPort: 7000,
operation: "CLIENT_QUERY",
},
{
name: "unix_default",
mode: pkgconfig.SocketUnix,
mode: netlib.SocketUnix,
address: "/tmp/dnscollector.sock",
listenPort: 0,
operation: "CLIENT_QUERY",
Expand All @@ -57,7 +58,7 @@ func Test_DnstapCollector(t *testing.T) {
if tc.listenPort > 0 {
config.Collectors.Dnstap.ListenPort = tc.listenPort
}
if tc.mode == pkgconfig.SocketUnix {
if tc.mode == netlib.SocketUnix {
config.Collectors.Dnstap.SockPath = tc.address
}

Expand Down Expand Up @@ -136,7 +137,7 @@ func Test_DnstapCollector_CloseFrameStream(t *testing.T) {
go c.Run()

// simulate dns server connection to collector
conn, err := net.Dial(pkgconfig.SocketUnix, "/tmp/dnscollector.sock")
conn, err := net.Dial(netlib.SocketUnix, "/tmp/dnscollector.sock")
if err != nil {
t.Error("could not connect: ", err)
}
Expand Down
3 changes: 2 additions & 1 deletion collectors/file_tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/netlib"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-dnscollector/transformers"
"github.com/dmachard/go-logger"
Expand Down Expand Up @@ -220,7 +221,7 @@ RUN_LOOP:
if familyIndex != -1 {
dm.NetworkInfo.Family = matches[familyIndex]
} else {
dm.NetworkInfo.Family = pkgconfig.ProtoIPv4
dm.NetworkInfo.Family = netlib.ProtoIPv4
}

protocolIndex := re.SubexpIndex("protocol")
Expand Down
4 changes: 2 additions & 2 deletions collectors/powerdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ func (c *ProtobufPowerDNS) Listen() error {
// update tls min version according to the user config
tlsConfig.MinVersion = pkgconfig.TLSVersion[c.config.Collectors.PowerDNS.TLSMinVersion]

listener, err = tls.Listen(pkgconfig.SocketTCP, addrlisten, tlsConfig)
listener, err = tls.Listen(netlib.SocketTCP, addrlisten, tlsConfig)
} else {
listener, err = net.Listen(pkgconfig.SocketTCP, addrlisten)
listener, err = net.Listen(netlib.SocketTCP, addrlisten)
}
// something is wrong ?
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion collectors/powerdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/loggers"
"github.com/dmachard/go-dnscollector/netlib"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-logger"
)
Expand All @@ -20,7 +21,7 @@ func TestPowerDNS_Run(t *testing.T) {
}
go c.Run()

conn, err := net.Dial(pkgconfig.SocketTCP, ":6001")
conn, err := net.Dial(netlib.SocketTCP, ":6001")
if err != nil {
t.Error("could not connect to TCP server: ", err)
}
Expand Down
5 changes: 3 additions & 2 deletions collectors/sniffer_xdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/perf"
"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/netlib"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-dnscollector/processors"
"github.com/dmachard/go-dnscollector/xdp"
Expand Down Expand Up @@ -239,9 +240,9 @@ func (c *XDPSniffer) Run() {
dm.NetworkInfo.ResponsePort = fmt.Sprint(pkt.DstPort)

if pkt.IpVersion == 0x0800 {
dm.NetworkInfo.Family = pkgconfig.ProtoIPv4
dm.NetworkInfo.Family = netlib.ProtoIPv4
} else {
dm.NetworkInfo.Family = pkgconfig.ProtoIPv6
dm.NetworkInfo.Family = netlib.ProtoIPv6
}

if pkt.IpProto == 0x11 {
Expand Down
5 changes: 3 additions & 2 deletions collectors/tzsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"syscall"

"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/netlib"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-dnscollector/processors"
"github.com/dmachard/go-logger"
Expand Down Expand Up @@ -193,14 +194,14 @@ func (c *TZSPSniffer) Run() {
for _, layertyp := range decodedLayers {
switch layertyp {
case layers.LayerTypeIPv4:
dm.NetworkInfo.Family = pkgconfig.ProtoIPv4
dm.NetworkInfo.Family = netlib.ProtoIPv4
dm.NetworkInfo.QueryIP = ip4.SrcIP.String()
dm.NetworkInfo.ResponseIP = ip4.DstIP.String()

case layers.LayerTypeIPv6:
dm.NetworkInfo.QueryIP = ip6.SrcIP.String()
dm.NetworkInfo.ResponseIP = ip6.DstIP.String()
dm.NetworkInfo.Family = pkgconfig.ProtoIPv6
dm.NetworkInfo.Family = netlib.ProtoIPv6

case layers.LayerTypeUDP:
dm.NetworkInfo.QueryPort = fmt.Sprint(int(udp.SrcPort))
Expand Down
25 changes: 13 additions & 12 deletions dnsutils/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"time"

"github.com/dmachard/go-dnscollector/netlib"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-dnstap-protobuf"
"github.com/google/gopacket"
Expand Down Expand Up @@ -658,7 +659,7 @@ func (dm *DNSMessage) ToDNSTap() ([]byte, error) {
mt := dnstap.Message_Type(dnstap.Message_Type_value[dm.DNSTap.Operation])

var sf dnstap.SocketFamily
if ipNet, valid := pkgconfig.IPToInet[dm.NetworkInfo.Family]; valid {
if ipNet, valid := netlib.IPToInet[dm.NetworkInfo.Family]; valid {
sf = dnstap.SocketFamily(dnstap.SocketFamily_value[ipNet])
}
sp := dnstap.SocketProtocol(dnstap.SocketProtocol_value[dm.NetworkInfo.Protocol])
Expand Down Expand Up @@ -689,15 +690,15 @@ func (dm *DNSMessage) ToDNSTap() ([]byte, error) {
msg.SocketProtocol = &sp

reqIP := net.ParseIP(dm.NetworkInfo.QueryIP)
if dm.NetworkInfo.Family == pkgconfig.ProtoIPv4 {
if dm.NetworkInfo.Family == netlib.ProtoIPv4 {
msg.QueryAddress = reqIP.To4()
} else {
msg.QueryAddress = reqIP.To16()
}
msg.QueryPort = &qport

rspIP := net.ParseIP(dm.NetworkInfo.ResponseIP)
if dm.NetworkInfo.Family == pkgconfig.ProtoIPv4 {
if dm.NetworkInfo.Family == netlib.ProtoIPv4 {
msg.ResponseAddress = rspIP.To4()
} else {
msg.ResponseAddress = rspIP.To16()
Expand Down Expand Up @@ -744,11 +745,11 @@ func (dm *DNSMessage) ToPacketLayer() ([]gopacket.SerializableLayer, error) {

// set source and destination IP
switch dm.NetworkInfo.Family {
case pkgconfig.ProtoIPv4:
case netlib.ProtoIPv4:
eth.EthernetType = layers.EthernetTypeIPv4
ip4.SrcIP = net.ParseIP(srcIP)
ip4.DstIP = net.ParseIP(dstIP)
case pkgconfig.ProtoIPv6:
case netlib.ProtoIPv6:
eth.EthernetType = layers.EthernetTypeIPv6
ip6.SrcIP = net.ParseIP(srcIP)
ip6.DstIP = net.ParseIP(dstIP)
Expand All @@ -766,11 +767,11 @@ func (dm *DNSMessage) ToPacketLayer() ([]gopacket.SerializableLayer, error) {

// update iplayer
switch dm.NetworkInfo.Family {
case pkgconfig.ProtoIPv4:
case netlib.ProtoIPv4:
ip4.Protocol = layers.IPProtocolUDP
udp.SetNetworkLayerForChecksum(ip4)
pkt = append(pkt, gopacket.Payload(dm.DNS.Payload), udp, ip4)
case pkgconfig.ProtoIPv6:
case netlib.ProtoIPv6:
ip6.NextHeader = layers.IPProtocolUDP
udp.SetNetworkLayerForChecksum(ip6)
pkt = append(pkt, gopacket.Payload(dm.DNS.Payload), udp, ip6)
Expand All @@ -789,11 +790,11 @@ func (dm *DNSMessage) ToPacketLayer() ([]gopacket.SerializableLayer, error) {

// update iplayer
switch dm.NetworkInfo.Family {
case pkgconfig.ProtoIPv4:
case netlib.ProtoIPv4:
ip4.Protocol = layers.IPProtocolTCP
tcp.SetNetworkLayerForChecksum(ip4)
pkt = append(pkt, gopacket.Payload(append(dnsLengthField, dm.DNS.Payload...)), tcp, ip4)
case pkgconfig.ProtoIPv6:
case netlib.ProtoIPv6:
ip6.NextHeader = layers.IPProtocolTCP
tcp.SetNetworkLayerForChecksum(ip6)
pkt = append(pkt, gopacket.Payload(append(dnsLengthField, dm.DNS.Payload...)), tcp, ip6)
Expand All @@ -807,11 +808,11 @@ func (dm *DNSMessage) ToPacketLayer() ([]gopacket.SerializableLayer, error) {

// update iplayer
switch dm.NetworkInfo.Family {
case pkgconfig.ProtoIPv4:
case netlib.ProtoIPv4:
ip4.Protocol = layers.IPProtocolUDP
udp.SetNetworkLayerForChecksum(ip4)
pkt = append(pkt, gopacket.Payload(dm.DNS.Payload), udp, ip4)
case pkgconfig.ProtoIPv6:
case netlib.ProtoIPv6:
ip6.NextHeader = layers.IPProtocolUDP
udp.SetNetworkLayerForChecksum(ip6)
pkt = append(pkt, gopacket.Payload(dm.DNS.Payload), udp, ip6)
Expand Down Expand Up @@ -860,7 +861,7 @@ func GetFakeDNSMessageWithPayload() DNSMessage {
dnsquestion, _ := dnsmsg.Pack()

dm := GetFakeDNSMessage()
dm.NetworkInfo.Family = pkgconfig.ProtoIPv4
dm.NetworkInfo.Family = netlib.ProtoIPv4
dm.NetworkInfo.Protocol = pkgconfig.ProtoUDP
dm.DNS.Payload = dnsquestion
dm.DNS.Length = len(dnsquestion)
Expand Down
13 changes: 7 additions & 6 deletions loggers/dnstapclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/netlib"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-dnscollector/transformers"
"github.com/dmachard/go-framestream"
Expand Down Expand Up @@ -64,10 +65,10 @@ func (c *DnstapSender) ReadConfig() {

// begin backward compatibility
if c.config.Loggers.DNSTap.TLSSupport {
c.transport = pkgconfig.SocketTLS
c.transport = netlib.SocketTLS
}
if len(c.config.Loggers.DNSTap.SockPath) > 0 {
c.transport = pkgconfig.SocketUnix
c.transport = netlib.SocketUnix
}
// end

Expand Down Expand Up @@ -139,19 +140,19 @@ func (c *DnstapSender) ConnectToRemote() {
var err error

switch c.transport {
case pkgconfig.SocketUnix:
case netlib.SocketUnix:
address = c.config.Loggers.DNSTap.RemoteAddress
if len(c.config.Loggers.DNSTap.SockPath) > 0 {
address = c.config.Loggers.DNSTap.SockPath
}
c.LogInfo("connecting to %s://%s", c.transport, address)
conn, err = net.DialTimeout(c.transport, address, connTimeout)

case pkgconfig.SocketTCP:
case netlib.SocketTCP:
c.LogInfo("connecting to %s://%s", c.transport, address)
conn, err = net.DialTimeout(c.transport, address, connTimeout)

case pkgconfig.SocketTLS:
case netlib.SocketTLS:
c.LogInfo("connecting to %s://%s", c.transport, address)

var tlsConfig *tls.Config
Expand All @@ -167,7 +168,7 @@ func (c *DnstapSender) ConnectToRemote() {
tlsConfig, err = pkgconfig.TLSClientConfig(tlsOptions)
if err == nil {
dialer := &net.Dialer{Timeout: connTimeout}
conn, err = tls.DialWithDialer(dialer, pkgconfig.SocketTCP, address, tlsConfig)
conn, err = tls.DialWithDialer(dialer, netlib.SocketTCP, address, tlsConfig)
}
default:
c.logger.Fatal("logger=dnstap - invalid transport:", c.transport)
Expand Down
Loading

0 comments on commit 342cd64

Please sign in to comment.