1226031Sstas/* 2226031Sstas * Copyright (c) 2009 Kungliga Tekniska H�gskolan 3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden). 4226031Sstas * All rights reserved. 5226031Sstas * 6226031Sstas * Portions Copyright (c) 2009 Apple Inc. All rights reserved. 7226031Sstas * 8226031Sstas * Redistribution and use in source and binary forms, with or without 9226031Sstas * modification, are permitted provided that the following conditions 10226031Sstas * are met: 11226031Sstas * 12226031Sstas * 1. Redistributions of source code must retain the above copyright 13226031Sstas * notice, this list of conditions and the following disclaimer. 14226031Sstas * 15226031Sstas * 2. Redistributions in binary form must reproduce the above copyright 16226031Sstas * notice, this list of conditions and the following disclaimer in the 17226031Sstas * documentation and/or other materials provided with the distribution. 18226031Sstas * 19226031Sstas * 3. Neither the name of the Institute nor the names of its contributors 20226031Sstas * may be used to endorse or promote products derived from this software 21226031Sstas * without specific prior written permission. 22226031Sstas * 23226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26226031Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33226031Sstas * SUCH DAMAGE. 34226031Sstas */ 35226031Sstas 36226031Sstas#include <stdio.h> 37226031Sstas#include <stdlib.h> 38226031Sstas#include <krb5-types.h> 39226031Sstas#include <heim-ipc.h> 40226031Sstas#include <getarg.h> 41226031Sstas#include <roken.h> 42226031Sstas 43226031Sstasstatic int help_flag; 44226031Sstasstatic int version_flag; 45226031Sstas 46226031Sstasstatic struct getargs args[] = { 47226031Sstas { "help", 'h', arg_flag, &help_flag }, 48226031Sstas { "version", 'v', arg_flag, &version_flag } 49226031Sstas}; 50226031Sstas 51226031Sstasstatic int num_args = sizeof(args) / sizeof(args[0]); 52226031Sstas 53226031Sstasstatic void 54226031Sstasusage(int ret) 55226031Sstas{ 56226031Sstas arg_printusage (args, num_args, NULL, ""); 57226031Sstas exit (ret); 58226031Sstas} 59226031Sstas 60226031Sstasstatic void 61226031Sstastest_service(void *ctx, const heim_idata *req, 62226031Sstas const heim_icred cred, 63226031Sstas heim_ipc_complete complete, 64226031Sstas heim_sipc_call cctx) 65226031Sstas{ 66226031Sstas heim_idata rep; 67226031Sstas printf("got request\n"); 68226031Sstas rep.length = 3; 69226031Sstas rep.data = strdup("hej"); 70226031Sstas (*complete)(cctx, 0, &rep); 71226031Sstas} 72226031Sstas 73226031Sstas 74226031Sstasstatic void 75226031Sstassetup_sockets(void) 76226031Sstas{ 77226031Sstas struct addrinfo hints, *res, *res0; 78226031Sstas int ret, s; 79226031Sstas heim_sipc u; 80226031Sstas 81226031Sstas memset(&hints, 0, sizeof(hints)); 82226031Sstas hints.ai_family = PF_UNSPEC; 83226031Sstas hints.ai_socktype = SOCK_STREAM; 84226031Sstas hints.ai_flags = AI_PASSIVE; 85226031Sstas ret = getaddrinfo(NULL, "8080", &hints, &res0); 86226031Sstas if (ret) 87226031Sstas errx(1, "%s", gai_strerror(ret)); 88226031Sstas 89226031Sstas for (res = res0; res ; res = res->ai_next) { 90226031Sstas s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 91226031Sstas if (s < 0) { 92226031Sstas warn("socket"); 93226031Sstas continue; 94226031Sstas } 95226031Sstas socket_set_reuseaddr(s, 1); 96226031Sstas socket_set_ipv6only(s, 1); 97226031Sstas 98226031Sstas if (bind(s, res->ai_addr, res->ai_addrlen) < 0) { 99226031Sstas warn("bind"); 100226031Sstas close(s); 101226031Sstas continue; 102226031Sstas } 103226031Sstas listen(s, 5); 104226031Sstas ret = heim_sipc_stream_listener(s, HEIM_SIPC_TYPE_HTTP, 105226031Sstas test_service, NULL, &u); 106226031Sstas if (ret) 107226031Sstas errx(1, "heim_sipc_stream_listener: %d", ret); 108226031Sstas } 109226031Sstas freeaddrinfo(res0); 110226031Sstas} 111226031Sstas 112226031Sstas 113226031Sstasint 114226031Sstasmain(int argc, char **argv) 115226031Sstas{ 116226031Sstas int optidx = 0; 117226031Sstas 118226031Sstas setprogname(argv[0]); 119226031Sstas 120226031Sstas if (getarg(args, num_args, argc, argv, &optidx)) 121226031Sstas usage(1); 122226031Sstas 123226031Sstas if (help_flag) 124226031Sstas usage(0); 125226031Sstas 126226031Sstas if (version_flag) { 127226031Sstas print_version(NULL); 128226031Sstas exit(0); 129226031Sstas } 130226031Sstas 131226031Sstas setup_sockets(); 132226031Sstas 133226031Sstas heim_ipc_main(); 134226031Sstas 135226031Sstas return 0; 136226031Sstas} 137