1162852Sdes/*
257429Smarkm * Copyright (c) 2009 Kungliga Tekniska H�gskolan
357429Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
457429Smarkm * All rights reserved.
557429Smarkm *
657429Smarkm * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
757429Smarkm *
876259Sgreen * Redistribution and use in source and binary forms, with or without
965668Skris * modification, are permitted provided that the following conditions
1065668Skris * are met:
1165668Skris *
1265668Skris * 1. Redistributions of source code must retain the above copyright
1365668Skris *    notice, this list of conditions and the following disclaimer.
1457429Smarkm *
1557429Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1657429Smarkm *    notice, this list of conditions and the following disclaimer in the
1757429Smarkm *    documentation and/or other materials provided with the distribution.
18162852Sdes *
19162852Sdes * 3. Neither the name of the Institute nor the names of its contributors
20162852Sdes *    may be used to endorse or promote products derived from this software
21162852Sdes *    without specific prior written permission.
22162852Sdes *
23162852Sdes * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2476259Sgreen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2576259Sgreen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2657429Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2757429Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2857429Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2957429Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3076259Sgreen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3176259Sgreen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3276259Sgreen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3376259Sgreen * SUCH DAMAGE.
3476259Sgreen */
3557429Smarkm
3676259Sgreen#include <stdio.h>
3757429Smarkm#include <stdlib.h>
3857429Smarkm#include <krb5-types.h>
3957429Smarkm#include <heim-ipc.h>
4057429Smarkm#include <getarg.h>
41162852Sdes#include <roken.h>
4257429Smarkm
43162852Sdesstatic int help_flag;
44162852Sdesstatic int version_flag;
45162852Sdes
46162852Sdesstatic struct getargs args[] = {
47162852Sdes    {	"help",		'h',	arg_flag,   &help_flag },
48162852Sdes    {	"version",	'v',	arg_flag,   &version_flag }
49162852Sdes};
50162852Sdes
51162852Sdesstatic int num_args = sizeof(args) / sizeof(args[0]);
52162852Sdes
53162852Sdesstatic void
54162852Sdesusage(int ret)
55162852Sdes{
56162852Sdes    arg_printusage (args, num_args, NULL, "");
57162852Sdes    exit (ret);
58162852Sdes}
5957429Smarkm
60162852Sdesstatic void
6157429Smarkmtest_service(void *ctx, const heim_idata *req,
6276259Sgreen	     const heim_icred cred,
6376259Sgreen	     heim_ipc_complete complete,
64162852Sdes	     heim_sipc_call cctx)
65162852Sdes{
6657429Smarkm    heim_idata rep;
6776259Sgreen    printf("got request\n");
6876259Sgreen    rep.length = 3;
6976259Sgreen    rep.data = strdup("hej");
7057429Smarkm    (*complete)(cctx, 0, &rep);
71162852Sdes}
72162852Sdes
7357429Smarkm
7457429Smarkmstatic void
7557429Smarkmsetup_sockets(void)
7660573Skris{
7757429Smarkm    struct addrinfo hints, *res, *res0;
7857429Smarkm    int ret, s;
7957429Smarkm    heim_sipc u;
8057429Smarkm
8157429Smarkm    memset(&hints, 0, sizeof(hints));
8257429Smarkm    hints.ai_family = PF_UNSPEC;
8357429Smarkm    hints.ai_socktype = SOCK_STREAM;
8457429Smarkm    hints.ai_flags = AI_PASSIVE;
8557429Smarkm    ret = getaddrinfo(NULL, "8080", &hints, &res0);
8657429Smarkm    if (ret)
8792555Sdes	errx(1, "%s", gai_strerror(ret));
8876259Sgreen
8957429Smarkm    for (res = res0; res ; res = res->ai_next) {
9092555Sdes	s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
9176259Sgreen	if (s < 0) {
9257429Smarkm	    warn("socket");
9357429Smarkm	    continue;
9457429Smarkm	}
95162852Sdes	socket_set_reuseaddr(s, 1);
96162852Sdes	socket_set_ipv6only(s, 1);
97162852Sdes
98162852Sdes	if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
99162852Sdes	    warn("bind");
100162852Sdes	    close(s);
101162852Sdes	    continue;
102162852Sdes	}
103162852Sdes	listen(s, 5);
104162852Sdes	ret = heim_sipc_stream_listener(s, HEIM_SIPC_TYPE_HTTP,
105162852Sdes					test_service, NULL, &u);
106162852Sdes	if (ret)
107162852Sdes	    errx(1, "heim_sipc_stream_listener: %d", ret);
108162852Sdes    }
109162852Sdes    freeaddrinfo(res0);
110162852Sdes}
111
112
113int
114main(int argc, char **argv)
115{
116    int optidx = 0;
117
118    setprogname(argv[0]);
119
120    if (getarg(args, num_args, argc, argv, &optidx))
121	usage(1);
122
123    if (help_flag)
124	usage(0);
125
126    if (version_flag) {
127	print_version(NULL);
128	exit(0);
129    }
130
131    setup_sockets();
132
133    heim_ipc_main();
134
135    return 0;
136}
137