本文整理匯總了Golang中net.IP函數的典型用法代碼示例。如果您正苦於以下問題:Golang IP函數的具體用法?Golang IP怎麽用?Golang IP使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了IP函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: readRequest
// step3-4
func (s socks5Handler) readRequest() (string, bool) {
var (
buf = make([]byte, 262) // 4+(1+255)+2
host string
ofs int
ver, cmd, atyp byte
)
var msg = []byte{5, 0, 0, 1, 0, 0, 0, 0, 0, 0}
setRTimeout(s.conn)
_, err := s.conn.Read(buf)
if err != nil {
exception.Spawn(&err, "socks: read request")
goto errLogging
}
ver, cmd, atyp = buf[0], buf[1], buf[3]
if ver != S5_VER || cmd != 1 {
exception.Spawn(&err, "socks: invalid request")
goto errHandler
}
buf = buf[4:]
switch atyp {
case IPV4:
host = net.IP(buf[:net.IPv4len]).String()
ofs = net.IPv4len
case IPV6:
host = "[" + net.IP(buf[:net.IPv6len]).String() + "]"
ofs = net.IPv6len
case DOMAIN:
dlen := int(buf[0])
ofs = dlen + 1
host = string(buf[1:ofs])
// literal IPv6
if strings.Count(host, ":") >= 2 && !strings.HasPrefix(host, "[") {
host = "[" + host + "]"
}
default:
exception.Spawn(&err, "socks: invalid request")
goto errHandler
}
// accept
_, err = s.conn.Write(msg)
if err != nil {
exception.Spawn(&err, "socks: write response")
goto errLogging
}
host += ":" + strconv.Itoa(int(binary.BigEndian.Uint16(buf[ofs:])))
return host, true
errHandler:
msg[1] = 0x1 // general SOCKS server failure
setWTimeout(s.conn)
s.conn.Write(msg)
errLogging:
log.Warningln(err)
return NULL, false
}
示例2: deserializeRoute
// deserializeRoute decodes a binary netlink message into a Route struct
func deserializeRoute(m []byte) (Route, error) {
route := Route{}
msg := nl.DeserializeRtMsg(m)
attrs, err := nl.ParseRouteAttr(m[msg.Len():])
if err != nil {
return route, err
}
route.Scope = Scope(msg.Scope)
route.Flags = int(msg.Flags)
native := nl.NativeEndian()
for _, attr := range attrs {
switch attr.Attr.Type {
case syscall.RTA_GATEWAY:
route.Gw = net.IP(attr.Value)
case syscall.RTA_PREFSRC:
route.Src = net.IP(attr.Value)
case syscall.RTA_DST:
route.Dst = &net.IPNet{
IP: attr.Value,
Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attr.Value)),
}
case syscall.RTA_OIF:
routeIndex := int(native.Uint32(attr.Value[0:4]))
route.LinkIndex = routeIndex
}
}
return route, nil
}
示例3: analyzeIpv4Header
func analyzeIpv4Header(ipv4 ethernetdecode.Ipv4Header) {
src := net.IP(ipv4.Source[:4]).String()
dst := net.IP(ipv4.Destination[:4]).String()
size := uint64(ipv4.Len)
updateCounters(src, dst, size)
}
示例4: decodeFromBytes
func (m *BGP4MPHeader) decodeFromBytes(data []byte) ([]byte, error) {
if m.isAS4 && len(data) < 8 {
return nil, fmt.Errorf("Not all BGP4MPMessageAS4 bytes available")
} else if !m.isAS4 && len(data) < 4 {
return nil, fmt.Errorf("Not all BGP4MPMessageAS bytes available")
}
if m.isAS4 {
m.PeerAS = binary.BigEndian.Uint32(data[:4])
m.LocalAS = binary.BigEndian.Uint32(data[4:8])
data = data[8:]
} else {
m.PeerAS = uint32(binary.BigEndian.Uint16(data[:2]))
m.LocalAS = uint32(binary.BigEndian.Uint16(data[2:4]))
data = data[4:]
}
m.InterfaceIndex = binary.BigEndian.Uint16(data[:2])
m.AddressFamily = binary.BigEndian.Uint16(data[2:4])
switch m.AddressFamily {
case AFI_IP:
m.PeerIpAddress = net.IP(data[4:8]).To4()
m.LocalIpAddress = net.IP(data[8:12]).To4()
data = data[12:]
case AFI_IP6:
m.PeerIpAddress = net.IP(data[4:20])
m.LocalIpAddress = net.IP(data[20:36])
data = data[36:]
default:
return nil, fmt.Errorf("unsupported address family: %d", m.AddressFamily)
}
return data, nil
}
示例5: consume
//export consume
func consume(ctx *C.knot_layer_t, pkt *C.knot_pkt_t) C.int {
req := (*C.struct_kr_request)(ctx.data)
qry := req.current_query
state := (C.int)(ctx.state)
if qry.flags&C.QUERY_CACHED != 0 {
return state
}
// Parse answer source address
sa := (*C.struct_sockaddr)(unsafe.Pointer(&qry.ns.addr[0]))
var ip net.IP
if sa.sa_family == C.AF_INET {
sa_v4 := (*C.struct_sockaddr_in)(unsafe.Pointer(sa))
ip = net.IP(C.GoBytes(unsafe.Pointer(&sa_v4.sin_addr), 4))
} else if sa.sa_family == C.AF_INET6 {
sa_v6 := (*C.struct_sockaddr_in6)(unsafe.Pointer(sa))
ip = net.IP(C.GoBytes(unsafe.Pointer(&sa_v6.sin6_addr), 16))
}
// Parse metadata
qname := C.knot_dname_to_str_alloc(C.knot_pkt_qname(pkt))
defer C.free(unsafe.Pointer(qname))
qtype := C.knot_pkt_qtype(pkt)
secure := (bool)(C.knot_pkt_has_dnssec(pkt))
// Sample metric
process_sample(C.GoString(qname), int(qtype), ip, secure)
return state
}
示例6: analyzeIpv6Header
func analyzeIpv6Header(ipv6 ethernetdecode.Ipv6Header) {
src := net.IP(ipv6.Source[:16]).String()
dst := net.IP(ipv6.Destination[:16]).String()
size := uint64(ipv6.PayloadLength)
updateCounters(src, dst, size)
}
示例7: TestTransportNonsense
func TestTransportNonsense(t *testing.T) {
testString := ` sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout Inode
0: 00000000:A6C0 00000000:0000 01 000000
broken line
`
p := NewProcNet([]byte(testString), tcpEstablished)
expected := []Connection{
{
LocalAddress: net.IP([]byte{0, 0, 0, 0}),
LocalPort: 0xa6c0,
RemoteAddress: net.IP([]byte{0, 0, 0, 0}),
RemotePort: 0x0,
},
}
for i := 0; i < 1; i++ {
have := p.Next()
want := expected[i]
if !reflect.DeepEqual(*have, want) {
t.Errorf("Got\n%+v\nExpected\n%+v\n", *have, want)
}
}
if got := p.Next(); got != nil {
t.Errorf("p.Next() wasn't empty")
}
}
示例8: parseSocks5Request
func (s *S5Step1) parseSocks5Request() string {
var buf = make([]byte, 262) // 4+(1+255)+2
_, err := s.conn.Read(buf)
ThrowErr(err)
ver, cmd, atyp := buf[0], buf[1], buf[3]
if ver != SOCKS5_VER || cmd != 1 {
s.err = INVALID_SOCKS5_REQUEST
return NULL
}
s.target = buf[3:]
buf = buf[4:]
var host string
var ofs int
switch atyp {
case IPV4:
host = net.IP(buf[:net.IPv4len]).String()
ofs = net.IPv4len
case IPV6:
host = net.IP(buf[:net.IPv6len]).String()
ofs = net.IPv6len
case DOMAIN:
dlen := int(buf[0])
host = string(buf[1 : dlen+1])
ofs = dlen + 1
default:
s.err = INVALID_SOCKS5_REQUEST
return NULL
}
var dst_port = binary.BigEndian.Uint16(buf[ofs : ofs+2])
return host + ":" + strconv.Itoa(int(dst_port))
}
示例9: decodeIGMPv3MembershipReport
func (i *IGMP) decodeIGMPv3MembershipReport(data []byte) error {
if len(data) < 8 {
return fmt.Errorf("IGMPv3 Membership Report too small #1")
}
i.Checksum = binary.BigEndian.Uint16(data[2:4])
i.NumberOfGroupRecords = binary.BigEndian.Uint16(data[6:8])
recordOffset := 8
for j := 0; j < int(i.NumberOfGroupRecords); j++ {
if len(data) < recordOffset+8 {
return fmt.Errorf("IGMPv3 Membership Report too small #2")
}
var gr IGMPv3GroupRecord
gr.Type = IGMPv3GroupRecordType(data[recordOffset])
gr.AuxDataLen = data[recordOffset+1]
gr.NumberOfSources = binary.BigEndian.Uint16(data[recordOffset+2 : recordOffset+4])
gr.MulticastAddress = net.IP(data[recordOffset+4 : recordOffset+8])
if len(data) < recordOffset+8+int(gr.NumberOfSources)*4 {
return fmt.Errorf("IGMPv3 Membership Report too small #3")
}
// append source address records.
for i := 0; i < int(gr.NumberOfSources); i++ {
sourceAddr := net.IP(data[recordOffset+8+i*4 : recordOffset+12+i*4])
gr.SourceAddresses = append(gr.SourceAddresses, sourceAddr)
}
i.GroupRecords = append(i.GroupRecords, gr)
recordOffset += 8 + 4*int(gr.NumberOfSources)
}
return nil
}
示例10: NewNodeReader
func NewNodeReader(r io.Reader) (*Node, os.Error) {
buf := make([]byte, maxSize)
if _, err := r.Read(buf[0:1]); err != nil {
return nil, err
}
nameLen := int(buf[0])
if nameLen == 0 {
return nil, NameTooShort
}
size := 1 + nameLen + sizeAddrs
if _, err := r.Read(buf[1:size]); err != nil {
return nil, err
}
pos := 1
name := string(buf[pos : pos+nameLen])
pos += nameLen
extIP := net.IP(buf[pos : pos+sizeIP6])
pos += sizeIP6
intIP4 := net.IP(buf[pos : pos+sizeIP4])
pos += sizeIP4
intIP6 := net.IP(buf[pos : pos+sizeIP6])
pos += sizeIP6
return NewNode(name, extIP, intIP4, intIP6), nil
}
示例11: unmarshalInet
func unmarshalInet(info TypeInfo, data []byte, value interface{}) error {
switch v := value.(type) {
case Unmarshaler:
return v.UnmarshalCQL(info, data)
case *net.IP:
if x := len(data); !(x == 4 || x == 16) {
return unmarshalErrorf("cannot unmarshal %s into %T: invalid sized IP: got %d bytes not 4 or 16", info, value, x)
}
buf := copyBytes(data)
ip := net.IP(buf)
if v4 := ip.To4(); v4 != nil {
*v = v4
return nil
}
*v = ip
return nil
case *string:
if len(data) == 0 {
*v = ""
return nil
}
ip := net.IP(data)
if v4 := ip.To4(); v4 != nil {
*v = v4.String()
return nil
}
*v = ip.String()
return nil
}
return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
}
示例12: unmarshalInet
func unmarshalInet(info TypeInfo, data []byte, value interface{}) error {
switch v := value.(type) {
case Unmarshaler:
return v.UnmarshalCQL(info, data)
case *net.IP:
ip := net.IP(data)
if v4 := ip.To4(); v4 != nil {
*v = v4
return nil
}
*v = ip
return nil
case *string:
if len(data) == 0 {
*v = ""
return nil
}
ip := net.IP(data)
if v4 := ip.To4(); v4 != nil {
*v = v4.String()
return nil
}
*v = ip.String()
return nil
}
return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
}
示例13: parseGretapData
func parseGretapData(link Link, data []syscall.NetlinkRouteAttr) {
gre := link.(*Gretap)
for _, datum := range data {
switch datum.Attr.Type {
case nl.IFLA_GRE_OKEY:
gre.IKey = ntohl(datum.Value[0:4])
case nl.IFLA_GRE_IKEY:
gre.OKey = ntohl(datum.Value[0:4])
case nl.IFLA_GRE_LOCAL:
gre.Local = net.IP(datum.Value[0:4])
case nl.IFLA_GRE_REMOTE:
gre.Remote = net.IP(datum.Value[0:4])
case nl.IFLA_GRE_ENCAP_SPORT:
gre.EncapSport = ntohs(datum.Value[0:2])
case nl.IFLA_GRE_ENCAP_DPORT:
gre.EncapDport = ntohs(datum.Value[0:2])
case nl.IFLA_GRE_IFLAGS:
gre.IFlags = ntohs(datum.Value[0:2])
case nl.IFLA_GRE_OFLAGS:
gre.OFlags = ntohs(datum.Value[0:2])
case nl.IFLA_GRE_TTL:
gre.Ttl = uint8(datum.Value[0])
case nl.IFLA_GRE_TOS:
gre.Tos = uint8(datum.Value[0])
case nl.IFLA_GRE_PMTUDISC:
gre.PMtuDisc = uint8(datum.Value[0])
case nl.IFLA_GRE_ENCAP_TYPE:
gre.EncapType = native.Uint16(datum.Value[0:2])
case nl.IFLA_GRE_ENCAP_FLAGS:
gre.EncapFlags = native.Uint16(datum.Value[0:2])
}
}
}
示例14: processDHCPRequest
//Process the incomming request message and create a responce packet.
func (dhcpServer *DHCPServer) processDHCPRequest(p Packet, msgType MessageType, options Options) (d Packet, e error) {
dhcpServer.Logger.Debug("Received a DHCP request packet. " + dhcpServer.packetToString(p))
serverIP := net.IP{192, 168, 182, 1}
leaseDuration := 2 * time.Hour
staticOptions := Options{
OptionSubnetMask: []byte{255, 255, 255, 0},
OptionRouter: []byte(serverIP), // Presuming Server is also your router
OptionDomainNameServer: []byte(serverIP), // Presuming Server is also your DNS server
}
if server, ok := options[OptionServerIdentifier]; ok && !net.IP(server).Equal(serverIP) {
return nil, nil // Message not for this dhcp server
}
if reqIP := net.IP(options[OptionRequestedIPAddress]); len(reqIP) == 4 {
retPacket := ReplyPacket(p, ACK, serverIP, net.IP(options[OptionRequestedIPAddress]), leaseDuration, staticOptions.SelectOrderOrAll(options[OptionParameterRequestList]))
retPacket.SetFile([]byte("keith.txt"))
retPacket.SetSIAddr([]byte(net.IP{192, 168, 182, 100}))
dhcpServer.Logger.Debug("Sending responce packet to a DHCP request. " + dhcpServer.packetToString(retPacket))
return retPacket, nil
}
retPacket := ReplyPacket(p, NAK, serverIP, nil, 0, nil)
dhcpServer.Logger.Debug("Sending responce packet to a DHCP request. " + dhcpServer.packetToString(retPacket))
return retPacket, nil
}
示例15: parseRequest
// step2
func (s *s5Handler) parseRequest() string {
var (
buf = make([]byte, 262) // 4+(1+255)+2
host string
ofs int
)
setRTimeout(s.conn)
_, err := s.conn.Read(buf)
ThrowErr(err)
ver, cmd, atyp := buf[0], buf[1], buf[3]
if ver != S5_VER || cmd != 1 {
s.err = INVALID_SOCKS5_REQUEST
return NULL
}
buf = buf[4:]
switch atyp {
case IPV4:
host = net.IP(buf[:net.IPv4len]).String()
ofs = net.IPv4len
case IPV6:
host = "[" + net.IP(buf[:net.IPv6len]).String() + "]"
ofs = net.IPv6len
case DOMAIN:
dlen := int(buf[0])
host = string(buf[1 : dlen+1])
ofs = dlen + 1
default:
s.err = INVALID_SOCKS5_REQUEST
return NULL
}
var dst_port = binary.BigEndian.Uint16(buf[ofs : ofs+2])
return host + ":" + strconv.Itoa(int(dst_port))
}