util

package
v0.1.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2025 License: MIT Imports: 2 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsLocallyServed added in v0.1.12

func IsLocallyServed(ip netip.Addr) (ok bool)

IsLocallyServed checks if ip belongs to any network defined by RFC 6303:

10.0.0.0/8
127.0.0.0/8
169.254.0.0/16
172.16.0.0/12
192.0.2.0/24
192.168.0.0/16
198.51.100.0/24
203.0.113.0/24
255.255.255.255/32

::/128
::1/128
2001:db8::/32
fd00::/8
fe80::/10

It may also be used as a [SubnetSetFunc].

func IsSpecialPurpose added in v0.1.12

func IsSpecialPurpose(ip netip.Addr) (ok bool)

IsSpecialPurpose checks if ip belongs to any network defined by IANA Special-Purpose Address Registry:

0.0.0.0/8          "This host on this network".
10.0.0.0/8         Private-Use.
100.64.0.0/10      Shared Address Space.
127.0.0.0/8        Loopback.
169.254.0.0/16     Link Local.
172.16.0.0/12      Private-Use.
192.0.0.0/24       IETF Protocol Assignments.
192.0.0.0/29       DS-Lite.
192.0.2.0/24       Documentation (TEST-NET-1)
192.88.99.0/24     6to4 Relay Anycast.
192.168.0.0/16     Private-Use.
198.18.0.0/15      Benchmarking.
198.51.100.0/24    Documentation (TEST-NET-2).
203.0.113.0/24     Documentation (TEST-NET-3).
240.0.0.0/4        Reserved.
255.255.255.255/32 Limited Broadcast.

::/128            Unspecified Address.
::1/128           Loopback Address.
64:ff9b::/96      IPv4-IPv6 Translation Address.
64:ff9b:1::/48    IPv4-IPv6 Translation Address.
100::/64          Discard-Only Address Block.
2001::/23         IETF Protocol Assignments.
2001::/32         TEREDO.
2001:1::1/128     Port Control Protocol Anycast.
2001:1::2/128     Traversal Using Relays around NAT Anycast.
2001:2::/48       Benchmarking.
2001:3::/32       AMT.
2001:4:112::/48   AS112-v6.
2001:10::/28      ORCHID.
2001:20::/28      ORCHIDv2.
2001:db8::/32     Documentation.
2002::/16         6to4.
2620:4f:8000::/48 Direct Delegation AS112 Service.
fc00::/7          Unique-Local.
fe80::/10         Linked-Scoped Unicast.

See https://www.iana.org/assignments/iana-ipv4-special-registry and https://www.iana.org/assignments/iana-ipv6-special-registry.

It may also be used as a [SubnetSetFunc].

func WriteAtLeast

func WriteAtLeast(w io.Writer, buf []byte, min int) (n int, err error)

WriteAtLeast 尝试将 buf 中的数据写入 w,直到成功写入至少 min 字节或发生错误。 若写入过程中已写入部分数据(n > 0)但遇到 EOF,会返回 io.ErrUnexpectedEOF。 当实际写入字节数 n >= min 时,错误会被置为 nil(表示已满足最小写入要求)。

参数:

w: 实现了 io.Writer 接口的写入目标
buf: 待写入的数据缓冲区(实际写入长度不超过 len(buf))
min: 需要至少写入的字节数(需满足 min <= len(buf),否则可能无法完成)

返回:

n: 实际成功写入的字节数(可能大于等于 min 或小于 min 但发生错误)
err: 错误信息(成功写入 >= min 字节时为 nil;写入未达 min 时返回具体错误)

func WriteFull

func WriteFull(w io.Writer, p []byte) (n int, err error)

WriteFull 尝试将 p 中的全部数据完整写入 w,直到成功写入 len(p) 字节或发生错误。 该函数是 WriteAtLeast 的便捷封装,等价于调用 WriteAtLeast(w, p, len(p))。 当成功写入全部 len(p) 字节时返回的 err 为 nil;若写入未完成则返回具体错误。

参数:

w: 实现了 io.Writer 接口的写入目标
p: 待完整写入的数据缓冲区(要求写入长度为 len(p))

返回:

n: 实际成功写入的字节数(可能等于 len(p) 或小于 len(p) 但发生错误)
err: 错误信息(成功写入全部数据时为 nil;写入未完成时返回具体错误)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL