Path: blob/main/crypto/heimdal/lib/roken/getaddrinfo_hostspec.c
39507 views
/*1* Copyright (c) 2000 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <config.h>3435#include "roken.h"3637/* getaddrinfo via string specifying host and port */3839ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL40roken_getaddrinfo_hostspec2(const char *hostspec,41int socktype,42int port,43struct addrinfo **ai)44{45const char *p;46char portstr[NI_MAXSERV];47char host[MAXHOSTNAMELEN];48struct addrinfo hints;49int hostspec_len;5051struct hst {52const char *prefix;53int socktype;54int protocol;55int port;56} *hstp, hst[] = {57{ "http://", SOCK_STREAM, IPPROTO_TCP, 80 },58{ "http/", SOCK_STREAM, IPPROTO_TCP, 80 },59{ "tcp/", SOCK_STREAM, IPPROTO_TCP, 0 },60{ "udp/", SOCK_DGRAM, IPPROTO_UDP, 0 },61{ NULL, 0, 0, 0 }62};6364memset(&hints, 0, sizeof(hints));6566hints.ai_socktype = socktype;6768for(hstp = hst; hstp->prefix; hstp++) {69if(strncmp(hostspec, hstp->prefix, strlen(hstp->prefix)) == 0) {70hints.ai_socktype = hstp->socktype;71hints.ai_protocol = hstp->protocol;72if(port == 0)73port = hstp->port;74hostspec += strlen(hstp->prefix);75break;76}77}7879p = strchr (hostspec, ':');80if (p != NULL) {81char *end;8283port = strtol (p + 1, &end, 0);84hostspec_len = p - hostspec;85} else {86hostspec_len = strlen(hostspec);87}88snprintf (portstr, sizeof(portstr), "%u", port);8990snprintf (host, sizeof(host), "%.*s", hostspec_len, hostspec);91return getaddrinfo (host, portstr, &hints, ai);92}9394ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL95roken_getaddrinfo_hostspec(const char *hostspec,96int port,97struct addrinfo **ai)98{99return roken_getaddrinfo_hostspec2(hostspec, 0, port, ai);100}101102103