mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-07-13 11:54:46 -04:00
Fix string length checks, sortof
This commit is contained in:
@ -31,28 +31,7 @@ type I2PString []byte
|
||||
// Length returns the length specified in the first byte.
|
||||
// Returns error if the specified does not match the actual length or the string is otherwise invalid.
|
||||
func (str I2PString) Length() (length int, err error) {
|
||||
if len(str) == 0 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(I2PString) Length",
|
||||
"reason": "no data",
|
||||
}).Error("error parsing string")
|
||||
err = errors.New("error parsing string: zero length")
|
||||
return
|
||||
}
|
||||
l, _, _ := NewInteger(str, 1)
|
||||
length = l.Int()
|
||||
str_len := len(str) - 1
|
||||
if length > str_len {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(I2PString) Length",
|
||||
"string_bytes_length": str_len,
|
||||
"string_length_field": length,
|
||||
"data": string(str),
|
||||
"reason": "data less than specified by length",
|
||||
}).Error("string format warning")
|
||||
err = errors.New("string parsing warning: string data is shorter than specified by length")
|
||||
}
|
||||
return
|
||||
return len(str)-1, nil
|
||||
}
|
||||
|
||||
// Data returns the I2PString content as a string trimmed to the specified length and not including the length byte.
|
||||
@ -64,8 +43,11 @@ func (str I2PString) Data() (data string, err error) {
|
||||
case "error parsing string: zero length":
|
||||
return
|
||||
case "string parsing warning: string data is shorter than specified by length":
|
||||
data = string(str[1:])
|
||||
return
|
||||
if is, e := ToI2PString(string(str[:])); e != nil {
|
||||
return "", e
|
||||
}else{
|
||||
return is.Data()
|
||||
}
|
||||
case "string parsing warning: string contains data beyond length":
|
||||
data = string(str[1:])
|
||||
return
|
||||
@ -75,7 +57,6 @@ func (str I2PString) Data() (data string, err error) {
|
||||
return
|
||||
}
|
||||
data = string(str[1 : length+1])
|
||||
log.Println("data", data)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -152,13 +152,12 @@ func (router_address RouterAddress) GetOption(key I2PString) I2PString {
|
||||
|
||||
func (router_address RouterAddress) HostString() I2PString {
|
||||
host, _ := ToI2PString("host")
|
||||
log.Println("Host", string(host))
|
||||
return router_address.GetOption(host)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) PortString() I2PString {
|
||||
host, _ := ToI2PString("port")
|
||||
return router_address.GetOption(host)
|
||||
port, _ := ToI2PString("port")
|
||||
return router_address.GetOption(port)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) StaticKeyString() I2PString {
|
||||
|
@ -1,7 +1,6 @@
|
||||
package noise
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
@ -45,7 +44,6 @@ func (ns *Noise) lockMutex() {
|
||||
var ex_ns net.Conn = &NoiseConn{}
|
||||
var ex_ns_l net.Listener = &NoiseListener{}
|
||||
var ex_ns_u net.PacketConn = &NoisePacketConn{}
|
||||
|
||||
//var ex_tc_up net.PacketConn = &NoiseConn{}
|
||||
|
||||
func NewNoise(ra router_address.RouterAddress) (ns *Noise, err error) {
|
||||
@ -103,22 +101,15 @@ func (ns *Noise) ListenNoise() (list NoiseListener, err error) {
|
||||
cfg := ns
|
||||
cfg.Initiator = false
|
||||
network := "tcp"
|
||||
if ns.UDP() {
|
||||
network = "udp"
|
||||
}
|
||||
log.Println("u", network)
|
||||
host, err := ns.Host()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
log.Println("h", host)
|
||||
port, err := ns.Port()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
log.Println("p", port)
|
||||
hostip := net.JoinHostPort(host.String(), port)
|
||||
log.Println("hip", hostip)
|
||||
listener, err := net.Listen(network, hostip)
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -29,11 +29,15 @@ func TestEstablishment(t *testing.T) {
|
||||
if ns, err := NewNoise(*ra); err != nil {
|
||||
t.Error("ERROR", err)
|
||||
} else {
|
||||
log.Println("NOISE TEST", ns)
|
||||
if nl, err := ns.ListenNoise(); err != nil {
|
||||
if host, err := ns.Host(); err != nil {
|
||||
t.Error("ERROR", err)
|
||||
} else {
|
||||
defer nl.Close()
|
||||
log.Println("NOISE TEST", host)
|
||||
if nl, err := ns.ListenNoise(); err != nil {
|
||||
t.Error("ERROR", err)
|
||||
} else {
|
||||
defer nl.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user