github.com/iDigitalFlame/xmt@v0.5.4/com/c_no_compat.go (about) 1 //go:build go1.18 2 // +build go1.18 3 4 // Copyright (C) 2020 - 2023 iDigitalFlame 5 // 6 // This program is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with this program. If not, see <https://www.gnu.org/licenses/>. 18 // 19 20 package com 21 22 import ( 23 "net" 24 "net/netip" 25 "syscall" 26 ) 27 28 var emptyAddr netip.AddrPort 29 30 type udpAddr netip.AddrPort 31 type udpSockInternal interface { 32 WriteToUDPAddrPort([]byte, netip.AddrPort) (int, error) 33 ReadFromUDPAddrPort([]byte) (int, netip.AddrPort, error) 34 } 35 36 func (u udpAddr) IsValid() bool { 37 return netip.AddrPort(u).IsValid() 38 } 39 func (u udpAddr) String() string { 40 // NOTE(dij): This causes IPv4 addresses to weirdly be wrapped as an IPv6 41 // address. This doesn't seem to affect how it works on IPv4, but 42 // we'll watch it. It only makes IPv4 addresses print out as IPv6 43 // formatted addresses. 44 return netip.AddrPort(u).String() 45 } 46 func (u *udpCompat) ReadPacket(p []byte) (int, udpAddr, error) { 47 n, a, err := u.udpSock.ReadFromUDPAddrPort(p) 48 return n, udpAddr(a), err 49 } 50 func (u *udpCompat) WritePacket(p []byte, a udpAddr) (int, error) { 51 return u.udpSock.WriteToUDPAddrPort(p, netip.AddrPort(a)) 52 } 53 func (i *ipPacketConn) ReadFromUDPAddrPort(b []byte) (int, netip.AddrPort, error) { 54 // NOTE(dij): Have to add this as there's no support for the netip 55 // package for IPConns. 56 n, a, err := i.ReadFrom(b) 57 if a == nil { 58 return n, emptyAddr, err 59 } 60 v, ok := a.(*net.IPAddr) 61 if !ok { 62 if err != nil { 63 return n, emptyAddr, err 64 } 65 return n, emptyAddr, syscall.EINVAL 66 } 67 x, _ := netip.AddrFromSlice(v.IP) 68 return n, netip.AddrPortFrom(x, 0), err 69 } 70 func (i *ipPacketConn) WriteToUDPAddrPort(b []byte, a netip.AddrPort) (int, error) { 71 return i.WriteTo(b, &net.IPAddr{IP: a.Addr().AsSlice()}) 72 }