diff options
| author | Shulhan <ms@kilabit.info> | 2025-01-22 23:16:24 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2025-01-23 00:00:30 +0700 |
| commit | 8ad0d7cc01c08f60402d3d18df5dea13bfd94ece (patch) | |
| tree | 89657439f55005f135180f2a894c37331114de4e /lib/dns | |
| parent | 536ff758c77cefe8d8c5c3ccdba67087128684ef (diff) | |
| download | pakakeh.go-8ad0d7cc01c08f60402d3d18df5dea13bfd94ece.tar.xz | |
all: replace "lib/bytes.AppendXxx" with standard library
Since Go 1.19, package "encoding/binary.BigEndian" support appending
byte order.
Diffstat (limited to 'lib/dns')
| -rw-r--r-- | lib/dns/dot_client.go | 10 | ||||
| -rw-r--r-- | lib/dns/message.go | 69 | ||||
| -rw-r--r-- | lib/dns/rdata_opt.go | 11 | ||||
| -rw-r--r-- | lib/dns/rdata_svcb.go | 38 | ||||
| -rw-r--r-- | lib/dns/tcp_client.go | 10 |
5 files changed, 76 insertions, 62 deletions
diff --git a/lib/dns/dot_client.go b/lib/dns/dot_client.go index 7ee2bb29..1fc02920 100644 --- a/lib/dns/dot_client.go +++ b/lib/dns/dot_client.go @@ -1,17 +1,17 @@ -// Copyright 2019, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2019 M. Shulhan <ms@kilabit.info> +// +// SPDX-License-Identifier: BSD-3-Clause package dns import ( "crypto/tls" + "encoding/binary" "errors" "fmt" "net" "time" - libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes" libnet "git.sr.ht/~shulhan/pakakeh.go/lib/net" ) @@ -166,7 +166,7 @@ func (cl *DoTClient) Write(msg []byte) (n int, err error) { packet = make([]byte, 0, 2+lenmsg) ) - packet = libbytes.AppendUint16(packet, uint16(lenmsg)) + packet = binary.BigEndian.AppendUint16(packet, uint16(lenmsg)) packet = append(packet, msg...) n, err = cl.conn.Write(packet) diff --git a/lib/dns/message.go b/lib/dns/message.go index 058a159e..2777ea58 100644 --- a/lib/dns/message.go +++ b/lib/dns/message.go @@ -1,10 +1,11 @@ -// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2018 M. Shulhan <ms@kilabit.info> +// +// SPDX-License-Identifier: BSD-3-Clause package dns import ( + "encoding/binary" "fmt" "net" "strconv" @@ -462,8 +463,10 @@ func (msg *Message) packDomainName(dname []byte, doCompress bool) (n int) { func (msg *Message) packQuestion() { msg.packDomainName([]byte(msg.Question.Name), false) - msg.packet = libbytes.AppendUint16(msg.packet, uint16(msg.Question.Type)) - msg.packet = libbytes.AppendUint16(msg.packet, uint16(msg.Question.Class)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, + uint16(msg.Question.Type)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, + uint16(msg.Question.Class)) } func (msg *Message) packRR(rr *ResourceRecord) { @@ -479,8 +482,8 @@ func (msg *Message) packRR(rr *ResourceRecord) { msg.packDomainName([]byte(rr.Name), true) } - msg.packet = libbytes.AppendUint16(msg.packet, uint16(rr.Type)) - msg.packet = libbytes.AppendUint16(msg.packet, uint16(rr.Class)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(rr.Type)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(rr.Class)) if rr.Type == RecordTypeOPT { rr.TTL = 0 @@ -495,7 +498,7 @@ func (msg *Message) packRR(rr *ResourceRecord) { } rr.idxTTL = uint16(len(msg.packet)) - msg.packet = libbytes.AppendUint32(msg.packet, rr.TTL) + msg.packet = binary.BigEndian.AppendUint32(msg.packet, rr.TTL) msg.packRData(rr) } @@ -548,7 +551,7 @@ func (msg *Message) packRData(rr *ResourceRecord) { } func (msg *Message) packA(rr *ResourceRecord) { - msg.packet = libbytes.AppendUint16(msg.packet, rdataIPv4Size) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, rdataIPv4Size) var ( rrText string @@ -580,7 +583,7 @@ func (msg *Message) packTextAsDomain(rr *ResourceRecord) { ) // Reserve two octets for rdlength - msg.packet = libbytes.AppendUint16(msg.packet, 0) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, 0) n = msg.packDomainName([]byte(rrText), true) libbytes.WriteUint16(msg.packet, off, uint16(n)) @@ -596,18 +599,21 @@ func (msg *Message) packSOA(rr *ResourceRecord) { ) // Reserve two octets for rdlength. - msg.packet = libbytes.AppendUint16(msg.packet, 0) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, 0) n = msg.packDomainName([]byte(rrSOA.MName), true) total = n n = msg.packDomainName([]byte(rrSOA.RName), true) total += n - msg.packet = libbytes.AppendUint32(msg.packet, rrSOA.Serial) - msg.packet = libbytes.AppendInt32(msg.packet, rrSOA.Refresh) - msg.packet = libbytes.AppendInt32(msg.packet, rrSOA.Retry) - msg.packet = libbytes.AppendInt32(msg.packet, rrSOA.Expire) - msg.packet = libbytes.AppendUint32(msg.packet, rrSOA.Minimum) + msg.packet = binary.BigEndian.AppendUint32(msg.packet, rrSOA.Serial) + msg.packet = binary.BigEndian.AppendUint32(msg.packet, + uint32(rrSOA.Refresh)) + msg.packet = binary.BigEndian.AppendUint32(msg.packet, + uint32(rrSOA.Retry)) + msg.packet = binary.BigEndian.AppendUint32(msg.packet, + uint32(rrSOA.Expire)) + msg.packet = binary.BigEndian.AppendUint32(msg.packet, rrSOA.Minimum) total += 20 // Write rdlength. @@ -621,7 +627,7 @@ func (msg *Message) packWKS(rr *ResourceRecord) { ) // Write rdlength. - msg.packet = libbytes.AppendUint16(msg.packet, n) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, n) msg.packet = append(msg.packet, rrWKS.Address[:4]...) msg.packet = append(msg.packet, rrWKS.Protocol) @@ -636,7 +642,7 @@ func (msg *Message) packHINFO(rr *ResourceRecord) { // Write rdlength. n += len(rrHInfo.OS) + 1 - msg.packet = libbytes.AppendUint16(msg.packet, uint16(n)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(n)) msg.packet = append(msg.packet, byte(len(rrHInfo.CPU))) msg.packet = append(msg.packet, rrHInfo.CPU...) msg.packet = append(msg.packet, byte(len(rrHInfo.OS))) @@ -652,7 +658,7 @@ func (msg *Message) packMINFO(rr *ResourceRecord) { ) // Reserve two octets for rdlength. - msg.packet = libbytes.AppendUint16(msg.packet, 0) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, 0) n = msg.packDomainName([]byte(rrMInfo.RMailBox), true) n += msg.packDomainName([]byte(rrMInfo.EmailBox), true) @@ -671,9 +677,10 @@ func (msg *Message) packMX(rr *ResourceRecord) { // Reserve two octets for rdlength. off = uint(len(msg.packet)) - msg.packet = libbytes.AppendUint16(msg.packet, 0) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, 0) - msg.packet = libbytes.AppendInt16(msg.packet, rrMX.Preference) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, + uint16(rrMX.Preference)) n = msg.packDomainName([]byte(rrMX.Exchange), true) @@ -687,7 +694,7 @@ func (msg *Message) packTXT(rr *ResourceRecord) { n = uint16(len(rrText)) ) - msg.packet = libbytes.AppendUint16(msg.packet, n+1) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, n+1) msg.packet = append(msg.packet, byte(n)) msg.packet = append(msg.packet, rrText...) @@ -702,11 +709,11 @@ func (msg *Message) packSRV(rr *ResourceRecord) { ) // Reserve two octets for rdlength - msg.packet = libbytes.AppendUint16(msg.packet, 0) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, 0) - msg.packet = libbytes.AppendUint16(msg.packet, rrSRV.Priority) - msg.packet = libbytes.AppendUint16(msg.packet, rrSRV.Weight) - msg.packet = libbytes.AppendUint16(msg.packet, rrSRV.Port) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, rrSRV.Priority) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, rrSRV.Weight) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, rrSRV.Port) n = msg.packDomainName([]byte(rrSRV.Target), false) + 6 @@ -720,7 +727,7 @@ func (msg *Message) packAAAA(rr *ResourceRecord) { ip = net.ParseIP(rrText) ) - msg.packet = libbytes.AppendUint16(msg.packet, rdataIPv6Size) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, rdataIPv6Size) if ip == nil { msg.packet = append(msg.packet, rrText[:rdataIPv6Size]...) @@ -736,7 +743,7 @@ func (msg *Message) packOPT(rr *ResourceRecord) { rrOPT, _ = rr.Value.(*RDataOPT) var rdata = rrOPT.pack() - msg.packet = libbytes.AppendUint16(msg.packet, uint16(len(rdata))) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(len(rdata))) msg.packet = append(msg.packet, rdata...) } @@ -753,7 +760,7 @@ func (msg *Message) packSVCB(rr *ResourceRecord) { // Reserve two octets for rdlength. var off = uint(len(msg.packet)) - msg.packet = libbytes.AppendUint16(msg.packet, 0) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, 0) var n = svcb.pack(msg) @@ -774,10 +781,10 @@ func (msg *Message) packHTTPS(rr *ResourceRecord) { // Reserve two octets for rdlength. var off = uint(len(msg.packet)) - msg.packet = libbytes.AppendUint16(msg.packet, 0) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, 0) // Priority. - msg.packet = libbytes.AppendUint16(msg.packet, 0) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, 0) var n = msg.packDomainName([]byte(rrhttps.TargetName), false) diff --git a/lib/dns/rdata_opt.go b/lib/dns/rdata_opt.go index fffba7ec..15efcea6 100644 --- a/lib/dns/rdata_opt.go +++ b/lib/dns/rdata_opt.go @@ -1,10 +1,11 @@ -// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2018 M. Shulhan <ms@kilabit.info> +// +// SPDX-License-Identifier: BSD-3-Clause package dns import ( + "encoding/binary" "fmt" "strings" @@ -54,8 +55,8 @@ func (opt *RDataOPT) String() string { func (opt *RDataOPT) pack() (rdata []byte) { var optvar RDataOPTVar for _, optvar = range opt.ListVar { - rdata = libbytes.AppendUint16(rdata, optvar.Code) - rdata = libbytes.AppendUint16(rdata, uint16(len(optvar.Data))) + rdata = binary.BigEndian.AppendUint16(rdata, optvar.Code) + rdata = binary.BigEndian.AppendUint16(rdata, uint16(len(optvar.Data))) rdata = append(rdata, optvar.Data...) } return rdata diff --git a/lib/dns/rdata_svcb.go b/lib/dns/rdata_svcb.go index 306b6f48..c9c16d45 100644 --- a/lib/dns/rdata_svcb.go +++ b/lib/dns/rdata_svcb.go @@ -6,6 +6,7 @@ package dns import ( "bytes" + "encoding/binary" "errors" "fmt" "io" @@ -352,7 +353,7 @@ func (svcb *RDataSVCB) keys() (listKey []int) { func (svcb *RDataSVCB) pack(msg *Message) (n int) { n = len(msg.packet) - msg.packet = libbytes.AppendUint16(msg.packet, svcb.Priority) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, svcb.Priority) _ = msg.packDomainName([]byte(svcb.TargetName), false) @@ -373,7 +374,8 @@ func (svcb *RDataSVCB) pack(msg *Message) (n int) { svcb.packALPN(msg, listValue) case svcbKeyIDNoDefaultALPN: - msg.packet = libbytes.AppendUint16(msg.packet, uint16(svcbKeyIDNoDefaultALPN)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, + uint16(svcbKeyIDNoDefaultALPN)) case svcbKeyIDPort: svcb.packPort(msg, listValue) @@ -397,10 +399,11 @@ func (svcb *RDataSVCB) pack(msg *Message) (n int) { } func (svcb *RDataSVCB) packMandatory(msg *Message, listValue []string) { - msg.packet = libbytes.AppendUint16(msg.packet, uint16(svcbKeyIDMandatory)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, + uint16(svcbKeyIDMandatory)) var total = 2 * len(listValue) - msg.packet = libbytes.AppendUint16(msg.packet, uint16(total)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(total)) var ( listKeyID = make([]int, 0, len(listValue)) @@ -413,7 +416,7 @@ func (svcb *RDataSVCB) packMandatory(msg *Message, listValue []string) { } sort.Ints(listKeyID) for _, keyid = range listKeyID { - msg.packet = libbytes.AppendUint16(msg.packet, uint16(keyid)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(keyid)) } } @@ -426,8 +429,8 @@ func (svcb *RDataSVCB) packALPN(msg *Message, listValue []string) { total += 1 + len(val) } - msg.packet = libbytes.AppendUint16(msg.packet, uint16(svcbKeyIDALPN)) - msg.packet = libbytes.AppendUint16(msg.packet, uint16(total)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(svcbKeyIDALPN)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(total)) for _, val = range listValue { msg.packet = append(msg.packet, byte(len(val))) @@ -446,16 +449,18 @@ func (svcb *RDataSVCB) packPort(msg *Message, listValue []string) { return } - msg.packet = libbytes.AppendUint16(msg.packet, uint16(svcbKeyIDPort)) - msg.packet = libbytes.AppendUint16(msg.packet, 2) - msg.packet = libbytes.AppendUint16(msg.packet, uint16(port)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, + uint16(svcbKeyIDPort)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, 2) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(port)) } func (svcb *RDataSVCB) packIpv4hint(msg *Message, listValue []string) { - msg.packet = libbytes.AppendUint16(msg.packet, uint16(svcbKeyIDIpv4hint)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, + uint16(svcbKeyIDIpv4hint)) var total = 4 * len(listValue) - msg.packet = libbytes.AppendUint16(msg.packet, uint16(total)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(total)) var val string @@ -465,10 +470,11 @@ func (svcb *RDataSVCB) packIpv4hint(msg *Message, listValue []string) { } func (svcb *RDataSVCB) packIpv6hint(msg *Message, listValue []string) { - msg.packet = libbytes.AppendUint16(msg.packet, uint16(svcbKeyIDIpv6hint)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, + uint16(svcbKeyIDIpv6hint)) var total = 16 * len(listValue) - msg.packet = libbytes.AppendUint16(msg.packet, uint16(total)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(total)) var val string @@ -480,8 +486,8 @@ func (svcb *RDataSVCB) packIpv6hint(msg *Message, listValue []string) { func (svcb *RDataSVCB) packGenericValue(keyid int, msg *Message, listValue []string) { var val = strings.Join(listValue, `,`) - msg.packet = libbytes.AppendUint16(msg.packet, uint16(keyid)) - msg.packet = libbytes.AppendUint16(msg.packet, uint16(len(val))) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(keyid)) + msg.packet = binary.BigEndian.AppendUint16(msg.packet, uint16(len(val))) msg.packet = append(msg.packet, []byte(val)...) } diff --git a/lib/dns/tcp_client.go b/lib/dns/tcp_client.go index 324710ae..402540f6 100644 --- a/lib/dns/tcp_client.go +++ b/lib/dns/tcp_client.go @@ -1,17 +1,17 @@ -// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2018 M. Shulhan <ms@kilabit.info> +// +// SPDX-License-Identifier: BSD-3-Clause package dns import ( + "encoding/binary" "errors" "fmt" "io" "net" "time" - libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes" libnet "git.sr.ht/~shulhan/pakakeh.go/lib/net" ) @@ -176,7 +176,7 @@ func (cl *TCPClient) Write(msg []byte) (n int, err error) { packet = make([]byte, 0, 2+lenmsg) ) - packet = libbytes.AppendUint16(packet, uint16(lenmsg)) + packet = binary.BigEndian.AppendUint16(packet, uint16(lenmsg)) packet = append(packet, msg...) n, err = cl.conn.Write(packet) |
