mirror of
https://github.com/impact-eintr/netstack.git
synced 2025-10-06 05:16:50 +08:00
太难了
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user