aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2025-01-22 23:16:24 +0700
committerShulhan <ms@kilabit.info>2025-01-23 00:00:30 +0700
commit8ad0d7cc01c08f60402d3d18df5dea13bfd94ece (patch)
tree89657439f55005f135180f2a894c37331114de4e
parent536ff758c77cefe8d8c5c3ccdba67087128684ef (diff)
downloadpakakeh.go-8ad0d7cc01c08f60402d3d18df5dea13bfd94ece.tar.xz
all: replace "lib/bytes.AppendXxx" with standard library
Since Go 1.19, package "encoding/binary.BigEndian" support appending byte order.
-rw-r--r--lib/bytes/bytes.go58
-rw-r--r--lib/bytes/bytes_example_test.go119
-rw-r--r--lib/dns/dot_client.go10
-rw-r--r--lib/dns/message.go69
-rw-r--r--lib/dns/rdata_opt.go11
-rw-r--r--lib/dns/rdata_svcb.go38
-rw-r--r--lib/dns/tcp_client.go10
-rw-r--r--lib/play/request.go5
8 files changed, 78 insertions, 242 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index 76bb7874..94e7d402 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -13,64 +13,6 @@ import (
"git.sr.ht/~shulhan/pakakeh.go/lib/ascii"
)
-// AppendInt16 append an int16 value into slice of byte.
-func AppendInt16(data []byte, v int16) []byte {
- data = append(data, byte(v>>8))
- data = append(data, byte(v))
- return data
-}
-
-// AppendInt32 append an int32 value into slice of byte.
-func AppendInt32(data []byte, v int32) []byte {
- data = append(data, byte(v>>24))
- data = append(data, byte(v>>16))
- data = append(data, byte(v>>8))
- data = append(data, byte(v))
- return data
-}
-
-// AppendInt64 append an int64 value into slice of byte.
-func AppendInt64(data []byte, v int64) []byte {
- data = append(data, byte(v>>56))
- data = append(data, byte(v>>48))
- data = append(data, byte(v>>40))
- data = append(data, byte(v>>32))
- data = append(data, byte(v>>24))
- data = append(data, byte(v>>16))
- data = append(data, byte(v>>8))
- data = append(data, byte(v))
- return data
-}
-
-// AppendUint16 append an uint16 value into slice of byte.
-func AppendUint16(data []byte, v uint16) []byte {
- data = append(data, byte(v>>8))
- data = append(data, byte(v))
- return data
-}
-
-// AppendUint32 append an uint32 value into slice of byte.
-func AppendUint32(data []byte, v uint32) []byte {
- data = append(data, byte(v>>24))
- data = append(data, byte(v>>16))
- data = append(data, byte(v>>8))
- data = append(data, byte(v))
- return data
-}
-
-// AppendUint64 append an uint64 value into slice of byte.
-func AppendUint64(data []byte, v uint64) []byte {
- data = append(data, byte(v>>56))
- data = append(data, byte(v>>48))
- data = append(data, byte(v>>40))
- data = append(data, byte(v>>32))
- data = append(data, byte(v>>24))
- data = append(data, byte(v>>16))
- data = append(data, byte(v>>8))
- data = append(data, byte(v))
- return data
-}
-
// CutUntilToken cut text until we found token.
//
// If token found, it will return all bytes before token, position of byte
diff --git a/lib/bytes/bytes_example_test.go b/lib/bytes/bytes_example_test.go
index e8c18d9b..5ca7a1ea 100644
--- a/lib/bytes/bytes_example_test.go
+++ b/lib/bytes/bytes_example_test.go
@@ -6,130 +6,11 @@ package bytes_test
import (
"fmt"
- "math"
"git.sr.ht/~shulhan/pakakeh.go/lib/ascii"
libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
)
-func ExampleAppendInt16() {
- for _, v := range []int16{math.MinInt16, 0xab, 0xabc, math.MaxInt16} {
- out := libbytes.AppendInt16([]byte{}, v)
- fmt.Printf("%6d => %#04x => %#02v\n", v, v, out)
- }
- // Output:
- // -32768 => -0x8000 => []byte{0x80, 0x00}
- // 171 => 0x00ab => []byte{0x00, 0xab}
- // 2748 => 0x0abc => []byte{0x0a, 0xbc}
- // 32767 => 0x7fff => []byte{0x7f, 0xff}
-}
-
-func ExampleAppendInt32() {
- for _, v := range []int32{math.MinInt32, 0xab, 0xabc, math.MaxInt32} {
- out := libbytes.AppendInt32([]byte{}, v)
- fmt.Printf("%11d => %#x => %#v\n", v, v, out)
- }
- // Output:
- // -2147483648 => -0x80000000 => []byte{0x80, 0x0, 0x0, 0x0}
- // 171 => 0xab => []byte{0x0, 0x0, 0x0, 0xab}
- // 2748 => 0xabc => []byte{0x0, 0x0, 0xa, 0xbc}
- // 2147483647 => 0x7fffffff => []byte{0x7f, 0xff, 0xff, 0xff}
-}
-
-func ExampleAppendInt64() {
- var listInt64 = []int64{
- math.MaxInt64,
- 1000,
- math.MinInt64,
- }
-
- var (
- v int64
- out []byte
- )
- for _, v = range listInt64 {
- out = libbytes.AppendInt64(nil, v)
- fmt.Printf("%d\n=> %#x\n=> %#v\n", v, v, out)
- }
- // Output:
- // 9223372036854775807
- // => 0x7fffffffffffffff
- // => []byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
- // 1000
- // => 0x3e8
- // => []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xe8}
- // -9223372036854775808
- // => -0x8000000000000000
- // => []byte{0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
-}
-
-func ExampleAppendUint16() {
- inputs := []uint16{0, 0xab, 0xabc, math.MaxInt16, math.MaxUint16}
- for _, v := range inputs {
- out := libbytes.AppendUint16([]byte{}, v)
- fmt.Printf("%5d => %#04x => %#02v\n", v, v, out)
- }
-
- v := inputs[4] + 1 // MaxUint16 + 1
- out := libbytes.AppendUint16([]byte{}, v)
- fmt.Printf("%5d => %#04x => %#02v\n", v, v, out)
-
- // Output:
- // 0 => 0x0000 => []byte{0x00, 0x00}
- // 171 => 0x00ab => []byte{0x00, 0xab}
- // 2748 => 0x0abc => []byte{0x0a, 0xbc}
- // 32767 => 0x7fff => []byte{0x7f, 0xff}
- // 65535 => 0xffff => []byte{0xff, 0xff}
- // 0 => 0x0000 => []byte{0x00, 0x00}
-}
-
-func ExampleAppendUint32() {
- inputs := []uint32{0, 0xab, 0xabc, math.MaxInt32, math.MaxUint32}
- for _, v := range inputs {
- out := libbytes.AppendUint32([]byte{}, v)
- fmt.Printf("%11d => %#x => %#v\n", v, v, out)
- }
-
- v := inputs[4] + 2 // MaxUint32 + 2
- out := libbytes.AppendUint32([]byte{}, v)
- fmt.Printf("%11d => %#x => %#v\n", v, v, out)
-
- // Output:
- // 0 => 0x0 => []byte{0x0, 0x0, 0x0, 0x0}
- // 171 => 0xab => []byte{0x0, 0x0, 0x0, 0xab}
- // 2748 => 0xabc => []byte{0x0, 0x0, 0xa, 0xbc}
- // 2147483647 => 0x7fffffff => []byte{0x7f, 0xff, 0xff, 0xff}
- // 4294967295 => 0xffffffff => []byte{0xff, 0xff, 0xff, 0xff}
- // 1 => 0x1 => []byte{0x0, 0x0, 0x0, 0x1}
-}
-
-func ExampleAppendUint64() {
- var listUint64 = []uint64{
- math.MaxUint64,
- math.MaxInt64,
- 1000,
- }
-
- var (
- v uint64
- out []byte
- )
- for _, v = range listUint64 {
- out = libbytes.AppendUint64(nil, v)
- fmt.Printf("%d\n=> %#x\n=> %#v\n", v, v, out)
- }
- // Output:
- // 18446744073709551615
- // => 0xffffffffffffffff
- // => []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
- // 9223372036854775807
- // => 0x7fffffffffffffff
- // => []byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
- // 1000
- // => 0x3e8
- // => []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xe8}
-}
-
func ExampleCutUntilToken() {
text := []byte(`\\abc \def \deg`)
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)
diff --git a/lib/play/request.go b/lib/play/request.go
index c38e9674..8d4ae82f 100644
--- a/lib/play/request.go
+++ b/lib/play/request.go
@@ -6,14 +6,13 @@ package play
import (
"crypto/sha256"
+ "encoding/binary"
"encoding/hex"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
-
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
)
const cookieNameSid = `sid`
@@ -79,7 +78,7 @@ func (req *Request) generateSid() string {
var plain = []byte(req.Body)
var epoch = now()
- plain = libbytes.AppendInt64(plain, epoch)
+ plain = binary.BigEndian.AppendUint64(plain, uint64(epoch))
var cipher = sha256.Sum256(plain)
var dst = make([]byte, hex.EncodedLen(len(cipher)))
hex.Encode(dst, cipher[:])