written from scratch based on RFC1035