太难了

This commit is contained in:
impact-eintr
2021-09-02 21:48:59 +08:00
parent 347bc5217b
commit 293b15a309
9 changed files with 196 additions and 9 deletions

View File

@@ -1,5 +1,12 @@
package tcpip
import (
"fmt"
"log"
"strconv"
"strings"
)
type Error struct {
msg string
ignoreStats bool
@@ -23,8 +30,8 @@ var (
ErrBadLinkEndPoint = &Error{msg: "bad link layer endpoint"}
ErrAlreadyBound = &Error{msg: "endpoint already bound", ignoreStats: true}
ErrInvalidEndpointState = &Error{msg: "endpoint is in invalid state"}
ErrAlreadConnecting = &Error{msg: "endpoint is already connecting", ignoreStats: true}
ErrAlreadConnected = &Error{msg: "endpoint is already connected", ignoreStats: true}
ErrAlreadyConnecting = &Error{msg: "endpoint is already connecting", ignoreStats: true}
ErrAlreadyConnected = &Error{msg: "endpoint is already connected", ignoreStats: true}
ErrNoPortAvailable = &Error{msg: "no port are available"}
ErrPortInUse = &Error{msg: "port is in use"}
ErrBadLocalAddress = &Error{msg: "bad local address"}
@@ -48,3 +55,46 @@ var (
ErrMessageTooLong = &Error{msg: "message too long"}
ErrNoBufferSpace = &Error{msg: "no buffer space available"}
)
type LinkAddress string
func (a LinkAddress) String() string {
switch len(a) {
case 6:
// MAC地址的格式 6位 4bit的十六进制数
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", a[0], a[1], a[2], a[3], a[4], a[5])
default:
return fmt.Sprintf("%x", []byte(a))
}
}
// aa:bb:cc:dd:ee:ff aa-bb-cc-dd-ee-ff
func ParseMACAddress(s string) (LinkAddress, error) {
parts := strings.FieldsFunc(s, func(c rune) bool {
return c == ':' || c == '-'
})
log.Println(parts)
if len(parts) != 6 {
return "", fmt.Errorf("inconsistent parts: %s", s)
}
addr := make([]byte, 0, len(parts))
for _, part := range parts {
u, err := strconv.ParseUint(part, 16, 8)
if err != nil {
return "", fmt.Errorf("invalid hex digits: %s", s)
}
addr = append(addr, byte(u))
}
return LinkAddress(addr), nil
}
type NetworkProtocolNumber uint32
type Address string
type ProtocolAddr struct {
Protocol NetworkProtocolNumber
Address Address
}