1*d3273b5bSchristos /* $NetBSD: ts-http.c,v 1.2 2017/01/28 21:31:48 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 2009 Kungliga Tekniska H�gskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9ca1c9b0cSelric *
10ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
11ca1c9b0cSelric * modification, are permitted provided that the following conditions
12ca1c9b0cSelric * are met:
13ca1c9b0cSelric *
14ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
15ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
16ca1c9b0cSelric *
17ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
18ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
19ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
20ca1c9b0cSelric *
21ca1c9b0cSelric * 3. Neither the name of the Institute nor the names of its contributors
22ca1c9b0cSelric * may be used to endorse or promote products derived from this software
23ca1c9b0cSelric * without specific prior written permission.
24ca1c9b0cSelric *
25ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ca1c9b0cSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ca1c9b0cSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ca1c9b0cSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ca1c9b0cSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ca1c9b0cSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ca1c9b0cSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ca1c9b0cSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ca1c9b0cSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ca1c9b0cSelric * SUCH DAMAGE.
36ca1c9b0cSelric */
37ca1c9b0cSelric
38ca1c9b0cSelric #include <stdio.h>
39ca1c9b0cSelric #include <stdlib.h>
40ca1c9b0cSelric #include <krb5/krb5-types.h>
41ca1c9b0cSelric #include <heim-ipc.h>
42ca1c9b0cSelric #include <krb5/getarg.h>
43ca1c9b0cSelric #include <krb5/roken.h>
44ca1c9b0cSelric
45ca1c9b0cSelric static int help_flag;
46ca1c9b0cSelric static int version_flag;
47ca1c9b0cSelric
48ca1c9b0cSelric static struct getargs args[] = {
49b9d004c6Schristos { "help", 'h', arg_flag, &help_flag, NULL, NULL },
50b9d004c6Schristos { "version", 'v', arg_flag, &version_flag, NULL, NULL }
51ca1c9b0cSelric };
52ca1c9b0cSelric
53ca1c9b0cSelric static int num_args = sizeof(args) / sizeof(args[0]);
54ca1c9b0cSelric
55ca1c9b0cSelric static void
usage(int ret)56ca1c9b0cSelric usage(int ret)
57ca1c9b0cSelric {
58ca1c9b0cSelric arg_printusage (args, num_args, NULL, "");
59ca1c9b0cSelric exit (ret);
60ca1c9b0cSelric }
61ca1c9b0cSelric
62ca1c9b0cSelric static void
test_service(void * ctx,const heim_idata * req,const heim_icred cred,heim_ipc_complete complete,heim_sipc_call cctx)63ca1c9b0cSelric test_service(void *ctx, const heim_idata *req,
64ca1c9b0cSelric const heim_icred cred,
65ca1c9b0cSelric heim_ipc_complete complete,
66ca1c9b0cSelric heim_sipc_call cctx)
67ca1c9b0cSelric {
68ca1c9b0cSelric heim_idata rep;
69ca1c9b0cSelric printf("got request\n");
70ca1c9b0cSelric rep.length = 3;
71ca1c9b0cSelric rep.data = strdup("hej");
72ca1c9b0cSelric (*complete)(cctx, 0, &rep);
73ca1c9b0cSelric }
74ca1c9b0cSelric
75ca1c9b0cSelric
76ca1c9b0cSelric static void
setup_sockets(void)77ca1c9b0cSelric setup_sockets(void)
78ca1c9b0cSelric {
79ca1c9b0cSelric struct addrinfo hints, *res, *res0;
80ca1c9b0cSelric int ret, s;
81ca1c9b0cSelric heim_sipc u;
82ca1c9b0cSelric
83ca1c9b0cSelric memset(&hints, 0, sizeof(hints));
84ca1c9b0cSelric hints.ai_family = PF_UNSPEC;
85ca1c9b0cSelric hints.ai_socktype = SOCK_STREAM;
86ca1c9b0cSelric hints.ai_flags = AI_PASSIVE;
87ca1c9b0cSelric ret = getaddrinfo(NULL, "8080", &hints, &res0);
88ca1c9b0cSelric if (ret)
89ca1c9b0cSelric errx(1, "%s", gai_strerror(ret));
90ca1c9b0cSelric
91ca1c9b0cSelric for (res = res0; res ; res = res->ai_next) {
92ca1c9b0cSelric s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
93ca1c9b0cSelric if (s < 0) {
94ca1c9b0cSelric warn("socket");
95ca1c9b0cSelric continue;
96ca1c9b0cSelric }
97ca1c9b0cSelric socket_set_reuseaddr(s, 1);
98ca1c9b0cSelric socket_set_ipv6only(s, 1);
99ca1c9b0cSelric
100ca1c9b0cSelric if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
101ca1c9b0cSelric warn("bind");
102ca1c9b0cSelric close(s);
103ca1c9b0cSelric continue;
104ca1c9b0cSelric }
105ca1c9b0cSelric listen(s, 5);
106ca1c9b0cSelric ret = heim_sipc_stream_listener(s, HEIM_SIPC_TYPE_HTTP,
107ca1c9b0cSelric test_service, NULL, &u);
108ca1c9b0cSelric if (ret)
109ca1c9b0cSelric errx(1, "heim_sipc_stream_listener: %d", ret);
110ca1c9b0cSelric }
111ca1c9b0cSelric freeaddrinfo(res0);
112ca1c9b0cSelric }
113ca1c9b0cSelric
114ca1c9b0cSelric
115ca1c9b0cSelric int
main(int argc,char ** argv)116ca1c9b0cSelric main(int argc, char **argv)
117ca1c9b0cSelric {
118ca1c9b0cSelric int optidx = 0;
119ca1c9b0cSelric
120ca1c9b0cSelric setprogname(argv[0]);
121ca1c9b0cSelric
122ca1c9b0cSelric if (getarg(args, num_args, argc, argv, &optidx))
123ca1c9b0cSelric usage(1);
124ca1c9b0cSelric
125ca1c9b0cSelric if (help_flag)
126ca1c9b0cSelric usage(0);
127ca1c9b0cSelric
128ca1c9b0cSelric if (version_flag) {
129ca1c9b0cSelric print_version(NULL);
130ca1c9b0cSelric exit(0);
131ca1c9b0cSelric }
132ca1c9b0cSelric
133ca1c9b0cSelric setup_sockets();
134ca1c9b0cSelric
135ca1c9b0cSelric heim_ipc_main();
136ca1c9b0cSelric
137ca1c9b0cSelric return 0;
138ca1c9b0cSelric }
139