netio #
Networking Library for V
netio is a flexible cross-platform networking library for V programming language.
This module provides the low-level wrappers around C APIs and supports any kind of sockets, socket options, address families and protocols. netio.protocol submodule provides access to the operating system protocols database.
netio is mainly tested on Linux, but should work on all POSIX-compliant operating systems. Microsoft Windows support is experimental.
See examples/ dir in source code repository for usage examples.
Constants #
const af_alg = AddrFamily(38) // AF_ALG
const af_appletalk = AddrFamily(5) // AF_APPLETALK
const af_ash = AddrFamily(18) // AF_ASH
const af_atmpvc = AddrFamily(8) // AF_ATMPVC
const af_atmsvc = AddrFamily(20) // AF_ATMSVC
const af_ax25 = AddrFamily(3) // AF_AX25
const af_bluetooth = AddrFamily(31) // AF_BLUETOOTH
const af_bridge = AddrFamily(7) // AF_BRIDGE
const af_caif = AddrFamily(37) // AF_CAIF
const af_can = AddrFamily(29) // AF_CAN
const af_decnet = AddrFamily(12) // AF_DECNET
const af_econet = AddrFamily(19) // AF_ECONET
const af_file = AddrFamily(1) // AF_FILE
const af_ib = AddrFamily(27) // AF_IB
const af_ieee802154 = AddrFamily(36) // AF_IEEE802154
const af_inet6 = AddrFamily(10) // AF_INET6
const af_inet = AddrFamily(2) // AF_INET
const af_ipx = AddrFamily(4) // AF_IPX
const af_irda = AddrFamily(23) // AF_IRDA
const af_isdn = AddrFamily(34) // AF_ISDN
const af_iucv = AddrFamily(32) // AF_IUCV
const af_kcm = AddrFamily(41) // AF_KCM
const af_key = AddrFamily(15) // AF_KEY
const af_llc = AddrFamily(26) // AF_LLC
const af_local = AddrFamily(1) // AF_LOCAL
const af_max = AddrFamily(46) // AF_MAX
const af_mctp = AddrFamily(45) // AF_MCTP
const af_mpls = AddrFamily(28) // AF_MPLS
const af_netbeui = AddrFamily(13) // AF_NETBEUI
const af_netlink = AddrFamily(16) // AF_NETLINK
const af_netrom = AddrFamily(6) // AF_NETROM
const af_nfc = AddrFamily(39) // AF_NFC
const af_packet = AddrFamily(17) // AF_PACKET
const af_phonet = AddrFamily(35) // AF_PHONET
const af_pppox = AddrFamily(24) // AF_PPPOX
const af_qipcrtr = AddrFamily(42) // AF_QIPCRTR
const af_rds = AddrFamily(21) // AF_RDS
const af_rose = AddrFamily(11) // AF_ROSE
const af_route = AddrFamily(16) // AF_ROUTE
const af_rxrpc = AddrFamily(33) // AF_RXRPC
const af_security = AddrFamily(14) // AF_SECURITY
const af_smc = AddrFamily(43) // AF_SMC
const af_sna = AddrFamily(22) // AF_SNA
const af_tipc = AddrFamily(30) // AF_TIPC
const af_unix = AddrFamily(1) // AF_UNIX
const af_unspec = AddrFamily(0) // AF_UNSPEC
const af_vsock = AddrFamily(40) // AF_VSOCK
const af_wanpipe = AddrFamily(25) // AF_WANPIPE
const af_x25 = AddrFamily(9) // AF_X25
const af_xdp = AddrFamily(44) // AF_XDP
const ai_addrconfig = AddrInfoFlag(32) // AI_ADDRCONFIG
const ai_all = AddrInfoFlag(16) // AI_ALL
const ai_canonname = AddrInfoFlag(2) // AI_CANONNAME
const ai_numerichost = AddrInfoFlag(4) // AI_NUMERICHOST
const ai_numericserv = AddrInfoFlag(1024) // AI_NUMERICSERV
const ai_passive = AddrInfoFlag(1) // AI_PASSIVE
const ai_v4mapped = AddrInfoFlag(8) // AI_V4MAPPED
const ipproto_ah = SocketLevel(51) // IPPROTO_AH
const ipproto_beetph = SocketLevel(94) // IPPROTO_BEETPH
const ipproto_comp = SocketLevel(108) // IPPROTO_COMP
const ipproto_dccp = SocketLevel(33) // IPPROTO_DCCP
const ipproto_dstopts = SocketLevel(60) // IPPROTO_DSTOPTS
const ipproto_egp = SocketLevel(8) // IPPROTO_EGP
const ipproto_encap = SocketLevel(98) // IPPROTO_ENCAP
const ipproto_esp = SocketLevel(50) // IPPROTO_ESP
const ipproto_ethernet = SocketLevel(143) // IPPROTO_ETHERNET
const ipproto_fragment = SocketLevel(44) // IPPROTO_FRAGMENT
const ipproto_gre = SocketLevel(47) // IPPROTO_GRE
const ipproto_hopopts = SocketLevel(0) // IPPROTO_HOPOPTS
const ipproto_icmpv6 = SocketLevel(58) // IPPROTO_ICMPV6
const ipproto_icmp = SocketLevel(1) // IPPROTO_ICMP
const ipproto_idp = SocketLevel(22) // IPPROTO_IDP
const ipproto_igmp = SocketLevel(2) // IPPROTO_IGMP
const ipproto_ipip = SocketLevel(4) // IPPROTO_IPIP
const ipproto_ipv6 = SocketLevel(41) // IPPROTO_IPV6
const ipproto_ip = SocketLevel(0) // IPPROTO_IP
const ipproto_l2tp = SocketLevel(115) // IPPROTO_L2TP
const ipproto_mh = SocketLevel(135) // IPPROTO_MH
const ipproto_mpls = SocketLevel(137) // IPPROTO_MPLS
const ipproto_mptcp = SocketLevel(262) // IPPROTO_MPTCP
const ipproto_mtp = SocketLevel(92) // IPPROTO_MTP
const ipproto_none = SocketLevel(59) // IPPROTO_NONE
const ipproto_pim = SocketLevel(103) // IPPROTO_PIM
const ipproto_pup = SocketLevel(12) // IPPROTO_PUP
const ipproto_raw = SocketLevel(255) // IPPROTO_RAW
const ipproto_routing = SocketLevel(43) // IPPROTO_ROUTING
const ipproto_rsvp = SocketLevel(46) // IPPROTO_RSVP
const ipproto_sctp = SocketLevel(132) // IPPROTO_SCTP
const ipproto_smc = SocketLevel(256) // IPPROTO_SMC
const ipproto_tcp = SocketLevel(6) // IPPROTO_TCP
const ipproto_tp = SocketLevel(29) // IPPROTO_TP
const ipproto_udplite = SocketLevel(136) // IPPROTO_UDPLITE
const ipproto_udp = SocketLevel(17) // IPPROTO_UDP
const ipv6_2292dstopts = SocketOption(4) // IPV6_2292DSTOPTS
const ipv6_2292hoplimit = SocketOption(8) // IPV6_2292HOPLIMIT
const ipv6_2292hopopts = SocketOption(3) // IPV6_2292HOPOPTS
const ipv6_2292pktinfo = SocketOption(2) // IPV6_2292PKTINFO
const ipv6_2292pktoptions = SocketOption(6) // IPV6_2292PKTOPTIONS
const ipv6_2292rthdr = SocketOption(5) // IPV6_2292RTHDR
const ipv6_addrform = SocketOption(1) // IPV6_ADDRFORM
const ipv6_addr_preferences = SocketOption(72) // IPV6_ADDR_PREFERENCES
const ipv6_add_membership = SocketOption(20) // IPV6_ADD_MEMBERSHIP
const ipv6_authhdr = SocketOption(10) // IPV6_AUTHHDR
const ipv6_autoflowlabel = SocketOption(70) // IPV6_AUTOFLOWLABEL
const ipv6_checksum = SocketOption(7) // IPV6_CHECKSUM
const ipv6_dontfrag = SocketOption(62) // IPV6_DONTFRAG
const ipv6_drop_membership = SocketOption(21) // IPV6_DROP_MEMBERSHIP
const ipv6_dstopts = SocketOption(59) // IPV6_DSTOPTS
const ipv6_freebind = SocketOption(78) // IPV6_FREEBIND
const ipv6_hdrincl = SocketOption(36) // IPV6_HDRINCL
const ipv6_hoplimit = SocketOption(52) // IPV6_HOPLIMIT
const ipv6_hopopts = SocketOption(54) // IPV6_HOPOPTS
const ipv6_ipsec_policy = SocketOption(34) // IPV6_IPSEC_POLICY
const ipv6_join_anycast = SocketOption(27) // IPV6_JOIN_ANYCAST
const ipv6_join_group = SocketOption(20) // IPV6_JOIN_GROUP
const ipv6_leave_anycast = SocketOption(28) // IPV6_LEAVE_ANYCAST
const ipv6_leave_group = SocketOption(21) // IPV6_LEAVE_GROUP
const ipv6_minhopcount = SocketOption(73) // IPV6_MINHOPCOUNT
const ipv6_mtu = SocketOption(24) // IPV6_MTU
const ipv6_mtu_discover = SocketOption(23) // IPV6_MTU_DISCOVER
const ipv6_multicast_all = SocketOption(29) // IPV6_MULTICAST_ALL
const ipv6_multicast_hops = SocketOption(18) // IPV6_MULTICAST_HOPS
const ipv6_multicast_if = SocketOption(17) // IPV6_MULTICAST_IF
const ipv6_multicast_loop = SocketOption(19) // IPV6_MULTICAST_LOOP
const ipv6_nexthop = SocketOption(9) // IPV6_NEXTHOP
const ipv6_origdstaddr = SocketOption(74) // IPV6_ORIGDSTADDR
const ipv6_pathmtu = SocketOption(61) // IPV6_PATHMTU
const ipv6_pktinfo = SocketOption(50) // IPV6_PKTINFO
const ipv6_pmtudisc_dont = SocketOption(0) // IPV6_PMTUDISC_DONT
const ipv6_pmtudisc_do = SocketOption(2) // IPV6_PMTUDISC_DO
const ipv6_pmtudisc_interface = SocketOption(4) // IPV6_PMTUDISC_INTERFACE
const ipv6_pmtudisc_omit = SocketOption(5) // IPV6_PMTUDISC_OMIT
const ipv6_pmtudisc_probe = SocketOption(3) // IPV6_PMTUDISC_PROBE
const ipv6_pmtudisc_want = SocketOption(1) // IPV6_PMTUDISC_WANT
const ipv6_recvdstopts = SocketOption(58) // IPV6_RECVDSTOPTS
const ipv6_recverr = SocketOption(25) // IPV6_RECVERR
const ipv6_recverr_rfc4884 = SocketOption(31) // IPV6_RECVERR_RFC4884
const ipv6_recvfragsize = SocketOption(77) // IPV6_RECVFRAGSIZE
const ipv6_recvhoplimit = SocketOption(51) // IPV6_RECVHOPLIMIT
const ipv6_recvhopopts = SocketOption(53) // IPV6_RECVHOPOPTS
const ipv6_recvorigdstaddr = SocketOption(74) // IPV6_RECVORIGDSTADDR
const ipv6_recvpathmtu = SocketOption(60) // IPV6_RECVPATHMTU
const ipv6_recvpktinfo = SocketOption(49) // IPV6_RECVPKTINFO
const ipv6_recvrthdr = SocketOption(56) // IPV6_RECVRTHDR
const ipv6_recvtclass = SocketOption(66) // IPV6_RECVTCLASS
const ipv6_router_alert = SocketOption(22) // IPV6_ROUTER_ALERT
const ipv6_router_alert_isolate = SocketOption(30) // IPV6_ROUTER_ALERT_ISOLATE
const ipv6_rthdrdstopts = SocketOption(55) // IPV6_RTHDRDSTOPTS
const ipv6_rthdr = SocketOption(57) // IPV6_RTHDR
const ipv6_rthdr_loose = SocketOption(0) // IPV6_RTHDR_LOOSE
const ipv6_rthdr_strict = SocketOption(1) // IPV6_RTHDR_STRICT
const ipv6_rthdr_type_0 = SocketOption(0) // IPV6_RTHDR_TYPE_0
const ipv6_rxdstopts = SocketOption(59) // IPV6_RXDSTOPTS
const ipv6_rxhopopts = SocketOption(54) // IPV6_RXHOPOPTS
const ipv6_tclass = SocketOption(67) // IPV6_TCLASS
const ipv6_transparent = SocketOption(75) // IPV6_TRANSPARENT
const ipv6_unicast_hops = SocketOption(16) // IPV6_UNICAST_HOPS
const ipv6_unicast_if = SocketOption(76) // IPV6_UNICAST_IF
const ipv6_v6only = SocketOption(26) // IPV6_V6ONLY
const ipv6_xfrm_policy = SocketOption(35) // IPV6_XFRM_POLICY
const ip_add_membership = SocketOption(35) // IP_ADD_MEMBERSHIP
const ip_add_source_membership = SocketOption(39) // IP_ADD_SOURCE_MEMBERSHIP
const ip_bind_address_no_port = SocketOption(24) // IP_BIND_ADDRESS_NO_PORT
const ip_block_source = SocketOption(38) // IP_BLOCK_SOURCE
const ip_checksum = SocketOption(23) // IP_CHECKSUM
const ip_default_multicast_loop = SocketOption(1) // IP_DEFAULT_MULTICAST_LOOP
const ip_default_multicast_ttl = SocketOption(1) // IP_DEFAULT_MULTICAST_TTL
const ip_drop_membership = SocketOption(36) // IP_DROP_MEMBERSHIP
const ip_drop_source_membership = SocketOption(40) // IP_DROP_SOURCE_MEMBERSHIP
const ip_freebind = SocketOption(15) // IP_FREEBIND
const ip_hdrincl = SocketOption(3) // IP_HDRINCL
const ip_ipsec_policy = SocketOption(16) // IP_IPSEC_POLICY
const ip_local_port_range = SocketOption(51) // IP_LOCAL_PORT_RANGE
const ip_max_memberships = SocketOption(20) // IP_MAX_MEMBERSHIPS
const ip_minttl = SocketOption(21) // IP_MINTTL
const ip_msfilter = SocketOption(41) // IP_MSFILTER
const ip_mtu = SocketOption(14) // IP_MTU
const ip_mtu_discover = SocketOption(10) // IP_MTU_DISCOVER
const ip_multicast_all = SocketOption(49) // IP_MULTICAST_ALL
const ip_multicast_if = SocketOption(32) // IP_MULTICAST_IF
const ip_multicast_loop = SocketOption(34) // IP_MULTICAST_LOOP
const ip_multicast_ttl = SocketOption(33) // IP_MULTICAST_TTL
const ip_nodefrag = SocketOption(22) // IP_NODEFRAG
const ip_options = SocketOption(4) // IP_OPTIONS
const ip_origdstaddr = SocketOption(20) // IP_ORIGDSTADDR
const ip_passsec = SocketOption(18) // IP_PASSSEC
const ip_pktinfo = SocketOption(8) // IP_PKTINFO
const ip_pktoptions = SocketOption(9) // IP_PKTOPTIONS
const ip_pmtudisc = SocketOption(10) // IP_PMTUDISC
const ip_pmtudisc_dont = SocketOption(0) // IP_PMTUDISC_DONT
const ip_pmtudisc_do = SocketOption(2) // IP_PMTUDISC_DO
const ip_pmtudisc_interface = SocketOption(4) // IP_PMTUDISC_INTERFACE
const ip_pmtudisc_omit = SocketOption(5) // IP_PMTUDISC_OMIT
const ip_pmtudisc_probe = SocketOption(3) // IP_PMTUDISC_PROBE
const ip_pmtudisc_want = SocketOption(1) // IP_PMTUDISC_WANT
const ip_protocol = SocketOption(52) // IP_PROTOCOL
const ip_recverr = SocketOption(11) // IP_RECVERR
const ip_recverr_rfc4884 = SocketOption(26) // IP_RECVERR_RFC4884
const ip_recvfragsize = SocketOption(25) // IP_RECVFRAGSIZE
const ip_recvopts = SocketOption(6) // IP_RECVOPTS
const ip_recvorigdstaddr = SocketOption(20) // IP_RECVORIGDSTADDR
const ip_recvretopts = SocketOption(7) // IP_RECVRETOPTS
const ip_recvtos = SocketOption(13) // IP_RECVTOS
const ip_recvttl = SocketOption(12) // IP_RECVTTL
const ip_retopts = SocketOption(7) // IP_RETOPTS
const ip_router_alert = SocketOption(5) // IP_ROUTER_ALERT
const ip_tos = SocketOption(1) // IP_TOS
const ip_transparent = SocketOption(19) // IP_TRANSPARENT
const ip_ttl = SocketOption(2) // IP_TTL
const ip_unblock_source = SocketOption(37) // IP_UNBLOCK_SOURCE
const ip_unicast_if = SocketOption(50) // IP_UNICAST_IF
const ip_xfrm_policy = SocketOption(17) // IP_XFRM_POLICY
const msg_batch = MsgFlag(262144) // MSG_BATCH
const msg_cmsg_cloexec = MsgFlag(1073741824) // MSG_CMSG_CLOEXEC
const msg_confirm = MsgFlag(2048) // MSG_CONFIRM
const msg_ctrunc = MsgFlag(8) // MSG_CTRUNC
const msg_dontroute = MsgFlag(4) // MSG_DONTROUTE
const msg_dontwait = MsgFlag(64) // MSG_DONTWAIT
const msg_eor = MsgFlag(128) // MSG_EOR
const msg_errqueue = MsgFlag(8192) // MSG_ERRQUEUE
const msg_fastopen = MsgFlag(536870912) // MSG_FASTOPEN
const msg_fin = MsgFlag(512) // MSG_FIN
const msg_more = MsgFlag(32768) // MSG_MORE
const msg_nosignal = MsgFlag(16384) // MSG_NOSIGNAL
const msg_oob = MsgFlag(1) // MSG_OOB
const msg_peek = MsgFlag(2) // MSG_PEEK
const msg_proxy = MsgFlag(16) // MSG_PROXY
const msg_rst = MsgFlag(4096) // MSG_RST
const msg_sock_devmem = MsgFlag(33554432) // MSG_SOCK_DEVMEM
const msg_syn = MsgFlag(1024) // MSG_SYN
const msg_trunc = MsgFlag(32) // MSG_TRUNC
const msg_waitall = MsgFlag(256) // MSG_WAITALL
const msg_waitforone = MsgFlag(65536) // MSG_WAITFORONE
const msg_zerocopy = MsgFlag(67108864) // MSG_ZEROCOPY
const ni_dgram = NameInfoFlag(16) // NI_DGRAM
const ni_maxhost = NameInfoFlag(1025) // NI_MAXHOST
const ni_maxserv = NameInfoFlag(32) // NI_MAXSERV
const ni_namereqd = NameInfoFlag(8) // NI_NAMEREQD
const ni_nofqdn = NameInfoFlag(4) // NI_NOFQDN
const ni_numerichost = NameInfoFlag(1) // NI_NUMERICHOST
const ni_numericserv = NameInfoFlag(2) // NI_NUMERICSERV
const sock_cloexec = SocketType(524288) // SOCK_CLOEXEC
const sock_dccp = SocketType(6) // SOCK_DCCP
const sock_dgram = SocketType(2) // SOCK_DGRAM
const sock_nonblock = SocketType(2048) // SOCK_NONBLOCK
const sock_packet = SocketType(10) // SOCK_PACKET
const sock_raw = SocketType(3) // SOCK_RAW
const sock_rdm = SocketType(4) // SOCK_RDM
const sock_seqpacket = SocketType(5) // SOCK_SEQPACKET
const sock_stream = SocketType(1) // SOCK_STREAM
const sol_aal = SocketLevel(265) // SOL_AAL
const sol_alg = SocketLevel(279) // SOL_ALG
const sol_atm = SocketLevel(264) // SOL_ATM
const sol_bluetooth = SocketLevel(274) // SOL_BLUETOOTH
const sol_caif = SocketLevel(278) // SOL_CAIF
const sol_dccp = SocketLevel(269) // SOL_DCCP
const sol_decnet = SocketLevel(261) // SOL_DECNET
const sol_icmpv6 = SocketLevel(58) // SOL_ICMPV6
const sol_ipv6 = SocketLevel(41) // SOL_IPV6
const sol_ip = SocketLevel(0) // SOL_IP
const sol_irda = SocketLevel(266) // SOL_IRDA
const sol_iucv = SocketLevel(277) // SOL_IUCV
const sol_kcm = SocketLevel(281) // SOL_KCM
const sol_llc = SocketLevel(268) // SOL_LLC
const sol_mctp = SocketLevel(285) // SOL_MCTP
const sol_mptcp = SocketLevel(284) // SOL_MPTCP
const sol_netbeui = SocketLevel(267) // SOL_NETBEUI
const sol_netlink = SocketLevel(270) // SOL_NETLINK
const sol_nfc = SocketLevel(280) // SOL_NFC
const sol_packet = SocketLevel(263) // SOL_PACKET
const sol_pnpipe = SocketLevel(275) // SOL_PNPIPE
const sol_pppol2tp = SocketLevel(273) // SOL_PPPOL2TP
const sol_raw = SocketLevel(255) // SOL_RAW
const sol_rds = SocketLevel(276) // SOL_RDS
const sol_rxrpc = SocketLevel(272) // SOL_RXRPC
const sol_smc = SocketLevel(286) // SOL_SMC
const sol_socket = SocketLevel(1) // SOL_SOCKET
const sol_tcp = SocketLevel(6) // SOL_TCP
const sol_tipc = SocketLevel(271) // SOL_TIPC
const sol_tls = SocketLevel(282) // SOL_TLS
const sol_udp = SocketLevel(17) // SOL_UDP
const sol_vsock = SocketLevel(287) // SOL_VSOCK
const sol_x25 = SocketLevel(262) // SOL_X25
const sol_xdp = SocketLevel(283) // SOL_XDP
const so_acceptconn = SocketOption(30) // SO_ACCEPTCONN
const so_attach_bpf = SocketOption(50) // SO_ATTACH_BPF
const so_attach_filter = SocketOption(26) // SO_ATTACH_FILTER
const so_attach_reuseport_cbpf = SocketOption(51) // SO_ATTACH_REUSEPORT_CBPF
const so_attach_reuseport_ebpf = SocketOption(52) // SO_ATTACH_REUSEPORT_EBPF
const so_bindtodevice = SocketOption(25) // SO_BINDTODEVICE
const so_bindtoifindex = SocketOption(62) // SO_BINDTOIFINDEX
const so_bpf_extensions = SocketOption(48) // SO_BPF_EXTENSIONS
const so_broadcast = SocketOption(6) // SO_BROADCAST
const so_bsdcompat = SocketOption(14) // SO_BSDCOMPAT
const so_buf_lock = SocketOption(72) // SO_BUF_LOCK
const so_busy_poll = SocketOption(46) // SO_BUSY_POLL
const so_busy_poll_budget = SocketOption(70) // SO_BUSY_POLL_BUDGET
const so_cnx_advice = SocketOption(53) // SO_CNX_ADVICE
const so_cookie = SocketOption(57) // SO_COOKIE
const so_debug = SocketOption(1) // SO_DEBUG
const so_detach_bpf = SocketOption(27) // SO_DETACH_BPF
const so_detach_filter = SocketOption(27) // SO_DETACH_FILTER
const so_detach_reuseport_bpf = SocketOption(68) // SO_DETACH_REUSEPORT_BPF
const so_devmem_dmabuf = SocketOption(79) // SO_DEVMEM_DMABUF
const so_devmem_dontneed = SocketOption(80) // SO_DEVMEM_DONTNEED
const so_devmem_linear = SocketOption(78) // SO_DEVMEM_LINEAR
const so_domain = SocketOption(39) // SO_DOMAIN
const so_dontroute = SocketOption(5) // SO_DONTROUTE
const so_error = SocketOption(4) // SO_ERROR
const so_get_filter = SocketOption(26) // SO_GET_FILTER
const so_incoming_cpu = SocketOption(49) // SO_INCOMING_CPU
const so_incoming_napi_id = SocketOption(56) // SO_INCOMING_NAPI_ID
const so_inq = SocketOption(84) // SO_INQ
const so_keepalive = SocketOption(9) // SO_KEEPALIVE
const so_linger = SocketOption(13) // SO_LINGER
const so_lock_filter = SocketOption(44) // SO_LOCK_FILTER
const so_mark = SocketOption(36) // SO_MARK
const so_max_pacing_rate = SocketOption(47) // SO_MAX_PACING_RATE
const so_meminfo = SocketOption(55) // SO_MEMINFO
const so_netns_cookie = SocketOption(71) // SO_NETNS_COOKIE
const so_nofcs = SocketOption(43) // SO_NOFCS
const so_no_check = SocketOption(11) // SO_NO_CHECK
const so_oobinline = SocketOption(10) // SO_OOBINLINE
const so_passcred = SocketOption(16) // SO_PASSCRED
const so_passpidfd = SocketOption(76) // SO_PASSPIDFD
const so_passrights = SocketOption(83) // SO_PASSRIGHTS
const so_passsec = SocketOption(34) // SO_PASSSEC
const so_peek_off = SocketOption(42) // SO_PEEK_OFF
const so_peercred = SocketOption(17) // SO_PEERCRED
const so_peergroups = SocketOption(59) // SO_PEERGROUPS
const so_peername = SocketOption(28) // SO_PEERNAME
const so_peerpidfd = SocketOption(77) // SO_PEERPIDFD
const so_peersec = SocketOption(31) // SO_PEERSEC
const so_prefer_busy_poll = SocketOption(69) // SO_PREFER_BUSY_POLL
const so_priority = SocketOption(12) // SO_PRIORITY
const so_protocol = SocketOption(38) // SO_PROTOCOL
const so_rcvbufforce = SocketOption(33) // SO_RCVBUFFORCE
const so_rcvbuf = SocketOption(8) // SO_RCVBUF
const so_rcvlowat = SocketOption(18) // SO_RCVLOWAT
const so_rcvmark = SocketOption(75) // SO_RCVMARK
const so_rcvpriority = SocketOption(82) // SO_RCVPRIORITY
const so_rcvtimeo = SocketOption(20) // SO_RCVTIMEO
const so_rcvtimeo_new = SocketOption(66) // SO_RCVTIMEO_NEW
const so_rcvtimeo_old = SocketOption(20) // SO_RCVTIMEO_OLD
const so_reserve_mem = SocketOption(73) // SO_RESERVE_MEM
const so_reuseaddr = SocketOption(2) // SO_REUSEADDR
const so_reuseport = SocketOption(15) // SO_REUSEPORT
const so_rxq_ovfl = SocketOption(40) // SO_RXQ_OVFL
const so_security_authentication = SocketOption(22) // SO_SECURITY_AUTHENTICATION
const so_security_encryption_network = SocketOption(24) // SO_SECURITY_ENCRYPTION_NETWORK
const so_security_encryption_transport = SocketOption(23) // SO_SECURITY_ENCRYPTION_TRANSPORT
const so_select_err_queue = SocketOption(45) // SO_SELECT_ERR_QUEUE
const so_sndbufforce = SocketOption(32) // SO_SNDBUFFORCE
const so_sndbuf = SocketOption(7) // SO_SNDBUF
const so_sndlowat = SocketOption(19) // SO_SNDLOWAT
const so_sndtimeo = SocketOption(21) // SO_SNDTIMEO
const so_sndtimeo_new = SocketOption(67) // SO_SNDTIMEO_NEW
const so_sndtimeo_old = SocketOption(21) // SO_SNDTIMEO_OLD
const so_timestamping = SocketOption(37) // SO_TIMESTAMPING
const so_timestamping_new = SocketOption(65) // SO_TIMESTAMPING_NEW
const so_timestamping_old = SocketOption(37) // SO_TIMESTAMPING_OLD
const so_timestampns = SocketOption(35) // SO_TIMESTAMPNS
const so_timestampns_new = SocketOption(64) // SO_TIMESTAMPNS_NEW
const so_timestampns_old = SocketOption(35) // SO_TIMESTAMPNS_OLD
const so_timestamp = SocketOption(29) // SO_TIMESTAMP
const so_timestamp_new = SocketOption(63) // SO_TIMESTAMP_NEW
const so_timestamp_old = SocketOption(29) // SO_TIMESTAMP_OLD
const so_txrehash = SocketOption(74) // SO_TXREHASH
const so_txtime = SocketOption(61) // SO_TXTIME
const so_type = SocketOption(3) // SO_TYPE
const so_wifi_status = SocketOption(41) // SO_WIFI_STATUS
const so_zerocopy = SocketOption(60) // SO_ZEROCOPY
const tcp_ao_keyf_exclude_opt = SocketOption(2) // TCP_AO_KEYF_EXCLUDE_OPT
const tcp_ao_keyf_ifindex = SocketOption(1) // TCP_AO_KEYF_IFINDEX
const tcp_ao_maxkeylen = SocketOption(80) // TCP_AO_MAXKEYLEN
const tcp_cc_info = SocketOption(26) // TCP_CC_INFO
const tcp_cm_inq = SocketOption(36) // TCP_CM_INQ
const tcp_congestion = SocketOption(13) // TCP_CONGESTION
const tcp_cookie_in_always = SocketOption(1) // TCP_COOKIE_IN_ALWAYS
const tcp_cookie_max = SocketOption(16) // TCP_COOKIE_MAX
const tcp_cookie_min = SocketOption(8) // TCP_COOKIE_MIN
const tcp_cookie_out_never = SocketOption(2) // TCP_COOKIE_OUT_NEVER
const tcp_cookie_pair_size = SocketOption(32) // TCP_COOKIE_PAIR_SIZE
const tcp_cookie_transactions = SocketOption(15) // TCP_COOKIE_TRANSACTIONS
const tcp_cork = SocketOption(3) // TCP_CORK
const tcp_defer_accept = SocketOption(9) // TCP_DEFER_ACCEPT
const tcp_fastopen = SocketOption(23) // TCP_FASTOPEN
const tcp_fastopen_connect = SocketOption(30) // TCP_FASTOPEN_CONNECT
const tcp_fastopen_key = SocketOption(33) // TCP_FASTOPEN_KEY
const tcp_fastopen_no_cookie = SocketOption(34) // TCP_FASTOPEN_NO_COOKIE
const tcp_info = SocketOption(11) // TCP_INFO
const tcp_inq = SocketOption(36) // TCP_INQ
const tcp_keepcnt = SocketOption(6) // TCP_KEEPCNT
const tcp_keepidle = SocketOption(4) // TCP_KEEPIDLE
const tcp_keepintvl = SocketOption(5) // TCP_KEEPINTVL
const tcp_linger2 = SocketOption(8) // TCP_LINGER2
const tcp_maxseg = SocketOption(2) // TCP_MAXSEG
const tcp_maxwin = SocketOption(65535) // TCP_MAXWIN
const tcp_max_winshift = SocketOption(14) // TCP_MAX_WINSHIFT
const tcp_md5sig = SocketOption(14) // TCP_MD5SIG
const tcp_md5sig_ext = SocketOption(32) // TCP_MD5SIG_EXT
const tcp_md5sig_flag_ifindex = SocketOption(2) // TCP_MD5SIG_FLAG_IFINDEX
const tcp_md5sig_flag_prefix = SocketOption(1) // TCP_MD5SIG_FLAG_PREFIX
const tcp_md5sig_maxkeylen = SocketOption(80) // TCP_MD5SIG_MAXKEYLEN
const tcp_mss = SocketOption(512) // TCP_MSS
const tcp_mss_default = SocketOption(536) // TCP_MSS_DEFAULT
const tcp_mss_desired = SocketOption(1220) // TCP_MSS_DESIRED
const tcp_nodelay = SocketOption(1) // TCP_NODELAY
const tcp_notsent_lowat = SocketOption(25) // TCP_NOTSENT_LOWAT
const tcp_queue_seq = SocketOption(21) // TCP_QUEUE_SEQ
const tcp_quickack = SocketOption(12) // TCP_QUICKACK
const tcp_repair = SocketOption(19) // TCP_REPAIR
const tcp_repair_off = SocketOption(0) // TCP_REPAIR_OFF
const tcp_repair_off_no_wp = SocketOption(-1) // TCP_REPAIR_OFF_NO_WP
const tcp_repair_on = SocketOption(1) // TCP_REPAIR_ON
const tcp_repair_options = SocketOption(22) // TCP_REPAIR_OPTIONS
const tcp_repair_queue = SocketOption(20) // TCP_REPAIR_QUEUE
const tcp_repair_window = SocketOption(29) // TCP_REPAIR_WINDOW
const tcp_saved_syn = SocketOption(28) // TCP_SAVED_SYN
const tcp_save_syn = SocketOption(27) // TCP_SAVE_SYN
const tcp_syncnt = SocketOption(7) // TCP_SYNCNT
const tcp_s_data_in = SocketOption(4) // TCP_S_DATA_IN
const tcp_s_data_out = SocketOption(8) // TCP_S_DATA_OUT
const tcp_thin_dupack = SocketOption(17) // TCP_THIN_DUPACK
const tcp_thin_linear_timeouts = SocketOption(16) // TCP_THIN_LINEAR_TIMEOUTS
const tcp_timestamp = SocketOption(24) // TCP_TIMESTAMP
const tcp_tx_delay = SocketOption(37) // TCP_TX_DELAY
const tcp_ulp = SocketOption(31) // TCP_ULP
const tcp_user_timeout = SocketOption(18) // TCP_USER_TIMEOUT
const tcp_window_clamp = SocketOption(10) // TCP_WINDOW_CLAMP
const tcp_zerocopy_receive = SocketOption(35) // TCP_ZEROCOPY_RECEIVE
const udp_cork = SocketOption(1) // UDP_CORK
const udp_encap = SocketOption(100) // UDP_ENCAP
const udp_encap_espinudp = SocketOption(2) // UDP_ENCAP_ESPINUDP
const udp_encap_espinudp_non_ike = SocketOption(1) // UDP_ENCAP_ESPINUDP_NON_IKE
const udp_encap_gtp0 = SocketOption(4) // UDP_ENCAP_GTP0
const udp_encap_gtp1u = SocketOption(5) // UDP_ENCAP_GTP1U
const udp_encap_l2tpinudp = SocketOption(3) // UDP_ENCAP_L2TPINUDP
const udp_gro = SocketOption(104) // UDP_GRO
const udp_no_check6_rx = SocketOption(102) // UDP_NO_CHECK6_RX
const udp_no_check6_tx = SocketOption(101) // UDP_NO_CHECK6_TX
const udp_segment = SocketOption(103) // UDP_SEGMENT
const max_unix_path_len = $if linux {
108
} $else $if windows {
108
} $else {
104
}
fn addr_info #
fn addr_info(hints AddrInfoParams) ![]AddrInfo
addr_info translates the network addresses and services. This is a low-level wrapper around the getaddrinfo(3) C API.
fn find_network_interface #
fn find_network_interface(s string) !NetworkInterfaceId
find_network_interface returns the network interface index and name by provided string s. If network interface is not present the NetworkInterfaceNotFound error will be returned. See also index_to_name() and name_to_index().
Examples
assert netio.find_network_interface('eth0')!.index == 2
assert netio.find_network_interface('2')!.name == 'eth0'
fn index_to_name #
fn index_to_name(index u32) !string
index_to_name translates the network interface index to name e.g. 'eth0'. See also if_indextoname(3p).
fn name_info #
fn name_info(sa SocketAddr, params NameInfoParams) !(string, string)
name_info does address-to-name translation and returns the host and service names. See getnameinfo(3) for details.
fn name_to_index #
fn name_to_index(name string) !u32
name_to_index translates the network interface name to index. See also if_nametoindex(3p).
fn network_interfaces #
fn network_interfaces() ![]NetworkInterfaceId
network_interfaces returns an array with names and indexes of all network interfaces on system. See also if_nameindex(3p).
fn Shutdown.from #
fn Shutdown.from[W](input W) !Shutdown
fn Socket.new #
fn Socket.new(domain AddrFamily, st SocketType, proto Protocol) !Socket
Socket.new creates the new socket. See socket(7) and socket(3) for details.
fn SocketAddr.from_ptr #
fn SocketAddr.from_ptr(ptr voidptr, size isize) !SocketAddr
SocketAddr.from_ptr creates new socket address from specified pointer.
Note: Data is reused, not copied.
fn SocketAddr.from_ptr_copy #
fn SocketAddr.from_ptr_copy(ptr voidptr, size isize) !SocketAddr
SocketAddr.from_ptr_copy creates new socket address by copying data from specified pointer.
fn SocketAddr.new #
fn SocketAddr.new(af AddrFamily, size isize) SocketAddr
SocketAddr.new creates new instance of SocketAddr with specified address family and size.
This function allocates memory (zero filled) for the address, but does not initialize the address itself, you need to do that manually. The benefit is that you can create the any kind of socket address.
SocketAddr is a builder for sockaddr(3type) objects. Use this function only if you understand what you do. Using the write() method you must write the data for the desired socket address, ensuring the correct sizes of all types, the order of the fields in the struct, the byte order, and the total size of the struct. The sizes and byte order may vary by platform, so you'll need to keep an eye on that as well. A mistake while creating an address will crash your application. So this function is marked as unsafe.
The example below creates a sockaddr_in struct describing the loopback IPv4-address 127.0.0.1 with port number 1080. Note the comment in the example. This is a fragment of sockaddr_in(3type) manual page, which shows the target C struct. Summing the field sizes yields 8 bytes, but we need to allocate 16 bytes according to the <netinet/in.h>. Data must be padded to sockaddr struct size which is 16 bytes. Each field is then written in turn, from top to bottom. Keep in mind that two-byte address family field (sin_family in this case) is already written. According to the manual page, the address and port are written using the network (big-endian) byte order.
On *BSD, the first byte of the socket address stores the address size, and the second byte stores the address family.
Example
import encoding.binary
import netio
// struct sockaddr_in {
// sa_family_t sin_family; /* AF_INET */
// in_port_t sin_port; /* Port number */
// struct in_addr sin_addr; /* IPv4 address */
// };
//
// struct in_addr {
// in_addr_t s_addr;
// };
//
// typedef uint32_t in_addr_t;
// typedef uint16_t in_port_t;
mut sa := unsafe { netio.SocketAddr.new(netio.af_inet, 16) }
unsafe {
sa.write(binary.big_endian_get_u16(u16(1080)))!
sa.write([u8(127), 0, 0, 1])!
}
println(sa)
fn SocketAddr.new_ipv4 #
fn SocketAddr.new_ipv4(addr [4]u8, port u16) SocketAddr
SocketAddr.new_ipv4 creates new AF_INET socket address. addr must be set in network (big-endian) byte order.
fn SocketAddr.new_ipv6 #
fn SocketAddr.new_ipv6(addr [16]u8, port u16, params Inet6SocketAddrParams) SocketAddr
SocketAddr.new_ipv6 creates new AF_INET6 socket address. addr must be set in network (big-endian) byte order. Use find_network_interface() to get an integer scope_id from its string representation.
fn SocketAddr.new_unix #
fn SocketAddr.new_unix(path string) !SocketAddr
SocketAddr.new_unix creates new AF_UNIX socket address with given path. The path must fit in platform dependent max_unix_path_len const value.
type AddrFamily #
type AddrFamily = int
The socket address family type. See address_families(7).
type AddrInfoFlag #
type AddrInfoFlag = int
Flag type for addr_info(). See getaddrinfo(3) for details.
type MsgFlag #
type MsgFlag = int
Type for recv, recvfrom, recvmsg, send, sendto, sendmsg flags.
type NameInfoFlag #
type NameInfoFlag = int
Flag type for name_info(). See getnameinfo(3) for details.
type Protocol #
type Protocol = int
The protocol number. See protocols(5).
type SocketAddrStorage #
type SocketAddrStorage = C.sockaddr_storage
SocketAddrStorage represents the sockaddr_storage struct which can be used to store any socket address. See sockaddr(3type) for details.
type SocketLevel #
type SocketLevel = int
The socket level type.
type SocketOption #
type SocketOption = int
The socket option type.
type SocketType #
type SocketType = int
The socket type.
enum Shutdown #
enum Shutdown {
read
write
read_and_write
}
Socket shutdown modes. See shutdown(3p) for details.
struct AddrInfo #
struct AddrInfo {
pub:
flags AddrInfoFlag
family AddrFamily
socktype SocketType
protocol Protocol
addr SocketAddr
canonical string
}
AddrInfo represents the addrinfo struct.
struct AddrInfoParams #
struct AddrInfoParams {
pub:
node ?string
service ?string
family AddrFamily = af_unspec
socktype SocketType
protocol Protocol
flags AddrInfoFlag
}
struct Inet6SocketAddrParams #
struct Inet6SocketAddrParams {
pub:
flow_info u32
scope_id u32
}
struct NameInfoParams #
struct NameInfoParams {
pub:
flags NameInfoFlag
}
struct NetworkInterfaceId #
struct NetworkInterfaceId {
pub:
index u32
name string
}
struct NetworkInterfaceNotFound #
struct NetworkInterfaceNotFound {
Error
pub:
index u32
name string
}
fn (NetworkInterfaceNotFound) msg #
fn (e NetworkInterfaceNotFound) msg() string
msg returns the error message.
struct Socket #
struct Socket {
pub:
fd int = -1
}
fn (Socket) type #
fn (s Socket) type() !SocketType
type reports the actual socket type. Look for sock_* constants.
fn (Socket) connect #
fn (s Socket) connect(addr SocketAddr) !
connect connects a socket. See connect(3p) for details.
fn (Socket) bind #
fn (s Socket) bind(addr SocketAddr) !
bind binds a name to a socket. See bind(3p) for details.
fn (Socket) listen #
fn (s Socket) listen(backlog int) !
listen starts listening for socket connections and limit the queue of incoming connections. See listen(3p) for details.
fn (Socket) accept #
fn (s Socket) accept() !(Socket, SocketAddr)
accept accepts a new connection on a socket. The return values are the new socket connected to remote and the remote socket address. See accept(3p) for details.
fn (Socket) accept_addr #
fn (s Socket) accept_addr(mut addr SocketAddr) !Socket
accept_addr accepts a new connection on a socket. This function does exactly the same as accept, but allows you to specify your own sockaddr instance instead of creating a new one on each call.
Example
import netio
addr := netio.SocketAddr.new_ipv6([16]u8{}, 9999) // [::]:9999
mut socket := netio.Socket.new(netio.af_inet6, netio.sock_stream, 0)!
socket.set_option(netio.sol_socket, netio.so_reuseaddr, 1)!
socket.set_option(netio.ipproto_ipv6, netio.ipv6_v6only, 0)!
socket.bind(addr)!
socket.listen(10)!
println('Listening on tcp://${addr}, try connect to it via telnet...')
mut peer_addr := unsafe { netio.SocketAddr.new(netio.af_unspec, sizeof(netio.SocketAddrStorage)) }
new_socket := socket.accept_addr(mut peer_addr)!
dump(peer_addr)
dump(peer_addr.family()) // 10 (AF_INET6)
dump(peer_addr.size()) // 28
new_socket.close()!
socket.close()!
fn (Socket) accept_no_addr #
fn (s Socket) accept_no_addr() !Socket
accept_no_addr accepts a new connection on a socket. Unlike accept and accept_addr this function does not provide peer address.
fn (Socket) set_option #
fn (s Socket) set_option[T](level SocketLevel, option SocketOption, value T) !
set_option sets the socket option. See socket(7) and setsockopt(3p) for details.
fn (Socket) get_option #
fn (s Socket) get_option[T](level SocketLevel, option SocketOption) !T
get_option gets the socket option. See socket(7) and getsockopt(3p) for details.
Example
import netio
mut socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
socket.set_option(netio.sol_socket, netio.so_reuseaddr, 1)!
assert socket.get_option[int](netio.sol_socket, netio.so_reuseaddr)! == 1
fn (Socket) recv #
fn (s Socket) recv(mut buf []u8, flags MsgFlag) !int
recv receives a message from a connected socket. See recv(3p) for details.
fn (Socket) recv_from #
fn (s Socket) recv_from(mut buf []u8, flags MsgFlag) !(int, SocketAddr)
recv_from receives a message from a connected socket and returns the number of bytes read and the remote peer address. See recvfrom(3p) for details.
fn (Socket) send #
fn (s Socket) send(buf []u8, flags MsgFlag) !int
send sends a message on socket. See send(3p) for details.
fn (Socket) send_to #
fn (s Socket) send_to(buf []u8, dst SocketAddr, flags MsgFlag) !int
send_to sends a message on socket using the dst socket address as destination instead of the socket peer address. See sendto(3p) for details.
fn (Socket) shutdown #
fn (s Socket) shutdown(how Shutdown) !
shutdown shut downs socket send and receive operations. See shutodwn(3p) for details.
fn (Socket) close #
fn (s Socket) close() !
close closes the socket. See close(3p) for details.
struct SocketAddr #
struct SocketAddr {
mut:
data &u8 = unsafe { nil }
len int
pos int
}
fn (SocketAddr) family #
fn (a SocketAddr) family() AddrFamily
family returns the socket address family.
Note: It returns 0 if socket address is nil, see also is_empty().
fn (SocketAddr) is_empty #
fn (a SocketAddr) is_empty() bool
is_empty returns true if socket address is unspecified: the data pointer is nil or socket address data is zero. Empty address cannot be used in bind() and connect() calls.
fn (SocketAddr) write #
fn (mut a SocketAddr) write(buf []u8) !int
write writes buf.len bytes of data to the socket address memory area and returns the number of bytes written. write will return an error if the socket address pointer is nil or the buffer length, including the length of the data already written, exceeds the size of the socket address.
fn (SocketAddr) ptr #
fn (a SocketAddr) ptr() voidptr
ptr returns the pointer to sockaddr data.
fn (SocketAddr) size #
fn (a SocketAddr) size() u32
size reports the size of sockaddr data.
fn (SocketAddr) str #
fn (a SocketAddr) str() string
str returns the string representation of socket address. Only AF_UNIX, AF_INET, and AF_INET6 are supported. str will return a string consisting of the address and port number separated by a colon, or the absolute path to the socket file. The IPv6 address will be returned in expanded form and enclosed in square brackets. For all other address families, str will return a string of the form SocketAddr(0x00000000) with the socket address data pointer in brackets.
Note: See also name_info().
- README
- Constants
- fn addr_info
- fn find_network_interface
- fn index_to_name
- fn name_info
- fn name_to_index
- fn network_interfaces
- fn Shutdown.from
- fn Socket.new
- fn SocketAddr.from_ptr
- fn SocketAddr.from_ptr_copy
- fn SocketAddr.new
- fn SocketAddr.new_ipv4
- fn SocketAddr.new_ipv6
- fn SocketAddr.new_unix
- type AddrFamily
- type AddrInfoFlag
- type MsgFlag
- type NameInfoFlag
- type Protocol
- type SocketAddrStorage
- type SocketLevel
- type SocketOption
- type SocketType
- enum Shutdown
- struct AddrInfo
- struct AddrInfoParams
- struct Inet6SocketAddrParams
- struct NameInfoParams
- struct NetworkInterfaceId
- struct NetworkInterfaceNotFound
- struct Socket
- struct SocketAddr