1177633Sdfr/*
2177633Sdfr * Copyright (c) 2009 Kungliga Tekniska H�gskolan
3177633Sdfr * (Royal Institute of Technology, Stockholm, Sweden).
4177633Sdfr * All rights reserved.
5177633Sdfr *
6177633Sdfr * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7177633Sdfr *
8177633Sdfr * Redistribution and use in source and binary forms, with or without
9177633Sdfr * modification, are permitted provided that the following conditions
10177633Sdfr * are met:
11177633Sdfr *
12177633Sdfr * 1. Redistributions of source code must retain the above copyright
13177633Sdfr *    notice, this list of conditions and the following disclaimer.
14177633Sdfr *
15177633Sdfr * 2. Redistributions in binary form must reproduce the above copyright
16177633Sdfr *    notice, this list of conditions and the following disclaimer in the
17177633Sdfr *    documentation and/or other materials provided with the distribution.
18177633Sdfr *
19177633Sdfr * 3. Neither the name of the Institute nor the names of its contributors
20177633Sdfr *    may be used to endorse or promote products derived from this software
21177633Sdfr *    without specific prior written permission.
22177633Sdfr *
23177633Sdfr * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24177633Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25177633Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26177633Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27177633Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28177633Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29177633Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30177633Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31177633Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32177633Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33177633Sdfr * SUCH DAMAGE.
34177633Sdfr */
35177633Sdfr
36177633Sdfr#include <stdio.h>
37177633Sdfr#include <stdlib.h>
38177633Sdfr#include <krb5-types.h>
39177633Sdfr#include <heim-ipc.h>
40177633Sdfr#include <getarg.h>
41177633Sdfr#include <roken.h>
42177633Sdfr
43177633Sdfrstatic int help_flag;
44177633Sdfrstatic int version_flag;
45177633Sdfr
46177633Sdfrstatic struct getargs args[] = {
47177633Sdfr    {	"help",		'h',	arg_flag,   &help_flag },
48177633Sdfr    {	"version",	'v',	arg_flag,   &version_flag }
49177633Sdfr};
50177633Sdfr
51177633Sdfrstatic int num_args = sizeof(args) / sizeof(args[0]);
52177633Sdfr
53177633Sdfrstatic void
54177633Sdfrusage(int ret)
55177633Sdfr{
56177633Sdfr    arg_printusage (args, num_args, NULL, "");
57177633Sdfr    exit (ret);
58177633Sdfr}
59177633Sdfr
60177633Sdfrstatic void
61177633Sdfrtest_service(void *ctx, const heim_idata *req,
62177633Sdfr	     const heim_icred cred,
63177633Sdfr	     heim_ipc_complete complete,
64177633Sdfr	     heim_sipc_call cctx)
65177633Sdfr{
66177633Sdfr    heim_idata rep;
67184588Sdfr    printf("got request\n");
68177633Sdfr    rep.length = 3;
69177633Sdfr    rep.data = strdup("hej");
70244008Srmacklem    (*complete)(cctx, 0, &rep);
71177633Sdfr}
72177633Sdfr
73177633Sdfr
74196503Szecstatic void
75196503Szecsetup_sockets(void)
76196503Szec{
77184588Sdfr    struct addrinfo hints, *res, *res0;
78177633Sdfr    int ret, s;
79177633Sdfr    heim_sipc u;
80177685Sdfr
81244008Srmacklem    memset(&hints, 0, sizeof(hints));
82177633Sdfr    hints.ai_family = PF_UNSPEC;
83177633Sdfr    hints.ai_socktype = SOCK_STREAM;
84177633Sdfr    hints.ai_flags = AI_PASSIVE;
85177633Sdfr    ret = getaddrinfo(NULL, "8080", &hints, &res0);
86177633Sdfr    if (ret)
87177633Sdfr	errx(1, "%s", gai_strerror(ret));
88180025Sdfr
89184588Sdfr    for (res = res0; res ; res = res->ai_next) {
90177633Sdfr	s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
91177633Sdfr	if (s < 0) {
92177633Sdfr	    warn("socket");
93177633Sdfr	    continue;
94184588Sdfr	}
95177633Sdfr	socket_set_reuseaddr(s, 1);
96177633Sdfr	socket_set_ipv6only(s, 1);
97193272Sjhb
98177633Sdfr	if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
99177633Sdfr	    warn("bind");
100177633Sdfr	    close(s);
101177633Sdfr	    continue;
102177633Sdfr	}
103177633Sdfr	listen(s, 5);
104184588Sdfr	ret = heim_sipc_stream_listener(s, HEIM_SIPC_TYPE_HTTP,
105177633Sdfr					test_service, NULL, &u);
106177633Sdfr	if (ret)
107177633Sdfr	    errx(1, "heim_sipc_stream_listener: %d", ret);
108177633Sdfr    }
109193437Srmacklem    freeaddrinfo(res0);
110193437Srmacklem}
111177633Sdfr
112177633Sdfr
113177633Sdfrint
114177633Sdfrmain(int argc, char **argv)
115177633Sdfr{
116177633Sdfr    int optidx = 0;
117177633Sdfr
118177633Sdfr    setprogname(argv[0]);
119177633Sdfr
120177633Sdfr    if (getarg(args, num_args, argc, argv, &optidx))
121177633Sdfr	usage(1);
122177633Sdfr
123177633Sdfr    if (help_flag)
124177633Sdfr	usage(0);
125177633Sdfr
126177633Sdfr    if (version_flag) {
127177633Sdfr	print_version(NULL);
128177633Sdfr	exit(0);
129177633Sdfr    }
130177633Sdfr
131177633Sdfr    setup_sockets();
132177633Sdfr
133177633Sdfr    heim_ipc_main();
134221127Srmacklem
135221127Srmacklem    return 0;
136177633Sdfr}
137177633Sdfr