packageutilsimport("encoding/binary""fmt""log/slog""net""strconv""strings")// SanitizePort removes the port description from the port string// and returns the port number as an uint16.funcSanitizePort(portstring,logger*slog.Logger)uint16{ifport==""{logger.Error("port is empty")return0}p,err:=strconv.Atoi(strings.Split(port,"(")[0])iferr!=nil{logger.Error("failed to convert port to integer","error",err)return0}ifp<1||p>65535{logger.Error("port is out of range","port",port)return0}returnuint16(p)}// SplitAddr splits a network address into host and sanitized port.funcSplitAddr(addrstring,logger*slog.Logger)(string,uint16){host,port,err:=net.SplitHostPort(addr)iferr!=nil{logger.Error("failed to split host and port","error",err)return"",0}returnhost,SanitizePort(port,logger)}// BuildAddress builds a network address from listen address and port.funcBuildAddress(listenAddrstring,portuint16)string{returnnet.JoinHostPort(listenAddr,strconv.Itoa(int(port)))}funcIntToIP(ipuint32)string{returnfmt.Sprintf("%d.%d.%d.%d",(ip>>24)&0xff,(ip>>16)&0xff,(ip>>8)&0xff,ip&0xff,)}funcIPToInt(ipstring)(uint32,error){parsed:=net.ParseIP(ip)ifparsed==nil{return0,fmt.Errorf("invalid IP address: %s",ip)}ipv4:=parsed.To4()ifipv4==nil{return0,fmt.Errorf("not an IPv4 address: %s",ip)}returnbinary.BigEndian.Uint32(ipv4),nil}