1 /*	$NetBSD: getaddrinfo_hostspec.c,v 1.1.1.1 2011/04/13 18:15:41 elric Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <config.h>
37 
38 #include <krb5/roken.h>
39 
40 /* getaddrinfo via string specifying host and port */
41 
42 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
43 roken_getaddrinfo_hostspec2(const char *hostspec,
44 			    int socktype,
45 			    int port,
46 			    struct addrinfo **ai)
47 {
48     const char *p;
49     char portstr[NI_MAXSERV];
50     char host[MAXHOSTNAMELEN];
51     struct addrinfo hints;
52     int hostspec_len;
53 
54     struct hst {
55 	const char *prefix;
56 	int socktype;
57 	int protocol;
58 	int port;
59     } *hstp, hst[] = {
60 	{ "http://", SOCK_STREAM, IPPROTO_TCP, 80 },
61 	{ "http/", SOCK_STREAM, IPPROTO_TCP, 80 },
62 	{ "tcp/", SOCK_STREAM, IPPROTO_TCP },
63 	{ "udp/", SOCK_DGRAM, IPPROTO_UDP },
64 	{ NULL }
65     };
66 
67     memset(&hints, 0, sizeof(hints));
68 
69     hints.ai_socktype = socktype;
70 
71     for(hstp = hst; hstp->prefix; hstp++) {
72 	if(strncmp(hostspec, hstp->prefix, strlen(hstp->prefix)) == 0) {
73 	    hints.ai_socktype = hstp->socktype;
74 	    hints.ai_protocol = hstp->protocol;
75 	    if(port == 0)
76 		port = hstp->port;
77 	    hostspec += strlen(hstp->prefix);
78 	    break;
79 	}
80     }
81 
82     p = strchr (hostspec, ':');
83     if (p != NULL) {
84 	char *end;
85 
86 	port = strtol (p + 1, &end, 0);
87 	hostspec_len = p - hostspec;
88     } else {
89 	hostspec_len = strlen(hostspec);
90     }
91     snprintf (portstr, sizeof(portstr), "%u", port);
92 
93     snprintf (host, sizeof(host), "%.*s", hostspec_len, hostspec);
94     return getaddrinfo (host, portstr, &hints, ai);
95 }
96 
97 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
98 roken_getaddrinfo_hostspec(const char *hostspec,
99 			   int port,
100 			   struct addrinfo **ai)
101 {
102     return roken_getaddrinfo_hostspec2(hostspec, 0, port, ai);
103 }
104